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
23 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
25 fputs(USAGE_HEADER
, out
);
27 " %s [options] <program> [arguments ...]\n"),
28 program_invocation_short_name
);
30 fputs(USAGE_OPTIONS
, out
);
31 fputs(_(" -c, --ctty set the controlling terminal to the current one\n"),
34 fputs(USAGE_HELP
, out
);
35 fputs(USAGE_VERSION
, out
);
37 fprintf(out
, USAGE_MAN_TAIL("setsid(1)"));
38 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
41 int main(int argc
, char **argv
)
46 static const struct option longopts
[] = {
47 {"ctty", no_argument
, NULL
, 'c'},
48 {"version", no_argument
, NULL
, 'V'},
49 {"help", no_argument
, NULL
, 'h'},
53 setlocale(LC_ALL
, "");
54 bindtextdomain(PACKAGE
, LOCALEDIR
);
57 while ((ch
= getopt_long(argc
, argv
, "+Vhc", longopts
, NULL
)) != -1)
60 printf(UTIL_LINUX_VERSION
);
74 if (getpgrp() == getpid()) {
77 err(EXIT_FAILURE
, _("fork"));
88 err(EXIT_FAILURE
, _("setsid failed"));
91 if (ioctl(STDIN_FILENO
, TIOCSCTTY
, 1))
92 warn(_("failed to set the controlling terminal"));
94 execvp(argv
[optind
], argv
+ optind
+ 1);
95 err(EXIT_FAILURE
, _("execvp failed"));