示例#1
0
/**
 * @brief Initialize async module children
 */
static int child_init(int rank)
{
	int pid;
	int i;

	if (rank==PROC_INIT) {
		if(evapi_init_notify_sockets()<0) {
			LM_ERR("failed to initialize notify sockets\n");
			return -1;
		}
		return 0;
	}

	if (rank!=PROC_MAIN) {
		evapi_close_notify_sockets_parent();
		return 0;
	}

	pid=fork_process(PROC_NOCHLDINIT, "EvAPI Dispatcher", 1);
	if (pid<0)
		return -1; /* error */
	if(pid==0) {
		/* child */

		/* initialize the config framework */
		if (cfg_child_init())
			return -1;
		/* main function for dispatcher */
		evapi_close_notify_sockets_child();
		if(evapi_run_dispatcher(_evapi_bind_addr, _evapi_bind_port)<0) {
			LM_ERR("failed to initialize disptacher process\n");
			return -1;
		}
	}

	for(i=0; i<_evapi_workers; i++) {
		pid=fork_process(PROC_RPC, "EvAPI Worker", 1);
		if (pid<0)
			return -1; /* error */
		if(pid==0) {
			/* child */

			/* initialize the config framework */
			if (cfg_child_init())
				return -1;
			/* main function for workers */
			if(evapi_run_worker(i+1)<0) {
				LM_ERR("failed to initialize worker process: %d\n", i);
				return -1;
			}
		}
	}

	return 0;
}
示例#2
0
int jsonrpc_dgram_child_init(int rank)
{
	int i;
	int pid;

	if (rank==PROC_MAIN) {
		if(jsonrpc_dgram_pre_process()!=0) {
			LM_ERR("pre-fork function failed\n");
			return -1;
		}
		for(i=0; i<jsonrpc_dgram_workers; i++) {
			pid=fork_process(PROC_NOCHLDINIT, "JSONRPC-S DATAGRAM", 1);
			if (pid<0)
				return -1; /* error */
			if(pid==0) {
				/* child */

				/* initialize the config framework */
				if (cfg_child_init())
					return -1;

				jsonrpc_dgram_process(i);
				return 0;
			}
		}
		if(jsonrpc_dgram_post_process()!=0) {
			LM_ERR("post-fork function failed\n");
			return -1;
		}
	}
	return 0;
}
示例#3
0
文件: sms.c 项目: 4N7HR4X/kamailio
int sms_child_init(int rank)
{
	int  i, foo;

	/* only the child 1 will execute this */
	if (rank != 1) goto done;

	/* creates processes for each modem */
	for(i=0;i<nr_of_modems;i++)
	{
		if ( (foo=fork())<0 ) {
			LM_ERR("cannot fork \n");
			goto error;
		}
		if (!foo) {
			/* initialize the config framework */
			if (cfg_child_init()) goto error;

			modem_process(&(modems[i]));
			goto done;
		}
	}

done:
	return 0;
error:
	return-1;
}
示例#4
0
static int cpl_child_init(int rank)
{
	pid_t pid;

	/* don't do anything for main process and TCP manager process */
	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
		return 0;

	/* only child 1 will fork the aux process */
	if (rank==1) {
		pid = fork();
		if (pid==-1) {
			LOG(L_CRIT,"ERROR:cpl_child_init(%d): cannot fork: %s!\n",
				rank, strerror(errno));
			goto error;
		} else if (pid==0) {
			/* I'm the child */

			/* initialize the config framework */
			if (cfg_child_init()) goto error;

			cpl_aux_process( cpl_env.cmd_pipe[0], cpl_env.log_dir);
		} else {
			LOG(L_INFO,"INFO:cpl_child_init(%d): I just gave birth to a child!"
				" I'm a PARENT!!\n",rank);
			/* I'm the parent -> remember the pid */
			aux_process = pid;
		}
	}

	return cpl_db_init(DB_URL, DB_TABLE);
error:
	return -1;
}
示例#5
0
static int child_init(int rank)
{
	int pid;

	if (rank!=PROC_INIT)
		cmd_pipe = pipe_fds[1];

	if (rank!=PROC_MAIN)
		return 0;

	pid=fork_process(PROC_NOCHLDINIT, "jsonrpc io handler", 1);
	if (pid<0) {
		LM_ERR("failed to fork jsonrpc io handler\n");
		return -1; /* error */
	}
	if(pid==0){
		/* child */
		close(pipe_fds[1]);
		/* initialize the config framework */
		if (cfg_child_init())
			return -1;
		return jsonrpc_io_child_process(pipe_fds[0], servers_param);
	}
	return 0;
}
示例#6
0
static int child_init(int rank)
{
	evrexec_task_t *it;
	int i;
	int pid;
	char si_desc[MAX_PT_DESC];

	if(_evrexec_list==NULL)
		return 0;

	if (rank!=PROC_MAIN)
		return 0;

	it = _evrexec_list;
	while(it) {
		for(i=0; i<it->workers; i++) {
			snprintf(si_desc, MAX_PT_DESC, "EVREXEC child=%d exec=%.*s",
					i, it->ename.len, it->ename.s);
			pid=fork_process(PROC_RPC, si_desc, 1);
			if (pid<0)
				return -1; /* error */
			if(pid==0){
				/* child */
				/* initialize the config framework */
				if (cfg_child_init())
					return -1;

				evrexec_process(it, i);
			}
		}
		it = it->next;
	}

	return 0;
}
示例#7
0
/*! This function is called when Kamailio has finished creating all instances of
 * itself.  It is at this point that we want to create our AgentX sub-agent
 * process, and register a handler for any state changes of our child. */
