Skip to content

Commit

Permalink
core: added function to detect when processes were initialized
Browse files Browse the repository at this point in the history
- sr_instance_ready() return 1 if all processes finished child_init()

(cherry picked from commit 97af78a)
  • Loading branch information
miconda committed Jan 7, 2019
1 parent 260aa34 commit 3d9396f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 11 deletions.
53 changes: 44 additions & 9 deletions src/core/pt.c
Expand Up @@ -125,6 +125,7 @@ int init_pt(int proc_no)
#endif
process_no=0; /*main process number*/
pt[process_no].pid=getpid();
pt[process_no].rank=PROC_MAIN;
memcpy(pt[process_no].desc,"main",5);
*process_count=1;
return 0;
Expand Down Expand Up @@ -322,6 +323,7 @@ int fork_process(int child_id, char *desc, int make_sock)
/* record pid twice to avoid the child using it, before
* parent gets a chance to set it*/
pt[process_no].pid=getpid();
pt[process_no].rank=child_id;
#else
/* wait for parent to get out of critical zone.
* this is actually relevant as the parent updates
Expand All @@ -335,10 +337,14 @@ int fork_process(int child_id, char *desc, int make_sock)
unix_tcp_sock=sockfd[1];
}
#endif
if ((child_id!=PROC_NOCHLDINIT) && (init_child(child_id) < 0)) {
LM_ERR("init_child failed for process %d, pid %d, \"%s\"\n",
process_no, pt[process_no].pid, pt[process_no].desc);
return -1;
if (child_id!=PROC_NOCHLDINIT) {
if(init_child(child_id) < 0) {
LM_ERR("init_child failed for process %d, pid %d, \"%s\"\n",
process_no, pt[process_no].pid, pt[process_no].desc);
return -1;
}
} else {
pt[process_no].status = 1;
}
return pid;
} else {
Expand All @@ -349,6 +355,7 @@ int fork_process(int child_id, char *desc, int make_sock)
#endif
/* add the process to the list in shm */
pt[child_process_no].pid=pid;
pt[child_process_no].rank=child_id;
if (desc){
strncpy(pt[child_process_no].desc, desc, MAX_PT_DESC-1);
}
Expand Down Expand Up @@ -468,14 +475,19 @@ int fork_tcp_process(int child_id, char *desc, int r, int *reader_fd_1)
lock_get(process_lock);
lock_release(process_lock);
#endif
pt[process_no].rank=child_id;
close(sockfd[0]);
unix_tcp_sock=sockfd[1];
close(reader_fd[0]);
if (reader_fd_1) *reader_fd_1=reader_fd[1];
if ((child_id!=PROC_NOCHLDINIT) && (init_child(child_id) < 0)) {
LM_ERR("init_child failed for process %d, pid %d, \"%s\"\n",
process_no, pt[process_no].pid, pt[process_no].desc);
return -1;
if (child_id!=PROC_NOCHLDINIT) {
if (init_child(child_id) < 0) {
LM_ERR("init_child failed for process %d, pid %d, \"%s\"\n",
process_no, pt[process_no].pid, pt[process_no].desc);
return -1;
}
} else {
pt[process_no].status = 1;
}
return pid;
} else {
Expand All @@ -486,6 +498,7 @@ int fork_tcp_process(int child_id, char *desc, int r, int *reader_fd_1)
#endif
/* add the process to the list in shm */
pt[child_process_no].pid=pid;
pt[child_process_no].rank=child_id;
pt[child_process_no].unix_sock=sockfd[0];
pt[child_process_no].idx=r;
if (desc){
Expand Down Expand Up @@ -580,4 +593,26 @@ int mem_dump_shm_fixup(void *handle, str *gname, str *name, void **val)
}
return 0;
}
#endif

/* cache if child processes were initialized */
static int _sr_instance_ready = 0;

/**
* return 1 if all child processes were initialized
*/
int sr_instance_ready(void)
{
int i;
if(_sr_instance_ready==1) {
return 1;
}
for (i=0; i<*process_count; i++) {
if(pt[i].rank!=PROC_MAIN && pt[i].rank!=PROC_NOCHLDINIT) {
if(pt[i].status==0) {
return 0;
}
}
}
_sr_instance_ready = 1;
return 1;
}
4 changes: 4 additions & 0 deletions src/core/pt.h
Expand Up @@ -40,6 +40,8 @@ struct process_table {
int unix_sock; /* unix socket on which tcp main listens */
int idx; /* tcp child index, -1 for other processes */
#endif
int status; /* set to 1 when child init is done */
int rank; /* rank of process */
char desc[MAX_PT_DESC];
};

Expand Down Expand Up @@ -100,4 +102,6 @@ unsigned int set_fork_delay(unsigned int v);

int sr_instance_started(void);

int sr_instance_ready(void);

#endif
10 changes: 9 additions & 1 deletion src/core/sr_module.c
Expand Up @@ -819,6 +819,9 @@ int init_child(int rank)
}
}
}
if(rank!=PROC_INIT) {
pt[process_no].status = 1;
}
return 0;
}

Expand Down Expand Up @@ -864,10 +867,15 @@ static int init_mod_child( struct sr_module* m, int rank )
*/
int init_child(int rank)
{
int ret;
if(async_task_child_init(rank)<0)
return -1;

return init_mod_child(modules, rank);
ret = init_mod_child(modules, rank);
if(rank!=PROC_INIT) {
pt[process_no].status = 1;
}
return ret;
}


Expand Down
7 changes: 6 additions & 1 deletion src/main.c
Expand Up @@ -507,7 +507,12 @@ char *sr_memmng_shm = NULL;

static int *_sr_instance_started = NULL;

int sr_instance_initialized(void)
/**
* return 1 if all child processes were forked
* - note: they might still be in init phase (i.e., child init)
* - note: see also sr_insance_ready()
*/
int sr_instance_started(void)
{
if(_sr_instance_started!=NULL && *_sr_instance_started==1) {
return 1;
Expand Down

0 comments on commit 3d9396f

Please sign in to comment.