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