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

Mokou/main.c
Mokou/mk_server.h
Mokou/server.c

index 432a5a6c279dcfa8e2b9551589d1d1643da55563..ff6aafd9da95b08938e5cd12c6dff968e5e04921 100644 (file)
@@ -7,6 +7,7 @@
 #include <stdbool.h>
 
 #include "mk_service.h"
+#include "mk_server.h"
 #include "mk_log.h"
 #include "mk_util.h"
 #include "mk_version.h"
@@ -15,10 +16,13 @@ extern bool mk_syslog;
 
 int main(int argc, char** argv){
        int i;
+       bool daemon = true;
        for(i = 1; i < argc; i++){
                if(argv[i][0] == '-'){
                        if(strcmp(argv[i], "--stderr") == 0 || strcmp(argv[i], "-S") == 0){
                                mk_syslog = false;
+                       }else if(strcmp(argv[i], "-D") == 0){
+                               daemon = false;
                        }else{
                                fprintf(stderr, "%s: %s: unknown flag\n", argv[0], argv[i]);
                                return 1;
@@ -35,4 +39,25 @@ int main(int argc, char** argv){
        mk_service_scan();
        mk_start_services();
        mk_log("Mokou is up, creating the server socket");
+       if(mk_server_init() != 0){
+               mk_log("Could not initialize the server");
+               return 1;
+       }
+       unsigned long long pid = 0;
+       if(daemon){
+               mk_log("Spawning daemon");
+               pid = fork();
+       }
+       if(pid == 0){
+               if(daemon){
+                       mk_log("Hello from daemon");
+               }
+               mk_log("Entering server loop");
+               mk_server_loop();
+               return 0;
+       }else if(daemon){
+               FILE* f = fopen("/var/run/mokou.pid", "w");
+               fprintf(f, "%llu", pid);
+               fclose(f);
+       }
 }
index 1a58dfa43a956c3c0bf7ab46de0489c76fe11520..a01b5ba6fa095b48d0e56eee2881607681a4c735 100644 (file)
@@ -3,5 +3,7 @@
 #ifndef __MK_SERVER_H__
 #define __MK_SERVER_H__
 
+int mk_server_init(void);
+void mk_server_loop(void);
 
 #endif
index c1918f6270a84c17a4b56f703125722e4c5145db..eac544dd6fdccf261cccc037342fda63855911c2 100644 (file)
@@ -1,3 +1,54 @@
 /* $Id$ */
 
 #include "mk_server.h"
+
+#include "mk_version.h"
+#include "mk_util.h"
+#include "mk_log.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+
+struct sockaddr_un sun;
+int server;
+
+int mk_server_init(void){
+       remove("/tmp/mokou.sock");
+       memset(&sun, 0, sizeof(sun));
+       server = socket(AF_LOCAL, SOCK_STREAM, 0);
+       if(server == -1){
+               mk_log("Socket creation failure");
+               return 1;
+       }
+       sun.sun_family = AF_LOCAL;
+       strcpy(sun.sun_path, "/tmp/mokou.sock");
+       if(bind(server, (struct sockaddr*)&sun, sizeof(sun)) == -1){
+               mk_log("Bind failure");
+               close(server);
+               return 1;
+       }
+       if(listen(server, 16) == -1){
+               mk_log("Listen failure");
+               close(server);
+               return 1;
+       }
+       return 0;
+}
+
+void mk_server_loop(void){
+       struct sockaddr_un cun;
+       socklen_t socklen = sizeof(cun);
+       char* ver = mk_strcat3("V", mk_get_version(), "\n");
+       while(1){
+               mk_log("Waiting for the connection");
+               int cli = accept(server, (struct sockaddr*)&cun, &socklen);
+               send(cli, ver, strlen(ver), 0);
+               close(cli);
+       }
+       free(ver);
+}