static int mod_child_init(int rank)
{
	int pid;

	/* We only want to setup a single process, under the main attendant. */
	if(rank != PROC_MAIN) {
		return 0;
	}

	/* Spawn SNMP AgentX process */
	pid = fork_process(PROC_NOCHLDINIT, "SNMP AgentX", 1);
	if(pid < 0)
		return -1; /* error */
	if(pid == 0) {
		/* child */
		/* initialize the config framework */
		if(cfg_child_init())
			return -1;

		agentx_child(1);
		return 0;
	}

	/* Spawn a child that will check the system up time. */
	spawn_sysUpTime_child();

	return 0;
}
示例#8
0
static int child_init(int rank)
{
    
	LM_DBG("Initialization of module in child [%d] \n", rank);
        if ((subscribe_to_reginfo == 1) && (rank == PROC_MAIN)) {
	     LM_DBG("Creating RegInfo Event Processor process\n");
	    int pid = fork_process(PROC_SIPINIT, "RegInfo Event Processor", 1);
	    if (pid < 0)
		return -1; //error
	    if (pid == 0) {
		if (cfg_child_init())
		    return -1; //error
		reginfo_event_process();
	    }
	}
    
	if (rank == PROC_MAIN || rank == PROC_TCP_MAIN)
		return 0;
	if (rank == 1) {
		/* init stats */
		//TODO if parameters are modified via cfg framework do i change them?
		//update_stat( max_expires_stat, default_registrar_cfg.max_expires ); update_stat( max_contacts_stat, default_registrar_cfg.max_contacts ); update_stat( default_expire_stat, default_registrar_cfg.default_expires );
	}

	/* don't do anything for main process and TCP manager process */
	if (rank == PROC_MAIN || rank == PROC_TCP_MAIN)
		return 0;

	return 0;
}
示例#9
0
int jsonrpc_fifo_child_init(int rank)
{
	int pid;

	if(jsonrpc_fifo == NULL) {
		LM_ERR("invalid fifo file path\n");
	}

	pid=fork_process(PROC_NOCHLDINIT, "JSONRPC-S FIFO", 1);
	if (pid<0) {
		return -1; /* error */
	}

	if(pid==0) {
		/* child */

		/* initialize the config framework */
		if (cfg_child_init())
			return -1;

		jsonrpc_fifo_process(1);
	}

	return 0;
}
示例#10
0
/**
 * \brief Forks a separate simple sleep() -&- sync periodic timer
 *
 * Forks a very basic periodic timer process, that just sleep()s for 
 * the specified interval and then calls the timer function.
 * The new "sync timer" process execution start immediately, the sleep()
 * is called first (so the first call to the timer function will happen
 * \<interval\> seconds after the call to fork_sync_timer)
 * @param child_id  @see fork_process()
 * @param desc      @see fork_process()
 * @param make_sock @see fork_process()
 * @param f         timer function/callback
 * @param param     parameter passed to the timer function
 * @param interval  interval in seconds.
 * @return pid of the new process on success, -1 on error
 * (doesn't return anything in the child process)
 */
int fork_sync_timer(int child_id, char* desc, int make_sock,
						timer_function* f, void* param, int interval)
{
	int pid;
	ticks_t ts1 = 0;
	ticks_t ts2 = 0;

	pid=fork_process(child_id, desc, make_sock);
	if (pid<0) return -1;
	if (pid==0){
		/* child */
		ts2 = interval;
		if (cfg_child_init()) return -1;
		for(;;){
			if(ts2>0) sleep(ts2);
			else sleep(1);
			ts1 = get_ticks();
			cfg_update();
			f(get_ticks(), param); /* ticks in s for compatibility with old
									  timers */
			ts2 = interval - get_ticks() + ts1;
		}
	}
	/* parent */
	return pid;
}
示例#11
0
文件: seas.c 项目: gbour/kamailio
/**
 * This function initializes each one of the processes spawn by the server.
 * the rank is 1 only when the main process is being initialized, so in that
 * case the function spawns the SEAS process to handle as_events triggered
 * from the other SER processes (executing the script).
 * the new process created, then goes into dispatcher_main_loop(), where
 * it reads() the pipe waiting for events produced by other SER processes.
 */
