]> Nishi Git Mirror - mokou.git/commitdiff
works
authorNishi <nishi@nishi.boats>
Sat, 7 Sep 2024 10:07:49 +0000 (10:07 +0000)
committerNishi <nishi@nishi.boats>
Sat, 7 Sep 2024 10:07:49 +0000 (10:07 +0000)
git-svn-id: file:///raid/svn-personal/mokou/trunk@11 35d6bad2-6c5c-c749-ada2-a2c82cb3bd79

Control/main.c
Mokou/version.c

index c1885e1945f01005714be736f2ca6c28cd7339f9..6acc5fe11f7e4d1b67ee6c1f34b536f195a96621 100644 (file)
@@ -1,4 +1,92 @@
 /* $Id$ */
 
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <errno.h>
+
 int main(int argc, char** argv){
+       if(argc < 2){
+               fprintf(stderr, "Usage: %s action [target]\n", argv[0]);
+               fprintf(stderr, "Actions:\n");
+               fprintf(stderr, "\treload   Reloads the service direcetory\n");
+               fprintf(stderr, "\tstart    Start the service\n");
+               fprintf(stderr, "\tstop     Stop the service\n");
+               return 1;
+       }
+       char* msg = NULL;
+       if(strcmp(argv[1], "reload") == 0){
+               msg = malloc(3);
+               msg[0] = 'R';
+               msg[1] = '\n';
+               msg[2] = 0;
+       }else if(strcmp(argv[1], "start") == 0){
+               if(argv[2] == NULL){
+                       fprintf(stderr, "Target is needed for this action\n");
+                       return 1;
+               }
+               msg = malloc(3 + strlen(argv[2]));
+               msg[0] = 'U';
+               strcpy(msg + 1, argv[2]);
+               msg[1 + strlen(argv[2])] = '\n';
+               msg[2 + strlen(argv[2])] = 0;
+       }else if(strcmp(argv[1], "stop") == 0){
+               if(argv[2] == NULL){
+                       fprintf(stderr, "Target is needed for this action\n");
+                       return 1;
+               }
+               msg = malloc(3 + strlen(argv[2]));
+               msg[0] = 'D';
+               strcpy(msg + 1, argv[2]);
+               msg[1 + strlen(argv[2])] = '\n';
+               msg[2 + strlen(argv[2])] = 0;
+       }else{
+               fprintf(stderr, "Unknown action: %s\n", argv[1]);
+               return 1;
+       }
+       if(msg != NULL){
+               int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
+               if(sock == -1){
+                       fprintf(stderr, "Socket creation failure\n");
+                       return 1;
+               }
+               struct sockaddr_un sun;
+               sun.sun_family = AF_LOCAL;
+               strcpy(sun.sun_path, "/tmp/mokou.sock");
+               if(connect(sock, (struct sockaddr*)&sun, sizeof(sun)) == -1){
+                       fprintf(stderr, "Connection failure: %s\n", strerror(errno));
+                       close(sock);
+               }
+               send(sock, msg, strlen(msg), 0);
+               
+               char cbuf[2];
+               cbuf[1] = 0;
+
+               bool first = true;
+               bool err = false;
+               while(1){
+                       if(recv(sock, cbuf, 1, 0) <= 0) break;
+                       if(cbuf[0] == '\n'){
+                               break;
+                       }else if(cbuf[0] != '\r'){
+                               if(first && cbuf[0] == 'E'){
+                                       err = true;
+                                       fprintf(stderr, "Error: ");
+                               }else if(!first){
+                                       fprintf(err ? stderr : stdout, "%c", cbuf[0]);
+                               }
+                               first = false;
+                       }
+               }
+               fprintf(err ? stderr : stdout, "\n");
+
+               close(sock);
+               free(msg);
+       }
 }
index b2fa85f9567752248a10a7834bc2a5c211139b4f..cb1f524c8e542ac9b90209db07a470538e46cf4c 100644 (file)
@@ -2,7 +2,7 @@
 
 #include "mk_version.h"
 
-const char* MOKOU_VERSION = "0.0";
+const char* MOKOU_VERSION = "1.0";
 
 const char* mk_get_version(void){
        return MOKOU_VERSION;