Root/tools/lib/daemon.c

Source at commit 8c574484b889c7b17d2ba4b6929947050f87d91e created 6 years 3 months ago.
By Josef Filzmaier, atusb/fw: Introduce DEBUG flag
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