static int seas_child_init(int rank)
{
   int pid;

   /* only the child 1 will execute this */
   if (rank != PROC_MAIN){
      /* only dispatcher needs to read from the pipe, so close reading fd*/
      /*close(read_pipe);*/
      return 0;
   }
   if ((pid=fork_process(PROC_NOCHLDINIT,"SEAS",0))<0) {
      LM_ERR("forking failed\n");
      return -1;
   }
   if (!pid) {
      /*dispatcher child. we leave writing end open so that new childs spawned
       * by event dispatcher can also write to pipe.. */

		/* initialize the config framework */
		if (cfg_child_init())
			return -1;

      /* close(write_pipe); */
      return dispatcher_main_loop();
   }
   return 0;
}
示例#12
0
/**
 * \brief Forks a separate simple milisecond-sleep() -&- sync periodic timer
 *
 * Forks a very basic periodic timer process, that just ms-sleep()s for 
 * the specified interval and then calls the timer function.
 * The new "sync timer" process execution start immediately, the ms-sleep()
 * is called first (so the first call to the timer function will happen
 * \<interval\> seconds after the call to fork_basic_utimer)
 * @param child_id  @see fork_process()
 * @param desc      @see fork_process()
 * @param make_sock @see fork_process()
 * @param f         timer function/callback
 * @param param     parameter passed to the timer function
 * @param uinterval  interval in mili-seconds.
 * @return pid of the new process on success, -1 on error
 * (doesn't return anything in the child process)
 */
int fork_sync_utimer(int child_id, char* desc, int make_sock,
						utimer_function* f, void* param, int uinterval)
{
	int pid;
	ticks_t ts1 = 0;
	ticks_t ts2 = 0;

	pid=fork_process(child_id, desc, make_sock);
	if (pid<0) return -1;
	if (pid==0){
		/* child */
		ts2 = uinterval;
		if (cfg_child_init()) return -1;
		for(;;){
			if(ts2>0) sleep_us(uinterval);
			else sleep_us(1);
			ts1 = get_ticks_raw();
			cfg_update();
			f(TICKS_TO_MS(ts1), param); /* ticks in mili-seconds */
			ts2 = uinterval - get_ticks_raw() + ts1;
		}
	}
	/* parent */
	return pid;
}
示例#13
0
/**
 * Initializes the module in child.
 */
static int mod_child_init(int rank) {
    LM_DBG("Initialization of module in child [%d] \n", rank);

    if (rank == PROC_MAIN) {
        int pid = fork_process(PROC_SIPINIT, "Rx Event Processor", 1);
        if (pid < 0)
            return -1; //error
        if (pid == 0) {
            if (cfg_child_init())
                return -1; //error
            cdp_cb_event_process();
        }
    }
    /* don't do anything for main process and TCP manager process */
    if (rank == PROC_MAIN || rank == PROC_TCP_MAIN) {
        return 0;
    }

    lock_get(process_lock);
    if ((*callback_singleton) == 0) {
        *callback_singleton = 1;
        cdpb.AAAAddRequestHandler(callback_cdp_request, NULL);
    }
    lock_release(process_lock);

    return 0;
}
示例#14
0
文件: timer.c 项目: kamailio/kamailio
/* arm the "slow" timer ( start it)
 * returns -1 on error
 * WARNING: use it in the same process as the timer
 *  (the one using pause(); timer_handler()) or
 *  change run_timer to a pointer in shared mem */
int arm_slow_timer()
{
	sigemptyset(&slow_timer_sset);
	sigaddset(&slow_timer_sset, SLOW_TIMER_SIG);
again:
	if (sigprocmask(SIG_BLOCK, &slow_timer_sset, 0)==-1){
		if (errno==EINTR) goto again;
		LM_ERR("sigprocmask failed: %s [%d]}n", strerror(errno), errno);
		goto error;
	}
#ifdef __OS_darwin
	/* workaround for darwin sigwait bug, see slow_timer_main() for more
	 * info (or grep __OS_darwin) */
	/* keep in sync wih main.c: sig_usr() - signals we are interested in */
	sigaddset(&slow_timer_sset, SIGINT);
	sigaddset(&slow_timer_sset, SIGTERM);
	sigaddset(&slow_timer_sset, SIGUSR1);
	sigaddset(&slow_timer_sset, SIGHUP);
	sigaddset(&slow_timer_sset, SIGCHLD);
	sigaddset(&slow_timer_sset, SIGALRM);
#endif
	/* initialize the config framework */
	if (cfg_child_init()) goto error;

	return 0;
error:
	return -1;
}
示例#15
0
文件: timer.c 项目: kamailio/kamailio
/* arm the timer ( start it)
 * returns -1 on error
 * WARNING: use it in the same process as the timer
 *  (the one using pause(); timer_handler()) or
 *  change run_timer to a pointer in shared mem */
