1 |
|
/* |
2 |
|
* i-scream central monitoring system |
3 |
< |
* Copyright (C) 2000-2002 i-scream |
3 |
> |
* http://www.i-scream.org |
4 |
> |
* Copyright (C) 2000-2004 i-scream |
5 |
|
* |
6 |
|
* This program is free software; you can redistribute it and/or |
7 |
|
* modify it under the terms of the GNU General Public License |
18 |
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
*/ |
20 |
|
|
21 |
+ |
#ifdef HAVE_CONFIG_H |
22 |
+ |
#include "config.h" |
23 |
+ |
#endif |
24 |
+ |
|
25 |
|
#include <stdio.h> |
26 |
|
#include <stdlib.h> |
22 |
– |
#include <sys/types.h> |
27 |
|
#include <unistd.h> |
24 |
– |
#include <syslog.h> |
25 |
– |
#include <netinet/in.h> |
26 |
– |
#include "ukcprog.h" |
27 |
– |
#include <netdb.h> |
28 |
|
#include <string.h> |
29 |
< |
#include "statgrab.h" |
30 |
< |
#include <time.h> |
29 |
> |
#include <sys/types.h> |
30 |
> |
#include <stdarg.h> |
31 |
> |
#include <errno.h> |
32 |
> |
#ifndef WIN32 |
33 |
|
#include <sys/socket.h> |
34 |
+ |
#include <netdb.h> |
35 |
|
#include <netinet/in.h> |
36 |
< |
#include <arpa/inet.h> |
37 |
< |
#include <syslog.h> |
36 |
> |
#else |
37 |
> |
#include <winsock2.h> |
38 |
> |
#include <getopt.h> |
39 |
> |
#endif |
40 |
> |
#include <time.h> |
41 |
|
|
42 |
< |
#define versionNo 0.8 |
43 |
< |
#define RECONFIGURE_RETURN_CODE 2 |
38 |
< |
#define UDP_MAX_PACKET_SIZE 8192 |
39 |
< |
#define PID_FILE "/var/tmp/.ihost.pid" |
42 |
> |
#include <ukcprog.h> |
43 |
> |
#include <statgrab.h> |
44 |
|
|
45 |
< |
#define logmessage(level, ...) do { cur_level = level; errf(__VA_ARGS__); } while (0) |
45 |
> |
#define LOG_CRIT 0 |
46 |
> |
#define LOG_ERR 1 |
47 |
> |
#define LOG_INFO 2 |
48 |
> |
#define LOG_DEBUG 3 |
49 |
|
|
50 |
+ |
#define SERVICENAME "ihost" |
51 |
+ |
|
52 |
+ |
/* Windows printf does not understand %lld, so we have to use %I64d */ |
53 |
+ |
#ifdef WIN32 |
54 |
+ |
#define LLD "%I64d" |
55 |
+ |
#define OPTSTRING "vVn:i:s:p:w:h" |
56 |
+ |
#define SLEEP 10000 |
57 |
+ |
#else |
58 |
+ |
#define LLD "%lld" |
59 |
+ |
#define OPTSTRING "vfVn:i:s:p:h" |
60 |
+ |
#define SLEEP 10 |
61 |
+ |
#endif |
62 |
+ |
|
63 |
+ |
/* The "socket" */ |
64 |
+ |
#ifdef WIN32 |
65 |
+ |
#define IHOST_SOCKET SOCKET |
66 |
+ |
#define CLOSESOCKET(socket) closesocket(socket) |
67 |
+ |
#else |
68 |
+ |
#define IHOST_SOCKET FILE * |
69 |
+ |
#define CLOSESOCKET(socket) fclose(socket) |
70 |
+ |
#endif |
71 |
+ |
|
72 |
|
typedef struct{ |
73 |
< |
int fm_port; |
74 |
< |
char *fm_host; |
73 |
> |
int filtermanager_port; |
74 |
> |
char *filtermanager_host; |
75 |
|
|
76 |
< |
char *my_ip; |
77 |
< |
char *my_fqdn; |
76 |
> |
char *host_ip; |
77 |
> |
char *host_fqdn; |
78 |
> |
int preset_fqdn; |
79 |
> |
int preset_ip; |
80 |
> |
|
81 |
|
char *server_fqdn; |
82 |
|
int server_udp_port; |
83 |
< |
int server_tcp_port; |
83 |
> |
|
84 |
> |
/* Weird stuff iscream wants sent to it */ |
85 |
|
char *last_modified; |
86 |
< |
char *files_list; |
87 |
< |
char *key; |
86 |
> |
char *file_list; |
87 |
> |
|
88 |
|
int udp_update_time; |
56 |
– |
int tcp_update_time; |
89 |
|
|
90 |
+ |
time_t config_ttl; |
91 |
+ |
|
92 |
|
}ihost_state_t; |
93 |
|
|
94 |
< |
static int log_level; |
95 |
< |
static int cur_level; |
96 |
< |
static int syslog_logging; |
94 |
> |
typedef struct{ |
95 |
> |
int verbose; |
96 |
> |
int daemon; |
97 |
|
|
98 |
< |
void log_errors(const char *message){ |
99 |
< |
if(log_level>=cur_level){ |
100 |
< |
if (syslog_logging==1){ |
101 |
< |
syslog(cur_level, "%s\n", message); |
102 |
< |
}else{ |
103 |
< |
fprintf(stderr, "%s\n", message); |
98 |
> |
FILE *log; |
99 |
> |
}ihost_config_t; |
100 |
> |
|
101 |
> |
typedef struct{ |
102 |
> |
struct sockaddr_in addr; |
103 |
> |
int sock; |
104 |
> |
}udp_sockinfo_t; |
105 |
> |
|
106 |
> |
ihost_config_t ihost_config; |
107 |
> |
ihost_state_t ihost_state; |
108 |
> |
|
109 |
> |
extern int errno; |
110 |
> |
|
111 |
> |
#ifdef WIN32 |
112 |
> |
int run_server = 1; |
113 |
> |
int run_as_service = 0; |
114 |
> |
SERVICE_STATUS service_status; |
115 |
> |
SERVICE_STATUS_HANDLE hstatus; |
116 |
> |
#endif |
117 |
> |
|
118 |
> |
/* Taken from the OpenSSH code. Its licence included in function.*/ |
119 |
> |
#ifndef HAVE_STRLCAT |
120 |
> |
|
121 |
> |
/* |
122 |
> |
* Copyright (c) 1998 Todd C. Miller <Todd.Miller@courtesan.com> |
123 |
> |
* All rights reserved. |
124 |
> |
* |
125 |
> |
* Redistribution and use in source and binary forms, with or without |
126 |
> |
* modification, are permitted provided that the following conditions |
127 |
> |
* are met: |
128 |
> |
* 1. Redistributions of source code must retain the above copyright |
129 |
> |
* notice, this list of conditions and the following disclaimer. |
130 |
> |
* 2. Redistributions in binary form must reproduce the above copyright |
131 |
> |
* notice, this list of conditions and the following disclaimer in the |
132 |
> |
* documentation and/or other materials provided with the distribution. |
133 |
> |
* 3. The name of the author may not be used to endorse or promote products |
134 |
> |
* derived from this software without specific prior written permission. |
135 |
> |
* |
136 |
> |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
137 |
> |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
138 |
> |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
139 |
> |
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
140 |
> |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
141 |
> |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
142 |
> |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
143 |
> |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
144 |
> |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
145 |
> |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
146 |
> |
*/ |
147 |
> |
|
148 |
> |
/* |
149 |
> |
* Appends src to string dst of size siz (unlike strncat, siz is the |
150 |
> |
* full size of dst, not space left). At most siz-1 characters |
151 |
> |
* will be copied. Always NUL terminates (unless siz <= strlen(dst)). |
152 |
> |
* Returns strlen(src) + MIN(siz, strlen(initial dst)). |
153 |
> |
* If retval >= siz, truncation occurred. |
154 |
> |
*/ |
155 |
> |
size_t |
156 |
> |
strlcat(dst, src, siz) |
157 |
> |
char *dst; |
158 |
> |
const char *src; |
159 |
> |
size_t siz; |
160 |
> |
{ |
161 |
> |
register char *d = dst; |
162 |
> |
register const char *s = src; |
163 |
> |
register size_t n = siz; |
164 |
> |
size_t dlen; |
165 |
> |
|
166 |
> |
/* Find the end of dst and adjust bytes left but don't go past end */ |
167 |
> |
while (n-- != 0 && *d != '\0') |
168 |
> |
d++; |
169 |
> |
dlen = d - dst; |
170 |
> |
n = siz - dlen; |
171 |
> |
|
172 |
> |
if (n == 0) |
173 |
> |
return(dlen + strlen(s)); |
174 |
> |
while (*s != '\0') { |
175 |
> |
if (n != 1) { |
176 |
> |
*d++ = *s; |
177 |
> |
n--; |
178 |
|
} |
179 |
< |
} |
179 |
> |
s++; |
180 |
> |
} |
181 |
> |
*d = '\0'; |
182 |
> |
|
183 |
> |
return(dlen + (s - src)); /* count does not include NUL */ |
184 |
|
} |
185 |
|
|
186 |
< |
char* sock_comm(FILE *f_r, FILE *f_w, char *sendString){ |
187 |
< |
char *reply; |
76 |
< |
fprintf(f_w, "%s\n", sendString); |
77 |
< |
fflush(f_w); |
78 |
< |
reply=fpgetline(f_r); |
79 |
< |
/* Returns pointer to static buffer */ |
80 |
< |
return reply; |
81 |
< |
} |
186 |
> |
#endif |
187 |
> |
/* End strlcat function taken from OpenSSH */ |
188 |
|
|
189 |
< |
int ihost_configure(ihost_state_t *ihost_state){ |
190 |
< |
struct sockaddr_in addr; |
191 |
< |
struct in_addr haddr; |
86 |
< |
struct sockaddr ip; |
87 |
< |
int ip_len; |
88 |
< |
int sd; |
89 |
< |
FILE *fm_fd_r, *fm_fd_w; |
90 |
< |
char *reply; |
91 |
< |
char *reply_ptr; |
189 |
> |
void log_msg(int level, char *format, ...){ |
190 |
> |
int cur_errno; |
191 |
> |
va_list ap; |
192 |
|
|
193 |
< |
/* Check to see if anything needs to be free'd */ |
94 |
< |
if (ihost_state->my_fqdn!=NULL) free(ihost_state->my_fqdn); |
95 |
< |
if (ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
96 |
< |
if (ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
97 |
< |
if (ihost_state->files_list!=NULL) free(ihost_state->files_list); |
193 |
> |
cur_errno=errno; |
194 |
|
|
195 |
< |
logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->fm_host, ihost_state->fm_port); |
196 |
< |
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
197 |
< |
logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
198 |
< |
return -1; |
199 |
< |
} |
200 |
< |
|
201 |
< |
if (get_host_addr(ihost_state->fm_host, &haddr) != 0){ |
202 |
< |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->fm_host); |
203 |
< |
return -1; |
195 |
> |
if(level<=ihost_config.verbose){ |
196 |
> |
va_start(ap, format); |
197 |
> |
vfprintf(ihost_config.log, format, ap); |
198 |
> |
va_end(ap); |
199 |
> |
if(level==LOG_CRIT){ |
200 |
> |
fprintf(ihost_config.log, " (%s)\n", strerror(cur_errno)); |
201 |
> |
}else{ |
202 |
> |
fprintf(ihost_config.log, "\n"); |
203 |
> |
} |
204 |
> |
fflush(ihost_config.log); |
205 |
|
} |
206 |
+ |
} |
207 |
|
|
208 |
< |
memset(&addr, 0, sizeof addr); |
111 |
< |
addr.sin_family = AF_INET; |
112 |
< |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
113 |
< |
addr.sin_port = htons(ihost_state->fm_port); |
208 |
> |
int create_udp_sockinfo(udp_sockinfo_t *udp_sockinfo, char *hostname, int port){ |
209 |
|
|
210 |
< |
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
211 |
< |
logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->fm_host, ihost_state->fm_port); |
212 |
< |
return -1; |
210 |
> |
struct in_addr haddr; |
211 |
> |
|
212 |
> |
log_msg(LOG_DEBUG, "Resolving name for udp connection"); |
213 |
> |
if(get_host_addr(hostname, &haddr) != 0){ |
214 |
> |
log_msg(LOG_CRIT, "Failed to lookup name"); |
215 |
> |
return 1; |
216 |
|
} |
217 |
|
|
218 |
< |
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
219 |
< |
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
220 |
< |
logmessage(LOG_ERR, "Failed to open read stream (%m)"); |
123 |
< |
return -1; |
218 |
> |
if((udp_sockinfo->sock=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
219 |
> |
log_msg(LOG_CRIT, "Failed to create UDP socket"); |
220 |
> |
return 1; |
221 |
|
} |
222 |
|
|
223 |
< |
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
224 |
< |
logmessage(LOG_ERR, "Failed to open write stream (%m)"); |
225 |
< |
return -1; |
223 |
> |
memset(&(udp_sockinfo->addr), 0, sizeof(struct sockaddr_in)); |
224 |
> |
udp_sockinfo->addr.sin_family=AF_INET; |
225 |
> |
memcpy((char *)&(udp_sockinfo->addr.sin_addr), &haddr, sizeof(haddr)); |
226 |
> |
udp_sockinfo->addr.sin_port = htons(port); |
227 |
> |
|
228 |
> |
log_msg(LOG_DEBUG, "Socket created"); |
229 |
> |
return 0; |
230 |
> |
} |
231 |
> |
|
232 |
> |
IHOST_SOCKET create_tcp_connection(char *hostname, int port){ |
233 |
> |
#ifdef WIN32 |
234 |
> |
SOCKET sock; |
235 |
> |
#else |
236 |
> |
int sock; |
237 |
> |
#endif |
238 |
> |
struct sockaddr_in addr; |
239 |
> |
struct in_addr haddr; |
240 |
> |
FILE *f; |
241 |
> |
|
242 |
> |
log_msg(LOG_DEBUG, "Creating tcp socket"); |
243 |
> |
if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP))<0){ |
244 |
> |
log_msg(LOG_CRIT, "Failed to make TCP Socket"); |
245 |
> |
goto out; |
246 |
|
} |
247 |
< |
ip_len=sizeof ip; |
248 |
< |
memset(&ip, 0, ip_len); |
249 |
< |
if((getsockname(sd, &ip, &ip_len)) != 0){ |
250 |
< |
logmessage(LOG_ERR, "Failed to get IP address (%m)"); |
251 |
< |
return -1; |
247 |
> |
|
248 |
> |
if((get_host_addr(hostname, &haddr))!=0){ |
249 |
> |
log_msg(LOG_CRIT, "Failed to lookup name for %s", hostname); |
250 |
> |
close(sock); |
251 |
> |
goto out; |
252 |
|
} |
253 |
< |
if (ip.sa_family!=AF_INET){ |
254 |
< |
logmessage(LOG_ERR, "sa family is wrong type"); |
255 |
< |
return -1; |
253 |
> |
|
254 |
> |
memset(&addr, 0, sizeof(addr)); |
255 |
> |
addr.sin_family = AF_INET; |
256 |
> |
memcpy(&addr.sin_addr, &haddr, sizeof(haddr)); |
257 |
> |
addr.sin_port = htons(port); |
258 |
> |
|
259 |
> |
log_msg(LOG_DEBUG, "Creating a tcp connection"); |
260 |
> |
if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) !=0){ |
261 |
> |
log_msg(LOG_CRIT, "Failed to connect to hostname %s on port %d", hostname, port); |
262 |
> |
close(sock); |
263 |
> |
goto out; |
264 |
|
} |
265 |
+ |
|
266 |
+ |
#ifndef WIN32 |
267 |
+ |
/* Windows does not treat sockets like this :( */ |
268 |
+ |
if((f=fdopen(sock, "r+"))==NULL){ |
269 |
+ |
log_msg(LOG_CRIT, "Failed to connect to open filedescriptor on tcp connection"); |
270 |
+ |
close(sock); |
271 |
+ |
return NULL; |
272 |
+ |
} |
273 |
+ |
|
274 |
+ |
return f; |
275 |
+ |
#else |
276 |
+ |
return sock; |
277 |
+ |
#endif |
278 |
+ |
|
279 |
+ |
out: |
280 |
+ |
#ifdef WIN32 |
281 |
+ |
return 0; |
282 |
+ |
#else |
283 |
+ |
return NULL; |
284 |
+ |
#endif |
285 |
+ |
} |
286 |
+ |
|
287 |
+ |
#ifndef WIN32 |
288 |
+ |
int tcp_comm(FILE *f, char *send, char **response, char *expected){ |
289 |
+ |
|
290 |
+ |
log_msg(LOG_DEBUG, "Sending %s", send); |
291 |
+ |
fprintf(f, "%s\n", send); |
292 |
+ |
fflush(f); |
293 |
+ |
*response=fpgetline(f); |
294 |
+ |
fseek(f, 0, SEEK_CUR); |
295 |
+ |
|
296 |
+ |
if(*response!=NULL) log_msg(LOG_DEBUG, "Received %s", *response); |
297 |
+ |
|
298 |
+ |
if( (*response==NULL) || (strcmp(*response, "ERROR")==0) ) return -1; |
299 |
+ |
|
300 |
+ |
if(expected==NULL) return 0; |
301 |
+ |
|
302 |
+ |
if((strcmp(expected, *response))==0) return 0; |
303 |
+ |
|
304 |
+ |
log_msg(LOG_DEBUG, "Did not get expected response"); |
305 |
+ |
return -1; |
306 |
+ |
} |
307 |
+ |
#else |
308 |
+ |
int tcp_comm(SOCKET soc, char *sendstr, char **response, char *expected) { |
309 |
+ |
static char *rec = NULL; |
310 |
+ |
char sendbuf[128]; |
311 |
+ |
char recbuf[128]; |
312 |
+ |
int bytesRecv; |
313 |
+ |
char *tmp; |
314 |
|
|
315 |
< |
if((ihost_state->my_ip=inet_ntoa(((struct sockaddr_in *)&ip)->sin_addr))==NULL){ |
316 |
< |
logmessage(LOG_ERR, "Failed to get IP (%m)"); |
315 |
> |
sprintf(sendbuf, "%s\n", sendstr); |
316 |
> |
|
317 |
> |
log_msg(LOG_DEBUG, "Sending %s", sendstr); |
318 |
> |
send(soc, sendbuf, strlen(sendbuf), 0); |
319 |
> |
bytesRecv = recv(soc, recbuf, 128, 0); |
320 |
> |
|
321 |
> |
if(bytesRecv != 0 && bytesRecv != SOCKET_ERROR) { |
322 |
> |
/* remove \n and copy to a string */ |
323 |
> |
recbuf[bytesRecv-1] = '\0'; |
324 |
> |
tmp = realloc(rec, bytesRecv); |
325 |
> |
if(tmp == NULL) { |
326 |
> |
free(rec); |
327 |
> |
rec = NULL; |
328 |
> |
log_msg(LOG_CRIT, "out of memory"); |
329 |
> |
return -1; |
330 |
> |
} |
331 |
> |
rec = tmp; |
332 |
> |
rec = strcpy(rec, recbuf); |
333 |
> |
*response = rec; |
334 |
> |
log_msg(LOG_DEBUG, "Received %s", *response); |
335 |
> |
} else { |
336 |
|
return -1; |
144 |
– |
} |
145 |
– |
|
146 |
– |
logmessage(LOG_DEBUG, "Sending STARTCONIG"); |
147 |
– |
reply=sock_comm(fm_fd_r, fm_fd_w, "STARTCONFIG"); |
148 |
– |
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
149 |
– |
logmessage(LOG_ERR, "Server error on STARTCONFIG"); |
150 |
– |
return -1; |
337 |
|
} |
338 |
+ |
if(strcmp(*response, "ERROR")==0) return -1; |
339 |
|
|
340 |
< |
logmessage(LOG_DEBUG, "Sending LASTMODIFIED"); |
341 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "LASTMODIFIED"); |
342 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
343 |
< |
logmessage(LOG_ERR, "Server error on LASTMODIFIED (%m)"); |
340 |
> |
if(expected==NULL) return 0; |
341 |
> |
|
342 |
> |
if((strcmp(expected, *response))==0) return 0; |
343 |
> |
|
344 |
> |
log_msg(LOG_DEBUG, "Did not get expected response"); |
345 |
> |
return -1; |
346 |
> |
} |
347 |
> |
#endif /* WIN32 */ |
348 |
> |
|
349 |
> |
int tcp_comm_strdup(IHOST_SOCKET f, char *send, char **response, char *expected) |
350 |
> |
{ |
351 |
> |
if((tcp_comm(f, send, response, expected))!=0){ |
352 |
|
return -1; |
353 |
|
} |
159 |
– |
if((ihost_state->last_modified=strdup(reply)) == NULL){ |
160 |
– |
logmessage(LOG_ERR, "strdup failed (%m)"); |
161 |
– |
return -1; |
162 |
– |
} |
354 |
|
|
355 |
< |
logmessage(LOG_DEBUG, "Sending FILELIST"); |
356 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILELIST"); |
357 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0)){ |
358 |
< |
logmessage(LOG_ERR, "Server error on FILELIST (%m)"); |
355 |
> |
*response=strdup(*response); |
356 |
> |
if (response==NULL) return -1; |
357 |
> |
|
358 |
> |
return 0; |
359 |
> |
} |
360 |
> |
|
361 |
> |
int ihost_getconfig(ihost_state_t *ihost_state){ |
362 |
> |
IHOST_SOCKET tcp_con; |
363 |
> |
char *response; |
364 |
> |
char *response_ptr; |
365 |
> |
|
366 |
> |
/* Keep these in case of a failure and so it can keep running on the old config */ |
367 |
> |
char *file_list=NULL; |
368 |
> |
char *last_modified=NULL; |
369 |
> |
char *host_fqdn=NULL; |
370 |
> |
char *host_ip=NULL; |
371 |
> |
int udp_update_time=0; |
372 |
> |
char *server_fqdn=NULL; |
373 |
> |
int server_udp_port=0; |
374 |
> |
time_t config_ttl=0; |
375 |
> |
|
376 |
> |
if((tcp_con=create_tcp_connection(ihost_state->filtermanager_host, ihost_state->filtermanager_port)) |
377 |
> |
#ifndef WIN32 |
378 |
> |
==NULL |
379 |
> |
#else |
380 |
> |
==0 |
381 |
> |
#endif |
382 |
> |
) { |
383 |
|
return -1; |
384 |
|
} |
170 |
– |
if((ihost_state->files_list=strdup(reply)) == NULL){ |
171 |
– |
logmessage(LOG_ERR, "strdup failed (%m)"); |
172 |
– |
return -1; |
173 |
– |
} |
385 |
|
|
386 |
< |
logmessage(LOG_DEBUG, "Sending FQDN"); |
387 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "FQDN"); |
388 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
389 |
< |
logmessage(LOG_ERR, "Server error on FQDN (%m)"); |
390 |
< |
return -1; |
386 |
> |
if(ihost_state->file_list!=NULL || ihost_state->last_modified!=NULL){ |
387 |
> |
#ifndef WIN32 |
388 |
> |
if(tcp_con==NULL) |
389 |
> |
#else |
390 |
> |
if(tcp_con==0) |
391 |
> |
#endif |
392 |
> |
{ |
393 |
> |
goto error; |
394 |
> |
} |
395 |
> |
|
396 |
> |
if((tcp_comm(tcp_con, "CHECKCONFIG", &response, "OK"))!=0){ |
397 |
> |
goto error; |
398 |
> |
} |
399 |
> |
|
400 |
> |
if((tcp_comm(tcp_con, ihost_state->file_list, &response, "OK"))!=0){ |
401 |
> |
goto error; |
402 |
> |
} |
403 |
> |
|
404 |
> |
if((tcp_comm(tcp_con, ihost_state->last_modified, &response, "OK"))==0){ |
405 |
> |
if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ |
406 |
> |
goto error; |
407 |
> |
} |
408 |
> |
CLOSESOCKET(tcp_con); |
409 |
> |
return 0; |
410 |
> |
}else{ |
411 |
> |
if((strcmp(response, "EXPIRED"))!=0){ |
412 |
> |
goto error; |
413 |
> |
} |
414 |
> |
} |
415 |
|
} |
416 |
< |
if((ihost_state->my_fqdn=strdup(reply)) == NULL){ |
417 |
< |
logmessage(LOG_ERR, "strdup failed (%m)"); |
418 |
< |
return -1; |
416 |
> |
|
417 |
> |
/* If we got to here, the config must of expired */ |
418 |
> |
|
419 |
> |
if((tcp_comm(tcp_con, "STARTCONFIG", &response, "OK"))!=0){ |
420 |
> |
goto error; |
421 |
|
} |
422 |
|
|
423 |
< |
logmessage(LOG_DEBUG, "Sending FQDN"); |
424 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "UDPUpdateTime"); |
188 |
< |
if(reply== NULL){ |
189 |
< |
logmessage(LOG_ERR, "Server error (%m)"); |
190 |
< |
return -1; |
423 |
> |
if((tcp_comm_strdup(tcp_con, "LASTMODIFIED", &response, NULL))!=0){ |
424 |
> |
goto error; |
425 |
|
} |
426 |
< |
if (strncasecmp(reply, "ERROR", 5) != 0){ |
427 |
< |
ihost_state->udp_update_time=atoi(reply); |
426 |
> |
last_modified=response; |
427 |
> |
|
428 |
> |
if((tcp_comm_strdup(tcp_con, "FILELIST", &response, NULL))!=0){ |
429 |
> |
goto error; |
430 |
|
} |
431 |
< |
|
432 |
< |
logmessage(LOG_DEBUG, "Sending TCPUpdateTime"); |
433 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "TCPUpdateTime"); |
434 |
< |
if(reply== NULL){ |
199 |
< |
logmessage(LOG_ERR, "Server error on TCPUpdateTime (%m)"); |
200 |
< |
return -1; |
431 |
> |
file_list=response; |
432 |
> |
|
433 |
> |
if((tcp_comm_strdup(tcp_con, "FQDN", &response, NULL))!=0){ |
434 |
> |
goto error; |
435 |
|
} |
436 |
< |
if (strncasecmp(reply, "ERROR", 5) != 0){ |
437 |
< |
ihost_state->tcp_update_time=atoi(reply); |
436 |
> |
host_fqdn=response; |
437 |
> |
|
438 |
> |
if((tcp_comm_strdup(tcp_con, "IP", &response, NULL))!=0){ |
439 |
> |
goto error; |
440 |
|
} |
441 |
+ |
host_ip=response; |
442 |
|
|
443 |
< |
logmessage(LOG_DEBUG, "Sending ENDCONFIG"); |
444 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDCONFIG"); |
208 |
< |
if(reply== NULL){ |
209 |
< |
logmessage(LOG_ERR, "Server error on ENDCONFIG (%m)"); |
210 |
< |
return -1; |
443 |
> |
if((tcp_comm(tcp_con, "UDPUpdateTime", &response, NULL))!=0){ |
444 |
> |
goto error; |
445 |
|
} |
446 |
+ |
udp_update_time=atoi(response); |
447 |
|
|
448 |
< |
logmessage(LOG_DEBUG, "Sending FILTER"); |
449 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "FILTER"); |
215 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5)==0)){ |
216 |
< |
logmessage(LOG_ERR, "Server error FILTER failed (%m)"); |
217 |
< |
return -1; |
448 |
> |
if((tcp_comm(tcp_con, "ConfigTTL", &response, NULL))!=0){ |
449 |
> |
goto error; |
450 |
|
} |
451 |
< |
reply_ptr=strchr(reply,';'); |
452 |
< |
if (reply_ptr==NULL){ |
453 |
< |
logmessage(LOG_ERR, "Incorrect data returned"); |
454 |
< |
return -1; |
451 |
> |
config_ttl=atoi(response); |
452 |
> |
|
453 |
> |
if((tcp_comm(tcp_con, "ENDCONFIG", &response, NULL))!=0){ |
454 |
> |
goto error; |
455 |
|
} |
456 |
< |
*reply_ptr='\0'; |
457 |
< |
if((ihost_state->server_fqdn=strdup(reply)) == NULL){ |
458 |
< |
logmessage(LOG_ERR, "strdup failed (%m)"); |
459 |
< |
return -1; |
456 |
> |
|
457 |
> |
if((tcp_comm(tcp_con, "FILTER", &response, NULL))!=0){ |
458 |
> |
goto error; |
459 |
> |
}else{ |
460 |
> |
response_ptr=strchr(response,';'); |
461 |
> |
if(response_ptr==NULL){ |
462 |
> |
log_msg(LOG_ERR, "Incorrect data sent by server"); |
463 |
> |
goto error; |
464 |
> |
} |
465 |
> |
*response_ptr='\0'; |
466 |
> |
server_fqdn=strdup(response); |
467 |
> |
if(server_fqdn==NULL){ |
468 |
> |
goto error; |
469 |
> |
} |
470 |
> |
response_ptr++; |
471 |
> |
if(response_ptr==NULL){ |
472 |
> |
log_msg(LOG_ERR, "Incorrect data sent by server"); |
473 |
> |
goto error; |
474 |
> |
} |
475 |
> |
|
476 |
> |
server_udp_port=atoi(response_ptr); |
477 |
> |
|
478 |
> |
if (server_udp_port==0){ |
479 |
> |
log_msg(LOG_ERR, "Incorrect data sent by server"); |
480 |
> |
goto error; |
481 |
> |
} |
482 |
|
} |
483 |
< |
reply=reply_ptr + 1; |
484 |
< |
reply_ptr=strchr(reply,';'); |
485 |
< |
if (reply_ptr==NULL){ |
232 |
< |
logmessage(LOG_ERR, "Incorrect data returned 2"); |
233 |
< |
return -1; |
483 |
> |
|
484 |
> |
if((tcp_comm(tcp_con, "END", &response, "OK"))!=0){ |
485 |
> |
goto error; |
486 |
|
} |
235 |
– |
*reply_ptr='\0'; |
236 |
– |
ihost_state->server_udp_port=atoi(reply); |
237 |
– |
reply=reply_ptr+1; |
238 |
– |
ihost_state->server_tcp_port=atoi(reply); |
239 |
– |
if ((ihost_state->server_tcp_port==0) || (ihost_state->server_udp_port==0)){ |
240 |
– |
logmessage(LOG_ERR, "Incorrect data returned 3 "); |
241 |
– |
return -1; |
242 |
– |
} |
487 |
|
|
488 |
< |
logmessage(LOG_DEBUG, "Sending END"); |
489 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "END"); |
490 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
491 |
< |
logmessage(LOG_ERR, "Server error on END (%m)"); |
492 |
< |
return -1; |
488 |
> |
CLOSESOCKET(tcp_con); |
489 |
> |
|
490 |
> |
/* We have the data we need, and its all been read correctly */ |
491 |
> |
|
492 |
> |
/* Free the old data before pointing them to the new data. m_free copes should |
493 |
> |
* this already be NULL */ |
494 |
> |
if(ihost_state->file_list!=NULL) free(ihost_state->file_list); |
495 |
> |
if(ihost_state->last_modified!=NULL) free(ihost_state->last_modified); |
496 |
> |
if(ihost_state->server_fqdn!=NULL) free(ihost_state->server_fqdn); |
497 |
> |
|
498 |
> |
if(ihost_state->preset_fqdn){ |
499 |
> |
if(host_fqdn != NULL) free(host_fqdn); |
500 |
> |
}else{ |
501 |
> |
if(ihost_state->host_fqdn!=NULL) free(ihost_state->host_fqdn); |
502 |
> |
ihost_state->host_fqdn=host_fqdn; |
503 |
|
} |
504 |
|
|
505 |
< |
if(fclose(fm_fd_r) !=0){ |
506 |
< |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
507 |
< |
return -1; |
505 |
> |
if(ihost_state->preset_ip){ |
506 |
> |
if(host_ip != NULL) free(host_ip); |
507 |
> |
}else{ |
508 |
> |
if(ihost_state->host_ip!=NULL) free(ihost_state->host_ip); |
509 |
> |
ihost_state->host_ip=host_ip; |
510 |
|
} |
255 |
– |
if(fclose(fm_fd_w) !=0){ |
256 |
– |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
257 |
– |
return -1; |
258 |
– |
} |
511 |
|
|
512 |
+ |
|
513 |
+ |
ihost_state->file_list=file_list; |
514 |
+ |
ihost_state->last_modified=last_modified; |
515 |
+ |
ihost_state->server_fqdn=server_fqdn; |
516 |
+ |
ihost_state->server_udp_port=server_udp_port; |
517 |
+ |
ihost_state->udp_update_time=udp_update_time; |
518 |
+ |
ihost_state->config_ttl=config_ttl; |
519 |
+ |
|
520 |
+ |
log_msg(LOG_DEBUG, "UDP Update time %d", udp_update_time); |
521 |
+ |
log_msg(LOG_DEBUG, "Configure ttl %d", config_ttl); |
522 |
+ |
|
523 |
|
return 0; |
524 |
+ |
|
525 |
+ |
error: |
526 |
+ |
|
527 |
+ |
if(file_list!=NULL) free(file_list); |
528 |
+ |
if(last_modified!=NULL) free(last_modified); |
529 |
+ |
if(host_fqdn!=NULL) free(host_fqdn); |
530 |
+ |
if(server_fqdn!=NULL) free(server_fqdn); |
531 |
+ |
if(host_ip!=NULL) free(host_ip); |
532 |
+ |
CLOSESOCKET(tcp_con); |
533 |
+ |
|
534 |
+ |
return -1; |
535 |
|
} |
536 |
|
|
537 |
< |
int heartbeat(ihost_state_t *ihost_state){ |
538 |
< |
struct sockaddr_in addr; |
539 |
< |
struct in_addr haddr; |
540 |
< |
int sd; |
541 |
< |
FILE *fm_fd_r, *fm_fd_w; |
542 |
< |
char *reply; |
543 |
< |
int exitcode=0; |
537 |
> |
int get_system_stats(int seq_no, ihost_state_t *ihost_state, char *xml, int size){ |
538 |
> |
char tmp[size]; |
539 |
> |
sg_cpu_percents *cpu_percent; |
540 |
> |
sg_mem_stats *mem_stats; |
541 |
> |
sg_load_stats *load_stats; |
542 |
> |
sg_user_stats *user_stats; |
543 |
> |
sg_swap_stats *swap_stats; |
544 |
> |
sg_host_info *general_stats; |
545 |
> |
sg_fs_stats *disk_stats; |
546 |
> |
sg_disk_io_stats *diskio_stats; |
547 |
> |
sg_process_count *process_stats; |
548 |
> |
sg_network_io_stats *network_stats; |
549 |
> |
sg_page_stats *page_stats; |
550 |
> |
int disk_entries=0; |
551 |
> |
int diskio_entries=0; |
552 |
> |
int network_entries=0; |
553 |
|
|
554 |
< |
logmessage(LOG_DEBUG, "Setting up configure socket to %s on port %d", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
555 |
< |
if ((sd = socket(AF_INET, SOCK_STREAM, PF_UNSPEC)) < 0) { |
556 |
< |
logmessage(LOG_ERR, "Can't create AF_INET socket (%m)"); |
274 |
< |
return -1; |
275 |
< |
} |
554 |
> |
int counter; |
555 |
> |
long long x; |
556 |
> |
long long y; |
557 |
|
|
558 |
< |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
559 |
< |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
560 |
< |
return -1; |
280 |
< |
} |
558 |
> |
/* Print start of the packet we want */ |
559 |
> |
snprintf(xml, size, "<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\">", \ |
560 |
> |
seq_no, ihost_state->host_fqdn, (long) time(NULL), ihost_state->host_ip); |
561 |
|
|
562 |
< |
memset(&addr, 0, sizeof addr); |
563 |
< |
addr.sin_family = AF_INET; |
564 |
< |
memcpy(&addr.sin_addr, &haddr, sizeof haddr); |
565 |
< |
addr.sin_port = htons(ihost_state->server_tcp_port); |
562 |
> |
/* Get cpu stats, check it is correct, then fill in its entry for the xml */ |
563 |
> |
if((cpu_percent=sg_get_cpu_percents())==NULL){ |
564 |
> |
log_msg(LOG_CRIT, "Failed to get cpu statistics: %s (%s)", |
565 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
566 |
> |
}else{ |
567 |
> |
snprintf(tmp, size, \ |
568 |
> |
"<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", \ |
569 |
> |
cpu_percent->user, \ |
570 |
> |
cpu_percent->kernel, \ |
571 |
> |
cpu_percent->idle, \ |
572 |
> |
cpu_percent->iowait, \ |
573 |
> |
cpu_percent->swap); |
574 |
|
|
575 |
< |
if (connect(sd, (struct sockaddr *)&addr, sizeof addr) != 0) { |
576 |
< |
logmessage(LOG_ERR, "Failed to connect to %s on port %d (%m)", ihost_state->server_fqdn, ihost_state->server_tcp_port); |
289 |
< |
return -1; |
290 |
< |
} |
575 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
576 |
> |
} |
577 |
|
|
292 |
– |
/* Need to open 2 files, one for reading one for writing, as it gets confused if we only use 1 :) */ |
293 |
– |
if ((fm_fd_r=fdopen(sd,"r")) == NULL){ |
294 |
– |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
295 |
– |
return -1; |
296 |
– |
} |
578 |
|
|
579 |
< |
if ((fm_fd_w=fdopen(dup(sd),"w")) == NULL){ |
299 |
< |
logmessage(LOG_ERR, "Failed to open stream (%m)"); |
300 |
< |
return -1; |
301 |
< |
} |
579 |
> |
/*Get mem stats, and fill in xml */ |
580 |
|
|
581 |
< |
logmessage(LOG_DEBUG, "Sending HEARTBEAT"); |
582 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "HEARTBEAT"); |
583 |
< |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
584 |
< |
logmessage(LOG_ERR, "Server error on HEARTBEAT"); |
585 |
< |
return -1; |
586 |
< |
} |
581 |
> |
if((mem_stats=sg_get_mem_stats())==NULL){ |
582 |
> |
log_msg(LOG_CRIT, "Failed to get memory statistics: %s (%s)", |
583 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
584 |
> |
}else{ |
585 |
> |
snprintf(tmp, size, \ |
586 |
> |
"<memory><total>"LLD"</total><free>"LLD"</free><used>"LLD"</used><cache>"LLD"</cache></memory>", \ |
587 |
> |
mem_stats->total, \ |
588 |
> |
mem_stats->free, \ |
589 |
> |
mem_stats->used, \ |
590 |
> |
mem_stats->cache); |
591 |
|
|
592 |
< |
logmessage(LOG_DEBUG, "Sending CONFIG"); |
593 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "CONFIG"); |
312 |
< |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
313 |
< |
logmessage(LOG_ERR, "Server error on CONFIG"); |
314 |
< |
return -1; |
315 |
< |
} |
592 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
593 |
> |
} |
594 |
|
|
317 |
– |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->files_list); |
318 |
– |
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->files_list); |
319 |
– |
if ((reply==NULL) || (strncasecmp(reply, "OK", 2) != 0) ) { |
320 |
– |
logmessage(LOG_ERR, "Server error on fileslist"); |
321 |
– |
return -1; |
322 |
– |
} |
595 |
|
|
596 |
< |
logmessage(LOG_DEBUG, "Sending %s", ihost_state->last_modified); |
597 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, ihost_state->last_modified); |
598 |
< |
if (reply==NULL) { |
599 |
< |
logmessage(LOG_ERR, "Server error NULL recieved on lastmodified"); |
600 |
< |
return -1; |
601 |
< |
} |
602 |
< |
if (strncasecmp(reply, "ERROR", 5) == 0){ |
603 |
< |
/* Means the config has changed */ |
604 |
< |
logmessage(LOG_INFO, "Recieved ERROR from server for a reconfigure required"); |
605 |
< |
exitcode=RECONFIGURE_RETURN_CODE; |
596 |
> |
/* Get load stats */ |
597 |
> |
|
598 |
> |
if((load_stats=sg_get_load_stats())==NULL){ |
599 |
> |
log_msg(LOG_CRIT, "Failed to get load statistics: %s (%s)", |
600 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
601 |
> |
}else{ |
602 |
> |
snprintf(tmp, size, \ |
603 |
> |
"<load><load1>%.2lf</load1><load5>%.2lf</load5><load15>%.2lf</load15></load>", \ |
604 |
> |
load_stats->min1, \ |
605 |
> |
load_stats->min5, \ |
606 |
> |
load_stats->min15); |
607 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
608 |
|
} |
609 |
|
|
336 |
– |
logmessage(LOG_DEBUG,"Sending KEY"); |
337 |
– |
reply=sock_comm(fm_fd_r, fm_fd_w, "KEY"); |
338 |
– |
if ((reply==NULL) || (strncasecmp(reply, "ERROR", 5) == 0) ) { |
339 |
– |
logmessage(LOG_ERR, "Server error on KEY"); |
340 |
– |
return -1; |
341 |
– |
} |
342 |
– |
if (ihost_state->key!=NULL) free(ihost_state->key); |
343 |
– |
|
344 |
– |
if((ihost_state->key=strdup(reply)) == NULL){ |
345 |
– |
logmessage(LOG_ERR, "strdup failed (%m)"); |
346 |
– |
return -1; |
347 |
– |
} |
610 |
|
|
611 |
< |
logmessage(LOG_DEBUG,"Sending ENDHEARTBEAT"); |
350 |
< |
reply=sock_comm(fm_fd_r, fm_fd_w, "ENDHEARTBEAT"); |
351 |
< |
if((reply== NULL) || (strncasecmp(reply, "ERROR", 5) ==0 )){ |
352 |
< |
logmessage(LOG_ERR, "Server error on ENDHEARTBEAT (%m)"); |
353 |
< |
return -1; |
354 |
< |
} |
611 |
> |
/* get user stats */ |
612 |
|
|
613 |
< |
fflush(fm_fd_r); |
614 |
< |
fflush(fm_fd_w); |
613 |
> |
if((user_stats=sg_get_user_stats())==NULL){ |
614 |
> |
log_msg(LOG_CRIT, "Failed to get user statistics: %s (%s)", |
615 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
616 |
> |
}else{ |
617 |
|
|
618 |
< |
if(fclose(fm_fd_r) !=0){ |
619 |
< |
logmessage(LOG_ERR, "Failed to close read FD (%m)"); |
620 |
< |
return -1; |
621 |
< |
} |
363 |
< |
if(fclose(fm_fd_w) !=0){ |
364 |
< |
logmessage(LOG_ERR, "Failed to close write FD (%m)"); |
365 |
< |
return -1; |
366 |
< |
} |
618 |
> |
snprintf(tmp, size, \ |
619 |
> |
"<users><list>%s</list><count>%d</count></users>", \ |
620 |
> |
user_stats->name_list, \ |
621 |
> |
user_stats->num_entries); |
622 |
|
|
623 |
< |
return exitcode; |
624 |
< |
} |
623 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
624 |
> |
} |
625 |
|
|
371 |
– |
char *stat_grab(ihost_state_t *ihost_state, int counter){ |
372 |
– |
#define NUM_STATS 9 |
373 |
– |
char *stats[NUM_STATS]; |
374 |
– |
char *xml_data=NULL; |
375 |
– |
char *xml_data_p; |
376 |
– |
int x=0; |
626 |
|
|
627 |
< |
logmessage(LOG_DEBUG,"get_cpu_stats"); |
379 |
< |
stats[0]=get_cpu_stats(); |
380 |
< |
logmessage(LOG_DEBUG,"get_disk_stats"); |
381 |
< |
stats[1]=get_disk_stats(); |
382 |
< |
logmessage(LOG_DEBUG,"get_load_stats"); |
383 |
< |
stats[2]=get_load_stats(); |
384 |
< |
logmessage(LOG_DEBUG,"get_memory_stats"); |
385 |
< |
stats[3]=get_memory_stats(); |
386 |
< |
logmessage(LOG_DEBUG,"get_os_info"); |
387 |
< |
stats[4]=get_os_info(); |
388 |
< |
logmessage(LOG_DEBUG,"get_page_stats"); |
389 |
< |
stats[5]=get_page_stats(); |
390 |
< |
logmessage(LOG_DEBUG,"get_process_stats"); |
391 |
< |
stats[6]=get_process_stats(); |
392 |
< |
logmessage(LOG_DEBUG,"get_swap_stats"); |
393 |
< |
stats[7]=get_swap_stats(); |
394 |
< |
logmessage(LOG_DEBUG,"get_user_stats"); |
395 |
< |
stats[8]=get_user_stats(); |
627 |
> |
/* swap stats */ |
628 |
|
|
629 |
< |
for(;x<NUM_STATS;x++){ |
630 |
< |
if(stats[x]==NULL){ |
631 |
< |
logmessage(LOG_ERR,"Function returned NULL"); |
632 |
< |
return NULL; |
629 |
> |
if((swap_stats=sg_get_swap_stats())==NULL){ |
630 |
> |
log_msg(LOG_CRIT, "Failed to get swap statistics: %s (%s)", |
631 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
632 |
> |
}else{ |
633 |
> |
snprintf(tmp, size, \ |
634 |
> |
"<swap><total>"LLD"</total><used>"LLD"</used><free>"LLD"</free></swap>",\ |
635 |
> |
swap_stats->total, \ |
636 |
> |
swap_stats->used, \ |
637 |
> |
swap_stats->free); |
638 |
> |
|
639 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
640 |
> |
} |
641 |
> |
|
642 |
> |
|
643 |
> |
/* general stats */ |
644 |
> |
|
645 |
> |
if((general_stats=sg_get_host_info())==NULL){ |
646 |
> |
log_msg(LOG_CRIT, "Failed to get host info statistics: %s (%s)", |
647 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
648 |
> |
}else{ |
649 |
> |
snprintf(tmp, size, \ |
650 |
> |
"<os><name>%s</name><release>%s</release><version>%s</version><sysname>%s</sysname><platform>%s</platform><uptime>%ld</uptime></os>", \ |
651 |
> |
general_stats->os_name, \ |
652 |
> |
general_stats->os_release, \ |
653 |
> |
general_stats->os_version, \ |
654 |
> |
general_stats->hostname, \ |
655 |
> |
general_stats->platform, \ |
656 |
> |
(long)general_stats->uptime); |
657 |
> |
|
658 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
659 |
> |
|
660 |
> |
} |
661 |
> |
|
662 |
> |
|
663 |
> |
/* process stats */ |
664 |
> |
|
665 |
> |
if((process_stats=sg_get_process_count())==NULL){ |
666 |
> |
log_msg(LOG_CRIT, "Failed to get process statistics: %s (%s)", |
667 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
668 |
> |
}else{ |
669 |
> |
snprintf(tmp, size, \ |
670 |
> |
"<processes><sleeping>%d</sleeping><cpu>%d</cpu><zombie>%d</zombie><stopped>%d</stopped><total>%d</total></processes>",\ |
671 |
> |
process_stats->sleeping, \ |
672 |
> |
process_stats->running, \ |
673 |
> |
process_stats->zombie, \ |
674 |
> |
process_stats->stopped, \ |
675 |
> |
process_stats->total); |
676 |
> |
|
677 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
678 |
> |
|
679 |
> |
} |
680 |
> |
|
681 |
> |
|
682 |
> |
/* Get paging stats */ |
683 |
> |
|
684 |
> |
if((page_stats=sg_get_page_stats_diff())==NULL){ |
685 |
> |
log_msg(LOG_CRIT, "Failed to get paging statistics: %s (%s)", |
686 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
687 |
> |
}else{ |
688 |
> |
if(page_stats->systime!=0){ |
689 |
> |
x=page_stats->pages_pagein / page_stats->systime; |
690 |
> |
y=page_stats->pages_pageout / page_stats->systime; |
691 |
> |
}else{ |
692 |
> |
x=page_stats->pages_pagein; |
693 |
> |
y=page_stats->pages_pageout; |
694 |
|
} |
695 |
< |
if(xml_data==NULL){ |
696 |
< |
if((xml_data=strf("%s", stats[x])) == NULL){ |
697 |
< |
logmessage(LOG_ERR, "str failed (%m)"); |
698 |
< |
return NULL; |
695 |
> |
snprintf(tmp, size, \ |
696 |
> |
"<pages><pageins>"LLD"</pageins><pageouts>"LLD"</pageouts></pages>", \ |
697 |
> |
x, \ |
698 |
> |
y); |
699 |
> |
|
700 |
> |
if(strlcat(xml, tmp, size) >= size) goto too_big_error; |
701 |
> |
} |
702 |
> |
|
703 |
> |
|
704 |
> |
/* get diskio stats */ |
705 |
> |
|
706 |
> |
if((diskio_stats=sg_get_disk_io_stats_diff(&diskio_entries))==NULL){ |
707 |
> |
log_msg(LOG_CRIT, "Failed to get disk io statistics: %s (%s)", |
708 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
709 |
> |
}else{ |
710 |
> |
strlcat(xml, "<diskio>", size); |
711 |
> |
for(counter=0;counter<diskio_entries;counter++){ |
712 |
> |
|
713 |
> |
if(diskio_stats->systime!=0){ |
714 |
> |
x=diskio_stats->read_bytes / diskio_stats->systime; |
715 |
> |
y=diskio_stats->write_bytes / diskio_stats->systime; |
716 |
> |
}else{ |
717 |
> |
x=diskio_stats->read_bytes; |
718 |
> |
y=diskio_stats->write_bytes; |
719 |
|
} |
720 |
< |
}else{ |
721 |
< |
xml_data_p=xml_data; |
722 |
< |
if((xml_data=strf("%s%s", xml_data, stats[x])) == NULL){ |
723 |
< |
logmessage(LOG_ERR, "str failed (%m)"); |
724 |
< |
return NULL; |
720 |
> |
|
721 |
> |
snprintf(tmp, size, \ |
722 |
> |
"<p%d name=\"%s\" rbytes=\""LLD"\" wbytes=\""LLD"\"></p%d>", \ |
723 |
> |
counter, \ |
724 |
> |
diskio_stats->disk_name, \ |
725 |
> |
x, \ |
726 |
> |
y, \ |
727 |
> |
counter); |
728 |
> |
|
729 |
> |
strlcat(xml, tmp, size); |
730 |
> |
diskio_stats++; |
731 |
> |
} |
732 |
> |
|
733 |
> |
if(strlcat(xml, "</diskio>", size) >= size) goto too_big_error; |
734 |
> |
|
735 |
> |
} |
736 |
> |
|
737 |
> |
|
738 |
> |
/* get networks stats */ |
739 |
> |
|
740 |
> |
if((network_stats=sg_get_network_io_stats_diff(&network_entries))==NULL){ |
741 |
> |
log_msg(LOG_CRIT, "Failed to get network io statistics: %s (%s)", |
742 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
743 |
> |
}else{ |
744 |
> |
strlcat(xml, "<net>", size); |
745 |
> |
for(counter=0;counter<network_entries;counter++){ |
746 |
> |
if(network_stats->systime!=0){ |
747 |
> |
x=network_stats->rx / network_stats->systime; |
748 |
> |
y=network_stats->tx / network_stats->systime; |
749 |
> |
}else{ |
750 |
> |
x=network_stats->rx; |
751 |
> |
y=network_stats->tx; |
752 |
|
} |
753 |
< |
free(xml_data_p); |
753 |
> |
|
754 |
> |
snprintf(tmp, size, \ |
755 |
> |
"<p%d name=\"%s\" rx=\""LLD"\" tx=\""LLD"\"></p%d>", \ |
756 |
> |
counter, \ |
757 |
> |
network_stats->interface_name, \ |
758 |
> |
x, \ |
759 |
> |
y, \ |
760 |
> |
counter); |
761 |
> |
|
762 |
> |
strlcat(xml, tmp, size); |
763 |
> |
network_stats++; |
764 |
|
} |
765 |
< |
free(stats[x]); |
765 |
> |
|
766 |
> |
if(strlcat(xml, "</net>", size) >= size) goto too_big_error; |
767 |
> |
|
768 |
|
} |
417 |
– |
xml_data_p=xml_data; |
418 |
– |
xml_data=strf("<packet seq_no=\"%d\" machine_name=\"%s\" date=\"%ld\" type=\"data\" ip=\"%s\" key=\"%s\">%s</packet>", counter, ihost_state->my_fqdn, time(NULL), ihost_state->my_ip, ihost_state->key, xml_data); |
419 |
– |
free(xml_data_p); |
769 |
|
|
770 |
< |
logmessage(LOG_DEBUG,"Generated XML Data of : %s", xml_data); |
771 |
< |
return xml_data; |
770 |
> |
|
771 |
> |
/* get disk stats */ |
772 |
> |
|
773 |
> |
if((disk_stats=sg_get_fs_stats(&disk_entries))==NULL){ |
774 |
> |
log_msg(LOG_CRIT, "Failed to get fs statistics: %s (%s)", |
775 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
776 |
> |
}else{ |
777 |
> |
strlcat(xml, "<disk>", size); |
778 |
> |
for(counter=0;counter<disk_entries;counter++){ |
779 |
> |
if(strcmp(disk_stats->fs_type, "nfs") == 0) continue; |
780 |
> |
snprintf(tmp, size, \ |
781 |
> |
"<p%d name=\"%s\" mount=\"%s\" fstype=\"%s\" total=\""LLD"\" used=\""LLD"\" avail=\""LLD"\" totalinodes=\""LLD"\" usedinodes=\""LLD"\" freeinodes=\""LLD"\"></p%d>", \ |
782 |
> |
counter, \ |
783 |
> |
disk_stats->device_name, \ |
784 |
> |
disk_stats->mnt_point, \ |
785 |
> |
disk_stats->fs_type, \ |
786 |
> |
disk_stats->size, \ |
787 |
> |
disk_stats->used, \ |
788 |
> |
disk_stats->avail, \ |
789 |
> |
disk_stats->total_inodes, \ |
790 |
> |
disk_stats->used_inodes, \ |
791 |
> |
disk_stats->free_inodes, \ |
792 |
> |
counter); |
793 |
> |
|
794 |
> |
strlcat(xml, tmp, size); |
795 |
> |
|
796 |
> |
disk_stats++; |
797 |
> |
} |
798 |
> |
|
799 |
> |
if(strlcat(xml, "</disk>", size) >= size) goto too_big_error; |
800 |
> |
|
801 |
> |
} |
802 |
> |
|
803 |
> |
|
804 |
> |
if(strlcat(xml, "</packet>", size) >= size) goto too_big_error; |
805 |
> |
|
806 |
> |
/*If we got to here, it should of all been filled in nicely now */ |
807 |
> |
return 0; |
808 |
> |
|
809 |
> |
too_big_error: |
810 |
> |
log_msg(LOG_ERR, "UDP Packet is too large. Throwing away the packet"); |
811 |
> |
return -1; |
812 |
|
} |
813 |
|
|
425 |
– |
int send_stats(ihost_state_t *ihost_state, char *data_stream){ |
426 |
– |
struct sockaddr_in addr; |
427 |
– |
struct in_addr haddr; |
814 |
|
|
429 |
– |
int sd; |
430 |
– |
size_t len; |
815 |
|
|
816 |
< |
len=strlen(data_stream); |
817 |
< |
if(len>UDP_MAX_PACKET_SIZE){ |
818 |
< |
logmessage(LOG_ERR, "Too big to send to server. Please reconfigure client and server and recompile"); |
819 |
< |
exit(1); |
816 |
> |
/* WIN32 NT service stuff */ |
817 |
> |
#ifdef WIN32 |
818 |
> |
void service_stopped() { |
819 |
> |
if(run_as_service) { |
820 |
> |
service_status.dwCurrentState = SERVICE_STOPPED; |
821 |
> |
service_status.dwWin32ExitCode = errno; |
822 |
> |
service_status.dwServiceSpecificExitCode = 0; |
823 |
> |
|
824 |
> |
SetServiceStatus(hstatus, &service_status); |
825 |
|
} |
826 |
< |
logmessage(LOG_DEBUG,"Resolving IP of server"); |
827 |
< |
if (get_host_addr(ihost_state->server_fqdn, &haddr) != 0){ |
828 |
< |
logmessage(LOG_ERR, "Failed to resolve address %s (%m)", ihost_state->server_fqdn); |
829 |
< |
return -1; |
830 |
< |
} |
831 |
< |
logmessage(LOG_DEBUG,"Creating UDP connection to %s on %d",ihost_state->server_fqdn, ihost_state->server_udp_port); |
832 |
< |
if((sd=socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0){ |
833 |
< |
logmessage(LOG_ERR, "failed to create UDP socket (%m)"); |
834 |
< |
return -1; |
826 |
> |
} |
827 |
> |
|
828 |
> |
void control_handler(DWORD request) { |
829 |
> |
switch(request) { |
830 |
> |
case SERVICE_CONTROL_STOP: |
831 |
> |
case SERVICE_CONTROL_SHUTDOWN: |
832 |
> |
service_status.dwWin32ExitCode = 0; |
833 |
> |
service_status.dwCurrentState = SERVICE_STOP_PENDING; |
834 |
> |
break; |
835 |
> |
default: |
836 |
> |
break; |
837 |
|
} |
838 |
|
|
839 |
< |
memset(&addr, 0, sizeof(addr)); |
840 |
< |
addr.sin_family=AF_INET; |
450 |
< |
memcpy((char *)&addr.sin_addr, &haddr, sizeof haddr); |
451 |
< |
addr.sin_port = htons(ihost_state->server_udp_port); |
839 |
> |
SetServiceStatus (hstatus, &service_status); |
840 |
> |
} |
841 |
|
|
842 |
< |
logmessage(LOG_INFO,"Sending packet : %s", data_stream); |
843 |
< |
if((sendto(sd, data_stream, len, 0, (struct sockaddr *) &addr, sizeof(addr))) != len){ |
844 |
< |
logmessage(LOG_ERR, "Send the wrong number of bytes (%m)"); |
845 |
< |
return -1; |
842 |
> |
/* attempt to tell service manager to start our service. |
843 |
> |
* return 0 on error, 1 on ok |
844 |
> |
*/ |
845 |
> |
int start_service() { |
846 |
> |
SC_HANDLE scm; |
847 |
> |
SC_HANDLE service; |
848 |
> |
|
849 |
> |
// Open SCM |
850 |
> |
if((scm = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT)) == NULL) { |
851 |
> |
log_msg(LOG_CRIT, "Failed to open Service Control Manager"); |
852 |
> |
return 0; |
853 |
|
} |
854 |
+ |
// Locate the service |
855 |
+ |
if((service = OpenService(scm, SERVICENAME, SERVICE_START)) == NULL) { |
856 |
+ |
log_msg(LOG_CRIT, "Unable to open the service"); |
857 |
+ |
return 0; |
858 |
+ |
} |
859 |
+ |
// Start the service |
860 |
+ |
if(StartService(service, 0, NULL) == 0) { |
861 |
+ |
log_msg(LOG_CRIT, "Unable to start the service"); |
862 |
+ |
return 0; |
863 |
+ |
} |
864 |
+ |
return 1; |
865 |
+ |
} |
866 |
|
|
867 |
< |
close(sd); |
867 |
> |
int stop_service() { |
868 |
> |
SC_HANDLE scm; |
869 |
> |
SC_HANDLE service; |
870 |
> |
SERVICE_STATUS status; |
871 |
|
|
872 |
< |
return 0; |
872 |
> |
// Open SCM |
873 |
> |
if((scm = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT)) == NULL) { |
874 |
> |
log_msg(LOG_CRIT, "Failed to open Service Control Manager"); |
875 |
> |
return 0; |
876 |
> |
} |
877 |
> |
// Locate service |
878 |
> |
if((service = OpenService(scm, SERVICENAME, SERVICE_STOP)) == NULL) { |
879 |
> |
log_msg(LOG_CRIT, "Unable to open the service"); |
880 |
> |
return 0; |
881 |
> |
} |
882 |
> |
// Stop the service |
883 |
> |
if(!ControlService(service, SERVICE_CONTROL_STOP, &status)) { |
884 |
> |
log_msg(LOG_CRIT, "Unable to stop the service"); |
885 |
> |
return 0; |
886 |
> |
} |
887 |
> |
return 1; |
888 |
|
} |
889 |
|
|
890 |
< |
void usage(char *progname){ |
891 |
< |
fprintf(stderr, "Usage %s [options] server port\n", progname); |
892 |
< |
fprintf(stderr, "Options\n"); |
893 |
< |
fprintf(stderr, " -v Verbose, the more v flags the more verbose, eg -vv\n"); |
894 |
< |
fprintf(stderr, " -d Daemon mode, self backgrounding\n"); |
895 |
< |
fprintf(stderr, " -s Send errors to syslog\n"); |
896 |
< |
fprintf(stderr, " -V Print version number\n"); |
471 |
< |
fprintf(stderr, " -h Prints this help page\n"); |
472 |
< |
exit(1); |
473 |
< |
} |
890 |
> |
int register_service(int argc, char **argv) { |
891 |
> |
char path[MAX_PATH]; |
892 |
> |
int cmdline_len; |
893 |
> |
int i; |
894 |
> |
char *cmdline; |
895 |
> |
char *tmp; |
896 |
> |
SC_HANDLE scm; |
897 |
|
|
898 |
< |
int main(int argc, char **argv){ |
899 |
< |
ihost_state_t ihost_state; |
900 |
< |
int heartbeat_exit; |
901 |
< |
int counter=0; |
479 |
< |
long udp_time=0, tcp_time=0, stat_grab_time=0, cur_time=0; |
480 |
< |
int sleep_delay=0; |
481 |
< |
char *xml_stats; |
482 |
< |
pid_t pid; |
483 |
< |
int cmdopt; |
484 |
< |
extern int optind; |
485 |
< |
int verbose=0, daemon=0; |
486 |
< |
extern int syslog_logging; |
487 |
< |
extern int log_level; |
488 |
< |
extern int cur_level; |
489 |
< |
FILE *f; |
898 |
> |
if(!GetModuleFileName(NULL, path, MAX_PATH)) { |
899 |
> |
log_msg(LOG_CRIT, "GetModuleFileName failed"); |
900 |
> |
return 0; |
901 |
> |
} |
902 |
|
|
903 |
< |
log_level=1; |
904 |
< |
cur_level=1; |
905 |
< |
syslog_logging=0; |
903 |
> |
// Calculate command line length |
904 |
> |
// 14 = 10 for " -w service", 2x quote around path and ending \0 |
905 |
> |
cmdline_len = strlen(path) + 14; |
906 |
> |
for (i=0; i<argc; i++) { |
907 |
> |
// +1 for space prefix |
908 |
> |
cmdline_len += strlen(argv[i]) +1; |
909 |
> |
} |
910 |
|
|
911 |
< |
errf_set_ofunc(log_errors); |
912 |
< |
/* NULL'ify so i can tell if i need to free it or not */ |
913 |
< |
ihost_state.fm_host=NULL; |
914 |
< |
ihost_state.my_fqdn=NULL; |
915 |
< |
ihost_state.server_fqdn=NULL; |
916 |
< |
ihost_state.last_modified=NULL; |
917 |
< |
ihost_state.files_list=NULL; |
918 |
< |
ihost_state.key=NULL; |
911 |
> |
cmdline = malloc(cmdline_len); |
912 |
> |
if(cmdline == NULL) { |
913 |
> |
log_msg(LOG_CRIT, "out of memory"); |
914 |
> |
return 0; |
915 |
> |
} |
916 |
> |
tmp = malloc(cmdline_len); |
917 |
> |
if(tmp == NULL) { |
918 |
> |
log_msg(LOG_CRIT, "out of memory"); |
919 |
> |
free(cmdline); |
920 |
> |
return 0; |
921 |
> |
} |
922 |
|
|
923 |
< |
errf_set_progname(argv[0]); |
924 |
< |
|
923 |
> |
// Compile up the final command line call |
924 |
> |
snprintf(cmdline, cmdline_len, "\"%s\" -w service", path); |
925 |
> |
for (i=0; i<argc; i++) { |
926 |
> |
snprintf(tmp, cmdline_len, " %s", argv[i]); |
927 |
> |
strcat(cmdline, tmp); |
928 |
> |
} |
929 |
> |
free(tmp); |
930 |
|
|
931 |
< |
while((cmdopt=getopt(argc, argv, "vdshV")) != -1){ |
932 |
< |
switch(cmdopt){ |
933 |
< |
case 'v': |
934 |
< |
verbose++; |
935 |
< |
break; |
512 |
< |
|
513 |
< |
case 'd': |
514 |
< |
/* Force syslog logging since stderr will be closed in this case */ |
515 |
< |
syslog_logging=1; |
516 |
< |
daemon=1; |
517 |
< |
break; |
518 |
< |
|
519 |
< |
case 's': |
520 |
< |
syslog_logging=1; |
521 |
< |
break; |
522 |
< |
|
523 |
< |
case 'h': |
524 |
< |
usage(argv[0]); |
525 |
< |
break; |
931 |
> |
// Open SCM |
932 |
> |
if((scm = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) == NULL) { |
933 |
> |
log_msg(LOG_CRIT, "Unable to open Service Control Manager"); |
934 |
> |
return 0; |
935 |
> |
} |
936 |
|
|
937 |
< |
case 'V': |
938 |
< |
errf("%s version %f",argv[0], versionNo); |
939 |
< |
break; |
937 |
> |
// Add the service |
938 |
> |
if (CreateService(scm, SERVICENAME, SERVICENAME, |
939 |
> |
SC_MANAGER_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, |
940 |
> |
SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, cmdline, NULL, |
941 |
> |
NULL, NULL, NULL, NULL) == NULL) { |
942 |
> |
log_msg(LOG_CRIT, "Unable to create service"); |
943 |
> |
return 0; |
944 |
> |
} |
945 |
|
|
946 |
< |
default: |
947 |
< |
usage(argv[0]); |
948 |
< |
exit(1); |
949 |
< |
} |
946 |
> |
return 1; |
947 |
> |
} |
948 |
> |
|
949 |
> |
int unregister_service() { |
950 |
> |
SC_HANDLE scm; |
951 |
> |
SC_HANDLE service; |
952 |
> |
|
953 |
> |
// Open the SCM |
954 |
> |
if((scm = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE)) ==NULL) { |
955 |
> |
log_msg(LOG_CRIT, "Unable to open Service Control Manager"); |
956 |
> |
return 0; |
957 |
|
} |
958 |
+ |
// Get the service |
959 |
+ |
if((service = OpenService(scm, SERVICENAME, SC_MANAGER_ALL_ACCESS)) == NULL) { |
960 |
+ |
log_msg(LOG_CRIT, "Unable to locate the service"); |
961 |
+ |
return 0; |
962 |
+ |
} |
963 |
+ |
// Delete service |
964 |
+ |
if(DeleteService(service) == 0) { |
965 |
+ |
log_msg(LOG_CRIT, "Unable to remove the service"); |
966 |
+ |
return 0; |
967 |
+ |
} |
968 |
+ |
return 1; |
969 |
+ |
} |
970 |
+ |
#endif |
971 |
|
|
972 |
< |
if(argc!=optind+2){ |
973 |
< |
usage(argv[0]); |
972 |
> |
|
973 |
> |
|
974 |
> |
void usage(char *progname){ |
975 |
> |
const char *usage; |
976 |
> |
#ifdef WIN32 |
977 |
> |
usage = "Usage %s [-w option] [-v[v]] [-n name] [-i ip] [-s server] [-p port] [-V] [-h]\n\n"; |
978 |
> |
#else |
979 |
> |
usage = "Usage %s [-v[v]] [-f] [-n name] [-i ip] [-s server] [-p port] [-V] [-h]\n\n"; |
980 |
> |
#endif |
981 |
> |
fprintf(stderr, usage, progname); |
982 |
> |
fprintf(stderr, " -v Verbose mode, -vv would make even more verbose\n"); |
983 |
> |
#ifndef WIN32 |
984 |
> |
fprintf(stderr, " -f Foreground mode, print errors to stderr\n"); |
985 |
> |
#else |
986 |
> |
fprintf(stderr, " -w Windows Service options:\n"); |
987 |
> |
fprintf(stderr, " start - start the service\n"); |
988 |
> |
fprintf(stderr, " stop - stop the service\n"); |
989 |
> |
fprintf(stderr, " register - register the service\n"); |
990 |
> |
fprintf(stderr, " unregister - unregister the service\n"); |
991 |
> |
/* service - run as a service, only called by windows service */ |
992 |
> |
#endif |
993 |
> |
fprintf(stderr, " -n Set the machine name to be reported as\n"); |
994 |
> |
fprintf(stderr, " -i Set the IP to be reported as\n"); |
995 |
> |
fprintf(stderr, " -s Specifies the i-scream server to connect to\n"); |
996 |
> |
fprintf(stderr, " default: %s\n", DEF_SERVER_NAME); |
997 |
> |
fprintf(stderr, " -p Specifies the i-scream server port\n"); |
998 |
> |
fprintf(stderr, " default: %d\n", DEF_SERVER_PORT); |
999 |
> |
fprintf(stderr, " -V Print version number\n"); |
1000 |
> |
fprintf(stderr, " -h Prints this help page\n"); |
1001 |
> |
fprintf(stderr, "\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT); |
1002 |
> |
exit(1); |
1003 |
> |
} |
1004 |
> |
|
1005 |
> |
int run_ihost(){ |
1006 |
> |
|
1007 |
> |
udp_sockinfo_t udp_sockinfo; |
1008 |
> |
|
1009 |
> |
pid_t pid; |
1010 |
> |
FILE *f; |
1011 |
> |
int packet_num=0; |
1012 |
> |
int len; |
1013 |
> |
|
1014 |
> |
char packet[MAX_UDP_PACKET_SIZE]; |
1015 |
> |
|
1016 |
> |
time_t cur_time, sleep_delay, udp_time=0, config_time=0; |
1017 |
> |
#ifdef WIN32 |
1018 |
> |
int tries=0; |
1019 |
> |
WSADATA wsaData; |
1020 |
> |
|
1021 |
> |
if(WSAStartup(MAKEWORD(2,2), &wsaData) != NO_ERROR) { |
1022 |
> |
log_msg(LOG_CRIT, "Failed to init a winsock"); |
1023 |
> |
service_stopped(); |
1024 |
|
exit(1); |
1025 |
|
} |
1026 |
< |
ihost_state.fm_host=argv[optind]; |
1027 |
< |
ihost_state.fm_port=atoi(argv[optind+1]); |
1028 |
< |
if(ihost_state.fm_port==0){ |
1029 |
< |
errf("Invalid port number"); |
1030 |
< |
usage(argv[0]); |
1026 |
> |
#endif |
1027 |
> |
|
1028 |
> |
if(gethostbyname(ihost_state.filtermanager_host)==NULL){ |
1029 |
> |
printf("%s\n", ihost_state.filtermanager_host); |
1030 |
> |
log_msg(LOG_CRIT, "Failed to lookup hostname. Please check settings"); |
1031 |
> |
goto out; |
1032 |
|
} |
1033 |
+ |
if(ihost_state.filtermanager_port==0){ |
1034 |
+ |
log_msg(LOG_ERR, "Invalid port number"); |
1035 |
+ |
goto out; |
1036 |
+ |
} |
1037 |
|
|
1038 |
< |
if(daemon==1){ |
1038 |
> |
#ifndef WIN32 |
1039 |
> |
if(ihost_config.daemon){ |
1040 |
|
pid=fork(); |
1041 |
|
if(pid==-1){ |
1042 |
< |
errf("Fork failed, can't background. Exiting"); |
1042 |
> |
log_msg(LOG_CRIT, "Failed to background exiting"); |
1043 |
|
exit(1); |
1044 |
|
}else if(pid!=0){ |
1045 |
|
/* Parent process */ |
1047 |
|
} |
1048 |
|
/* We should now be in the background*/ |
1049 |
|
if(setsid()==-1){ |
1050 |
< |
errf("setsid failed (%m)"); |
1050 |
> |
log_msg(LOG_CRIT, "setsid failed"); |
1051 |
|
exit(1); |
1052 |
|
} |
1053 |
+ |
|
1054 |
+ |
if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){ |
1055 |
+ |
ihost_config.log=stderr; |
1056 |
+ |
log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE); |
1057 |
+ |
exit(1); |
1058 |
+ |
} |
1059 |
+ |
|
1060 |
|
fclose(stdin); |
1061 |
|
fclose(stdout); |
1062 |
|
fclose(stderr); |
1063 |
+ |
|
1064 |
|
} |
1065 |
+ |
#endif |
1066 |
|
|
1067 |
< |
if(syslog_logging==1){ |
1068 |
< |
openlog(errf_get_progname(),0,LOG_ERR); |
1069 |
< |
setlogmask(LOG_UPTO(LOG_DEBUG)); |
1067 |
> |
log_msg(LOG_INFO, "Starting ihost..."); |
1068 |
> |
|
1069 |
> |
log_msg(LOG_DEBUG, "Running sg_init()"); |
1070 |
> |
if(sg_init()){ |
1071 |
> |
log_msg(LOG_CRIT, "sg_init failed: %s (%s)", |
1072 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
1073 |
> |
goto out; |
1074 |
|
} |
1075 |
< |
|
1076 |
< |
switch(verbose){ |
1077 |
< |
case 0: |
574 |
< |
/* Critical errors + */ |
575 |
< |
log_level=LOG_ERR; |
576 |
< |
break; |
577 |
< |
case 1: |
578 |
< |
/* Recoverable errors */ |
579 |
< |
log_level=LOG_WARNING; |
580 |
< |
break; |
581 |
< |
case 2: |
582 |
< |
/* Print stuff like the XML packets */ |
583 |
< |
log_level=LOG_INFO; |
584 |
< |
break; |
585 |
< |
default: |
586 |
< |
/* Must have lots of v's */ |
587 |
< |
/* Print out everything its doing */ |
588 |
< |
log_level=LOG_DEBUG; |
589 |
< |
break; |
1075 |
> |
if(sg_snapshot()) { |
1076 |
> |
log_msg(LOG_ERR, "sg_snapshot failed: %s (%s)", |
1077 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
1078 |
|
} |
1079 |
|
|
1080 |
< |
logmessage(LOG_DEBUG,"Writing PID FILE"); |
1080 |
> |
#ifndef WIN32 |
1081 |
> |
log_msg(LOG_DEBUG,"Writing PID FILE"); |
1082 |
> |
|
1083 |
|
pid=getpid(); |
1084 |
+ |
|
1085 |
|
if((f=fopen(PID_FILE,"w")) == NULL){ |
1086 |
< |
logmessage(LOG_WARNING, "Failed to write PID file"); |
1086 |
> |
log_msg(LOG_CRIT, "Failed to write PID file"); |
1087 |
|
}else{ |
1088 |
< |
if((fprintf(f,"%d",(int)pid)) != sizeof(pid)){ |
1089 |
< |
logmessage(LOG_WARNING, "Failed to write PID file"); |
1088 |
> |
if((fprintf(f,"%d",(int)pid)) <= 0 ){ |
1089 |
> |
log_msg(LOG_CRIT, "Failed to write PID file"); |
1090 |
|
} |
1091 |
|
if((fclose(f))!=0){ |
1092 |
< |
logmessage(LOG_ERR, "failed to close PID file"); |
602 |
< |
exit(1); |
1092 |
> |
log_msg(LOG_CRIT, "failed to close PID file"); |
1093 |
|
} |
1094 |
|
} |
1095 |
+ |
#endif |
1096 |
|
|
1097 |
< |
if(ihost_configure(&ihost_state)!=0){ |
1098 |
< |
logmessage(LOG_ERR,"configure failed"); |
1099 |
< |
/* Ok, ideally we prob should have 2 copies of the structure and carry on if this |
1100 |
< |
happens.. But we dont :) (at the moment) */ |
1101 |
< |
exit(1); |
1097 |
> |
/* Get the initial config from the filter manager. Should this fail, |
1098 |
> |
* wait, and then try again. */ |
1099 |
> |
/* Win32 - for a max of one minute */ |
1100 |
> |
|
1101 |
> |
sg_get_disk_io_stats_diff(&packet_num); |
1102 |
> |
packet_num=0; |
1103 |
> |
|
1104 |
> |
while(ihost_getconfig(&ihost_state)!=0 |
1105 |
> |
#ifdef WIN32 |
1106 |
> |
&& (tries<6) |
1107 |
> |
#endif |
1108 |
> |
){ |
1109 |
> |
log_msg(LOG_ERR, "Failed to get ihost config"); |
1110 |
> |
sleep(SLEEP); |
1111 |
> |
#ifdef WIN32 |
1112 |
> |
tries++; |
1113 |
> |
#endif |
1114 |
|
} |
1115 |
|
|
1116 |
< |
for(;;){ |
1116 |
> |
#ifdef WIN32 |
1117 |
> |
if (tries == 6) { |
1118 |
> |
log_msg(LOG_ERR, "Giving up"); |
1119 |
> |
goto out; |
1120 |
> |
} |
1121 |
> |
tries = 0; |
1122 |
> |
#endif |
1123 |
> |
|
1124 |
> |
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0 |
1125 |
> |
#ifdef WIN32 |
1126 |
> |
&& (tries<6) |
1127 |
> |
#endif |
1128 |
> |
){ |
1129 |
> |
log_msg(LOG_ERR, "Failed to create udp socket"); |
1130 |
> |
sleep(SLEEP); |
1131 |
> |
#ifdef WIN32 |
1132 |
> |
tries++; |
1133 |
> |
#endif |
1134 |
> |
} |
1135 |
> |
|
1136 |
> |
#ifdef WIN32 |
1137 |
> |
if (tries == 6) { |
1138 |
> |
log_msg(LOG_ERR, "Giving up"); |
1139 |
> |
goto out; |
1140 |
> |
} |
1141 |
> |
#endif |
1142 |
> |
|
1143 |
> |
config_time=time(NULL)+ihost_state.config_ttl; |
1144 |
> |
|
1145 |
> |
#ifdef WIN32 |
1146 |
> |
/* Finally, we have started successfully! |
1147 |
> |
* We set state to running even for non-service calls so the while |
1148 |
> |
* loop works. Bit of a hack ;) |
1149 |
> |
*/ |
1150 |
> |
service_status.dwCurrentState = SERVICE_RUNNING; |
1151 |
> |
if(run_as_service) { |
1152 |
> |
SetServiceStatus(hstatus, &service_status); |
1153 |
> |
} |
1154 |
> |
printf("started\n"); |
1155 |
> |
#endif |
1156 |
> |
|
1157 |
> |
/* Now have config.. collect data and send as often as required */ |
1158 |
> |
#ifdef WIN32 |
1159 |
> |
while(service_status.dwCurrentState == SERVICE_RUNNING) |
1160 |
> |
#else |
1161 |
> |
for(;;) |
1162 |
> |
#endif |
1163 |
> |
{ |
1164 |
|
cur_time=time(NULL); |
615 |
– |
if(cur_time>=tcp_time){ |
616 |
– |
logmessage(LOG_DEBUG,"Sending heartbeat"); |
617 |
– |
heartbeat_exit=heartbeat(&ihost_state); |
618 |
– |
if(heartbeat_exit==RECONFIGURE_RETURN_CODE){ |
619 |
– |
logmessage(LOG_INFO,"heartbeat needs to be reconfigured"); |
620 |
– |
ihost_configure(&ihost_state); |
621 |
– |
udp_time=0; |
622 |
– |
} |
623 |
– |
if(heartbeat_exit==-1){ |
624 |
– |
logmessage(LOG_ERR,"Heartbeat failed"); |
625 |
– |
exit(1); |
626 |
– |
} |
627 |
– |
tcp_time=time(NULL)+ihost_state.tcp_update_time; |
628 |
– |
logmessage(LOG_DEBUG,"next tcp time should be %d", tcp_time); |
629 |
– |
} |
1165 |
|
|
1166 |
|
if(cur_time>=udp_time){ |
1167 |
< |
logmessage(LOG_DEBUG,"Sending udp data"); |
1167 |
> |
if(sg_snapshot()) { |
1168 |
> |
log_msg(LOG_ERR, "sg_snapshot failed: %s (%s)", |
1169 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
1170 |
> |
continue; /* Could this get ugly? */ |
1171 |
> |
} |
1172 |
> |
if((get_system_stats(packet_num++, &ihost_state, packet, MAX_UDP_PACKET_SIZE))!=0){ |
1173 |
> |
log_msg(LOG_ERR, "Failed to get system stats"); |
1174 |
> |
} |
1175 |
|
|
1176 |
< |
stat_grab_time=time(NULL); |
1177 |
< |
if((xml_stats=stat_grab(&ihost_state, counter++)) == NULL){ |
1178 |
< |
logmessage(LOG_ERR,"Failed to get stats (%m)"); |
1179 |
< |
exit(1); |
1176 |
> |
len=strlen(packet); |
1177 |
> |
log_msg(LOG_DEBUG, "Packet size: %d\nPacket: %s\n", len, packet); |
1178 |
> |
|
1179 |
> |
if((sendto(udp_sockinfo.sock, packet, len, 0, (struct sockaddr *) &udp_sockinfo.addr, sizeof(udp_sockinfo.addr)))!=len){ |
1180 |
> |
log_msg(LOG_CRIT, "Failed to send packet"); |
1181 |
|
} |
1182 |
< |
stat_grab_time=time(NULL)-stat_grab_time; |
1183 |
< |
send_stats(&ihost_state, xml_stats); |
641 |
< |
free(xml_stats); |
642 |
< |
udp_time=time(NULL)+ihost_state.udp_update_time-stat_grab_time; |
643 |
< |
logmessage(LOG_DEBUG,"next udp time should be %d", udp_time); |
1182 |
> |
udp_time=cur_time+ihost_state.udp_update_time; |
1183 |
> |
log_msg(LOG_DEBUG, "Next packet should be sent on %d", udp_time); |
1184 |
|
} |
1185 |
|
|
1186 |
< |
if(tcp_time<udp_time){ |
1187 |
< |
sleep_delay=tcp_time-time(NULL); |
1188 |
< |
}else{ |
1189 |
< |
sleep_delay=udp_time-time(NULL); |
1186 |
> |
if(cur_time>=config_time){ |
1187 |
> |
if(ihost_getconfig(&ihost_state)!=0){ |
1188 |
> |
/* If we can't get the config, try again 5 minutes time */ |
1189 |
> |
log_msg(LOG_ERR, "Failed to get config, try again 5 minutes time"); |
1190 |
> |
config_time=time(NULL)+300; |
1191 |
> |
}else{ |
1192 |
> |
close(udp_sockinfo.sock); |
1193 |
> |
|
1194 |
> |
while((create_udp_sockinfo(&udp_sockinfo, ihost_state.server_fqdn, ihost_state.server_udp_port))!=0){ |
1195 |
> |
log_msg(LOG_CRIT, "Failed to create udp socket"); |
1196 |
> |
sleep(SLEEP); |
1197 |
> |
} |
1198 |
> |
|
1199 |
> |
config_time=time(NULL)+ihost_state.config_ttl; |
1200 |
> |
|
1201 |
> |
log_msg(LOG_DEBUG, "Config expires on %d\n", ihost_state.config_ttl); |
1202 |
> |
} |
1203 |
|
} |
1204 |
< |
logmessage(LOG_DEBUG,"Sleeping for %d", sleep_delay); |
1204 |
> |
|
1205 |
> |
sleep_delay=udp_time-time(NULL); |
1206 |
> |
log_msg(LOG_DEBUG, "Sleeping for %d", sleep_delay); |
1207 |
> |
#ifdef WIN32 |
1208 |
> |
/* convert to millisecs */ |
1209 |
> |
sleep_delay *= 1000; |
1210 |
> |
#endif |
1211 |
|
if(sleep_delay>0) sleep(sleep_delay); |
1212 |
|
} |
1213 |
< |
return 0; |
1213 |
> |
|
1214 |
> |
if(sg_shutdown()) { |
1215 |
> |
log_msg(LOG_ERR, "sg_shutdown failed: %s (%s)", |
1216 |
> |
sg_str_error(sg_get_error()), sg_get_error_arg()); |
1217 |
> |
} |
1218 |
> |
|
1219 |
> |
#ifdef WIN32 |
1220 |
> |
WSACleanup(); |
1221 |
> |
if(run_as_service) { |
1222 |
> |
service_status.dwWin32ExitCode = 0; |
1223 |
> |
service_status.dwCurrentState = SERVICE_STOPPED; |
1224 |
> |
SetServiceStatus(hstatus, &service_status); |
1225 |
> |
} |
1226 |
> |
#endif |
1227 |
> |
log_msg(LOG_ERR, "ihost shutdown successfully"); |
1228 |
> |
fclose(ihost_config.log); |
1229 |
> |
|
1230 |
> |
return(0); |
1231 |
> |
|
1232 |
> |
out: |
1233 |
> |
#ifdef WIN32 |
1234 |
> |
WSACleanup(); |
1235 |
> |
service_stopped(); |
1236 |
> |
#endif |
1237 |
> |
exit(1); |
1238 |
|
} |
1239 |
|
|
1240 |
+ |
#ifdef WIN32 |
1241 |
+ |
int service_main(int argc, char **argv) { |
1242 |
+ |
int error; |
1243 |
+ |
|
1244 |
+ |
service_status.dwServiceType = SERVICE_WIN32; |
1245 |
+ |
service_status.dwCurrentState = SERVICE_START_PENDING; |
1246 |
+ |
service_status.dwControlsAccepted = SERVICE_ACCEPT_STOP | |
1247 |
+ |
SERVICE_ACCEPT_SHUTDOWN; |
1248 |
+ |
service_status.dwWin32ExitCode = 0; |
1249 |
+ |
service_status.dwServiceSpecificExitCode = 0; |
1250 |
+ |
service_status.dwCheckPoint = 0; |
1251 |
+ |
service_status.dwWaitHint = 0; |
1252 |
+ |
|
1253 |
+ |
hstatus = RegisterServiceCtrlHandler(SERVICENAME, |
1254 |
+ |
(LPHANDLER_FUNCTION)control_handler); |
1255 |
+ |
if(hstatus == (SERVICE_STATUS_HANDLE)0) { |
1256 |
+ |
log_msg(LOG_CRIT, "RegisterServiceCtrlHandle failed"); |
1257 |
+ |
return -1; |
1258 |
+ |
} |
1259 |
+ |
|
1260 |
+ |
return run_ihost(); |
1261 |
+ |
} |
1262 |
+ |
|
1263 |
+ |
|
1264 |
+ |
void parse_w(int argc, char **argv) { |
1265 |
+ |
if (strcasecmp(optarg, "start") == 0) { |
1266 |
+ |
log_msg(LOG_ERR, "Attempting to start service..."); |
1267 |
+ |
run_server = 0; |
1268 |
+ |
if(start_service()) |
1269 |
+ |
log_msg(LOG_ERR, "Service started"); |
1270 |
+ |
} else if (strcasecmp(optarg, "stop") == 0) { |
1271 |
+ |
log_msg(LOG_ERR, "Attempting to stop service..."); |
1272 |
+ |
run_server = 0; |
1273 |
+ |
if(stop_service()) |
1274 |
+ |
log_msg(LOG_ERR, "Service stopped"); |
1275 |
+ |
} else if (strcasecmp(optarg, "service") == 0) { |
1276 |
+ |
run_as_service = 1; |
1277 |
+ |
} else if (strcasecmp(optarg, "register") == 0) { |
1278 |
+ |
log_msg(LOG_ERR, "Attempting to register service..."); |
1279 |
+ |
run_server = 0; |
1280 |
+ |
int i = optind; |
1281 |
+ |
optind = argc; |
1282 |
+ |
/* Grab the remaining arguments and use each time the service |
1283 |
+ |
* is started */ |
1284 |
+ |
if (register_service(argc-i, &argv[i])) |
1285 |
+ |
log_msg(LOG_ERR, "Registered service successfully"); |
1286 |
+ |
} else if (strcasecmp(optarg, "unregister") == 0) { |
1287 |
+ |
log_msg(LOG_ERR, "Attempting to unregister service..."); |
1288 |
+ |
run_server = 0; |
1289 |
+ |
if(unregister_service()) |
1290 |
+ |
log_msg(LOG_ERR, "Unregistered service successfully"); |
1291 |
+ |
} else { |
1292 |
+ |
fprintf(stderr, "Unknown -w option\n"); |
1293 |
+ |
usage(argv[0]); |
1294 |
+ |
exit(1); |
1295 |
+ |
} |
1296 |
+ |
} |
1297 |
+ |
#endif |
1298 |
+ |
|
1299 |
+ |
|
1300 |
+ |
int main(int argc, char **argv){ |
1301 |
+ |
int cmdopt; |
1302 |
+ |
extern int optind; |
1303 |
+ |
|
1304 |
+ |
/* Set default settings */ |
1305 |
+ |
ihost_config.verbose=1; |
1306 |
+ |
ihost_config.daemon=1; |
1307 |
+ |
/* Set all errors to go down stderr until told otherwise */ |
1308 |
+ |
ihost_config.log=stderr; |
1309 |
+ |
|
1310 |
+ |
/* Blank ihost_state to default settings */ |
1311 |
+ |
ihost_state.filtermanager_host=DEF_SERVER_NAME; |
1312 |
+ |
ihost_state.filtermanager_port=DEF_SERVER_PORT; |
1313 |
+ |
ihost_state.host_fqdn=NULL; |
1314 |
+ |
ihost_state.host_ip=NULL; |
1315 |
+ |
ihost_state.preset_fqdn = 0; |
1316 |
+ |
ihost_state.preset_ip = 0; |
1317 |
+ |
ihost_state.server_fqdn=NULL; |
1318 |
+ |
ihost_state.file_list=NULL; |
1319 |
+ |
ihost_state.last_modified=NULL; |
1320 |
+ |
|
1321 |
+ |
while((cmdopt=getopt(argc, argv, OPTSTRING)) != -1){ |
1322 |
+ |
switch(cmdopt){ |
1323 |
+ |
case 'v': |
1324 |
+ |
ihost_config.verbose++; |
1325 |
+ |
break; |
1326 |
+ |
|
1327 |
+ |
#ifndef WIN32 |
1328 |
+ |
case 'f': |
1329 |
+ |
/* Force syslog logging since stderr will be closed in this case */ |
1330 |
+ |
ihost_config.daemon=0; |
1331 |
+ |
break; |
1332 |
+ |
#endif |
1333 |
+ |
|
1334 |
+ |
case 'V': |
1335 |
+ |
fprintf(stderr, "%s version %s\n", argv[0], VERSION); |
1336 |
+ |
break; |
1337 |
+ |
case 'n': |
1338 |
+ |
ihost_state.preset_fqdn = 1; |
1339 |
+ |
ihost_state.host_fqdn = strdup(optarg); |
1340 |
+ |
if(ihost_state.host_fqdn == NULL){ |
1341 |
+ |
fprintf(stderr, "Missing hostname\n"); |
1342 |
+ |
usage(argv[0]); |
1343 |
+ |
} |
1344 |
+ |
break; |
1345 |
+ |
case 'i': |
1346 |
+ |
/* Hmm.. Someone could set any string to be the IP, and it will let it */ |
1347 |
+ |
ihost_state.preset_ip = 1; |
1348 |
+ |
ihost_state.host_ip = strdup(optarg); |
1349 |
+ |
if(ihost_state.host_ip == NULL){ |
1350 |
+ |
fprintf(stderr, "Missing ip\n"); |
1351 |
+ |
usage(argv[0]); |
1352 |
+ |
} |
1353 |
+ |
break; |
1354 |
+ |
|
1355 |
+ |
case 's': |
1356 |
+ |
ihost_state.filtermanager_host=strdup(optarg); |
1357 |
+ |
break; |
1358 |
+ |
|
1359 |
+ |
case 'p': |
1360 |
+ |
ihost_state.filtermanager_port=atoi(optarg); |
1361 |
+ |
break; |
1362 |
+ |
|
1363 |
+ |
#ifdef WIN32 |
1364 |
+ |
case 'w': |
1365 |
+ |
parse_w(argc, argv); |
1366 |
+ |
break; |
1367 |
+ |
#endif |
1368 |
+ |
|
1369 |
+ |
case 'h': |
1370 |
+ |
default: |
1371 |
+ |
usage(argv[0]); |
1372 |
+ |
exit(1); |
1373 |
+ |
} |
1374 |
+ |
} |
1375 |
+ |
|
1376 |
+ |
#ifdef WIN32 |
1377 |
+ |
if (run_server) { |
1378 |
+ |
if(run_as_service) { |
1379 |
+ |
if((ihost_config.log=fopen(LOG_FILE, "a"))==NULL){ |
1380 |
+ |
ihost_config.log=stderr; |
1381 |
+ |
log_msg(LOG_CRIT, "Failed to open Logfiles %s for writing", LOG_FILE); |
1382 |
+ |
exit(1); |
1383 |
+ |
} |
1384 |
+ |
log_msg(LOG_ERR, "Starting Service-Mode i-host"); |
1385 |
+ |
SERVICE_TABLE_ENTRY service_table[2]; |
1386 |
+ |
service_table[0].lpServiceName = SERVICENAME; |
1387 |
+ |
service_table[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)service_main; |
1388 |
+ |
service_table[1].lpServiceName = NULL; |
1389 |
+ |
service_table[1].lpServiceProc = NULL; |
1390 |
+ |
return StartServiceCtrlDispatcher(service_table); |
1391 |
+ |
} else { |
1392 |
+ |
log_msg(LOG_ERR, "Starting User-Mode i-host"); |
1393 |
+ |
return run_ihost(); |
1394 |
+ |
} |
1395 |
+ |
} |
1396 |
+ |
#else |
1397 |
+ |
return run_ihost(); |
1398 |
+ |
#endif |
1399 |
+ |
} |