2013-06-21 01:27:28 +00:00
|
|
|
/* echld_dispatcher.c
|
|
|
|
* epan working child API internals
|
|
|
|
* Dispatcher process routines and definitions
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
|
|
|
* Copyright (c) 2013 by Luis Ontanon <luis@ontanon.org>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "echld-int.h"
|
2013-11-08 11:50:57 +00:00
|
|
|
/**
|
2013-06-21 01:27:28 +00:00
|
|
|
DISPATCHER
|
|
|
|
**/
|
|
|
|
|
|
|
|
struct dispatcher_child {
|
2013-06-22 20:26:13 +00:00
|
|
|
echld_chld_id_t chld_id;
|
2013-06-21 01:27:28 +00:00
|
|
|
child_state_t state;
|
|
|
|
echld_reader_t reader;
|
|
|
|
int write_fd;
|
|
|
|
int pid;
|
2013-07-01 01:49:34 +00:00
|
|
|
int reqh_id;
|
2013-06-21 01:27:28 +00:00
|
|
|
gboolean closing;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dispatcher {
|
|
|
|
int parent_out;
|
|
|
|
echld_reader_t parent_in;
|
|
|
|
struct dispatcher_child* children;
|
|
|
|
int max_children;
|
|
|
|
int nchildren;
|
|
|
|
int reqh_id;
|
|
|
|
int pid;
|
|
|
|
int ppid;
|
|
|
|
struct _encs {
|
|
|
|
child_encoder_t* to_parent;
|
|
|
|
echld_parent_encoder_t* to_child;
|
|
|
|
} enc;
|
|
|
|
struct _decs {
|
|
|
|
child_decoder_t* from_parent;
|
|
|
|
parent_decoder_t* from_child;
|
|
|
|
} dec;
|
|
|
|
|
2013-06-27 19:00:01 +00:00
|
|
|
int dumpcap_pid;
|
2013-06-21 01:27:28 +00:00
|
|
|
gboolean closing;
|
2013-07-04 05:34:25 +00:00
|
|
|
capture_options capture_opts;
|
2013-06-21 01:27:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct dispatcher* dispatcher;
|
|
|
|
|
|
|
|
#ifdef DEBUG_DISPATCHER
|
2013-06-27 03:41:48 +00:00
|
|
|
static int debug_lvl = DEBUG_DISPATCHER;
|
|
|
|
static FILE* debug_fp = NULL;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
#define DCOM() /*echld_common_set_dbg(debug_lvl,debug_fp,"Disp")*/
|
2013-06-27 03:41:48 +00:00
|
|
|
|
|
|
|
int dispatcher_debug(int level, const char* fmt, ...) {
|
2013-06-21 01:27:28 +00:00
|
|
|
va_list ap;
|
|
|
|
char* str;
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
if (debug_lvl<level) return 1;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-11-08 11:50:57 +00:00
|
|
|
va_start(ap, fmt);
|
2013-06-21 01:27:28 +00:00
|
|
|
str = g_strdup_vprintf(fmt,ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
if (dispatcher) {
|
|
|
|
fprintf(debug_fp, "dispatcher[%d]: reqh_id=%d dbg_level=%d message='%s'\n", dispatcher->pid, dispatcher->reqh_id, level, str);
|
|
|
|
} else {
|
|
|
|
fprintf(debug_fp, "dispatcher: dbg_level=%d message='%s'\n", level, str);
|
|
|
|
}
|
|
|
|
|
|
|
|
fflush(debug_fp);
|
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
g_free(str);
|
2013-06-27 03:41:48 +00:00
|
|
|
|
|
|
|
return 1;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
static char* param_get_dbg_level(char** err _U_) {
|
2013-06-27 03:41:48 +00:00
|
|
|
return g_strdup_printf("%d",debug_lvl);
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static echld_bool_t param_set_dbg_level(char* val , char** err ) {
|
|
|
|
char* p;
|
2013-06-22 20:26:13 +00:00
|
|
|
int lvl = (int)strtol(val, &p, 10);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
if (p<=val) {
|
|
|
|
*err = g_strdup("not an integer");
|
|
|
|
return FALSE;
|
|
|
|
} else if (lvl < 0 || lvl > 5) {
|
|
|
|
*err = g_strdup_printf("invalid level=%d (min=0 max=5)",lvl);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
debug_lvl = lvl;
|
2013-06-27 19:00:01 +00:00
|
|
|
DCOM();
|
2013-06-21 01:27:28 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
static long dbg_r = 0;
|
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
#define DISP_DBG(attrs) ( dispatcher_debug attrs )
|
2013-06-27 19:00:01 +00:00
|
|
|
#define DISP_DBG_INIT() do { debug_fp = stderr; DCOM(); } while(0)
|
2014-10-18 06:39:20 +00:00
|
|
|
#define DISP_DBG_START(fname) do { debug_fp = ws_fopen(fname,"a"); DCOM(); DISP_DBG((0,"Log Started")); } while(0)
|
|
|
|
#define DISP_WRITE(FD,BA,CH,T,RH) ( dbg_r = echld_write_frame(FD,BA,CH,T,RH,NULL), DISP_DBG((1,"SND fd=%d ch=%d ty='%s' rh=%d msg='%s'",FD,CH,TY(T),RH, (dbg_r>0?"ok":g_strerror(errno)))))
|
2013-06-29 01:16:48 +00:00
|
|
|
#define CHLD_SET_STATE(c,st) do { DISP_DBG((1,"Child[%d] State %s => %s",(c)->chld_id, ST((c)->state), ST((st)) )); (c)->state=(st); } while(0)
|
2013-06-21 01:27:28 +00:00
|
|
|
#else
|
|
|
|
#define DISP_DBG(attrs)
|
2013-06-27 03:41:48 +00:00
|
|
|
#define DISP_DBG_INIT()
|
|
|
|
#define DISP_DBG_START(fname)
|
|
|
|
#define DISP_WRITE(FD,BA,CH,T,RH) echld_write_frame(FD,BA,CH,T,RH,NULL)
|
2013-06-29 01:16:48 +00:00
|
|
|
#define CHLD_SET_STATE(c,st) ((c)->state = (st))
|
2013-06-21 01:27:28 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
#define DISP_RESP(B,T) (DISP_WRITE( dispatcher->parent_out, (B), 0, (T), dispatcher->reqh_id))
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
|
|
|
|
static echld_epan_stuff_t stuff;
|
|
|
|
|
|
|
|
static void init_stuff(void) {
|
|
|
|
#ifdef HAVE_LIBPCAP
|
|
|
|
capture_opts_init(&stuff.cap_opts);
|
2014-09-09 20:42:58 +00:00
|
|
|
capture_session_init(&stuff.cap_sess, &stuff.cfile);
|
2013-07-05 23:57:07 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
static void children_massacre(void) {
|
|
|
|
int i;
|
|
|
|
struct dispatcher_child* cc = dispatcher->children;
|
|
|
|
int max_children = dispatcher->max_children;
|
|
|
|
|
|
|
|
for(i = 0; i < max_children; i++) {
|
|
|
|
struct dispatcher_child* c = &(cc[i]);
|
|
|
|
if (c->pid > 0) {
|
|
|
|
DISP_DBG((0,"killing ch=%d pid=%d",c->chld_id,c->pid));
|
|
|
|
kill(c->pid,SIGTERM);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
static void dispatcher_fatal(int cause, const char* fmt, ...) {
|
|
|
|
size_t len= 1024;
|
|
|
|
gchar err_str[len];
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
g_vsnprintf(err_str,len,fmt,ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
DISP_DBG((0,"fatal cause=%d msg=\"%s\"",cause ,err_str));
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
children_massacre();
|
2013-06-27 03:41:48 +00:00
|
|
|
|
|
|
|
exit(cause);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DISP_FATAL(attrs) dispatcher_fatal attrs
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
static void dispatcher_err(int errnum, const char* fmt, ...) {
|
|
|
|
size_t len= 1024;
|
|
|
|
gchar err_str[len];
|
|
|
|
va_list ap;
|
|
|
|
static GByteArray* ba;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
va_start(ap, fmt);
|
|
|
|
g_vsnprintf(err_str,len,fmt,ap);
|
|
|
|
va_end(ap);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
DISP_DBG((0,"error=\"%s\"",err_str));
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
ba = dispatcher->enc.to_parent->error(errnum, err_str);
|
|
|
|
DISP_RESP(ba,ECHLD_ERROR);
|
|
|
|
g_byte_array_free(ba,TRUE);
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
/* parameters */
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
/* interface listing */
|
|
|
|
|
2013-07-04 05:34:25 +00:00
|
|
|
static char* intflist2json(GList* if_list, char** if_cap_err) {
|
2013-06-22 20:26:13 +00:00
|
|
|
#define ADDRSTRLEN 46 /* Covers IPv4 & IPv6 */
|
|
|
|
|
|
|
|
GList *if_entry;
|
|
|
|
if_info_t *if_info;
|
|
|
|
GSList *addr;
|
|
|
|
if_addr_t *if_addr;
|
2013-07-04 05:34:25 +00:00
|
|
|
if_capabilities_t *caps;
|
2013-06-22 20:26:13 +00:00
|
|
|
char addr_str[ADDRSTRLEN];
|
2013-06-28 21:10:52 +00:00
|
|
|
GString *str = g_string_new("{ what='interfaces', interfaces={ \n");
|
2013-06-22 20:26:13 +00:00
|
|
|
|
|
|
|
for (if_entry = g_list_first(if_list); if_entry != NULL;
|
|
|
|
if_entry = g_list_next(if_entry)) {
|
|
|
|
if_info = (if_info_t *)if_entry->data;
|
2013-06-28 21:10:52 +00:00
|
|
|
g_string_append_printf(str," %s={ intf='%s',", if_info->name, if_info->name);
|
2013-06-22 20:26:13 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Print the contents of the if_entry struct in a parseable format.
|
|
|
|
* Each if_entry element is tab-separated. Addresses are comma-
|
|
|
|
* separated.
|
|
|
|
*/
|
|
|
|
/* XXX - Make sure our description doesn't contain a tab */
|
|
|
|
if (if_info->vendor_description != NULL)
|
|
|
|
g_string_append_printf(str," vnd_desc='%s',", if_info->vendor_description);
|
|
|
|
|
|
|
|
/* XXX - Make sure our friendly name doesn't contain a tab */
|
|
|
|
if (if_info->friendly_name != NULL)
|
|
|
|
g_string_append_printf(str," name='%s', addrs=[ ", if_info->friendly_name);
|
|
|
|
|
|
|
|
for (addr = g_slist_nth(if_info->addrs, 0); addr != NULL;
|
|
|
|
addr = g_slist_next(addr)) {
|
|
|
|
|
|
|
|
if_addr = (if_addr_t *)addr->data;
|
|
|
|
switch(if_addr->ifat_type) {
|
|
|
|
case IF_AT_IPv4:
|
|
|
|
if (inet_ntop(AF_INET, &if_addr->addr.ip4_addr, addr_str,
|
|
|
|
ADDRSTRLEN)) {
|
2013-06-28 21:10:52 +00:00
|
|
|
g_string_append_printf(str,"'%s',", addr_str);
|
2013-06-22 20:26:13 +00:00
|
|
|
} else {
|
2013-06-28 21:10:52 +00:00
|
|
|
g_string_append(str,"'<unknown IPv4>',");
|
2013-06-22 20:26:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case IF_AT_IPv6:
|
|
|
|
if (inet_ntop(AF_INET6, &if_addr->addr.ip6_addr,
|
|
|
|
addr_str, ADDRSTRLEN)) {
|
2013-06-28 21:10:52 +00:00
|
|
|
g_string_append_printf(str,"'%s',", addr_str);
|
2013-06-22 20:26:13 +00:00
|
|
|
} else {
|
2013-06-28 21:10:52 +00:00
|
|
|
g_string_append(str,"'<unknown IPv6>',");
|
2013-06-22 20:26:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2013-06-28 21:10:52 +00:00
|
|
|
g_string_append_printf(str,"'<type unknown %u>',", if_addr->ifat_type);
|
2013-06-22 20:26:13 +00:00
|
|
|
}
|
2013-11-08 11:50:57 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
}
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
g_string_truncate(str,str->len - 1); /* the last comma or space (on empty list) */
|
2013-07-04 05:34:25 +00:00
|
|
|
g_string_append(str," ]"); /* addrs */
|
2013-06-22 20:26:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (if_info->loopback)
|
|
|
|
g_string_append(str,", loopback=1");
|
|
|
|
else
|
|
|
|
g_string_append(str,", loopback=0");
|
|
|
|
|
2013-07-04 05:34:25 +00:00
|
|
|
|
|
|
|
|
2015-07-17 15:01:27 +00:00
|
|
|
caps = capture_get_if_capabilities(if_info->name, 0, NULL, if_cap_err, NULL);
|
2013-07-04 05:34:25 +00:00
|
|
|
|
|
|
|
if (caps != NULL) {
|
|
|
|
if (caps->data_link_types != NULL) {
|
|
|
|
GList* lt_entry = caps->data_link_types;
|
|
|
|
data_link_info_t *data_link_info;
|
|
|
|
|
|
|
|
g_string_append(str,", data_link_types=[");
|
|
|
|
|
|
|
|
for (; lt_entry != NULL; lt_entry = g_list_next(lt_entry) ) {
|
|
|
|
|
|
|
|
data_link_info = (data_link_info_t *)lt_entry->data;
|
|
|
|
g_string_append_printf(str,"{ name='%s', desc='%s' }, ", data_link_info->name, (data_link_info->description) ? data_link_info->description : "" );
|
2013-11-08 11:50:57 +00:00
|
|
|
}
|
2013-07-04 05:34:25 +00:00
|
|
|
|
|
|
|
g_string_truncate(str,str->len - 2); /* the comma and space */
|
|
|
|
g_string_append(str,"]");
|
2013-11-08 11:50:57 +00:00
|
|
|
}
|
2013-07-04 05:34:25 +00:00
|
|
|
|
|
|
|
g_string_append_printf(str,", can_set_rfmon=%s", caps->can_set_rfmon ? "1" : "0");
|
|
|
|
|
|
|
|
if (caps->can_set_rfmon) {
|
|
|
|
free_if_capabilities(caps);
|
2015-07-17 15:01:27 +00:00
|
|
|
caps = capture_get_if_capabilities(if_info->name, 1, NULL, if_cap_err, NULL);
|
2013-07-04 05:34:25 +00:00
|
|
|
|
|
|
|
if (caps->data_link_types != NULL) {
|
|
|
|
GList* lt_entry = caps->data_link_types;
|
|
|
|
data_link_info_t *data_link_info;
|
|
|
|
|
|
|
|
g_string_append(str,", data_link_types_rfmon=[");
|
|
|
|
|
|
|
|
for (; lt_entry != NULL; lt_entry = g_list_next(lt_entry)) {
|
|
|
|
data_link_info = (data_link_info_t *)lt_entry->data;
|
|
|
|
g_string_append_printf(str,"{ name='%s', desc='%s' }, ", data_link_info->name, (data_link_info->description) ? data_link_info->description : "" );
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_truncate(str,str->len - 2); /* the comma and space */
|
|
|
|
g_string_append(str,"]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
free_if_capabilities(caps);
|
|
|
|
}
|
2013-11-08 11:50:57 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
g_string_append(str,"},\n");
|
2013-06-22 20:26:13 +00:00
|
|
|
}
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
g_string_truncate(str,str->len - 2); /* the comma and return */
|
2013-06-22 20:26:13 +00:00
|
|
|
g_string_append(str,"}");
|
|
|
|
|
2015-01-13 23:13:40 +00:00
|
|
|
return g_string_free(str,FALSE);
|
2013-06-22 20:26:13 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
static char* intf_list = NULL;
|
2013-07-04 05:34:25 +00:00
|
|
|
|
|
|
|
static void get_interfaces(char** err) {
|
2013-06-22 20:26:13 +00:00
|
|
|
int err_no = 0;
|
|
|
|
GList* if_list;
|
2013-11-08 11:50:57 +00:00
|
|
|
|
|
|
|
err = NULL;
|
2013-06-27 19:00:01 +00:00
|
|
|
if_list = capture_interface_list(&err_no, err, NULL);
|
2013-06-22 20:26:13 +00:00
|
|
|
|
2013-07-04 05:34:25 +00:00
|
|
|
if (err) {
|
|
|
|
DISP_DBG((1,"Could not get capture interface list: %s",err));
|
|
|
|
} else {
|
|
|
|
intf_list = intflist2json(if_list,err);
|
|
|
|
if (err) {
|
|
|
|
DISP_DBG((1,"get capabilities error: %s",err));
|
|
|
|
}
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
free_interface_list(if_list);
|
2013-07-04 05:34:25 +00:00
|
|
|
}
|
2013-06-22 20:26:13 +00:00
|
|
|
|
2013-07-04 05:34:25 +00:00
|
|
|
|
|
|
|
static char* param_get_interfaces(char** err _U_) {
|
|
|
|
return g_strdup(intf_list ? intf_list : "");
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-07-01 01:49:34 +00:00
|
|
|
static long disp_loop_timeout_usec = DISPATCHER_WAIT_INITIAL;
|
2013-06-27 03:41:48 +00:00
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
static char* param_get_loop_timeout(char** err _U_) {
|
2013-07-01 01:49:34 +00:00
|
|
|
return g_strdup_printf("%fs", (((float)disp_loop_timeout_usec)/1000000.0) );
|
2013-06-27 03:41:48 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
static echld_bool_t param_set_loop_timeout(char* val , char** err ) {
|
2013-06-27 03:41:48 +00:00
|
|
|
char* p;
|
2013-07-23 18:26:38 +00:00
|
|
|
int usec = (int)strtol(val, &p, 10); /* now usecs 2DO: "10ms" or "500us" or "1s" */
|
2013-06-27 03:41:48 +00:00
|
|
|
|
|
|
|
if (p<=val) {
|
|
|
|
*err = g_strdup("not an integer");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-07-01 01:49:34 +00:00
|
|
|
disp_loop_timeout_usec = usec;
|
2013-06-27 03:41:48 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-06-22 20:26:13 +00:00
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
static GString *comp_info_str;
|
|
|
|
static GString *runtime_info_str;
|
|
|
|
static const char* version_str = "Echld " VERSION;
|
|
|
|
static char* version_long_str = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
static char* param_get_long_version(char** err _U_) {
|
2013-11-08 11:50:57 +00:00
|
|
|
return g_strdup(version_long_str);
|
2013-07-05 23:57:07 +00:00
|
|
|
}
|
2013-07-01 01:49:34 +00:00
|
|
|
|
|
|
|
static char* param_get_version(char** err _U_) {
|
2013-07-05 23:57:07 +00:00
|
|
|
return g_strdup(version_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char* param_get_capture_types(char** err _U_) {
|
|
|
|
GString* str = g_string_new("");
|
|
|
|
int i;
|
|
|
|
|
2013-11-08 11:50:57 +00:00
|
|
|
for (i = 0; i < WTAP_NUM_FILE_TYPES_SUBTYPES; i++) {
|
2013-07-05 23:57:07 +00:00
|
|
|
if (wtap_dump_can_open(i)) {
|
|
|
|
g_string_append_printf(str,"%s: %s\n",
|
2013-11-18 07:25:12 +00:00
|
|
|
wtap_file_type_subtype_short_string(i), wtap_file_type_subtype_string(i));
|
2013-07-05 23:57:07 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-01 01:49:34 +00:00
|
|
|
|
2015-01-13 23:13:40 +00:00
|
|
|
return g_string_free(str,FALSE);
|
2013-07-05 23:57:07 +00:00
|
|
|
}
|
2013-07-01 01:49:34 +00:00
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
static echld_bool_t param_set_add_hosts_file(char* val, char** err) {
|
|
|
|
if (add_hosts_file(val)) {
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
*err = g_strdup_printf("Can't read host entries from \"%s\"",val);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
2013-07-01 01:49:34 +00:00
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
static echld_bool_t param_set_x_opt(char* val, char** err) {
|
|
|
|
if (ex_opt_add(val)) {
|
|
|
|
return TRUE;
|
|
|
|
} else {
|
|
|
|
*err = g_strdup_printf("Cannot set X opt '%s'",val);
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-07-01 01:49:34 +00:00
|
|
|
}
|
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2013-07-04 05:34:25 +00:00
|
|
|
static char* param_get_params(char** err _U_);
|
2013-06-22 20:26:13 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
static param_t disp_params[] = {
|
|
|
|
#ifdef DEBUG_DISPATCHER
|
2013-07-05 23:57:07 +00:00
|
|
|
PARAM(dbg_level,"0>int>5"),
|
2013-06-21 01:27:28 +00:00
|
|
|
# endif
|
2013-07-05 23:57:07 +00:00
|
|
|
RO_PARAM(long_version,"long version string"),
|
|
|
|
RO_PARAM(version,"version string"),
|
|
|
|
PARAM(loop_timeout,"main loop step timeout"),
|
|
|
|
RO_PARAM(interfaces,"interface information"),
|
|
|
|
RO_PARAM(capture_types,"the available capture types"),
|
|
|
|
WO_PARAM(add_hosts_file,"Add a hosts file"),
|
|
|
|
WO_PARAM(x_opt,"Set a -X option"),
|
|
|
|
RO_PARAM(params,"This List"),
|
2013-07-04 05:34:25 +00:00
|
|
|
{NULL,NULL,NULL,NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
static char* param_get_params(char** err _U_) {
|
2013-07-05 23:57:07 +00:00
|
|
|
return paramset_get_params_list(disp_params,PARAM_LIST_FMT);
|
2013-06-27 03:41:48 +00:00
|
|
|
}
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
static struct dispatcher_child* dispatcher_get_child(struct dispatcher* d, int chld_id) {
|
2013-06-21 01:27:28 +00:00
|
|
|
int i;
|
|
|
|
struct dispatcher_child* cc = d->children;
|
|
|
|
int max_children = d->max_children;
|
|
|
|
|
|
|
|
for(i = 0; i < max_children; i++) {
|
2013-06-22 20:26:13 +00:00
|
|
|
struct dispatcher_child* c = &(cc[i]);
|
2013-06-21 01:27:28 +00:00
|
|
|
if (c->chld_id == chld_id) return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void dispatcher_clear_child(struct dispatcher_child* c) {
|
|
|
|
echld_reset_reader(&(c->reader), -1, 4096);
|
2013-06-28 21:10:52 +00:00
|
|
|
c->chld_id = -1;
|
|
|
|
c->state = FREE;
|
|
|
|
c->reader.fd = -1;
|
|
|
|
c->write_fd = -1;
|
|
|
|
c->pid = -1;
|
2013-07-01 01:49:34 +00:00
|
|
|
c->reqh_id = -1;
|
2013-06-28 21:10:52 +00:00
|
|
|
c->closing = FALSE;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 19:00:01 +00:00
|
|
|
static void set_dumpcap_pid(int pid) {
|
2013-07-23 18:26:38 +00:00
|
|
|
|
2013-06-27 19:00:01 +00:00
|
|
|
dispatcher->dumpcap_pid = pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void preinit_epan(char* argv0, int (*main)(int, char **)) {
|
2013-07-05 23:57:07 +00:00
|
|
|
char *gdp_path, *dp_path;
|
2014-10-18 06:39:20 +00:00
|
|
|
#if 0
|
|
|
|
char *gpf_path, *pf_path;
|
|
|
|
int gpf_open_errno, gpf_read_errno;
|
|
|
|
int pf_open_errno, pf_read_errno;
|
|
|
|
#endif
|
2013-07-05 23:57:07 +00:00
|
|
|
int gdp_open_errno, gdp_read_errno;
|
|
|
|
int dp_open_errno, dp_read_errno;
|
2013-07-04 05:34:25 +00:00
|
|
|
char* error;
|
|
|
|
|
2015-02-20 00:23:39 +00:00
|
|
|
error = init_progfile_dir(argv0, (void *)main);
|
2013-06-27 19:00:01 +00:00
|
|
|
|
2014-12-31 06:28:25 +00:00
|
|
|
comp_info_str = get_compiled_version_info(NULL, epan_get_compiled_version_info);
|
2013-07-01 01:49:34 +00:00
|
|
|
|
2014-12-31 06:28:25 +00:00
|
|
|
runtime_info_str = get_runtime_version_info(NULL);
|
2013-07-01 01:49:34 +00:00
|
|
|
|
2014-06-22 01:54:53 +00:00
|
|
|
version_long_str = g_strdup_printf("Echld %s\n%s\n%s\n%s",
|
|
|
|
get_ws_vcs_version_info(), get_copyright_info(),
|
2013-07-05 23:57:07 +00:00
|
|
|
comp_info_str->str, runtime_info_str->str);
|
2013-06-27 19:00:01 +00:00
|
|
|
|
2013-07-04 05:34:25 +00:00
|
|
|
if (error) {
|
|
|
|
DISP_FATAL((CANNOT_PREINIT_EPAN,"Failed epan_preinit: msg='%s'",error));
|
2013-06-27 19:00:01 +00:00
|
|
|
}
|
|
|
|
|
2013-07-01 01:49:34 +00:00
|
|
|
/* Add it to the information to be reported on a crash. */
|
2014-06-22 01:02:27 +00:00
|
|
|
ws_add_crash_info("Echld %s\n%s\n%s",
|
2014-06-22 01:54:53 +00:00
|
|
|
get_ws_vcs_version_info(), comp_info_str->str, runtime_info_str->str);
|
2013-07-05 23:57:07 +00:00
|
|
|
|
|
|
|
init_stuff();
|
2013-11-08 11:50:57 +00:00
|
|
|
|
2013-06-27 19:00:01 +00:00
|
|
|
capture_sync_set_fetch_dumpcap_pid_cb(set_dumpcap_pid);
|
|
|
|
|
2013-11-08 11:50:57 +00:00
|
|
|
init_process_policies();
|
2013-07-04 05:34:25 +00:00
|
|
|
|
|
|
|
get_interfaces(&error);
|
2013-11-08 11:50:57 +00:00
|
|
|
|
2013-07-04 05:34:25 +00:00
|
|
|
if (error) {
|
|
|
|
DISP_FATAL((CANNOT_PREINIT_EPAN,"Error getting interfaces: %s", error));
|
|
|
|
}
|
|
|
|
|
|
|
|
prefs_apply_all();
|
2013-07-01 01:49:34 +00:00
|
|
|
|
2013-07-04 05:34:25 +00:00
|
|
|
/* disabled protocols as per configuration file */
|
|
|
|
set_disabled_protos_list();
|
2015-07-05 21:31:14 +00:00
|
|
|
set_disabled_heur_dissector_list();
|
2013-06-27 19:00:01 +00:00
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
setlocale(LC_ALL, "");
|
2013-07-23 18:26:38 +00:00
|
|
|
DISP_DBG((1,"---5"));
|
2013-07-05 23:57:07 +00:00
|
|
|
|
|
|
|
read_disabled_protos_list(&gdp_path, &gdp_open_errno, &gdp_read_errno, &dp_path, &dp_open_errno, &dp_read_errno);
|
2015-07-05 21:31:14 +00:00
|
|
|
read_disabled_heur_dissector_list(&gdp_path, &gdp_open_errno, &gdp_read_errno, &dp_path, &dp_open_errno, &dp_read_errno);
|
2013-07-23 18:26:38 +00:00
|
|
|
|
|
|
|
DISP_DBG((1,"---6"));
|
2013-06-27 19:00:01 +00:00
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
cap_file_init(&stuff.cfile);
|
2013-07-23 18:26:38 +00:00
|
|
|
DISP_DBG((1,"---7"));
|
2013-07-05 23:57:07 +00:00
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
DISP_DBG((1,"---8"));
|
2014-10-18 06:39:20 +00:00
|
|
|
timestamp_set_precision(TS_PREC_AUTO);
|
2013-07-23 18:26:38 +00:00
|
|
|
|
2014-10-18 06:39:20 +00:00
|
|
|
#if 0
|
|
|
|
sleep(10);
|
2013-07-23 18:26:38 +00:00
|
|
|
|
2014-10-18 06:39:20 +00:00
|
|
|
initialize_funnel_ops();
|
|
|
|
stuff.prefs = read_prefs(&gpf_open_errno, &gpf_read_errno, &gpf_path, &pf_open_errno, &pf_read_errno, &pf_path);
|
|
|
|
check 4 errors
|
|
|
|
#endif
|
2013-07-23 18:26:38 +00:00
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
DISP_DBG((2,"epan preinit done"));
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
static void dispatcher_clear(void) {
|
2013-06-28 21:10:52 +00:00
|
|
|
DISP_DBG((2,"dispatcher_clear"));
|
2013-06-21 01:27:28 +00:00
|
|
|
/* remove unnecessary stuff for the working child */
|
2013-07-05 23:57:07 +00:00
|
|
|
/* remove signal handlers */
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
void dispatcher_sig(int sig) {
|
|
|
|
DISP_FATAL((TERMINATED,"SIG sig=%d",sig));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
void dispatcher_reaper(int sig) {
|
|
|
|
int status;
|
|
|
|
int i;
|
|
|
|
struct dispatcher_child* cc = dispatcher->children;
|
|
|
|
int max_children = dispatcher->max_children;
|
|
|
|
int pid = waitpid(-1, &status, WNOHANG);
|
2013-11-08 11:50:57 +00:00
|
|
|
int reqh_id_save = dispatcher->reqh_id;
|
2013-06-27 19:00:01 +00:00
|
|
|
|
|
|
|
dispatcher->reqh_id = 0;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
if (sig != SIGCHLD) {
|
|
|
|
DISP_DBG((1,"Reaper got wrong signal=%d",sig));
|
2013-06-27 19:00:01 +00:00
|
|
|
dispatcher->reqh_id = reqh_id_save;
|
2013-06-22 20:26:13 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DISP_DBG((2,"Child dead pid=%d",pid));
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
for(i = 0; i < max_children; i++) {
|
|
|
|
struct dispatcher_child* c = &(cc[i]);
|
|
|
|
if ( c->pid == pid ) {
|
|
|
|
if (c->closing || dispatcher->closing) {
|
2013-07-01 01:49:34 +00:00
|
|
|
DISP_WRITE(dispatcher->parent_out, NULL, c->chld_id, ECHLD_CLOSING, c->reqh_id);
|
2013-06-21 01:27:28 +00:00
|
|
|
} else {
|
2013-06-22 20:26:13 +00:00
|
|
|
char* s = NULL;
|
2013-07-01 01:49:34 +00:00
|
|
|
GByteArray* em;
|
2013-06-22 20:26:13 +00:00
|
|
|
|
|
|
|
if (WIFEXITED(status)) {
|
|
|
|
s = g_strdup_printf(
|
2013-11-08 11:50:57 +00:00
|
|
|
"Unexpected dead: reason='exited' pid=%d status=%d",
|
|
|
|
pid, WEXITSTATUS(status));
|
2013-06-22 20:26:13 +00:00
|
|
|
} else if ( WIFSIGNALED(status) ) {
|
|
|
|
s = g_strdup_printf(
|
2013-11-08 11:50:57 +00:00
|
|
|
"Unexpected dead: reason='signaled' pid=%d termsig=%d coredump=%s",
|
|
|
|
pid, WTERMSIG(status), WCOREDUMP(status) ? "yes":"no");
|
2013-06-22 20:26:13 +00:00
|
|
|
|
|
|
|
/*if (WCOREDUMP(status)) { system("analyze_coredump.sh pid=%d") } */
|
|
|
|
|
|
|
|
} else if (WIFSTOPPED(status)) {
|
|
|
|
s = g_strdup_printf(
|
2013-11-08 11:50:57 +00:00
|
|
|
"Unexpected dead: reason='stopped' pid=%d stopsig=%d",
|
|
|
|
pid, WSTOPSIG(status));
|
2013-06-22 20:26:13 +00:00
|
|
|
}
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
em = dispatcher->enc.to_parent->child_dead(s);
|
|
|
|
dispatcher_err(ECHLD_ERR_CRASHED_CHILD, s);
|
|
|
|
if (s) g_free(s);
|
2013-07-01 01:49:34 +00:00
|
|
|
DISP_WRITE(dispatcher->parent_out, em, c->chld_id, ECHLD_CHILD_DEAD, 0);
|
|
|
|
if (em) g_byte_array_free(em,TRUE);
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
CHLD_SET_STATE(c,CLOSED);
|
2013-06-21 01:27:28 +00:00
|
|
|
dispatcher_clear_child(c);
|
2013-06-27 19:00:01 +00:00
|
|
|
dispatcher->reqh_id = reqh_id_save;
|
2013-06-21 01:27:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 19:00:01 +00:00
|
|
|
if (pid == dispatcher->dumpcap_pid) {
|
|
|
|
dispatcher->dumpcap_pid = 0;
|
|
|
|
dispatcher->reqh_id = reqh_id_save;
|
2013-06-28 21:10:52 +00:00
|
|
|
DISP_DBG((2,"dumpcap dead pid=%d",pid));
|
2013-06-27 19:00:01 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-12-23 15:53:13 +00:00
|
|
|
dispatcher_err(ECHLD_ERR_UNKNOWN_PID, "Unknown child pid: %d", pid);
|
2013-06-27 19:00:01 +00:00
|
|
|
dispatcher->reqh_id = reqh_id_save;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
static void dispatcher_destroy(void) {
|
2013-06-21 01:27:28 +00:00
|
|
|
/* destroy the dispatcher stuff at closing */
|
|
|
|
|
|
|
|
dispatcher->closing = TRUE;
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
children_massacre();
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
exit(0);
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* stuff coming from child going to parent */
|
2013-06-22 20:26:13 +00:00
|
|
|
static long dispatch_to_parent(guint8* b, size_t len, echld_chld_id_t chld_id, echld_msg_type_t type, echld_reqh_id_t reqh_id, void* data) {
|
2013-06-21 01:27:28 +00:00
|
|
|
/* TODO: timeouts, clear them */
|
|
|
|
/* TODO: keep stats */
|
2013-06-22 20:26:13 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
GByteArray in_ba;
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
struct dispatcher_child* c = (struct dispatcher_child*)data;
|
|
|
|
|
2013-07-01 01:49:34 +00:00
|
|
|
dispatcher->reqh_id = c->reqh_id = reqh_id;
|
2013-11-08 11:50:57 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
in_ba.data = b;
|
2013-06-22 20:26:13 +00:00
|
|
|
in_ba.len = (guint)len;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
if (chld_id != c->chld_id) {
|
|
|
|
goto misbehabing;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case ECHLD_ERROR: break;
|
|
|
|
case ECHLD_TIMED_OUT: break;
|
2013-06-28 21:10:52 +00:00
|
|
|
case ECHLD_HELLO: CHLD_SET_STATE(c,IDLE); break;
|
2013-07-01 01:49:34 +00:00
|
|
|
case ECHLD_CLOSING:
|
|
|
|
c->closing = TRUE;
|
|
|
|
CHLD_SET_STATE(c,CLOSING);
|
|
|
|
break;
|
2013-06-21 01:27:28 +00:00
|
|
|
case ECHLD_PARAM: break;
|
|
|
|
case ECHLD_PONG: break;
|
2013-06-28 21:10:52 +00:00
|
|
|
case ECHLD_FILE_OPENED: CHLD_SET_STATE(c,READING); break;
|
|
|
|
case ECHLD_INTERFACE_OPENED: CHLD_SET_STATE(c,READY); break;
|
|
|
|
case ECHLD_CAPTURE_STARTED: CHLD_SET_STATE(c,CAPTURING); break;
|
|
|
|
case ECHLD_NOTIFY: break;
|
|
|
|
case ECHLD_PACKET_SUM: break;
|
|
|
|
case ECHLD_TREE: break;
|
|
|
|
case ECHLD_BUFFER: break;
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
case ECHLD_EOF:
|
2013-06-28 21:10:52 +00:00
|
|
|
case ECHLD_CAPTURE_STOPPED: CHLD_SET_STATE(c,DONE); break;
|
|
|
|
|
2013-11-08 11:50:57 +00:00
|
|
|
case ECHLD_NOTE_ADDED: break;
|
2013-06-28 21:10:52 +00:00
|
|
|
case ECHLD_PACKET_LIST: break;
|
2013-06-21 01:27:28 +00:00
|
|
|
case ECHLD_FILE_SAVED: break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
goto misbehabing;
|
|
|
|
}
|
2013-11-08 11:50:57 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
DISP_DBG((4,"Dispatching to parent reqh_id=%d chld_id=%d type='%c'",reqh_id,c->chld_id,type));
|
2013-06-27 03:41:48 +00:00
|
|
|
return DISP_WRITE(dispatcher->parent_out, &in_ba, chld_id, type, reqh_id);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
misbehabing:
|
2013-06-28 21:10:52 +00:00
|
|
|
CHLD_SET_STATE(c,ERRORED);
|
2013-06-21 01:27:28 +00:00
|
|
|
c->closing = TRUE;
|
|
|
|
kill(c->pid,SIGTERM);
|
|
|
|
dispatcher_err(ECHLD_ERR_CRASHED_CHILD,"chld_id=%d",chld_id);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-11-08 11:50:57 +00:00
|
|
|
static struct timeval start_wait_time;
|
2013-06-28 21:10:52 +00:00
|
|
|
static long start_wait_time_us = CHILD_START_WAIT_TIME;
|
|
|
|
|
|
|
|
static void detach_new_child(enc_msg_t* em, echld_chld_id_t chld_id) {
|
|
|
|
struct dispatcher_child* c;
|
|
|
|
int reqh_id = dispatcher->reqh_id;
|
2013-11-08 11:50:57 +00:00
|
|
|
int pid;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
if (( c = dispatcher_get_child(dispatcher, chld_id) )) {
|
|
|
|
dispatcher_err(ECHLD_ERR_CHILD_EXISTS,"chld_id=%d exists already while creating new child",chld_id);
|
|
|
|
return;
|
|
|
|
} else if (( c = dispatcher_get_child(dispatcher, -1) )) {
|
|
|
|
int disp_pipe_fds[2];
|
2013-06-21 01:27:28 +00:00
|
|
|
int child_pipe_fds[2];
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
int pipe_to_disp;
|
|
|
|
int pipe_from_disp;
|
2013-06-21 01:27:28 +00:00
|
|
|
int pipe_to_child;
|
|
|
|
int pipe_from_child;
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
DISP_DBG((5,"new_child pipe(dispatcher)"));
|
|
|
|
if( pipe(disp_pipe_fds) < 0) {
|
2014-10-18 06:39:20 +00:00
|
|
|
dispatcher_err(ECHLD_ERR_CANNOT_FORK,"CANNOT OPEN PARENT PIPE: %s",g_strerror(errno));
|
2013-06-21 01:27:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
pipe_from_disp = disp_pipe_fds[0];
|
|
|
|
pipe_to_child = disp_pipe_fds[1];
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
DISP_DBG((5,"new_child pipe(child)"));
|
2013-06-21 01:27:28 +00:00
|
|
|
if( pipe(child_pipe_fds) < 0) {
|
2013-06-28 21:10:52 +00:00
|
|
|
close(pipe_from_disp);
|
2013-06-21 01:27:28 +00:00
|
|
|
close(pipe_to_child);
|
2014-10-18 06:39:20 +00:00
|
|
|
dispatcher_err(ECHLD_ERR_CANNOT_FORK,"CANNOT OPEN CHILD PIPE: %s",g_strerror(errno));
|
2013-06-21 01:27:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
pipe_from_child = child_pipe_fds[0];
|
2013-06-28 21:10:52 +00:00
|
|
|
pipe_to_disp = child_pipe_fds[1];
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
DISP_DBG((4,"New Child Forking()"));
|
2013-06-21 01:27:28 +00:00
|
|
|
switch (( pid = fork() )) {
|
|
|
|
case -1: {
|
|
|
|
close(pipe_to_child);
|
2013-06-28 21:10:52 +00:00
|
|
|
close(pipe_to_disp);
|
2013-06-21 01:27:28 +00:00
|
|
|
close(pipe_from_child);
|
2013-06-28 21:10:52 +00:00
|
|
|
close(pipe_from_disp);
|
2014-10-18 06:39:20 +00:00
|
|
|
dispatcher_err(ECHLD_ERR_CANNOT_FORK,"CANNOT FORK: %s",g_strerror(errno));
|
2013-06-21 01:27:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-06-28 21:10:52 +00:00
|
|
|
case 0: {
|
|
|
|
/* I'm the child */
|
2013-06-22 20:26:13 +00:00
|
|
|
dispatcher_clear();
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
close(pipe_to_child);
|
|
|
|
close(pipe_from_child);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
echld_child_initialize(chld_id, pipe_from_disp,pipe_to_disp,reqh_id,&stuff);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
exit( echld_child_loop() );
|
|
|
|
|
|
|
|
/* it won't */
|
2013-11-08 11:50:57 +00:00
|
|
|
return;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
default: {
|
2013-06-28 21:10:52 +00:00
|
|
|
/* I'm the parent */
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
close(pipe_to_disp);
|
|
|
|
close(pipe_from_disp);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
echld_reset_reader(&(c->reader), pipe_from_child,4096);
|
|
|
|
c->write_fd = pipe_to_child;
|
|
|
|
c->pid = pid;
|
2013-06-28 21:10:52 +00:00
|
|
|
c->chld_id = chld_id;
|
|
|
|
c->closing = FALSE;
|
|
|
|
|
2013-06-29 01:16:48 +00:00
|
|
|
CHLD_SET_STATE(c,CREATING);
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
DISP_DBG((4,"Child Forked pid=%d chld_id=%d from_fd=%d to_fd=%d",
|
2013-06-29 01:16:48 +00:00
|
|
|
pid, c->chld_id, pipe_from_child, pipe_to_child));
|
2013-06-27 03:41:48 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
start_wait_time.tv_sec = (int)(start_wait_time_us / 1000000);
|
|
|
|
start_wait_time.tv_usec = (int)(start_wait_time_us % 1000000);
|
|
|
|
|
|
|
|
select(0,NULL,NULL,NULL,&start_wait_time);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
/* configure child */
|
2013-06-28 21:10:52 +00:00
|
|
|
DISP_WRITE(pipe_to_child, em, c->chld_id, ECHLD_NEW_CHILD, dispatcher->reqh_id);
|
2013-06-21 01:27:28 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dispatcher_err(ECHLD_ERR_CANNOT_FORK, "MAX CHILDREN REACHED: max_children=%d",dispatcher->max_children);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* process signals sent from parent */
|
2013-06-28 21:10:52 +00:00
|
|
|
static long dispatch_to_child(guint8* b, size_t len, echld_chld_id_t chld_id, echld_msg_type_t type, echld_reqh_id_t reqh_id, void* data _U_) {
|
|
|
|
GByteArray in_ba;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
in_ba.data = b;
|
|
|
|
in_ba.len = (guint)len;
|
2013-06-22 20:26:13 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
dispatcher->reqh_id = reqh_id;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
DISP_DBG((1,"RCV<- type='%s' chld_id=%d reqh_id=%d",TY(type),chld_id,reqh_id));
|
|
|
|
|
|
|
|
if (chld_id == 0) { /* these are messages sent to the dispatcher itself */
|
|
|
|
DISP_DBG((2,"Message to Dispatcher"));
|
2013-06-21 01:27:28 +00:00
|
|
|
switch(type) {
|
|
|
|
case ECHLD_CLOSE_CHILD:
|
|
|
|
dispatcher_destroy();
|
|
|
|
return 0;
|
2013-11-08 11:50:57 +00:00
|
|
|
case ECHLD_PING:
|
2013-06-27 03:41:48 +00:00
|
|
|
DISP_DBG((2,"PONG reqh_id=%d",reqh_id));
|
2013-06-28 21:10:52 +00:00
|
|
|
DISP_WRITE(dispatcher->parent_out, NULL, chld_id, ECHLD_PONG, reqh_id);
|
2013-06-21 01:27:28 +00:00
|
|
|
return 0;
|
|
|
|
case ECHLD_SET_PARAM:{
|
|
|
|
char* param;
|
|
|
|
char* value;
|
2013-06-28 21:10:52 +00:00
|
|
|
if ( dispatcher->dec.from_parent->set_param(b,len,¶m,&value) ) {
|
2013-06-21 01:27:28 +00:00
|
|
|
GByteArray* ba;
|
|
|
|
char* err;
|
2013-07-05 23:57:07 +00:00
|
|
|
if (! paramset_apply_set (disp_params, param, value, &err) ) {
|
|
|
|
dispatcher_err(ECHLD_CANNOT_SET_PARAM,"%s",err);
|
2013-06-21 01:27:28 +00:00
|
|
|
g_free(err);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
ba = dispatcher->enc.to_parent->param(param,value);
|
2013-06-21 01:27:28 +00:00
|
|
|
DISP_RESP(ba,ECHLD_PARAM);
|
|
|
|
g_byte_array_free(ba,TRUE);
|
|
|
|
DISP_DBG((1,"Set Param: param='%s' value='%s'",param,value));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
dispatcher_err(ECHLD_CANNOT_SET_PARAM,"reason='decoder error'");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case ECHLD_GET_PARAM: {
|
|
|
|
GByteArray* ba;
|
|
|
|
char* param;
|
2013-06-28 21:10:52 +00:00
|
|
|
if ( dispatcher->dec.from_parent->get_param(b,len,¶m) ) {
|
2013-06-21 01:27:28 +00:00
|
|
|
char* err;
|
|
|
|
char* val;
|
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
if (! (val = paramset_apply_get (disp_params, param, &err)) ) {
|
|
|
|
dispatcher_err(ECHLD_CANNOT_GET_PARAM,"%s",err);
|
2013-06-21 01:27:28 +00:00
|
|
|
g_free(err);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-11-08 11:50:57 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
ba = dispatcher->enc.to_parent->param(param,val);
|
2013-06-21 01:27:28 +00:00
|
|
|
DISP_RESP(ba,ECHLD_PARAM);
|
|
|
|
g_byte_array_free(ba,TRUE);
|
2013-06-28 21:10:52 +00:00
|
|
|
DISP_DBG((1,"Get Param: param='%s' value='%s'",param,val));
|
2013-06-21 01:27:28 +00:00
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
dispatcher_err(ECHLD_CANNOT_GET_PARAM,"reason='decoder error'");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
dispatcher_err(ECHLD_ERR_WRONG_MSG, "wrong message to dispatcher type='%c'", type);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
struct dispatcher_child* c;
|
|
|
|
|
2013-06-27 19:00:01 +00:00
|
|
|
DISP_DBG((2,"Parent => Child"));
|
2013-06-27 03:41:48 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
if (! (c = dispatcher_get_child(dispatcher, chld_id)) ) {
|
2013-06-28 21:10:52 +00:00
|
|
|
if (type == ECHLD_NEW_CHILD) {
|
|
|
|
detach_new_child(&in_ba,chld_id);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
dispatcher_err(ECHLD_ERR_NO_SUCH_CHILD, "wrong chld_id %d", chld_id);
|
2013-06-21 01:27:28 +00:00
|
|
|
return 0;
|
2013-06-28 21:10:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch(type) {
|
2013-11-08 11:50:57 +00:00
|
|
|
case ECHLD_CLOSE_CHILD:
|
2013-06-28 21:10:52 +00:00
|
|
|
CHLD_SET_STATE(c,CLOSED);
|
|
|
|
goto relay_frame;
|
|
|
|
|
|
|
|
case ECHLD_OPEN_FILE:
|
|
|
|
CHLD_SET_STATE(c,READING);
|
|
|
|
goto relay_frame;
|
|
|
|
|
|
|
|
case ECHLD_OPEN_INTERFACE:
|
|
|
|
CHLD_SET_STATE(c,READY);
|
|
|
|
goto relay_frame;
|
|
|
|
|
|
|
|
case ECHLD_START_CAPTURE:
|
|
|
|
CHLD_SET_STATE(c,CAPTURING);
|
|
|
|
goto relay_frame;
|
|
|
|
|
|
|
|
case ECHLD_STOP_CAPTURE:
|
|
|
|
CHLD_SET_STATE(c,DONE);
|
|
|
|
goto relay_frame;
|
|
|
|
|
|
|
|
case ECHLD_SAVE_FILE:
|
|
|
|
case ECHLD_APPLY_FILTER:
|
|
|
|
case ECHLD_SET_PARAM:
|
|
|
|
case ECHLD_GET_PARAM:
|
|
|
|
case ECHLD_PING:
|
|
|
|
case ECHLD_GET_SUM:
|
|
|
|
case ECHLD_GET_TREE:
|
|
|
|
case ECHLD_GET_BUFFER:
|
|
|
|
case ECHLD_ADD_NOTE:
|
|
|
|
relay_frame: {
|
|
|
|
DISP_DBG((3,"Relay to Child chld_id=%d type='%c' req_id=%d",chld_id, type, reqh_id));
|
|
|
|
return DISP_WRITE(c->write_fd, &in_ba, chld_id, type, reqh_id);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
dispatcher_err(ECHLD_ERR_WRONG_MSG, "wrong message %d %c", reqh_id, type);
|
|
|
|
return 0;
|
|
|
|
}
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
int dispatcher_loop(void) {
|
2013-06-21 01:27:28 +00:00
|
|
|
int parent_out = dispatcher->parent_out;
|
|
|
|
int parent_in = dispatcher->parent_in.fd;
|
|
|
|
struct dispatcher_child* children = dispatcher->children;
|
2013-06-27 03:41:48 +00:00
|
|
|
|
|
|
|
DISP_DBG((5,"LOOP in_fd=%d out_fd=%d",parent_in, parent_out));
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
fd_set rfds;
|
|
|
|
fd_set efds;
|
|
|
|
struct dispatcher_child* c;
|
|
|
|
int nfds;
|
2013-06-27 03:41:48 +00:00
|
|
|
int nchld = 0;
|
2013-07-01 01:49:34 +00:00
|
|
|
struct timeval disp_loop_timeout;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
FD_ZERO(&rfds);
|
|
|
|
FD_ZERO(&efds);
|
|
|
|
|
|
|
|
FD_SET(parent_in,&rfds);
|
|
|
|
FD_SET(parent_in,&efds);
|
|
|
|
FD_SET(parent_out,&efds);
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
for (c = children; c->pid; c++) {
|
|
|
|
if (c->chld_id > 0) {
|
|
|
|
nchld++;
|
2013-06-21 01:27:28 +00:00
|
|
|
FD_SET(c->reader.fd, &rfds);
|
|
|
|
FD_SET(c->reader.fd, &efds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-01 01:49:34 +00:00
|
|
|
DISP_DBG((4,"Select()ing nchld=%d",nchld,disp_loop_timeout.tv_usec));
|
|
|
|
|
|
|
|
disp_loop_timeout.tv_sec = (int)(disp_loop_timeout_usec / 1000000);
|
|
|
|
disp_loop_timeout.tv_usec = (int)(disp_loop_timeout_usec % 1000000);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
nfds = select(FD_SETSIZE, &rfds, NULL, &efds, &disp_loop_timeout);
|
|
|
|
|
2013-07-01 01:49:34 +00:00
|
|
|
DISP_DBG((5,"Select()ed nfds=%d",nchld,nfds));
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
if (nfds < 0) {
|
2014-10-18 06:39:20 +00:00
|
|
|
DISP_DBG((1,"select error='%s'",g_strerror(errno) ));
|
2013-06-27 03:41:48 +00:00
|
|
|
continue;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-07-01 01:49:34 +00:00
|
|
|
if ( FD_ISSET(parent_in, &rfds)) {
|
2013-06-22 20:26:13 +00:00
|
|
|
long st = echld_read_frame(&(dispatcher->parent_in), dispatch_to_child, dispatcher);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
if (st < 0) {
|
2013-06-27 03:41:48 +00:00
|
|
|
DISP_DBG((1,"read frame returning < 0 for parent"));
|
|
|
|
/* XXX: ??? */
|
2013-06-21 01:27:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
if ( FD_ISSET(parent_in, &efds) ) {
|
|
|
|
DISP_DBG((1,"Parent In Pipe Errored!"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( FD_ISSET(parent_out, &efds) ) {
|
|
|
|
DISP_DBG((1,"Parent Out Pipe Errored!"));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
for (c=children; c->pid; c++) {
|
2013-06-28 21:10:52 +00:00
|
|
|
if (c->reader.fd > 0) {
|
|
|
|
if ( FD_ISSET(c->reader.fd,&efds) ) {
|
|
|
|
struct timeval wait_time;
|
|
|
|
wait_time.tv_sec = 0;
|
|
|
|
wait_time.tv_usec = DISP_KILLED_CHILD_WAIT;
|
|
|
|
|
|
|
|
DISP_DBG((1,"errored child pipe chld_id=%d",c->chld_id));
|
|
|
|
kill(c->pid,SIGTERM);
|
|
|
|
select(0,NULL,NULL,NULL,&wait_time);
|
|
|
|
dispatcher_clear_child(c);
|
2013-11-08 11:50:57 +00:00
|
|
|
continue;
|
2013-06-28 21:10:52 +00:00
|
|
|
}
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
if (FD_ISSET(c->reader.fd,&rfds)) {
|
2013-06-22 20:26:13 +00:00
|
|
|
long st = echld_read_frame(&(c->reader), dispatch_to_parent, c);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
if (st < 0) {
|
2013-06-27 03:41:48 +00:00
|
|
|
DISP_DBG((1,"read_frame returned < 0 for chld_id=%d",c->chld_id));
|
|
|
|
/* XXX */
|
2013-06-21 01:27:28 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(1);
|
|
|
|
|
|
|
|
/* won't */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-01 01:49:34 +00:00
|
|
|
void dispatcher_alrm(int sig _U_) {
|
|
|
|
DISP_DBG((1,"ALRM received"));
|
|
|
|
}
|
2013-06-27 03:41:48 +00:00
|
|
|
|
2013-06-27 19:00:01 +00:00
|
|
|
void echld_dispatcher_start(int* in_pipe_fds, int* out_pipe_fds, char* argv0, int (*main)(int, char **)) {
|
2013-06-21 01:27:28 +00:00
|
|
|
static struct dispatcher d;
|
2013-06-28 21:10:52 +00:00
|
|
|
int i;
|
2013-06-27 03:41:48 +00:00
|
|
|
|
|
|
|
DISP_DBG_INIT();
|
|
|
|
DISP_DBG((2,"Dispatcher Starting"));
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
signal(SIGCHLD,dispatcher_reaper);
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
signal(SIGTERM,dispatcher_sig);
|
|
|
|
signal(SIGPIPE,dispatcher_sig);
|
|
|
|
signal(SIGINT,SIG_IGN);
|
|
|
|
signal(SIGCONT,SIG_IGN);
|
2013-07-01 01:49:34 +00:00
|
|
|
signal(SIGABRT,dispatcher_sig);
|
|
|
|
signal(SIGHUP,dispatcher_sig);
|
|
|
|
signal(SIGALRM,dispatcher_alrm);
|
2013-06-27 03:41:48 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
dispatcher = &d;
|
|
|
|
|
|
|
|
echld_init_reader(&(d.parent_in),in_pipe_fds[0],4096);
|
|
|
|
d.parent_out = out_pipe_fds[1];
|
2013-06-22 20:26:13 +00:00
|
|
|
d.children = g_new0(struct dispatcher_child,ECHLD_MAX_CHILDREN);
|
2013-06-21 01:27:28 +00:00
|
|
|
d.max_children = ECHLD_MAX_CHILDREN;
|
|
|
|
d.nchildren = 0;
|
|
|
|
d.reqh_id = -1;
|
|
|
|
d.pid = getpid();
|
2013-06-27 19:00:01 +00:00
|
|
|
d.dumpcap_pid = 0;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
for (i=0;i<ECHLD_MAX_CHILDREN;i++) dispatcher_clear_child(&(d.children[i]));
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
close(out_pipe_fds[0]);
|
|
|
|
close(in_pipe_fds[1]);
|
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_get_all_codecs(&(d.enc.to_parent), &(d.dec.from_parent), &(d.enc.to_child), &(d.dec.from_child));
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
DISP_DBG((2,"Dispatcher Configured pid=%d parent_in=%d parent_out=%d",d.pid,in_pipe_fds[0],d.parent_out));
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 19:00:01 +00:00
|
|
|
preinit_epan(argv0,main);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
DISP_WRITE(dispatcher->parent_out, NULL, 0, ECHLD_HELLO, 0);
|
2013-06-21 01:27:28 +00:00
|
|
|
exit(dispatcher_loop());
|
|
|
|
}
|
|
|
|
|
2014-07-28 22:04:00 +00:00
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local variables:
|
|
|
|
* c-basic-offset: 4
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: t
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* vi: set shiftwidth=4 tabstop=8 noexpandtab:
|
|
|
|
* :indentSize=4:tabSize=8:noTabs=false:
|
|
|
|
*/
|