[comp.unix.questions] Signal handlers and errno

MANNS%DBNPIB5.BITNET@cunyvm.cuny.edu (Jochen Manns, PI der Uni Bonn, 732738/3611) (01/04/91)

Dear UNIX programmers,

        I got the following problem with a signal handler and the
        global 'errno' variable:

        a) there is a main program, doing something like semop or
           select in the form:
                while ( system_call() == -1 )
                 if ( errno != EINTR )
                  fatal_error();
        b) now, a signal handler is installed via 'alarm', which has
           various tasks. Especially it may get some 'errno' from the
           system calls it makes.
        c) so, if the handler interrupts the system_call in the main
           this call will fail with EINTR - no problem so far. But if
           the handler is called again before EINTR is checked - which
           is possible in a heavy loaded multitasking system even when
           using 'alarm' - this may crash the program when it becomes
           a modified 'errno'.

        For now I keep track on 'errno' in the signal handler but I
        want to ask you if there is no better method to solve such
        problems. Is't it a general problem with handlers? Can some
        of you give me some hints on that problem?



                        Jochen Manns
                        Physikalisches Institut der Universitaet Bonn
                        Nussallee 12
                        5300 Bonn 1
                        (Germany)

brnstnd@kramden.acf.nyu.edu (Dan Bernstein) (01/05/91)

In article <25390@adm.brl.mil> MANNS%DBNPIB5.BITNET@cunyvm.cuny.edu (Jochen Manns, PI der Uni Bonn, 732738/3611) writes:
  [ a signal handler may modify errno ]
>         For now I keep track on 'errno' in the signal handler but I
>         want to ask you if there is no better method to solve such
>         problems.

Saving and restoring errno is the correct method. The only ``better''
solution is not to make system calls inside handlers in the first place.

---Dan