X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fudev%2Fcollect%2Fcollect.c;h=6cf41c67bb322f6adef1ddb2190b52340fd6e99f;hb=2eec67acbb00593e414549a7e5b35eb7dd776b1b;hp=f594814dbec261f7c7e22cd53401b02cddab8270;hpb=d5a89d7dc17a5ba5cf4fc71f82963c5c94a31c3d;p=elogind.git diff --git a/src/udev/collect/collect.c b/src/udev/collect/collect.c index f594814db..6cf41c67b 100644 --- a/src/udev/collect/collect.c +++ b/src/udev/collect/collect.c @@ -20,23 +20,15 @@ */ #include -#include #include -#include -#include -#include #include -#include #include -#include -#include -#include "libudev.h" #include "libudev-private.h" #include "macro.h" -#define BUFSIZE 16 -#define UDEV_ALARM_TIMEOUT 180 +#define BUFSIZE 16 +#define UDEV_ALARM_TIMEOUT 180 enum collect_state { STATE_NONE, @@ -61,22 +53,26 @@ static inline struct _mate *node_to_mate(struct udev_list_node *node) return container_of(node, struct _mate, node); } -_noreturn_ static void sig_alrm(int signo) +noreturn static void sig_alrm(int signo) { exit(4); } static void usage(void) { - printf("usage: collect [--add|--remove] [--debug] \n" - "\n" + printf("%s [options] \n\n" + "Collect variables across events.\n\n" + " -h --help Print this message\n" + " -a --add Add ID to the list \n" + " -r --remove Remove ID from the list \n" + " -d --debug Debug to stderr\n\n" " Adds ID to the list governed by .\n" " must be part of the list .\n" " If all IDs given by are listed (ie collect has been\n" " invoked for each ID in ) collect returns 0, the\n" " number of missing IDs otherwise.\n" - " On error a negative number is returned.\n" - "\n"); + " On error a negative number is returned.\n\n" + , program_invocation_short_name); } /* @@ -86,18 +82,18 @@ static void usage(void) */ static int prepare(char *dir, char *filename) { - struct stat statbuf; char buf[512]; - int fd; + int r, fd; - if (stat(dir, &statbuf) < 0) - mkdir(dir, 0700); + r = mkdir(dir, 0700); + if (r < 0 && errno != EEXIST) + return -errno; snprintf(buf, sizeof(buf), "%s/%s", dir, filename); - fd = open(buf,O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); + fd = open(buf,O_RDWR|O_CREAT|O_CLOEXEC, S_IRUSR|S_IWUSR); if (fd < 0) - fprintf(stderr, "Cannot open %s: %s\n", buf, strerror(errno)); + fprintf(stderr, "Cannot open %s: %m\n", buf); if (lockf(fd,F_TLOCK,0) < 0) { if (debug) @@ -109,7 +105,7 @@ static int prepare(char *dir, char *filename) fprintf(stderr, "Acquired lock on %s\n", buf); } else { if (debug) - fprintf(stderr, "Could not get lock on %s: %s\n", buf, strerror(errno)); + fprintf(stderr, "Could not get lock on %s: %m\n", buf); } } @@ -139,12 +135,12 @@ static int checkout(int fd) restart: len = bufsize >> 1; - buf = calloc(1,bufsize + 1); - if (!buf) { - fprintf(stderr, "Out of memory.\n"); + buf = malloc(bufsize + 1); + if (!buf) return log_oom(); - } memset(buf, ' ', bufsize); + buf[bufsize] = '\0'; + ptr = buf + len; while ((read(fd, buf + len, len)) > 0) { while (ptr && *ptr) { @@ -153,7 +149,7 @@ static int checkout(int fd) if (!ptr && word < (buf + len)) { bufsize = bufsize << 1; if (debug) - fprintf(stderr, "ID overflow, restarting with size %zi\n", bufsize); + fprintf(stderr, "ID overflow, restarting with size %zu\n", bufsize); free(buf); lseek(fd, 0, SEEK_SET); goto restart; @@ -213,7 +209,7 @@ static void invite(char *us) udev_list_node_foreach(him_node, &bunch) { struct _mate *him = node_to_mate(him_node); - if (!strcmp(him->name, us)) { + if (streq(him->name, us)) { him->state = STATE_CONFIRMED; who = him; } @@ -241,7 +237,7 @@ static void reject(char *us) udev_list_node_foreach(him_node, &bunch) { struct _mate *him = node_to_mate(him_node); - if (!strcmp(him->name, us)) { + if (streq(him->name, us)) { him->state = STATE_NONE; who = him; } @@ -254,7 +250,7 @@ static void reject(char *us) * kickout * * Remove all IDs in the internal list which are not part - * of the list passed via the commandline. + * of the list passed via the command line. */ static void kickout(void) { @@ -404,7 +400,7 @@ int main(int argc, char **argv) us = argv[argi++]; if (signal(SIGALRM, sig_alrm) == SIG_ERR) { - fprintf(stderr, "Cannot set SIGALRM: %s\n", strerror(errno)); + fprintf(stderr, "Cannot set SIGALRM: %m\n"); ret = 2; goto exit; } @@ -434,7 +430,7 @@ int main(int argc, char **argv) udev_list_node_foreach(him_node, &bunch) { struct _mate *him = node_to_mate(him_node); - if (!strcmp(him->name, argv[i])) + if (streq(him->name, argv[i])) who = him; } if (!who) { @@ -442,19 +438,19 @@ int main(int argc, char **argv) if (debug) fprintf(stderr, "ID %s: not in database\n", argv[i]); - him = malloc(sizeof (struct _mate)); + him = new(struct _mate, 1); if (!him) { ret = ENOMEM; goto out; } - him->name = malloc(strlen(argv[i]) + 1); + him->name = strdup(argv[i]); if (!him->name) { + free(him); ret = ENOMEM; goto out; } - strcpy(him->name, argv[i]); him->state = STATE_NONE; udev_list_node_append(&him->node, &bunch); } else {