]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sd-daemon: fix sd_is_mq for non-mq fds 1422/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Sat, 5 Sep 2015 13:20:15 +0000 (15:20 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 30 Sep 2015 18:23:13 +0000 (14:23 -0400)
mq_getattr returns -1/EBADF for file descriptors which are not mq.
But we should return 0 in this case.

We first check that fd is a valid fd, so we can assume that if
mq_getattr returns EBADF, it is simply a non-mq fd. There is a slight
race, but there doesn't seem to be a nice way to fix it.

src/libsystemd/sd-daemon/sd-daemon.c

index ba7b8da85fbbbb2befa7bcb24271d4fed0116b04..5fb0b73d02ca3f5f79b414a59faadce6167d23dc 100644 (file)
@@ -310,10 +310,15 @@ _public_ int sd_is_socket_unix(int fd, int type, int listening, const char *path
 _public_ int sd_is_mq(int fd, const char *path) {
         struct mq_attr attr;
 
-        assert_return(fd >= 0, -EBADF);
+        /* Check that the fd is valid */
+        assert_return(fcntl(fd, F_GETFD) >= 0, -errno);
 
-        if (mq_getattr(fd, &attr) < 0)
+        if (mq_getattr(fd, &attr) < 0) {
+                if (errno == EBADF)
+                        /* A non-mq fd (or an invalid one, but we ruled that out above) */
+                        return 0;
                 return -errno;
+        }
 
         if (path) {
                 char fpath[PATH_MAX];