int arm_timer()
{
	struct itimerval it;
	/* init signal generation */
	it.it_interval.tv_sec=0;
	it.it_interval.tv_usec=1000000/TIMER_TICKS_HZ;
	it.it_value=it.it_interval;
	/* install the signal handler */
	if (set_sig_h(SIGALRM, sig_timer) == SIG_ERR ){
		LM_CRIT("SIGALRM signal handler cannot be installed: %s [%d]\n",
					strerror(errno), errno);
		return -1;
	}
	if (setitimer(ITIMER_REAL, &it, 0) == -1){
		LM_CRIT("setitimer failed: %s [%d]\n", strerror(errno), errno);
		return -1;
	}
	if (gettimeofday(&last_time, 0)<0){
		LM_ERR("gettimeofday failed: %s [%d]\n", strerror(errno), errno);
		return -1;
	}
	/* initialize the config framework */
	if (cfg_child_init()) return -1;

	return 0;
}
示例#16
0
/**
 * \brief Forks a separate simple sleep() -&- sync periodic timer
 *
 * Forks a very basic periodic timer process, that just sleep()s for 
 * the specified interval and then calls the timer function.
 * The new "sync timer" process execution start immediately, the sleep()
 * is called first (so the first call to the timer function will happen
 * \<interval\> seconds after the call to fork_sync_timer)
 * @param child_id  @see fork_process()
 * @param desc      @see fork_process()
 * @param make_sock @see fork_process()
 * @param f         timer function/callback
 * @param param     parameter passed to the timer function
 * @param interval  interval in seconds.
 * @return pid of the new process on success, -1 on error
 * (doesn't return anything in the child process)
 */
int fork_sync_timer(int child_id, char* desc, int make_sock,
						timer_function* f, void* param, int interval)
{
	int pid;
	ticks_t ts1 = 0;
	ticks_t ts2 = 0;

	pid=fork_process(child_id, desc, make_sock);
	if (pid<0) return -1;
	if (pid==0){
		/* child */
		interval *= 1000;  /* miliseconds */
		ts2 = interval;
		if (cfg_child_init()) return -1;
		for(;;){
			if (ts2>interval)
				sleep_us(1000);    /* 1 milisecond sleep to catch up */
			else
				sleep_us(ts2*1000); /* microseconds sleep */
			ts1 = get_ticks_raw();
			cfg_update();
			f(TICKS_TO_S(ts1), param); /* ticks in sec for compatibility with old
									  timers */
			/* adjust the next sleep duration */
			ts2 = interval - TICKS_TO_MS(get_ticks_raw()) + TICKS_TO_MS(ts1);
		}
	}
	/* parent */
	return pid;
}
示例#17
0
/**
 * @brief Initialize async module children
 */
