Commit Graph

64 Commits

Author SHA1 Message Date
Panu Matilainen d5701c466e Implement SIG_DFL/SIG_IGN/SIG_ERR counterparts in rpmsq
RPMSQ_DFL is like passing NULL to rpmsqSetAction(), this already
set the default action but now we have a nice name for it too.
RPMSQ_IGN is the only special case needed in rpmsqSetAction() as
it corresponds to an actual function. Return RPMSQ_ERR for any
signal not supported by rpmsq.

This doesn't actually change any core behavior as such.
2017-05-10 14:20:21 +03:00
Panu Matilainen 6278fcfb39 Use bash-style 128 + signum exit code when exiting on signals 2017-05-10 13:59:06 +03:00
Panu Matilainen 8f7f85f2fb Don't warn about getting killed by SIGPIPE
SIGPIPE is business as usual when pipes are involved and require
no special warnings (that's why we're handling it after all), but
getting killed by other means seems still worth logging.
2017-05-10 13:59:00 +03:00
Panu Matilainen b60ce10ada Only add rpmsq-handled signals into the rpmsq-caught set 2017-05-10 13:58:53 +03:00
Panu Matilainen ff43d03d1f Static signal-related variables need to be thread local 2016-12-16 09:51:12 +02:00
Panu Matilainen bbdf819392 Literally blocking *all* signals is a bit too much
Programming errors like SIGSEGV and SIGBUS need to get through no
matter what and blocking them is undefined behavior anyway.
The odd man out in this list is SIGTSTP which is just otherwise useful
and not harmful since the process can be continued afterwards.
2016-12-15 15:15:33 +02:00
Panu Matilainen 120229d73a Replace sigprocmask() uses with pthread_sigmask()
We're not exactly thread-safe anywhere but lets at least use the variant
whose behavior is not decidedly undefined in a threaded environment.
2016-12-15 15:05:08 +02:00
Panu Matilainen 20f54a28ca Add an shortcut API for blocking/unblocking signals
For rpm's purposes blocking all signals and restoring to previous
status is the only necessary operation, make this part of the
official API to make it available everywhere and replace rpmdb
internal signal handling code with it.

Noteworthy points:
- The block/unblock operation is reference counted so these can be
  nested to arbitrary level without messing up things, much like
  the internal chroot API.
- The rpmdb code used to remove rpmsq signals from the blocked mask, but
  blocked should really mean blocked, really. The pending signals will be
  delivered when unblocked and there's no need for us to mess with it.
- Unlike the rpmdb variant, we now Run rpmsqPoll() *after* unblocking
  the signals. This actually matters now because rpmsqPoll() honors blocked
  signals since commit 07620f4ae7.
2016-12-15 14:12:33 +02:00
Panu Matilainen 4edaafc26f Remember the first signal to arrive, not last
This is more in line with how "normal" signals behave - if a signal
of the same type is already pending then the subsequent signals of
that type are dropped.
2016-12-15 12:40:31 +02:00
Panu Matilainen 07620f4ae7 Honor blocked signals during rpmsqPoll() too
Queued signals might get processed much much later than they arrive,
and almost anything can happen in the meanwhile. Such as program
blocking signals before entering a critical piece of code, which
routinely calls rpmsqPoll() underneath since the code is not always
critical. Such as rpmdb iterator init/free - sometimes they're
called during harmless read-only query, at other times they're in
middle of transaction...
2016-12-15 12:16:41 +02:00
Panu Matilainen b275b94061 Make signal queue on/off a global atomic state
We always want the signal queue either enabled for the certain set
of signals or completely disabled, make the API reflect that and
make the switch "atomic", ie signal delivery is disabled while
changing state.

Perhaps more importantly, this allows changing signal handlers "offline",
so an application can set its own signal handler for, say, SIGINT
*in case* the signal queue is activated.
2016-12-15 10:59:02 +02:00
Panu Matilainen 3ecd11318e Rename static handler function to make (name)space available 2016-12-15 10:59:02 +02:00
Panu Matilainen ff6b9c56d3 Refactor finding a signal in the table to a helper function 2016-12-15 10:59:02 +02:00
Panu Matilainen ab676d6300 Lets do something silly with the newly gained signal info argument
(sorry, found the temptation to say greetings to Lemmy overwhelming)
2016-12-14 18:08:39 +02:00
Panu Matilainen 524a29051e Support the info argument from rpmsqPoll() handlers
Copy the info contents on signal arrival, pass on to handlers during
poll round. There are some pointers in siginfo_t whose validity might
be questionable at the time we get it, but then those pointers like
address of segfault aren't exactly something to go poking at anyway.
2016-12-14 17:51:14 +02:00
Panu Matilainen 8c6c2a199d Make the signal queue actually customizable and useful
The original rpmsq API is somehow backwards and upside down: it allows
overriding the handler when enabling the queue, but if the queue is
to be enabled for a signal, then the only possible handler for
it is the action that stores the signal in the queue. If you wanted
to have some other kind of behavior you wouldn't want to enable the
queue for that signal to begin with!

