[systemd-commits] 2 commits - Makefile.am src/bootchart src/libudev src/nspawn src/shared src/udev

Lennart Poettering lennart at kemper.freedesktop.org
Thu Feb 13 14:46:18 CET 2014


 Makefile.am                         |    1 +
 src/bootchart/bootchart.c           |   10 +++++-----
 src/bootchart/store.c               |   24 +++++++++++-------------
 src/libudev/libudev-device.c        |    2 +-
 src/libudev/libudev-enumerate.c     |    2 +-
 src/libudev/libudev-list.c          |    2 +-
 src/libudev/libudev-monitor.c       |    2 +-
 src/libudev/libudev-queue-private.c |    4 ++--
 src/libudev/libudev-queue.c         |    2 +-
 src/libudev/libudev-util.c          |    7 ++++---
 src/libudev/libudev.c               |    2 +-
 src/nspawn/nspawn.c                 |   22 ++++++++++++++++++++++
 src/shared/time-dst.c               |    2 +-
 src/udev/udev-ctrl.c                |    2 +-
 src/udev/udev-event.c               |    2 +-
 src/udev/udev-rules.c               |    2 +-
 src/udev/udevadm-hwdb.c             |    8 ++++----
 src/udev/udevd.c                    |    4 ++--
 18 files changed, 61 insertions(+), 39 deletions(-)

New commits:
commit 955d98c9c1104d469c2989dbfb58f58ee6fe9bdc
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Feb 13 14:45:51 2014 +0100

    everywhere: make use of new0() and macro() macros, and stop using perror()

diff --git a/src/bootchart/bootchart.c b/src/bootchart/bootchart.c
index 27fd505..630f9c5 100644
--- a/src/bootchart/bootchart.c
+++ b/src/bootchart/bootchart.c
@@ -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++;
diff --git a/src/bootchart/store.c b/src/bootchart/store.c
index b6ba113..3e0052d 100644
--- a/src/bootchart/store.c
+++ b/src/bootchart/store.c
@@ -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;
diff --git a/src/libudev/libudev-device.c b/src/libudev/libudev-device.c
index 38144e3..9f80f56 100644
--- a/src/libudev/libudev-device.c
+++ b/src/libudev/libudev-device.c
@@ -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;
diff --git a/src/libudev/libudev-enumerate.c b/src/libudev/libudev-enumerate.c
index 48ffe83..1a880c2 100644
--- a/src/libudev/libudev-enumerate.c
+++ b/src/libudev/libudev-enumerate.c
@@ -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;
diff --git a/src/libudev/libudev-list.c b/src/libudev/libudev-list.c
index c6e5a61..42fcb85 100644
--- a/src/libudev/libudev-list.c
+++ b/src/libudev/libudev-list.c
@@ -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);
diff --git a/src/libudev/libudev-monitor.c b/src/libudev/libudev-monitor.c
index f976da0..ba1b04d 100644
--- a/src/libudev/libudev-monitor.c
+++ b/src/libudev/libudev-monitor.c
@@ -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;
diff --git a/src/libudev/libudev-queue-private.c b/src/libudev/libudev-queue-private.c
index 80d7cee..d5a2b50 100644
--- a/src/libudev/libudev-queue-private.c
+++ b/src/libudev/libudev-queue-private.c
@@ -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;
diff --git a/src/libudev/libudev-queue.c b/src/libudev/libudev-queue.c
index f67dba9..2cb4d67 100644
--- a/src/libudev/libudev-queue.c
+++ b/src/libudev/libudev-queue.c
@@ -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;
diff --git a/src/libudev/libudev-util.c b/src/libudev/libudev-util.c
index c940e06..d9cdde1 100644
--- a/src/libudev/libudev-util.c
+++ b/src/libudev/libudev-util.c
@@ -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;
 }
diff --git a/src/libudev/libudev.c b/src/libudev/libudev.c
index e1ba82f..e2ab960 100644
--- a/src/libudev/libudev.c
+++ b/src/libudev/libudev.c
@@ -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;
diff --git a/src/shared/time-dst.c b/src/shared/time-dst.c
index 3950a88..65e2998 100644
--- a/src/shared/time-dst.c
+++ b/src/shared/time-dst.c
@@ -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;
 
