Skip to content

Commit b323136

Browse files
committed
Script out running of setup routine in the main process.
1 parent c678985 commit b323136

File tree

2 files changed

+109
-137
lines changed

2 files changed

+109
-137
lines changed

main.c

Lines changed: 53 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@
151151

152152
#include "ssl_tweaks.h"
153153

154+
#include "main_script.h"
155+
154156
/* global vars */
155157

156158
#ifdef VERSION_NODATE
@@ -195,6 +197,49 @@ static void print_ct_constants(void)
195197
#endif
196198
}
197199

200+
static int
201+
init_suid(void)
202+
{
203+
/* all processes should have access to all the sockets (for sending)
204+
* so we open all first*/
205+
return do_suid(user_id, group_id);
206+
}
207+
208+
static int
209+
init_shm_mem_dbg(void)
210+
{
211+
if (shm_memlog_size)
212+
shm_mem_enable_dbg();
213+
return 0;
214+
}
215+
216+
static const struct main_script main_script[] = {
217+
FN_HNDLR(init_reactor_size, <, 0, "internal reactor"),
218+
FN_HNDLR(init_timer, <, 0, "timer"),
219+
FN_HNDLR(init_shm_mem_dbg, <, 0, "shm_mem_enable_dbg"),
220+
FN_HNDLR(init_ipc, <, 0, "IPC support"),
221+
FN_HNDLR(init_serialization, !=, 0, "serialization"),
222+
FN_HNDLR(init_mi_core, <, 0, "MI core"),
223+
FN_HNDLR(evi_register_core, !=, 0, "register core events"),
224+
FN_HNDLR(init_black_lists, !=, 0, "black list engine"),
225+
FN_HNDLR(resolv_blacklist_init, !=, 0, "resolver's blacklist"),
226+
FN_HNDLR(init_dset, !=, 0, "SIP forking logic"),
227+
FN_HNDLR(init_db_support, !=, 0, "SQL database support"),
228+
FN_HNDLR(init_cdb_support, !=, 0, "CacheDB support"),
229+
FN_HNDLR(init_modules, !=, 0, "modules"),
230+
FN_HNDLR(init_xlog, <, 0, "xlog"),
231+
FN_HNDLR(register_route_timers, <, 0, "route_timers"),
232+
FN_HNDLR(init_pvar_support, !=, 0, "pseudo-variable support"),
233+
FN_HNDLR(init_multi_proc_support, !=, 0, "multi processes support"),
234+
FN_HNDLR(init_extra_avps, !=, 0, "avps"),
235+
FN_HNDLR(fix_rls, !=, 0, "routing lists"),
236+
FN_HNDLR(init_log_level, !=, 0, "logging levels"),
237+
FN_HNDLR(init_log_event_cons, <, 0, "log event consumer"),
238+
FN_HNDLR(trans_init_all_listeners, <, 0, "all SIP listeners"),
239+
FN_HNDLR(init_script_reload, <, 0, "cfg reload ctx"),
240+
FN_HNDLR(init_suid, ==, -1, "do_suid"),
241+
FN_HNDLR(run_post_fork_handlers, <, 0, "post-fork handlers"),
242+
};
198243

