Updates to Tomato RAF including NGINX && PHP
[tomato.git] / release / src / router / minidlna / daemonize.c
blob7de16ad4ebbd18a4338330b623756282610c66bf
1 /* MiniUPnP project
2 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * Copyright (c) 2006, Thomas Bernard
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * * The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <unistd.h>
32 #include <fcntl.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <errno.h>
36 #include <string.h>
37 #include <signal.h>
39 #include "daemonize.h"
40 #include "config.h"
41 #include "log.h"
43 int
44 daemonize(void)
46 int pid;
47 #ifndef USE_DAEMON
48 int i;
50 switch(fork())
52 /* fork error */
53 case -1:
54 perror("fork()");
55 exit(1);
57 /* child process */
58 case 0:
59 /* obtain a new process group */
60 if( (pid = setsid()) < 0)
62 perror("setsid()");
63 exit(1);
66 /* close all descriptors */
67 for (i=getdtablesize();i>=0;--i) close(i);
69 i = open("/dev/null",O_RDWR); /* open stdin */
70 dup(i); /* stdout */
71 dup(i); /* stderr */
73 umask(027);
74 chdir("/");
76 break;
77 /* parent process */
78 default:
79 exit(0);
81 #else
82 if( daemon(0, 0) < 0 )
83 perror("daemon()");
84 pid = getpid();
85 #endif
86 return pid;
89 int
90 writepidfile(const char * fname, int pid)
92 char pidstring[16];
93 int pidstringlen;
94 int pidfile;
96 if(!fname || (strlen(fname) == 0))
97 return -1;
99 if( (pidfile = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0)
101 DPRINTF(E_INFO, L_GENERAL, "Unable to open pidfile for writing %s: %s\n", fname, strerror(errno));
102 return -1;
105 pidstringlen = snprintf(pidstring, sizeof(pidstring), "%d\n", pid);
106 if(pidstringlen <= 0)
108 DPRINTF(E_INFO, L_GENERAL,
109 "Unable to write to pidfile %s: snprintf(): FAILED\n", fname);
110 close(pidfile);
111 return -1;
113 else
115 if(write(pidfile, pidstring, pidstringlen) < 0)
116 DPRINTF(E_INFO, L_GENERAL, "Unable to write to pidfile %s: %s\n", fname, strerror(errno));
119 close(pidfile);
121 return 0;
125 checkforrunning(const char * fname)
127 char buffer[64];
128 int pidfile;
129 pid_t pid;
131 if(!fname || (strlen(fname) == 0))
132 return -1;
134 if( (pidfile = open(fname, O_RDONLY)) < 0)
135 return 0;
137 memset(buffer, 0, 64);
139 if(read(pidfile, buffer, 63))
141 if( (pid = atol(buffer)) > 0)
143 if(!kill(pid, 0))
145 close(pidfile);
146 return -2;
151 close(pidfile);
153 return 0;