client: fix redirect ssl to ssl
[platform/upstream/libwebsockets.git] / lib / daemonize.c
1 /*
2  * This code is mainly taken from Doug Potter's page
3  *
4  * http://www-theorie.physik.unizh.ch/~dpotter/howto/daemonize
5  *
6  * I contacted him 2007-04-16 about the license for the original code,
7  * he replied it is Public Domain.  Use the URL above to get the original
8  * Public Domain version if you want it.
9  *
10  * This version is LGPL2.1+SLE like the rest of libwebsockets and is
11  * Copyright (c)2006 - 2013 Andy Green <andy@warmcat.com>
12  */
13
14 #include <stdlib.h>
15 #include <string.h>
16 #include <stdio.h>
17 #include <signal.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <limits.h>
22 #include <unistd.h>
23 #include <errno.h>
24
25 #include "private-libwebsockets.h"
26
27 int pid_daemon;
28 static char *lock_path;
29
30 int get_daemonize_pid()
31 {
32         return pid_daemon;
33 }
34
35 static void
36 child_handler(int signum)
37 {
38         int fd, len, sent;
39         char sz[20];
40
41         switch (signum) {
42
43         case SIGALRM: /* timed out daemonizing */
44                 exit(0);
45                 break;
46
47         case SIGUSR1: /* positive confirmation we daemonized well */
48
49                 if (lock_path) {
50                         /* Create the lock file as the current user */
51
52                         fd = open(lock_path, O_TRUNC | O_RDWR | O_CREAT, 0640);
53                         if (fd < 0) {
54                                 fprintf(stderr,
55                                    "unable to create lock file %s, code=%d (%s)\n",
56                                         lock_path, errno, strerror(errno));
57                                 exit(0);
58                         }
59                         len = sprintf(sz, "%u", pid_daemon);
60                         sent = write(fd, sz, len);
61                         if (sent != len)
62                                 fprintf(stderr,
63                                   "unable to write pid to lock file %s, code=%d (%s)\n",
64                                                      lock_path, errno, strerror(errno));
65
66                         close(fd);
67                 }
68                 exit(0);
69                 //!!(sent == len));
70
71         case SIGCHLD: /* daemonization failed */
72                 exit(0);
73                 break;
74         }
75 }
76
77 static void lws_daemon_closing(int sigact)
78 {
79         if (getpid() == pid_daemon)
80                 if (lock_path) {
81                         unlink(lock_path);
82                         lws_free_set_NULL(lock_path);
83                 }
84
85         kill(getpid(), SIGKILL);
86 }
87
88 /*
89  * You just need to call this from your main(), when it
90  * returns you are all set "in the background" decoupled
91  * from the console you were started from.
92  *
93  * The process context you called from has been terminated then.
94  */
95
96 LWS_VISIBLE int
97 lws_daemonize(const char *_lock_path)
98 {
99         struct sigaction act;
100         pid_t sid, parent;
101         int n, fd, ret;
102         char buf[10];
103
104         /* already a daemon */
105 //      if (getppid() == 1)
106 //              return 1;
107
108         if (_lock_path) {
109                 fd = open(_lock_path, O_RDONLY);
110                 if (fd >= 0) {
111                         n = read(fd, buf, sizeof(buf));
112                         close(fd);
113                         if (n) {
114                                 n = atoi(buf);
115                                 ret = kill(n, 0);
116                                 if (ret >= 0) {
117                                         fprintf(stderr,
118                                              "Daemon already running from pid %d\n", n);
119                                         exit(1);
120                                 }
121                                 fprintf(stderr,
122                                     "Removing stale lock file %s from dead pid %d\n",
123                                                                          _lock_path, n);
124                                 unlink(lock_path);
125                         }
126                 }
127
128                 n = strlen(_lock_path) + 1;
129                 lock_path = lws_malloc(n);
130                 if (!lock_path) {
131                         fprintf(stderr, "Out of mem in lws_daemonize\n");
132                         return 1;
133                 }
134                 strcpy(lock_path, _lock_path);
135         }
136
137         /* Trap signals that we expect to receive */
138         signal(SIGCHLD, child_handler); /* died */
139         signal(SIGUSR1, child_handler); /* was happy */
140         signal(SIGALRM, child_handler); /* timeout daemonizing */
141
142         /* Fork off the parent process */
143         pid_daemon = fork();
144         if (pid_daemon < 0) {
145                 fprintf(stderr, "unable to fork daemon, code=%d (%s)",
146                     errno, strerror(errno));
147                 exit(9);
148         }
149
150         /* If we got a good PID, then we can exit the parent process. */
151         if (pid_daemon > 0) {
152
153                /*
154                 * Wait for confirmation signal from the child via
155                 * SIGCHILD / USR1, or for two seconds to elapse
156                 * (SIGALRM).  pause() should not return.
157                 */
158                alarm(2);
159
160                pause();
161                /* should not be reachable */
162                exit(1);
163        }
164
165         /* At this point we are executing as the child process */
166         parent = getppid();
167         pid_daemon = getpid();
168
169         /* Cancel certain signals */
170         signal(SIGCHLD, SIG_DFL); /* A child process dies */
171         signal(SIGTSTP, SIG_IGN); /* Various TTY signals */
172         signal(SIGTTOU, SIG_IGN);
173         signal(SIGTTIN, SIG_IGN);
174         signal(SIGHUP, SIG_IGN); /* Ignore hangup signal */
175
176         /* Change the file mode mask */
177         umask(0);
178
179         /* Create a new SID for the child process */
180         sid = setsid();
181         if (sid < 0) {
182                 fprintf(stderr,
183                         "unable to create a new session, code %d (%s)",
184                         errno, strerror(errno));
185                 exit(2);
186         }
187
188         /*
189          * Change the current working directory.  This prevents the current
190          * directory from being locked; hence not being able to remove it.
191          */
192         if (chdir("/tmp") < 0) {
193                 fprintf(stderr,
194                         "unable to change directory to %s, code %d (%s)",
195                         "/", errno, strerror(errno));
196                 exit(3);
197         }
198
199         /* Redirect standard files to /dev/null */
200         if (!freopen("/dev/null", "r", stdin))
201                 fprintf(stderr, "unable to freopen() stdin, code %d (%s)",
202                                                        errno, strerror(errno));
203
204         if (!freopen("/dev/null", "w", stdout))
205                 fprintf(stderr, "unable to freopen() stdout, code %d (%s)",
206                                                        errno, strerror(errno));
207
208         if (!freopen("/dev/null", "w", stderr))
209                 fprintf(stderr, "unable to freopen() stderr, code %d (%s)",
210                                                        errno, strerror(errno));
211
212         /* Tell the parent process that we are A-okay */
213         kill(parent, SIGUSR1);
214
215         act.sa_handler = lws_daemon_closing;
216         sigemptyset(&act.sa_mask);
217         act.sa_flags = 0;
218
219         sigaction(SIGTERM, &act, NULL);
220
221         /* return to continue what is now "the daemon" */
222
223         return 0;
224 }
225