Subversion Repositories Mokou

Rev

Rev 9 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
5 nishi 1
/* $Id: server.c 10 2024-09-07 09:55:12Z nishi $ */
2
 
3
#include "mk_server.h"
7 nishi 4
 
8 nishi 5
#include "mk_service.h"
7 nishi 6
#include "mk_version.h"
7
#include "mk_util.h"
8
#include "mk_log.h"
9
 
10 nishi 10
#include <string.h>
7 nishi 11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <unistd.h>
9 nishi 14
#include <poll.h>
10 nishi 15
#include <time.h>
7 nishi 16
 
17
#include <sys/types.h>
18
#include <sys/socket.h>
19
#include <sys/un.h>
20
 
21
struct sockaddr_un sun;
22
int server;
8 nishi 23
extern const char* mk_errors[];
7 nishi 24
 
25
int mk_server_init(void){
26
	remove("/tmp/mokou.sock");
27
	memset(&sun, 0, sizeof(sun));
28
	server = socket(AF_LOCAL, SOCK_STREAM, 0);
29
	if(server == -1){
30
		mk_log("Socket creation failure");
31
		return 1;
32
	}
33
	sun.sun_family = AF_LOCAL;
34
	strcpy(sun.sun_path, "/tmp/mokou.sock");
35
	if(bind(server, (struct sockaddr*)&sun, sizeof(sun)) == -1){
36
		mk_log("Bind failure");
37
		close(server);
38
		return 1;
39
	}
40
	if(listen(server, 16) == -1){
41
		mk_log("Listen failure");
42
		close(server);
43
		return 1;
44
	}
45
	return 0;
46
}
47
 
8 nishi 48
#define PROTOCOL_ERROR "EProtocol Error\n"
49
 
7 nishi 50
void mk_server_loop(void){
51
	struct sockaddr_un cun;
52
	socklen_t socklen = sizeof(cun);
8 nishi 53
	char cbuf[2];
54
	cbuf[1] = 0;
55
	char* str = malloc(1);
56
	str[0] = 0;
9 nishi 57
	struct pollfd pollfds[16 + 1];
58
	pollfds[0].fd = server;
59
	pollfds[0].events = POLLIN | POLLPRI;
10 nishi 60
	time_t trigger = time(NULL) + 5;
7 nishi 61
	while(1){
9 nishi 62
		int r = poll(pollfds, 16 + 1, 5000);
10 nishi 63
		if(!(r > 0 && pollfds[0].revents & POLLIN)){
64
			time_t t = time(NULL);
65
			if(t >= trigger){
66
				mk_resurrect_services();
67
				trigger = time(NULL) + 5;
68
			}
69
			usleep(1000);
70
			continue;
71
		}
7 nishi 72
		int cli = accept(server, (struct sockaddr*)&cun, &socklen);
8 nishi 73
		while(1){
74
			if(recv(cli, cbuf, 1, 0) <= 0) break;
75
			if(cbuf[0] == '\n'){
76
				if(str[0] == 'U'){
77
					int err = mk_start_service(str + 1);
78
					if(err != 0){
79
						send(cli, "E", 1, 0);
80
						send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
81
						send(cli, "\n", 1, 0);
82
					}else{
10 nishi 83
						send(cli, "MOk\n", 4, 0);
8 nishi 84
					}
85
				}else if(str[0] == 'D'){
86
					int err = mk_stop_service(str + 1);
87
					if(err != 0){
88
						send(cli, "E", 1, 0);
89
						send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
90
						send(cli, "\n", 1, 0);
91
					}else{
10 nishi 92
						send(cli, "MOk\n", 4, 0);
8 nishi 93
					}
10 nishi 94
				}else if(str[0] == 'R'){
95
					mk_service_scan();
96
					send(cli, "MOk\n", 4, 0);
8 nishi 97
				}else{
98
					send(cli, PROTOCOL_ERROR, strlen(PROTOCOL_ERROR), 0);
99
				}
100
				free(str);
101
				str = malloc(1);
102
				str[0] = 0;
103
				break;
104
			}else if(cbuf[0] != '\r'){
105
				char* tmp = str;
106
				str = mk_strcat(tmp, cbuf);
107
				free(tmp);
108
			}
109
		}
7 nishi 110
		close(cli);
111
	}
10 nishi 112
	free(str);
7 nishi 113
}