Subversion Repositories Mokou

Rev

Rev 9 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 9 Rev 10
Line 1... Line 1...
1
/* $Id: server.c 9 2024-09-07 09:25:26Z nishi $ */
1
/* $Id: server.c 10 2024-09-07 09:55:12Z nishi $ */
2
 
2
 
3
#include "mk_server.h"
3
#include "mk_server.h"
4
 
4
 
5
#include "mk_service.h"
5
#include "mk_service.h"
6
#include "mk_version.h"
6
#include "mk_version.h"
7
#include "mk_util.h"
7
#include "mk_util.h"
8
#include "mk_log.h"
8
#include "mk_log.h"
9
 
9
 
-
 
10
#include <string.h>
10
#include <stdio.h>
11
#include <stdio.h>
11
#include <stdlib.h>
12
#include <stdlib.h>
12
#include <unistd.h>
13
#include <unistd.h>
13
#include <poll.h>
14
#include <poll.h>
-
 
15
#include <time.h>
14
 
16
 
15
#include <sys/types.h>
17
#include <sys/types.h>
16
#include <sys/socket.h>
18
#include <sys/socket.h>
17
#include <sys/un.h>
19
#include <sys/un.h>
18
 
20
 
Line 46... Line 48...
46
#define PROTOCOL_ERROR "EProtocol Error\n"
48
#define PROTOCOL_ERROR "EProtocol Error\n"
47
 
49
 
48
void mk_server_loop(void){
50
void mk_server_loop(void){
49
	struct sockaddr_un cun;
51
	struct sockaddr_un cun;
50
	socklen_t socklen = sizeof(cun);
52
	socklen_t socklen = sizeof(cun);
51
	char* ver = mk_strcat3("R", mk_get_version(), "\n");
-
 
52
	char cbuf[2];
53
	char cbuf[2];
53
	cbuf[1] = 0;
54
	cbuf[1] = 0;
54
	char* str = malloc(1);
55
	char* str = malloc(1);
55
	str[0] = 0;
56
	str[0] = 0;
56
	struct pollfd pollfds[16 + 1];
57
	struct pollfd pollfds[16 + 1];
57
	pollfds[0].fd = server;
58
	pollfds[0].fd = server;
58
	pollfds[0].events = POLLIN | POLLPRI;
59
	pollfds[0].events = POLLIN | POLLPRI;
-
 
60
	time_t trigger = time(NULL) + 5;
59
	while(1){
61
	while(1){
60
		mk_log("Waiting for the connection");
-
 
61
		int r = poll(pollfds, 16 + 1, 5000);
62
		int r = poll(pollfds, 16 + 1, 5000);
-
 
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
		}
62
		int cli = accept(server, (struct sockaddr*)&cun, &socklen);
72
		int cli = accept(server, (struct sockaddr*)&cun, &socklen);
63
		send(cli, ver, strlen(ver), 0);
-
 
64
		while(1){
73
		while(1){
65
			if(recv(cli, cbuf, 1, 0) <= 0) break;
74
			if(recv(cli, cbuf, 1, 0) <= 0) break;
66
			if(cbuf[0] == '\n'){
75
			if(cbuf[0] == '\n'){
67
				if(str[0] == 'U'){
76
				if(str[0] == 'U'){
68
					int err = mk_start_service(str + 1);
77
					int err = mk_start_service(str + 1);
69
					if(err != 0){
78
					if(err != 0){
70
						send(cli, "E", 1, 0);
79
						send(cli, "E", 1, 0);
71
						send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
80
						send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
72
						send(cli, "\n", 1, 0);
81
						send(cli, "\n", 1, 0);
73
					}else{
82
					}else{
74
						send(cli, "Mok\n", 4, 0);
83
						send(cli, "MOk\n", 4, 0);
75
					}
84
					}
76
				}else if(str[0] == 'D'){
85
				}else if(str[0] == 'D'){
77
					int err = mk_stop_service(str + 1);
86
					int err = mk_stop_service(str + 1);
78
					if(err != 0){
87
					if(err != 0){
79
						send(cli, "E", 1, 0);
88
						send(cli, "E", 1, 0);
80
						send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
89
						send(cli, mk_errors[err], strlen(mk_errors[err]), 0);
81
						send(cli, "\n", 1, 0);
90
						send(cli, "\n", 1, 0);
82
					}else{
91
					}else{
83
						send(cli, "Mok\n", 4, 0);
92
						send(cli, "MOk\n", 4, 0);
84
					}
93
					}
-
 
94
				}else if(str[0] == 'R'){
-
 
95
					mk_service_scan();
-
 
96
					send(cli, "MOk\n", 4, 0);
85
				}else{
97
				}else{
86
					send(cli, PROTOCOL_ERROR, strlen(PROTOCOL_ERROR), 0);
98
					send(cli, PROTOCOL_ERROR, strlen(PROTOCOL_ERROR), 0);
87
				}
99
				}
88
				free(str);
100
				free(str);
89
				str = malloc(1);
101
				str = malloc(1);
Line 95... Line 107...
95
				free(tmp);
107
				free(tmp);
96
			}
108
			}
97
		}
109
		}
98
		close(cli);
110
		close(cli);
99
	}
111
	}
100
	free(ver);
-
 
101
	free(ver);
112
	free(str);
102
}
113
}