X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcommon.c;h=1af2f144647b191c38321ddd4aed4264b2c79577;hb=e8999694aac7184ac4eea29564a2892f188c4171;hp=ae89c44f9aee71ceaf5ceb8df2e117ef418d2d1f;hpb=f73c5b3069fd10048f1fcdf5266d8da2fcb403e5;p=collectd.git diff --git a/src/common.c b/src/common.c index ae89c44f..1af2f144 100644 --- a/src/common.c +++ b/src/common.c @@ -18,6 +18,8 @@ * Authors: * Florian octo Forster * Niki W. Waibel + * Sebastian Harl + * Michał Mirosław **/ #if HAVE_CONFIG_H @@ -347,28 +349,56 @@ int escape_slashes (char *buf, int buf_len) return (0); } /* int escape_slashes */ -int timeval_sub_timespec (struct timeval *tv0, struct timeval *tv1, struct timespec *ret) +int timeval_cmp (struct timeval tv0, struct timeval tv1, struct timeval *delta) { - if ((tv0 == NULL) || (tv1 == NULL) || (ret == NULL)) - return (-2); + struct timeval *larger; + struct timeval *smaller; - if ((tv0->tv_sec < tv1->tv_sec) - || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec))) - return (-1); + int status; - ret->tv_sec = tv0->tv_sec - tv1->tv_sec; - ret->tv_nsec = 1000 * ((long) (tv0->tv_usec - tv1->tv_usec)); + NORMALIZE_TIMEVAL (tv0); + NORMALIZE_TIMEVAL (tv1); - if (ret->tv_nsec < 0) + if ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec == tv1.tv_usec)) { - assert (ret->tv_sec > 0); + if (delta != NULL) { + delta->tv_sec = 0; + delta->tv_usec = 0; + } + return (0); + } - ret->tv_nsec += 1000000000; - ret->tv_sec -= 1; + if ((tv0.tv_sec < tv1.tv_sec) + || ((tv0.tv_sec == tv1.tv_sec) && (tv0.tv_usec < tv1.tv_usec))) + { + larger = &tv1; + smaller = &tv0; + status = -1; + } + else + { + larger = &tv0; + smaller = &tv1; + status = 1; } - return (0); -} + if (delta != NULL) { + delta->tv_sec = larger->tv_sec - smaller->tv_sec; + + if (smaller->tv_usec <= larger->tv_usec) + delta->tv_usec = larger->tv_usec - smaller->tv_usec; + else + { + --delta->tv_sec; + delta->tv_usec = 1000000 + larger->tv_usec - smaller->tv_usec; + } + } + + assert ((delta == NULL) + || ((0 <= delta->tv_usec) && (delta->tv_usec < 1000000))); + + return (status); +} /* int timeval_cmp */ int check_create_dir (const char *file_orig) { @@ -383,7 +413,7 @@ int check_create_dir (const char *file_orig) char *saveptr; int last_is_file = 1; int path_is_absolute = 0; - int len; + size_t len; int i; /* @@ -439,7 +469,8 @@ int check_create_dir (const char *file_orig) */ if (fields[i][0] == '.') { - ERROR ("Cowardly refusing to create a directory that begins with a `.' (dot): `%s'", file_orig); + ERROR ("Cowardly refusing to create a directory that " + "begins with a `.' (dot): `%s'", file_orig); return (-2); } @@ -454,32 +485,42 @@ int check_create_dir (const char *file_orig) return (-1); } - if (stat (dir, &statbuf) == -1) - { - if (errno == ENOENT) + while (42) { + if (stat (dir, &statbuf) == -1) { - if (mkdir (dir, 0755) == -1) + if (errno == ENOENT) { + if (mkdir (dir, 0755) == 0) + break; + + /* this might happen, if a different thread created + * the directory in the meantime + * => call stat() again to check for S_ISDIR() */ + if (EEXIST == errno) + continue; + char errbuf[1024]; ERROR ("check_create_dir: mkdir (%s): %s", dir, sstrerror (errno, errbuf, sizeof (errbuf))); return (-1); } + else + { + char errbuf[1024]; + ERROR ("check_create_dir: stat (%s): %s", dir, + sstrerror (errno, errbuf, + sizeof (errbuf))); + return (-1); + } } - else + else if (!S_ISDIR (statbuf.st_mode)) { - char errbuf[1024]; - ERROR ("stat (%s): %s", dir, - sstrerror (errno, errbuf, - sizeof (errbuf))); + ERROR ("check_create_dir: `%s' exists but is not " + "a directory!", dir); return (-1); } - } - else if (!S_ISDIR (statbuf.st_mode)) - { - ERROR ("stat (%s): Not a directory!", dir); - return (-1); + break; } } @@ -490,26 +531,26 @@ int check_create_dir (const char *file_orig) int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name) { char ident[128]; + + *ksp_ptr = NULL; if (kc == NULL) return (-1); ssnprintf (ident, sizeof (ident), "%s,%i,%s", module, instance, name); + *ksp_ptr = kstat_lookup (kc, module, instance, name); if (*ksp_ptr == NULL) { - if ((*ksp_ptr = kstat_lookup (kc, module, instance, name)) == NULL) - { - ERROR ("Cound not find kstat %s", ident); - return (-1); - } + ERROR ("get_kstat: Cound not find kstat %s", ident); + return (-1); + } - if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED) - { - WARNING ("kstat %s has wrong type", ident); - *ksp_ptr = NULL; - return (-1); - } + if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED) + { + ERROR ("get_kstat: kstat %s has wrong type", ident); + *ksp_ptr = NULL; + return (-1); } #ifdef assert @@ -519,13 +560,13 @@ int get_kstat (kstat_t **ksp_ptr, char *module, int instance, char *name) if (kstat_read (kc, *ksp_ptr, NULL) == -1) { - WARNING ("kstat %s could not be read", ident); + ERROR ("get_kstat: kstat %s could not be read", ident); return (-1); } if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED) { - WARNING ("kstat %s has wrong type", ident); + ERROR ("get_kstat: kstat %s has wrong type", ident); return (-1); } @@ -925,4 +966,22 @@ int read_file_contents (const char *filename, char *buf, int bufsize) return n; } +counter_t counter_diff (counter_t old_value, counter_t new_value) +{ + counter_t diff; + + if (old_value > new_value) + { + if (old_value <= 4294967295U) + diff = (4294967295U - old_value) + new_value; + else + diff = (18446744073709551615ULL - old_value) + + new_value; + } + else + { + diff = new_value - old_value; + } + return (diff); +} /* counter_t counter_to_gauge */