Browse code

core: use defines for types of preprocessor defs

Daniel-Constantin Mierla authored on 20/09/2022 08:52:42
Showing 1 changed files
... ...
@@ -2286,7 +2286,7 @@ int main(int argc, char** argv)
2286 2286
 					} else {
2287 2287
 						tmp_len = strlen(optarg);
2288 2288
 					}
2289
-					pp_define_set_type(0);
2289
+					pp_define_set_type(KSR_PPDEF_DEFINE);
2290 2290
 					if(pp_define(tmp_len, optarg)<0) {
2291 2291
 						fprintf(stderr, "error at define param: -A %s\n",
2292 2292
 								optarg);
Browse code

core: support to specify options for load module

- prototypes:
loadmodule("path", "options")
loadmodulex("path", "options")
- options is a string with characters:
- 'g' or 'G' - open the module shared object file with RTLD_GLOBAL set,
wich can be used for modules related to external scripting languages
to avoid reloading
- example: loadmodule("app_lua", "g")

Daniel-Constantin Mierla authored on 02/09/2022 11:15:35
Showing 1 changed files
... ...
@@ -2446,7 +2446,7 @@ int main(int argc, char** argv)
2446 2446
 						fprintf(stderr, "bad load module parameter\n");
2447 2447
 						goto error;
2448 2448
 					}
2449
-					if (load_module(optarg)!=0) {
2449
+					if (ksr_load_module(optarg, NULL)!=0) {
2450 2450
 						LM_ERR("failed to load the module: %s\n", optarg);
2451 2451
 						goto error;
2452 2452
 					}
Browse code

core: init pv buffers earlier to be avaialble for cli params

- GH #3152

Daniel-Constantin Mierla authored on 28/06/2022 16:41:50
Showing 1 changed files
... ...
@@ -2178,6 +2178,10 @@ int main(int argc, char** argv)
2178 2178
 	init_tcp_options(); /* set the defaults before the config */
2179 2179
 #endif
2180 2180
 
2181
+	if (pv_init_buffer()<0) {
2182
+		goto error;
2183
+	}
2184
+
2181 2185
 	pp_define_core();
2182 2186
 
2183 2187
 	/* process command line (cfg. file path etc) */
Browse code

core: stop at the first config error on startup

- continuing to look for more errors to print them on one check can end
up in crashing because some internal interpreter structure may not be
filled properly
- new cli option --all-errors that can be used to enable printing
details for more detected config errors

Daniel-Constantin Mierla authored on 14/06/2022 09:44:24
Showing 1 changed files
... ...
@@ -206,6 +206,7 @@ Options:\n\
206 206
     --modparam=modname:paramname:type:value set the module parameter\n\
207 207
                   type has to be 's' for string value and 'i' for int value, \n\
208 208
                   example: --modparam=corex:alias_subdomains:s:" NAME ".org\n\
209
+    --all-errors Print details about all config errors that can be detected\n\
209 210
     -M nr        Size of private memory allocated, in Megabytes\n\
210 211
     -n processes Number of child processes to fork per interface\n\
211 212
                   (default: 8)\n"
... ...
@@ -482,6 +483,7 @@ int child_rank = 0;
482 483
 int ser_kill_timeout=DEFAULT_SER_KILL_TIMEOUT;
483 484
 
484 485
 int ksr_verbose_startup = 0;
486
+int ksr_all_errors = 0;
485 487
 
486 488
 /* cfg parsing */
487 489
 int cfg_errors=0;
... ...
@@ -2018,6 +2020,7 @@ int main(int argc, char** argv)
2018 2020
 		{"debug",       required_argument, 0, KARGOPTVAL + 8},
2019 2021
 		{"cfg-print",   no_argument,       0, KARGOPTVAL + 9},
2020 2022
 		{"atexit",      required_argument, 0, KARGOPTVAL + 10},
2023
+		{"all-errors",  no_argument,       0, KARGOPTVAL + 11},
2021 2024
 		{0, 0, 0, 0 }
2022 2025
 	};
2023 2026
 
... ...
@@ -2128,6 +2131,9 @@ int main(int argc, char** argv)
2128 2131
 						goto error;
2129 2132
 					}
2130 2133
 					break;
2134
+			case KARGOPTVAL+11:
2135
+					ksr_all_errors = 1;
2136
+					break;
2131 2137
 
2132 2138
 			default:
