"Fossies" - the Fresh Open Source Software Archive

Member "vnstat-2.9/src/common.c" (1 Jan 2022, 6410 Bytes) of package /linux/misc/vnstat-2.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "common.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.8_vs_2.9.

    1 #include "common.h"
    2 
    3 /* global variables */
    4 CFG cfg;
    5 IFINFO ifinfo;
    6 char errorstring[1024];
    7 ibwnode *ifacebw;
    8 int debug;
    9 int noexit; /* = running as daemon if 2 */
   10 int intsignal;
   11 int pidfile;
   12 int disableprints;
   13 
   14 int printe(const PrintType type)
   15 {
   16     int result = 1;
   17     char timestamp[22];
   18     time_t current;
   19 
   20     if (disableprints) {
   21         return 1;
   22 
   23         /* daemon running but log not enabled */
   24     } else if (noexit == 2 && cfg.uselogging == 0) {
   25         return 1;
   26 
   27         /* daemon running, log enabled */
   28     } else if (noexit == 2) {
   29 
   30         switch (type) {
   31             case PT_Multiline:
   32                 break;
   33             case PT_Info:
   34             case PT_Infoless:
   35             case PT_Warning:
   36             case PT_Error:
   37             case PT_Config:
   38             case PT_ShortMultiline:
   39                 result = logprint(type);
   40                 break;
   41         }
   42 
   43         /* daemon isn't running or is running attached to a terminal */
   44     } else {
   45 
   46         if (cfg.timestampprints && type != PT_ShortMultiline) {
   47             current = time(NULL);
   48             strftime(timestamp, 22, DATETIMEFORMAT, localtime(&current));
   49             printf("[%s] ", timestamp);
   50         }
   51 
   52         switch (type) {
   53             case PT_Info:
   54                 printf("Info: %s\n", errorstring);
   55                 break;
   56             case PT_Infoless:
   57                 printf("%s\n", errorstring);
   58                 break;
   59             case PT_Warning:
   60                 printf("Warning: %s\n", errorstring);
   61                 break;
   62             case PT_Error:
   63                 printf("Error: %s\n", errorstring);
   64                 break;
   65             case PT_Config:
   66                 printf("Config: %s\n", errorstring);
   67                 break;
   68             case PT_Multiline:
   69                 printf("%s\n", errorstring);
   70                 break;
   71             case PT_ShortMultiline:
   72                 break;
   73         }
   74         fflush(stdout);
   75     }
   76 
   77     return result;
   78 }
   79 
   80 int logprint(const PrintType type)
   81 {
   82     /* buffer needs some extra space for timestamp + info compared to errorstring */
   83     char timestamp[22], buffer[1060];
   84     time_t current;
   85     FILE *logfile;
   86 
   87     buffer[0] = '\0';
   88 
   89     /* logfile */
   90     if (cfg.uselogging == 1) {
   91 
   92         if (type == PT_Multiline) {
   93             return 0;
   94         }
   95 
   96         if ((logfile = fopen(cfg.logfile, "a")) == NULL) {
   97             return 0;
   98         }
   99 
  100         current = time(NULL);
  101         strftime(timestamp, 22, DATETIMEFORMAT, localtime(&current));
  102 
  103         switch (type) {
  104             case PT_Info:
  105             case PT_Infoless:
  106                 snprintf(buffer, 1060, "[%s] %s\n", timestamp, errorstring);
  107                 break;
  108             case PT_Warning:
  109                 snprintf(buffer, 1060, "[%s] Warning: %s\n", timestamp, errorstring);
  110                 break;
  111             case PT_Error:
  112                 snprintf(buffer, 1060, "[%s] Error: %s\n", timestamp, errorstring);
  113                 break;
  114             case PT_Config:
  115                 snprintf(buffer, 1060, "[%s] Config: %s\n", timestamp, errorstring);
  116                 break;
  117             case PT_Multiline:
  118                 break;
  119             case PT_ShortMultiline:
  120                 snprintf(buffer, 1060, "[%s] %s\n", timestamp, errorstring);
  121                 break;
  122         }
  123 
  124         if (fwrite(buffer, strlen(buffer), 1, logfile) != 1) {
  125             fclose(logfile);
  126             return 0;
  127         }
  128 
  129         fclose(logfile);
  130         return 1;
  131 
  132         /* syslog */
  133     } else if (cfg.uselogging == 2) {
  134 
  135         openlog("vnstatd", LOG_PID, LOG_DAEMON);
  136 
  137         switch (type) {
  138             case PT_Multiline:
  139                 break;
  140             case PT_Warning:
  141                 syslog(LOG_WARNING, "Warning: %s", errorstring);
  142                 break;
  143             case PT_Error:
  144                 syslog(LOG_ERR, "Error: %s", errorstring);
  145                 break;
  146             case PT_Config:
  147                 syslog(LOG_ERR, "Config: %s", errorstring);
  148                 break;
  149             case PT_Info:
  150             case PT_Infoless:
  151             case PT_ShortMultiline:
  152                 syslog(LOG_NOTICE, "%s", errorstring);
  153                 break;
  154         }
  155 
  156         closelog();
  157         return 1;
  158     }
  159 
  160     return 0;
  161 }
  162 
  163 int verifylogaccess(void)
  164 {
  165     FILE *logfile;
  166 
  167     /* only logfile logging can be verified */
  168     if (cfg.uselogging == 1) {
  169         if ((logfile = fopen(cfg.logfile, "a")) == NULL) {
  170             return 0;
  171         }
  172         fclose(logfile);
  173     }
  174     return 1;
  175 }
  176 
  177 int dmonth(const int month)
  178 {
  179     static int dmon[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  180     int year;
  181     time_t current;
  182 
  183     /* handle leap years */
  184     if (month == 1) {
  185         current = time(NULL);
  186         year = localtime(&current)->tm_year + 1900;
  187         if (isleapyear(year)) {
  188             return 29;
  189         } else {
  190             return 28;
  191         }
  192     } else {
  193         return dmon[month];
  194     }
  195 }
  196 
  197 int isleapyear(const int year)
  198 {
  199     if (year % 4 != 0) {
  200         return 0;
  201     } else if (year % 100 != 0) {
  202         return 1;
  203     } else if (year % 400 != 0) {
  204         return 0;
  205     }
  206     return 1;
  207 }
  208 
  209 time_t mosecs(time_t month, time_t updated)
  210 {
  211     struct tm d;
  212 
  213     if (localtime_r(&month, &d) == NULL) {
  214         return 1;
  215     }
  216 
  217     d.tm_mday = cfg.monthrotate;
  218     d.tm_hour = d.tm_min = d.tm_sec = 0;
  219 
  220     if ((updated - month) > 0) {
  221         return updated - mktime(&d);
  222     } else {
  223         return 1;
  224     }
  225 }
  226 
  227 uint64_t countercalc(const uint64_t *a, const uint64_t *b, const short is64bit)
  228 {
  229     /* no rollover */
  230     if (*b >= *a) {
  231         if (debug)
  232             printf("cc (%d): %" PRIu64 " - %" PRIu64 " = %" PRIu64 "\n", is64bit, *b, *a, *b - *a);
  233         return *b - *a;
  234 
  235         /* rollover exists */
  236     } else {
  237         /* counter is 64bit */
  238         if (*a > MAX32 || *b > MAX32 || is64bit == 1) {
  239             if (debug)
  240                 printf("cc64 (%d): uint64 - %" PRIu64 " + %" PRIu64 " = %" PRIu64 "\n", is64bit, *a, *b, (uint64_t)MAX64 - *a + *b);
  241             return MAX64 - *a + *b;
  242 
  243             /* counter is 32bit */
  244         } else {
  245             if (debug)
  246                 printf("cc32 (%d): uint32 - %" PRIu64 " + %" PRIu64 " = %" PRIu64 "\n", is64bit, *a, *b, (uint64_t)MAX32 - *a + *b);
  247             return MAX32 - *a + *b;
  248         }
  249     }
  250 }
  251 
  252 /* strncpy with ensured null termination */
  253 char *strncpy_nt(char *dest, const char *src, size_t n)
  254 {
  255     strncpy(dest, src, n);
  256     dest[n - 1] = '\0';
  257     return dest;
  258 }
  259 
  260 int isnumeric(const char *s)
  261 {
  262     size_t i, len = strlen(s);
  263 
  264     if (!len) {
  265         return 0;
  266     }
  267 
  268     for (i = 0; i < len; i++) {
  269         if (!isdigit(s[i])) {
  270             return 0;
  271         }
  272     }
  273 
  274     return 1;
  275 }
  276 
  277 __attribute__((noreturn)) void panicexit(const char *sourcefile, const int sourceline)
  278 {
  279     snprintf(errorstring, 1024, "Unexpected error (%s), exiting. (%s:%d)", strerror(errno), sourcefile, sourceline);
  280     fprintf(stderr, "%s\n", errorstring);
  281     printe(PT_Error);
  282     exit(EXIT_FAILURE);
  283 }
  284 
  285 char *getversion(void)
  286 {
  287     int i;
  288     static char versionbuffer[16];
  289     strncpy_nt(versionbuffer, VERSION, 16);
  290     for (i = 0; i < (int)strlen(versionbuffer); i++) {
  291         if (versionbuffer[i] == '_') {
  292             versionbuffer[i] = ' ';
  293         }
  294     }
  295     return versionbuffer;
  296 }
  297 
  298 double timeused(const char *func, const int reset)
  299 {
  300     static struct timeval starttime;
  301     struct timeval endtime;
  302     double used_secs;
  303 
  304     if (reset) {
  305         gettimeofday(&starttime, NULL);
  306         return 0.0;
  307     }
  308 
  309     if (gettimeofday(&endtime, NULL) != 0) {
  310         return 0.0;
  311     }
  312 
  313     used_secs = (double)(endtime.tv_usec - starttime.tv_usec) / 1000000 + (double)(endtime.tv_sec - starttime.tv_sec);
  314 
  315     if (debug) {
  316         printf("%s() in %f s\n", func, used_secs);
  317     }
  318 
  319     return used_secs;
  320 }
  321 
  322 void timeused_debug(const char *func, const int reset)
  323 {
  324     if (!debug) {
  325         return;
  326     }
  327 
  328     timeused(func, reset);
  329 }