-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
213 lines (185 loc) · 4.07 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <sys/types.h>
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/un.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#include <stdarg.h>
#define NAME "/tmp/deamon.socket"
#define CMD_START "START"
#define CMD_STOP "STOP"
#define CMD_RESTART "RESTART"
#define CMD_SWITCH_CAM "SWITCH_SOFTCAM"
#define CMD_SWITCH_CARDSERVER "SWITCH_CARDSERVER"
static int verbose = 0;
int processMessage(char *inData);
static FILE *log_stream;
void LOG(const char *format, ...)
{
char buf[2048];
char timebuf[50];
va_list other_args;
time_t t;
struct tm *tm;
va_start(other_args, format);
vsnprintf(buf, sizeof(buf), format, other_args);
va_end(other_args);
time(&t);
tm = gmtime(&t);
if (tm)
{
strftime(timebuf, sizeof(timebuf), "%Y-%m-%dT%H:%M:%SZ", tm);
fprintf(log_stream, "[%s] %s", timebuf, buf);
}
else
{
fprintf(log_stream, "%s", buf);
}
fflush(log_stream);
}
int main(int argc, char **argv)
{
int sock, msgsock, rval;
struct sockaddr_un server;
char buf[256];
int c = 0;
log_stream = stdout;
while ((c = getopt(argc, argv, "v")) != -1)
{
if (c == 'v')
verbose = 1;
}
if (unlink(NAME) == -1 && errno != ENOENT)
{
perror("delete server socket");
exit(1);
}
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock < 0)
{
perror("opening stream socket");
exit(1);
}
server.sun_family = AF_UNIX;
strcpy(server.sun_path, NAME);
if (bind(sock, (struct sockaddr *)&server, sizeof(struct sockaddr_un)))
{
perror("binding stream socket");
exit(1);
}
LOG("Start\n");
int val = 1;
// LOG("Socket has name %s\n", server.sun_path);
listen(sock, 5);
for (;;)
{
msgsock = accept(sock, 0, 0);
if (msgsock == -1)
perror("accept");
else
do
{
bzero(buf, sizeof(buf));
if ((rval = read(msgsock, buf, 256)) < 0) {
perror("reading stream message");
}
else
{
if (strlen(buf) > 0)
{
if (verbose)
LOG("processMessage %d --> '%s' \n", strlen(buf), buf);
int rc = processMessage(buf);
size_t wr = send(msgsock, "DONE", 4, MSG_NOSIGNAL);
if (verbose)
LOG("write DONE --> %d\n", wr);
}
}
} while (rval > 0);
close(msgsock);
}
close(sock);
unlink(NAME);
LOG("END");
return EXIT_SUCCESS;
}
int processMessage(char *inData)
{
char *tmp;
char command[20];
char data[256];
char buff[256];
char cmd[100];
int rc;
buff[0] = 0;
tmp = strchr(inData, ',');
if (tmp)
{
strncpy(command, inData, tmp - inData);
command[tmp - inData] = 0;
strcpy(data, tmp + 1);
}
else
{
strcpy(command, inData);
data[0] = 0;
}
if (verbose)
LOG("processMessage Command='%s'\n", command);
if (strcmp(command, CMD_SWITCH_CAM) == 0)
{
rc = system("/etc/init.d/softcam stop");
usleep(500000); // wait 500 ms after stop
if (verbose)
LOG("Run softcam stop -> RC %d\n", rc);
unlink("/etc/init.d/softcam");
sprintf(cmd, "ln -s /etc/init.d/softcam.%s /etc/init.d/softcam", data);
rc = system(cmd);
if (verbose)
LOG("Run cmd='%s' -> RC %d\n", cmd, rc);
rc = system("/etc/init.d/softcam start");
if (verbose)
LOG("Run softcam start -> RC %d\n", rc);
}
else if (strcmp(command, CMD_SWITCH_CARDSERVER) == 0)
{
rc = system("/etc/init.d/cardserver stop");
usleep(500000); // wait 500 ms after stop
if (verbose)
LOG("Run cardserver stop -> RC %d\n", rc);
unlink("/etc/init.d/cardserver");
sprintf(cmd, "ln -s /etc/init.d/cardserver.%s /etc/init.d/cardserver", data);
rc = system(cmd);
if (verbose)
LOG("Run cmd='%s' -> RC %d\n", cmd, rc);
rc = system("/etc/init.d/cardserver start");
if (verbose)
LOG("Run cardserver start -> RC %d\n", rc);
}
else
{
if (strcmp(command, CMD_RESTART) == 0)
{
sprintf(cmd, "/etc/init.d/%s restart", data);
}
else if (strcmp(command, CMD_STOP) == 0)
{
sprintf(cmd, "/etc/init.d/%s stop", data);
}
else if (strcmp(command, CMD_START) == 0)
{
sprintf(cmd, "/etc/init.d/%s start", data);
}
else
return -1;
rc = system(cmd);
if (verbose)
LOG("Run cmd='%s' -> RC %d\n", cmd, rc);
}
return rc;
}