Browse code

core, lib, modules: restructured source code tree

- new folder src/ to hold the source code for main project applications
- main.c is in src/
- all core files are subfolder are in src/core/
- modules are in src/modules/
- libs are in src/lib/
- application Makefiles are in src/
- application binary is built in src/ (src/kamailio)

Daniel-Constantin Mierla authored on 07/12/2016 11:03:51
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,656 +0,0 @@
1
-/*
2
- * Copyright (C) 2001-2003 FhG Fokus
3
- *
4
- * Permission to use, copy, modify, and distribute this software for any
5
- * purpose with or without fee is hereby granted, provided that the above
6
- * copyright notice and this permission notice appear in all copies.
7
- *
8
- * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9
- * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10
- * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11
- * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12
- * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13
- * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14
- * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
- */
16
-
17
-/*!
18
- * \file
19
- * \brief Kamailio core :: Daemon init
20
- * \ingroup core
21
- * Module: \ref core
22
- */
23
-
24
-
25
-
26
-#include <sys/types.h>
27
-
28
-#define _XOPEN_SOURCE   /*!< needed on linux for the  getpgid prototype,  but
29
-                           openbsd 3.2 won't include common types (uint a.s.o)
30
-                           if defined before including sys/types.h */
31
-#define _XOPEN_SOURCE_EXTENDED /*!< same as \ref _XOPEN_SOURCE */
32
-#define __USE_XOPEN_EXTENDED /*!< same as \ref _XOPEN_SOURCE, overrides features.h */
33
-#define __EXTENSIONS__ /*!< needed on solaris: if XOPEN_SOURCE is defined
34
-                          struct timeval defintion from <sys/time.h> won't
35
-                          be included => workarround define _EXTENSIONS_
36
-                           -andrei */
37
-#include <signal.h>
38
-#include <syslog.h>
39
-#include <errno.h>
40
-#include <string.h>
41
-#include <stdio.h>
42
-#include <stdlib.h>
43
-#include <sys/time.h>    
44
-#include <sys/resource.h> /* setrlimit */
45
-#include <unistd.h>
46
-#include <pwd.h>
47
-#include <grp.h>
48
-
49
-#ifdef __OS_linux
50
-#include <sys/prctl.h>
51
-#endif
52
-
53
-#ifdef HAVE_SCHED_SETSCHEDULER
54
-#include <sched.h>
55
-#endif
56
-
57
-#ifdef _POSIX_MEMLOCK
58
-#define HAVE_MLOCKALL
59
-#include <sys/mman.h>
60
-#endif
61
-
62
-#include "daemonize.h"
63
-#include "globals.h"
64
-#include "dprint.h"
65
-#include "signals.h"
66
-#include "cfg/cfg.h"
67
-
68
-
69
-#define MAX_FD 32 /* maximum number of inherited open file descriptors,
70
-		    (normally it shouldn't  be bigger  than 3) */
71
-
72
-/** temporary pipe FDs for sending exit status back to the ancestor process.
73
- * This pipe is used to send the desired exit status to the initial process,
74
- * that waits for it in the foreground. This way late errors preventing
75
- * startup (e.g. during modules child inits or TCP late init) can still be
76
- * reported back.
77
- */
78
-static int daemon_status_fd[2];
79
-
80
-
81
-
82
-/** init daemon status reporting.
83
- * Must be called before any other daemon_status function has a chance to
84
- * run.
85
- */
86
-void daemon_status_init()
87
-{
88
-	daemon_status_fd[0] = -1;
89
-	daemon_status_fd[1] = -1;
90
-}
91
-
92
-
93
-
94
-/** pre-daemonize init for daemon status reporting.
95
- * Must be called before forking.
96
- * Typically the parent process will call daemon_status_wait() while
97
- * one of the children will call daemon_status_send() at some point.
98
- *
99
- * @return 0 on success, -1 on error (and sets errno).
100
- */
101
-int daemon_status_pre_daemonize(void)
102
-{
103
-	int ret;
104
-	
105
-retry:
106
-	ret = pipe(daemon_status_fd);
107
-	if (ret < 0 && errno == EINTR)
108
-		goto retry;
109
-	return ret;
110
-}
111
-
112
-
113
-
114
-/** wait for an exit status to be send by daemon_status_send().
115
- * @param status - filled with the sent status (a char).
116
- * @return  0 on success, -1 on error (e.g. process died before sending
117
- *          status, not initialized a.s.o.).
118
- * Side-effects: it will close the write side of the pipe
119
- *  (must not be used from the same process as the daemon_status_send()).
120
- * Note: if init is not complete (only init, but no pre-daemonize)
121
- * it will return success always and status 0.
122
- */
123
-int daemon_status_wait(char* status)
124
-{
125
-	int ret;
126
-	
127
-	/* close the output side of the pipe */
128
-	if (daemon_status_fd[1] != -1) {
129
-		close(daemon_status_fd[1]);
130
-		daemon_status_fd[1] = -1;
131
-	}
132
-	if (daemon_status_fd[0] == -1) {
133
-		*status = 0;
134
-		return -1;
135
-	}
136
-retry:
137
-	ret = read(daemon_status_fd[0], status, 1);
138
-	if (ret < 0 && errno == EINTR)
139
-		goto retry;
140
-	return (ret ==1 ) ? 0 : -1;
141
-}
142
-
143
-
144
-
145
-/** send 'status' to a waiting process running daemon_status_wait().
146
- * @param status - status byte
147
- * @return 0 on success, -1 on error.
148
- * Note: if init is not complete (only init, but no pre-daemonize)
149
- * it will return success always.
150
- */
151
-int daemon_status_send(char status)
152
-{
153
-	int ret;
154
-
155
-	if (daemon_status_fd[1] == -1)
156
-		return 0;
157
-retry:
158
-	ret = write(daemon_status_fd[1], &status, 1);
159
-	if (ret < 0 && errno == EINTR)
160
-		goto retry;
161
-	return (ret ==1 ) ? 0 : -1;
162
-}
163
-
164
-
165
-
166
-/** cleanup functions for new processes.
167
- * Should be called after fork(), for each new process that _does_ _not_
168
- * use  daemon_status_send() or daemon_status_wait().
169
- */
170
-void daemon_status_on_fork_cleanup()
171
-{
172
-	if (daemon_status_fd[0] != -1) {
173
-		close(daemon_status_fd[0]);
174
-		daemon_status_fd[0] = -1;
175
-	}
176
-	if (daemon_status_fd[1] != -1) {
177
-		close(daemon_status_fd[1]);
178
-		daemon_status_fd[1] = -1;
179
-	}
180
-}
181
-
182
-
183
-
184
-/** cleanup functions for processes that don't intead to wait.
185
- * Should be called after fork(), for each new process that doesn't
186
- * use daemon_status_wait().
187
- */
188
-void daemon_status_no_wait()
189
-{
190
-	if (daemon_status_fd[0] != -1) {
191
-		close(daemon_status_fd[0]);
192
-		daemon_status_fd[0] = -1;
193
-	}
194
-}
195
-
196
-
197
-/**
198
- * enable dumpable flag for core dumping after setuid() & friends
199
- * @return 0 when no critical error occurred, -1 on such error
200
- */
201
-int enable_dumpable(void)
202
-{
203
-#ifdef __OS_linux
204
-	struct rlimit lim;
205
-	/* re-enable core dumping on linux after setuid() & friends */
206
-	if(disable_core_dump==0) {
207
-		LM_DBG("trying enable core dumping...\n");
208
-		if(prctl(PR_GET_DUMPABLE, 0, 0, 0, 0)<=0) {
209
-			LM_DBG("core dumping is disabled now...\n");
210
-			if(prctl(PR_SET_DUMPABLE, 1, 0, 0, 0)<0) {
211
-				LM_WARN("cannot re-enable core dumping!\n");
212
-			} else {
213
-				LM_DBG("core dumping has just been enabled...\n");
214
-				if (getrlimit(RLIMIT_CORE, &lim)<0){
215
-					LM_CRIT( "cannot get the maximum core size: %s\n",
216
-							strerror(errno));
217
-					return -1;
218
-				} else {
219
-					LM_DBG("current core file limit: %lu (max: %lu)\n",
220
-						(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
221
-				}
222
-			}
223
-		} else {
224
-			LM_DBG("core dumping is enabled now (%d)...\n",
225
-					prctl(PR_GET_DUMPABLE, 0, 0, 0, 0));
226
-		}
227
-	}
228
-#endif
229
-	return 0;
230
-}
231
-
232
-/** daemon init.
233
- *@param name - daemon name used for logging (used when opening syslog).
234
- *@param status_wait  - if 1 the original process will wait until it gets
235
- *                  an exit code send using daemon_status_send().
236
- *@return 0 in the child process (in case of daemonize mode),
237
- *        -1 on error.
238
- * The original process that called daemonize() will be terminated if
239
- * dont_daemonize == 0. The exit code depends on status_wait. If status_wait
240
- * is non-zero, the original process will wait for a status code, that
241
- * must be sent with daemon_status_send() (daemon_status_send() must be
242
- * called or the original process will remain waiting until all the children
243
- * close()). If status_wait is 0, the original process will exit immediately
244
- * with exit(0).
245
- * Global variables/config params used:
246
- * dont_daemonize
247
- * chroot_dir
248
- * working_dir
249
- * pid_file - if set the pid will be written here (ascii).
250
- * pgid_file - if set, the pgid will be written here (ascii).
251
- * log_stderr - if not set syslog will be opened (openlog(name,...))
252
- * 
253
- *
254
- * Side-effects:
255
- *  sets own_pgid after becoming session leader (own process group).
256
-*/
257
-int daemonize(char*  name,  int status_wait)
258
-{
259
-	FILE *pid_stream;
260
-	pid_t pid;
261
-	int r, p;
262
-	char pipe_status;
263
-	uid_t pid_uid;
264
-	gid_t pid_gid;
265
-
266
-	if(uid) pid_uid = uid;
267
-	else pid_uid = -1;
268
-
269
-	if(gid) pid_gid = gid;
270
-	else pid_gid = -1;
271
-
272
-	p=-1;
273
-	/* flush std file descriptors to avoid flushes after fork
274
-	 *  (same message appearing multiple times)
275
-	 *  and switch to unbuffered
276
-	 */
277
-	setbuf(stdout, 0);
278
-	setbuf(stderr, 0);
279
-	if (chroot_dir&&(chroot(chroot_dir)<0)){
280
-		LM_CRIT("Cannot chroot to %s: %s\n", chroot_dir, strerror(errno));
281
-		goto error;
282
-	}
283
-	
284
-	if (chdir(working_dir)<0){
285
-		LM_CRIT("cannot chdir to %s: %s\n", working_dir, strerror(errno));
286
-		goto error;
287
-	}
288
-
289
-	if (!dont_daemonize) {
290
-		if (status_wait) {
291
-			if (daemon_status_pre_daemonize() < 0)
292
-				goto error;
293
-		}
294
-		/* fork to become!= group leader*/
295
-		if ((pid=fork())<0){
296
-			LM_CRIT("Cannot fork:%s\n", strerror(errno));
297
-			goto error;
298
-		}else if (pid!=0){
299
-			if (status_wait) {
300
-				if (daemon_status_wait(&pipe_status) == 0)
301
-					exit((int)pipe_status);
302
-				else{
303
-					LM_ERR("Main process exited before writing to pipe\n");
304
-					exit(-1);
305
-				}
306
-			}
307
-			exit(0);
308
-		}
309
-		if (status_wait)
310
-			daemon_status_no_wait(); /* clean unused read fd */
311
-		/* become session leader to drop the ctrl. terminal */
312
-		if (setsid()<0){
313
-			LM_WARN("setsid failed: %s\n",strerror(errno));
314
-		}else{
315
-			own_pgid=1;/* we have our own process group */
316
-		}
317
-		/* fork again to drop group  leadership */
318
-		if ((pid=fork())<0){
319
-			LM_CRIT("Cannot  fork:%s\n", strerror(errno));
320
-			goto error;
321
-		}else if (pid!=0){
322
-			/*parent process => exit */
323
-			exit(0);
324
-		}
325
-	}
326
-
327
-	if(enable_dumpable()<0)
328
-		goto error;
329
-
330
-	/* added by noh: create a pid file for the main process */
331
-	if (pid_file!=0){
332
-		
333
-		if ((pid_stream=fopen(pid_file, "r"))!=NULL){
334
-			if (fscanf(pid_stream, "%d", &p) < 0) {
335
-				LM_WARN("could not parse pid file %s\n", pid_file);
336
-			}
337
-			fclose(pid_stream);
338
-			if (p==-1){
339
-				LM_CRIT("pid file %s exists, but doesn't contain a valid"
340
-					" pid number\n", pid_file);
341
-				goto error;
342
-			}
343
-			if (kill((pid_t)p, 0)==0 || errno==EPERM){
344
-				LM_CRIT("running process found in the pid file %s\n",
345
-					pid_file);
346
-				goto error;
347
-			}else{
348
-				LM_WARN("pid file contains old pid, replacing pid\n");
349
-			}
350
-		}
351
-		pid=getpid();
352
-		if ((pid_stream=fopen(pid_file, "w"))==NULL){
353
-			LM_WARN("unable to create pid file %s: %s\n", 
354
-				pid_file, strerror(errno));
355
-			goto error;
356
-		}else{
357
-			fprintf(pid_stream, "%i\n", (int)pid);
358
-			fclose(pid_stream);
359
-			if(chown(pid_file, pid_uid, pid_gid)<0) {
360
-				LM_ERR("failed to chwon PID file: %s\n", strerror(errno));
361
-				goto error;
362
-			}
363
-		}
364
-	}
365
-
366
-	if (pgid_file!=0){
367
-		if ((pid_stream=fopen(pgid_file, "r"))!=NULL){
368
-			if (fscanf(pid_stream, "%d", &p) < 0) {
369
-				 LM_WARN("could not parse pgid file %s\n", pgid_file);
370
-			}
371
-			fclose(pid_stream);
372
-			if (p==-1){
373
-				LM_CRIT("pgid file %s exists, but doesn't contain a valid"
374
-				    " pgid number\n", pgid_file);
375
-				goto error;
376
-			}
377
-		}
378
-		if (own_pgid){
379
-			pid=getpgid(0);
380
-			if ((pid_stream=fopen(pgid_file, "w"))==NULL){
381
-				LM_WARN("unable to create pgid file %s: %s\n",
382
-					pgid_file, strerror(errno));
383
-				goto error;
384
-			}else{
385
-				fprintf(pid_stream, "%i\n", (int)pid);
386
-				fclose(pid_stream);
387
-				if(chown(pid_file, pid_uid, pid_gid)<0) {
388
-					LM_ERR("failed to chwon PGID file: %s\n", strerror(errno));
389
-					goto error;
390
-				}
391
-			}
392
-		}else{
393
-			LM_WARN("we don't have our own process so we won't save"
394
-					" our pgid\n");
395
-			unlink(pgid_file); /* just to be sure nobody will miss-use the old
396
-								  value*/
397
-		}
398
-	}
399
-	
400
-	/* try to replace stdin, stdout & stderr with /dev/null */
401
-	if (freopen("/dev/null", "r", stdin)==0){
402
-		LM_ERR("unable to replace stdin with /dev/null: %s\n",
403
-				strerror(errno));
404
-		/* continue, leave it open */
405
-	};
406
-	if (freopen("/dev/null", "w", stdout)==0){
407
-		LM_ERR("unable to replace stdout with /dev/null: %s\n",
408
-				strerror(errno));
409
-		/* continue, leave it open */
410
-	};
411
-	/* close stderr only if log_stderr=0 */
412
-	if ((!log_stderr) &&(freopen("/dev/null", "w", stderr)==0)){
413
-		LM_ERR("unable to replace stderr with /dev/null: %s\n",
414
-				strerror(errno));
415
-		/* continue, leave it open */
416
-	};
417
-	
418
-	/* close all but the daemon_status_fd output as the main process
419
-	  must still write into it to tell the parent to exit with 0 */
420
-	closelog();
421
-	for (r=3;r<MAX_FD; r++){
422
-		if(r !=  daemon_status_fd[1])
423
-			close(r);
424
-	}
425
-	
426
-	if (log_stderr==0)
427
-		openlog(name, LOG_PID|LOG_CONS, cfg_get(core, core_cfg, log_facility));
428
-		/* LOG_CONS, LOG_PERRROR ? */
429
-
430
-	return  0;
431
-
432
-error:
433
-	return -1;
434
-}
435
-
436
-
437
-
438
-int do_suid()
439
-{
440
-	struct passwd *pw;
441
-	
442
-	if (gid){
443
-		if(gid!=getgid()) {
444
-			if(setgid(gid)<0){
445
-				LM_CRIT("cannot change gid to %d: %s\n", gid, strerror(errno));
446
-				goto error;
447
-			}
448
-		}
449
-	}
450
-	
451
-	if(uid){
452
-		if (!(pw = getpwuid(uid))){
453
-			LM_CRIT("user lookup failed: %s\n", strerror(errno));
454
-			goto error;
455
-		}
456
-		if(uid!=getuid()) {
457
-			if(initgroups(pw->pw_name, pw->pw_gid)<0){
458
-				LM_CRIT("cannot set supplementary groups: %s\n", 
459
-							strerror(errno));
460
-				goto error;
461
-			}
462
-			if(setuid(uid)<0){
463
-				LM_CRIT("cannot change uid to %d: %s\n", uid, strerror(errno));
464
-				goto error;
465
-			}
466
-		}
467
-	}
468
-
469
-	if(enable_dumpable()<0)
470
-		goto error;
471
-
472
-	return 0;
473
-error:
474
-	return -1;
475
-}
476
-
477
-
478
-
479
-/*! \brief try to increase the open file limit */
480
-int increase_open_fds(int target)
481
-{
482
-	struct rlimit lim;
483
-	struct rlimit orig;
484
-	
485
-	if (getrlimit(RLIMIT_NOFILE, &lim)<0){
486
-		LM_CRIT("cannot get the maximum number of file descriptors: %s\n",
487
-				strerror(errno));
488
-		goto error;
489
-	}
490
-	orig=lim;
491
-	LM_DBG("current open file limits: %lu/%lu\n",
492
-			(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
493
-	if ((lim.rlim_cur==RLIM_INFINITY) || (target<=lim.rlim_cur))
494
-		/* nothing to do */
495
-		goto done;
496
-	else if ((lim.rlim_max==RLIM_INFINITY) || (target<=lim.rlim_max)){
497
-		lim.rlim_cur=target; /* increase soft limit to target */
498
-	}else{
499
-		/* more than the hard limit */
500
-		LM_INFO("trying to increase the open file limit"
501
-				" past the hard limit (%ld -> %d)\n", 
502
-				(unsigned long)lim.rlim_max, target);
503
-		lim.rlim_max=target;
504
-		lim.rlim_cur=target;
505
-	}
506
-	LM_DBG("increasing open file limits to: %lu/%lu\n",
507
-			(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
508
-	if (setrlimit(RLIMIT_NOFILE, &lim)<0){
509
-		LM_CRIT("cannot increase the open file limit to"
510
-				" %lu/%lu: %s\n",
511
-				(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max,
512
-				strerror(errno));
513
-		if (orig.rlim_max>orig.rlim_cur){
514
-			/* try to increase to previous maximum, better than not increasing
515
-		 	* at all */
516
-			lim.rlim_max=orig.rlim_max;
517
-			lim.rlim_cur=orig.rlim_max;
518
-			if (setrlimit(RLIMIT_NOFILE, &lim)==0){
519
-				LM_CRIT(" maximum number of file descriptors increased to"
520
-						" %u\n",(unsigned)orig.rlim_max);
521
-			}
522
-		}
523
-		goto error;
524
-	}
525
-done:
526
-	return 0;
527
-error:
528
-	return -1;
529
-}
530
-
531
-
532
-
533
-/*! \brief enable core dumps */
534
-int set_core_dump(int enable, long unsigned int size)
535
-{
536
-	struct rlimit lim;
537
-	struct rlimit newlim;
538
-	
539
-	if (enable){
540
-		if (getrlimit(RLIMIT_CORE, &lim)<0){
541
-			LM_CRIT("cannot get the maximum core size: %s\n",
542
-					strerror(errno));
543
-			goto error;
544
-		}
545
-		if (lim.rlim_cur<size){
546
-			/* first try max limits */
547
-			newlim.rlim_max=RLIM_INFINITY;
548
-			newlim.rlim_cur=newlim.rlim_max;
549
-			if (setrlimit(RLIMIT_CORE, &newlim)==0) goto done;
550
-			/* now try with size */
551
-			if (lim.rlim_max<size){
552
-				newlim.rlim_max=size;
553
-			}
554
-			newlim.rlim_cur=newlim.rlim_max;
555
-			if (setrlimit(RLIMIT_CORE, &newlim)==0) goto done;
556
-			/* if this failed too, try rlim_max, better than nothing */
557
-			newlim.rlim_max=lim.rlim_max;
558
-			newlim.rlim_cur=newlim.rlim_max;
559
-			if (setrlimit(RLIMIT_CORE, &newlim)<0){
560
-				LM_CRIT("could increase core limits at all: %s\n",
561
-						strerror (errno));
562
-			}else{
563
-				LM_CRIT("core limits increased only to %lu\n",
564
-						(unsigned long)lim.rlim_max);
565
-			}
566
-			goto error; /* it's an error we haven't got the size we wanted*/
567
-		}else{
568
-			newlim.rlim_cur=lim.rlim_cur;
569
-			newlim.rlim_max=lim.rlim_max;
570
-			goto done; /*nothing to do */
571
-		}
572
-	}else{
573
-		/* disable */
574
-		newlim.rlim_cur=0;
575
-		newlim.rlim_max=0;
576
-		if (setrlimit(RLIMIT_CORE, &newlim)<0){
577
-			LM_CRIT("failed to disable core dumps: %s\n",
578
-					strerror(errno));
579
-			goto error;
580
-		}
581
-	}
582
-done:
583
-	LM_DBG("core dump limits set to %lu\n", (unsigned long)newlim.rlim_cur);
584
-	return 0;
585
-error:
586
-	return -1;
587
-}
588
-
589
-
590
-
591
-/*! \brief lock pages in memory (make the process not swapable) */
592
-int mem_lock_pages()
593
-{
594
-#ifdef HAVE_MLOCKALL
595
-	if (mlockall(MCL_CURRENT|MCL_FUTURE) !=0){
596
-		LM_WARN("failed to lock the memory pages (disable swap): %s [%d]\n",
597
-				strerror(errno), errno);
598
-		goto error;
599
-	}
600
-	return 0;
601
-error:
602
-	return -1;
603
-#else /* if MLOCKALL not defined return error */
604
-		LM_WARN("failed to lock the memory pages: no mlockall support\n");
605
-	return -1;
606
-#endif 
607
-}
608
-
609
-
610
-/*! \brief tries to set real time priority 
611
- * policy: 0 - SCHED_OTHER, 1 - SCHED_RR, 2 - SCHED_FIFO */
612
-int set_rt_prio(int prio, int policy)
613
-{
614
-#ifdef HAVE_SCHED_SETSCHEDULER
615
-	struct sched_param sch_p;
616
-	int min_prio, max_prio;
617
-	int sched_policy;
618
-	
619
-	switch(policy){
620
-		case 0:
621
-			sched_policy=SCHED_OTHER;
622
-			break;
623
-		case 1:
624
-			sched_policy=SCHED_RR;
625
-			break;
626
-		case 2:
627
-			sched_policy=SCHED_FIFO;
628
-			break;
629
-		default:
630
-			LM_WARN("invalid scheduling policy,using SCHED_OTHER\n");
631
-			sched_policy=SCHED_OTHER;
632
-	}
633
-	memset(&sch_p, 0, sizeof(sch_p));
634
-	max_prio=sched_get_priority_max(policy);
635
-	min_prio=sched_get_priority_min(policy);
636
-	if (prio<min_prio){
637
-		LM_WARN("scheduling priority %d too small, using minimum value"
638
-					" (%d)\n", prio, min_prio);
639
-		prio=min_prio;
640
-	}else if (prio>max_prio){
641
-		LM_WARN("scheduling priority %d too big, using maximum value"
642
-					" (%d)\n", prio, max_prio);
643
-		prio=max_prio;
644
-	}
645
-	sch_p.sched_priority=prio;
646
-	if (sched_setscheduler(0, sched_policy, &sch_p) != 0){
647
-		LM_WARN("could not switch to real time priority: %s [%d]\n",
648
-					strerror(errno), errno);
649
-		return -1;
650
-	};
651
-	return 0;
652
-#else
653
-	LM_WARN("real time support not available\n");
654
-	return -1;
655
-#endif
656
-}
Browse code

core: fix spelling errors catched by lintian #688

Victor Seva authored on 29/06/2016 14:48:09
Showing 1 changed files
... ...
@@ -196,7 +196,7 @@ void daemon_status_no_wait()
196 196
 
197 197
 /**
198 198
  * enable dumpable flag for core dumping after setuid() & friends
199
- * @return 0 when no critical error occured, -1 on such error
199
+ * @return 0 when no critical error occurred, -1 on such error
200 200
  */
201 201
 int enable_dumpable(void)
202 202
 {
Browse code

core: fix spelling errors catched by lintian #688

Victor Seva authored on 29/06/2016 14:29:17
Showing 1 changed files
... ...
@@ -114,7 +114,7 @@ retry:
114 114
 /** wait for an exit status to be send by daemon_status_send().
115 115
  * @param status - filled with the sent status (a char).
116 116
  * @return  0 on success, -1 on error (e.g. process died before sending
117
- *          status, not intialized a.s.o.).
117
+ *          status, not initialized a.s.o.).
118 118
  * Side-effects: it will close the write side of the pipe
119 119
  *  (must not be used from the same process as the daemon_status_send()).
120 120
  * Note: if init is not complete (only init, but no pre-daemonize)
Browse code

daemonize.c: logging: DBG -> LM_DBG

Ovidiu Sas authored on 13/01/2015 17:33:55
Showing 1 changed files
... ...
@@ -488,7 +488,7 @@ int increase_open_fds(int target)
488 488
 		goto error;
489 489
 	}
490 490
 	orig=lim;
491
-	DBG("current open file limits: %lu/%lu\n",
491
+	LM_DBG("current open file limits: %lu/%lu\n",
492 492
 			(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
493 493
 	if ((lim.rlim_cur==RLIM_INFINITY) || (target<=lim.rlim_cur))
494 494
 		/* nothing to do */
... ...
@@ -503,7 +503,7 @@ int increase_open_fds(int target)
503 503
 		lim.rlim_max=target;
504 504
 		lim.rlim_cur=target;
505 505
 	}
506
-	DBG("increasing open file limits to: %lu/%lu\n",
506
+	LM_DBG("increasing open file limits to: %lu/%lu\n",
507 507
 			(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
508 508
 	if (setrlimit(RLIMIT_NOFILE, &lim)<0){
509 509
 		LM_CRIT("cannot increase the open file limit to"
... ...
@@ -580,7 +580,7 @@ int set_core_dump(int enable, long unsigned int size)
580 580
 		}
581 581
 	}
582 582
 done:
583
-	DBG("core dump limits set to %lu\n", (unsigned long)newlim.rlim_cur);
583
+	LM_DBG("core dump limits set to %lu\n", (unsigned long)newlim.rlim_cur);
584 584
 	return 0;
585 585
 error:
586 586
 	return -1;
Browse code

core Update headers, change "SIP-router" to "Kamailio", update doxygen file headers

Olle E. Johansson authored on 03/01/2015 14:15:58
Showing 1 changed files
... ...
@@ -16,7 +16,7 @@
16 16
 
17 17
 /*!
18 18
  * \file
19
- * \brief SIP-router core :: 
19
+ * \brief Kamailio core :: Daemon init
20 20
  * \ingroup core
21 21
  * Module: \ref core
22 22
  */
Browse code

Core Removed history, svn $id$ and doxygen udpates on the .c files

Olle E. Johansson authored on 03/01/2015 09:53:17
Showing 1 changed files
... ...
@@ -1,6 +1,4 @@
1 1
 /*
2
- * $Id$
3
- *
4 2
  * Copyright (C) 2001-2003 FhG Fokus
5 3
  *
6 4
  * Permission to use, copy, modify, and distribute this software for any
... ...
@@ -15,17 +13,7 @@
15 13
  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 14
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 15
  */
18
-/*
19
- * 
20
- * History:
21
- * --------
22
- *  2004-02-20  removed from ser main.c into its own file (andrei)
23
- *  2004-03-04  moved setuid/setgid in do_suid() (andrei)
24
- *  2004-03-25  added increase_open_fds & set_core_dump (andrei)
25
- *  2004-05-03  applied pgid patch from janakj
26
- *  2007-06-07  added mlock_pages (no swap) support (andrei)
27
-  *             added set_rt_prio() (andrei)
28
- */
16
+
29 17
 /*!
30 18
  * \file
31 19
  * \brief SIP-router core :: 
Browse code

core: don't do initgroups() if started by same user as -u - closing FS#473

Ovidiu Sas authored on 03/01/2015 03:03:22
Showing 1 changed files
... ...
@@ -465,12 +465,12 @@ int do_suid()
465 465
 			LM_CRIT("user lookup failed: %s\n", strerror(errno));
466 466
 			goto error;
467 467
 		}
468
-		if(initgroups(pw->pw_name, pw->pw_gid)<0){
469
-			LM_CRIT("cannot set supplementary groups: %s\n", 
470
-							strerror(errno));
471
-			goto error;
472
-		}
473 468
 		if(uid!=getuid()) {
469
+			if(initgroups(pw->pw_name, pw->pw_gid)<0){
470
+				LM_CRIT("cannot set supplementary groups: %s\n", 
471
+							strerror(errno));
472
+				goto error;
473
+			}
474 474
 			if(setuid(uid)<0){
475 475
 				LM_CRIT("cannot change uid to %d: %s\n", uid, strerror(errno));
476 476
 				goto error;
Browse code

core: don't do setuid() if started by same user as -u

- closing FS#473

Daniel-Constantin Mierla authored on 02/01/2015 10:16:28
Showing 1 changed files
... ...
@@ -452,9 +452,11 @@ int do_suid()
452 452
 	struct passwd *pw;
453 453
 	
454 454
 	if (gid){
455
-		if(setgid(gid)<0){
456
-			LM_CRIT("cannot change gid to %d: %s\n", gid, strerror(errno));
457
-			goto error;
455
+		if(gid!=getgid()) {
456
+			if(setgid(gid)<0){
457
+				LM_CRIT("cannot change gid to %d: %s\n", gid, strerror(errno));
458
+				goto error;
459
+			}
458 460
 		}
459 461
 	}
460 462
 	
... ...
@@ -468,9 +470,11 @@ int do_suid()
468 470
 							strerror(errno));
469 471
 			goto error;
470 472
 		}
471
-		if(setuid(uid)<0){
472
-			LM_CRIT("cannot change uid to %d: %s\n", uid, strerror(errno));
473
-			goto error;
473
+		if(uid!=getuid()) {
474
+			if(setuid(uid)<0){
475
+				LM_CRIT("cannot change uid to %d: %s\n", uid, strerror(errno));
476
+				goto error;
477
+			}
474 478
 		}
475 479
 	}
476 480
 
Browse code

daemonize.c: logging: convert LOG to LM_*

Ovidiu Sas authored on 03/10/2014 21:41:36
Showing 1 changed files
... ...
@@ -635,8 +635,7 @@ int set_rt_prio(int prio, int policy)
635 635
 			sched_policy=SCHED_FIFO;
636 636
 			break;
637 637
 		default:
638
-			LM_WARN("WARNING: invalid scheduling policy,using"
639
-						" SCHED_OTHER\n");
638
+			LM_WARN("invalid scheduling policy,using SCHED_OTHER\n");
640 639
 			sched_policy=SCHED_OTHER;
641 640
 	}
642 641
 	memset(&sch_p, 0, sizeof(sch_p));
Browse code

core: daemonize.c: convert logs to new format

Ovidiu Sas authored on 01/10/2014 16:54:31
Showing 1 changed files
... ...
@@ -224,11 +224,11 @@ int enable_dumpable(void)
224 224
 			} else {
225 225
 				LM_DBG("core dumping has just been enabled...\n");
226 226
 				if (getrlimit(RLIMIT_CORE, &lim)<0){
227
-					LOG(L_CRIT, "cannot get the maximum core size: %s\n",
227
+					LM_CRIT( "cannot get the maximum core size: %s\n",
228 228
 							strerror(errno));
229 229
 					return -1;
230 230
 				} else {
231
-					DBG("current core file limit: %lu (max: %lu)\n",
231
+					LM_DBG("current core file limit: %lu (max: %lu)\n",
232 232
 						(unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
233 233
 				}
234 234
 			}
... ...
@@ -289,12 +289,12 @@ int daemonize(char*  name,  int status_wait)
289 289
 	setbuf(stdout, 0);
290 290
 	setbuf(stderr, 0);
291 291
 	if (chroot_dir&&(chroot(chroot_dir)<0)){
292
-		LOG(L_CRIT, "Cannot chroot to %s: %s\n", chroot_dir, strerror(errno));
292
+		LM_CRIT("Cannot chroot to %s: %s\n", chroot_dir, strerror(errno));
293 293
 		goto error;
294 294
 	}
295 295
 	
296 296
 	if (chdir(working_dir)<0){
297
-		LOG(L_CRIT,"cannot chdir to %s: %s\n", working_dir, strerror(errno));
297
+		LM_CRIT("cannot chdir to %s: %s\n", working_dir, strerror(errno));
298 298
 		goto error;
299 299
 	}
300 300
 
... ...
@@ -305,14 +305,14 @@ int daemonize(char*  name,  int status_wait)
305 305
 		}
306 306
 		/* fork to become!= group leader*/
307 307
 		if ((pid=fork())<0){
308
-			LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno));
308
+			LM_CRIT("Cannot fork:%s\n", strerror(errno));
309 309
 			goto error;
310 310
 		}else if (pid!=0){
311 311
 			if (status_wait) {
312 312
 				if (daemon_status_wait(&pipe_status) == 0)
313 313
 					exit((int)pipe_status);
314 314
 				else{
315
-					LOG(L_ERR, "Main process exited before writing to pipe\n");
315
+					LM_ERR("Main process exited before writing to pipe\n");
316 316
 					exit(-1);
317 317
 				}
318 318
 			}
... ...
@@ -322,13 +322,13 @@ int daemonize(char*  name,  int status_wait)
322 322
 			daemon_status_no_wait(); /* clean unused read fd */
323 323
 		/* become session leader to drop the ctrl. terminal */
324 324
 		if (setsid()<0){
325
-			LOG(L_WARN, "setsid failed: %s\n",strerror(errno));
325
+			LM_WARN("setsid failed: %s\n",strerror(errno));
326 326
 		}else{
327 327
 			own_pgid=1;/* we have our own process group */
328 328
 		}
329 329
 		/* fork again to drop group  leadership */
330 330
 		if ((pid=fork())<0){
331
-			LOG(L_CRIT, "Cannot  fork:%s\n", strerror(errno));
331
+			LM_CRIT("Cannot  fork:%s\n", strerror(errno));
332 332
 			goto error;
333 333
 		}else if (pid!=0){
334 334
 			/*parent process => exit */
... ...
@@ -348,21 +348,21 @@ int daemonize(char*  name,  int status_wait)
348 348
 			}
349 349
 			fclose(pid_stream);
350 350
 			if (p==-1){
351
-				LOG(L_CRIT, "pid file %s exists, but doesn't contain a valid"
351
+				LM_CRIT("pid file %s exists, but doesn't contain a valid"
352 352
 					" pid number\n", pid_file);
353 353
 				goto error;
354 354
 			}
355 355
 			if (kill((pid_t)p, 0)==0 || errno==EPERM){
356
-				LOG(L_CRIT, "running process found in the pid file %s\n",
356
+				LM_CRIT("running process found in the pid file %s\n",
357 357
 					pid_file);
358 358
 				goto error;
359 359
 			}else{
360
-				LOG(L_WARN, "pid file contains old pid, replacing pid\n");
360
+				LM_WARN("pid file contains old pid, replacing pid\n");
361 361
 			}
362 362
 		}
363 363
 		pid=getpid();
364 364
 		if ((pid_stream=fopen(pid_file, "w"))==NULL){
365
-			LOG(L_WARN, "unable to create pid file %s: %s\n", 
365
+			LM_WARN("unable to create pid file %s: %s\n", 
366 366
 				pid_file, strerror(errno));
367 367
 			goto error;
368 368
 		}else{
... ...
@@ -382,7 +382,7 @@ int daemonize(char*  name,  int status_wait)
382 382
 			}
383 383
 			fclose(pid_stream);
384 384
 			if (p==-1){
385
-				LOG(L_CRIT, "pgid file %s exists, but doesn't contain a valid"
385
+				LM_CRIT("pgid file %s exists, but doesn't contain a valid"
386 386
 				    " pgid number\n", pgid_file);
387 387
 				goto error;
388 388
 			}
... ...
@@ -390,7 +390,7 @@ int daemonize(char*  name,  int status_wait)
390 390
 		if (own_pgid){
391 391
 			pid=getpgid(0);
392 392
 			if ((pid_stream=fopen(pgid_file, "w"))==NULL){
393
-				LOG(L_WARN, "unable to create pgid file %s: %s\n",
393
+				LM_WARN("unable to create pgid file %s: %s\n",
394 394
 					pgid_file, strerror(errno));
395 395