chiark / gitweb /
core: add new logic for services to store file descriptors in PID 1
[elogind.git] / src / shared / util.c
index bda3c93ae38d91ddb54e74bb44131dcab269c5c8..f01022ed0b3a6f20ba9ba39919bbce8a19bdb7f9 100644 (file)
@@ -7672,3 +7672,28 @@ int fd_setcrtime(int fd, usec_t usec) {
 
         return 0;
 }
+
+int same_fd(int a, int b) {
+        struct stat sta, stb;
+
+        assert(a >= 0);
+        assert(b >= 0);
+
+        if (a == b)
+                return true;
+
+        if (fstat(a, &sta) < 0)
+                return -errno;
+
+        if (fstat(b, &stb) < 0)
+                return -errno;
+
+        if ((sta.st_mode & S_IFMT) != (stb.st_mode & S_IFMT))
+                return false;
+
+        if (S_ISREG(sta.st_mode) || S_ISDIR(sta.st_mode) || S_ISFIFO(sta.st_mode) || S_ISSOCK(sta.st_mode) || S_ISLNK(sta.st_mode))
+                return (sta.st_dev == stb.st_dev) && (sta.st_ino == stb.st_ino);
+
+        /* We consider all device fds different... */
+        return false;
+}