chiark / gitweb /
everywhere: make use of new0() and macro() macros, and stop using perror()
authorLennart Poettering <lennart@poettering.net>
Thu, 13 Feb 2014 13:45:51 +0000 (14:45 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 13 Feb 2014 13:45:51 +0000 (14:45 +0100)
16 files changed:
src/bootchart/bootchart.c
src/bootchart/store.c
src/libudev/libudev-device.c
src/libudev/libudev-enumerate.c
src/libudev/libudev-list.c
src/libudev/libudev-monitor.c
src/libudev/libudev-queue-private.c
src/libudev/libudev-queue.c
src/libudev/libudev-util.c
src/libudev/libudev.c
src/shared/time-dst.c
src/udev/udev-ctrl.c
src/udev/udev-event.c
src/udev/udev-rules.c
src/udev/udevadm-hwdb.c
src/udev/udevd.c

index 27fd505..630f9c5 100644 (file)
@@ -318,10 +318,10 @@ int main(int argc, char *argv[]) {
         (void) setrlimit(RLIMIT_NOFILE, &rlim);
 
         /* start with empty ps LL */
-        ps_first = calloc(1, sizeof(struct ps_struct));
+        ps_first = new0(struct ps_struct, 1);
         if (!ps_first) {
-                perror("calloc(ps_struct)");
-                exit(EXIT_FAILURE);
+                log_oom();
+                return EXIT_FAILURE;
         }
 
         /* handle TERM/INT nicely */
@@ -397,8 +397,8 @@ int main(int argc, char *argv[]) {
                                         /* caught signal, probably HUP! */
                                         break;
                                 }
-                                perror("nanosleep()");
-                                exit (EXIT_FAILURE);
+                                log_error("nanosleep() failed: %m");
+                                exit(EXIT_FAILURE);
                         }
                 } else {
                         overrun++;
index b6ba113..3e0052d 100644 (file)
@@ -132,8 +132,6 @@ void log_sample(int sample, struct list_sample_data **ptr) {
         struct list_sample_data *sampledata;
         struct ps_sched_struct *ps_prev = NULL;
 
-
-
         sampledata = *ptr;
 
         /* all the per-process stuff goes here */
@@ -151,8 +149,8 @@ void log_sample(int sample, struct list_sample_data **ptr) {
                 /* block stuff */
                 vmstat = openat(procfd, "vmstat", O_RDONLY);
                 if (vmstat == -1) {
-                        perror("open /proc/vmstat");
-                        exit (EXIT_FAILURE);
+                        log_error("Failed to open /proc/vmstat: %m");
+                        exit(EXIT_FAILURE);
                 }
         }
 
@@ -183,8 +181,8 @@ vmstat_next:
                 /* overall CPU utilization */
                 schedstat = openat(procfd, "schedstat", O_RDONLY);
                 if (schedstat == -1) {
-                        perror("open /proc/schedstat");
-                        exit (EXIT_FAILURE);
+                        log_error("Failed to open /proc/schedstat: %m");
+                        exit(EXIT_FAILURE);
                 }
         }
 
@@ -257,17 +255,17 @@ schedstat_next:
                         char t[32];
                         struct ps_struct *parent;
 
-                        ps->next_ps = calloc(1, sizeof(struct ps_struct));
+                        ps->next_ps = new0(struct ps_struct, 1);
                         if (!ps->next_ps) {
-                                perror("calloc(ps_struct)");
+                                log_oom();
                                 exit (EXIT_FAILURE);
                         }
                         ps = ps->next_ps;
                         ps->pid = pid;
 
-                        ps->sample = calloc(1, sizeof(struct ps_sched_struct));
+                        ps->sample = new0(struct ps_sched_struct, 1);
                         if (!ps->sample) {
-                                perror("calloc(ps_struct)");
+                                log_oom();
                                 exit (EXIT_FAILURE);
                         }
                         ps->sample->sampledata = sampledata;
@@ -393,10 +391,10 @@ schedstat_next:
                 if (!sscanf(buf, "%s %s %*s", rt, wt))
                         continue;
 
-                ps->sample->next = calloc(1, sizeof(struct ps_sched_struct));
+                ps->sample->next = new0(struct ps_sched_struct, 1);
                 if (!ps->sample) {
-                        perror("calloc(ps_struct)");
-                        exit (EXIT_FAILURE);
+                        log_oom();
+                        exit(EXIT_FAILURE);
                 }
                 ps->sample->next->prev = ps->sample;
                 ps->sample = ps->sample->next;
index 38144e3..9f80f56 100644 (file)
@@ -647,7 +647,7 @@ struct udev_device *udev_device_new(struct udev *udev)
         if (udev == NULL)
                 return NULL;
 
-        udev_device = calloc(1, sizeof(struct udev_device));
+        udev_device = new0(struct udev_device, 1);
         if (udev_device == NULL)
                 return NULL;
         udev_device->refcount = 1;
index 48ffe83..1a880c2 100644 (file)
@@ -83,7 +83,7 @@ _public_ struct udev_enumerate *udev_enumerate_new(struct udev *udev)
 
         if (udev == NULL)
                 return NULL;
-        udev_enumerate = calloc(1, sizeof(struct udev_enumerate));
+        udev_enumerate = new0(struct udev_enumerate, 1);
         if (udev_enumerate == NULL)
                 return NULL;
         udev_enumerate->refcount = 1;
index c6e5a61..42fcb85 100644 (file)
@@ -165,7 +165,7 @@ struct udev_list_entry *udev_list_entry_add(struct udev_list *list, const char *
         }
 
         /* add new name */
-        entry = calloc(1, sizeof(struct udev_list_entry));
+        entry = new0(struct udev_list_entry, 1);
         if (entry == NULL)
                 return NULL;
         entry->name = strdup(name);
index f976da0..ba1b04d 100644 (file)
@@ -96,7 +96,7 @@ static struct udev_monitor *udev_monitor_new(struct udev *udev)
 {
         struct udev_monitor *udev_monitor;
 
-        udev_monitor = calloc(1, sizeof(struct udev_monitor));
+        udev_monitor = new0(struct udev_monitor, 1);
         if (udev_monitor == NULL)
                 return NULL;
         udev_monitor->refcount = 1;
index 80d7cee..d5a2b50 100644 (file)
@@ -79,7 +79,7 @@ struct udev_queue_export *udev_queue_export_new(struct udev *udev)
         if (udev == NULL)
                 return NULL;
 
-        udev_queue_export = calloc(1, sizeof(struct udev_queue_export));
+        udev_queue_export = new0(struct udev_queue_export, 1);
         if (udev_queue_export == NULL)
                 return NULL;
         udev_queue_export->udev = udev;
@@ -162,7 +162,7 @@ static struct queue_devpaths *build_index(struct udev_queue_export *udev_queue_e
                 udev_err(udev_queue_export->udev, "queue file overflow\n");
                 return NULL;
         }
-        devpaths = calloc(1, sizeof(struct queue_devpaths) + (range + 1) * sizeof(long));
+        devpaths = malloc0(sizeof(struct queue_devpaths) + (range + 1) * sizeof(long));
         if (devpaths == NULL)
                 return NULL;
         devpaths->devpaths_size = range + 1;
index f67dba9..2cb4d67 100644 (file)
@@ -69,7 +69,7 @@ _public_ struct udev_queue *udev_queue_new(struct udev *udev)
         if (udev == NULL)
                 return NULL;
 
-        udev_queue = calloc(1, sizeof(struct udev_queue));
+        udev_queue = new0(struct udev_queue, 1);
         if (udev_queue == NULL)
                 return NULL;
         udev_queue->refcount = 1;
index c940e06..d9cdde1 100644 (file)
@@ -203,6 +203,7 @@ int util_resolve_subsys_kernel(struct udev *udev, const char *string,
         udev_device_unref(dev);
         return 0;
 }
+
 ssize_t util_get_sys_core_link_value(struct udev *udev, const char *slink, const char *syspath, char *value, size_t size)
 {
         char path[UTIL_PATH_SIZE];
@@ -417,7 +418,7 @@ uint64_t util_string_bloom64(const char *str)
 
 ssize_t print_kmsg(const char *fmt, ...)
 {
-        int fd;
+        _cleanup_close_ int fd = -1;
         va_list ap;
         char text[1024];
         ssize_t len;
@@ -435,7 +436,7 @@ ssize_t print_kmsg(const char *fmt, ...)
 
         ret = write(fd, text, len);
         if (ret < 0)
-                ret = -errno;
-        close(fd);
+                return -errno;
+
         return ret;
 }
index e1ba82f..e2ab960 100644 (file)
@@ -121,7 +121,7 @@ _public_ struct udev *udev_new(void)
         const char *env;
         FILE *f;
 
-        udev = calloc(1, sizeof(struct udev));
+        udev = new0(struct udev, 1);
         if (udev == NULL)
                 return NULL;
         udev->refcount = 1;
index 3950a88..65e2998 100644 (file)
@@ -183,7 +183,7 @@ read_again:
                         return -EINVAL;
         }
 
-        transitions = (time_t *)calloc(total_size + tzspec_len, 1);
+        transitions = malloc0(total_size + tzspec_len);
         if (transitions == NULL)
                 return -EINVAL;
 
index a65603b..2633423 100644 (file)
@@ -75,7 +75,7 @@ struct udev_ctrl *udev_ctrl_new_from_fd(struct udev *udev, int fd)
         struct udev_ctrl *uctrl;
         const int on = 1;
 
-        uctrl = calloc(1, sizeof(struct udev_ctrl));
+        uctrl = new0(struct udev_ctrl, 1);
         if (uctrl == NULL)
                 return NULL;
         uctrl->refcount = 1;
index c8e216c..8eb1e2b 100644 (file)
@@ -39,7 +39,7 @@ struct udev_event *udev_event_new(struct udev_device *dev)
         struct udev *udev = udev_device_get_udev(dev);
         struct udev_event *event;
 
-        event = calloc(1, sizeof(struct udev_event));
+        event = new0(struct udev_event, 1);
         if (event == NULL)
                 return NULL;
         event->dev = dev;
index e908298..0a32604 100644 (file)
@@ -1615,7 +1615,7 @@ struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names)
         char **files, **f;
         int r;
 
-        rules = calloc(1, sizeof(struct udev_rules));
+        rules = new0(struct udev_rules, 1);
         if (rules == NULL)
                 return NULL;
         rules->udev = udev;
index a22de1d..16abbe8 100644 (file)
@@ -190,7 +190,7 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
                                 continue;
 
                         /* split node */
-                        new_child = calloc(sizeof(struct trie_node), 1);
+                        new_child = new0(struct trie_node, 1);
                         if (!new_child)
                                 return -ENOMEM;
 
@@ -233,7 +233,7 @@ static int trie_insert(struct trie *trie, struct trie_node *node, const char *se
                         ssize_t off;
 
                         /* new child */
-                        child = calloc(sizeof(struct trie_node), 1);
+                        child = new0(struct trie_node, 1);
                         if (!child)
                                 return -ENOMEM;
 
@@ -576,7 +576,7 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
                 char **files, **f;
                 _cleanup_free_ char *hwdb_bin = NULL;
 
-                trie = calloc(sizeof(struct trie), 1);
+                trie = new0(struct trie, 1);
                 if (!trie) {
                         rc = EXIT_FAILURE;
                         goto out;
@@ -590,7 +590,7 @@ static int adm_hwdb(struct udev *udev, int argc, char *argv[]) {
                 }
 
                 /* index */
-                trie->root = calloc(sizeof(struct trie_node), 1);
+                trie->root = new0(struct trie_node, 1);
                 if (!trie->root) {
                         rc = EXIT_FAILURE;
                         goto out;
index dca62f2..9d7fa03 100644 (file)
@@ -200,7 +200,7 @@ static void worker_new(struct event *event)
         udev_monitor_allow_unicast_sender(worker_monitor, monitor);
         udev_monitor_enable_receiving(worker_monitor);
 
-        worker = calloc(1, sizeof(struct worker));
+        worker = new0(struct worker, 1);
         if (worker == NULL) {
                 udev_monitor_unref(worker_monitor);
                 return;
@@ -431,7 +431,7 @@ static int event_queue_insert(struct udev_device *dev)
 {
         struct event *event;
 
-        event = calloc(1, sizeof(struct event));
+        event = new0(struct event, 1);
         if (event == NULL)
                 return -1;