Discrepancy between kevent error codes with socketpair fds

Adam Nowacki nowakpl at platinum.linux.pl
Fri Mar 3 15:43:31 UTC 2017


Because the first descriptor is now a kqueue (kqfd == sv[0]). You can't add a kqueue descriptor to another kqueue so EINVAL. Second descriptor doesn't exist anymore so EBADF.

On 2017-03-03 15:11, ss griffon wrote:
> I'm hoping somebody can explain the following behavior to me.  It's not
> causing me any issues but I find it curious:
> 
> 1. open a socketpair (int sv[2])
> 2. close both ends
> 3. attempt to add sv[0] to kevent and "Invalid Argument" is returned
> (EINVAL)
> 4. attempt to add sv[1] to kevent instead of sv[0] and "Bad Descriptor" is
> returned (EBADF).
> 
> It doesn't seem to matter the order of closing the sockets.  Sample code is
> below.  Thanks in advance.
> 
> #include <sys/types.h>
> #include <sys/socket.h>
> #include <sys/event.h>
> #include <unistd.h>
> #include <stdio.h>
> #include <stdlib.h>
> #include <cstddef>
> 
> int main(int argc, char** argv)
> {
>     int sv[2];
> 
>     int err = socketpair(AF_LOCAL, SOCK_SEQPACKET, 0, sv);
>     if(err == -1)
>     {
>         perror("socketpair");
>         exit(1);
>     }
> 
>     err = close(sv[0]);
>     if(err == -1)
>     {
>         perror("close1");
>         exit(1);
>     }
> 
>     err = close(sv[1]);
>     if(err == -1)
>     {
>         perror("close2");
>         exit(1);
>     }
> 
>     int kqfd = kqueue();
>     if(kqfd == -1)
>     {
>         perror("kqueue");
>         exit(1);
>     }
> 
>     struct kevent event;
>     EV_SET(&event, sv[0], EVFILT_READ, EV_ADD, 0, 0, 0); //Change to sv[1]
> to get EBADF
> 
>     int events = kevent(kqfd, &event, 1,
>                         nullptr, 0, nullptr); //Returns EINVAL
>     if(events == -1)
>     {
>         perror("kevent");
>         exit(1);
>     }
> 
>     return 0;
> }
> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"
> 



More information about the freebsd-hackers mailing list