What applications need is the ability to override what happens at
signal poll, not arrival time - arrival time is at odds with the
whole queue notion. So introduce a poll function which runs the
handlers for caught signals, clearing them on the way. Add to that
the notion of default handler which is called if no custom handler
is set. Now this can be trivially used to replace the signal foobar
inside rpmdb and suddenly applications can replace the standard behavior
with a simple

    void my_sigint_handler(....)
    {
        /* do foo */
    }

    rpmsqEnable(SIGINT, my_sigint_handler);

...and the custom hander will be called when a SIGINT has arrived
and the next signal polling round from rpmdb internals or whever
occurs.

rpmsqAction() is removed from the API/ABI here, but it's not as if
it ever was useful to anybody anyway.
2016-12-14 17:34:02 +02:00
Panu Matilainen f60131dae0 Drop support for systems without SA_SIGINFO
Life's too short to keep worrying about ancient junk forever.
Even Hurd has this now, can you imagine? (Hurd was the reason
this was special-cased back in 2008)
2016-12-14 15:32:21 +02:00
Panu Matilainen afbb9713ce Simplify rpmsq activate signals tracking
There's little better way of tracking set of signals than a sigset_t
with its associated APIs. For one, this way we dont for example need to
loop through the table to see if a signal is active or not.

Note that This drops the "fancy" reference counting: calling rpmsqEnable()
with different handlers would increase the refcount but not actually change
the behavior, so the refcount means exactly what? The refcounting also
hasn't been used by rpm at all/in a long time, because whether its
active or not is tracked by the rpmdb code which is the only place
really knowing if its needed or not.
2016-12-14 13:09:39 +02:00
Panu Matilainen 4639f4b2ba Use abs() when abs() is called for 2016-12-14 12:51:14 +02:00
Panu Matilainen ecc9c61e4b Remove useless signal table defines 2016-12-13 12:02:19 +02:00
Colin Walters 56f49d7f5a Add API to completely disable librpm's use of Unix signal handlers
On Tue, Feb 17, 2015, at 07:07 AM, Florian Festi wrote:
> Sorry, for the last response. DevConf takes its toll...
>
> On 01/23/2015 04:07 AM, Colin Walters wrote:
> > Numerous consumers of librpm use it in a pattern where they're
> > constructing fresh chroots.  For example, rpm-ostree operates this
> > way, and is used to provide atomic upgrades in concert with rpm.
> >
> > If the process dies due to SIGINT or another signal, the root can
> > simply be discarded.
> >
> > Currently today, rpm-ostree undoes the signal handlers after loading
> > librpm so that Control-C does what I want, but there's still a race
> > condition where the interrupt can be lost.
> >
> > Add an API so callers can disable the behavior.
>
> Is there any chance someone would want to switch them back on?

I can't think of one offhand...tools that interact with a live root
should be happy with what RPM does today, right?

> My gut
> feeling tells me this should rather be rpmsqSetInterruptSafety(int on);

But here's a patch which does it, in case you prefer it.  I did write
a better API doc this time.

From ae6d2de85b7b81cf91318183ba253402ac538785 Mon Sep 17 00:00:00 2001
From: Colin Walters <walters@verbum.org>
Date: Thu, 22 Jan 2015 17:57:14 -0500
Subject: [PATCH] Add API to disable librpm's use of Unix signal handlers

Numerous consumers of librpm use it in a pattern where they're
constructing fresh chroots.  For example, rpm-ostree operates this
way, and is used to provide atomic upgrades in concert with rpm.

If the process dies due to SIGINT or another signal, the root can
simply be discarded.

Currently today, rpm-ostree undoes the signal handlers after loading
librpm so that Control-C does what I want, but there's still a race
condition where the interrupt can be lost.

Add an API so callers can disable the behavior.
2015-02-20 15:54:21 +01:00
Panu Matilainen 269df02ae1 RIP rpmsqFork() + rpmsqWait() and the related bits
- Also remove additional thread protection: we're not supporting
  threads anywhere else either. If/when thread-protection is added,
  this is ulikely to be the first place anyway...
