]> git.ipfire.org Git - thirdparty/systemd.git/blobdiff - src/libsystemd/sd-daemon/sd-daemon.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / libsystemd / sd-daemon / sd-daemon.c
index 6f0b975627e7632938b2d099cc1d23d75726ccf9..91f96051717c2e8b1d489e3e839fbd395ac98112 100644 (file)
@@ -23,9 +23,9 @@
 #include "path-util.h"
 #include "process-util.h"
 #include "socket-util.h"
+#include "stat-util.h"
 #include "strv.h"
 #include "time-util.h"
-#include "util.h"
 
 #define SNDBUF_SIZE (8*1024*1024)
 
@@ -40,7 +40,7 @@ static void unsetenv_all(bool unset_environment) {
 
 _public_ int sd_listen_fds(int unset_environment) {
         const char *e;
-        int n, r, fd;
+        int n, r;
         pid_t pid;
 
         e = getenv("LISTEN_PID");
@@ -75,7 +75,7 @@ _public_ int sd_listen_fds(int unset_environment) {
                 goto finish;
         }
 
-        for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
+        for (int fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd ++) {
                 r = fd_cloexec(fd, true);
                 if (r < 0)
                         goto finish;
@@ -150,9 +150,7 @@ _public_ int sd_is_fifo(int fd, const char *path) {
                         return -errno;
                 }
 
-                return
-                        st_path.st_dev == st_fd.st_dev &&
-                        st_path.st_ino == st_fd.st_ino;
+                return stat_inode_same(&st_path, &st_fd);
         }
 
         return 1;
@@ -181,9 +179,7 @@ _public_ int sd_is_special(int fd, const char *path) {
                 }
 
                 if (S_ISREG(st_fd.st_mode) && S_ISREG(st_path.st_mode))
-                        return
-                                st_path.st_dev == st_fd.st_dev &&
-                                st_path.st_ino == st_fd.st_ino;
+                        return stat_inode_same(&st_path, &st_fd);
                 else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
                         return st_path.st_rdev == st_fd.st_rdev;
                 else
@@ -193,7 +189,7 @@ _public_ int sd_is_special(int fd, const char *path) {
         return 1;
 }
 
-static int sd_is_socket_internal(int fd, int type, int listening) {
+static int is_socket_internal(int fd, int type, int listening) {
         struct stat st_fd;
 
         assert_return(fd >= 0, -EBADF);
@@ -242,7 +238,7 @@ _public_ int sd_is_socket(int fd, int family, int type, int listening) {
         assert_return(fd >= 0, -EBADF);
         assert_return(family >= 0, -EINVAL);
 
-        r = sd_is_socket_internal(fd, type, listening);
+        r = is_socket_internal(fd, type, listening);
         if (r <= 0)
                 return r;
 
@@ -270,7 +266,7 @@ _public_ int sd_is_socket_inet(int fd, int family, int type, int listening, uint
         assert_return(fd >= 0, -EBADF);
         assert_return(IN_SET(family, 0, AF_INET, AF_INET6), -EINVAL);
 
-        r = sd_is_socket_internal(fd, type, listening);
+        r = is_socket_internal(fd, type, listening);
         if (r <= 0)
                 return r;
 
@@ -310,7 +306,7 @@ _public_ int sd_is_socket_sockaddr(int fd, int type, const struct sockaddr* addr
         assert_return(addr_len >= sizeof(sa_family_t), -ENOBUFS);
         assert_return(IN_SET(addr->sa_family, AF_INET, AF_INET6), -EPFNOSUPPORT);
 
-        r = sd_is_socket_internal(fd, type, listening);
+        r = is_socket_internal(fd, type, listening);
         if (r <= 0)
                 return r;
 
@@ -365,7 +361,7 @@ _public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path
 
         assert_return(fd >= 0, -EBADF);
 
-        r = sd_is_socket_internal(fd, type, listening);
+        r = is_socket_internal(fd, type, listening);
         if (r <= 0)
                 return r;
 
@@ -415,7 +411,7 @@ _public_ int sd_is_mq(int fd, const char *path) {
         }
 
         if (path) {
-                char fpath[PATH_MAX];
+                _cleanup_free_ char *fpath = NULL;
                 struct stat a, b;
 
                 assert_return(path_is_absolute(path), -EINVAL);
@@ -423,14 +419,14 @@ _public_ int sd_is_mq(int fd, const char *path) {
                 if (fstat(fd, &a) < 0)
                         return -errno;
 
-                strncpy(stpcpy(fpath, "/dev/mqueue"), path, sizeof(fpath) - 12);
-                fpath[sizeof(fpath)-1] = 0;
+                fpath = path_join("/dev/mqueue", path);
+                if (!fpath)
+                        return -ENOMEM;
 
                 if (stat(fpath, &b) < 0)
                         return -errno;
 
-                if (a.st_dev != b.st_dev ||
-                    a.st_ino != b.st_ino)
+                if (!stat_inode_same(&a, &b))
                         return 0;
         }
 
@@ -451,7 +447,7 @@ _public_ int sd_pid_notify_with_fds(
                 .msg_iovlen = 1,
                 .msg_name = &sockaddr,
         };
-        _cleanup_close_ int fd = -1;
+        _cleanup_close_ int fd = -EBADF;
         struct cmsghdr *cmsg = NULL;
         const char *e;
         bool send_ucred;
@@ -644,7 +640,7 @@ _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
         r = safe_atou64(s, &u);
         if (r < 0)
                 goto finish;
-        if (u <= 0 || u >= USEC_INFINITY) {
+        if (!timestamp_is_set(u)) {
                 r = -EINVAL;
                 goto finish;
         }