Subversion Repositories Mokou

Rev

Rev 2 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2 Rev 11
Line 1... Line 1...
1
/* $Id: main.c 2 2024-09-05 18:31:51Z nishi $ */
1
/* $Id: main.c 11 2024-09-07 10:07:49Z nishi $ */
-
 
2
 
-
 
3
#include <sys/types.h>
-
 
4
#include <sys/socket.h>
-
 
5
#include <sys/un.h>
-
 
6
 
-
 
7
#include <stdbool.h>
-
 
8
#include <stdio.h>
-
 
9
#include <string.h>
-
 
10
#include <stdlib.h>
-
 
11
#include <unistd.h>
-
 
12
#include <errno.h>
2
 
13
 
3
int main(int argc, char** argv){
14
int main(int argc, char** argv){
-
 
15
	if(argc < 2){
-
 
16
		fprintf(stderr, "Usage: %s action [target]\n", argv[0]);
-
 
17
		fprintf(stderr, "Actions:\n");
-
 
18
		fprintf(stderr, "\treload   Reloads the service direcetory\n");
-
 
19
		fprintf(stderr, "\tstart    Start the service\n");
-
 
20
		fprintf(stderr, "\tstop     Stop the service\n");
-
 
21
		return 1;
-
 
22
	}
-
 
23
	char* msg = NULL;
-
 
24
	if(strcmp(argv[1], "reload") == 0){
-
 
25
		msg = malloc(3);
-
 
26
		msg[0] = 'R';
-
 
27
		msg[1] = '\n';
-
 
28
		msg[2] = 0;
-
 
29
	}else if(strcmp(argv[1], "start") == 0){
-
 
30
		if(argv[2] == NULL){
-
 
31
			fprintf(stderr, "Target is needed for this action\n");
-
 
32
			return 1;
-
 
33
		}
-
 
34
		msg = malloc(3 + strlen(argv[2]));
-
 
35
		msg[0] = 'U';
-
 
36
		strcpy(msg + 1, argv[2]);
-
 
37
		msg[1 + strlen(argv[2])] = '\n';
-
 
38
		msg[2 + strlen(argv[2])] = 0;
-
 
39
	}else if(strcmp(argv[1], "stop") == 0){
-
 
40
		if(argv[2] == NULL){
-
 
41
			fprintf(stderr, "Target is needed for this action\n");
-
 
42
			return 1;
-
 
43
		}
-
 
44
		msg = malloc(3 + strlen(argv[2]));
-
 
45
		msg[0] = 'D';
-
 
46
		strcpy(msg + 1, argv[2]);
-
 
47
		msg[1 + strlen(argv[2])] = '\n';
-
 
48
		msg[2 + strlen(argv[2])] = 0;
-
 
49
	}else{
-
 
50
		fprintf(stderr, "Unknown action: %s\n", argv[1]);
-
 
51
		return 1;
-
 
52
	}
-
 
53
	if(msg != NULL){
-
 
54
		int sock = socket(AF_LOCAL, SOCK_STREAM, 0);
-
 
55
		if(sock == -1){
-
 
56
			fprintf(stderr, "Socket creation failure\n");
-
 
57
			return 1;
-
 
58
		}
-
 
59
		struct sockaddr_un sun;
-
 
60
		sun.sun_family = AF_LOCAL;
-
 
61
		strcpy(sun.sun_path, "/tmp/mokou.sock");
-
 
62
		if(connect(sock, (struct sockaddr*)&sun, sizeof(sun)) == -1){
-
 
63
			fprintf(stderr, "Connection failure: %s\n", strerror(errno));
-
 
64
			close(sock);
-
 
65
		}
-
 
66
		send(sock, msg, strlen(msg), 0);
-
 
67
		
-
 
68
		char cbuf[2];
-
 
69
		cbuf[1] = 0;
-
 
70
 
-
 
71
		bool first = true;
-
 
72
		bool err = false;
-
 
73
		while(1){
-
 
74
			if(recv(sock, cbuf, 1, 0) <= 0) break;
-
 
75
			if(cbuf[0] == '\n'){
-
 
76
				break;
-
 
77
			}else if(cbuf[0] != '\r'){
-
 
78
				if(first && cbuf[0] == 'E'){
-
 
79
					err = true;
-
 
80
					fprintf(stderr, "Error: ");
-
 
81
				}else if(!first){
-
 
82
					fprintf(err ? stderr : stdout, "%c", cbuf[0]);
-
 
83
				}
-
 
84
				first = false;
-
 
85
			}
-
 
86
		}
-
 
87
		fprintf(err ? stderr : stdout, "\n");
-
 
88
 
-
 
89
		close(sock);
-
 
90
		free(msg);
-
 
91
	}
4
}
92
}