1  /*
       2   * setsid.c -- execute a command in a new session
       3   * Rick Sladkey <jrs@world.std.com>
       4   * In the public domain.
       5   *
       6   * 1999-02-22 Arkadiusz MiĆkiewicz <misiek@pld.ORG.PL>
       7   * - added Native Language Support
       8   *
       9   * 2001-01-18 John Fremlin <vii@penguinpowered.com>
      10   * - fork in case we are process group leader
      11   *
      12   * 2008-08-20 Daniel Kahn Gillmor <dkg@fifthhorseman.net>
      13   * - if forked, wait on child process and emit its return code.
      14   */
      15  
      16  #include <getopt.h>
      17  #include <stdio.h>
      18  #include <stdlib.h>
      19  #include <unistd.h>
      20  #include <sys/ioctl.h>
      21  #include <sys/types.h>
      22  #include <sys/wait.h>
      23  
      24  #include "c.h"
      25  #include "nls.h"
      26  #include "closestream.h"
      27  
      28  static void __attribute__((__noreturn__)) usage(void)
      29  {
      30  	FILE *out = stdout;
      31  	fputs(USAGE_HEADER, out);
      32  	fprintf(out, _(
      33  		" %s [options] <program> [arguments ...]\n"),
      34  		program_invocation_short_name);
      35  
      36  	fputs(USAGE_SEPARATOR, out);
      37  	fputs(_("Run a program in a new session.\n"), out);
      38  
      39  	fputs(USAGE_OPTIONS, out);
      40  	fputs(_(" -c, --ctty     set the controlling terminal to the current one\n"), out);
      41  	fputs(_(" -f, --fork     always fork\n"), out);
      42  	fputs(_(" -w, --wait     wait program to exit, and use the same return\n"), out);
      43  
      44  	printf(USAGE_HELP_OPTIONS(16));
      45  
      46  	printf(USAGE_MAN_TAIL("setsid(1)"));
      47  	exit(EXIT_SUCCESS);
      48  }
      49  
      50  int main(int argc, char **argv)
      51  {
      52  	int ch, forcefork = 0;
      53  	int ctty = 0;
      54  	pid_t pid;
      55  	int status = 0;
      56  
      57  	static const struct option longopts[] = {
      58  		{"ctty", no_argument, NULL, 'c'},
      59  		{"fork", no_argument, NULL, 'f'},
      60  		{"wait", no_argument, NULL, 'w'},
      61  		{"version", no_argument, NULL, 'V'},
      62  		{"help", no_argument, NULL, 'h'},
      63  		{NULL, 0, NULL, 0}
      64  	};
      65  
      66  	setlocale(LC_ALL, "");
      67  	bindtextdomain(PACKAGE, LOCALEDIR);
      68  	textdomain(PACKAGE);
      69  	close_stdout_atexit();
      70  
      71  	while ((ch = getopt_long(argc, argv, "+Vhcfw", longopts, NULL)) != -1)
      72  		switch (ch) {
      73  		case 'c':
      74  			ctty=1;
      75  			break;
      76  		case 'f':
      77  			forcefork = 1;
      78  			break;
      79  		case 'w':
      80  			status = 1;
      81  			break;
      82  
      83  		case 'h':
      84  			usage();
      85  		case 'V':
      86  			print_version(EXIT_SUCCESS);
      87  		default:
      88  			errtryhelp(EXIT_FAILURE);
      89  		}
      90  
      91  	if (argc - optind < 1) {
      92  		warnx(_("no command specified"));
      93  		errtryhelp(EXIT_FAILURE);
      94  	}
      95  
      96  	if (forcefork || getpgrp() == getpid()) {
      97  		pid = fork();
      98  		switch (pid) {
      99  		case -1:
     100  			err(EXIT_FAILURE, _("fork"));
     101  		case 0:
     102  			/* child */
     103  			break;
     104  		default:
     105  			/* parent */
     106  			if (!status)
     107  				return EXIT_SUCCESS;
     108  			if (wait(&status) != pid)
     109  				err(EXIT_FAILURE, "wait");
     110  			if (WIFEXITED(status))
     111  				return WEXITSTATUS(status);
     112  			err(status, _("child %d did not exit normally"), pid);
     113  		}
     114  	}
     115  	if (setsid() < 0)
     116  		/* cannot happen */
     117  		err(EXIT_FAILURE, _("setsid failed"));
     118  
     119  	if (ctty && ioctl(STDIN_FILENO, TIOCSCTTY, 1))
     120  		err(EXIT_FAILURE, _("failed to set the controlling terminal"));
     121  	execvp(argv[optind], argv + optind);
     122  	errexec(argv[optind]);
     123  }