199244
/**
200245
* Main loop, forks the children, bind to addresses,
@@ -344,7 +389,7 @@ static int main_loop(void)
344389
*/
345390
int main(int argc, char** argv)
346391
{
347-
int c,r;
392+
int c;
348393
char *tmp;
349394
int tmp_len;
350395
int port;
@@ -828,142 +873,13 @@ int main(int argc, char** argv)
828873
LM_NOTICE("using system memory for private process memory\n");
829874
#endif
830875

831-
/* init async reactor */
832-
if (init_reactor_size()<0) {
833-
LM_CRIT("failed to init internal reactor, exiting...\n");
834-
goto error;
835-
}
836-
837-
/* init timer */
838-
if (init_timer()<0){
839-
LM_CRIT("could not initialize timer, exiting...\n");
840-
goto error;
841-
}
842-
843-
if (shm_memlog_size)
844-
shm_mem_enable_dbg();
845-
846-
/* init IPC */
847-
if (init_ipc()<0){
848-
LM_CRIT("could not initialize IPC support, exiting...\n");
849-
goto error;
850-
}
851-
852-
/* init serial forking engine */
853-
if (init_serialization()!=0) {
854-
LM_ERR("failed to initialize serialization\n");
855-
goto error;
856-
}
857-
/* Init MI */
858-
if (init_mi_core()<0) {
859-
LM_ERR("failed to initialize MI core\n");
860-
goto error;
861-
}
862-
863-
/* Register core events */
864-
if (evi_register_core() != 0) {
865-
LM_ERR("failed register core events\n");
866-
goto error;
867-
}
868-
869-
/* init black list engine */
870-
if (init_black_lists()!=0) {
871-
LM_CRIT("failed to init blacklists\n");
872-
goto error;
873-
}
874-
/* init resolver's blacklist */
875-
if (resolv_blacklist_init()!=0) {
876-
LM_CRIT("failed to create DNS blacklist\n");
877-
goto error;
878-
}
879-
880-
if (init_dset() != 0) {
881-
LM_ERR("failed to initialize SIP forking logic!\n");
882-
goto error;
883-
}
884-
885-
/* init SQL DB support */
886-
if (init_db_support() != 0) {
887-
LM_ERR("failed to initialize SQL database support\n");
888-
goto error;
889-
}
890-
891-
/* init CacheDB support */
892-
if (init_cdb_support() != 0) {
893-
LM_ERR("failed to initialize CacheDB support\n");
894-
goto error;
895-
}
896-
897-
/* init modules */
898-
if (init_modules() != 0) {
899-
LM_ERR("error while initializing modules\n");
900-
goto error;
901-
}
902-
903-
/* init xlog */
904-
if (init_xlog() < 0) {
905-
LM_ERR("error while initializing xlog!\n");
906-
goto error;
907-
}
908-
909-
/* register route timers */
910-
if(register_route_timers() < 0) {
911-
LM_ERR("Failed to register timer\n");
912-
goto error;
913-
}
914-
915-
/* init pseudo-variable support */
916-
if (init_pvar_support() != 0) {
917-
LM_ERR("failed to init pvar support\n");
918-
goto error;
919-
}
920-
921-
/* init multi processes support */
922-
if (init_multi_proc_support()!=0) {
923-
LM_ERR("failed to init multi-proc support\n");
924-
goto error;
925-
}
926-
927-
/* init avps */
928-
if (init_extra_avps() != 0) {
929-
LM_ERR("error while initializing avps\n");
930-
goto error;
931-
}
932-
933-
/* fix routing lists */
934-
if ( (r=fix_rls())!=0){
935-
LM_ERR("failed to fix configuration with err code %d\n", r);
936-
goto error;
937-
}
938-
939-
if (init_log_level() != 0) {
940-
LM_ERR("failed to init logging levels\n");
941-
goto error;
942-
}
943-
944-
if (init_log_event_cons() < 0) {
945-
LM_ERR("Failed to initialize log event consumer\n");
946-
goto error;
947-
}
948-
949-
if (trans_init_all_listeners()<0) {
950-
LM_ERR("failed to init all SIP listeners, aborting\n");
951-
goto error;
952-
}
953-
954-
if (init_script_reload()<0) {
955-
LM_ERR("failed to init cfg reload ctx, aborting\n");
956-
goto error;
957-
}
958-
959-
/* all processes should have access to all the sockets (for sending)
960-
* so we open all first*/
961-
if (do_suid(user_id, group_id)==-1)
962-
goto error;
963-
964-
if (run_post_fork_handlers()<0) {
965-
LM_ERR("failed to init post-fork handlers\n");
966-
goto error;
876+
for (int n = 0; n < howmany(main_script, main_script[0]); n++) {
877+
int result = main_script[n].hndlr();
878+
pred_cmp_f pred_cmp = cmps_ops[main_script[n].pval];
879+
if (pred_cmp(result, main_script[n].pred)) {
880+
LM_ERR("failed to initialize %s!\n", main_script[n].desc);
881+
goto error;
882+
}
967883
}
968884

969885
ret = main_loop();

main_script.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright (C) 2023 Sippy Software, Inc., http://www.sippysoft.com
3+
*
4+
* This file is part of opensips, a free SIP server.
5+
*
6+
* opensips is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version
10+
*
11+
* opensips is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19+
*
20+
*/
21+
22+
#pragma once
23+
24+
typedef int (*pred_cmp_f)(int, int);
25+
26+
struct main_script {
27+
int (*hndlr)(void);
28+
int pval;
29+
int pred;
30+
const char *desc;
31+
};
32+
33+
static int op_neq(int r, int e) { return (r != e); }
34+
static int op_eq(int r, int e) { return (r == e); }
35+
static int op_lt(int r, int e) { return (r < e); }
36+
static int op_lte(int r, int e) { return (r <= e); }
37+
static int op_gt(int r, int e) { return (r > e); }
38+
static int op_gte(int r, int e) { return (r >= e); }
39+
40+
#define opidx(lop) (((0 lop 0) << 2) | ((0 lop 1) << 1) | (1 lop 0))
41+
42+
static const pred_cmp_f cmps_ops[] = {
43+
[opidx(!=)] = op_neq,
44+
[opidx(==)] = op_eq,
45+
[opidx(<)] = op_lt,
46+
[opidx(>)] = op_gt,
47+
[opidx(<=)] = op_lte,
48+
[opidx(>=)] = op_gte
49+
};
50+
51+
#define FN_HNDLR(h, lop, p, d) (struct main_script){ \
52+
.hndlr = (h), \
53+
.pval = opidx(lop), \
54+
.pred = (p), \
55+
.desc = (d), \
56+
}

0 commit comments

Comments
 (0)