2013-07-23 18:26:38 +00:00
|
|
|
/* parent.c
|
2013-06-21 01:27:28 +00:00
|
|
|
* epan working child API internals
|
2013-07-23 18:26:38 +00:00
|
|
|
* Parent process routines and definitions
|
2013-06-21 01:27:28 +00:00
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-07-23 13:23:40 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
#include "echld-int.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
PARENT and API
|
|
|
|
**/
|
|
|
|
|
|
|
|
#define MAX_PENDING_REQS 16;
|
|
|
|
|
|
|
|
typedef struct _req {
|
|
|
|
int reqh_id;
|
|
|
|
echld_msg_cb_t cb;
|
|
|
|
void* cb_data;
|
|
|
|
struct timeval tv;
|
|
|
|
} reqh_t;
|
|
|
|
|
|
|
|
typedef struct _hdlr {
|
|
|
|
int id;
|
|
|
|
echld_msg_type_t type;
|
|
|
|
echld_msg_cb_t cb;
|
|
|
|
void* cb_data;
|
|
|
|
} hdlr_t;
|
|
|
|
|
|
|
|
typedef struct _echld_child {
|
|
|
|
int chld_id;
|
|
|
|
void* data;
|
2013-07-23 18:26:38 +00:00
|
|
|
echld_new_cb_t cb;
|
2013-06-21 01:27:28 +00:00
|
|
|
child_state_t state;
|
|
|
|
GArray* handlers;
|
|
|
|
GArray* reqs;
|
|
|
|
} echld_t;
|
|
|
|
|
|
|
|
struct _echld_parent {
|
|
|
|
echld_t* children;
|
|
|
|
echld_reader_t reader;
|
|
|
|
int dispatcher_fd;
|
|
|
|
int dispatcher_pid;
|
|
|
|
int reqh_id;
|
|
|
|
GByteArray* snd;
|
|
|
|
int closing;
|
|
|
|
echld_parent_encoder_t* enc;
|
|
|
|
parent_decoder_t* dec;
|
|
|
|
} parent = {NULL,{NULL,0,NULL,-1,NULL,0},-1,-1,1,NULL,0,NULL,NULL};
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
|
|
|
|
static int reqh_ids = 1;
|
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_PARENT
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
static int dbg_level = DEBUG_PARENT;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
static void parent_dbg(int level, const char* fmt, ...) {
|
2013-06-21 01:27:28 +00:00
|
|
|
va_list ap;
|
|
|
|
char str[1024];
|
|
|
|
|
|
|
|
if (level > dbg_level) return;
|
|
|
|
|
|
|
|
va_start(ap,fmt);
|
2013-06-25 17:43:52 +00:00
|
|
|
g_vsnprintf(str,1024,fmt,ap);
|
2013-06-21 01:27:28 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
fprintf(stderr,"ParentDebug: level=%d msg='%s'\n",level,str);
|
2013-06-25 17:43:52 +00:00
|
|
|
fflush(stderr);
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
2013-06-22 20:26:13 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
#define PARENT_DBG(attrs) parent_dbg attrs
|
2013-06-28 21:10:52 +00:00
|
|
|
#define PARENT_SEND(BYTEARR,CHILDNUM,TYPE,R_ID) do { long st = echld_write_frame(parent.dispatcher_fd, BYTEARR, CHILDNUM, TYPE, R_ID, NULL); PARENT_DBG((1,"SEND type='%s' chld_id=%d reqh_id=%d err_msg='%s'",TY(TYPE),CHILDNUM,R_ID, ( st >= 8 ? "ok" : ((st<0)?strerror(errno):"?") ) )); } while(0)
|
2013-06-22 20:26:13 +00:00
|
|
|
|
2013-07-23 13:23:40 +00:00
|
|
|
#else
|
|
|
|
#define PARENT_DBG(attrs)
|
2013-06-27 03:41:48 +00:00
|
|
|
#define PARENT_SEND(BYTEARR,CHILDNUM,TYPE,R_ID) echld_write_frame(parent.dispatcher_fd, BYTEARR, CHILDNUM, TYPE, R_ID, NULL)
|
2013-06-21 01:27:28 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern void echld_set_parent_dbg_level(int lvl) {
|
2013-06-25 17:43:52 +00:00
|
|
|
(dbg_level = lvl);
|
2013-06-27 03:41:48 +00:00
|
|
|
if (lvl > 6) {
|
2013-06-27 19:00:01 +00:00
|
|
|
echld_common_set_dbg(lvl,stderr,"parent");
|
2013-06-27 03:41:48 +00:00
|
|
|
}
|
2013-06-25 17:43:52 +00:00
|
|
|
PARENT_DBG((0,"Debug Level Set: %d",lvl));
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 17:43:52 +00:00
|
|
|
|
|
|
|
#define PARENT_FATAL(attrs) parent_fatal attrs
|
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
static void parent_fatal(int exit_code, const char* fmt, ...) {
|
|
|
|
va_list ap;
|
|
|
|
char str[1024];
|
|
|
|
|
|
|
|
va_start(ap,fmt);
|
2013-06-25 17:43:52 +00:00
|
|
|
g_vsnprintf(str,1024,fmt,ap);
|
2013-06-21 01:27:28 +00:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
#ifdef DEBUG_PARENT
|
2013-06-25 17:43:52 +00:00
|
|
|
PARENT_DBG((0,"Fatal error: exit_code=%d str=%s",exit_code,str));
|
2013-06-21 01:27:28 +00:00
|
|
|
#else
|
2013-06-25 17:43:52 +00:00
|
|
|
fprintf(stderr,"Fatal error: exit_code=%d str=%s",exit_code,str);
|
2013-06-21 01:27:28 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
kill(parent.dispatcher_pid,SIGTERM);
|
2013-06-21 01:27:28 +00:00
|
|
|
exit(exit_code);
|
|
|
|
}
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
static void echld_cleanup(void) {
|
2013-06-27 03:41:48 +00:00
|
|
|
// int i;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_DBG((4,"echld_cleanup starting"));
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
// for (i=0;i<ECHLD_MAX_CHILDREN;i++) {
|
|
|
|
// if ( parent.children[i].handlers ) g_array_free(parent.children[i].handlers,TRUE);
|
|
|
|
// if ( parent.children[i].reqs ) g_array_free(parent.children[i].reqs,TRUE);
|
|
|
|
// };
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
// g_free(parent.children);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
// g_byte_array_free(parent.snd,TRUE);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_DBG((3,"echld_cleanup done"));
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
static void parent_child_cleanup(echld_t* c) {
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
PARENT_DBG((2,"cleanup chld_id=%d",c->chld_id));
|
|
|
|
c->chld_id = -1;
|
|
|
|
c->data = NULL;
|
|
|
|
c->state = FREE;
|
2013-06-22 20:26:13 +00:00
|
|
|
g_array_set_size(c->handlers,0);
|
|
|
|
g_array_set_size(c->reqs,0);
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void parent_reaper(int sig) {
|
|
|
|
int pid;
|
|
|
|
int status;
|
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
if (sig == SIGCHLD) {
|
2013-06-21 01:27:28 +00:00
|
|
|
PARENT_FATAL((3333,"Must be SIGCHLD!"));
|
|
|
|
}
|
|
|
|
|
|
|
|
pid = waitpid(-1, &status, WNOHANG);
|
|
|
|
PARENT_DBG((2,"SIGCHLD pid=%d",pid));
|
|
|
|
|
|
|
|
if (pid == parent.dispatcher_pid) {
|
|
|
|
|
|
|
|
if (! parent.closing) {
|
|
|
|
/* crashed */
|
2013-07-23 18:26:38 +00:00
|
|
|
sleep(120);
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_FATAL((DISPATCHER_DEAD,"Dispatcher process dead"));
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
/* XXX: do we care? */
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
static echld_bool_t hello_cb(echld_msg_type_t type, enc_msg_t* msg_buff, void* ud) {
|
|
|
|
echld_init_t* init = (echld_init_t*)ud;
|
|
|
|
char* err = NULL;
|
|
|
|
int errnum = 0;
|
|
|
|
|
|
|
|
if (init && init->dispatcher_hello_cb) {
|
|
|
|
switch(type) {
|
|
|
|
case ECHLD_ERROR:
|
|
|
|
parent.dec->error(msg_buff, &errnum ,&err);
|
|
|
|
break;
|
2013-07-23 20:45:25 +00:00
|
|
|
case ECHLD_TIMED_OUT:
|
2013-07-23 18:26:38 +00:00
|
|
|
err = g_strdup("timedout");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
err = g_strdup_printf("Wrong MSG 'HELLO' expected, got '%s",TY(type));
|
|
|
|
break;
|
|
|
|
case ECHLD_HELLO:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
init->dispatcher_hello_cb(ud,err);
|
|
|
|
if (err) g_free(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
/* will initialize epan registering protocols and taps */
|
2013-07-05 23:57:07 +00:00
|
|
|
void echld_initialize(echld_init_t* init) {
|
2013-06-21 01:27:28 +00:00
|
|
|
int from_disp[2];
|
|
|
|
int to_disp[2];
|
2013-06-25 17:43:52 +00:00
|
|
|
PARENT_DBG((1,"Echld Starting"));
|
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
if (!init) {
|
|
|
|
PARENT_FATAL((NO_INITIALIZER,"Missing Initializer"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (init->encoding != ECHLD_ENCODING_JSON) {
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_FATAL((UNIMPLEMENTED,"Only JSON implemented"));
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( pipe(to_disp) ) {
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_FATAL((DISPATCHER_PIPE_FAILED,"Failed to open pipe to dispatcher"));
|
2013-06-21 01:27:28 +00:00
|
|
|
} else if( pipe(from_disp) ) {
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_FATAL((DISPATCHER_PIPE_FAILED,"Failed to open pipe from dispatcher"));
|
2013-06-21 01:27:28 +00:00
|
|
|
} else {
|
|
|
|
int pid;
|
|
|
|
int i;
|
2013-06-25 17:43:52 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_DBG((3,"Pipes Opened fr[0]=%d fr[1]=%d to[0]=%d to[1]=%d",from_disp[0],from_disp[1],to_disp[0],to_disp[1]));
|
|
|
|
|
2013-06-25 17:43:52 +00:00
|
|
|
pid = fork();
|
|
|
|
|
|
|
|
if ( pid < 0 ) {
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_FATAL((CANNOT_FORK,"Failed to fork() reason='%s'",strerror(errno)));
|
2013-06-21 01:27:28 +00:00
|
|
|
} else if ( pid == 0) {
|
|
|
|
#ifdef PARENT_THREADS
|
|
|
|
reader_realloc_buf = child_realloc_buff;
|
|
|
|
#endif
|
|
|
|
/* child code */
|
2013-06-27 03:41:48 +00:00
|
|
|
echld_cleanup();
|
2013-07-23 13:23:40 +00:00
|
|
|
|
2013-07-05 23:57:07 +00:00
|
|
|
if (init->after_fork_cb)
|
|
|
|
init->after_fork_cb(init->after_fork_cb_data);
|
|
|
|
|
|
|
|
echld_dispatcher_start(to_disp,from_disp,init->argv0,init->main);
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_FATAL((SHOULD_HAVE_EXITED_BEFORE,"This shoudln't happen"));
|
2013-06-25 17:43:52 +00:00
|
|
|
} else {
|
|
|
|
/* parent code */
|
|
|
|
#ifdef PARENT_THREADS
|
|
|
|
reader_realloc_buf = parent_realloc_buff;
|
|
|
|
#endif
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
/* echld_common_set_dbg(9,stderr,"parent"); */
|
2013-06-27 03:41:48 +00:00
|
|
|
|
2013-06-25 17:43:52 +00:00
|
|
|
PARENT_DBG((3,"Dispatcher forked"));
|
|
|
|
|
|
|
|
echld_get_all_codecs(NULL, NULL, &parent.enc, &parent.dec);
|
|
|
|
parent.children = g_new0(echld_t,ECHLD_MAX_CHILDREN);
|
|
|
|
parent.snd = g_byte_array_new();
|
2013-06-27 03:41:48 +00:00
|
|
|
parent.dispatcher_fd = to_disp[1];
|
|
|
|
parent.dispatcher_pid = pid;
|
2013-06-25 17:43:52 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
echld_init_reader(&(parent.reader),from_disp[0],4096);
|
2013-06-25 17:43:52 +00:00
|
|
|
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
for (i=0;i<ECHLD_MAX_CHILDREN;i++) {
|
2013-06-25 17:43:52 +00:00
|
|
|
parent.children[i].chld_id = -1;
|
|
|
|
parent.children[i].data = NULL;
|
|
|
|
parent.children[i].state = FREE;
|
|
|
|
parent.children[i].handlers = g_array_new(TRUE,TRUE,sizeof(hdlr_t));
|
2013-06-27 03:41:48 +00:00
|
|
|
parent.children[i].reqs = g_array_new(TRUE,TRUE,sizeof(reqh_t));
|
2013-06-25 17:43:52 +00:00
|
|
|
}
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
parent.children[0].chld_id = 0;
|
|
|
|
parent.children[0].state = IDLE;
|
|
|
|
|
2013-06-25 17:43:52 +00:00
|
|
|
signal(SIGCHLD,parent_reaper);
|
2013-06-27 03:41:48 +00:00
|
|
|
//close(to_disp[0]);
|
|
|
|
//close(from_disp[1]);
|
2013-07-23 18:26:38 +00:00
|
|
|
if (init->dispatcher_hello_cb) echld_msgh(0, ECHLD_HELLO, hello_cb, init);
|
|
|
|
|
2013-06-25 17:43:52 +00:00
|
|
|
PARENT_DBG((3,"Ready"));
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern echld_state_t echld_terminate(void) {
|
2013-06-27 03:41:48 +00:00
|
|
|
|
|
|
|
parent.closing = TRUE;
|
|
|
|
PARENT_SEND(NULL,0,ECHLD_CLOSE_CHILD,++reqh_ids);
|
2013-07-23 13:23:40 +00:00
|
|
|
|
2013-09-13 17:23:00 +00:00
|
|
|
do {;} while(sleep(1)); /* wait a full sec without signals */
|
2013-07-23 13:23:40 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_cleanup();
|
2013-06-27 03:41:48 +00:00
|
|
|
close(parent.dispatcher_fd);
|
|
|
|
kill(parent.dispatcher_pid,SIGTERM);
|
2013-06-21 01:27:28 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static echld_t* get_child(int id) {
|
|
|
|
int i;
|
|
|
|
for (i=0;i<ECHLD_MAX_CHILDREN;i++) {
|
|
|
|
if (parent.children[i].chld_id == id) return &(parent.children[i]);
|
|
|
|
};
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* send a request */
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
int reqh_id_idx(echld_t* c, int reqh_id) {
|
|
|
|
int i;
|
|
|
|
int imax = c->reqs->len;
|
|
|
|
reqh_t* rr = (reqh_t*)c->reqs->data;
|
|
|
|
|
|
|
|
for(i=0; i < imax ; i++) {
|
|
|
|
if (rr[i].reqh_id == reqh_id)
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
static echld_state_t reqh_snd(echld_t* c, echld_msg_type_t t, GByteArray* ba, echld_msg_cb_t resp_cb, void* cb_data) {
|
2013-06-27 03:41:48 +00:00
|
|
|
int idx;
|
|
|
|
reqh_t* r;
|
|
|
|
int reqh_id = reqh_ids++;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
if (!c) {
|
|
|
|
PARENT_DBG((1,"REQH_SND: No such child"));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
idx = reqh_id_idx(c,-1);
|
|
|
|
if (idx < 0) {
|
|
|
|
reqh_t req;
|
|
|
|
idx = c->reqs->len;
|
|
|
|
g_array_append_val(c->reqs,req);
|
|
|
|
}
|
2013-07-23 13:23:40 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
r = &(((reqh_t*)c->reqs->data)[idx]);
|
|
|
|
|
|
|
|
r->reqh_id = reqh_id;
|
|
|
|
r->cb = resp_cb;
|
|
|
|
r->cb_data = cb_data;
|
|
|
|
|
|
|
|
gettimeofday(&(r->tv),NULL);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_DBG((4,"reqh_add: idx='%d'",idx));
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
PARENT_DBG((3,"REQH_SND: type='%s' chld_id=%d reqh_id=%d",TY(t), c->chld_id,reqh_id));
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_SEND(ba,c->chld_id,t,reqh_id);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
if (ba) g_byte_array_free(ba,TRUE); /* do we? */
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
return reqh_id;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern echld_reqh_id_t echld_reqh(
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_chld_id_t child_id,
|
|
|
|
echld_msg_type_t t,
|
2013-07-23 13:23:40 +00:00
|
|
|
int usecs_timeout _U_,
|
2013-06-21 01:27:28 +00:00
|
|
|
enc_msg_t* ba,
|
|
|
|
echld_msg_cb_t resp_cb,
|
|
|
|
void* cb_data) {
|
2013-06-22 20:26:13 +00:00
|
|
|
return reqh_snd(get_child(child_id),t,ba,resp_cb,cb_data);
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get callback data for a live request */
|
2013-06-25 03:33:02 +00:00
|
|
|
extern void* echld_reqh_get_data(int child_id, int reqh_id) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (!c) return NULL;
|
|
|
|
|
|
|
|
idx = reqh_id_idx(c,reqh_id);
|
|
|
|
|
|
|
|
if (idx >= 0)
|
|
|
|
return g_array_index(c->reqs, reqh_t, idx).cb_data;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the callback for a live request */
|
2013-06-25 03:33:02 +00:00
|
|
|
extern echld_msg_cb_t echld_reqh_get_cb(int child_id, int reqh_id) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (!c) return NULL;
|
|
|
|
|
|
|
|
idx = reqh_id_idx(c,reqh_id);
|
|
|
|
|
|
|
|
if (idx >= 0)
|
|
|
|
return g_array_index(c->reqs, reqh_t, idx).cb;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set callback data for a live request */
|
2013-06-25 03:33:02 +00:00
|
|
|
extern gboolean echld_reqh_set_data(int child_id, int reqh_id, void* cb_data) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (!c) return FALSE;
|
|
|
|
|
|
|
|
idx = reqh_id_idx(c,reqh_id);
|
|
|
|
|
|
|
|
if (idx < 0) return FALSE;
|
|
|
|
|
|
|
|
g_array_index(c->reqs, reqh_t, idx).cb_data = cb_data;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the callback for a live request */
|
2013-06-25 03:33:02 +00:00
|
|
|
extern gboolean echld_reqh_set_cb(int child_id, int reqh_id, echld_msg_cb_t cb){
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (!c) return FALSE;
|
|
|
|
|
|
|
|
idx = reqh_id_idx(c,reqh_id);
|
|
|
|
|
|
|
|
if (idx < 0) return FALSE;
|
|
|
|
|
|
|
|
g_array_index(c->reqs, reqh_t, idx).cb = cb;
|
|
|
|
return TRUE;
|
|
|
|
}
|
2013-07-23 13:23:40 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
/* stop receiving a live request */
|
2013-06-25 03:33:02 +00:00
|
|
|
extern gboolean echld_reqh_detach(int child_id, int reqh_id) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (!c) return FALSE;
|
|
|
|
|
|
|
|
idx = reqh_id_idx(c,reqh_id);
|
|
|
|
|
|
|
|
if (idx < 0) return FALSE;
|
|
|
|
|
|
|
|
g_array_remove_index(c->reqs,idx);
|
2013-06-22 20:26:13 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static echld_bool_t parent_dead_child(echld_msg_type_t type, enc_msg_t* ba, void* data) {
|
2013-06-22 20:26:13 +00:00
|
|
|
echld_t* c = (echld_t*)data;
|
|
|
|
char* s;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
if (type != ECHLD_CHILD_DEAD) {
|
2013-06-22 20:26:13 +00:00
|
|
|
PARENT_DBG((1, "Must Be ECHLD_CHILD_DEAD"));
|
2013-06-21 01:27:28 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
if ( parent.dec->child_dead(ba,&s) ) {
|
|
|
|
PARENT_DBG((1,"Dead Child[%d]: %s",c->chld_id,s));
|
|
|
|
g_free(s);
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parent_child_cleanup(c);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
static echld_bool_t parent_get_hello(echld_msg_type_t type, enc_msg_t* ba, void* data) {
|
2013-06-22 20:26:13 +00:00
|
|
|
echld_t* c = (echld_t*)data;
|
2013-07-23 18:26:38 +00:00
|
|
|
int err_id;
|
|
|
|
char* err = NULL;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
switch (type) {
|
2013-07-23 13:23:40 +00:00
|
|
|
case ECHLD_HELLO:
|
2013-06-22 20:26:13 +00:00
|
|
|
PARENT_DBG((1,"Child[%d]: =>IDLE",c->chld_id));
|
2013-06-21 01:27:28 +00:00
|
|
|
c->state = IDLE;
|
2013-07-23 18:26:38 +00:00
|
|
|
break;
|
2013-06-21 01:27:28 +00:00
|
|
|
case ECHLD_ERROR:
|
2013-07-23 18:26:38 +00:00
|
|
|
parent.dec->error(ba,&err_id,&err);
|
|
|
|
break;
|
2013-07-23 20:45:25 +00:00
|
|
|
case ECHLD_TIMED_OUT:
|
2013-07-23 18:26:38 +00:00
|
|
|
err = g_strdup("timedout");
|
|
|
|
break;
|
2013-06-21 01:27:28 +00:00
|
|
|
default:
|
2013-07-23 18:26:38 +00:00
|
|
|
err = g_strdup_printf("Wrong MSG 'HELLO' expected, got '%s",TY(type));
|
|
|
|
break;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
2013-07-23 18:26:38 +00:00
|
|
|
|
|
|
|
if (c->cb)
|
|
|
|
c->cb(c->data,err);
|
|
|
|
|
|
|
|
if (err) g_free(err);
|
|
|
|
|
|
|
|
return TRUE;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int chld_cmp(const void *a, const void *b) {
|
|
|
|
return ((echld_t*)b)->chld_id - ((echld_t*)a)->chld_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int msgh_attach(echld_t* c, echld_msg_type_t t, echld_msg_cb_t resp_cb, void* cb_data);
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
static int next_chld_id = 1;
|
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
extern int echld_new(enc_msg_t* new_child_em, echld_new_cb_t cb, void* child_data) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(-1);
|
|
|
|
|
|
|
|
if (!c) return -1;
|
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
c->chld_id = (next_chld_id++);
|
2013-06-21 01:27:28 +00:00
|
|
|
c->data = child_data;
|
|
|
|
c->state = CREATING;
|
2013-07-23 18:26:38 +00:00
|
|
|
c->cb = cb;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
PARENT_DBG((1,"Child[%d]: =>CREATING",c->chld_id));
|
2013-07-23 13:23:40 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
msgh_attach(c,ECHLD_CHILD_DEAD, parent_dead_child , c);
|
2013-07-05 23:57:07 +00:00
|
|
|
reqh_snd(c, ECHLD_NEW_CHILD, (GByteArray*)new_child_em, parent_get_hello, c);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
return c->chld_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* XXX these fail silently */
|
2013-06-25 03:33:02 +00:00
|
|
|
extern void* echld_get_data(int child_id) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
return c ? c->data : NULL;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern echld_state_t echld_set_data(echld_chld_id_t chld_id, void* data) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(chld_id);
|
|
|
|
if (c) {
|
|
|
|
c->data = data;
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int msgh_idx(echld_t* c, int msgh_id) {
|
|
|
|
int i = 0;
|
|
|
|
int imax = c->handlers->len;
|
|
|
|
|
|
|
|
for (i=0;i<imax;i++) {
|
|
|
|
if (((hdlr_t*)(c->handlers->data))[i].id == msgh_id) return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* start a message handler */
|
|
|
|
static int msgh_attach(echld_t* c, echld_msg_type_t t, echld_msg_cb_t resp_cb, void* cb_data) {
|
|
|
|
hdlr_t h;
|
2013-06-22 20:26:13 +00:00
|
|
|
static int msgh_id = 1;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
h.id = msgh_id++;
|
|
|
|
h.type = t;
|
|
|
|
h.cb = resp_cb;
|
|
|
|
h.cb_data = cb_data;
|
|
|
|
|
|
|
|
g_array_append_val(c->handlers,h);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern int echld_msgh(int child_id, echld_msg_type_t t, echld_msg_cb_t resp_cb, void* cb_data) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
|
|
|
|
if (c) return msgh_attach(c,t,resp_cb,cb_data);
|
|
|
|
else return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* stop it */
|
|
|
|
static echld_state_t msgh_detach(echld_t* c, int msgh_id) {
|
|
|
|
int idx = msgh_idx(c,msgh_id);
|
2013-07-23 13:23:40 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
if (idx < 0) return -1;
|
|
|
|
|
|
|
|
g_array_remove_index(c->handlers,idx);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern echld_state_t echld_msgh_detach(int child_id, int msgh_id) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
return msgh_detach(c,msgh_id);
|
2013-07-23 13:23:40 +00:00
|
|
|
}
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
/* get a msgh's data */
|
|
|
|
|
|
|
|
static void* msgh_get_data(echld_t* c, int msgh_id) {
|
|
|
|
int idx = msgh_idx(c,msgh_id);
|
2013-07-23 13:23:40 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
if (idx < 0) return NULL;
|
|
|
|
|
|
|
|
return ((hdlr_t*)(c->handlers->data))[idx].cb_data;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern void* echld_msgh_get_data(int child_id, int msgh_id) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
return msgh_get_data(c,msgh_id);
|
2013-07-23 13:23:40 +00:00
|
|
|
}
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
/* get a msgh's cb */
|
|
|
|
static echld_msg_cb_t msgh_get_cb(echld_t* c, int msgh_id) {
|
|
|
|
int idx = msgh_idx(c,msgh_id);
|
2013-07-23 13:23:40 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
if (idx < 0) return NULL;
|
|
|
|
|
|
|
|
return ((hdlr_t*)(c->handlers->data))[idx].cb;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern echld_msg_cb_t echld_msgh_get_cb(int child_id, int msgh_id) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
2013-07-23 13:23:40 +00:00
|
|
|
return msgh_get_cb(c,msgh_id);
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get a msgh's type */
|
|
|
|
static echld_msg_type_t msgh_get_type(echld_t* c, int msgh_id) {
|
|
|
|
int idx = msgh_idx(c,msgh_id);
|
2013-07-23 13:23:40 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
if (idx < 0) return EC_ACTUAL_ERROR;
|
|
|
|
|
|
|
|
return ((hdlr_t*)(c->handlers->data))[idx].type;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern echld_msg_type_t echld_msgh_get_type(int child_id, int msgh_id) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
2013-07-23 13:23:40 +00:00
|
|
|
return c ? msgh_get_type(c,msgh_id) : EC_ACTUAL_ERROR;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* get it all from a msgh */
|
|
|
|
static echld_state_t msgh_get_all(echld_t* c, int msgh_id, echld_msg_type_t* t, echld_msg_cb_t* cb, void** data) {
|
|
|
|
int idx = msgh_idx(c,msgh_id);
|
2013-07-23 13:23:40 +00:00
|
|
|
hdlr_t* h;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
if (idx < 0) return -1;
|
|
|
|
|
|
|
|
h = &(((hdlr_t*)(c->handlers->data))[idx]);
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
if (t) *t = h->type;
|
|
|
|
if (cb) *cb = h->cb;
|
|
|
|
if (data) *data = h->cb_data;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern gboolean echld_msgh_get_all(int child_id, int msgh_id, echld_msg_type_t* t, echld_msg_cb_t* cb, void** data) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
return c && msgh_get_all(c,msgh_id,t,cb,data);
|
|
|
|
}
|
|
|
|
|
|
|
|
static echld_state_t msgh_set_all(echld_t* c, int msgh_id, echld_msg_type_t t, echld_msg_cb_t cb, void* data) {
|
|
|
|
int idx = msgh_idx(c,msgh_id);
|
2013-07-23 13:23:40 +00:00
|
|
|
hdlr_t* h;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
if (idx < 0) return -1;
|
|
|
|
|
|
|
|
h = &(((hdlr_t*)(c->handlers->data))[idx]);
|
|
|
|
|
|
|
|
h->type = t;
|
|
|
|
h->cb = cb;
|
|
|
|
h->cb_data = data;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern gboolean echld_msgh_set_all(int child_id, int msgh_id, echld_msg_type_t t, echld_msg_cb_t cb, void* data) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
return c ? msgh_set_all(c,msgh_id,t,cb,data) : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set a msgh's data */
|
|
|
|
static gboolean msgh_set_data(echld_t* c, int msgh_id, void* data) {
|
|
|
|
int idx = msgh_idx(c,msgh_id);
|
|
|
|
|
|
|
|
if (idx < 0) return FALSE;
|
|
|
|
|
|
|
|
((hdlr_t*)(c->handlers->data))[idx].cb_data = data;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern gboolean echld_msgh_set_data(int child_id, int msgh_id, void* data){
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
return c ? msgh_set_data(c,msgh_id,data) : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set a msgh's cb */
|
2013-06-25 03:33:02 +00:00
|
|
|
extern gboolean msgh_set_cb(echld_t* c, int msgh_id, echld_msg_cb_t cb) {
|
2013-06-21 01:27:28 +00:00
|
|
|
int idx = msgh_idx(c,msgh_id);
|
|
|
|
|
|
|
|
if (idx < 0) return FALSE;
|
|
|
|
|
|
|
|
((hdlr_t*)(c->handlers->data))[idx].cb = cb;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern gboolean echld_msgh_set_cb(int child_id, int msgh_id, echld_msg_cb_t cb) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
return c ? msgh_set_cb(c,msgh_id,cb) : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set a msgh's type */
|
|
|
|
|
|
|
|
static gboolean msgh_set_type(echld_t* c, int msgh_id, echld_msg_type_t t) {
|
|
|
|
int idx = msgh_idx(c,msgh_id);
|
|
|
|
|
|
|
|
if (idx < 0) return FALSE;
|
|
|
|
|
|
|
|
((hdlr_t*)(c->handlers->data))[idx].type = t;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern gboolean echld_msgh_set_type(int child_id, int msgh_id, echld_msg_type_t t) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(child_id);
|
|
|
|
return c ? msgh_set_type(c,msgh_id,t) : FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* call cb(id,child_data,cb_data) for each child*/
|
2013-06-25 03:33:02 +00:00
|
|
|
extern void echld_foreach_child(echld_iter_cb_t cb, void* cb_data) {
|
2013-06-21 01:27:28 +00:00
|
|
|
int i;
|
|
|
|
for(i=0;i<ECHLD_MAX_CHILDREN;i++) {
|
|
|
|
echld_t* c = &(parent.children[i]);
|
|
|
|
cb(c->chld_id,c->data,cb_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static reqh_t* get_req(echld_t* c, int reqh_id) {
|
|
|
|
int idx = reqh_id_idx(c,reqh_id);
|
|
|
|
if(idx < 0) return NULL;
|
|
|
|
|
|
|
|
return ((reqh_t*)(c->reqs->data))+idx;
|
|
|
|
}
|
|
|
|
|
|
|
|
static hdlr_t* get_next_hdlr_for_type(echld_t* c, echld_msg_type_t t, int* cookie) {
|
|
|
|
int imax = c->handlers->len;
|
2013-07-23 18:26:38 +00:00
|
|
|
hdlr_t* r = NULL;
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
for (;(*cookie)<imax;(*cookie)++) {
|
|
|
|
if (((hdlr_t*)(c->handlers->data))[*cookie].type == t) {
|
|
|
|
r = &( ((hdlr_t*)(c->handlers->data))[*cookie] );
|
|
|
|
(*cookie)++;
|
|
|
|
break;
|
|
|
|
}
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
return r;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
static long parent_read_frame(guint8* b, size_t len, echld_chld_id_t chld_id, echld_msg_type_t t, echld_reqh_id_t reqh_id, void* data _U_) {
|
2013-06-21 01:27:28 +00:00
|
|
|
echld_t* c = get_child(chld_id);
|
2013-06-22 20:26:13 +00:00
|
|
|
GByteArray* ba = g_byte_array_new();
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-28 21:10:52 +00:00
|
|
|
PARENT_DBG((1,"MSG_IN<- ch=%d t='%s' rh=%d",chld_id,TY(t),reqh_id));
|
2013-06-22 20:26:13 +00:00
|
|
|
g_byte_array_append(ba,b, (guint)len);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
if (c) {
|
|
|
|
reqh_t* r = get_req(c, reqh_id);
|
2013-07-23 18:26:38 +00:00
|
|
|
int i;
|
2013-06-21 01:27:28 +00:00
|
|
|
hdlr_t* h;
|
|
|
|
gboolean go_ahead = TRUE;
|
|
|
|
|
|
|
|
if (r) { /* got that reqh_id */
|
2013-06-27 03:41:48 +00:00
|
|
|
if (r->cb) {
|
|
|
|
go_ahead = r->cb(t,ba,r->cb_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
r->reqh_id = -1;
|
|
|
|
r->cb = NULL;
|
|
|
|
r->cb_data = 0;
|
|
|
|
r->tv.tv_sec = 0;
|
|
|
|
r->tv.tv_usec = 0;
|
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
PARENT_DBG((2,"handled by reqh_id=%d msg='%s'",reqh_id,go_ahead?"retrying":"done"));
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
i=0;
|
2013-06-21 01:27:28 +00:00
|
|
|
while(go_ahead && ( h = get_next_hdlr_for_type(c,t,&i))) {
|
2013-06-27 03:41:48 +00:00
|
|
|
if (h->cb)
|
|
|
|
go_ahead = h->cb(t,ba,h->cb_data);
|
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
PARENT_DBG((2,"handled by t='%s' msgh_id=%d msg='%s'",TY(h->type), h->id,go_ahead?"retrying":"done"));
|
2013-07-23 13:23:40 +00:00
|
|
|
}
|
2013-06-21 01:27:28 +00:00
|
|
|
} else {
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_DBG((1,"parent_read_frame: No such child"));
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-22 20:26:13 +00:00
|
|
|
g_byte_array_free(ba,TRUE);
|
|
|
|
return 1;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern int echld_fdset(fd_set* rfds, fd_set* efds) {
|
2013-06-21 01:27:28 +00:00
|
|
|
FD_SET(parent.reader.fd, rfds);
|
|
|
|
FD_SET(parent.reader.fd, efds);
|
|
|
|
FD_SET(parent.dispatcher_fd, efds);
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern int echld_fd_read(fd_set* rfds, fd_set* efds) {
|
2013-06-21 01:27:28 +00:00
|
|
|
int r_nfds=0;
|
|
|
|
if (FD_ISSET(parent.reader.fd,efds) || FD_ISSET(parent.dispatcher_fd,efds) ) {
|
|
|
|
/* Handle errored dispatcher */
|
2013-06-27 03:41:48 +00:00
|
|
|
PARENT_DBG((1,"parent errored"));
|
2013-06-22 20:26:13 +00:00
|
|
|
return -1;
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FD_ISSET(parent.reader.fd,rfds)) {
|
2013-07-23 18:26:38 +00:00
|
|
|
PARENT_DBG((3,"reading from dispatcher"));
|
2013-06-22 20:26:13 +00:00
|
|
|
echld_read_frame(&(parent.reader),parent_read_frame,&(parent));
|
2013-06-21 01:27:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r_nfds;
|
|
|
|
}
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
extern int echld_select(int nfds _U_, fd_set* rfds, fd_set* wfds, fd_set* efds, struct timeval* timeout) {
|
2013-06-21 01:27:28 +00:00
|
|
|
fd_set my_rfds, my_wfds, my_efds;
|
|
|
|
int r_nfds;
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
|
2013-06-21 01:27:28 +00:00
|
|
|
if (rfds == NULL) { rfds = &my_rfds; FD_ZERO(rfds); }
|
|
|
|
if (wfds == NULL) { wfds = &my_wfds; FD_ZERO(wfds); }
|
|
|
|
if (efds == NULL) { efds = &my_efds; FD_ZERO(efds); }
|
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
echld_fdset(rfds,efds);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
PARENT_DBG((5,"Select()"));
|
2013-06-27 03:41:48 +00:00
|
|
|
r_nfds = select(FD_SETSIZE, rfds, wfds, efds, timeout);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
2013-06-27 03:41:48 +00:00
|
|
|
echld_fd_read(rfds,efds);
|
2013-06-21 01:27:28 +00:00
|
|
|
|
|
|
|
return r_nfds ;
|
|
|
|
}
|
|
|
|
|
2013-06-25 03:33:02 +00:00
|
|
|
extern echld_state_t echld_wait(struct timeval* timeout) {
|
2013-06-21 01:27:28 +00:00
|
|
|
if ( echld_select(0, NULL, NULL, NULL, timeout) < 0) {
|
|
|
|
return -1;
|
|
|
|
} else {
|
|
|
|
return ECHLD_OK;
|
|
|
|
}
|
|
|
|
}
|
2013-07-05 23:57:07 +00:00
|
|
|
|
|
|
|
enc_msg_t* echld_new_child_params(void) {
|
|
|
|
return (enc_msg_t*)g_byte_array_new();
|
|
|
|
}
|
|
|
|
|
|
|
|
enc_msg_t* echld_new_child_params_merge(enc_msg_t* em1, enc_msg_t* em2) {
|
|
|
|
GByteArray* ba = g_byte_array_new();
|
|
|
|
GByteArray* b1 = (GByteArray*)em1;
|
|
|
|
GByteArray* b2 = (GByteArray*)em2;
|
|
|
|
|
|
|
|
g_byte_array_append(ba,b1->data,b1->len);
|
|
|
|
g_byte_array_append(ba,b2->data,b2->len);
|
|
|
|
|
|
|
|
return (enc_msg_t*)ba;
|
|
|
|
}
|
|
|
|
|
2013-07-23 18:26:38 +00:00
|
|
|
char* echld_new_child_params_str(enc_msg_t* em, const char* prefix, const char* postfix, int trunc_n, const char* fmt) {
|
|
|
|
GByteArray* ba = (GByteArray*)em;
|
|
|
|
GString* str = g_string_new(prefix);
|
|
|
|
char* p = (char*) ba->data;
|
|
|
|
int tot_len = ba->len;
|
|
|
|
long rem = tot_len;
|
|
|
|
p[rem-1] = '\0'; /* make sure last char is null */
|
|
|
|
|
|
|
|
while(rem > 2) {
|
|
|
|
char* param = p;
|
|
|
|
long param_len = strlen(param)+1;
|
|
|
|
char* value = p + param_len;
|
|
|
|
long value_len;
|
|
|
|
|
|
|
|
rem -= param_len;
|
|
|
|
|
|
|
|
if (rem < 0) {
|
|
|
|
g_string_free(str,TRUE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
value_len = strlen(value)+1;
|
|
|
|
|
|
|
|
rem -= value_len;
|
|
|
|
p = value + value_len;
|
|
|
|
|
|
|
|
if (rem < 0) {
|
|
|
|
g_string_free(str,TRUE);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_string_append_printf(str,fmt,param,value);
|
|
|
|
}
|
|
|
|
g_string_truncate(str, str->len - trunc_n);
|
|
|
|
g_string_append(str,postfix);
|
|
|
|
p = str->str;
|
|
|
|
g_string_free(str,FALSE);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
void echld_new_child_params_add_params(enc_msg_t* em, ...) {
|
2013-07-05 23:57:07 +00:00
|
|
|
GByteArray* ba = (GByteArray*) em;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap,em);
|
|
|
|
do {
|
|
|
|
char* param_str = va_arg(ap, char*);
|
|
|
|
|
|
|
|
if (param_str) {
|
|
|
|
char* val_str = va_arg(ap, char*);
|
|
|
|
|
|
|
|
g_byte_array_append(ba, (guint8*) param_str, (guint)strlen(param_str)+1);
|
|
|
|
g_byte_array_append(ba, (guint8*) val_str, (guint)strlen(val_str)+1);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
} while(1);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
}
|
2013-07-23 18:26:38 +00:00
|
|
|
|
|
|
|
|