From b8635d4ea7c045bb18d9ebfe60d987c3cd532952 Mon Sep 17 00:00:00 2001 From: Evan Overman Date: Fri, 9 Jun 2023 19:32:09 -0700 Subject: [PATCH] better error handling --- src/main.c | 23 ++++++++++++++++------- src/sysinf.c | 42 ++++++++++++++++++++++++------------------ src/sysinf.h | 23 +++++++++++++++++------ 3 files changed, 57 insertions(+), 31 deletions(-) diff --git a/src/main.c b/src/main.c index 02d0ff4..14766c4 100644 --- a/src/main.c +++ b/src/main.c @@ -69,26 +69,35 @@ int main(int argc, char** argv) { // This is every variable outputted as part of the fetch. unsigned long stor_capacity, stor_used; + unsigned int longest_entry_len, mem_capacity_u; double stor_capacity_dec, stor_used_dec, mem_capacity; char * stor_unit, * mem_unit, * kern_version, * host_name, cpu_model[1024]; const char* pac_man = NULL; - unsigned int longest_entry_len; - if (get_cpu_model(cpu_model, sizeof cpu_model)) { - perror("Failed to retrieve cpu model."); - } - // Cut down utsname.release to be just the kernel version. struct utsname utsname; uname(&utsname); kern_version = strtok(utsname.release, "-"); host_name = utsname.nodename; - get_root_size(&stor_capacity, &stor_used); + if (get_cpu_model(cpu_model, sizeof cpu_model)) { + printf("failed to get cpu model\n"); + exit(EXIT_FAILURE); + } + + if (get_root_size(&stor_capacity, &stor_used)) { + printf("failed to stat root filesystem\n"); + exit(EXIT_FAILURE); + } + + if (get_memory_capacity(&mem_capacity_u)) { + printf("failed to get memory capacity\n"); + exit(EXIT_FAILURE); + } stor_capacity_dec = (double)stor_capacity; stor_used_dec = (double)stor_used; - mem_capacity = (double)get_memory_capacity(); + mem_capacity = (double)mem_capacity_u; int e; diff --git a/src/sysinf.c b/src/sysinf.c index 5ac3efa..924a6d7 100644 --- a/src/sysinf.c +++ b/src/sysinf.c @@ -8,18 +8,12 @@ #include #include "sysinf.h" -int get_cpu_model(char* cpu_model, unsigned long len) { +sysinf_err_t get_cpu_model(char* cpu_model, unsigned long len) { FILE* cpuinfo_file; cpuinfo_file = fopen("/proc/cpuinfo", "r"); if (!cpuinfo_file) { - char err[64]; - - strncpy(err, "Could not read ", 30); - strncat(err, "/proc/cpuinfo", 30); - perror(err); - - exit(EXIT_FAILURE); + return SI_ERR_COULD_NOT_OPEN; } char line[1024]; @@ -30,36 +24,43 @@ int get_cpu_model(char* cpu_model, unsigned long len) { // space, and the third is the start of the CPU model. char* segment = &(strchr(line, ':')[2]); memcpy(cpu_model, segment, len); - return 0; + fclose(cpuinfo_file); + return SI_ERR_NONE; } } - return -1; + fclose(cpuinfo_file); + return SI_ERR_COULD_NOT_FIND; } -int get_root_size(unsigned long* size, unsigned long* usage) { +sysinf_err_t get_root_size(unsigned long* size, unsigned long* usage) { struct statvfs filesystem_stats; if (statvfs("/etc/fstab", &filesystem_stats)) { - perror("Failed to retrieve file system information"); - return -1; + return SI_ERR_COULD_NOT_STAT; } *size = filesystem_stats.f_frsize * filesystem_stats.f_blocks; *usage = filesystem_stats.f_frsize * (filesystem_stats.f_blocks - filesystem_stats.f_bfree); - return 0; + return SI_ERR_NONE; } -long get_memory_capacity() { +sysinf_err_t get_memory_capacity(unsigned int* mem_cap) { char mem_capacity_str[64]; FILE* meminfo = fopen("/proc/meminfo", "r"); + + if (!meminfo) { + return SI_ERR_COULD_NOT_OPEN; + } + int c = 0; while (true) { char cc = fgetc(meminfo); if (cc == EOF) { - return -1; + fclose(meminfo); + return SI_ERR_COULD_NOT_FIND; } if (strchr("1234567890", cc)) { @@ -79,7 +80,12 @@ long get_memory_capacity() { mem_capacity_str[c] = cc; } - // meminfo is in KB to multiply by 1000. - return ferror(meminfo) ? -1 : atol(mem_capacity_str) * 1000; + if (ferror(meminfo)) { + fclose(meminfo); + return SI_ERR_COULD_NOT_READ; + } + + *mem_cap = (unsigned int)atol(mem_capacity_str) * 1000; + return SI_ERR_NONE; } diff --git a/src/sysinf.h b/src/sysinf.h index 67d0836..3bd885c 100644 --- a/src/sysinf.h +++ b/src/sysinf.h @@ -4,6 +4,14 @@ #ifndef SYSINF_H #define SYSINF_H +typedef enum { + SI_ERR_NONE = 0, + SI_ERR_COULD_NOT_READ, + SI_ERR_COULD_NOT_FIND, + SI_ERR_COULD_NOT_STAT, + SI_ERR_COULD_NOT_OPEN +} sysinf_err_t; + /// Gets the CPU model from PROC_CPU_INFO. /// /// @param cpu_model @@ -14,8 +22,8 @@ /// max number of bytes to copy to `cpu_model`. /// /// @returns -/// non 0 on failure -int get_cpu_model(char* cpu_model, unsigned long len); +/// sysinf_err_t +sysinf_err_t get_cpu_model(char* cpu_model, unsigned long len); /// Gets the size and usage of the root filesystem. /// @@ -28,13 +36,16 @@ int get_cpu_model(char* cpu_model, unsigned long len); /// drive's usage. /// /// @returns -/// 0 on success, -1 on failiure. -int get_root_size(unsigned long* size, unsigned long* usage); +/// sysinf_err_t +sysinf_err_t get_root_size(unsigned long* size, unsigned long* usage); /// Get the memory capacity of the local device. /// +/// @param mem_cap +/// pointer to an unsigned int to be the memory capacity in bytes +/// /// @returns -/// The device's memory capacity in bytes. -long get_memory_capacity(); +/// sysinf_err_t +sysinf_err_t get_memory_capacity(unsigned int* mem_cap); #endif // SYSINF_H