2011-05-03 11:21:19 +03:00
Panu Matilainen 4ddd89ce69 We're already considering <pthread.h> mandatory header elsewhere
- ... so might as well do so here too
2010-11-23 17:52:06 +02:00
Panu Matilainen 1a50e4a30b Axe the rpmsq debug code which was never getting built anyway 2010-11-23 17:49:16 +02:00
Panu Matilainen 94ce07d12f Lose all the stillborn code in rpmsq.[ch]
- rpmsqThread(), rpmsqJoin(), rpmsqThreadEqual() and rpmsqExecve()
  have never been used... get rid of them while we're bumping sonames
  anyway
2010-11-23 17:43:45 +02:00
Panu Matilainen 5d5cb6dce7 Make rpmsqAction compatible to sa_sigaction 2010-09-22 09:22:39 +03:00
Panu Matilainen 05b2d979e8 Move <stdio.h> includes out of system.h to the places that need it 2010-01-05 16:06:02 +02:00
Panu Matilainen dd58384ede Move <errno.h> include out of system.h to the places that need it 2010-01-05 15:25:31 +02:00
Panu Matilainen e30ad6d1af rpmsqAction() proto needs SA_SIGINFO conditionals too (ticket #4) 2008-11-06 10:03:10 +02:00
Panu Matilainen 73f61ecf05 Fix building on systems where SA_SIGINFO isn't available (ticket #4)
- patch from Adam Tkac
2008-11-04 10:28:20 +02:00
Panu Matilainen 6f010ec18d Unexport rpmsqCaught now that nothing needs it 2008-06-03 12:11:05 +03:00
Panu Matilainen cd040ba59b Add rpmsqIsCaught() for testing if a signal has been caught 2008-06-03 12:04:57 +03:00
Panu Matilainen df206dbaaf Unexpose signal queue and direct manipulation functions
- nothing outside rpmsq needs to fiddle with the queue directly
2008-06-03 11:44:20 +03:00
Panu Matilainen 4727c2a0aa Remove unused variable 2008-04-28 10:59:42 +03:00
Panu Matilainen 9ce13e09ef Switch to <rpm/foo.h> style for public headers
- adjust include paths accordingly
2007-12-08 14:02:32 +02:00
Ralf Corsépius abeea80a38 Use #include <x.h> syntax to include public headers. 2007-11-23 06:46:19 +01:00
Panu Matilainen 7d032c3505 Minimal protection for rpmsqElem internals 2007-11-08 15:59:45 +02:00
Ralf Corsépius 7075f64050 Include "rpmsq.h" instead of <rpmsq.h>. 2007-10-28 06:11:57 +01:00
Ralf Corsépius f982bbaa79 Remove splint tags. 2007-09-11 16:48:54 +02:00
Ralf Corsépius bc5bbd543f Remove __LCLINT__-ifdef'ed blocks. 2007-09-03 13:29:05 +02:00
Panu Matilainen 31592ebc7f Cast signal handler pointers to void* to avoid compiler warnings. 2007-07-10 13:34:17 +03:00
Paul Nasrat 5521d15370 Fork failure patch from OpenSuSE 2007-04-16 12:49:38 +01:00
Paul Nasrat 69927942b1 Fix from James Olin Oden for a scriptlet deadlock (#146549). 2007-02-22 12:26:56 +00:00
jbj 51b32ae130 Add porting sanity check (#159024).
CVS patchset: 7879
CVS date: 2005/07/13 10:47:56
2005-07-13 10:47:56 +00:00
jbj 6bb128c5f4 - don't build with sqlite3 if <sqlite3.h> is missing.
CVS patchset: 7764
CVS date: 2005/02/14 01:34:12
2005-02-14 01:34:12 +00:00
jbj d0615e9641 - macosx: change to --prefix=/usr rather than /opt/local.
- use waitpid rather than SIGCHLD reaper.
- rip out DB_PRIVATE revert if not NPTL, it's not the right thing to do.

CVS patchset: 7761
CVS date: 2005/02/13 03:01:09
2005-02-13 03:01:09 +00:00
jbj 6da6f7fc0a - mac os x patches (#131943,#131944,#132924,#132926).
CVS patchset: 7671
CVS date: 2005/01/04 17:46:10
2005-01-04 17:46:10 +00:00
jbj 9e30825226 - fix: don't set handler if SIG_IGN is already set (#134474).
CVS patchset: 7462
CVS date: 2004/10/14 15:09:07
2004-10-14 15:09:07 +00:00
jbj a36aa761ec More changes from rpm-4_3 branch.
CVS patchset: 7441
CVS date: 2004/10/09 20:22:01
2004-10-09 20:22:01 +00:00
jbj 25e004f33d Add comments, minor performance improvement.
CVS patchset: 7154
CVS date: 2004/03/07 21:11:27
2004-03-07 21:11:27 +00:00