Skip to content

Commit 370b554

Browse files
committed
Implement --exit-with-parent option.
1 parent 5864ab2 commit 370b554

4 files changed

Lines changed: 71 additions & 1 deletion

File tree

‎configure.ac‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,9 @@ resources when it runs.])])
127127
dnl restore CFLAGS
128128
CFLAGS="${acx_nbdkit_save_CFLAGS}"
129129

130+
dnl Check for other headers, all optional.
131+
AC_CHECK_HEADERS([sys/prctl.h])
132+
130133
dnl Check for valgrind.
131134
AC_CHECK_PROG([VALGRIND],[valgrind],[valgrind],[no])
132135

‎docs/nbdkit.pod‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,15 @@ See L</PROBING CONFIGURATION AND PLUGINS> below.
8181
Dump out information about the plugin and exit.
8282
See L</PROBING CONFIGURATION AND PLUGINS> below.
8383

84+
=item B<--exit-with-parent>
85+
86+
If the parent process exits, we exit. This can be used to avoid
87+
complicated cleanup or orphaned nbdkit processes. There are some
88+
important caveats with this, see L</EXIT WITH PARENT> below. An
89+
alternative to this is L</CAPTIVE NBDKIT> described below.
90+
91+
This option implies I<--foreground>.
92+
8493
=item B<-e> EXPORTNAME
8594

8695
=item B<--export> EXPORTNAME
@@ -364,6 +373,34 @@ Unix socket, like this:
364373

365374
nbdkit -U - plugin [args] --run '...'
366375

376+
=head1 EXIT WITH PARENT
377+
378+
The I<--exit-with-parent> option is almost the opposite of
379+
L</CAPTIVE NBDKIT> described in the previous section.
380+
381+
Running nbdkit with this option, for example from a script:
382+
383+
nbdkit --exit-with-parent plugin ...
384+
385+
means that nbdkit will exit automatically if the parent program exits
386+
for any reason. This can be used to avoid complicated cleanups or
387+
orphaned nbdkit processes.
388+
389+
I<--exit-with-parent> is incompatible with forking into the background
390+
(because when we fork into the background we lose track of the parent
391+
process). Therefore I<-f> / I<--foreground> is implied.
392+
393+
This is currently implemented using a feature of the Linux kernel, so
394+
it requires a Linux build of nbdkit and won't work on other operating
395+
systems (patches welcome to make it work).
396+
397+
If the parent application is multithreaded, then (in the Linux
398+
implementation) if the parent I<thread> exits, that will cause nbdkit
399+
to exit. Thus in multithreaded applications you usually want to run
400+
C<nbdkit --exit-with-parent> only from the main thread (unless you
401+
actually want nbdkit to exit with the thread, but that may not work
402+
reliably if we extend the implementation to other operating systems).
403+
367404
=head1 NEW STYLE VS OLD STYLE PROTOCOL
368405

369406
The NBD protocol comes in two incompatible forms that we call

‎src/internal.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
#endif
9393

9494
/* main.c */
95+
extern int exit_with_parent;
9596
extern const char *exportname;
9697
extern const char *ipaddr;
9798
extern int newstyle;

‎src/main.c‎

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
#include <sys/wait.h>
4848
#include <errno.h>
4949

50+
#ifdef HAVE_SYS_PRCTL_H
51+
#include <sys/prctl.h>
52+
#endif
53+
5054
#include <pthread.h>
5155

5256
#include <dlfcn.h>
@@ -68,6 +72,7 @@ static uid_t parseuser (const char *);
6872
static gid_t parsegroup (const char *);
6973
static unsigned int get_socket_activation (void);
7074

75+
int exit_with_parent; /* --exit-with-parent */
7176
const char *exportname; /* -e */
7277
int foreground; /* -f */
7378
const char *ipaddr; /* -i */
@@ -94,6 +99,7 @@ static const struct option long_options[] = {
9499
{ "help", 0, NULL, HELP_OPTION },
95100
{ "dump-config",0, NULL, 0 },
96101
{ "dump-plugin",0, NULL, 0 },
102+
{ "exit-with-parent", 0, NULL, 0 },
97103
{ "export", 1, NULL, 'e' },
98104
{ "export-name",1, NULL, 'e' },
99105
{ "exportname", 1, NULL, 'e' },
@@ -125,7 +131,8 @@ static void
125131
usage (void)
126132
{
127133
printf ("nbdkit [--dump-config] [--dump-plugin]\n"
128-
" [-e EXPORTNAME] [-f] [-g GROUP] [-i IPADDR]\n"
134+
" [-e EXPORTNAME] [--exit-with-parent] [-f]\n"
135+
" [-g GROUP] [-i IPADDR]\n"
129136
" [--newstyle] [--oldstyle] [-P PIDFILE] [-p PORT] [-r]\n"
130137
" [--run CMD] [-s] [-U SOCKET] [-u USER] [-v] [-V]\n"
131138
" PLUGIN [key=value [key=value [...]]]\n"
@@ -178,6 +185,16 @@ main (int argc, char *argv[])
178185
else if (strcmp (long_options[option_index].name, "dump-plugin") == 0) {
179186
dump_plugin = 1;
180187
}
188+
else if (strcmp (long_options[option_index].name, "exit-with-parent") == 0) {
189+
#ifdef PR_SET_PDEATHSIG
190+
exit_with_parent = 1;
191+
foreground = 1;
192+
#else
193+
fprintf (stderr, "%s: --exit-with-parent is not implemented for this operating system\n",
194+
program_name);
195+
exit (EXIT_FAILURE);
196+
#endif
197+
}
181198
else if (strcmp (long_options[option_index].name, "run") == 0) {
182199
if (socket_activation) {
183200
fprintf (stderr, "%s: cannot use socket activation with --run flag\n",
@@ -326,6 +343,18 @@ main (int argc, char *argv[])
326343
if (exportname == NULL)
327344
exportname = "";
328345

346+
/* Implement --exit-with-parent early in case plugin initialization
347+
* takes a long time and the parent exits during that time.
348+
*/
349+
#ifdef PR_SET_PDEATHSIG
350+
if (exit_with_parent) {
351+
if (prctl (PR_SET_PDEATHSIG, SIGTERM) == -1) {
352+
perror ("prctl: PR_SET_PDEATHSIG");
353+
exit (EXIT_FAILURE);
354+
}
355+
}
356+
#endif
357+
329358
/* Remaining command line arguments define the plugins and plugin
330359
* configuration. If --help or --version was specified, we still
331360
* partially parse these in order that we can display the per-plugin

0 commit comments

Comments
 (0)