src/server/daemon/srvctrl.c

Sun, 08 May 2022 11:52:52 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 08 May 2022 11:52:52 +0200
branch
webdav
changeset 342
ebd1e67c3d5f
parent 254
4784c14aa639
child 415
d938228c382e
permissions
-rw-r--r--

fix invalid memory access in event_bsd.c

158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
77f4f0079428 adds reconfig and shutdown handler to srvctrl
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
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include <stdio.h>
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30 #include <stdlib.h>
176
f2268fcbe487 fixes linux build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
31 #include <unistd.h>
f2268fcbe487 fixes linux build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
32 #include <sys/socket.h>
f2268fcbe487 fixes linux build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
33 #include <sys/un.h>
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
35 #include "log.h"
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include "webserver.h"
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include "../util/systhr.h"
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
254
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 178
diff changeset
40 #include <ucx/utils.h>
4784c14aa639 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 178
diff changeset
41 #include <ucx/buffer.h>
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include "srvctrl.h"
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #define SRVCTRL_THREAD_STACKSIZE 8192
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 static int srvctrl;
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
48 static sstr_t srvctrl_path;
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
49
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 static WSBool srv_shutdown;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 int srvctrl_init(ServerConfiguration *cfg) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 // create srvctrl unix domain socket
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 // this socket is used for stop, reconfigure and other operations
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
55 srvctrl_path = ucx_sprintf("%s/private/srvctrl.sock", cfg->tmp.ptr);
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 struct sockaddr_un addr;
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
57 if(srvctrl_path.length > sizeof(addr.sun_path)-1) {
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 "path '%s' too long for unix domain socket",
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
61 srvctrl_path.ptr);
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 return -1;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 // make sure there is no old srvctrl socket file
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 // otherweise bind would fail
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
67 if(unlink(srvctrl_path.ptr)) {
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 if(errno != ENOENT) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 "cannot unlink old srvctrl socket '%s': %s",
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
72 srvctrl_path.ptr,
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 strerror(errno));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 return -1;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 ZERO(&addr, sizeof(addr));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 addr.sun_family = AF_UNIX;
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
80 memcpy(addr.sun_path, srvctrl_path.ptr, srvctrl_path.length);
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 srvctrl = socket(AF_UNIX, SOCK_STREAM, 0);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 if(srvctrl == -1) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 "Cannot create server control socket: %s",
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 strerror(errno));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 return -1;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 if(bind(srvctrl, (struct sockaddr*)&addr, sizeof(addr))) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 "srvctrl socket bind failed: %s",
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 strerror(errno));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 return -1;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 return 0;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 int srvctrl_wait() {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 listen(srvctrl, 8);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 for(;;) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 int fd = accept(srvctrl, NULL, 0);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 if(fd < 0) {
177
f144b60c71b1 fixes srvctrl socket shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 176
diff changeset
107 if(srv_shutdown) break;
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 LOG_FAILURE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 "srvctrl: accept failed: %s",
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 strerror(errno));
177
f144b60c71b1 fixes srvctrl socket shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 176
diff changeset
112 break;
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 SrvCtrlClient *client = srvctrl_create_client(fd);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 SYS_THREAD t = systhread_start(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 SYSTHREAD_DEFAULT_PRIORITY,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 SRVCTRL_THREAD_STACKSIZE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 (thrstartfunc)srvctrl_thread,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 client);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 systhread_detach(t);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 }
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
124
177
f144b60c71b1 fixes srvctrl socket shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 176
diff changeset
125 close(srvctrl);
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
126 unlink(srvctrl_path.ptr);
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
127 free(srvctrl_path.ptr);
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
128
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
129 return 0;
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 void srvctrl_shutdown() {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 srv_shutdown = TRUE;
177
f144b60c71b1 fixes srvctrl socket shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 176
diff changeset
134 shutdown(srvctrl, SHUT_RDWR);
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 SrvCtrlClient* srvctrl_create_client(int fd) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 SrvCtrlClient *client = malloc(sizeof(SrvCtrlClient));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 ZERO(client, sizeof(SrvCtrlClient));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 client->fd = fd;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 return client;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 void* srvctrl_thread(SrvCtrlClient *client) {
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
145 LogDup log;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
146 log.write = (log_writefunc)srvctrl_log;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
147 log.cookie = client;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
148 log_add_logdup(&log);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
149
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 char buf[64];
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 UcxBuffer *line = ucx_buffer_new(NULL, 32, UCX_BUFFER_AUTOEXTEND);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 ssize_t r;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 WSBool br = FALSE;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 while((r = read(client->fd, buf, 64)) > 0) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 for(int i=0;i<r;i++) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 char c = buf[i];
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 if(c == '\n') {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 sstr_t ln = sstrn(line->space, line->pos);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 if(srvctrl_handle_cmd(client, ln)) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 br = TRUE;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 break;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 } else {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 ucx_buffer_putc(line, c);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 if(br) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 break;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 }
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
172 log_remove_logdup(&log);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
173
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 ucx_buffer_free(line);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 close(client->fd);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 free(client);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 return NULL;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 int srvctrl_handle_cmd(SrvCtrlClient *client, sstr_t cmd) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 if(!sstrcmp(cmd, S("reconfig"))) {
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
182 log_ereport(LOG_INFORM, "reconfigure server");
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 if(webserver_reconfig()) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 log_ereport(LOG_FAILURE, "cannot reload config");
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 } else {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 log_ereport(LOG_INFORM, "reconfig: success");
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 } else if(!sstrcmp(cmd, S("shutdown"))) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 webserver_shutdown();
178
4760f7f1b197 closes branch srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
190 } else if(!sstrcmp(cmd, S("stat"))) {
4760f7f1b197 closes branch srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
191 // TODO: implement
4760f7f1b197 closes branch srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
192 } else if(!sstrcmp(cmd, S("log"))) {
4760f7f1b197 closes branch srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
193 return 0;
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 } else {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 LOG_FAILURE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 "unknown srvctrl command: %.*s",
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 (int)cmd.length,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 cmd.ptr);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 return 1;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 }
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
203
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
204 void srvctrl_log(SrvCtrlClient *client, char *msg, size_t len) {
173
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
205 char msgheader[4];
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
206 msgheader[0] = SRV_MSG_LOG;
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
207 uint16_t msglen = len;
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
208 memcpy(&msgheader[1], &msglen, 2);
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
209 write(client->fd, msgheader, 3);
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
210
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
211 size_t pos = 0;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
212 ssize_t w = 0;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
213 while(pos < len) {
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
214 w = write(client->fd, msg + pos, len - pos);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
215 if(w <= 0) {
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
216 break;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
217 }
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
218 pos += w;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
219 }
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
220 }

mercurial