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 *);
6872static gid_t parsegroup (const char * );
6973static unsigned int get_socket_activation (void );
7074
75+ int exit_with_parent ; /* --exit-with-parent */
7176const char * exportname ; /* -e */
7277int foreground ; /* -f */
7378const 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
125131usage (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