X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcommon.c;h=182f923dabe68e21dd8788e03863b437d4ab6030;hb=da70cd0f24be40b2d76a9dd88ff8264b52c989bd;hp=61a759b10b2c3dad4d85716ffe5ef1dd55979625;hpb=39ba035b60b509311874ccf89d11d240c0271a90;p=collectd.git diff --git a/src/common.c b/src/common.c index 61a759b1..182f923d 100644 --- a/src/common.c +++ b/src/common.c @@ -433,7 +433,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); } @@ -448,32 +449,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; } } @@ -537,12 +548,12 @@ long long get_kstat_value (kstat_t *ksp, char *name) #else if (ksp == NULL) { - fprintf (stderr, "ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__); + ERROR ("ERROR: %s:%i: ksp == NULL\n", __FILE__, __LINE__); return (-1LL); } else if (ksp->ks_type != KSTAT_TYPE_NAMED) { - fprintf (stderr, "ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__); + ERROR ("ERROR: %s:%i: ksp->ks_type != KSTAT_TYPE_NAMED\n", __FILE__, __LINE__); return (-1LL); } #endif