setsid.c - sbase - suckless unix tools
 (HTM) git clone git://git.suckless.org/sbase
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
       setsid.c (677B)
       ---
            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 int fflag = 0;
            8 
            9 static void
           10 usage(void)
           11 {
           12         eprintf("usage: %s [-f] cmd [arg ...]\n", argv0);
           13 }
           14 
           15 int
           16 main(int argc, char *argv[])
           17 {
           18         int savederrno;
           19 
           20         ARGBEGIN {
           21         case 'f':
           22                 fflag = 1;
           23                 break;
           24         default:
           25                 usage();
           26         } ARGEND
           27 
           28         if (!argc)
           29                 usage();
           30 
           31         if (fflag || getpgrp() == getpid()) {
           32                 switch (fork()) {
           33                 case -1:
           34                         eprintf("fork:");
           35                 case 0:
           36                         break;
           37                 default:
           38                         return 0;
           39                 }
           40         }
           41         if (setsid() < 0)
           42                 eprintf("setsid:");
           43         execvp(argv[0], argv);
           44         savederrno = errno;
           45         weprintf("execvp %s:", argv[0]);
           46 
           47         _exit(126 + (savederrno == ENOENT));
           48 }