tree-wide: when %m is used in log_*, always specify errno explicitly
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 13 May 2017 15:26:55 +0000 (11:26 -0400)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 19 May 2017 18:24:03 +0000 (14:24 -0400)
All those uses were correct, but I think it's better to be explicit.
Using implicit errno is too error prone, and with this change we can require
(in the sense of a style guideline) that the code is always specified.

Helpful query: git grep -n -P 'log_[^s][a-z]+\(.*%m'

12 files changed:
src/basic/selinux-util.c
src/core/cgroup.c
src/core/main.c
src/libsystemd-network/sd-dhcp-client.c
src/libsystemd/sd-device/device-enumerator.c
src/libsystemd/sd-device/sd-device.c
src/libudev/libudev-monitor.c
src/shared/install.c
src/stdio-bridge/stdio-bridge.c
src/test/test-nss.c
src/tmpfiles/tmpfiles.c
src/udev/udev-rules.c

index bc07654..bc53f81 100644 (file)
@@ -50,7 +50,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC(context_t, context_free);
 static int cached_use = -1;
 static struct selabel_handle *label_hnd = NULL;
 
-#define log_enforcing(...) log_full(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, __VA_ARGS__)
+#define log_enforcing(...) log_full_errno(security_getenforce() == 1 ? LOG_ERR : LOG_DEBUG, errno, __VA_ARGS__)
 #endif
 
 bool mac_selinux_have(void) {
index 774b832..71f307f 100644 (file)
@@ -398,8 +398,7 @@ static int whitelist_major(const char *path, const char *name, char type, const
         return 0;
 
 fail:
-        log_warning_errno(errno, "Failed to read /proc/devices: %m");
-        return -errno;
+        return log_warning_errno(errno, "Failed to read /proc/devices: %m");
 }
 
 static bool cgroup_context_has_cpu_weight(CGroupContext *c) {
index e6ae0be..c2439ed 100644 (file)
@@ -536,7 +536,7 @@ static int config_parse_cpu_affinity2(
                 return ncpus;
 
         if (sched_setaffinity(0, CPU_ALLOC_SIZE(ncpus), c) < 0)
-                log_warning("Failed to set CPU affinity: %m");
+                log_warning_errno(errno, "Failed to set CPU affinity: %m");
 
         return 0;
 }
index 7c03176..e20d339 100644 (file)
@@ -1655,7 +1655,8 @@ static int client_receive_message_udp(
                 if (errno == EAGAIN || errno == EINTR)
                         return 0;
 
-                return log_dhcp_client_errno(client, errno, "Could not receive message from UDP socket: %m");
+                return log_dhcp_client_errno(client, errno,
+                                             "Could not receive message from UDP socket: %m");
         }
         if ((size_t) len < sizeof(DHCPMessage)) {
                 log_dhcp_client(client, "Too small to be a DHCP message: ignoring");
@@ -1748,9 +1749,8 @@ static int client_receive_message_raw(
                 if (errno == EAGAIN || errno == EINTR)
                         return 0;
 
-                log_dhcp_client(client, "Could not receive message from raw socket: %m");
-
-                return -errno;
+                return log_dhcp_client_errno(client, errno,
+                                             "Could not receive message from raw socket: %m");
         } else if ((size_t)len < sizeof(DHCPPacket))
                 return 0;
 
index 86f8935..ebb8b2d 100644 (file)
@@ -631,10 +631,8 @@ static int enumerator_scan_devices_tag(sd_device_enumerator *enumerator, const c
         if (!dir) {
                 if (errno == ENOENT)
                         return 0;
-                else {
-                        log_error("sd-device-enumerator: could not open tags directory %s: %m", path);
-                        return -errno;
-                }
+                else
+                        return log_error_errno(errno, "sd-device-enumerator: could not open tags directory %s: %m", path);
         }
 
         /* TODO: filter away subsystems? */
@@ -758,10 +756,8 @@ static int parent_crawl_children(sd_device_enumerator *enumerator, const char *p
         int r = 0;
 
         dir = opendir(path);
-        if (!dir) {
-                log_debug("sd-device-enumerate: could not open parent directory %s: %m", path);
-                return -errno;
-        }
+        if (!dir)
+                return log_debug_errno(errno, "sd-device-enumerate: could not open parent directory %s: %m", path);
 
         FOREACH_DIRENT_ALL(dent, dir, return -errno) {
                 _cleanup_free_ char *child = NULL;
index 04ead29..c56ae39 100644 (file)
@@ -195,8 +195,7 @@ int device_set_syspath(sd_device *device, const char *_syspath, bool verify) {
                                         /* this is not a valid device */
                                         return -ENODEV;
 
-                                log_debug("sd-device: %s does not have an uevent file: %m", syspath);
-                                return -errno;
+                                return log_debug_errno(errno, "sd-device: %s does not have an uevent file: %m", syspath);
                         }
                 } else {
                         /* everything else just needs to be a directory */
index a40329d..8287694 100644 (file)
@@ -681,7 +681,7 @@ retry:
 
         udev_device = udev_device_new_from_nulstr(udev_monitor->udev, &buf.raw[bufpos], buflen - bufpos);
         if (!udev_device) {
-                log_debug("could not create device: %m");
+                log_debug_errno(errno, "could not create device: %m");
                 return NULL;
         }
 
index 58c8e85..7f3bc0d 100644 (file)
@@ -3045,7 +3045,7 @@ int unit_file_get_list(
                         if (errno == ENOENT)
                                 continue;
                         if (IN_SET(errno, ENOTDIR, EACCES)) {
-                                log_debug("Failed to open \"%s\": %m", *i);
+                                log_debug_errno(errno, "Failed to open \"%s\": %m", *i);
                                 continue;
                         }
 
index 02ba526..097f8c1 100644 (file)
@@ -292,7 +292,7 @@ int main(int argc, char *argv[]) {
                         r = ppoll(p, ELEMENTSOF(p), ts, NULL);
                 }
                 if (r < 0) {
-                        log_error("ppoll() failed: %m");
+                        log_error_errno(errno, "ppoll() failed: %m");
                         goto finish;
                 }
         }
index b4cb3f0..57eeb8e 100644 (file)
@@ -105,7 +105,7 @@ static int print_gaih_addrtuples(const struct gaih_addrtuple *tuples) {
                         goto numerical_index;
 
                 if (if_indextoname(it->scopeid, ifname) == NULL) {
-                        log_warning("if_indextoname(%d) failed: %m", it->scopeid);
+                        log_warning_errno(errno, "if_indextoname(%d) failed: %m", it->scopeid);
                 numerical_index:
                         xsprintf(ifname, "%i", it->scopeid);
                 };
index 36488c9..9419c99 100644 (file)
@@ -724,10 +724,9 @@ static int path_set_xattrs(Item *i, const char *path) {
 
                 n = strlen(*value);
                 log_debug("Setting extended attribute '%s=%s' on %s.", *name, *value, path);
-                if (lsetxattr(path, *name, *value, n, 0) < 0) {
-                        log_error("Setting extended attribute %s=%s on %s failed: %m", *name, *value, path);
-                        return -errno;
-                }
+                if (lsetxattr(path, *name, *value, n, 0) < 0)
+                        return log_error_errno(errno, "Setting extended attribute %s=%s on %s failed: %m",
+                                               *name, *value, path);
         }
         return 0;
 }
index 4d07b8f..a1b2513 100644 (file)
@@ -814,7 +814,7 @@ static const char *get_key_attribute(struct udev *udev, char *str) {
                 attr++;
                 pos = strchr(attr, '}');
                 if (pos == NULL) {
-                        log_error("missing closing brace for format");
+                        log_error("Missing closing brace for format");
                         return NULL;
                 }
                 pos[0] = '\0';
@@ -2536,19 +2536,19 @@ int udev_rules_apply_static_dev_perms(struct udev_rules *rules) {
                         }
                         if (mode != (stats.st_mode & 01777)) {
                                 r = chmod(device_node, mode);
-                                if (r < 0) {
-                                        log_error("failed to chmod '%s' %#o", device_node, mode);
-                                        return -errno;
-                                else
+                                if (r < 0)
+                                        return log_error_errno(errno, "Failed to chmod '%s' %#o: %m",
+                                                               device_node, mode);
+                                else
                                         log_debug("chmod '%s' %#o", device_node, mode);
                         }
 
                         if ((uid != 0 && uid != stats.st_uid) || (gid != 0 && gid != stats.st_gid)) {
                                 r = chown(device_node, uid, gid);
-                                if (r < 0) {
-                                        log_error("failed to chown '%s' %u %u ", device_node, uid, gid);
-                                        return -errno;
-                                else
+                                if (r < 0)
+                                        return log_error_errno(errno, "Failed to chown '%s' %u %u: %m",
+                                                               device_node, uid, gid);
+                                else
                                         log_debug("chown '%s' %u %u", device_node, uid, gid);
                         }