daemonize.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /*
  2. * $Id$
  3. *
  4. * Copyright (C) 2001-2003 FhG Fokus
  5. *
  6. * This file is part of ser, a free SIP server.
  7. *
  8. * ser is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version
  12. *
  13. * For a license to use the ser software under conditions
  14. * other than those described here, or to purchase support for this
  15. * software, please contact iptel.org by e-mail at the following addresses:
  16. * [email protected]
  17. *
  18. * ser is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with this program; if not, write to the Free Software
  25. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. */
  27. /*
  28. *
  29. * History:
  30. * --------
  31. * 2004-02-20 removed from ser main.c into its own file (andrei)
  32. * 2004-03-04 moved setuid/setgid in do_suid() (andrei)
  33. * 2004-03-25 added increase_open_fds & set_core_dump (andrei)
  34. * 2004-05-03 applied pgid patch from janakj
  35. * 2007-06-07 added mlock_pages (no swap) support (andrei)
  36. * added set_rt_prio() (andrei)
  37. */
  38. #include <sys/types.h>
  39. #define _XOPEN_SOURCE /* needed on linux for the getpgid prototype, but
  40. openbsd 3.2 won't include common types (uint a.s.o)
  41. if defined before including sys/types.h */
  42. #define _XOPEN_SOURCE_EXTENDED /* same as above */
  43. #define __USE_XOPEN_EXTENDED /* same as above, overrides features.h */
  44. #define __EXTENSIONS__ /* needed on solaris: if XOPEN_SOURCE is defined
  45. struct timeval defintion from <sys/time.h> won't
  46. be included => workarround define _EXTENSIONS_
  47. -andrei */
  48. #include <signal.h>
  49. #include <syslog.h>
  50. #include <errno.h>
  51. #include <string.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <sys/time.h>
  55. #include <sys/resource.h> /* setrlimit */
  56. #include <unistd.h>
  57. #include <pwd.h>
  58. #include <grp.h>
  59. #ifdef HAVE_SCHED_SETSCHEDULER
  60. #include <sched.h>
  61. #endif
  62. #ifdef _POSIX_MEMLOCK
  63. #define HAVE_MLOCKALL
  64. #include <sys/mman.h>
  65. #endif
  66. #include "daemonize.h"
  67. #include "globals.h"
  68. #include "dprint.h"
  69. #include "signals.h"
  70. #include "cfg/cfg.h"
  71. #define MAX_FD 32 /* maximum number of inherited open file descriptors,
  72. (normally it shouldn't be bigger than 3) */
  73. /* daemon init, return 0 on success, -1 on error */
  74. int daemonize(char* name)
  75. {
  76. FILE *pid_stream;
  77. pid_t pid;
  78. int r, p;
  79. p=-1;
  80. /* flush std file descriptors to avoid flushes after fork
  81. * (same message appearing multiple times)
  82. * and switch to unbuffered
  83. */
  84. setbuf(stdout, 0);
  85. setbuf(stderr, 0);
  86. if (chroot_dir&&(chroot(chroot_dir)<0)){
  87. LOG(L_CRIT, "Cannot chroot to %s: %s\n", chroot_dir, strerror(errno));
  88. goto error;
  89. }
  90. if (chdir(working_dir)<0){
  91. LOG(L_CRIT,"cannot chdir to %s: %s\n", working_dir, strerror(errno));
  92. goto error;
  93. }
  94. if (!dont_daemonize) {
  95. /* fork to become!= group leader*/
  96. if ((pid=fork())<0){
  97. LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno));
  98. goto error;
  99. }else if (pid!=0){
  100. /*parent process => exit */
  101. exit(0);
  102. }
  103. /* become session leader to drop the ctrl. terminal */
  104. if (setsid()<0){
  105. LOG(L_WARN, "setsid failed: %s\n",strerror(errno));
  106. }else{
  107. own_pgid=1;/* we have our own process group */
  108. }
  109. /* fork again to drop group leadership */
  110. if ((pid=fork())<0){
  111. LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno));
  112. goto error;
  113. }else if (pid!=0){
  114. /*parent process => exit */
  115. exit(0);
  116. }
  117. }
  118. /* added by noh: create a pid file for the main process */
  119. if (pid_file!=0){
  120. if ((pid_stream=fopen(pid_file, "r"))!=NULL){
  121. fscanf(pid_stream, "%d", &p);
  122. fclose(pid_stream);
  123. if (p==-1){
  124. LOG(L_CRIT, "pid file %s exists, but doesn't contain a valid"
  125. " pid number\n", pid_file);
  126. goto error;
  127. }
  128. if (kill((pid_t)p, 0)==0 || errno==EPERM){
  129. LOG(L_CRIT, "running process found in the pid file %s\n",
  130. pid_file);
  131. goto error;
  132. }else{
  133. LOG(L_WARN, "pid file contains old pid, replacing pid\n");
  134. }
  135. }
  136. pid=getpid();
  137. if ((pid_stream=fopen(pid_file, "w"))==NULL){
  138. LOG(L_WARN, "unable to create pid file %s: %s\n",
  139. pid_file, strerror(errno));
  140. goto error;
  141. }else{
  142. fprintf(pid_stream, "%i\n", (int)pid);
  143. fclose(pid_stream);
  144. }
  145. }
  146. if (pgid_file!=0){
  147. if ((pid_stream=fopen(pgid_file, "r"))!=NULL){
  148. fscanf(pid_stream, "%d", &p);
  149. fclose(pid_stream);
  150. if (p==-1){
  151. LOG(L_CRIT, "pgid file %s exists, but doesn't contain a valid"
  152. " pgid number\n", pgid_file);
  153. goto error;
  154. }
  155. }
  156. if (own_pgid){
  157. pid=getpgid(0);
  158. if ((pid_stream=fopen(pgid_file, "w"))==NULL){
  159. LOG(L_WARN, "unable to create pgid file %s: %s\n",
  160. pgid_file, strerror(errno));
  161. goto error;
  162. }else{
  163. fprintf(pid_stream, "%i\n", (int)pid);
  164. fclose(pid_stream);
  165. }
  166. }else{
  167. LOG(L_WARN, "we don't have our own process so we won't save"
  168. " our pgid\n");
  169. unlink(pgid_file); /* just to be sure nobody will miss-use the old
  170. value*/
  171. }
  172. }
  173. /* try to replace stdin, stdout & stderr with /dev/null */
  174. if (freopen("/dev/null", "r", stdin)==0){
  175. LOG(L_ERR, "unable to replace stdin with /dev/null: %s\n",
  176. strerror(errno));
  177. /* continue, leave it open */
  178. };
  179. if (freopen("/dev/null", "w", stdout)==0){
  180. LOG(L_ERR, "unable to replace stdout with /dev/null: %s\n",
  181. strerror(errno));
  182. /* continue, leave it open */
  183. };
  184. /* close stderr only if log_stderr=0 */
  185. if ((!log_stderr) &&(freopen("/dev/null", "w", stderr)==0)){
  186. LOG(L_ERR, "unable to replace stderr with /dev/null: %s\n",
  187. strerror(errno));
  188. /* continue, leave it open */
  189. };
  190. /* close any open file descriptors */
  191. closelog();
  192. for (r=3;r<MAX_FD; r++){
  193. close(r);
  194. }
  195. if (log_stderr==0)
  196. openlog(name, LOG_PID|LOG_CONS, cfg_get(core, core_cfg, log_facility));
  197. /* LOG_CONS, LOG_PERRROR ? */
  198. return 0;
  199. error:
  200. return -1;
  201. }
  202. int do_suid()
  203. {
  204. struct passwd *pw;
  205. if (gid){
  206. if(setgid(gid)<0){
  207. LOG(L_CRIT, "cannot change gid to %d: %s\n", gid, strerror(errno));
  208. goto error;
  209. }
  210. }
  211. if(uid){
  212. if (!(pw = getpwuid(uid))){
  213. LOG(L_CRIT, "user lookup failed: %s\n", strerror(errno));
  214. goto error;
  215. }
  216. if(initgroups(pw->pw_name, pw->pw_gid)<0){
  217. LOG(L_CRIT, "cannot set supplementary groups: %s\n",
  218. strerror(errno));
  219. goto error;
  220. }
  221. if(setuid(uid)<0){
  222. LOG(L_CRIT, "cannot change uid to %d: %s\n", uid, strerror(errno));
  223. goto error;
  224. }
  225. }
  226. return 0;
  227. error:
  228. return -1;
  229. }
  230. /* try to increase the open file limit */
  231. int increase_open_fds(int target)
  232. {
  233. struct rlimit lim;
  234. struct rlimit orig;
  235. if (getrlimit(RLIMIT_NOFILE, &lim)<0){
  236. LOG(L_CRIT, "cannot get the maximum number of file descriptors: %s\n",
  237. strerror(errno));
  238. goto error;
  239. }
  240. orig=lim;
  241. DBG("current open file limits: %lu/%lu\n",
  242. (unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
  243. if ((lim.rlim_cur==RLIM_INFINITY) || (target<=lim.rlim_cur))
  244. /* nothing to do */
  245. goto done;
  246. else if ((lim.rlim_max==RLIM_INFINITY) || (target<=lim.rlim_max)){
  247. lim.rlim_cur=target; /* increase soft limit to target */
  248. }else{
  249. /* more than the hard limit */
  250. LOG(L_INFO, "trying to increase the open file limit"
  251. " past the hard limit (%ld -> %d)\n",
  252. (unsigned long)lim.rlim_max, target);
  253. lim.rlim_max=target;
  254. lim.rlim_cur=target;
  255. }
  256. DBG("increasing open file limits to: %lu/%lu\n",
  257. (unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max);
  258. if (setrlimit(RLIMIT_NOFILE, &lim)<0){
  259. LOG(L_CRIT, "cannot increase the open file limit to"
  260. " %lu/%lu: %s\n",
  261. (unsigned long)lim.rlim_cur, (unsigned long)lim.rlim_max,
  262. strerror(errno));
  263. if (orig.rlim_max>orig.rlim_cur){
  264. /* try to increase to previous maximum, better than not increasing
  265. * at all */
  266. lim.rlim_max=orig.rlim_max;
  267. lim.rlim_cur=orig.rlim_max;
  268. if (setrlimit(RLIMIT_NOFILE, &lim)==0){
  269. LOG(L_CRIT, " maximum number of file descriptors increased to"
  270. " %u\n",(unsigned)orig.rlim_max);
  271. }
  272. }
  273. goto error;
  274. }
  275. done:
  276. return 0;
  277. error:
  278. return -1;
  279. }
  280. /* enable core dumps */
  281. int set_core_dump(int enable, int size)
  282. {
  283. struct rlimit lim;
  284. struct rlimit newlim;
  285. if (enable){
  286. if (getrlimit(RLIMIT_CORE, &lim)<0){
  287. LOG(L_CRIT, "cannot get the maximum core size: %s\n",
  288. strerror(errno));
  289. goto error;
  290. }
  291. if (lim.rlim_cur<size){
  292. /* first try max limits */
  293. newlim.rlim_max=RLIM_INFINITY;
  294. newlim.rlim_cur=newlim.rlim_max;
  295. if (setrlimit(RLIMIT_CORE, &newlim)==0) goto done;
  296. /* now try with size */
  297. if (lim.rlim_max<size){
  298. newlim.rlim_max=size;
  299. }
  300. newlim.rlim_cur=newlim.rlim_max;
  301. if (setrlimit(RLIMIT_CORE, &newlim)==0) goto done;
  302. /* if this failed too, try rlim_max, better than nothing */
  303. newlim.rlim_max=lim.rlim_max;
  304. newlim.rlim_cur=newlim.rlim_max;
  305. if (setrlimit(RLIMIT_CORE, &newlim)<0){
  306. LOG(L_CRIT, "could increase core limits at all: %s\n",
  307. strerror (errno));
  308. }else{
  309. LOG(L_CRIT, "core limits increased only to %lu\n",
  310. (unsigned long)lim.rlim_max);
  311. }
  312. goto error; /* it's an error we haven't got the size we wanted*/
  313. }
  314. goto done; /*nothing to do */
  315. }else{
  316. /* disable */
  317. newlim.rlim_cur=0;
  318. newlim.rlim_max=0;
  319. if (setrlimit(RLIMIT_CORE, &newlim)<0){
  320. LOG(L_CRIT, "failed to disable core dumps: %s\n",
  321. strerror(errno));
  322. goto error;
  323. }
  324. }
  325. done:
  326. DBG("core dump limits set to %lu\n", (unsigned long)newlim.rlim_cur);
  327. return 0;
  328. error:
  329. return -1;
  330. }
  331. /* lock pages in memory (make the process not swapable) */
  332. int mem_lock_pages()
  333. {
  334. #ifdef HAVE_MLOCKALL
  335. if (mlockall(MCL_CURRENT|MCL_FUTURE) !=0){
  336. LOG(L_WARN,"failed to lock the memory pages (disable swap): %s [%d]\n",
  337. strerror(errno), errno);
  338. goto error;
  339. }
  340. return 0;
  341. error:
  342. return -1;
  343. #else /* if MLOCKALL not defined return error */
  344. LOG(L_WARN,"failed to lock the memory pages: no mlockall support\n");
  345. return -1;
  346. #endif
  347. }
  348. /* tries to set real time priority
  349. * policy: 0 - SCHED_OTHER, 1 - SCHED_RR, 2 - SCHED_FIFO */
  350. int set_rt_prio(int prio, int policy)
  351. {
  352. #ifdef HAVE_SCHED_SETSCHEDULER
  353. struct sched_param sch_p;
  354. int min_prio, max_prio;
  355. int sched_policy;
  356. switch(policy){
  357. case 0:
  358. sched_policy=SCHED_OTHER;
  359. break;
  360. case 1:
  361. sched_policy=SCHED_RR;
  362. break;
  363. case 2:
  364. sched_policy=SCHED_FIFO;
  365. break;
  366. default:
  367. LOG(L_WARN, "WARNING: invalid scheduling policy,using"
  368. " SCHED_OTHER\n");
  369. sched_policy=SCHED_OTHER;
  370. }
  371. memset(&sch_p, 0, sizeof(sch_p));
  372. max_prio=sched_get_priority_max(policy);
  373. min_prio=sched_get_priority_min(policy);
  374. if (prio<min_prio){
  375. LOG(L_WARN, "scheduling priority %d too small, using minimum value"
  376. " (%d)\n", prio, min_prio);
  377. prio=min_prio;
  378. }else if (prio>max_prio){
  379. LOG(L_WARN, "scheduling priority %d too big, using maximum value"
  380. " (%d)\n", prio, max_prio);
  381. prio=max_prio;
  382. }
  383. sch_p.sched_priority=prio;
  384. if (sched_setscheduler(0, sched_policy, &sch_p) != 0){
  385. LOG(L_WARN, "could not switch to real time priority: %s [%d]\n",
  386. strerror(errno), errno);
  387. return -1;
  388. };
  389. return 0;
  390. #else
  391. LOG(L_WARN, "real time support not available\n");
  392. return -1;
  393. #endif
  394. }