src/tools/wstool.c

Mon, 12 Aug 2024 21:20:17 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 12 Aug 2024 21:20:17 +0200
changeset 544
27684460629f
parent 418
b7dcc9c4f270
permissions
-rw-r--r--

fix memory leak in case handle_request fails (some illegal requests)

173
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2017 Olaf Wintermann. All rights reserved.
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
63b8d52db390 adds new tool for webserver control
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
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "wstool.h"
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdio.h>
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <stdlib.h>
175
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
33 #include <string.h>
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
34
258
134279e804b6 remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
35 #include "../server/config/serverconfig.h"
173
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include "srvctrlsocket.h"
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 static void print_info(char *cmd) {
175
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
40 fprintf(stderr, "usage:\n");
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
41 fprintf(stderr, "%s -t <srvconfigfile>\n", cmd);
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
42 fprintf(stderr, "%s -s <socketpath> <command>\n", cmd);
173
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 fprintf(stderr, "Commands: reconfig, shutdown, stat, log\n");
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 }
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 int main(int argc, char **argv) {
175
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
47 if(argc > 2) {
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
48 if(!strcmp(argv[1], "-t")) {
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
49 return tool_get_tmpdir(argv[2]);
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
50 } else if(!strcmp(argv[1], "-s")) {
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
51 if(argc != 4) {
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
52 print_info(argv[0]);
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
53 return -2;
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
54 }
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
55 return tool_srvctrl(argv[2], argv[3]);
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
56 }
173
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 }
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58
175
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
59 print_info(argv[0]);
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
60 return -2;
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
61 }
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
62
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
63 int tool_get_tmpdir(char *configfile) {
258
134279e804b6 remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
64 ServerConfig *serverconf = serverconfig_load(configfile);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
65 CxList *list = serverconfig_get_node_list(serverconf->root, CONFIG_NODE_OBJECT, cx_str("Runtime"));
175
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
66 if(!list) {
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
67 fprintf(stderr, "Error: No Runtime element in %s\n", configfile);
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
68 return -1;
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
69 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
70 if(list->size != 1) {
175
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
71 fprintf(stderr, "Error: Multiple Runtime elements in %s\n", configfile);
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
72 return -1;
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
73 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
74 ConfigNode *runtime = cxListAt(list, 0);
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
75 cxstring tmp = serverconfig_object_directive_value(runtime, cx_str("Temp"));
258
134279e804b6 remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
76
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
77 cxListDestroy(list);
258
134279e804b6 remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 254
diff changeset
78
175
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
79 if(!tmp.ptr) {
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
80 fprintf(stderr, "Error: No Temp directive in Runtime Object\n");
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
81 return -1;
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
82 }
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
83
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
84 printf("%.*s\n", (int)tmp.length, tmp.ptr);
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
85
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
86 return 0;
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
87 }
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
88
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
89 int tool_srvctrl(char *socketfile, char *cmd) {
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
90 SrvConnection *srv = srvctrl_connet(socketfile);
173
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 if(!srv) {
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 return -1;
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
175
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
95 fprintf(srv->stream, "%s\n", cmd);
173
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 fflush(srv->stream);
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 SrvMsg msg;
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 while(!srvctrl_readmsg(srv, &msg)) {
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 if(msg.type == 0) {
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 fprintf(stdout, "%.*s", (int)msg.length, msg.message);
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 fflush(stdout);
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 } else if(msg.type == 1) {
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 fprintf(stderr, "%.*s", (int)msg.length, msg.message);
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 fflush(stderr);
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 }
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 }
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 srvctrl_close(srv);
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 return 0;
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 }
63b8d52db390 adds new tool for webserver control
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113
175
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
114
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
115
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
116 int log_ereport(int degree, const char *format, ...) {
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
117 va_list args;
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
118 va_start(args, format);
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
119 int ret = log_ereport_v(degree, format, args);
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
120 va_end(args);
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
121 return ret;
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
122 }
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
123
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
124 int log_ereport_v(int degree, const char *format, va_list args) {
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
125 return 0;
9823770ba4ee using wstool in server control scripts
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 173
diff changeset
126 }

mercurial