/** 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;
, 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);
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;
}
/**
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
/* 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;