|
@@ -0,0 +1,213 @@
|
|
1
|
+#include "monitor.h"
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+pid_t pyfcgi_spawn_monitor()
|
|
5
|
+{
|
|
6
|
+ pid_t res;
|
|
7
|
+
|
|
8
|
+ res = fork();
|
|
9
|
+ if(res == -1)
|
|
10
|
+ {
|
|
11
|
+ pyfcgi_log(LOG_ALERT, "Unable to fork into monitoring server process : %s",
|
|
12
|
+ strerror(errno));
|
|
13
|
+ sleep(1);
|
|
14
|
+ }
|
|
15
|
+ if(!res)
|
|
16
|
+ {
|
|
17
|
+ pyfcgi_logger_set_ident("StatServ");
|
|
18
|
+ pyfcgi_monitor_init();
|
|
19
|
+ pyfcgi_monitor_loop();
|
|
20
|
+ pyfcgi_log(LOG_ALERT, "Monitor loop should never return but just did it...");
|
|
21
|
+ exit(PYFCGI_FATAL);
|
|
22
|
+ }
|
|
23
|
+ return res;
|
|
24
|
+}
|
|
25
|
+
|
|
26
|
+void pyfcgi_monitor_init()
|
|
27
|
+{
|
|
28
|
+
|
|
29
|
+}
|
|
30
|
+
|
|
31
|
+void pyfcgi_monitor_loop()
|
|
32
|
+{
|
|
33
|
+
|
|
34
|
+}
|
|
35
|
+
|
|
36
|
+int pyfcgi_monitor_check_sock(const char* sockurl)
|
|
37
|
+{
|
|
38
|
+ const char *port;
|
|
39
|
+ short tcp;
|
|
40
|
+ const char *urlorig;
|
|
41
|
+
|
|
42
|
+ urlorig = sockurl;
|
|
43
|
+
|
|
44
|
+ if(!(tcp = strncasecmp("tcp://", sockurl, 6)) ||
|
|
45
|
+ !strncasecmp("udp://", sockurl, 6))
|
|
46
|
+ {
|
|
47
|
+ sockurl += 6; //first addr chr
|
|
48
|
+ }
|
|
49
|
+ else if(!strncasecmp("unix://", sockurl, 7))
|
|
50
|
+ {
|
|
51
|
+ sockurl += 7;
|
|
52
|
+ if(strlen(sockurl) > UNIX_SOCKPATH_MAX)
|
|
53
|
+ {
|
|
54
|
+ dprintf(2, "UNIX socket support only path with length <= %d but given path is %ld bytes long : '%s'",
|
|
55
|
+ UNIX_SOCKPATH_MAX, strlen(sockurl), urlorig);
|
|
56
|
+ return -1;
|
|
57
|
+ }
|
|
58
|
+ return 0;
|
|
59
|
+ }
|
|
60
|
+ else
|
|
61
|
+ {
|
|
62
|
+ dprintf(2, "Invalid protocol in '%s'\n", sockurl);
|
|
63
|
+ return -1;
|
|
64
|
+ }
|
|
65
|
+ do { sockurl++; } while(*sockurl && *sockurl != ':');
|
|
66
|
+ if(!sockurl)
|
|
67
|
+ {
|
|
68
|
+ dprintf(2, "%s protocol choosen but not port given : '%s'\n",
|
|
69
|
+ tcp?"TCP":"UDP", sockurl);
|
|
70
|
+ return -1;
|
|
71
|
+ }
|
|
72
|
+ sockurl++;
|
|
73
|
+ port = sockurl;
|
|
74
|
+ while(*sockurl && *sockurl >= '0' && *sockurl <= '9')
|
|
75
|
+ {
|
|
76
|
+ sockurl++;
|
|
77
|
+ }
|
|
78
|
+ if(sockurl)
|
|
79
|
+ {
|
|
80
|
+ dprintf(2, "Invalid port '%s' in socket URL '%s'\n",
|
|
81
|
+ port, urlorig);
|
|
82
|
+ return -1;
|
|
83
|
+ }
|
|
84
|
+ return 0;
|
|
85
|
+}
|
|
86
|
+
|
|
87
|
+int pyfcgi_monitor_parse_sock(const char *sockurl, int sockargs[3],
|
|
88
|
+ pyfcgi_monitor_addr_t *listen_addr)
|
|
89
|
+{
|
|
90
|
+ const char *addr_ptr;
|
|
91
|
+ short tcp;
|
|
92
|
+ int *domain, *type, *protocol;
|
|
93
|
+ struct sockaddr_un *addr_un;
|
|
94
|
+
|
|
95
|
+ domain = &sockargs[0];
|
|
96
|
+ type = &sockargs[1];
|
|
97
|
+ protocol = &sockargs[2];
|
|
98
|
+
|
|
99
|
+ addr_un = &(listen_addr->un);
|
|
100
|
+
|
|
101
|
+ sockurl = PyFCGI_conf.mon_socket;
|
|
102
|
+ if(strncasecmp("unix://", sockurl, 7))
|
|
103
|
+ {
|
|
104
|
+ addr_ptr = sockurl + 7;
|
|
105
|
+ *domain = AF_UNIX;
|
|
106
|
+ *type = SOCK_STREAM;
|
|
107
|
+ *protocol = 0;
|
|
108
|
+ addr_un->sun_family = AF_UNIX;
|
|
109
|
+ strncpy(addr_un->sun_path, addr_ptr, UNIX_SOCKPATH_MAX);
|
|
110
|
+ return 0;
|
|
111
|
+ }
|
|
112
|
+
|
|
113
|
+ if((tcp = strncasecmp("tcp://", sockurl, 6)) &&
|
|
114
|
+ strncasecmp("udp://", sockurl, 6))
|
|
115
|
+ { //Unchecked URL??!!
|
|
116
|
+ pyfcgi_log(LOG_ERR, "Invalid protocol in URL : '%s'",
|
|
117
|
+ sockurl);
|
|
118
|
+ return -1;
|
|
119
|
+ }
|
|
120
|
+ addr_ptr = sockurl + 6;
|
|
121
|
+
|
|
122
|
+ if(pyfcgi_monitor_parse_inet_addr(addr_ptr, *type, listen_addr, domain))
|
|
123
|
+ {
|
|
124
|
+ return -1;
|
|
125
|
+ }
|
|
126
|
+ *type = tcp ? SOCK_DGRAM : SOCK_STREAM;
|
|
127
|
+ *protocol = 0;
|
|
128
|
+ return 0;
|
|
129
|
+}
|
|
130
|
+
|
|
131
|
+int pyfcgi_monitor_parse_inet_addr(const char* addr_str, int socktype,
|
|
132
|
+ pyfcgi_monitor_addr_t *listen_addr, int* domain)
|
|
133
|
+{
|
|
134
|
+ char *addr, *port, *ptr, *ipstr;
|
|
135
|
+ char v6str[64];
|
|
136
|
+ struct addrinfo *infos, hints, *info;
|
|
137
|
+ short v4, v6, i;
|
|
138
|
+ int ret;
|
|
139
|
+
|
|
140
|
+ // initialize temporary address & port pointers
|
|
141
|
+ addr = strdup(addr_str);
|
|
142
|
+ if(!addr)
|
|
143
|
+ {
|
|
144
|
+ pyfcgi_log(LOG_ALERT, "strdup() failed to copy socket addr : %s",
|
|
145
|
+ strerror(errno));
|
|
146
|
+ return -1;
|
|
147
|
+ }
|
|
148
|
+ ptr = addr;
|
|
149
|
+ do { ptr++; }while(*ptr && *ptr != ':');
|
|
150
|
+ if(!ptr)
|
|
151
|
+ {
|
|
152
|
+ pyfcgi_log(LOG_ERR, "No port found in INET url : %s",
|
|
153
|
+ addr_str);
|
|
154
|
+ goto free_err;
|
|
155
|
+ }
|
|
156
|
+ *ptr = '\0';
|
|
157
|
+ port = ptr+1;
|
|
158
|
+
|
|
159
|
+ memset(&hints, 0, sizeof(struct addrinfo));
|
|
160
|
+ v4 = PyFCGI_conf.ipv4;
|
|
161
|
+ v6 = PyFCGI_conf.ipv6;
|
|
162
|
+ hints.ai_family = (v4?AF_INET:(v6?AF_INET6:PF_UNSPEC));
|
|
163
|
+ hints.ai_socktype = socktype;
|
|
164
|
+ hints.ai_flags = AI_CANONNAME;
|
|
165
|
+
|
|
166
|
+ if((ret = getaddrinfo(addr, port, &hints, &infos)))
|
|
167
|
+ {
|
|
168
|
+ pyfcgi_log(LOG_ALERT, "getaddrinfo fails on '%s' : %s",
|
|
169
|
+ gai_strerror(ret));
|
|
170
|
+ goto free_err;
|
|
171
|
+ }
|
|
172
|
+
|
|
173
|
+ for(info = infos; info != NULL; info = info->ai_next)
|
|
174
|
+ {
|
|
175
|
+ if(info->ai_family == AF_INET)
|
|
176
|
+ {
|
|
177
|
+ memcpy(&listen_addr->in, info->ai_addr,
|
|
178
|
+ info->ai_addrlen);
|
|
179
|
+ ipstr = inet_ntoa(listen_addr->in.sin_addr);
|
|
180
|
+ }
|
|
181
|
+ else if(info->ai_family == AF_INET6)
|
|
182
|
+ {
|
|
183
|
+ memcpy(&listen_addr->in6, info->ai_addr,
|
|
184
|
+ info->ai_addrlen);
|
|
185
|
+ ptr = v6str;
|
|
186
|
+ for(i=0; i<16; i++)
|
|
187
|
+ {
|
|
188
|
+ ptr += snprintf(ptr, 4,
|
|
189
|
+ "%s%02X", ((i==0)?"":":"),
|
|
190
|
+ listen_addr->in6.sin6_addr.s6_addr[i]);
|
|
191
|
+ }
|
|
192
|
+ ipstr = v6str;
|
|
193
|
+ }
|
|
194
|
+ else
|
|
195
|
+ {
|
|
196
|
+ continue;
|
|
197
|
+ }
|
|
198
|
+ pyfcgi_log(LOG_DEBUG, "Listen addr resolved to %s(%s)",
|
|
199
|
+ info->ai_canonname, ipstr);
|
|
200
|
+
|
|
201
|
+ freeaddrinfo(infos);
|
|
202
|
+ free(addr);
|
|
203
|
+ return 0;
|
|
204
|
+ }
|
|
205
|
+
|
|
206
|
+ pyfcgi_log(LOG_ERR, "Unable to resolve to a valid AF_INET[6] address");
|
|
207
|
+ freeaddrinfo(infos);
|
|
208
|
+
|
|
209
|
+free_err:
|
|
210
|
+ free(addr);
|
|
211
|
+ return -1;
|
|
212
|
+}
|
|
213
|
+
|