Root/tools/lib/daemon.c

Source at commit 1f003f1c2100f7f3670106fd13df0eaec7a0e4e1 created 4 years 2 months ago.
By Werner Almesberger, atusb/atusb.kicad_pcb: grow RF feed trace to 1.9 mm, for 1.0 mm PCB
1/*
2 * lib/daemon.c - Helper functions for proper daemonification
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdlib.h>
15#include <stdio.h>
16#include <unistd.h>
17
18#include "daemon.h"
19
20
21pid_t daemonize(void)
22{
23    pid_t pid;
24    int i;
25
26    pid = fork();
27    if (pid < 0) {
28        perror("fork");
29        exit(1);
30    }
31    if (pid)
32        return pid;
33    if (setsid() < 0) {
34        perror("setsid");
35        exit(1);
36    }
37    for (i = 0; i <= 2; i++)
38        (void) close(i);
39    (void) chdir("/");
40    return 0;
41}
42

Archive Download this file



interactive