src/server/daemon/main.c

Sat, 18 Mar 2023 11:44:37 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 18 Mar 2023 11:44:37 +0100
changeset 477
39ebd50cfc12
parent 444
96d2ba2f28db
child 556
b036ccad4b49
permissions
-rw-r--r--

fix nsapi_error_request() could send empty error messages with http status 200, if the request status code wasn't set

1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 27
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdio.h>
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdlib.h>
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
32 #include <unistd.h>
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
33 #include <signal.h>
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
34 #include <errno.h>
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
35 #include <pthread.h>
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
36 #include <fcntl.h>
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
37 #include <poll.h>
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
38 #include <semaphore.h>
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
14
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
40 #include "../util/pool.h"
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
41 #include "../public/nsapi.h"
b8bf95b39952 New source folder layout
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 9
diff changeset
42 #include "../util/plist.h"
27
05b7576dca2b added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
43 #include "../util/date.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 388
diff changeset
45 #include <cx/string.h>
156
724e107983e9 adds unix domain socket for server control ops
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 146
diff changeset
46
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 #include "webserver.h"
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
48 #include "log.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 #include "httprequest.h"
74
5bc6d078fb2c listeners can be dynamically added
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 68
diff changeset
50 #include "httplistener.h"
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 156
diff changeset
51 #include "srvctrl.h"
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 22
diff changeset
53 #include "configmanager.h"
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
54
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
55 #define LOG_THREAD_STACK_SIZE 32768
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
56 #define LOG_THREAD_MAX_POLL_FAILS 10
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
57 #define LOG_THREAD_READ_BUF 2048
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
58
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
59 static int std_out[2];
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
60 static int std_err[2];
115
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 86
diff changeset
61 static WSBool is_daemon;
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
62
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 void test() {
27
05b7576dca2b added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
64 time_t t = time(NULL);
05b7576dca2b added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
65 pool_handle_t *pool = pool_create();
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 388
diff changeset
66 cxmutstr date = date_format_http(t, pool);
27
05b7576dca2b added put method
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
67 printf("%s\n", date.ptr);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
115
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 86
diff changeset
70
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 86
diff changeset
71 WSBool main_is_daemon(void) {
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 86
diff changeset
72 return is_daemon;
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 86
diff changeset
73 }
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 86
diff changeset
74
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
75 /*
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
76 * SIGUSR1: reload the configuration files
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
77 */
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
78 void sig_usr1_reload(int sig) {
60
feb2f1e115c6 improved logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 48
diff changeset
79 log_ereport(LOG_INFORM, "sig reload");
444
96d2ba2f28db fix configuration referencing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 443
diff changeset
80 (void)webserver_reconfig();
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
81 signal(SIGUSR1, sig_usr1_reload);
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
82 }
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
83
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
84 /*
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
85 * SIGTERM: stop the server
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
86 */
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
87 void sig_term(int sig) {
68
f5102a892ed4 some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
88 webserver_shutdown();
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 156
diff changeset
89 //exit(EXIT_SUCCESS);
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
90 }
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
91
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
92 static void set_pipe_nonblocking(int fd) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
93 int flags = 0;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
94 flags = fcntl(fd, F_GETFL, 0);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
95 fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
96 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
97
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
98 static char log_pipe_readbuf[LOG_THREAD_READ_BUF];
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
99 static char log_pipe_stdout_buf[LOG_THREAD_READ_BUF];
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
100 static char log_pipe_stderr_buf[LOG_THREAD_READ_BUF];
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
101 static size_t log_pipe_stdout_tmp_pos = 0;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
102 static size_t log_pipe_stderr_tmp_pos = 0;
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
103
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
104 // pipe used for startup
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
105 // after fork, the parent process waits, until the daemon is fully started
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
106 // to get potential error and log messages
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
107 static int daemon_start_pipe[2];
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
108
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
109 // used for sending log messages from the daemon process to the parent process
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
110 // in the startup phase
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
111 static int parent_log_pipe[2];
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
112
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
113 static LogDup startup_log;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
114
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
115 static void finish_daemon_startup(int status) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
116 if(status != 0) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
117 log_ereport(LOG_FAILURE, "daemon startup failed");
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
118 if(status > 127) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
119 status = 127;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
120 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
121 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
122 char s = (char)status;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
123
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
124 // notify parent process about startup status
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
125 write(daemon_start_pipe[1], &s, 1);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
126 close(daemon_start_pipe[1]);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
127
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
128 // remove logdup
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
129 log_remove_logdup(&startup_log);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
130 close(parent_log_pipe[0]);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
131 close(parent_log_pipe[1]);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
132 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
133
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
134 static int log_pipe(const char *name, int fd, char *buf, size_t *pos) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
135 ssize_t r = read(fd, log_pipe_readbuf, 2);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
136 if(r <= 0) {
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
137 return r == 0 ? 0 : 1;
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
138 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
139
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
140 char *tmp = buf;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
141 int tmplen = *pos;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
142
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
143 int s = 0;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
144 for(int i=0;i<r;i++) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
145 if(log_pipe_readbuf[i] == '\n') {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
146 if(tmplen + i-s > 0) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
147 log_message(name, "%.*s%.*s", tmplen, tmp, i-s, log_pipe_readbuf + s);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
148 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
149 tmplen = 0;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
150 *pos = 0;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
151 s = i+1;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
152 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
153 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
154
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
155 int remaining = r - s;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
156 if(tmplen + remaining >= LOG_THREAD_READ_BUF) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
157 log_message(name, "%.*s%.*s", tmplen, tmp, remaining, log_pipe_readbuf + s);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
158 *pos = 0;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
159 } else if(remaining > 0) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
160 memcpy(buf + *pos, log_pipe_readbuf + s, remaining);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
161 *pos += remaining;
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
162 }
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
163
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
164 return 0;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
165 }
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
166
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
167 void* log_pipe_thread(void *data) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
168 set_pipe_nonblocking(std_out[0]);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
169 set_pipe_nonblocking(std_err[0]);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
170
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
171 struct pollfd fds[2];
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
172 fds[0].fd = std_out[0];
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
173 fds[0].events = POLLIN;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
174 fds[1].fd = std_err[0];
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
175 fds[1].events = POLLIN;
25
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
176
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
177 int poll_fails = 0;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
178 for(;;) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
179 if(poll(fds, 1, 1000000) < 0) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
180 if(errno == EINTR) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
181 continue;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
182 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
183 log_ereport(LOG_FAILURE, "log thread poll failed: %s", strerror(errno));
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
184 if(poll_fails++ > LOG_THREAD_MAX_POLL_FAILS) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
185 break;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
186 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
187 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
188
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
189 // check stdout
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
190 if(fds[0].revents & POLLIN) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
191 if(log_pipe("stdout", fds[0].fd, log_pipe_stdout_buf, &log_pipe_stdout_tmp_pos)) {
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
192 log_ereport(LOG_WARN, "log_pipe stdout failed");
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
193 break;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
194 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
195 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
196
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
197 // check stderr
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
198 if(fds[1].revents & POLLIN) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
199 if(log_pipe("stderr", fds[0].fd, log_pipe_stderr_buf, &log_pipe_stderr_tmp_pos)) {
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
200 log_ereport(LOG_WARN, "log_pipe stderr failed");
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
201 break;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
202 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
203 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
204 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
205
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
206 log_ereport(LOG_INFORM, "log thread end");
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
207
25
5dee29c7c530 Fixed config parser bug
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 23
diff changeset
208 return NULL;
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
209 }
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
210
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
211
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
212 // read (blocking) 1 byte from daemon_start_pipe and return the value
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
213 // this is used to wait in the parent process, until the daemon process
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
214 // is started
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
215 // the returning byte is used as the process return value
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
216 static int main_daemon_startup_wait(void) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
217 // receive log messages from daemon
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
218 char buf[2048];
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
219 ssize_t r;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
220 while((r = read(parent_log_pipe[0], buf, 2048)) > 0) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
221 ssize_t pos = 0;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
222 while(r > 0) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
223 ssize_t w = write(STDOUT_FILENO, buf+pos, r-pos);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
224 pos += w;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
225 r -= w;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
226 break;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
227 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
228 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
229 // log pipe closed, daemon_start_pipe should contain the status code now
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
230
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
231 char ret;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
232 if(read(daemon_start_pipe[0], &ret, 1) != 1) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
233 return 255;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
234 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
235 return ret;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
236 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
237
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
238 static void startup_log_write(void *cookie, char *msg, size_t length) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
239 write(parent_log_pipe[1], msg, length);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
240 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
241
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 int main(int argc, char **argv) {
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
243 //test();
68
f5102a892ed4 some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
244
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
245 // if the -c parameter is specified, we don't create a daemon
115
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 86
diff changeset
246 is_daemon = 1;
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
247 for(int i=0;i<argc;i++) {
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
248 char *p = argv[i];
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
249 if(p[0] == '-' && p[1] == 'c') {
115
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 86
diff changeset
250 is_daemon = 0;
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
251 break;
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
252 }
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
253 }
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
254
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
255 if(init_logging()) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
256 fprintf(stderr, "OOM\n");
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
257 return 1;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
258 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
259
115
51d9a15eac98 improves logging
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 86
diff changeset
260 if(is_daemon) {
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
261 // initialize startup pipes
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
262 if(pipe(daemon_start_pipe)) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
263 perror("pipe");
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
264 return EXIT_FAILURE;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
265 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
266 if(pipe(parent_log_pipe)) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
267 perror("pipe");
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
268 return EXIT_FAILURE;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
269 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
270
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
271 log_ereport(LOG_INFORM, "start daemon");
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
272
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
273 // register parent process as LogDup to receive startup log messages
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
274 // in the parent process, because stdout/stderr will be closed
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
275 // after fork
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
276 startup_log.write = startup_log_write;
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
277 log_add_logdup(&startup_log);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
278
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
279 // create daemon
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
280 pid_t pid = fork();
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
281 if(pid < 0) {
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
282 perror("fork");
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
283 return EXIT_FAILURE;
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
284 } else if(pid > 0) {
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
285 close(daemon_start_pipe[1]);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
286 close(parent_log_pipe[1]);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
287 return main_daemon_startup_wait();
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
288 }
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
289
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
290 if(setsid() < 0) {
47
ce9790523346 server can change uid
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
291 fprintf(stderr, "setsid failed\n");
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
292 return EXIT_FAILURE;
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
293 }
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
294
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
295 // close read-end of pipe in the daemon process
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
296 close(daemon_start_pipe[0]);
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
297
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
298 // stdio redirection
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
299 // create pipes
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
300 if(pipe(std_out) != 0) {
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
301 perror("pipe");
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
302 return EXIT_FAILURE;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
303 }
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
304 if(pipe(std_err) != 0) {
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
305 perror("pipe");
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
306 return EXIT_FAILURE;
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
307 }
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
308
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
309 for(int i=0;i<3;i++) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
310 close(i);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
311 }
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
312
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
313 dup2(std_out[1], 1);
444
96d2ba2f28db fix configuration referencing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 443
diff changeset
314 dup2(std_err[1], 2);
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
315 close(std_out[1]);
444
96d2ba2f28db fix configuration referencing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 443
diff changeset
316 close(std_err[1]);
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
317
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
318 // set log thread stack size
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
319 pthread_attr_t tattr;
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
320 pthread_attr_init(&tattr);
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
321 pthread_attr_setstacksize(&tattr, LOG_THREAD_STACK_SIZE);
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
322
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
323 pthread_t tid;
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
324 pthread_create(&tid, &tattr, log_pipe_thread, NULL);
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
325 }
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
326
9
30e51941a673 Added mod_jk dependencies
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 4
diff changeset
327 pool_init(NULL, NULL, NULL);
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
328
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
329 // add signal handler
22
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
330 signal(SIGUSR1, sig_usr1_reload);
adb0bda54e6b Server can run as daemon
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 14
diff changeset
331 signal(SIGTERM, sig_term);
68
f5102a892ed4 some fixes for mod_jk
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
332 signal(SIGINT, sig_term);
125
c913d515be1e adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
333
c913d515be1e adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
334 struct sigaction act;
146
a9591a91c004 fixes server user init
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 142
diff changeset
335 ZERO(&act, sizeof(struct sigaction));
125
c913d515be1e adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
336 act.sa_handler = SIG_IGN;
c913d515be1e adds more error handling
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
337 sigaction(SIGPIPE, &act, NULL);
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338
434
ff576305ae6e redirect stdout/stderr to log file
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
339 // start webserver
130
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 125
diff changeset
340 log_ereport(LOG_INFORM, "startup");
198ad9d8cec1 adds ssl protocol configuration
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 125
diff changeset
341
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 int status;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 status = webserver_init();
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 if(status != 0) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 388
diff changeset
345 log_ereport(LOG_FAILURE, "cannot initialize server.");
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 return EXIT_FAILURE;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 status = webserver_run();
443
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
350 if(is_daemon) {
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
351 finish_daemon_startup(status);
ef3c8a0e1fee improve daemon startup
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 434
diff changeset
352 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 if(status != 0) {
156
724e107983e9 adds unix domain socket for server control ops
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 146
diff changeset
354 log_ereport(LOG_FAILURE, "cannot run server.");
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 return EXIT_FAILURE;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 }
156
724e107983e9 adds unix domain socket for server control ops
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 146
diff changeset
357
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 156
diff changeset
358 if(srvctrl_wait()) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 156
diff changeset
359 return EXIT_FAILURE;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 156
diff changeset
360 }
1
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 return EXIT_SUCCESS;
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 }
3c066d52342d added source
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364

mercurial