Page MenuHomePhabricator

[libc] Add Initial Support for Signals
ClosedPublic

Authored by abrachet on Feb 13 2020, 12:18 AM.

Details

Summary

This patch adds signal support on Linux. The current implementation gets the SIG* macros and types like sigset_t from <linux/signals.h>

This patch also adds raise(3), and internal routines block_all_signals and restore_signals

Diff Detail

Event Timeline

abrachet created this revision.Feb 13 2020, 12:18 AM
gchatelet added inline comments.Feb 13 2020, 1:22 AM
libc/src/signal/linux/signal.h
25

Let's wait for D74530 to be in.

abrachet updated this revision to Diff 244498.Feb 13 2020, 11:56 AM
abrachet added a subscriber: libc-commits.

Use new syscall from D74530

abrachet marked an inline comment as done.Feb 13 2020, 11:56 AM
abrachet updated this revision to Diff 244504.Feb 13 2020, 12:12 PM

Add missing include guards in src/signal/linux/signal.h

sivachandra accepted this revision.Feb 13 2020, 4:01 PM
This revision is now accepted and ready to land.Feb 13 2020, 4:01 PM
MaskRay added inline comments.Feb 13 2020, 4:28 PM
libc/config/linux/api.td
144

What is it 64?

MaskRay added inline comments.Feb 13 2020, 4:32 PM
libc/spec/linux.td
117

Signal,

libc/spec/stdc.td
198

,

MaskRay added inline comments.Feb 13 2020, 4:47 PM
libc/config/linux/api.td
144

65 may be a reasonable choice for most architectures.

MIPS may require 129. A MIPS signal mask contains 128 bits.

The Linux uapi may be stuck with #define SIGRTMAX _NSIG but a libc should not replicate that (mistake). A libc should have SIGRTMAX-1 == _NSIG, though SIGRTMAX is not necessarily a constant.

abrachet marked an inline comment as done.Feb 13 2020, 5:09 PM
abrachet added inline comments.
libc/config/linux/api.td
144

Good catch.

My /usr/include/x86_64-linux-gnu/asm/signal.h has:

#define NSIG		32
typedef unsigned long sigset_t;
// ... further down
#define SIGRTMIN	32
#define SIGRTMAX	_NSIG

This is where I am currently getting sigset_t and NSIG. Should we not rely on this header then and define these our self? I do want to use this include to give me sigset_t, struct sigaction, because I think this lessens the burden on us. I would want to hear your thoughts on how difficult (or easy) it is to maintain portability between architectures without using the Linux headers.

65 may be a reasonable choice for most architectures.

MIPS may require 129. A MIPS signal mask contains 128 bits.

We already have x86_64 specific directory, for header gen config here, it shouldn't be a big problem to make NSIG platform dependent, I'm happy to do it in this patch, if we step away from using <linux/signal.h>

sivachandra added inline comments.Feb 14 2020, 12:15 AM
libc/config/linux/api.td
144

Though I accepted, I was not sure about employing this pattern here. I am guilty of setting an example with this pattern and I think I did not get it correct entirely.

We should strive to use platform headers if available and avoid copying them into the libc tree. Considering that we are keeping the platform specific definitions open anyway (as in, one can add them to the signal.h.in file in this case), we are not really blocking any platform which wants such definitions from a libc.

Which means, we should not need to list these macros at all in this file (and also the sigset_t type).

libc/src/signal/linux/signal.h
23

I should have pointed this out earlier: this is x86/x86_64 specific. I am OK for now as it would cause a compilation error for other architectures where sigset_t is not of an integer type. We can fix it then unless you want to fix it now.

abrachet updated this revision to Diff 244739.Feb 14 2020, 12:56 PM
  • Remove macros we can get from linux/sginals.h
  • Add internal wrapper type to make it easier to deal with non integer sigset_t in the future.
abrachet marked 4 inline comments as done.Feb 14 2020, 1:02 PM
abrachet added inline comments.
libc/config/linux/api.td
144

I've removed those macros, except for SIG_BLOCK and friends because those are not defined in the linux headers. Are we happy with it like this?

libc/src/signal/linux/signal.h
23

I think this wrapper type is a good solution for when we need to deal with such sigset_t's.

sivachandra added inline comments.Feb 14 2020, 2:46 PM
libc/config/linux/api.td
144

On my machine, I do get SIG_BLOCK and friends from the linux headers. They are defined in asm-generic/signal-defs.h, which is included by asm/signal.h, which is included by linux/signal.h.

libc/src/signal/linux/signal.h
23

As long as it does not need a big surgery to extend, I am OK. I do not have any strong opinion here. Essentially because I have not worked on any other arch.

abrachet updated this revision to Diff 244803.Feb 14 2020, 6:38 PM

Remove unnecessary DefineIfNot<"SIG_BLOCK", "0"> and friends from config/linux/api.td

abrachet marked 4 inline comments as done.Feb 14 2020, 6:39 PM
abrachet added inline comments.
libc/config/linux/api.td
144

Indeed, good catch.

libc/src/signal/linux/signal.h
23

I feel the same way. I think that I should be able to keep any big changes inside Sigset in the future.

This revision was automatically updated to reflect the committed changes.
abrachet marked 2 inline comments as done.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 20 2020, 11:15 AM
gchatelet added inline comments.Feb 21 2020, 2:27 AM
libc/src/signal/linux/raise.cpp
18

This is yielding an unused variable error

abrachet marked 2 inline comments as done.Feb 21 2020, 5:51 PM
abrachet added inline comments.
libc/src/signal/linux/raise.cpp
18

Fixed. Thanks!