X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fudev%2Fcollect%2Fcollect.c;h=6580a091e0a95890f0975b0131030a135e2d08cb;hb=7d566799022b24fef2bb8ca6d671bf8ad14d9e26;hp=882564a5f52bb58310933fb37b21c64d934ff873;hpb=6ada823a9a0979ea145fd70add1007c21caa45c0;p=elogind.git diff --git a/src/udev/collect/collect.c b/src/udev/collect/collect.c index 882564a5f..6580a091e 100644 --- a/src/udev/collect/collect.c +++ b/src/udev/collect/collect.c @@ -33,6 +33,7 @@ #include "libudev.h" #include "libudev-private.h" +#include "macro.h" #define BUFSIZE 16 #define UDEV_ALARM_TIMEOUT 180 @@ -55,16 +56,12 @@ static int debug; /* This can increase dynamically */ static size_t bufsize = BUFSIZE; -static struct _mate *node_to_mate(struct udev_list_node *node) +static inline struct _mate *node_to_mate(struct udev_list_node *node) { - char *mate; - - mate = (char *)node; - mate -= offsetof(struct _mate, node); - return (struct _mate *)mate; + return container_of(node, struct _mate, node); } -static void sig_alrm(int signo) +_noreturn_ static void sig_alrm(int signo) { exit(4); } @@ -96,7 +93,7 @@ static int prepare(char *dir, char *filename) if (stat(dir, &statbuf) < 0) mkdir(dir, 0700); - sprintf(buf, "%s/%s", dir, filename); + snprintf(buf, sizeof(buf), "%s/%s", dir, filename); fd = open(buf,O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); if (fd < 0) @@ -142,12 +139,14 @@ static int checkout(int fd) restart: len = bufsize >> 1; - buf = calloc(1,bufsize + 1); + buf = malloc(bufsize + 1); if (!buf) { - fprintf(stderr, "Out of memory\n"); - return -1; + fprintf(stderr, "Out of memory.\n"); + return log_oom(); } memset(buf, ' ', bufsize); + buf[bufsize] = '\0'; + ptr = buf + len; while ((read(fd, buf + len, len)) > 0) { while (ptr && *ptr) { @@ -170,7 +169,16 @@ static int checkout(int fd) if (debug) fprintf(stderr, "Found word %s\n", word); him = malloc(sizeof (struct _mate)); + if (!him) { + free(buf); + return log_oom(); + } him->name = strdup(word); + if (!him->name) { + free(buf); + free(him); + return log_oom(); + } him->state = STATE_OLD; udev_list_node_append(&him->node, &bunch); word = NULL; @@ -207,7 +215,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; } @@ -235,7 +243,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; } @@ -279,7 +287,7 @@ static int missing(int fd) buf = malloc(bufsize); if (!buf) - return -1; + return log_oom(); udev_list_node_foreach(him_node, &bunch) { struct _mate *him = node_to_mate(him_node); @@ -294,12 +302,15 @@ static int missing(int fd) tmpbuf = realloc(buf, bufsize); if (!tmpbuf) { free(buf); - return -1; + return log_oom(); } buf = tmpbuf; } snprintf(buf, strlen(him->name)+2, "%s ", him->name); - write(fd, buf, strlen(buf)); + if (write(fd, buf, strlen(buf)) < 0) { + free(buf); + return -1; + } } } @@ -405,7 +416,7 @@ int main(int argc, char **argv) if (debug) fprintf(stderr, "Using checkpoint '%s'\n", checkpoint); - util_strscpyl(tmpdir, sizeof(tmpdir), "/run/udev/collect", NULL); + strscpyl(tmpdir, sizeof(tmpdir), "/run/udev/collect", NULL); fd = prepare(tmpdir, checkpoint); if (fd < 0) { ret = 3; @@ -425,7 +436,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) { @@ -434,7 +445,17 @@ int main(int argc, char **argv) if (debug) fprintf(stderr, "ID %s: not in database\n", argv[i]); him = malloc(sizeof (struct _mate)); + if (!him) { + ret = ENOMEM; + goto out; + } + him->name = malloc(strlen(argv[i]) + 1); + if (!him->name) { + ret = ENOMEM; + goto out; + } + strcpy(him->name, argv[i]); him->state = STATE_NONE; udev_list_node_append(&him->node, &bunch);