src/server/daemon/srvctrl.c

Mon, 10 Jul 2023 18:52:28 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 10 Jul 2023 18:52:28 +0200
changeset 506
696246f5758f
parent 439
255e316db762
permissions
-rw-r--r--

add WS_CFLAGS to admin makefile

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
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
40 #include <cx/utils.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
41 #include <cx/buffer.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
42 #include <cx/printf.h>
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #include "srvctrl.h"
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
439
255e316db762 fix srvctrl thread stacksize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
46 #define SRVCTRL_THREAD_STACKSIZE 262144
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 static int srvctrl;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
49 static cxmutstr srvctrl_path;
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
50
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 static WSBool srv_shutdown;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 int srvctrl_init(ServerConfiguration *cfg) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 // create srvctrl unix domain socket
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 // this socket is used for stop, reconfigure and other operations
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
56 srvctrl_path = cx_asprintf("%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
57 struct sockaddr_un addr;
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
58 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
59 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 "path '%s' too long for unix domain socket",
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
62 srvctrl_path.ptr);
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 return -1;
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
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 // 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
67 // otherweise bind would fail
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
68 if(unlink(srvctrl_path.ptr)) {
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 if(errno != ENOENT) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 "cannot unlink old srvctrl socket '%s': %s",
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
73 srvctrl_path.ptr,
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 strerror(errno));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 return -1;
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
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 ZERO(&addr, sizeof(addr));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 addr.sun_family = AF_UNIX;
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
81 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
82
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 srvctrl = socket(AF_UNIX, SOCK_STREAM, 0);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 if(srvctrl == -1) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 "Cannot create server control socket: %s",
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 strerror(errno));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 return -1;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 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
92 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 "srvctrl socket bind failed: %s",
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 strerror(errno));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 return -1;
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
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 return 0;
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
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 int srvctrl_wait() {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 listen(srvctrl, 8);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 for(;;) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 int fd = accept(srvctrl, NULL, 0);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 if(fd < 0) {
177
f144b60c71b1 fixes srvctrl socket shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 176
diff changeset
108 if(srv_shutdown) break;
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 LOG_FAILURE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 "srvctrl: accept failed: %s",
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 strerror(errno));
177
f144b60c71b1 fixes srvctrl socket shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 176
diff changeset
113 break;
158
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
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 SrvCtrlClient *client = srvctrl_create_client(fd);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 SYS_THREAD t = systhread_start(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 SYSTHREAD_DEFAULT_PRIORITY,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 SRVCTRL_THREAD_STACKSIZE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 (thrstartfunc)srvctrl_thread,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 client);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 systhread_detach(t);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 }
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
125
177
f144b60c71b1 fixes srvctrl socket shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 176
diff changeset
126 close(srvctrl);
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
127 unlink(srvctrl_path.ptr);
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
128 free(srvctrl_path.ptr);
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
129
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
130 return 0;
158
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
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 void srvctrl_shutdown() {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 srv_shutdown = TRUE;
177
f144b60c71b1 fixes srvctrl socket shutdown
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 176
diff changeset
135 shutdown(srvctrl, SHUT_RDWR);
158
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
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 SrvCtrlClient* srvctrl_create_client(int fd) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 SrvCtrlClient *client = malloc(sizeof(SrvCtrlClient));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 ZERO(client, sizeof(SrvCtrlClient));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 client->fd = fd;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 return client;
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
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 void* srvctrl_thread(SrvCtrlClient *client) {
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
146 LogDup log;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
147 log.write = (log_writefunc)srvctrl_log;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
148 log.cookie = client;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
149 log_add_logdup(&log);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
150
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 char buf[64];
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
152 CxBuffer line;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
153 cxBufferInit(&line, NULL, 32, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 ssize_t r;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 WSBool br = FALSE;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 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
158 for(int i=0;i<r;i++) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 char c = buf[i];
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 if(c == '\n') {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
161 cxmutstr ln = cx_mutstrn(line.space, line.pos);
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 if(srvctrl_handle_cmd(client, ln)) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 br = TRUE;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 break;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 } else {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
167 cxBufferPut(&line, c);
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 if(br) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 break;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
174 log_remove_logdup(&log);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
175
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
176 cxBufferDestroy(&line);
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 close(client->fd);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 free(client);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 return NULL;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
182 int srvctrl_handle_cmd(SrvCtrlClient *client, cxmutstr cmd) {
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
183 if(!cx_strcmp(cx_strcast(cmd), cx_str("reconfig"))) {
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
184 log_ereport(LOG_INFORM, "reconfigure server");
439
255e316db762 fix srvctrl thread stacksize
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
185 if(!webserver_reconfig()) {
158
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 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
188 } else if(!cx_strcmp(cx_strcast(cmd), cx_str("shutdown"))) {
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 webserver_shutdown();
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
190 } else if(!cx_strcmp(cx_strcast(cmd), cx_str("stat"))) {
178
4760f7f1b197 closes branch srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 177
diff changeset
191 // TODO: implement
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
192 } else if(!cx_strcmp(cx_strcast(cmd), cx_str("log"))) {
178
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