2 * setsid.c -- execute a command in a new session
3 * Rick Sladkey <jrs@world.std.com>
4 * In the public domain.
6 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
7 * - added Native Language Support
9 * 2001-01-18 John Fremlin <vii@penguinpowered.com>
10 * - fork in case we are process group leader
18 #include <sys/ioctl.h>
22 #include "closestream.h"
24 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
26 fputs(USAGE_HEADER
, out
);
28 " %s [options] <program> [arguments ...]\n"),
29 program_invocation_short_name
);
31 fputs(USAGE_OPTIONS
, out
);
32 fputs(_(" -c, --ctty set the controlling terminal to the current one\n"),
35 fputs(USAGE_HELP
, out
);
36 fputs(USAGE_VERSION
, out
);
38 fprintf(out
, USAGE_MAN_TAIL("setsid(1)"));
39 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
42 int main(int argc
, char **argv
)
47 static const struct option longopts
[] = {
48 {"ctty", no_argument
, NULL
, 'c'},
49 {"version", no_argument
, NULL
, 'V'},
50 {"help", no_argument
, NULL
, 'h'},
54 setlocale(LC_ALL
, "");
55 bindtextdomain(PACKAGE
, LOCALEDIR
);
59 while ((ch
= getopt_long(argc
, argv
, "+Vhc", longopts
, NULL
)) != -1)
62 printf(UTIL_LINUX_VERSION
);
76 if (getpgrp() == getpid()) {
79 err(EXIT_FAILURE
, _("fork"));
90 err(EXIT_FAILURE
, _("setsid failed"));
93 if (ioctl(STDIN_FILENO
, TIOCSCTTY
, 1))
94 warn(_("failed to set the controlling terminal"));
96 execvp(argv
[optind
], argv
+ optind
);
97 err(EXIT_FAILURE
, _("execvp failed"));