chiark / gitweb /
sd-daemon: Add sd_is_special for special file descriptors
authorWilliam Douglas <william.r.douglas@gmail.com>
Thu, 16 Jun 2011 21:21:11 +0000 (14:21 -0700)
committerLennart Poettering <lennart@poettering.net>
Mon, 20 Jun 2011 15:54:17 +0000 (17:54 +0200)
With the addition of ListenSpecial as a socket option we need the
the usual sd_is_ functions for special files.  This patch does
that.

src/sd-daemon.c
src/sd-daemon.h

index d9f23d677ba6fed30368776e004d18444800a9a9..a2ec74cceb59a744f9bec7486972832896cdb2ff 100644 (file)
@@ -169,6 +169,42 @@ _sd_hidden_ int sd_is_fifo(int fd, const char *path) {
         return 1;
 }
 
+_sd_hidden_ int sd_is_special(int fd, const char *path) {
+        struct stat st_fd;
+
+        if (fd < 0)
+                return -EINVAL;
+
+        if (fstat(fd, &st_fd) < 0)
+                return -errno;
+
+        if (!S_ISREG(st_fd.st_mode) && !S_ISCHR(st_fd.st_mode))
+                return 0;
+
+        if (path) {
+                struct stat st_path;
+
+                if (stat(path, &st_path) < 0) {
+
+                        if (errno == ENOENT || errno == ENOTDIR)
+                                return 0;
+
+                        return -errno;
+                }
+
+                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;
+                else if (S_ISCHR(st_fd.st_mode) && S_ISCHR(st_path.st_mode))
+                        return st_path.st_rdev == st_fd.st_rdev;
+                else
+                        return 0;
+        }
+
+        return 1;
+}
+
 static int sd_is_socket_internal(int fd, int type, int listening) {
         struct stat st_fd;
 
index 884c36107d3439119ee8223a7f0f82e9e9ccb868..46dc7fd7e596a3f40c9d03d486e70ece67bab7b3 100644 (file)
@@ -123,6 +123,18 @@ int sd_listen_fds(int unset_environment);
 */
 int sd_is_fifo(int fd, const char *path);
 
+/*
+  Helper call for identifying a passed file descriptor. Returns 1 if
+  the file descriptor is a special character device on the file
+  system stored under the specified path, 0 otherwise.
+  If path is NULL a path name check will not be done and the call
+  only verifies if the file descriptor refers to a special character.
+  Returns a negative errno style error code on failure.
+
+  See sd_is_special(3) for more information.
+*/
+int sd_is_special(int fd, const char *path);
+
 /*
   Helper call for identifying a passed file descriptor. Returns 1 if
   the file descriptor is a socket of the specified family (AF_INET,