Skip to content

Commit

Permalink
Add FreeBSD support
Browse files Browse the repository at this point in the history
FreeBSD's `procctl` supports a similar death signal facility with
slightly different arguments.

from `procctl(2)`:
```
int procctl(idtype_t idtype, id_t id, int cmd, void *data);

P_PID   Control the process with the process ID id.  id zero is a
        shortcut for the calling process ID.

PROC_PDEATHSIG_CTL  Request the delivery of a signal when the parent of
                    the calling process exits.  idtype must be P_PID and
                    id must be the either caller's pid or zero, with no
                    difference in effect.  The value is cleared for
                    child processes and when executing set-user-ID or
                    set-group-ID binaries.  data must point to a value
                    of type int indicating the signal that should be
                    delivered to the caller.  Use zero to cancel a
                    previously requested signal delivery.

```
  • Loading branch information
jonyesno committed Nov 7, 2022
1 parent 19bfb52 commit 95088bd
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pact.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,25 @@
#ifdef __linux__
#include <sys/prctl.h>
#elif __FreeBSD__
#include <sys/procctl.h>
#endif
#include <sys/signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>

int do_ctl(int signo) {
#ifdef __linux__
return prctl(PR_SET_PDEATHSIG, signo, 0, 0, 0);
#elif __FreeBSD__
return procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &signo);
#else
# no platform support
return -1
#endif
}

int main(int argc, char **argv) {
if(argc < 2)
return 1;
Expand All @@ -16,8 +32,8 @@ int main(int argc, char **argv) {
cmd = 2;
}
if (!signo)
signo = 15;
if (prctl(PR_SET_PDEATHSIG, signo, 0, 0, 0) < 0) {
signo = SIGTERM;
if (do_ctl(signo) < 0) {
perror("prctl");
return 1;
}
Expand Down

0 comments on commit 95088bd

Please sign in to comment.