aboutsummaryrefslogtreecommitdiffstats
path: root/gg_sniff/gg_sniff.c
diff options
context:
space:
mode:
authorLaurent Ghigonis <laurent@p1sec.com>2012-12-04 13:43:10 +0100
committerLaurent Ghigonis <laurent@p1sec.com>2012-12-04 13:43:10 +0100
commit757e944fd2121d5b7ca722fba65bd489d62078b7 (patch)
tree461d2daed4e6fc7a64de8dc7501865b4666d2af8 /gg_sniff/gg_sniff.c
parentcleanup all objects (diff)
downloadglouglou-757e944fd2121d5b7ca722fba65bd489d62078b7.tar.xz
glouglou-757e944fd2121d5b7ca722fba65bd489d62078b7.zip
fix signal handler, cmdline args and retval
Diffstat (limited to 'gg_sniff/gg_sniff.c')
-rw-r--r--gg_sniff/gg_sniff.c47
1 files changed, 26 insertions, 21 deletions
diff --git a/gg_sniff/gg_sniff.c b/gg_sniff/gg_sniff.c
index 7b8c08d..e35238e 100644
--- a/gg_sniff/gg_sniff.c
+++ b/gg_sniff/gg_sniff.c
@@ -28,6 +28,8 @@
#define GG_SNIFF_USER "_gg_sniff"
#define GG_SNIFF_LOGFILE "/var/log/ggsniff.log"
+struct event_base *_ev_base = NULL;
+
#if defined(__OpenBSD__)
void __dead
#else
@@ -37,7 +39,7 @@ usage(void)
{
extern char *__progname;
- fprintf(stderr, "usage: %s [-hv]", __progname);
+ fprintf(stderr, "usage: %s [-hv] interface [ip [port]]", __progname);
exit(1);
}
@@ -46,22 +48,23 @@ sig_handler(int sig, short why, void *data)
{
gg_log_info("got signal %d", sig);
if (sig == SIGINT || sig == SIGTERM)
- event_loopexit(NULL);
+ if (_ev_base)
+ event_base_loopexit(_ev_base, NULL);
}
int
main(int argc, char **argv)
{
- struct event_base *ev_base;
struct gg_client *ggcli = NULL;
struct ggnet *net = NULL;
- struct event ev_sigint, ev_sigterm, ev_sigchld, ev_sighup;
+ struct event *ev_sigint, *ev_sigterm, *ev_sigchld, *ev_sighup;
char ggserv_ip[30] = "127.0.0.1";
char iface[30] = "lo";
int ggserv_port = GLOUGLOU_PROBE_DEFAULT_PORT;
int pcap_init = 0;
int dns_init = 0;
int loglevel = 0;
+ int retval = -1;
int op;
if (geteuid() != 0)
@@ -81,9 +84,9 @@ main(int argc, char **argv)
}
}
switch (argc) {
- case 3: ggserv_port = atoi(argv[3]);
- case 2: strncpy(ggserv_ip, argv[2], sizeof(ggserv_ip));
- case 1: strncpy(iface, argv[1], sizeof(iface));
+ case 4: ggserv_port = atoi(argv[3]);
+ case 3: strncpy(ggserv_ip, argv[2], sizeof(ggserv_ip));
+ case 2: strncpy(iface, argv[1], sizeof(iface));
break;
default:
usage();
@@ -91,36 +94,38 @@ main(int argc, char **argv)
}
gg_log_init(GG_SNIFF_LOGFILE, loglevel);
+ gg_log_warn("Starting gg_sniff");
- ev_base = event_base_new();
+ _ev_base = event_base_new();
net = ggnet_new();
if (!net)
goto quit;
- ggcli = gg_client_connect(ev_base, ggserv_ip, ggserv_port, NULL, NULL, NULL);
+ ggcli = gg_client_connect(_ev_base, ggserv_ip, ggserv_port, NULL, NULL, NULL);
if (!ggcli)
goto quit;
- pcap_init = ggsniff_pcap_init(ev_base, ggcli, net, iface);
+ pcap_init = ggsniff_pcap_init(_ev_base, ggcli, net, iface);
if (!pcap_init)
goto quit;
- dns_init = ggsniff_dns_init(ev_base);
+ dns_init = ggsniff_dns_init(_ev_base);
if (!dns_init)
goto quit;
- signal_set(&ev_sigint, SIGINT, sig_handler, NULL);
- signal_set(&ev_sigterm, SIGTERM, sig_handler, NULL);
- signal_set(&ev_sigchld, SIGCHLD, sig_handler, NULL);
- signal_set(&ev_sighup, SIGHUP, sig_handler, NULL);
- signal_add(&ev_sigint, NULL);
- signal_add(&ev_sigterm, NULL);
- signal_add(&ev_sigchld, NULL);
- signal_add(&ev_sighup, NULL);
+ ev_sigint = evsignal_new(_ev_base, SIGINT, sig_handler, NULL);
+ ev_sigterm = evsignal_new(_ev_base, SIGTERM, sig_handler, NULL);
+ ev_sigchld = evsignal_new(_ev_base, SIGCHLD, sig_handler, NULL);
+ ev_sighup = evsignal_new(_ev_base, SIGHUP, sig_handler, NULL);
+ evsignal_add(ev_sigint, NULL);
+ evsignal_add(ev_sigterm, NULL);
+ evsignal_add(ev_sigchld, NULL);
+ evsignal_add(ev_sighup, NULL);
signal(SIGPIPE, SIG_IGN);
droppriv(GG_SNIFF_USER, 1, NULL);
gg_log_info("entering event loop");
- event_base_dispatch(ev_base);
+ event_base_dispatch(_ev_base);
+ retval = 0;
quit:
if (dns_init)
@@ -136,5 +141,5 @@ quit:
gg_log_shutdown();
- exit(0);
+ exit(retval);
}