glusterfs
65 строк · 1.2 Кб
1/*
2Copyright (c) 2008-2012 Red Hat, Inc. <http://www.redhat.com>
3This file is part of GlusterFS.
4
5This file is licensed to you under your choice of the GNU Lesser
6General Public License, version 3 or any later version (LGPLv3 or
7later), or the GNU General Public License, version 2 (GPLv2), in all
8cases as published by the Free Software Foundation.
9*/
10
11#include <unistd.h>12#include <stdio.h>13#include "glusterfs/daemon.h"14
15int
16os_daemon_return(int nochdir, int noclose)17{
18pid_t pid = -1;19int ret = -1;20FILE *ptr = NULL;21
22ret = fork();23if (ret)24return ret;25
26pid = setsid();27
28if (pid == -1) {29ret = -1;30goto out;31}32
33if (!nochdir)34ret = chdir("/");35
36if (!noclose) {37ptr = freopen(DEVNULLPATH, "r", stdin);38if (!ptr)39goto out;40
41ptr = freopen(DEVNULLPATH, "w", stdout);42if (!ptr)43goto out;44
45ptr = freopen(DEVNULLPATH, "w", stderr);46if (!ptr)47goto out;48}49
50ret = 0;51out:52return ret;53}
54
55int
56os_daemon(int nochdir, int noclose)57{
58int ret = -1;59
60ret = os_daemon_return(nochdir, noclose);61if (ret <= 0)62return ret;63
64_exit(0);65}
66