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

William Douglas william.r.douglas at gmail.com
Wed Jun 15 14:00:49 PDT 2011


Lennart Poettering <lennart at poettering.net> writes:

> 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

Awww I suck.

So I just went ahead and verified that st_fd and st_path
are the same type before doing the return comparison and if they aren't
the same type return false.



More information about the systemd-devel mailing list