From: Zbigniew Jędrzejewski-Szmek Date: Sat, 5 Sep 2015 13:20:15 +0000 (+0200) Subject: sd-daemon: fix sd_is_mq for non-mq fds X-Git-Tag: v227.2^2~46 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=9277945f9aca8ed56f8575ab3c6bab5ace859d3a;p=elogind.git sd-daemon: fix sd_is_mq for non-mq fds 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. --- diff --git a/src/libelogind/sd-daemon/sd-daemon.c b/src/libelogind/sd-daemon/sd-daemon.c index af3dab7e4..7639b6d3b 100644 --- a/src/libelogind/sd-daemon/sd-daemon.c +++ b/src/libelogind/sd-daemon/sd-daemon.c @@ -315,10 +315,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];