static int child_init(int rank)
{
	int pid;
	int i;

	LM_DBG("child initializing async http\n");

	if(num_workers<=0)
		return 0;

	if (rank==PROC_INIT) {
		for(i=0; i<num_workers; i++) {
			LM_DBG("initializing worker notification socket: %d\n", i);
			if(async_http_init_sockets(&workers[i])<0) {
				LM_ERR("failed to initialize tasks sockets\n");
				return -1;
			}
		}

		return 0;
	}

	if(rank>0) {
		for(i=0; i<num_workers; i++) {
			close(workers[i].notication_socket[0]);
		}
		return 0;
	}
	if (rank!=PROC_MAIN)
		return 0;

	for(i=0; i<num_workers; i++) {
		if(async_http_init_worker(i+1, &workers[i])<0) {
			LM_ERR("failed to initialize worker process: %d\n", i);
			return -1;
		}
		pid=fork_process(PROC_RPC, "Http Worker", 1);
		if (pid<0)
			return -1; /* error */
		if(pid==0) {
			/* child */
			/* enforce http_reply_parse=yes */
			http_reply_parse = 1;
			/* initialize the config framework */
			if (cfg_child_init())
				return -1;
			/* init msg structure for http reply parsing */
			ah_reply = pkg_malloc(sizeof(struct sip_msg));
			if(!ah_reply) {
				LM_ERR("failed to allocate pkg memory\n");
				return -1;
			}
			memset(ah_reply, 0, sizeof(struct sip_msg));
			/* main function for workers */
			async_http_run_worker(&workers[i]);
		}
	}

	return 0;
}
示例#18
0
文件: mi_fifo.c 项目: gbour/kamailio
/*! \brief Initialize module for child processes */
static int mi_child_init(int rank)
{
	int pid;

	if (rank==PROC_TIMER || rank>0 ) {
		if ( mi_writer_init(read_buf_size, mi_reply_indent)!=0 ) {
			LM_CRIT("failed to init the reply writer\n");
			return -1;
		}
	}

	if (rank==PROC_MAIN) {
		pid=fork_process(PROC_NOCHLDINIT, "MI FIFO", 1);
		if (pid<0)
			return -1; /* error */
		if(pid==0){
			/* child */

			/* initialize the config framework */
			if (cfg_child_init())
				return -1;

			fifo_process(1);
		}
	}

	return 0;
}
示例#19
0
static int child_init(int rank)
{
	int pid;
	if (rank!=PROC_MAIN)
		return 0;

	if(!reg_db_url.s || reg_db_url.len<=0)
		return 0;

	pid=fork_process(PROC_TIMER, "TIMER UAC REG", 1);
	if (pid<0)
	{
		LM_ERR("failed to register timer routine as process\n");
		return -1;
	}
	if (pid==0){
		/* child */
		/* initialize the config framework */
		if (cfg_child_init())
			return -1;

		uac_reg_load_db();
		uac_reg_timer(0);
		for(;;){
			/* update the local config framework structures */
			cfg_update();

			sleep(reg_timer_interval);
			uac_reg_timer(get_ticks());
		}
	}
	/* parent */
	return 0;
}
示例#20
0
static int child_init(int rank) {
    int pid;
    int k=0;
    
    LM_DBG("Initialization of module in child [%d] \n", rank);

if (rank == PROC_MAIN) {
    
        /* fork notification workers */
	for(k=0;k<notification_processes;k++){
		pid = fork_process(1001+k,"notification_worker",1);
		if (pid==-1){
			LM_CRIT("init_notification_worker(): Error on fork() for worker!\n");
			return 0;
		}
		if (pid==0) {
			if (cfg_child_init()) return 0;
			notification_event_process();
			LM_CRIT("init_notification_worker():: worker_process finished without exit!\n");
			exit(-1);
		}
	}
    }
    
    
    if (rank == PROC_MAIN || rank == PROC_TCP_MAIN)
        return 0;
    if (rank == 1) {
        /* init stats */
        //TODO if parameters are modified via cfg framework do i change them?
        update_stat(max_expires_stat, default_registrar_cfg.max_expires);
        update_stat(max_contacts_stat, default_registrar_cfg.max_contacts);
        update_stat(default_expire_stat, default_registrar_cfg.default_expires);
    }
    /* don't do anything for main process and TCP manager process */

    /* don't do anything for main process and TCP manager process */
    if (rank == PROC_MAIN || rank == PROC_TCP_MAIN)
        return 0;

    /* Init the user data parser */
    if (!parser_init(scscf_user_data_dtd, scscf_user_data_xsd))
        return -1;
    
    
    lock_get(process_lock);
    if ((*callback_singleton) == 0) {
        *callback_singleton = 1;
        cdpb.AAAAddRequestHandler(callback_cdp_request, NULL);
    }
    lock_release(process_lock);

    return 0;
}
示例#21
0
int async_task_child_init(int rank)
{
	int pid;
	int i;

	if(_async_task_workers<=0)
		return 0;

	LM_DBG("child initializing asynk task framework\n");

	if (rank==PROC_INIT) {
		if(async_task_init_sockets()<0) {
			LM_ERR("failed to initialize tasks sockets\n");
			return -1;
		}
		return 0;
	}

	if(rank>0) {
		async_task_close_sockets_parent();
		return 0;
	}
	if (rank!=PROC_MAIN)
		return 0;

	for(i=0; i<_async_task_workers; i++) {
		pid=fork_process(PROC_RPC, "Async Task Worker", 1);
		if (pid<0)
			return -1; /* error */
		if(pid==0) {
			/* child */

			/* initialize the config framework */
			if (cfg_child_init())
				return -1;
			/* main function for workers */
			if(async_task_run(i+1)<0) {
				LM_ERR("failed to initialize task worker process: %d\n", i);
				return -1;
			}
		}
	}

	return 0;
}
示例#22
0
/**
 * \brief Forks a separate simple sleep() periodic timer
 * 
 * Forks a very basic periodic timer process, that just sleep()s for 
 * the specified interval and then calls the timer function.
 * The new "basic timer" process execution start immediately, the sleep()
 * is called first (so the first call to the timer function will happen
 * \<interval\> seconds after the call to fork_basic_timer)
 * @param child_id  @see fork_process()
 * @param desc      @see fork_process()
 * @param make_sock @see fork_process()
 * @param f         timer function/callback
 * @param param     parameter passed to the timer function
 * @param interval  interval in seconds.
 * @return pid of the new process on success, -1 on error
 * (doesn't return anything in the child process)
 */
int fork_basic_timer(int child_id, char* desc, int make_sock,
						timer_function* f, void* param, int interval)
{
	int pid;
	
	pid=fork_process(child_id, desc, make_sock);
	if (pid<0) return -1;
	if (pid==0){
		/* child */
		if (cfg_child_init()) return -1;
		for(;;){
			sleep(interval);
			cfg_update();
			f(get_ticks(), param); /* ticks in s for compatibility with old
									  timers */
		}
	}
	/* parent */
	return pid;
}
示例#23
0
/**
 * initialize child processes
 */
