Skip to content

Commit

Permalink
Script out running of setup routine in the main process.
Browse files Browse the repository at this point in the history
  • Loading branch information
sobomax committed Jun 16, 2023
1 parent c678985 commit b323136
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 137 deletions.
190 changes: 53 additions & 137 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@

#include "ssl_tweaks.h"

#include "main_script.h"

/* global vars */

#ifdef VERSION_NODATE
Expand Down Expand Up @@ -195,6 +197,49 @@ static void print_ct_constants(void)
#endif
}

static int
init_suid(void)
{
/* all processes should have access to all the sockets (for sending)
* so we open all first*/
return do_suid(user_id, group_id);
}

static int
init_shm_mem_dbg(void)
{
if (shm_memlog_size)
shm_mem_enable_dbg();
return 0;
}

static const struct main_script main_script[] = {
FN_HNDLR(init_reactor_size, <, 0, "internal reactor"),
FN_HNDLR(init_timer, <, 0, "timer"),
FN_HNDLR(init_shm_mem_dbg, <, 0, "shm_mem_enable_dbg"),
FN_HNDLR(init_ipc, <, 0, "IPC support"),
FN_HNDLR(init_serialization, !=, 0, "serialization"),
FN_HNDLR(init_mi_core, <, 0, "MI core"),
FN_HNDLR(evi_register_core, !=, 0, "register core events"),
FN_HNDLR(init_black_lists, !=, 0, "black list engine"),
FN_HNDLR(resolv_blacklist_init, !=, 0, "resolver's blacklist"),
FN_HNDLR(init_dset, !=, 0, "SIP forking logic"),
FN_HNDLR(init_db_support, !=, 0, "SQL database support"),
FN_HNDLR(init_cdb_support, !=, 0, "CacheDB support"),
FN_HNDLR(init_modules, !=, 0, "modules"),
FN_HNDLR(init_xlog, <, 0, "xlog"),
FN_HNDLR(register_route_timers, <, 0, "route_timers"),
FN_HNDLR(init_pvar_support, !=, 0, "pseudo-variable support"),
FN_HNDLR(init_multi_proc_support, !=, 0, "multi processes support"),
FN_HNDLR(init_extra_avps, !=, 0, "avps"),
FN_HNDLR(fix_rls, !=, 0, "routing lists"),
FN_HNDLR(init_log_level, !=, 0, "logging levels"),
FN_HNDLR(init_log_event_cons, <, 0, "log event consumer"),
FN_HNDLR(trans_init_all_listeners, <, 0, "all SIP listeners"),
FN_HNDLR(init_script_reload, <, 0, "cfg reload ctx"),
FN_HNDLR(init_suid, ==, -1, "do_suid"),
FN_HNDLR(run_post_fork_handlers, <, 0, "post-fork handlers"),
};

