diff options
Diffstat (limited to 'contrib/unbound/daemon/unbound.c')
-rw-r--r-- | contrib/unbound/daemon/unbound.c | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/contrib/unbound/daemon/unbound.c b/contrib/unbound/daemon/unbound.c index 457a08032857..d6c371571d75 100644 --- a/contrib/unbound/daemon/unbound.c +++ b/contrib/unbound/daemon/unbound.c @@ -366,9 +366,8 @@ readpid (const char* file) /** write pid to file. * @param pidfile: file name of pid file. * @param pid: pid to write to file. - * @return false on failure */ -static int +static void writepid (const char* pidfile, pid_t pid) { int fd; @@ -383,7 +382,7 @@ writepid (const char* pidfile, pid_t pid) , 0644)) == -1) { log_err("cannot open pidfile %s: %s", pidfile, strerror(errno)); - return 0; + return; } while(count < strlen(pidbuf)) { ssize_t r = write(fd, pidbuf+count, strlen(pidbuf)-count); @@ -393,17 +392,16 @@ writepid (const char* pidfile, pid_t pid) log_err("cannot write to pidfile %s: %s", pidfile, strerror(errno)); close(fd); - return 0; + return; } else if(r == 0) { log_err("cannot write any bytes to pidfile %s: " "write returns 0 bytes written", pidfile); close(fd); - return 0; + return; } count += r; } close(fd); - return 1; } /** @@ -545,7 +543,15 @@ perform_setup(struct daemon* daemon, struct config_file* cfg, int debug_mode, cfg, 1); if(!daemon->pidfile) fatal_exit("pidfile alloc: out of memory"); - checkoldpid(daemon->pidfile, pidinchroot); + /* Check old pid if there is no username configured. + * With a username, the assumption is that the privilege + * drop makes a pidfile not removed when the server stopped + * last time. The server does not chown the pidfile for it, + * because that creates privilege escape problems, with the + * pidfile writable by unprivileged users, but used by + * privileged users. */ + if(cfg->username && cfg->username[0]) + checkoldpid(daemon->pidfile, pidinchroot); } #endif @@ -557,18 +563,7 @@ perform_setup(struct daemon* daemon, struct config_file* cfg, int debug_mode, /* write new pidfile (while still root, so can be outside chroot) */ #ifdef HAVE_KILL if(cfg->pidfile && cfg->pidfile[0] && need_pidfile) { - if(writepid(daemon->pidfile, getpid())) { - if(cfg->username && cfg->username[0] && cfg_uid != (uid_t)-1 && - pidinchroot) { -# ifdef HAVE_CHOWN - if(chown(daemon->pidfile, cfg_uid, cfg_gid) == -1) { - verbose(VERB_QUERY, "cannot chown %u.%u %s: %s", - (unsigned)cfg_uid, (unsigned)cfg_gid, - daemon->pidfile, strerror(errno)); - } -# endif /* HAVE_CHOWN */ - } - } + writepid(daemon->pidfile, getpid()); } #else (void)daemon; @@ -746,7 +741,11 @@ run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode, int need_pi if(daemon->pidfile) { int fd; /* truncate pidfile */ - fd = open(daemon->pidfile, O_WRONLY | O_TRUNC, 0644); + fd = open(daemon->pidfile, O_WRONLY | O_TRUNC +#ifdef O_NOFOLLOW + | O_NOFOLLOW +#endif + , 0644); if(fd != -1) close(fd); /* delete pidfile */ |