restoring errno in signal handlers

  • Thread starter Hallvard B Furuseth
  • Start date
H

Hallvard B Furuseth

Signal handlers may call some OS functions - but these commonly can set
errno. As far as the rest of the program is concerned, errno can thus
be set to a garbage value at any time except when signals are blocked.

What's the best way to deal with this? Should the signal handler just
do

void handler(int sig) {
save_errno = errno; ... errno = save_errno;
}

or are reads safer than writes (even of the same value) so they
should do
... if (errno != save_errno) errno = save_errno;

Or can even a read scramble a an access in the the main program,
so the safest route is "your program should work even if errno
gets set to garbage once in a while"?
 
R

Roberto Divia

Hallvard said:
Signal handlers may call some OS functions - but these commonly can set
errno. As far as the rest of the program is concerned, errno can thus
be set to a garbage value at any time except when signals are blocked.

What's the best way to deal with this?

I have the feeling that the safest strategy in this scenario would be to have
the signal handler set an atomic flag to be served outside the exception handler...

Ciao,
--
Roberto Divia` Love at first sight is one of the greatest
Dep:pH Bat:53 Mailbox:C02110 labour-saving devices the world has ever seen
Route de Meyrin 385 ---------------------------------------------
Case Postale Phone: +41-22-767-4994
CH-1211 Geneve 23 CERN Fax: +41-22-767-9585
Switzerland E-Mail: (e-mail address removed)
 
H

Hallvard B Furuseth

Roberto said:
I have the feeling that the safest strategy in this scenario would be
to have the signal handler set an atomic flag to be served outside the
exception handler...

True enough, but that's not always practical.
 
K

Kaz Kylheku

Signal handlers may call some OS functions - but these commonly can set
errno. As far as the rest of the program is concerned, errno can thus
be set to a garbage value at any time except when signals are blocked.

ISO C is of no help, nor is The Single Unix Specification. SUS Issue 6 online
says:

If the signal occurs other than as the result of calling abort(),
raise(), kill(), pthread_kill(), or sigqueue(), the behavior is
undefined [ ... ] if the signal handler calls any function in the
standard library other than one of the functions listed in Signal
Concepts.

Furthermore, if such a call fails, the value of errno is unspecified.
What's the best way to deal with this? Should the signal handler just
do

void handler(int sig) {
save_errno = errno; ... errno = save_errno;
}

Many Unix programs do just this and it works for their intended target
platforms, but evidently this practice hasn't been codified by the
applicable standard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top