Re: Problem to read on a serial port

From: Geoff Clare (geoff_at_clare.See-My-Signature.invalid)
Date: 03/31/05


Date: Thu, 31 Mar 2005 14:01:23 +0100

Jens.Toerring@physik.fu-berlin.de wrote, on Thu, 31 Mar 2005:

> What do you think about the
>
> fcntl(fd, F_SETFL, 0);
>
> Wouldn't that also clear out other flags than O_NDELAY, which
> might have unintended effects (at least I don't know if there's
> a consensus between systems which flags exist at all and which
> get influenced by fcntl())? So, shouldn't one use e.g.
>
> fcntl(fd, F_SETFL, fcntl( fd, F_GET_FL ) & ~ O_NDELAY );
>
> to explicitely only reset the non-delay flag?

In the general case the get-and-set method should always be used.
According to POSIX fcntl(fd, F_SETFL, 0) would turn off O_APPEND,
O_NONBLOCK, O_SYNC, O_DSYNC and O_RSYNC if any of them were set.
Implementations could also have additional non-standard flags for
use with fcntl F_[GS]ETFL (such as O_NDELAY) which would be
turned off if they were set.

However, if you know for sure that other flags were not set when
the fd was opened, and have not been set since, then
fcntl(fd, F_SETFL, 0) should work fine to turn off only the
flags you know are set.

> Also, I didn't find
> 'FNDELAY' in POSIX, so wouldn't it be better to use O_NDELAY? Or
> is there some magic involved with the use of 'FNDELAY'?

Neither FNDELAY nor O_NDELAY are in POSIX. It is best to use
O_NONBLOCK.

> Another thing I am not sure about is what read() is supposed to
> return in non-blocking mode when no data are available. Is it
> really true that read() should return 0 in that case and not -1
> with errno being set to EAGAIN?

The zero return is the historical behaviour with O_NDELAY, and the
fact that it is broken-by-design (because it can't be distinguished
from EOF) is the reason POSIX invented O_NONBLOCK to replace O_NDELAY.
When O_NONBLOCK is used read() must indicate that no data are
available by returning -1 and setting errno to EAGAIN.

-- 
Geoff Clare <netnews@gclare.org.uk>


Relevant Pages

  • Re: Serial port: tcgetattr and tcsetattr
    ... >> flags, or elements to be added to the termios struct. ... POSIX contains a very detailed description of exactly how ... exactly how the termios configuration should be *after* a POSIX ... behavior of options in the termios structure. ...
    (comp.unix.programmer)
  • RE: *at family of syscalls in FreeBSD
    ... getting parameters/behavior right seemed a little more useful than preparing ... EBADF if the path isn't an absolute path AND the fd is invalid, ... POSIX doesn't seem to have rmdirat (yes, ... Looks like POSIX just overloads unlinkatwith a new flags parameter ...
    (freebsd-arch)
  • Re: *at family of syscalls in FreeBSD
    ... necessarily stable in the standard. ... linux implements flags too. ... POSIX doesn't seem to have rmdirat (yes, ... Can't say that's my favorite API, ...
    (freebsd-arch)
  • Re: Serial port: tcgetattr and tcsetattr
    ... >>terminal interfaces behave (in a conforming environment). ... >>terminal flags are run in a conforming environment. ... > There is no requirement that applications use only the POSIX-defined ... POSIX then obviously POSIX no longer makes any guarantee about what ...
    (comp.unix.programmer)
  • Re: Serial port: tcgetattr and tcsetattr
    ... >>contributing factors that determine what a conforming environment ... > flags" because POSIX explicitly allows additional options, ... > flags, or elements to be added to the termios struct. ... terminal interfaces behave. ...
    (comp.unix.programmer)