[systemd-devel] [PATCH] Add sd_is_special for special file descriptors

Jim Meyering jim at meyering.net
Wed Jun 15 11:29:51 PDT 2011


William Douglas wrote:
>>From 85f51557a62be9224ed475047aff503bece342b4 Mon Sep 17 00:00:00 2001
> From: William Douglas <william.douglas at intel.com>
> Date: Wed, 15 Jun 2011 11:08:17 -0700
>
> 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 |   33 +++++++++++++++++++++++++++++++++
>  src/sd-daemon.h |   12 ++++++++++++
>  2 files changed, 45 insertions(+), 0 deletions(-)
>
> diff --git a/src/sd-daemon.c b/src/sd-daemon.c
> index d9f23d6..3cac4d5 100644
> --- a/src/sd-daemon.c
> +++ b/src/sd-daemon.c
> @@ -169,6 +169,39 @@ _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;
> +
> +        memset(&st_fd, 0, sizeof(st_fd));
> +        if (fstat(fd, &st_fd) < 0)
> +                return -errno;

Hello,
I notice that other code in this file does the same thing,
so maybe you're just following that example, but why bother
to clear the bits of a struct stat before calling fstat or stat
to fill it in?

Aren't the two memset calls here unnecessary?

> +        if (!S_ISREG(st_fd.st_mode))
> +                return 0;
> +
> +        if (path) {
> +                struct stat st_path;
> +
> +                memset(&st_path, 0, sizeof(st_path));
> +                if (stat(path, &st_path) < 0) {
> +
> +                        if (errno == ENOENT || errno == ENOTDIR)
> +                                return 0;
> +
> +                        return -errno;
> +                }
> +
> +                return
> +                        st_path.st_dev == st_fd.st_dev &&
> +                        st_path.st_ino == st_fd.st_ino;
> +        }
> +
> +        return 1;
> +}


More information about the systemd-devel mailing list