server/captive.c
/* Handle the --run option.  If run is NULL, does nothing.  If run is
 * not NULL then run nbdkit as a captive subprocess of the command.
 */
Handle the --run option.  If run is NULL, does nothing.  If run is
not NULL then run nbdkit as a captive subprocess of the command.
Handle the -- run option . If run is NULL does nothing . If run is not NULL then run nbdkit as a captive subprocess of the command .
void
run_command (void)
{
  char *url;
  char *cmd;
  int r;
  pid_t pid;
  if (!run)
    return;
  if (strstr (run, "guestfish")) {
    if (port)
      r = asprintf (&url, "nbd://localhost:%s", port);
    else if (unixsocket)
      r = asprintf (&url, "nbd://?socket=%s", unixsocket);
    else
      abort ();
  }
  else  {
    if (port)
      r = asprintf (&url, "nbd:localhost:%s", port);
    else if (unixsocket)
      r = asprintf (&url, "nbd:unix:%s", unixsocket);
    else
      abort ();
  }
  if (r == -1) {
    perror ("asprintf");
    exit (EXIT_FAILURE);
  }
  r = asprintf (&cmd,
                "nbd='%s'\n"
                "port='%s'\n"
                "unixsocket='%s'\n"
                "%s",
                url, port ? port : "", unixsocket ? unixsocket : "", run);
  if (r == -1) {
    perror ("asprintf");
    exit (EXIT_FAILURE);
  }
  free (url);
  pid = fork ();
  if (pid == -1) {
    perror ("fork");
    exit (EXIT_FAILURE);
  }
  if (pid > 0) {              
    r = system (cmd);
    if (WIFEXITED (r))
      r = WEXITSTATUS (r);
    else if (WIFSIGNALED (r)) {
      fprintf (stderr, "%s: external command was killed by signal %d\n",
               program_name, WTERMSIG (r));
      r = 1;
    }
    else if (WIFSTOPPED (r)) {
      fprintf (stderr, "%s: external command was stopped by signal %d\n",
               program_name, WSTOPSIG (r));
      r = 1;
    }
    kill (pid, SIGTERM);        
    _exit (r);
  }
  free (cmd);
  debug ("forked into background (new pid = %d)", getpid ());
}
void run_command ( void ) { char * url ; char * cmd ; int r ; pid_t pid ; if ( ! run ) return ; if ( strstr ( run , " " ) ) { if ( port ) r = asprintf ( & url , " " , port ) ; else if ( unixsocket ) r = asprintf ( & url , " " , unixsocket ) ; else abort ( ) ; } else { if ( port ) r = asprintf ( & url , " " , port ) ; else if ( unixsocket ) r = asprintf ( & url , " " , unixsocket ) ; else abort ( ) ; } if ( r == -1 ) { perror ( " " ) ; exit ( EXIT_FAILURE ) ; } r = asprintf ( & cmd , " \n " " \n " " \n " " " , url , port ? port : " " , unixsocket ? unixsocket : " " , run ) ; if ( r == -1 ) { perror ( " " ) ; exit ( EXIT_FAILURE ) ; } free ( url ) ; pid = fork ( ) ; if ( pid == -1 ) { perror ( " " ) ; exit ( EXIT_FAILURE ) ; } if ( pid > 0 ) { r = system ( cmd ) ; if ( WIFEXITED ( r ) ) r = WEXITSTATUS ( r ) ; else if ( WIFSIGNALED ( r ) ) { fprintf ( stderr , " \n " , program_name , WTERMSIG ( r ) ) ; r = 1 ; } else if ( WIFSTOPPED ( r ) ) { fprintf ( stderr , " \n " , program_name , WSTOPSIG ( r ) ) ; r = 1 ; } kill ( pid , SIGTERM ) ; _exit ( r ) ; } free ( cmd ) ; debug ( " " , getpid ( ) ) ; }
Handle the --run option.
Handle the -- run option .
/* Construct an nbd "URL".  Unfortunately guestfish and qemu take
   * different syntax, so try to guess which one we need.
   */ /* XXX escaping? */ /* qemu */ /* Construct the final command including shell variables. */ /* XXX Escaping again. */ /* Fork.  Captive nbdkit runs as the child process. */ /* Parent process is the run command. */ /* Kill captive nbdkit. */