util-linux/sys-utils/setsid.c

49 lines
911 B
C
Raw Normal View History

2006-12-06 17:25:32 -06:00
/*
* setsid.c -- execute a command in a new session
* Rick Sladkey <jrs@world.std.com>
* In the public domain.
2006-12-06 17:25:39 -06:00
*
* 1999-02-22 Arkadiusz Mi<EFBFBD>kiewicz <misiek@pld.ORG.PL>
2006-12-06 17:25:39 -06:00
* - added Native Language Support
*
* 2001-01-18 John Fremlin <vii@penguinpowered.com>
* - fork in case we are process group leader
*
2006-12-06 17:25:32 -06:00
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
2006-12-06 17:25:39 -06:00
#include "nls.h"
2006-12-06 17:25:32 -06:00
int
main(int argc, char *argv[]) {
2006-12-06 17:25:39 -06:00
setlocale(LC_ALL, "");
bindtextdomain(PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
2006-12-06 17:25:32 -06:00
if (argc < 2) {
2006-12-06 17:25:39 -06:00
fprintf(stderr, _("usage: %s program [arg ...]\n"),
2006-12-06 17:25:32 -06:00
argv[0]);
exit(1);
}
if (getpgrp() == getpid()) {
switch(fork()){
case -1:
perror("fork");
exit(1);
case 0: /* child */
break;
default: /* parent */
exit(0);
}
}
2006-12-06 17:25:32 -06:00
if (setsid() < 0) {
perror("setsid"); /* cannot happen */
2006-12-06 17:25:32 -06:00
exit(1);
}
execvp(argv[1], argv + 1);
perror("execvp");
exit(1);
}