Changeset 5722

Show
Ignore:
Timestamp:
05/13/09 18:09:06 (4 years ago)
Author:
andy
Message:

sensord: This patch does some refactoring of the loadConfig()
function.

* Simplifying the conditions makes code flow clearer and eliminates
long lines (> 80 chars).
* Removed useless stat() call.
* Return -1 in error case, instead of several positive values (never defined).

Location:
lm-sensors/trunk/prog/sensord
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • lm-sensors/trunk/prog/sensord/lib.c

    r5678 r5722  
    2222 */ 
    2323 
     24#include <errno.h> 
    2425#include <stdio.h> 
    2526#include <stdlib.h> 
     
    3536static int loadConfig(const char *cfgPath, int reload) 
    3637{ 
    37         struct stat stats; 
    38         FILE *cfg = NULL; 
    39         int ret = 0; 
     38        int ret; 
     39        FILE *fp; 
    4040 
    41         if (cfgPath && !strcmp(cfgPath, "-")) { 
    42                 if (!reload) { 
    43                         if ((ret = sensors_init(stdin))) { 
    44                                 sensorLog(LOG_ERR, 
    45                                           "Error loading sensors configuration file <stdin>: %s", 
    46                                           sensors_strerror(ret)); 
    47                                 ret = 12; 
    48                         } 
    49                 } 
    50         } else if (cfgPath && stat(cfgPath, &stats) < 0) { 
    51                 sensorLog(LOG_ERR, 
    52                           "Error stating sensors configuration file: %s", 
    53                           cfgPath); 
    54                 ret = 10; 
    55         } else { 
    56                 if (reload) { 
     41        /* Load default configuration. */ 
     42        if (!cfgPath) { 
     43                if (reload) { 
    5744                        sensorLog(LOG_INFO, "configuration reloading"); 
    5845                        sensors_cleanup(); 
    5946                } 
    60                 if (cfgPath && !(cfg = fopen(cfgPath, "r"))) { 
    61                         sensorLog(LOG_ERR, 
    62                                   "Error opening sensors configuration file: %s", 
    63                                   cfgPath); 
    64                         ret = 11; 
    65                 } else if ((ret = sensors_init(cfg))) { 
    66                         sensorLog(LOG_ERR, 
    67                                   "Error loading sensors configuration file %s: %s", 
    68                                   cfgPath ? cfgPath : "(default)", 
    69                                   sensors_strerror(ret)); 
    70                         ret = 11; 
    71                 } 
    72                 if (cfg) 
    73                         fclose(cfg); 
     47 
     48                ret = sensors_init(NULL); 
     49                if (ret) { 
     50                        sensorLog(LOG_ERR, "Error loading default" 
     51                                  " configuration file: %s", 
     52                                  sensors_strerror(ret)); 
     53                        return -1; 
     54                } 
     55                return 0; 
     56        } 
     57 
     58        fp = fopen(cfgPath, "r"); 
     59        if (!fp) { 
     60                sensorLog(LOG_ERR, "Error opening config file %s: %s", 
     61                          strerror(errno)); 
     62                return -1; 
     63        } 
     64 
     65        if (reload) { 
     66                sensorLog(LOG_INFO, "configuration reloading"); 
     67                sensors_cleanup(); 
    7468        } 
     69        ret = sensors_init(fp); 
     70        if (ret) { 
     71                sensorLog(LOG_ERR, "Error loading sensors configuration file" 
     72                          " %s: %s", cfgPath, sensors_strerror(ret)); 
     73                fclose(fp); 
     74                return -1; 
     75        } 
     76        fclose(fp); 
    7577 
    76         return ret; 
     78        return 0; 
    7779} 
    7880 
  • lm-sensors/trunk/prog/sensord/sensord.c

    r5719 r5722  
    9494                        ret = reloadLib(sensord_args.cfgFile); 
    9595                        if (ret) 
    96                                 sensorLog(LOG_NOTICE, 
    97                                           "config reload error (%d)", ret); 
     96                                sensorLog(LOG_NOTICE, "configuration reload" 
     97                                          " error"); 
    9898                        reload = 0; 
    9999                }