2133 2139
 					if (c == 'h' || (optarg && strcmp(optarg, "-h") == 0)) {
... ...
@@ -2317,6 +2323,7 @@ int main(int argc, char** argv)
2317 2323
 			case KARGOPTVAL+8:
2318 2324
 			case KARGOPTVAL+9:
2319 2325
 			case KARGOPTVAL+10:
2326
+			case KARGOPTVAL+11:
2320 2327
 					break;
2321 2328
 
2322 2329
 			/* long options */
Browse code

core: main - check the debug cli param value

Daniel-Constantin Mierla authored on 29/04/2022 07:44:46
Showing 1 changed files
... ...
@@ -2100,6 +2100,10 @@ int main(int argc, char** argv)
2100 2100
 					ksr_slog_init(optarg);
2101 2101
 					break;
2102 2102
 			case KARGOPTVAL+8:
2103
+					if (optarg == NULL) {
2104
+						fprintf(stderr, "bad debug level value\n");
2105
+						goto error;
2106
+					}
2103 2107
 					debug_flag = 1;
2104 2108
 					default_core_cfg.debug=(int)strtol(optarg, &tmp, 10);
2105 2109
 					if ((tmp==0) || (*tmp)){
Browse code

core: count and store global index for listen sockets at startup

Daniel-Constantin Mierla authored on 06/04/2022 11:54:59
Showing 1 changed files
... ...
@@ -2851,6 +2851,7 @@ try_again:
2851 2851
 		fprintf(stderr,  "failed to initialize list addresses\n");
2852 2852
 		goto error;
2853 2853
 	}
2854
+	ksr_sockets_index();
2854 2855
 	if (default_core_cfg.dns_try_ipv6 && !(socket_types & SOCKET_T_IPV6)){
2855 2856
 		/* if we are not listening on any ipv6 address => no point
2856 2857
 		 * to try to resovle ipv6 addresses */
Browse code

core: renamed global is_main to _ksr_is_main

- safer to avoid conflicts with other libs

Daniel-Constantin Mierla authored on 10/01/2022 17:31:38
Showing 1 changed files
... ...
@@ -508,7 +508,7 @@ extern FILE* yyin;
508 508
 extern int yyparse(void);
509 509
 
510 510
 
511
-int is_main=1; /* flag = is this the  "main" process? */
511
+int _ksr_is_main=1; /* flag = is this the  "main" process? */
512 512
 int fixup_complete=0; /* flag = is the fixup complete ? */
513 513
 
514 514
 char* pid_file = 0; /* filename as asked by use */
... ...
@@ -649,7 +649,7 @@ static void kill_all_children(int signum)
649 649
 		  * (only main can add processes, so from main is safe not to lock
650 650
 		  *  and moreover it avoids the lock-holding suicidal children problem)
651 651
 		  */
652
-		if (!is_main) lock_get(process_lock);
652
+		if (!_ksr_is_main) lock_get(process_lock);
653 653
 		for (r=1; r<*process_count; r++){
654 654
 			if (r==process_no) continue; /* try not to be suicidal */
655 655
 			if (pt[r].pid) {
... ...
@@ -658,7 +658,7 @@ static void kill_all_children(int signum)
658 658
 			else LM_CRIT("killing: %s > %d no pid!!!\n",
659 659
 							pt[r].desc, pt[r].pid);
660 660
 		}
661
-		if (!is_main) lock_release(process_lock);
661
+		if (!_ksr_is_main) lock_release(process_lock);
662 662
 	}
663 663
 }
664 664
 
... ...
@@ -832,7 +832,7 @@ void sig_usr(int signo)
832 832
 #endif
833 833
 #endif
834 834
 
835
-	if (is_main){
835
+	if (_ksr_is_main){
836 836
 		if (sig_flag==0) sig_flag=signo;
837 837
 		else /*  previous sig. not processed yet, ignoring? */
838 838
 			return; ;
... ...
@@ -3070,7 +3070,7 @@ try_again:
3070 3070
 	if (ret < 0)
3071 3071
 		goto error;
3072 3072
 	/*kill everything*/
3073
-	if (is_main) shutdown_children(SIGTERM, 0);
3073
+	if (_ksr_is_main) shutdown_children(SIGTERM, 0);
3074 3074
 	if (!dont_daemonize) {
3075 3075
 		if (daemon_status_send(0) < 0)
3076 3076
 			fprintf(stderr, "error sending exit status: %s [%d]\n",
... ...
@@ -3081,7 +3081,7 @@ try_again:
3081 3081
 
3082 3082
 error:
3083 3083
 	/*kill everything*/
3084
-	if (is_main) shutdown_children(SIGTERM, 0);
3084
+	if (_ksr_is_main) shutdown_children(SIGTERM, 0);
3085 3085
 	if (!dont_daemonize) {
3086 3086
 		if (daemon_status_send((char)-1) < 0)
3087 3087
 			fprintf(stderr, "error sending exit status: %s [%d]\n",
Browse code

core: clarification for -b cli parameter

Daniel-Constantin Mierla authored on 23/11/2021 16:22:42
Showing 1 changed files
... ...
@@ -169,8 +169,8 @@ Options:\n\
169 169
                   default is yes.\n\
170 170
     -A define    Add config pre-processor define (e.g., -A WITH_AUTH,\n\
171 171
                   -A 'FLT_ACC=1', -A 'DEFVAL=\"str-val\"')\n\
172
-    -b nr        Maximum receive buffer size which will not be exceeded by\n\
173
-                  auto-probing procedure even if  OS allows\n\
172
+    -b nr        Maximum OS UDP receive buffer size which will not be exceeded by\n\
173
+                  auto-probing-and-increase procedure even if OS allows\n\
174 174
     -c           Check configuration file for syntax errors\n\
175 175
     --cfg-print  Print configuration file evaluating includes and ifdefs\n\
176 176
     -d           Debugging level control (multiple -d to increase the level from 0)\n\
Browse code

core: mechanism to execute child_init() for special ranks on demand

- new special rank PROC_POSTCHILDINIT, to execute child_init() for main
process only when ksr_module_set_flag(KSRMOD_FLAG_POSTCHILDINIT) is
executed in mod_init()

Daniel-Constantin Mierla authored on 18/11/2021 13:33:28
Showing 1 changed files
... ...
@@ -1498,6 +1498,12 @@ int main_loop(void)
1498 1498
 			LM_ERR("init_child failed\n");
1499 1499
 			goto error;
1500 1500
 		}
1501
+
1502
+		if (init_child(PROC_POSTCHILDINIT) < 0) {
1503
+			LM_ERR("error in init_child for rank PROC_POSTCHILDINIT\n");
1504
+			goto error;
1505
+		}
1506
+
1501 1507
 		*_sr_instance_started = 1;
1502 1508
 		return udp_rcv_loop();
1503 1509
 	}else{ /* fork: */
... ...
@@ -1864,6 +1870,11 @@ int main_loop(void)
1864 1870
 			unix_tcp_sock=-1;
1865 1871
 		}
1866 1872
 #endif
1873
+		if (init_child(PROC_POSTCHILDINIT) < 0) {
1874
+			LM_ERR("error in init_child for rank PROC_POSTCHILDINIT\n");
1875
+			goto error;
1876
+		}
1877
+
1867 1878
 		/* init cfg, but without per child callbacks support */
1868 1879
 		cfg_child_no_cb_init();
1869 1880
 		cfg_ok=1;
Browse code

core: logging: JSON: add CEE schema support (#2826)

* core: logging: JSON: add CEE schema support

* core: logging: JSON: revise CEE create nested objects

* core: logging: JSON: revise CEE check for pthreads

Daniel Pocock authored on 28/08/2021 18:16:25 • GitHub committed on 28/08/2021 18:16:25
Showing 1 changed files
... ...
@@ -342,9 +342,11 @@ int dont_fork = 0;
342 342
 int dont_daemonize = 0;
343 343
 int log_stderr = 0;
344 344
 int log_color = 0;
345
+int log_cee = 0;
345 346
 /* set custom app name for syslog printing */
346 347
 char *log_name = 0;
347 348
 char *log_prefix_fmt = 0;
349
+char *log_fqdn = 0;
348 350
 pid_t creator_pid = (pid_t) -1;
349 351
 int config_check = 0;
350 352
 /* check if reply first via host==us */
... ...
@@ -2039,7 +2041,7 @@ int main(int argc, char** argv)
2039 2041
 	sr_cfgenv_init();
2040 2042
 	daemon_status_init();
2041 2043
 
2042
-	dprint_init_colors();
2044
+	log_init();
2043 2045
 
2044 2046
 	/* command line options */
2045 2047
 	options=  ":f:cm:M:dVIhEeb:l:L:n:vKrRDTN:W:w:t:u:g:P:G:SQ:O:a:A:x:X:Y:";
Browse code

core: main - remove commented debug function

Daniel-Constantin Mierla authored on 29/07/2021 12:17:37
Showing 1 changed files
... ...
@@ -293,22 +293,6 @@ void print_internals(void)
293 293
 	printf("Thank you for flying %s!\n", NAME);
294 294
 }
295 295
 
296
-/* debugging function */
297
-/*
298
-void receive_stdin_loop(void)
299
-{
300
-	#define BSIZE 1024
301
-	char buf[BSIZE+1];
302
-	int len;
303
-
304
-	while(1){
305
-		len=fread(buf,1,BSIZE,stdin);
306
-		buf[len+1]=0;
307
-		receive_msg(buf, len);
308
-		printf("-------------------------\n");
309
-	}
310
-}
311
-*/
312 296
 
313 297
 /* global vars */
314 298
 
... ...
@@ -323,7 +307,7 @@ unsigned int maxbuffer = MAX_RECV_BUFFER_SIZE; /* maximum buffer size we do
323 307
 												  not want to exceed during the
324 308
 												  auto-probing procedure; may
325 309
 												  be re-configured */
326
-unsigned int sql_buffer_size = 65535; /* Size for the SQL buffer. Defaults to 64k. 
310
+unsigned int sql_buffer_size = 65535; /* Size for the SQL buffer. Defaults to 64k.
327 311
                                          This may be re-configured */
328 312
 int socket_workers = 0;		/* number of workers processing requests for a socket
329 313
 							   - it's reset everytime with a new listen socket */
Browse code

core: fix building quoted value for #!subsdefs

Daniel-Constantin Mierla authored on 01/07/2021 09:17:56
Showing 1 changed files
... ...
@@ -2349,7 +2349,7 @@ int main(int argc, char** argv)
2349 2349
 						fprintf(stderr, "bad substdef parameter\n");
2350 2350
 						goto error;
2351 2351
 					}
2352
-					if(pp_substdef_add(optarg, 0)<0) {
2352
+					if(pp_substdef_add(optarg, KSR_PPDEF_NORMAL)<0) {
2353 2353
 						LM_ERR("failed to add substdef expression: %s\n", optarg);
2354 2354
 						goto error;
2355 2355
 					}
... ...
@@ -2359,7 +2359,7 @@ int main(int argc, char** argv)
2359 2359
 						fprintf(stderr, "bad substdefs parameter\n");
2360 2360
 						goto error;
2361 2361
 					}
2362
-					if(pp_substdef_add(optarg, 1)<0) {
2362
+					if(pp_substdef_add(optarg, KSR_PPDEF_QUOTED)<0) {
2363 2363
 						LM_ERR("failed to add substdefs expression: %s\n", optarg);
2364 2364
 						goto error;
2365 2365
 					}
Browse code

core: main - catch common mistakes for the arguments

- print error and exit on 'kamailio start|stop|restart'

Daniel-Constantin Mierla authored on 15/06/2021 06:49:24
Showing 1 changed files
... ...
@@ -2024,6 +2024,25 @@ int main(int argc, char** argv)
2024 2024
 		{0, 0, 0, 0 }
2025 2025
 	};
2026 2026
 
2027
+	if (argc > 1) {
2028
+		/* checks for common wrong arguments */
2029
+		if(strcasecmp(argv[1], "start")==0) {
2030
+			fprintf(stderr, "error: 'start' is not a supported argument\n");
2031
+			fprintf(stderr, "error: stopping " NAME " ...\n\n");
2032
+			exit(-1);
2033
+		}
2034
+		if(strcasecmp(argv[1], "stop")==0) {
2035
+			fprintf(stderr, "error: 'stop' is not a supported argument\n");
2036
+			fprintf(stderr, "error: stopping " NAME " ...\n\n");
2037
+			exit(-1);
2038
+		}
2039
+		if(strcasecmp(argv[1], "restart")==0) {
2040
+			fprintf(stderr, "error: 'restart' is not a supported argument\n");
2041
+			fprintf(stderr, "error: stopping " NAME " ...\n\n");
2042
+			exit(-1);
2043
+		}
2044
+	}
2045
+
2027 2046
 	/*init*/
2028 2047
 	time(&up_since);
2029 2048
 	creator_pid = getpid();
Browse code

core: clone define value to avoid losing the reference

- impacted also by substdef
- GH #2736

Daniel-Constantin Mierla authored on 25/05/2021 08:24:23
Showing 1 changed files
... ...
@@ -2264,7 +2264,7 @@ int main(int argc, char** argv)
2264 2264
 					}
2265 2265
 					if(p) {
2266 2266
 						p++;
2267
-						if(pp_define_set(strlen(p), p)<0) {
2267
+						if(pp_define_set(strlen(p), p, KSR_PPDEF_NORMAL)<0) {
2268 2268
 							fprintf(stderr, "error at define value: -A %s\n",
2269 2269
 								optarg);
2270 2270
 							goto error;
Browse code

core: main - set ending 0 in allocated cfg file path

Daniel-Constantin Mierla authored on 15/04/2021 08:28:24
Showing 1 changed files
... ...
@@ -1288,7 +1288,7 @@ int fix_cfg_file(void)
1288 1288
 		}
1289 1289
 		if ((res = malloc(cfg_len + 1)) == NULL) goto error;
1290 1290
 		memcpy(res, cfg_file, cfg_len);
1291
-		cfg_file[cfg_len] = 0;
1291
+		res[cfg_len] = 0;
1292 1292
 		cfg_file = res;
1293 1293
 		return 0;
1294 1294
 	}
Browse code

core: main - checking of optarg for covering all unixes

Daniel-Constantin Mierla authored on 09/04/2021 07:34:29
Showing 1 changed files
... ...
@@ -2095,6 +2095,10 @@ int main(int argc, char** argv)
2095 2095
 					ksr_cfg_print_mode = 1;
2096 2096
 					break;
2097 2097
 			case KARGOPTVAL+10:
2098
+					if (optarg == NULL) {
2099
+						fprintf(stderr, "bad atexit value\n");
2100
+						goto error;
2101
+					}
2098 2102
 					if(optarg[0]=='y' || optarg[0]=='1') {
2099 2103
 						ksr_atexit_mode = 1;
2100 2104
 					} else if(optarg[0]=='n' || optarg[0]=='0') {
... ...
@@ -2234,6 +2238,10 @@ int main(int argc, char** argv)
2234 2238
 					};
2235 2239
 					break;
2236 2240
 			case 'u':
2241
+					if (optarg == NULL) {
2242
+						fprintf(stderr, "bad -u parameter\n");
2243
+						goto error;
2244
+					}
2237 2245
 					/* user needed for possible shm. pre-init */
2238 2246
 					user=optarg;
2239 2247
 					break;
... ...
@@ -2293,6 +2301,10 @@ int main(int argc, char** argv)
2293 2301
 
2294 2302
 			/* long options */
2295 2303
 			case KARGOPTVAL:
2304
+					if (optarg == NULL) {
2305
+						fprintf(stderr, "bad alias parameter\n");
2306
+						goto error;
2307
+					}
2296 2308
 					if(parse_phostport(optarg, &tmp, &tmp_len,
2297 2309
 											&port, &proto)!=0) {
2298 2310
 						fprintf(stderr, "Invalid alias value '%s'\n", optarg);
... ...
@@ -2304,24 +2316,40 @@ int main(int argc, char** argv)
2304 2316
 					}
2305 2317
 					break;
2306 2318
 			case KARGOPTVAL+1:
2319
+					if (optarg == NULL) {
2320
+						fprintf(stderr, "bad subst parameter\n");
2321
+						goto error;
2322
+					}
2307 2323
 					if(pp_subst_add(optarg)<0) {
2308 2324
 						LM_ERR("failed to add subst expression: %s\n", optarg);
2309 2325
 						goto error;
2310 2326
 					}
2311 2327
 					break;
2312 2328
 			case KARGOPTVAL+2:
2329
+					if (optarg == NULL) {
2330
+						fprintf(stderr, "bad substdef parameter\n");
2331
+						goto error;
2332
+					}
2313 2333
 					if(pp_substdef_add(optarg, 0)<0) {
2314 2334
 						LM_ERR("failed to add substdef expression: %s\n", optarg);
2315 2335
 						goto error;
2316 2336
 					}
2317 2337
 					break;
2318 2338
 			case KARGOPTVAL+3:
2339
+					if (optarg == NULL) {
2340
+						fprintf(stderr, "bad substdefs parameter\n");
2341
+						goto error;
2342
+					}
2319 2343
 					if(pp_substdef_add(optarg, 1)<0) {
2320 2344
 						LM_ERR("failed to add substdefs expression: %s\n", optarg);
2321 2345
 						goto error;
2322 2346
 					}
2323 2347
 					break;
2324 2348
 			case KARGOPTVAL+4:
2349
+					if (optarg == NULL) {
2350
+						fprintf(stderr, "bad server if parameter\n");
2351
+						goto error;
2352
+					}
2325 2353
 					server_id=(int)strtol(optarg, &tmp, 10);
2326 2354
 					if ((tmp==0) || (*tmp)){
2327 2355
 						LM_ERR("bad server_id value: %s\n", optarg);
... ...
@@ -2383,12 +2411,20 @@ int main(int argc, char** argv)
2383 2411
 	while((c=getopt_long(argc, argv, options, long_options, &option_index))!=-1) {
2384 2412
 		switch(c) {
2385 2413
 			case KARGOPTVAL+5:
2414
+					if (optarg == NULL) {
2415
+						fprintf(stderr, "bad load module parameter\n");
2416
+						goto error;
2417
+					}
2386 2418
 					if (load_module(optarg)!=0) {
2387 2419
 						LM_ERR("failed to load the module: %s\n", optarg);
2388 2420
 						goto error;
2389 2421
 					}
2390 2422
 					break;
2391 2423
 			case KARGOPTVAL+6:
2424
+					if (optarg == NULL) {
2425
+						fprintf(stderr, "bad modparam parameter\n");
2426
+						goto error;
2427
+					}
2392 2428
 					if(set_mod_param_serialized(optarg) < 0) {
2393 2429
 						LM_ERR("failed to set modparam: %s\n", optarg);
2394 2430
 						goto error;
Browse code

core: main - coherent behaviour for fix_cfg_file()

- duplicate to build the output always, safer not to try to change the
static string defined via macro at runtime

Daniel-Constantin Mierla authored on 08/04/2021 07:38:48
Showing 1 changed files
... ...
@@ -1260,14 +1260,15 @@ static struct name_lst* parse_phostport_mh(char* s, char** host, int* hlen,
1260 1260
 
1261 1261
 
1262 1262
 
1263
-/** Update \c cfg_file variable to contain full pathname. The function updates
1263
+/** Update \c cfg_file variable to contain full pathname or '-' (for stdin)
1264
+ * allocated in system memory. The function updates
1264 1265
  * the value of \c cfg_file global variable to contain full absolute pathname
1265
- * to the main configuration file of SER. The function uses CFG_FILE macro to
1266
+ * to the main configuration file. The function uses CFG_FILE macro to
1266 1267
  * determine the default path to the configuration file if the user did not
1267 1268
  * specify one using the command line option. If \c cfg_file contains an
1268
- * absolute pathname then it is used unmodified, if it contains a relative
1269
+ * absolute pathname then it is cloned unmodified, if it contains a relative
1269 1270
  * pathanme than the value returned by \c getcwd function will be added at the
1270
- * beginning. This function must be run before SER changes its current working
1271
+ * beginning. This function must be run before changing its current working
1271 1272
  * directory to / (in daemon mode).
1272 1273
  * @return Zero on success, negative number
1273 1274
  * on error.
... ...
@@ -1276,34 +1277,55 @@ int fix_cfg_file(void)
1276 1277
 {
1277 1278
 	char* res = NULL;
1278 1279
 	size_t max_len, cwd_len, cfg_len;
1279
-	
1280
+
1280 1281
 	if (cfg_file == NULL) cfg_file = CFG_FILE;
1281
-	if (cfg_file[0] == '/') return 0;
1282
-	if (cfg_file[0] == '-' && strlen(cfg_file)==1) return 0;
1283
-	
1282
+	if (cfg_file[0] == '/') {
1283
+		cfg_len = strlen(cfg_file);
1284
+		if(cfg_len < 2) {
1285
+			/* do not accept only '/' */
1286
+			fprintf(stderr, "ERROR: invalid cfg file value\n");
1287
+			return -1;
1288
+		}
1289
+		if ((res = malloc(cfg_len + 1)) == NULL) goto error;
1290
+		memcpy(res, cfg_file, cfg_len);
1291
+		cfg_file[cfg_len] = 0;
1292
+		cfg_file = res;
1293
+		return 0;
1294
+	}
1295
+	if (cfg_file[0] == '-') {
1296
+		cfg_len = strlen(cfg_file);
1297
+		if(cfg_len == 1) {
1298
+			if ((res = malloc(2)) == NULL) goto error;
1299
+			res[0] = '-';
1300
+			res[1] = '\0';
1301
+			cfg_file = res;
1302
+			return 0;
1303
+		}
1304
+	}
1305
+
1284 1306
 	/* cfg_file contains a relative pathname, get the current
1285 1307
 	 * working directory and add it at the beginning
1286 1308
 	 */
1287 1309
 	cfg_len = strlen(cfg_file);
1288
-	
1310
+
1289 1311
 	max_len = pathmax();
1290 1312
 	if ((res = malloc(max_len)) == NULL) goto error;
1291
-	
1313
+
1292 1314
 	if (getcwd(res, max_len) == NULL) goto error;
1293 1315
 	cwd_len = strlen(res);
1294
-	
1316
+
1295 1317
 	/* Make sure that the buffer is big enough */
1296 1318
 	if (cwd_len + 1 + cfg_len >= max_len) goto error;
1297
-	
1319
+
1298 1320
 	res[cwd_len] = '/';
1299 1321
 	memcpy(res + cwd_len + 1, cfg_file, cfg_len);
1300
-	
1322
+
1301 1323
 	res[cwd_len + 1 + cfg_len] = '\0'; /* Add terminating zero */
1302 1324
 	cfg_file = res;
1303 1325
 	return 0;
1304
-	
1326
+
1305 1327
  error:
1306
-	fprintf(stderr, "ERROR: Unable to fix cfg_file to contain full pathname\n");
1328
+	fprintf(stderr, "ERROR: Unable to fix cfg file to contain full pathname\n");
1307 1329
 	if (res) free(res);
1308 1330
 	return -1;
1309 1331
 }
Browse code

core: main - several optarg safety checks

Daniel-Constantin Mierla authored on 06/04/2021 12:48:34
Showing 1 changed files
... ...
@@ -2140,6 +2140,10 @@ int main(int argc, char** argv)
2140 2140
 					/* ignore, they were parsed immediately after startup */
2141 2141
 					break;
2142 2142
 			case 'f':
2143
+					if (optarg == NULL) {
2144
+						fprintf(stderr, "bad -f parameter\n");
2145
+						goto error;
2146
+					}
2143 2147
 					cfg_file=optarg;
2144 2148
 					break;
2145 2149
 			case 'c':
... ...
@@ -2147,10 +2151,18 @@ int main(int argc, char** argv)
2147 2151
 					log_stderr=1; /* force stderr logging */
2148 2152
 					break;
2149 2153
 			case 'L':
2154
+					if (optarg == NULL) {
2155
+						fprintf(stderr, "bad -L parameter\n");
2156
+						goto error;
2157
+					}
2150 2158
 					mods_dir = optarg;
2151 2159
 					mods_dir_cmd = 1;
2152 2160
 					break;
2153 2161
 			case 'm':
2162
+					if (optarg == NULL) {
2163
+						fprintf(stderr, "bad shared mem size\n");
2164
+						goto error;
2165
+					}
2154 2166
 					shm_mem_size=strtol(optarg, &tmp, 10) * 1024 * 1024;
2155 2167
 					if (tmp &&(*tmp)){
2156 2168
 						fprintf(stderr, "bad shmem size number: -m %s\n",
... ...
@@ -2188,6 +2200,10 @@ int main(int argc, char** argv)
2188 2200
 					/* ignore it, was parsed immediately after startup */
2189 2201
 					break;
2190 2202
 			case 'O':
2203
+					if (optarg == NULL) {
2204
+						fprintf(stderr, "bad -O parameter\n");
2205
+						goto error;
2206
+					}
2191 2207
 					scr_opt_lev=strtol(optarg, &tmp, 10);
2192 2208
 					if (tmp &&(*tmp)){
2193 2209
 						fprintf(stderr, "bad optimization level: -O %s\n",
... ...
@@ -2200,6 +2216,10 @@ int main(int argc, char** argv)
2200 2216
 					user=optarg;
2201 2217
 					break;
2202 2218
 			case 'A':
2219
+					if (optarg == NULL) {
2220
+						fprintf(stderr, "bad -A parameter\n");
2221
+						goto error;
2222
+					}
2203 2223
 					p = strchr(optarg, '=');
2204 2224
 					if(p) {
2205 2225
 						tmp_len = p - optarg;
... ...
@@ -2451,6 +2471,10 @@ try_again:
2451 2471
 									   takes priority over config */
2452 2472
 					break;
2453 2473
 			case 'b':
2474
+					if (optarg == NULL) {
2475
+						fprintf(stderr, "bad -b parameter\n");
2476
+						goto error;
2477
+					}
2454 2478
 					maxbuffer=strtol(optarg, &tmp, 10);
2455 2479
 					if (tmp &&(*tmp)){
2456 2480
 						fprintf(stderr, "bad max buffer size number: -b %s\n",
... ...
@@ -2473,6 +2497,10 @@ try_again:
2473 2497
 				#endif
2474 2498
 					break;
2475 2499
 			case 'l':
2500
+					if (optarg == NULL) {
2501
+						fprintf(stderr, "bad -l parameter\n");
2502
+						goto error;
2503
+					}
2476 2504
 					p = strrchr(optarg, '/');
2477 2505
 					if(p==NULL) {
2478 2506
 						p = optarg;
... ...
@@ -2522,6 +2550,10 @@ try_again:
2522 2550
 					free_name_lst(n_lst);
2523 2551
 					break;
2524 2552
 			case 'n':
2553
+					if (optarg == NULL) {
2554
+						fprintf(stderr, "bad -n parameter\n");
2555
+						goto error;
2556
+					}
2525 2557
 					children_no=strtol(optarg, &tmp, 10);
2526 2558
 					if ((tmp==0) ||(*tmp)){
2527 2559
 						fprintf(stderr, "bad process number: -n %s\n",
... ...
@@ -2548,6 +2580,10 @@ try_again:
2548 2580
  									"TCP support disabled\n", optarg);
2549 2581
 						goto error;
2550 2582
 					}
2583
+					if (optarg == NULL) {
2584
+						fprintf(stderr, "bad -N parameter\n");
2585
+						goto error;
2586
+					}
2551 2587
 					tcp_cfg_children_no=strtol(optarg, &tmp, 10);
2552 2588
 					if ((tmp==0) ||(*tmp)){
2553 2589
 						fprintf(stderr, "bad process number: -N %s\n",
... ...
@@ -2560,6 +2596,10 @@ try_again:
2560 2596
 					break;
2561 2597
 			case 'W':
2562 2598
 				#ifdef USE_TCP
2599
+					if (optarg == NULL) {
2600
+						fprintf(stderr, "bad -W parameter\n");
2601
+						goto error;
2602
+					}
2563 2603
 					tcp_poll_method=get_poll_type(optarg);
2564 2604
 					if (tcp_poll_method==POLL_NONE){
2565 2605
 						fprintf(stderr, "bad poll method name: -W %s\ntry "
... ...
@@ -2577,6 +2617,10 @@ try_again:
2577 2617
 									"SCTP support disabled\n", optarg);
2578 2618
 						goto error;
2579 2619
 					}
2620
+					if (optarg == NULL) {
2621
+						fprintf(stderr, "bad -Q parameter\n");
2622
+						goto error;
2623
+					}
2580 2624
 					sctp_children_no=strtol(optarg, &tmp, 10);
2581 2625
 					if ((tmp==0) ||(*tmp)){
2582 2626
 						fprintf(stderr, "bad process number: -O %s\n",
Browse code

core: checks for trying to define empty or null ids

- use found pointer to compute the lenght of ID for -A parameter to save
a strlen()

Daniel-Constantin Mierla authored on 05/04/2021 15:50:31
Showing 1 changed files
... ...
@@ -2202,16 +2202,17 @@ int main(int argc, char** argv)
2202 2202
 			case 'A':
2203 2203
 					p = strchr(optarg, '=');
2204 2204
 					if(p) {
2205
-						*p = '\0';
2205
+						tmp_len = p - optarg;
2206
+					} else {
2207
+						tmp_len = strlen(optarg);
2206 2208
 					}
2207 2209
 					pp_define_set_type(0);
2208
-					if(pp_define(strlen(optarg), optarg)<0) {
2210
+					if(pp_define(tmp_len, optarg)<0) {
2209 2211
 						fprintf(stderr, "error at define param: -A %s\n",
2210 2212
 								optarg);
2211 2213
 						goto error;
2212 2214
 					}
2213 2215
 					if(p) {
2214
-						*p = '=';
2215 2216
 						p++;
2216 2217
 						if(pp_define_set(strlen(p), p)<0) {
2217 2218
 							fprintf(stderr, "error at define value: -A %s\n",
Browse code

core: renamed recent params wait_child1_* to wait_worker1_*

- consistent with the event route name

Daniel-Constantin Mierla authored on 16/03/2021 08:33:04
Showing 1 changed files
... ...
@@ -540,10 +540,10 @@ static int *_sr_instance_started = NULL;
540 540
 int ksr_cfg_print_mode = 0;
541 541
 int ksr_atexit_mode = 1;
542 542
 
543
-int ksr_wait_child1_mode = 0;
544
-int ksr_wait_child1_time = 4000000;
545
-int ksr_wait_child1_usleep = 100000;
546
-int *ksr_wait_child1_done = NULL;
543
+int ksr_wait_worker1_mode = 0;
544
+int ksr_wait_worker1_time = 4000000;
545
+int ksr_wait_worker1_usleep = 100000;
546
+int *ksr_wait_worker1_done = NULL;
547 547
 
548 548
 /**
549 549
  * return 1 if all child processes were forked
... ...
@@ -1655,13 +1655,13 @@ int main_loop(void)
1655 1655
 
1656 1656
 
1657 1657
 		woneinit = 0;
1658
-		if(ksr_wait_child1_mode!=0) {
1659
-			ksr_wait_child1_done=(int*)shm_malloc(sizeof(int));
1660
-			if(ksr_wait_child1_done==0) {
1658
+		if(ksr_wait_worker1_mode!=0) {
1659
+			ksr_wait_worker1_done=(int*)shm_malloc(sizeof(int));
1660
+			if(ksr_wait_worker1_done==0) {
1661 1661
 				SHM_MEM_ERROR;
1662 1662
 				goto error;
1663 1663
 			}
1664
-			*ksr_wait_child1_done = 0;
1664
+			*ksr_wait_worker1_done = 0;
1665 1665
 		}
1666 1666
 		/* udp processes */
1667 1667
 		for(si=udp_listen; si; si=si->next){
... ...
@@ -1701,23 +1701,26 @@ int main_loop(void)
1701 1701
 						if(run_child_one_init_route()<0)
1702 1702
 							goto error;
1703 1703
 					}
1704
-					if(ksr_wait_child1_mode!=0) {
1705
-						*ksr_wait_child1_done = 1;
1704
+					if(ksr_wait_worker1_mode!=0) {
1705
+						*ksr_wait_worker1_done = 1;
1706
+						LM_DBG("child one finished initialization\n");
1706 1707
 					}
1707 1708
 					return udp_rcv_loop();
1708 1709
 				}
1709 1710
 				/* main process */
1710
-				if(woneinit==0 && ksr_wait_child1_mode!=0) {
1711
+				if(woneinit==0 && ksr_wait_worker1_mode!=0) {
1711 1712
 					int wcount=0;
1712
-					while(*ksr_wait_child1_done==0) {
1713
-						sleep_us(ksr_wait_child1_usleep);
1713
+					while(*ksr_wait_worker1_done==0) {
1714
+						sleep_us(ksr_wait_worker1_usleep);
1714 1715
 						wcount++;
1715
-						if(ksr_wait_child1_time<=wcount*ksr_wait_child1_usleep) {
1716
+						if(ksr_wait_worker1_time<=wcount*ksr_wait_worker1_usleep) {
1716 1717
 							LM_ERR("waiting for child one too long - wait time: %d\n",
1717
-									ksr_wait_child1_time);
1718
+									ksr_wait_worker1_time);
1718 1719
 							goto error;
1719 1720
 						}
1720 1721
 					}
1722
+					LM_DBG("child one initialized after %d wait steps\n",
1723
+							wcount);
1721 1724
 				}
1722 1725
 				woneinit = 1;
1723 1726
 			}
... ...
@@ -1752,23 +1755,26 @@ int main_loop(void)
1752 1755
 							if(run_child_one_init_route()<0)
1753 1756
 								goto error;
1754 1757
 						}
1755
-						if(ksr_wait_child1_mode!=0) {
1756
-							*ksr_wait_child1_done = 1;
1758
+						if(ksr_wait_worker1_mode!=0) {
1759
+							*ksr_wait_worker1_done = 1;
1760
+							LM_DBG("child one finished initialization\n");
1757 1761
 						}
1758 1762
 						return sctp_core_rcv_loop();
1759 1763
 					}
1760 1764
 					/* main process */
1761
-					if(woneinit==0 && ksr_wait_child1_mode!=0) {
1765
+					if(woneinit==0 && ksr_wait_worker1_mode!=0) {
1762 1766
 						int wcount=0;
1763
-						while(*ksr_wait_child1_done==0) {
1764
-							sleep_us(ksr_wait_child1_usleep);
1767
+						while(*ksr_wait_worker1_done==0) {
1768
+							sleep_us(ksr_wait_worker1_usleep);
1765 1769
 							wcount++;
1766
-							if(ksr_wait_child1_time<=wcount*ksr_wait_child1_usleep) {
1770
+							if(ksr_wait_worker1_time<=wcount*ksr_wait_worker1_usleep) {
1767 1771
 								LM_ERR("waiting for child one too long - wait time: %d\n",
1768
-										ksr_wait_child1_time);
1772
+										ksr_wait_worker1_time);
1769 1773
 								goto error;
1770 1774
 							}
1771 1775
 						}
1776
+						LM_DBG("child one initialized after %d wait steps\n",
1777
+								wcount);
1772 1778
 					}
1773 1779
 					woneinit = 1;
1774 1780
 				}
Browse code

core: global parameters to enable waiting for child one worker initialization

- new paramters:
- wait_child1_mode=[0|1] - set to wait or not
- wait_child1_time=1000000 (micro-seconds) - how long to wait over all
- wait_child1_usleep=100000 (micro-seconds) - step to wait before
checking if initialization completed

Daniel-Constantin Mierla authored on 15/03/2021 11:29:08
Showing 1 changed files
... ...
@@ -156,7 +156,6 @@
156 156
 #endif
157 157
 
158 158
 
159
-
160 159
 static char help_msg[]= "\
161 160
 Usage: " NAME " [options]\n\
162 161
 Options:\n\
... ...
@@ -541,6 +540,11 @@ static int *_sr_instance_started = NULL;
541 540
 int ksr_cfg_print_mode = 0;
542 541
 int ksr_atexit_mode = 1;
543 542
 
543
+int ksr_wait_child1_mode = 0;
544
+int ksr_wait_child1_time = 4000000;
545
+int ksr_wait_child1_usleep = 100000;
546
+int *ksr_wait_child1_done = NULL;
547
+
544 548
 /**
545 549
  * return 1 if all child processes were forked
546 550
  * - note: they might still be in init phase (i.e., child init)
... ...
@@ -1651,6 +1655,14 @@ int main_loop(void)
1651 1655
 
1652 1656
 
1653 1657
 		woneinit = 0;
1658
+		if(ksr_wait_child1_mode!=0) {
1659
+			ksr_wait_child1_done=(int*)shm_malloc(sizeof(int));
1660
+			if(ksr_wait_child1_done==0) {
1661
+				SHM_MEM_ERROR;
1662
+				goto error;
1663
+			}
1664
+			*ksr_wait_child1_done = 0;
1665
+		}
1654 1666
 		/* udp processes */
1655 1667
 		for(si=udp_listen; si; si=si->next){
1656 1668
 			nrprocs = (si->workers>0)?si->workers:children_no;
... ...
@@ -1689,8 +1701,24 @@ int main_loop(void)
1689 1701
 						if(run_child_one_init_route()<0)
1690 1702
 							goto error;
1691 1703
 					}
1704
+					if(ksr_wait_child1_mode!=0) {
1705
+						*ksr_wait_child1_done = 1;
1706
+					}
1692 1707
 					return udp_rcv_loop();
1693 1708
 				}
1709
+				/* main process */
1710
+				if(woneinit==0 && ksr_wait_child1_mode!=0) {
1711
+					int wcount=0;
1712
+					while(*ksr_wait_child1_done==0) {
1713
+						sleep_us(ksr_wait_child1_usleep);
1714
+						wcount++;
1715
+						if(ksr_wait_child1_time<=wcount*ksr_wait_child1_usleep) {
1716
+							LM_ERR("waiting for child one too long - wait time: %d\n",
1717
+									ksr_wait_child1_time);
1718
+							goto error;
1719
+						}
1720
+					}
1721
+				}
1694 1722
 				woneinit = 1;
1695 1723
 			}
1696 1724
 			/*parent*/
... ...
@@ -1724,9 +1752,24 @@ int main_loop(void)
1724 1752
 							if(run_child_one_init_route()<0)
1725 1753
 								goto error;
1726 1754
 						}
1727
-
1755
+						if(ksr_wait_child1_mode!=0) {
1756
+							*ksr_wait_child1_done = 1;
1757
+						}
1728 1758
 						return sctp_core_rcv_loop();
1729 1759
 					}
1760
+					/* main process */
1761
+					if(woneinit==0 && ksr_wait_child1_mode!=0) {
1762
+						int wcount=0;
1763
+						while(*ksr_wait_child1_done==0) {
1764
+							sleep_us(ksr_wait_child1_usleep);
1765
+							wcount++;
1766
+							if(ksr_wait_child1_time<=wcount*ksr_wait_child1_usleep) {
1767
+								LM_ERR("waiting for child one too long - wait time: %d\n",
1768
+										ksr_wait_child1_time);
1769
+								goto error;
1770
+							}
1771
+						}
1772
+					}
1730 1773
 					woneinit = 1;
1731 1774
 				}
1732 1775
 			/*parent*/
Browse code

core: execute event_route[core:worker-one-init] for sctp or tcp

- if no udp worker is started, the event route is executed by next
available transport, in the order: sctp, tcp (or tls)

Daniel-Constantin Mierla authored on 12/03/2021 17:15:34
Showing 1 changed files
... ...
@@ -1720,8 +1720,14 @@ int main_loop(void)
1720 1720
 						/* child */
1721 1721
 						bind_address=si; /* shortcut */
1722 1722
 
1723
+						if(woneinit==0) {
1724
+							if(run_child_one_init_route()<0)
1725
+								goto error;
1726
+						}
1727
+
1723 1728
 						return sctp_core_rcv_loop();
1724 1729
 					}
1730
+					woneinit = 1;
1725 1731
 				}
1726 1732
 			/*parent*/
1727 1733
 			/*close(sctp_sock)*/; /*if closed=>sendto invalid fd errors?*/
... ...
@@ -1777,7 +1783,7 @@ int main_loop(void)
1777 1783
 #ifdef USE_TCP
1778 1784
 		if (!tcp_disable){
1779 1785
 				/* start tcp  & tls receivers */
1780
-			if (tcp_init_children()<0) goto error;
1786
+			if (tcp_init_children(&woneinit)<0) goto error;
1781 1787
 				/* start tcp+tls main attendant proc */
1782 1788
 			pid = fork_process(PROC_TCP_MAIN, "tcp main process", 0);
1783 1789
 			if (pid<0){
Browse code

core: main - terminate using ksr_exit() instead of return

Daniel-Constantin Mierla authored on 09/02/2021 13:00:23
Showing 1 changed files
... ...
@@ -2903,7 +2903,7 @@ try_again:
2903 2903
 					strerror(errno), errno);
2904 2904
 	}
2905 2905
 	/* else terminate process */
2906
-	return ret;
2906
+	ksr_exit(ret);
2907 2907
 
2908 2908
 error:
2909 2909
 	/*kill everything*/
... ...
@@ -2913,7 +2913,7 @@ error:
2913 2913
 			fprintf(stderr, "error sending exit status: %s [%d]\n",
2914 2914
 					strerror(errno), errno);
2915 2915
 	}
2916
-	return -1;
2916
+	ksr_exit(-1);
2917 2917
 }
2918 2918
 
2919 2919
 
Browse code

core: replaced --no-atexit with --atexit=val

- val can be y[es] or 1 to enable execution of atexit callbacks; n[o] or
0 to disable the execution of atexit callbacks
- default yes
- simplified ksr_exit() macro

Daniel-Constantin Mierla authored on 03/02/2021 11:49:29
Showing 1 changed files
... ...
@@ -164,6 +164,10 @@ Options:\n\
164 164
                   disable with no or off\n\
165 165
     --alias=val  Add an alias, the value has to be '[proto:]hostname[:port]'\n\
166 166
                   (like for 'alias' global parameter)\n\
167
+    --atexit=val Control atexit callbacks execution from external libraries\n\
168
+                  which may access destroyed shm memory causing crash on shutdown.\n\
169
+                  Can be y[es] or 1 to enable atexit callbacks, n[o] or 0 to disable,\n\
170
+                  default is yes.\n\
167 171
     -A define    Add config pre-processor define (e.g., -A WITH_AUTH,\n\
168 172
                   -A 'FLT_ACC=1', -A 'DEFVAL=\"str-val\"')\n\
169 173
     -b nr        Maximum receive buffer size which will not be exceeded by\n\
... ...
@@ -209,9 +213,7 @@ Options:\n\
209 213
 #ifdef USE_TCP
210 214
 "    -N           Number of tcp child processes (default: equal to `-n')\n"
211 215
 #endif
212
-"    --no-atexit  Skip atexit callbacks execution from external libraries\n\
213
-                 which may access destroyed shm memory causing crash on shutdown\n\
214
-    -O nr        Script optimization level (debugging option)\n\
216
+"    -O nr        Script optimization level (debugging option)\n\
215 217
     -P file      Create a pid file\n"
216 218
 #ifdef USE_SCTP
217 219
 "    -Q           Number of sctp child processes (default: equal to `-n')\n"
... ...
@@ -537,7 +539,7 @@ char *sr_memmng_shm = NULL;
537 539
 static int *_sr_instance_started = NULL;
538 540
 
539 541
 int ksr_cfg_print_mode = 0;
540
-int ksr_no_atexit = 0;
542
+int ksr_atexit_mode = 1;
541 543
 
542 544
 /**
543 545
  * return 1 if all child processes were forked
... ...
@@ -741,7 +743,7 @@ void handle_sigs(void)
741 743
 			LM_NOTICE("Thank you for flying " NAME "!!!\n");
742 744
 			/* shutdown/kill all the children */
743 745
 			shutdown_children(SIGTERM, 1);
744
-			ksr_exit(ksr_no_atexit, 0);
746
+			ksr_exit(0);
745 747
 			break;
746 748
 
747 749
 		case SIGUSR1:
... ...
@@ -809,9 +811,9 @@ void handle_sigs(void)
809 811
 			/* exit */
810 812
 			shutdown_children(SIGTERM, 1);
811 813
 			if (WIFSIGNALED(chld_status)) {
812
-				ksr_exit(ksr_no_atexit, 1);
814
+				ksr_exit(1);
813 815
 			} else {
814
-				ksr_exit(ksr_no_atexit, 0);
816