Date: Fri, 3 Mar 2017 16:43:02 +0100 From: Adam Nowacki <nowakpl@platinum.linux.pl> To: freebsd-hackers@freebsd.org Subject: Re: Discrepancy between kevent error codes with socketpair fds Message-ID: <c4444bb8-1329-732c-d1a5-f6fcb3c7cff9@platinum.linux.pl> In-Reply-To: <CAFYJ9ehKNWsLtG4FxzGM9p%2BPKM_ebVbU%2BynJRCrtT%2Bw0dxVgsg@mail.gmail.com> References: <CAFYJ9ehKNWsLtG4FxzGM9p%2BPKM_ebVbU%2BynJRCrtT%2Bw0dxVgsg@mail.gmail.com>
next in thread | previous in thread | raw e-mail | index | archive | help
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@freebsd.org mailing list
> https://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"
>
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?c4444bb8-1329-732c-d1a5-f6fcb3c7cff9>
