chiark / gitweb /
udev: check return value of write
[elogind.git] / src / udev / collect / collect.c
index 076fe479e22720758879b2c16628aa9558c31e9c..feae4bb6ba1ebd190f0326771f956fb65c30a7b8 100644 (file)
@@ -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);
 }
@@ -144,8 +141,8 @@ static int checkout(int fd)
         len = bufsize >> 1;
         buf = calloc(1,bufsize + 1);
         if (!buf) {
-                fprintf(stderr, "Out of memory\n");
-                return -1;
+                fprintf(stderr, "Out of memory.\n");
+                return log_oom();
         }
         memset(buf, ' ', bufsize);
         ptr = buf + len;
@@ -170,7 +167,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;
@@ -279,7 +285,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 +300,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 +414,7 @@ int main(int argc, char **argv)
         if (debug)
                 fprintf(stderr, "Using checkpoint '%s'\n", checkpoint);
 
-        util_strscpyl(tmpdir, sizeof(tmpdir), udev_get_run_path(udev), "/collect", NULL);
+        util_strscpyl(tmpdir, sizeof(tmpdir), "/run/udev/collect", NULL);
         fd = prepare(tmpdir, checkpoint);
         if (fd < 0) {
                 ret = 3;
@@ -434,7 +443,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);