master xplshn/aruu / cmd / pseudo / setsid.c
 1/* See LICENSE file for copyright and license details. */
 2
 3#include <errno.h>
 4#include <unistd.h>
 5
 6#include "util.h"
 7#include "wexec.h"
 8
 9static int fflag = 0;
10
11static void
12usage(void)
13{
14  eprintf("usage: %s [-f] cmd [arg ...]\n", argv0);
15}
16
17// ?man setsid: run in new session
18// ?man arguments: cmd [arg ...]
19// ?man run a program in a new session
20int
21main(int argc, char *argv[])
22{
23  int savederrno;
24
25  ARGBEGIN
26  {
27    // ?man -f: force the operation
28    case 'f':
29      fflag = 1;
30      break;
31    default:
32      usage();
33  }
34  ARGEND
35
36  if (!argc)
37    usage();
38
39  if (fflag || getpgrp() == getpid()) {
40    switch (fork()) {
41      case -1:
42        eprintf("fork:");
43      case 0:
44        break;
45      default:
46        return 0;
47    }
48  }
49  if (setsid() < 0)
50    eprintf("setsid:");
51  wexecvp_self(argv[0], argv);
52  savederrno = errno;
53  weprintf("wexecvp %s:", argv[0]);
54
55  _exit(126 + (savederrno == ENOENT));
56}