static int child_init(int rank)
{
	int pid;

	if (rank==PROC_MAIN) {
		pid=fork_process(PROC_NOCHLDINIT, "XMPP Manager", 1);
		if (pid<0)
			return -1; /* error */
		if(pid==0){
			/* child */
			/* initialize the config framework */
			if (cfg_child_init())
				return -1;

			xmpp_process(1);
		}
	}

	return 0;
}
示例#24
0
static int mi_child_init(int rank)
{
	int i;
	int pid;

	if (rank==PROC_TIMER || rank>0 ) {
		if(mi_datagram_writer_init( DATAGRAM_SOCK_BUF_SIZE , mi_reply_indent )!= 0) {
			LM_CRIT("failed to initiate mi_datagram_writer\n");
			return -1;
		}
	}
	if (rank==PROC_MAIN) {
		if(pre_datagram_process()!=0)
		{
			LM_ERR("pre-fork function failed\n");
			return -1;
		}
		for(i=0; i<mi_procs[0].no; i++)
		{
			pid=fork_process(PROC_NOCHLDINIT, "MI DATAGRAM", 1);
			if (pid<0)
				return -1; /* error */
			if(pid==0) {
				/* child */

				/* initialize the config framework */
				if (cfg_child_init())
					return -1;

				datagram_process(i);
				return 0;
			}
		}
		if(post_datagram_process()!=0)
		{
			LM_ERR("post-fork function failed\n");
			return -1;
		}
	}
	return 0;
}
示例#25
0
/**
 * \brief Forks a separate simple milisecond-sleep() periodic timer
 * 
 * Forks a very basic periodic timer process, that just ms-sleep()s for 
 * the specified interval and then calls the timer function.
 * The new "basic timer" process execution start immediately, the ms-sleep()
 * is called first (so the first call to the timer function will happen
 * \<interval\> seconds after the call to fork_basic_utimer)
 * @param child_id  @see fork_process()
 * @param desc      @see fork_process()
 * @param make_sock @see fork_process()
 * @param f         timer function/callback
 * @param param     parameter passed to the timer function
 * @param uinterval  interval in mili-seconds.
 * @return pid of the new process on success, -1 on error
 * (doesn't return anything in the child process)
 */
int fork_basic_utimer(int child_id, char* desc, int make_sock,
						utimer_function* f, void* param, int uinterval)
{
	int pid;
	ticks_t ts;
	
	pid=fork_process(child_id, desc, make_sock);
	if (pid<0) return -1;
	if (pid==0){
		/* child */
		if (cfg_child_init()) return -1;
		for(;;){
			sleep_us(uinterval);
			cfg_update();
			ts = get_ticks_raw();
			f(TICKS_TO_MS(ts), param); /* ticks in mili-seconds */
		}
	}
	/* parent */
	return pid;
}
示例#26
0
/**
 * Start the CDiameterPeer operations.
 * It forks all the processes required.
 * @param blocking - if this is set, use the calling processes for the timer and never
 * return; else fork a new one for the timer and return
 * @returns 1 on success, 0 on error, never if blocking
 */
