-
Notifications
You must be signed in to change notification settings - Fork 1
/
hubmanager.c
164 lines (147 loc) · 4.04 KB
/
hubmanager.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
const char *decoder_dir = NULL;
const char *executable = "tcphub";
int *hubs;
int hub_count = 0;
int hub_size = 0;
int hub_dead = 0;
void usage(char *cmd);
#define PID(i) (2*i)
#define PORT(i) ((2*i)+1)
int start_tcphub(const char *port, const char *decoder);
void sighandler(int signal)
{
int status;
int pid;
int value;
int i;
while ((pid = waitpid(-1, &status, WNOHANG)) != 0) {
if (pid < 0) {
if (errno == EINTR) {
continue;
} else if (errno == ECHILD) {
break;
} else {
perror("waitpid");
}
} else {
if (WIFEXITED(status)) {
value = WEXITSTATUS(status);
for (i = 0 ; i < hub_count ; i++) {
if (hubs[PID(i)] == pid) {
printf("[%5d] tcphub terminated with status code %d\n",
hubs[PORT(i)], value);
hub_dead++;
break;
}
}
} else if (WIFSIGNALED(status)) {
value = WTERMSIG(status);
for (i = 0 ; i < hub_count ; i++) {
if (hubs[PID(i)] == pid) {
printf("[%5d] tcphub terminated by signal %d\n",
hubs[PORT(i)], value);
hub_dead++;
break;
}
}
} else {
printf("event for process %d\n", pid);
}
}
}
}
int main(int argc, char *argv[])
{
int i, n, p;
char port[16];
char opt_decoder[256];
char *decoder = NULL;
if (argc < 2) {
usage(argv[0]);
return 0;
}
if (signal(SIGCHLD, sighandler) < 0) {
perror("signal");
return 1;
}
for (i = 1 ; i < argc ; i++) {
if (argv[i][0] == '-') {
if (!strcmp(argv[i], "-d") && (argc > i + 1)) {
decoder_dir = argv[++i];
} else {
usage(argv[0]);
return 1;
}
} else {
n = sscanf(argv[i], "%d@%256s", &p, opt_decoder);
if (n < 1) {
fprintf(stderr, "arg %d: invalid syntaxe, ignored\n", i);
continue;
}
if (n < 2) {
decoder = NULL;
} else {
decoder = opt_decoder;
}
if (p < 1) {
fprintf(stderr, "arg %d: invalid port, ignored\n", i);
continue;
}
sprintf(port, "%d", p);
if (start_tcphub(port, decoder) < 0) {
fprintf(stderr, "[%s] failed to launch tcphub\n", port);
continue;
}
}
}
pause();
while (hub_dead < hub_count)
pause();
}
int start_tcphub(const char *port, const char *decoder)
{
int pid, i;
int p = atoi(port);
int *new = NULL;
if (hub_count == hub_size) {
if ((new = realloc(hubs, (hub_size+5) * sizeof(*hubs) * 2)) == NULL) {
perror("realloc");
return -1;
}
hubs = new;
hub_size += 5;
}
pid = fork();
switch (pid) {
case -1:
perror("fork");
return -1;
case 0:
for (i = 0 ; i < getdtablesize() ; i++) {
close(i);
}
execlp(executable, "tcphub", port, decoder, decoder_dir, NULL);
perror("execlp");
exit(EXIT_FAILURE);
}
hubs[PID(hub_count)] = pid;
hubs[PORT(hub_count)] = p;
hub_count++;
if (decoder == NULL) {
decoder = "default";
}
printf("[%5d] tcphub launched with pid %d, decoder: %s\n", p, pid, decoder);
return 0;
}
void usage(char *cmd)
{
printf("Usage: %s [-d decoder_directory] "
"PORT1[@DECODER1] PORT2[@DECODER2] …\n", cmd);
}