morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

setsid.c (675B)


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