Sun, 19 Feb 2017 11:56:39 +0100
adds new tool for webserver control
--- a/src/Makefile Sun Jan 29 09:40:04 2017 +0100 +++ b/src/Makefile Sun Feb 19 11:56:39 2017 +0100 @@ -28,11 +28,17 @@ BUILD_ROOT = ../ -all: _ucx _server +all: ucx server tools -_ucx: +ucx: .FORCE cd ucx; $(MAKE) -_server: +server: ucx .FORCE cd server; $(MAKE) + +tools: .FORCE + cd tools; $(MAKE) + +.FORCE: + \ No newline at end of file
--- a/src/server/daemon/srvctrl.c Sun Jan 29 09:40:04 2017 +0100 +++ b/src/server/daemon/srvctrl.c Sun Feb 19 11:56:39 2017 +0100 @@ -199,6 +199,12 @@ } void srvctrl_log(SrvCtrlClient *client, char *msg, size_t len) { + char msgheader[4]; + msgheader[0] = SRV_MSG_LOG; + uint16_t msglen = len; + memcpy(&msgheader[1], &msglen, 2); + write(client->fd, msgheader, 3); + size_t pos = 0; ssize_t w = 0; while(pos < len) {
--- a/src/server/daemon/srvctrl.h Sun Jan 29 09:40:04 2017 +0100 +++ b/src/server/daemon/srvctrl.h Sun Feb 19 11:56:39 2017 +0100 @@ -39,6 +39,9 @@ extern "C" { #endif +#define SRV_MSG_RESPONSE 0 +#define SRV_MSG_LOG 1 + typedef struct { int fd; } SrvCtrlClient;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/Makefile Sun Feb 19 11:56:39 2017 +0100 @@ -0,0 +1,48 @@ +# +# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. +# +# Copyright 2013 Olaf Wintermann. All rights reserved. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# + +BUILD_ROOT = ../.. + +include $(BUILD_ROOT)/config.mk + +# list of source files +WSTOOL_SRC = wstool.c +WSTOOL_SRC += srvctrlsocket.c + +WSTOOL_OBJ = $(WSTOOL_SRC:%.c=$(BUILD_ROOT)/build/tools/%$(OBJ_EXT)) + +all: $(BUILD_ROOT)/build/tools $(BUILD_ROOT)/build/bin/wstool + +$(BUILD_ROOT)/build/bin/wstool: $(BUILD_ROOT)/build/tools $(WSTOOL_OBJ) + $(CC) -o $@ $(WSTOOL_OBJ) $(LDFLAGS) + +$(BUILD_ROOT)/build/tools/%$(OBJ_EXT): %.c + $(CC) $(CFLAGS) -c -o $@ $< + +$(BUILD_ROOT)/build/tools: + mkdir -p $(BUILD_ROOT)/build/tools
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/srvctrlsocket.c Sun Feb 19 11:56:39 2017 +0100 @@ -0,0 +1,112 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2017 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "srvctrlsocket.h" + +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <string.h> +#include <sys/socket.h> +#include <sys/un.h> + +SrvConnection* srvctrl_connet(char *socketfile) { + if(!socketfile) { + fprintf(stderr, "srvctrl_connect: no socketfile\n"); + return NULL; + } + size_t len = strlen(socketfile); + if(len == 0) { + fprintf(stderr, "srvctrl_connect: invalid socket path\n"); + return NULL; + } + if(len > 100) { + fprintf(stderr, "srvctrl_connect: socket path too long\n"); + return NULL; + } + + int fd = socket(AF_UNIX, SOCK_STREAM, 0); + if(fd == -1) { + perror("srvctrl_connect: cannot create socket"); + return NULL; + } + + struct sockaddr_un addr; + memset(&addr, 0, sizeof(addr)); + addr.sun_family = AF_UNIX; + memcpy(addr.sun_path, socketfile, len); + + if(connect(fd, (struct sockaddr*)&addr, sizeof(addr))) { + perror("srvctrl_connect"); + close(fd); + return NULL; + } + + FILE *stream = fdopen(fd, "r+"); + if(!stream) { + close(fd); + return NULL; + } + + SrvConnection *conn = calloc(1, sizeof(SrvConnection)); + conn->socket = fd; + conn->stream = stream; + + return conn; +} + +void srvctrl_close(SrvConnection *conn) { + fclose(conn->stream); + free(conn); +} + +int srvctrl_readmsg(SrvConnection *conn, SrvMsg *msg) { + int type = 0; + uint16_t length; + + type = fgetc(conn->stream); + if(type == EOF) { + return 1; + } + + if(fread(&length, 1, 2, conn->stream) != 2) { + return -1; + } + + msg->type = type; + msg->length = length; + msg->message = malloc(length); + + size_t r = fread(msg->message, 1, length, conn->stream); + if(r != length) { + free(msg->message); + return -1; + } + + return 0; +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/srvctrlsocket.h Sun Feb 19 11:56:39 2017 +0100 @@ -0,0 +1,75 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2017 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef SRVCTRLSOCKET_H +#define SRVCTRLSOCKET_H + +#include <stdio.h> +#include <stdlib.h> +#include <inttypes.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct SrvConnection { + int socket; + FILE *stream; +} SrvConnection; + +typedef struct SrvMsg { + /* + * message type + * 0: cmd response + * 1: log message + */ + int type; + + /* + * message data + */ + char *message; + + /* + * message length + */ + size_t length; +} SrvMsg; + +SrvConnection* srvctrl_connet(char *socketfile); + +void srvctrl_close(SrvConnection *conn); + +int srvctrl_readmsg(SrvConnection *conn, SrvMsg *msg); + +#ifdef __cplusplus +} +#endif + +#endif /* SRVCTRLSOCKET_H */ +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/wstool.c Sun Feb 19 11:56:39 2017 +0100 @@ -0,0 +1,70 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2017 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#include "wstool.h" + +#include <stdio.h> +#include <stdlib.h> + +#include "srvctrlsocket.h" + +static void print_info(char *cmd) { + fprintf(stderr, "usage: %s -s <socketpath> <command>\n\n"); + fprintf(stderr, "Commands: reconfig, shutdown, stat, log\n"); +} + +int main(int argc, char **argv) { + if(argc < 3) { + print_info(argv[0]); + return -2; + } + + SrvConnection *srv = srvctrl_connet(argv[1]); + if(!srv) { + return -1; + } + + fprintf(srv->stream, "%s\n", argv[2]); + fflush(srv->stream); + + SrvMsg msg; + while(!srvctrl_readmsg(srv, &msg)) { + if(msg.type == 0) { + fprintf(stdout, "%.*s", (int)msg.length, msg.message); + fflush(stdout); + } else if(msg.type == 1) { + fprintf(stderr, "%.*s", (int)msg.length, msg.message); + fflush(stderr); + } + } + + srvctrl_close(srv); + + return 0; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/tools/wstool.h Sun Feb 19 11:56:39 2017 +0100 @@ -0,0 +1,44 @@ +/* + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. + * + * Copyright 2017 Olaf Wintermann. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef WSTOOL_H +#define WSTOOL_H + +#ifdef __cplusplus +extern "C" { +#endif + + + + +#ifdef __cplusplus +} +#endif + +#endif /* WSTOOL_H */ +