Upload Tizen:Base source
[framework/base/util-linux-ng.git] / sys-utils / setsid.c
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  */
13
14 #include <stdio.h>
15 #include <unistd.h>
16 #include <stdlib.h>
17 #include "nls.h"
18
19 int
20 main(int argc, char *argv[]) {
21         setlocale(LC_ALL, "");
22         bindtextdomain(PACKAGE, LOCALEDIR);
23         textdomain(PACKAGE);
24         
25         if (argc < 2) {
26                 fprintf(stderr, _("usage: %s program [arg ...]\n"),
27                         argv[0]);
28                 exit(1);
29         }
30         if (getpgrp() == getpid()) {
31                 switch(fork()){
32                 case -1:
33                         perror("fork");
34                         exit(1);
35                 case 0:         /* child */
36                         break;
37                 default:        /* parent */
38                         exit(0);
39                 }
40         }
41         if (setsid() < 0) {
42                 perror("setsid"); /* cannot happen */
43                 exit(1);
44         }
45         execvp(argv[1], argv + 1);
46         perror("execvp");
47         exit(1);
48 }