/**
* Main loop, forks the children, bind to addresses,
Expand Down Expand Up @@ -344,7 +389,7 @@ static int main_loop(void)
*/
int main(int argc, char** argv)
{
int c,r;
int c;
char *tmp;
int tmp_len;
int port;
Expand Down Expand Up @@ -828,142 +873,13 @@ int main(int argc, char** argv)
LM_NOTICE("using system memory for private process memory\n");
#endif

/* init async reactor */
if (init_reactor_size()<0) {
LM_CRIT("failed to init internal reactor, exiting...\n");
goto error;
}

/* init timer */
if (init_timer()<0){
LM_CRIT("could not initialize timer, exiting...\n");
goto error;
}

if (shm_memlog_size)
shm_mem_enable_dbg();

/* init IPC */
if (init_ipc()<0){
LM_CRIT("could not initialize IPC support, exiting...\n");
goto error;
}

/* init serial forking engine */
if (init_serialization()!=0) {
LM_ERR("failed to initialize serialization\n");
goto error;
}
/* Init MI */
if (init_mi_core()<0) {
LM_ERR("failed to initialize MI core\n");
goto error;
}

/* Register core events */
if (evi_register_core() != 0) {
LM_ERR("failed register core events\n");
goto error;
}

/* init black list engine */
if (init_black_lists()!=0) {
LM_CRIT("failed to init blacklists\n");
goto error;
}
/* init resolver's blacklist */
if (resolv_blacklist_init()!=0) {
LM_CRIT("failed to create DNS blacklist\n");
goto error;
}

if (init_dset() != 0) {
LM_ERR("failed to initialize SIP forking logic!\n");
goto error;
}

/* init SQL DB support */
if (init_db_support() != 0) {
LM_ERR("failed to initialize SQL database support\n");
goto error;
}

/* init CacheDB support */
if (init_cdb_support() != 0) {
LM_ERR("failed to initialize CacheDB support\n");
goto error;
}

/* init modules */
if (init_modules() != 0) {
LM_ERR("error while initializing modules\n");
goto error;
}

/* init xlog */
if (init_xlog() < 0) {
LM_ERR("error while initializing xlog!\n");
goto error;
}

/* register route timers */
if(register_route_timers() < 0) {
LM_ERR("Failed to register timer\n");
goto error;
}

/* init pseudo-variable support */
if (init_pvar_support() != 0) {
LM_ERR("failed to init pvar support\n");
goto error;
}

/* init multi processes support */
if (init_multi_proc_support()!=0) {
LM_ERR("failed to init multi-proc support\n");
goto error;
}

/* init avps */
if (init_extra_avps() != 0) {
LM_ERR("error while initializing avps\n");
goto error;
}

/* fix routing lists */
if ( (r=fix_rls())!=0){
LM_ERR("failed to fix configuration with err code %d\n", r);
goto error;
}

if (init_log_level() != 0) {
LM_ERR("failed to init logging levels\n");
goto error;
}

if (init_log_event_cons() < 0) {
LM_ERR("Failed to initialize log event consumer\n");
goto error;
}

if (trans_init_all_listeners()<0) {
LM_ERR("failed to init all SIP listeners, aborting\n");
goto error;
}

if (init_script_reload()<0) {
LM_ERR("failed to init cfg reload ctx, aborting\n");
goto error;
}

/* all processes should have access to all the sockets (for sending)
* so we open all first*/
if (do_suid(user_id, group_id)==-1)
goto error;

if (run_post_fork_handlers()<0) {
LM_ERR("failed to init post-fork handlers\n");
goto error;
for (int n = 0; n < howmany(main_script, main_script[0]); n++) {
int result = main_script[n].hndlr();
pred_cmp_f pred_cmp = cmps_ops[main_script[n].pval];
if (pred_cmp(result, main_script[n].pred)) {
LM_ERR("failed to initialize %s!\n", main_script[n].desc);
goto error;
}
}

ret = main_loop();
Expand Down
56 changes: 56 additions & 0 deletions main_script.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2023 Sippy Software, Inc., http://www.sippysoft.com
*
* This file is part of opensips, a free SIP server.
*
* opensips 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
*
* opensips 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
*
*/

#pragma once

typedef int (*pred_cmp_f)(int, int);

struct main_script {
int (*hndlr)(void);
int pval;
int pred;
const char *desc;
};

static int op_neq(int r, int e) { return (r != e); }
static int op_eq(int r, int e) { return (r == e); }
static int op_lt(int r, int e) { return (r < e); }
static int op_lte(int r, int e) { return (r <= e); }
static int op_gt(int r, int e) { return (r > e); }
static int op_gte(int r, int e) { return (r >= e); }

#define opidx(lop) (((0 lop 0) << 2) | ((0 lop 1) << 1) | (1 lop 0))

static const pred_cmp_f cmps_ops[] = {
[opidx(!=)] = op_neq,
[opidx(==)] = op_eq,
[opidx(<)] = op_lt,
[opidx(>)] = op_gt,
[opidx(<=)] = op_lte,
[opidx(>=)] = op_gte
};

#define FN_HNDLR(h, lop, p, d) (struct main_script){ \
.hndlr = (h), \
.pval = opidx(lop), \
.pred = (p), \
.desc = (d), \
}

0 comments on commit b323136

Please sign in to comment.