News for hackers. Stuff that matters.
Re:All about Linux signals - Linux Programming Blog by javert on Monday April 02, 2012 @04:37PM (#3789939)
What exactly makes you say they are dangerous? Genuinely curious. Certain kinds of programmer error?

(I haven't finished reading the guide yet... apologies if it's in there... but I'd say I'm pretty familiar with signals.)

Re:All about Linux signals - Linux Programming Blog by gchpaco on Monday April 02, 2012 @05:48PM (#3790253)
Signals do not interact gracefully with the rest of libc or the kernel, let alone concurrency. Signals can come in and interrupt kernel system calls--this is what EINTR is there for. When in the signal handler itself, the list of libc functions it is safe to call is small; the Single UNIX specification only guarantees less than 120 functions. V7 signals are not reliable and so it is dangerous to be in the signal handler for an extended period of time; but even BSD and SysV reliable signals have a list of caveats as long as my arm. The synchronous (think SIGFPE or SIGSEGV)/asynchronous (think SIGINT) distinction is an additional complication that the interface needed like it needed an additional head. And I haven't even gotten into the interactions between signals and process groups. Basically signals are a primitive, crude and dangerous form of IPC that one is nonetheless obligated to pay attention to.

As a rule of thumb I generally prefer to use signal handlers to set a small amount of global data and nothing else, and have the main interrupt loop notice and deal with the condition. It's possible to use weird siglongjmp things to get to the main loop if you are not there already, but (like longjmp in general) it is kind of weird and bizarre.

Re:All about Linux signals - Linux Programming Blog by caf on Monday April 02, 2012 @07:50PM (#3790753)
The best thing to do in a signal handler is often to write() a single byte to a pipe.

Your main loop can then notice that the other end of the pipe is readable (the main loop is normally watching file descriptors for activity anyway).

Re:All about Linux signals - Linux Programming Blog by emmelaich on Monday April 02, 2012 @10:42PM (#3791210)
I believe this is referred to as the 'self pipe' trick. Dan Bernstein says he came up with it in 1990.

http://cr.yp.to/docs/selfpipe.html

Re:All about Linux signals - Linux Programming Blog by gchpaco on Monday April 02, 2012 @09:59PM (#3791113)
That's a good point, and what I actually did last time this came up. signalfd on Linux just codifies this convention, but it is (particularly in evented code) an excellent idea.
Re:All about Linux signals - Linux Programming Blog by javert on Monday April 02, 2012 @05:56PM (#3790284)
I've actually done some quite intensive work with signals, but the reason I asked the question was just to make sure I wasn't missing something. Yes, it's very complicated, and if one doesn't have a complete understanding, problems can arise.

When in the signal handler itself, the list of libc functions it is safe to call is small; the Single UNIX specification only guarantees less than 120 functions.

I don't think this is precisely accurate. I think it's safe to call libc functions anywhere, but the point is that you have to ensure that non-reentrant functions are not called simultaneously by the same thread.

One way to do that is to never call those functions in a signal handler, but if you really know what you're doing (i.e., you know the signal hanlder is not interrupting the function you want to call), you can call it in the signal handler.

Does this sound right? I'm not being pedantic; I'm actually trying to make sure I have it right in my head.

Re:All about Linux signals - Linux Programming Blog by caf on Monday April 02, 2012 @07:49PM (#3790746)
The problem is that you don't know what other functions the libc function you want to call itself calls under the hood.

For example, take the classic case of printf() - sure, you might be able to guarantee that your signal handler can never interrupt an ongoing printf() call elsewhere, but what if printf() calls malloc() internally, and your signal handler has interrupted a malloc()?

That's why there's a (short) list of async-signal-safe functions in POSIX (see http://pubs.opengroup.org/onlinepubs/9699919799/functions/V2...)

Re:All about Linux signals - Linux Programming Blog by calloc on Monday April 02, 2012 @05:13PM (#3790138)
There are certain race conditions if you rely on signals to interrupt system calls and the interrupt happens before the syscall is called (after setting the signal, but before the syscall happens).

Since your code could have been anywhere in the execution path you can only do very limited amount of things within the signal handler that won't cause catastrophic issues otherwise.

Re:All about Linux signals - Linux Programming Blog by javert on Monday April 02, 2012 @05:37PM (#3790213)
certain race conditions if you rely on signals to interrupt system calls and the interrupt happens before the syscall is called

I'm pretty familiar with signals, but I don't really know what the problem is here. Why would you rely on signals to interrupt system calls? Maybe if I know why you would do that, I will see why it's a problem if the signal comes before the syscall happens.

Re:All about Linux signals - Linux Programming Blog by spc476 on Monday April 02, 2012 @10:03PM (#3791122)
At work, we have a time-critical program. In that program, we need to lock a file in order to modify it safely. The files themselves are stored on NFS (don't ask---this was a design decision from long ago). fcntl() (used to lock files) does not include a timeout. We need a timeout because the program is time-critical. NFS is not a local filesystem, but across the network. If the network is flaky, we don't want to hang on locking the file.

So we have to rely upon SIGALRM (via setitimer()) to interrupt the fcntl() call so we don't hang indefinitely.

Re:All about Linux signals - Linux Programming Blog by javert on Tuesday April 03, 2012 @01:30AM (#3791617)
Interesting, thanks.
Re:All about Linux signals - Linux Programming Blog by X-Istence on Tuesday April 03, 2012 @05:12AM (#3792140)
The examples given in the article linked explain this pretty well actually...

For example, lets say you want Ctrl + C to quit the program (bare with me, very simple example), and you have a select() call. So you set up your signal handlers, and then start filling the required structures for the select() call, before select() is called though you receive a SIGINT, your signal handler simply sets a global flag that is checked when select() returns (with errno == EINTR). So now the flag is set, the handler has done its job, and select() gets called and your program now starts waiting on file descriptors.

What you really wanted to happen is that the program would see the flag, clean up nicely and quit. Now the user has to send a second Ctrl + C to interrupt the select() syscall and to have the code executed that checks for errno == EINTR and the global flag that was set in the signal handler.

This is but a simple example of a race condition that can exist. The SIGALRM example given by the other HN user is also an excellent example of when things can go awry when not intentioned, and unless you program your signal handlers with that in mind you may get results you weren't expecting.

Re:All about Linux signals - Linux Programming Blog by javert on Tuesday April 03, 2012 @02:18PM (#3794111)
Thanks.

That example doesn't live up to the hype that was mentioned by a previous poster and that had me worried. However, that hype was probably overstated.

For example, "there are certain race conditions if you rely on signals to interrupt system calls and the interrupt happens before the syscall is called" -> seems to imply there are race conditions inherent to the situation, not race conditions that can be introduced by programmer mistake (which is pretty obvious, IMO).

Also "since your code could have been anywhere in the execution path you can only do very limited amount of things within the signal handler that won't cause catastrophic issues otherwise" -> I just don't believe that; you can do lots of things in the signal handler if you know what you're doing.

Re:All about Linux signals - Linux Programming Blog by calloc on Tuesday April 03, 2012 @06:26PM (#3795115)
Sure, you can do lots of things in the signal handler, if you are careful, but that is the point, it is more likely you will make a mistake than not. It is very simple to think, let me add this to the signal handler it will make my life easier and cause all kinds of weird issues that happen randomly like a heisenbug...
Re:All about Linux signals - Linux Programming Blog by javert on Tuesday April 03, 2012 @01:26AM (#3791603)
Wow, I cannot fathom how I could be downvoted on an honest and purely technical question.
Re:All about Linux signals - Linux Programming Blog by antirez on Monday April 02, 2012 @05:12PM (#3790129)
reentrancy of everything called inside a signal handler, interrupted system calls to handle, complexity in masking/unmasking, reinstalling of signal handler, and so forth. It's a very complex system.
Re:All about Linux signals - Linux Programming Blog by javert on Monday April 02, 2012 @05:36PM (#3790209)
That's what I figured was being referred to. Those things aren't so bad if you really know what you're doing.