UNIXworkcode

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