int diameter_peer_start(int blocking)
{
	int pid;
	int k=0;
	peer *p;

	/* fork workers */
	for(k=0;k<config->workers;k++){
		pid = fork_process(1001+k,"cdp_worker",1);
		if (pid==-1){
			LM_CRIT("init_diameter_peer(): Error on fork() for worker!\n");
			return 0;
		}
		if (pid==0) {
			srandom(time(0)*k);
			snprintf(pt[process_no].desc, MAX_PT_DESC,"cdp worker child=%d", k );
			if (cfg_child_init()) return 0;
			worker_process(k);
			LM_CRIT("init_diameter_peer(): worker_process finished without exit!\n");
			exit(-1);
		}else{
			dp_add_pid(pid);
		}
	}

	/* init the fd_exchange pipes */
	receiver_init(NULL);
	for(p = peer_list->head,k=0;p;p=p->next,k++)
		receiver_init(p);


	/* fork receiver for unknown peers */

	pid = fork_process(1001+k,"cdp_receiver_peer_unkown",1);

	if (pid==-1){
		LM_CRIT("init_diameter_peer(): Error on fork() for unknown peer receiver!\n");
		return 0;
	}
	if (pid==0) {
		srandom(time(0)*k);
		snprintf(pt[process_no].desc, MAX_PT_DESC,
				"cdp receiver peer unknown");
		if (cfg_child_init()) return 0;
		receiver_process(NULL);
		LM_CRIT("init_diameter_peer(): receiver_process finished without exit!\n");
		exit(-1);
	}else{
		dp_add_pid(pid);
	}

	/* fork receivers for each pre-configured peers */
	lock_get(peer_list_lock);
	for(p = peer_list->head,k=-1;p;p = p->next,k--){
		pid = fork_process(1001+k,"cdp_receiver_peer",1);
		if (pid==-1){
			LM_CRIT("init_diameter_peer(): Error on fork() for peer receiver!\n");
			return 0;
		}
		if (pid==0) {
			srandom(time(0)*k);
				snprintf(pt[process_no].desc, MAX_PT_DESC,
					"cdp_receiver_peer=%.*s", p->fqdn.len,p->fqdn.s );
			if (cfg_child_init()) return 0;
			receiver_process(p);
			LM_CRIT("init_diameter_peer(): receiver_process finished without exit!\n");
			exit(-1);
		}else{
			dp_add_pid(pid);
		}
	}
	lock_release(peer_list_lock);


	/* Fork the acceptor process (after receivers, so it inherits all the right sockets) */
	pid = fork_process(1000,"cdp_acceptor",1);

	if (pid==-1){
		LM_CRIT("init_diameter_peer(): Error on fork() for acceptor!\n");
		return 0;
	}
	if (pid==0) {
		if (cfg_child_init()) return 0;
		acceptor_process(config);
		LM_CRIT("init_diameter_peer(): acceptor_process finished without exit!\n");
		exit(-1);
	}else{
		dp_add_pid(pid);
	}

	/* fork/become timer */
	if (blocking) {
		dp_add_pid(getpid());
		if (cfg_child_init()) return 0;
		timer_process(1);
	}
	else{
		pid = fork_process(1001,"cdp_timer",1);
		if (pid==-1){
			LM_CRIT("init_diameter_peer(): Error on fork() for timer!\n");
			return 0;
		}
		if (pid==0) {
			if (cfg_child_init()) return 0;
			timer_process(0);
			LM_CRIT("init_diameter_peer(): timer_process finished without exit!\n");
			exit(-1);
		}else{
			dp_add_pid(pid);
		}
	}

	return 1;
}
示例#27
0
文件: jabber.c 项目: 4N7HR4X/kamailio
/*
 * Initialize children
 */
