Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site hou3c.UUCP Path: utzoo!watmath!clyde!burl!hou3c!ka From: ka@hou3c.UUCP (Kenneth Almquist) Newsgroups: net.unix-wizards Subject: Re: The errno variable can get trashed Message-ID: <726@hou3c.UUCP> Date: Mon, 6-Aug-84 11:59:01 EDT Article-I.D.: hou3c.726 Posted: Mon Aug 6 11:59:01 1984 Date-Received: Wed, 8-Aug-84 08:05:12 EDT References: <511@calgary.UUCP> Organization: Bell Labs, Holmdel, NJ Lines: 34 The global 'errno' variable set by the assembler routines in the C library for system-call interface can be garbled before the program gets to look at it if a signal arrives and the signal-handling routine does another system call (which gets an error). This is a specific case of a general problem with signal handlers that modify global or static variables. For example, a call to malloc in a signal handler can result in a corrupted free space list. Radford suggests that the signal mechanism should save and restore the value of errno, but the only solution to the general problem is care on the part of the programmer. One way to fix Radford's example is to have the alarm routine simply set a flag for later processing. The code becomes: static int alflag; /* set when alarm signal received */ void i() { /* called on alarm signal */ alflag++; } void testalarm() { /* called from loop in main routine */ if (alflag) { alflag = 0; signal(SIGALARM, i); alarm(1); open(".", 1); /* this will fail */ } } Another fix would of course be to have the routine "i" save and restore the value of errno. Kenneth Almquist