src/server/daemon/srvctrl.c

Mon, 06 Mar 2017 15:43:28 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 06 Mar 2017 15:43:28 +0100
branch
srvctrl
changeset 175
9823770ba4ee
parent 173
63b8d52db390
child 176
f2268fcbe487
permissions
-rw-r--r--

using wstool in server control scripts

adds server config lib
wstool can read server.conf now
changes wstool usage

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>
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
32 #include "log.h"
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include "webserver.h"
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include "../util/systhr.h"
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include "../../ucx/utils.h"
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include "../../ucx/buffer.h"
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include "srvctrl.h"
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #define SRVCTRL_THREAD_STACKSIZE 8192
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 static int srvctrl;
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
45 static sstr_t srvctrl_path;
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
46
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 static WSBool srv_shutdown;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 int srvctrl_init(ServerConfiguration *cfg) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 // create srvctrl unix domain socket
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 // 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
52 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
53 struct sockaddr_un addr;
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
54 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
55 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 "path '%s' too long for unix domain socket",
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
58 srvctrl_path.ptr);
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 return -1;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 // 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
63 // otherweise bind would fail
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
64 if(unlink(srvctrl_path.ptr)) {
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 if(errno != ENOENT) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 "cannot unlink old srvctrl socket '%s': %s",
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
69 srvctrl_path.ptr,
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 strerror(errno));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 return -1;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 ZERO(&addr, sizeof(addr));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 addr.sun_family = AF_UNIX;
166
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
77 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
78
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 srvctrl = socket(AF_UNIX, SOCK_STREAM, 0);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 if(srvctrl == -1) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 "Cannot create server control socket: %s",
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 strerror(errno));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 return -1;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 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
88 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 LOG_CATASTROPHE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 "srvctrl socket bind failed: %s",
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 strerror(errno));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 return -1;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 return 0;
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 int srvctrl_wait() {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 listen(srvctrl, 8);
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 for(;;) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 int fd = accept(srvctrl, NULL, 0);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 if(fd < 0) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 if(srv_shutdown) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 break;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107
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));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 continue;
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
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
125 unlink(srvctrl_path.ptr);
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
126 free(srvctrl_path.ptr);
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
127
c07122f66676 adds debug log level
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 158
diff changeset
128 return 0;
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 }
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 void srvctrl_shutdown() {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 srv_shutdown = TRUE;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 close(srvctrl);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 }
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 SrvCtrlClient* srvctrl_create_client(int fd) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 SrvCtrlClient *client = malloc(sizeof(SrvCtrlClient));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 ZERO(client, sizeof(SrvCtrlClient));
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 client->fd = fd;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 return client;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 }
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 void* srvctrl_thread(SrvCtrlClient *client) {
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
144 LogDup log;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
145 log.write = (log_writefunc)srvctrl_log;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
146 log.cookie = client;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
147 log_add_logdup(&log);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
148
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 char buf[64];
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 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
151
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 ssize_t r;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 WSBool br = FALSE;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 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
155 for(int i=0;i<r;i++) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 char c = buf[i];
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 if(c == '\n') {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 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
159 if(srvctrl_handle_cmd(client, ln)) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 br = TRUE;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 break;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 } else {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 ucx_buffer_putc(line, c);
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 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 if(br) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 break;
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 }
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
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 return NULL;
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
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 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
182 if(!sstrcmp(cmd, S("reconfig"))) {
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
183 log_ereport(LOG_INFORM, "reconfigure server");
158
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 if(webserver_reconfig()) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 log_ereport(LOG_FAILURE, "cannot reload config");
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 } else {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 log_ereport(LOG_INFORM, "reconfig: success");
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 } else if(!sstrcmp(cmd, S("shutdown"))) {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 webserver_shutdown();
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 } else {
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 log_ereport(
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 LOG_FAILURE,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 "unknown srvctrl command: %.*s",
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 (int)cmd.length,
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 cmd.ptr);
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 }
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 return 1;
77f4f0079428 adds reconfig and shutdown handler to srvctrl
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 }
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
200
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
201 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
202 char msgheader[4];
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
203 msgheader[0] = SRV_MSG_LOG;
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
204 uint16_t msglen = len;
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
205 memcpy(&msgheader[1], &msglen, 2);
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
206 write(client->fd, msgheader, 3);
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 167
diff changeset
207
167
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
208 size_t pos = 0;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
209 ssize_t w = 0;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
210 while(pos < len) {
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
211 w = write(client->fd, msg + pos, len - pos);
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
212 if(w <= 0) {
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
213 break;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
214 }
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
215 pos += w;
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
216 }
4be7dd2b75b9 srvctrl gets log messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 166
diff changeset
217 }

mercurial