static int child_init(int rank)
{
	int i, j, mpid, cpid;

	DBG("XJAB:child_init: initializing child <%d>\n", rank);
	     /* Rank 0 is main process now - 1 is the first child (janakj) */
	if(rank == 1)
	{
#ifdef HAVE_IHTTP
		/** register iHTTP callbacks -- go forward in any case*/
		ihb.reg_f("xjab", "XMPP Gateway", IH_MENU_YES,
				xjab_mod_info, NULL);
		ihb.reg_f("xjabc", "XMPP connections", IH_MENU_YES,
				xjab_connections, NULL);
#endif
		if((mpid=fork())<0 )
		{
			LOG(L_ERR, "XJAB:child_init:error - cannot launch worker's"
					" manager\n");
			return -1;
		}
		if(mpid == 0)
		{
			/** launching the workers */
			for(i=0;i<nrw;i++)
			{
				if ( (cpid=fork())<0 )
				{
					LOG(L_ERR,"XJAB:child_init:error - cannot launch worker\n");
					return -1;
				}
				if (cpid == 0)
				{
					for(j=0;j<nrw;j++)
						if(j!=i) close(pipes[j][0]);
					close(pipes[i][1]);
					if(xj_wlist_set_pid(jwl, getpid(), i) < 0)
					{
						LOG(L_ERR, "XJAB:child_init:error setting worker's"
										" pid\n");
						return -1;
					}

					/* initialize the config framework */
					if (cfg_child_init()) return -1;

					ctx = db_ctx("jabber");
					if (ctx == NULL) goto dberror;
					if (db_add_db(ctx, db_url) < 0) goto dberror;
					if (db_connect(ctx) < 0) goto dberror;

					cmd = db_cmd(DB_GET, ctx, db_table, db_cols, db_params, NULL);
					if (!cmd) goto dberror;

					xj_worker_process(jwl,jaddress,jport,i, cmd);

					db_cmd_free(cmd);
					db_ctx_free(ctx);
					ctx = NULL;

					/* destroy the local config */
					cfg_child_destroy();

					exit(0);
				}
			}

			mpid = getpid();

			/* initialize the config framework */
			if (cfg_child_init()) return -1;

			while(1)
			{
				sleep(check_time);

				/* update the local config */
				cfg_update();

				xjab_check_workers(mpid);
			}
		}
	}

	//if(pipes)
	//{
	//	for(i=0;i<nrw;i++)
	//		close(pipes[i][0]);
	//}
	return 0;

 dberror:
	if (cmd) db_cmd_free(cmd);
	cmd = NULL;
	if (ctx) db_ctx_free(ctx);
	ctx = NULL;
	return -1;
}
示例#28
0
void tcp_receive_loop(int unix_sock)
{
	
	/* init */
	tcpmain_sock=unix_sock; /* init com. socket */
	if (init_io_wait(&io_w, get_max_open_fds(), tcp_poll_method)<0)
		goto error;
	tcp_reader_prev_ticks=get_ticks_raw();
	if (init_local_timer(&tcp_reader_ltimer, get_ticks_raw())!=0)
		goto error;
	/* add the unix socket */
	if (io_watch_add(&io_w, tcpmain_sock, POLLIN,  F_TCPMAIN, 0)<0){
		LM_CRIT("failed to add socket to the fd list\n");
		goto error;
	}

	/* initialize the config framework */
	if (cfg_child_init()) goto error;

	/* main loop */
	switch(io_w.poll_method){
		case POLL_POLL:
				while(1){
					io_wait_loop_poll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
					tcp_reader_timer_run();
				}
				break;
#ifdef HAVE_SELECT
		case POLL_SELECT:
			while(1){
				io_wait_loop_select(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
				tcp_reader_timer_run();
			}
			break;
#endif
#ifdef HAVE_SIGIO_RT
		case POLL_SIGIO_RT:
			while(1){
				io_wait_loop_sigio_rt(&io_w, TCP_CHILD_SELECT_TIMEOUT);
				tcp_reader_timer_run();
			}
			break;
#endif
#ifdef HAVE_EPOLL
		case POLL_EPOLL_LT:
			while(1){
				io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
				tcp_reader_timer_run();
			}
			break;
		case POLL_EPOLL_ET:
			while(1){
				io_wait_loop_epoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 1);
				tcp_reader_timer_run();
			}
			break;
#endif
#ifdef HAVE_KQUEUE
		case POLL_KQUEUE:
			while(1){
				io_wait_loop_kqueue(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
				tcp_reader_timer_run();
			}
			break;
#endif
#ifdef HAVE_DEVPOLL
		case POLL_DEVPOLL:
			while(1){
				io_wait_loop_devpoll(&io_w, TCP_CHILD_SELECT_TIMEOUT, 0);
				tcp_reader_timer_run();
			}
			break;
#endif
		default:
			LM_CRIT("no support for poll method %s (%d)\n", 
					poll_method_name(io_w.poll_method), io_w.poll_method);
			goto error;
	}
error:
	destroy_io_wait(&io_w);
	LM_CRIT("exiting...");
	exit(-1);
}
示例#29
0
static int mod_child_init(int rank)
{
	int pid;
	int i;
	kz_amqp_zone_ptr g;
	kz_amqp_server_ptr s;

	fire_init_event(rank);

	if (rank==PROC_INIT || rank==PROC_TCP_MAIN)
		return 0;

//	if (rank>PROC_MAIN)
//		kz_cmd_pipe = kz_cmd_pipe_fds[1];


	if (rank==PROC_MAIN) {
		/*
		pid=fork_process(PROC_NOCHLDINIT, "AMQP Timer", 0);
		if (pid<0)
			return -1;
		if(pid==0){
			return(kz_amqp_timeout_proc());
		}
		*/

		for(i=0; i < dbk_consumer_workers; i++) {
			pid=fork_process(i+1, "AMQP Consumer Worker", 1);
			if (pid<0)
				return -1; /* error */
			if(pid==0){
				if (cfg_child_init()) return -1;
				close(kz_worker_pipes_fds[i*2+1]);
				return(kz_amqp_consumer_worker_proc(kz_worker_pipes_fds[i*2]));
			}
		}

		for (g = kz_amqp_get_zones(); g != NULL; g = g->next) {
			int w = (g == kz_amqp_get_primary_zone() ? dbk_consumer_processes : 1);
			for(i=0; i < w; i++) {
				for (s = g->servers->head; s != NULL; s = s->next) {
					pid=fork_process(PROC_NOCHLDINIT, "AMQP Consumer", 0);
					if (pid<0)
						return -1; /* error */
					if(pid==0){
						if (cfg_child_init()) return -1;
						return(kz_amqp_consumer_proc(s));
					}
				}
			}
		}

		pid=fork_process(PROC_NOCHLDINIT, "AMQP Publisher", 1);
		if (pid<0)
			return -1; /* error */
		if(pid==0){
			if (cfg_child_init()) return -1;
			close(kz_cmd_pipe_fds[1]);
			kz_amqp_publisher_proc(kz_cmd_pipe_fds[0]);
		}
		return 0;
	}

	if(dbk_pua_mode == 1) {
		if (kz_pa_dbf.init==0)
		{
			LM_CRIT("child_init: database not bound\n");
			return -1;
		}
		kz_pa_db = kz_pa_dbf.init(&kz_db_url);
		if (!kz_pa_db)
		{
			LM_ERR("child %d: unsuccessful connecting to database\n", rank);
			return -1;
		}

		if (kz_pa_dbf.use_table(kz_pa_db, &kz_presentity_table) < 0)
		{
			LM_ERR( "child %d:unsuccessful use_table presentity_table\n", rank);
			return -1;
		}
		LM_DBG("child %d: Database connection opened successfully\n", rank);
	}

	return 0;
}