sbase

suckless unix tools
git clone git://git.2f30.org/sbase
Log | Files | Refs | README | LICENSE

setsid.c (607B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <errno.h>
      3 #include <unistd.h>
      4 
      5 #include "util.h"
      6 
      7 static void
      8 usage(void)
      9 {
     10 	eprintf("usage: %s cmd [arg ...]\n", argv0);
     11 }
     12 
     13 int
     14 main(int argc, char *argv[])
     15 {
     16 	int savederrno;
     17 
     18 	ARGBEGIN {
     19 	default:
     20 		usage();
     21 	} ARGEND
     22 
     23 	if (!argc)
     24 		usage();
     25 
     26 	if (getpgrp() == getpid()) {
     27 		switch (fork()) {
     28 		case -1:
     29 			eprintf("fork:");
     30 		case 0:
     31 			break;
     32 		default:
     33 			return 0;
     34 		}
     35 	}
     36 	if (setsid() < 0)
     37 		eprintf("setsid:");
     38 	execvp(argv[0], argv);
     39 	savederrno = errno;
     40 	weprintf("execvp %s:", argv[0]);
     41 
     42 	_exit(126 + (savederrno == ENOENT));
     43 }