Author: tjansen Date: Thu Oct 8 17:35:41 2009 New Revision: 1062 Log: -b option in pisa{c,s,n}d now uses pisa_daemonize from libpisa. Consolidated duplicate code into one central function. pisa_daemonize should be enhanced further, e.g. close file handles, set a directory... Modified: trunk/include/util.h trunk/libpisa/util.c trunk/pisacd/cdmain.c trunk/pisand/ndmain.c trunk/pisasd/sdmain.c Modified: trunk/include/util.h ============================================================================== --- trunk/include/util.h Thu Oct 8 17:21:45 2009 (r1061) +++ trunk/include/util.h Thu Oct 8 17:35:41 2009 (r1062) @@ -164,4 +164,6 @@ int pisa_check_and_create_dir(char *dirname, mode_t mode); int pisa_convert_string_to_address(const char *str, struct in6_addr *ip6); + +void pisa_daemonize(void); #endif /* PISA_UTIL_H */ Modified: trunk/libpisa/util.c ============================================================================== --- trunk/libpisa/util.c Thu Oct 8 17:21:45 2009 (r1061) +++ trunk/libpisa/util.c Thu Oct 8 17:35:41 2009 (r1062) @@ -1145,3 +1145,20 @@ return 0; } +/** + * Daemonize the running process. + */ +void pisa_daemonize(void) +{ + int result; + + result = fork(); + if (result < 0) { + PISA_ERROR("Forking to background failed!\n"); + exit(1); + } + if (result > 0) /* parent is done, let the child do the rest */ + exit(0); + + setsid(); +} Modified: trunk/pisacd/cdmain.c ============================================================================== --- trunk/pisacd/cdmain.c Thu Oct 8 17:21:45 2009 (r1061) +++ trunk/pisacd/cdmain.c Thu Oct 8 17:35:41 2009 (r1062) @@ -178,11 +178,9 @@ /* set the default or command line specified debug level */ cdconf_set_debuglevel(&cd_cfg); - /* fork in case of background running mode */ - if (cd_ctx.is_bgrun) { - if (fork() > 0) - exit(EXIT_SUCCESS); - } + /* daemonize pisacd when running background mode */ + if (cd_ctx.is_bgrun) + pisa_daemonize(); cd_ctx.natlist = pisa_nat_init(); Modified: trunk/pisand/ndmain.c ============================================================================== --- trunk/pisand/ndmain.c Thu Oct 8 17:21:45 2009 (r1061) +++ trunk/pisand/ndmain.c Thu Oct 8 17:35:41 2009 (r1062) @@ -154,11 +154,9 @@ nd_perf_init(); - /* fork in case of background running mode */ - if (nd_ctx.is_bgrun) { - if (fork() > 0) - exit(EXIT_SUCCESS); - } + /* daemonize pisand when running background mode */ + if (nd_ctx.is_bgrun) + pisa_daemonize(); /* Setup configuration file. * This is needed before any ndconf_read_*() operations. Modified: trunk/pisasd/sdmain.c ============================================================================== --- trunk/pisasd/sdmain.c Thu Oct 8 17:21:45 2009 (r1061) +++ trunk/pisasd/sdmain.c Thu Oct 8 17:35:41 2009 (r1062) @@ -196,11 +196,9 @@ /* set the default or command line specified debug level */ sdconf_set_debuglevel(&sd_cfg); - /* fork in case of background running mode */ - if (sd_ctx.is_bgrun) { - if (fork() > 0) - exit(EXIT_SUCCESS); - } + /* daemonize pisasd when running background mode */ + if (sd_ctx.is_bgrun) + pisa_daemonize(); sd_ctx.natlist = pisa_nat_init();