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

Lennart Poettering lennart at poettering.net
Wed Jun 15 13:21:04 PDT 2011


On Wed, 15.06.11 13:20, William Douglas (william.r.douglas at gmail.com) wrote:

> Ah right, I was only thinking of getting /proc/kmsg working =P.
> I have updated accordingly below.

Almost there now:

> +_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))
> +                        return
> +                                st_path.st_dev == st_fd.st_dev &&
> +                                st_path.st_ino == st_fd.st_ino;
> +                else
> +                        return st_path.st_rdev == st_fd.st_rdev;

if st_path refers to a normal file, but st_fd to a device node, then
you are accessing st_path.st_rdev which might not be initialized. Since
st_rdev is specific to device nodes you need to verify the type of
st_path, too.

Lennart

-- 
Lennart Poettering - Red Hat, Inc.


More information about the systemd-devel mailing list