Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.os.linux.development.apps > #670

Re: Linux O_NONBLOCK bug/ quirk

From Lusotec <nomail@nomail.not>
Newsgroups comp.os.linux.development.apps
Subject Re: Linux O_NONBLOCK bug/ quirk
Followup-To comp.os.linux.development.apps
Date 2014-03-28 23:13 +0000
Organization A noiseless patient Spider
Message-ID <lh4vnk$bdu$1@dont-email.me> (permalink)
References <878urvu0gx.fsf@sable.mobileactivedefense.com>

Followups directed to: comp.os.linux.development.apps

Show all headers | View raw


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

Rainer Weikusat wrote:
> As part of one of the usual 'pleasant exchanges' with the people whose
> ability to make a living depends on controlling access to the Linux code
> base,

Thats nonsense!

> it came to light that a receive operation on a socket in non-blocking mode 
> can actually be blocked forever on Linux, example code:
>
> ---------
> #include <fcntl.h>
> #include <string.h>
> #include <sys/socket.h>
> #include <sys/un.h>
> 
> int main(void)
> {
>     struct sockaddr_un sun;
>     int fd;
> 
>     fd = socket(AF_UNIX, SOCK_DGRAM, 0);
>     sun.sun_family = AF_UNIX;
>     strncpy(sun.sun_path, "/tmp/bla", sizeof(sun.sun_path));
>     bind(fd, (struct sockaddr *)&sun, sizeof(sun));
> 
>     if (fork() == 0) read(fd, &fd, sizeof(fd));
> 
>     sleep(1);
> 
>     fcntl(fd, F_SETFL, O_NONBLOCK);
>     read(fd, &fd, sizeof(fd));
> 
>     return 0;
> }
> --------
> 
> Killing the forked process results in the other aborting the read call
> with EAGAIN, as can be determined with strace.
> 
> I don't think this is of much practical relevance but it is something
> worth knowing about.

In the above code, both child and parent processes are reading from the same 
file descriptor.

Reads from a file descriptiors are queued and served in a fifo fashion. This 
is true for blocking and non-blocking reads. Even non-blocking reads still 
have to wait for any previous reads to complete, even if they are going to 
just return EAGAIN.

The issue with your code is that the file descriptor is set to non-blocking 
while the first read, a blocking read, is active. When a second read, this 
will be non-blocking, is made the first read is still blocking and thus the 
second non-blocking read has to wait for the first to finish.

Currently, in Linux fcntl affects future operations but not previous or 
current operations. As such, if a read is blocking a file descriptor, future 
reads, even if non-blocking will have to wait for the current blocking read 
to complete.

Now, for the code to work as you expect it (or at least as I understood your 
expectation), a fcntl must affect a already running operations. I think this 
is very problematic.

What are you trying to do by reading from the same socket in two processes, 
especially when you change the file descriptor status in the middle of the 
operations? Both are very unusual.

Regards
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)

iF4EAREIAAYFAlM2AjQACgkQGQjO2ccW76r7dQD/SENuSuyG79y3OZIpzRxbBnu+
Bi/vGVT28VPppASEJRoA+wSzEmQs7gYsRdlsQRttrJBN0+jvlk5i9pJH1GXr+uCZ
=p2OC
-----END PGP SIGNATURE-----

Back to comp.os.linux.development.apps | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Linux O_NONBLOCK bug/ quirk Rainer Weikusat <rweikusat@mobileactivedefense.com> - 2014-03-27 15:26 +0000
  Re: Linux O_NONBLOCK bug/ quirk crankypuss <crankypuss@nomail.invalid> - 2014-03-28 02:08 -0600
    Re: Linux O_NONBLOCK bug/ quirk Rainer Weikusat <rweikusat@mobileactivedefense.com> - 2014-03-28 12:45 +0000
      Re: Linux O_NONBLOCK bug/ quirk crankypuss <crankypuss@nomail.invalid> - 2014-03-29 05:19 -0600
  Re: Linux O_NONBLOCK bug/ quirk Rainer Weikusat <rweikusat@mobileactivedefense.com> - 2014-03-28 20:12 +0000
    Re: Linux O_NONBLOCK bug/ quirk unixb4coffee <unixb4coffee@gmail.com> - 2014-04-02 11:13 -0700
      Re: Linux O_NONBLOCK bug/ quirk Rainer Weikusat <rweikusat@mobileactivedefense.com> - 2014-04-03 16:36 +0100
        Re: Linux O_NONBLOCK bug/ quirk unixb4coffee <unixb4coffee@gmail.com> - 2014-04-03 11:43 -0700
          Re: Linux O_NONBLOCK bug/ quirk Rainer Weikusat <rweikusat@mobileactivedefense.com> - 2014-04-03 21:31 +0100
  Re: Linux O_NONBLOCK bug/ quirk Lusotec <nomail@nomail.not> - 2014-03-28 23:13 +0000
    Re: Linux O_NONBLOCK bug/ quirk Richard Kettlewell <rjk@greenend.org.uk> - 2014-03-29 11:15 +0000
    Re: Linux O_NONBLOCK bug/ quirk Rainer Weikusat <rweikusat@mobileactivedefense.com> - 2014-03-30 19:42 +0100
    Re: Linux O_NONBLOCK bug/ quirk Rainer Weikusat <rweikusat@mobileactivedefense.com> - 2014-04-16 12:42 +0100
      Re: Linux O_NONBLOCK bug/ quirk Rainer Weikusat <rweikusat@mobileactivedefense.com> - 2014-04-16 13:36 +0100

csiph-web