/** daemonize, drop user priviliges and chroot if needed */
static void
perform_setup(struct daemon* daemon, struct config_file* cfg, int debug_mode,
- const char** cfgfile)
+ const char** cfgfile, int need_pidfile)
{
#ifdef HAVE_KILL
int pidinchroot;
#ifdef HAVE_KILL
/* true if pidfile is inside chrootdir, or nochroot */
- pidinchroot = !(cfg->chrootdir && cfg->chrootdir[0]) ||
+ pidinchroot = need_pidfile && (!(cfg->chrootdir && cfg->chrootdir[0]) ||
(cfg->chrootdir && cfg->chrootdir[0] &&
strncmp(cfg->pidfile, cfg->chrootdir,
- strlen(cfg->chrootdir))==0);
+ strlen(cfg->chrootdir))==0));
/* check old pid file before forking */
- if(cfg->pidfile && cfg->pidfile[0]) {
+ if(cfg->pidfile && cfg->pidfile[0] && need_pidfile) {
/* calculate position of pidfile */
if(cfg->pidfile[0] == '/')
daemon->pidfile = strdup(cfg->pidfile);
/* write new pidfile (while still root, so can be outside chroot) */
#ifdef HAVE_KILL
- if(cfg->pidfile && cfg->pidfile[0]) {
+ if(cfg->pidfile && cfg->pidfile[0] && need_pidfile) {
writepid(daemon->pidfile, getpid());
if(cfg->username && cfg->username[0] && cfg_uid != (uid_t)-1 &&
pidinchroot) {
* @param log_default_identity: Default identity to report in logs
*/
static void
-run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode, const char* log_default_identity)
+run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode, const char* log_default_identity, int need_pidfile)
{
struct config_file* cfg = NULL;
struct daemon* daemon = NULL;
if(!daemon_open_shared_ports(daemon))
fatal_exit("could not open ports");
if(!done_setup) {
- perform_setup(daemon, cfg, debug_mode, &cfgfile);
+ perform_setup(daemon, cfg, debug_mode, &cfgfile, need_pidfile);
done_setup = 1;
} else {
/* reopen log after HUP to facilitate log rotation */
const char* log_ident_default;
int cmdline_verbose = 0;
int debug_mode = 0;
+ int need_pidfile = 1;
+
#ifdef UB_ON_WINDOWS
int cmdline_cfg = 0;
#endif
log_ident_default = strrchr(argv[0],'/')?strrchr(argv[0],'/')+1:argv[0];
log_ident_set(log_ident_default);
/* parse the options */
- while( (c=getopt(argc, argv, "c:dhvw:")) != -1) {
+ while( (c=getopt(argc, argv, "c:dhpvw:")) != -1) {
switch(c) {
case 'c':
cfgfile = optarg;
cmdline_verbose++;
verbosity++;
break;
+ case 'p':
+ need_pidfile = 0;
+ break;
case 'd':
debug_mode++;
break;
return 1;
}
- run_daemon(cfgfile, cmdline_verbose, debug_mode, log_ident_default);
+ run_daemon(cfgfile, cmdline_verbose, debug_mode, log_ident_default, need_pidfile);
log_init(NULL, 0, NULL); /* close logfile */
return 0;
}