diff --git a/src/udev/udev-ctrl.c b/src/udev/udev-ctrl.c
index a65603b..2633423 100644
--- a/src/udev/udev-ctrl.c
+++ b/src/udev/udev-ctrl.c
@@ -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;
diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c
index c8e216c..8eb1e2b 100644
--- a/src/udev/udev-event.c
+++ b/src/udev/udev-event.c
@@ -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;
diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c
index e908298..0a32604 100644
--- a/src/udev/udev-rules.c
+++ b/src/udev/udev-rules.c
@@ -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;
diff --git a/src/udev/udevadm-hwdb.c b/src/udev/udevadm-hwdb.c
index a22de1d..16abbe8 100644
--- a/src/udev/udevadm-hwdb.c
+++ b/src/udev/udevadm-hwdb.c
@@ -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;
diff --git a/src/udev/udevd.c b/src/udev/udevd.c
index dca62f2..9d7fa03 100644
--- a/src/udev/udevd.c
+++ b/src/udev/udevd.c
@@ -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;
 

commit 7e2270246b0906675c8f34bc278b1608b969e65c
Author: Lennart Poettering <lennart at poettering.net>
Date:   Thu Feb 13 14:38:02 2014 +0100

    nspawn: check with udev before we take possession of an interface

diff --git a/Makefile.am b/Makefile.am
index 713a3aa..61d678f 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1849,6 +1849,7 @@ systemd_nspawn_LDADD = \
 	libsystemd-capability.la \
 	libsystemd-internal.la \
 	libsystemd-daemon-internal.la \
+	libudev-internal.la \
 	libsystemd-shared.la
 
 # ------------------------------------------------------------------------------
diff --git a/src/nspawn/nspawn.c b/src/nspawn/nspawn.c
index 689592e..abcee3f 100644
--- a/src/nspawn/nspawn.c
+++ b/src/nspawn/nspawn.c
@@ -73,6 +73,7 @@
 #include "env-util.h"
 #include "def.h"
 #include "rtnl-util.h"
+#include "udev-util.h"
 
 typedef enum LinkJournal {
         LINK_NO,
@@ -1256,6 +1257,7 @@ static int reset_audit_loginuid(void) {
 
 static int move_network_interfaces(pid_t pid) {
         _cleanup_rtnl_unref_ sd_rtnl *rtnl = NULL;
+        _cleanup_udev_unref_ struct udev *udev = NULL;
         char **i;
         int r;
 
@@ -1271,8 +1273,16 @@ static int move_network_interfaces(pid_t pid) {
                 return r;
         }
 
+        udev = udev_new();
+        if (!udev) {
+                log_error("Failed to connect to udev.");
+                return -ENOMEM;
+        }
+
         STRV_FOREACH(i, arg_network_interfaces) {
                 _cleanup_rtnl_message_unref_ sd_rtnl_message *m = NULL;
+                _cleanup_udev_device_unref_ struct udev_device *d = NULL;
+                char ifi_str[2 + DECIMAL_STR_MAX(int)];
                 int ifi;
 
                 ifi = (int) if_nametoindex(*i);
@@ -1281,6 +1291,18 @@ static int move_network_interfaces(pid_t pid) {
                         return -errno;
                 }
 
+                sprintf(ifi_str, "n%i", ifi);
+                d = udev_device_new_from_device_id(udev, ifi_str);
+                if (!d) {
+                        log_error("Failed to get udev device for interface %s: %m", *i);
+                        return -errno;
+                }
+
+                if (udev_device_get_is_initialized(d) <= 0) {
+                        log_error("Network interface %s is not initialized yet.", *i);
+                        return -EBUSY;
+                }
+
                 r = sd_rtnl_message_new_link(RTM_NEWLINK, ifi, &m);
                 if (r < 0) {
                         log_error("Failed to allocate netlink message: %s", strerror(-r));



More information about the systemd-commits mailing list