if (demon) {
fp = open(pidfile, O_WRONLY|O_CREAT, S_IRUSR|S_IRGRP|S_IROTH);
if (fp >= 0) {
if (fp < 0)
sz1 = snprintf(buf, sizeof buf, "%ld\n",
(long)getpid());
if (sz1 > sizeof buf)
sz1 = sizeof buf;
write(fp, buf, sz1);
close(fp);
} else
err(1, "%s", pidfile);
if (daemon(0, 0) < 0)
err(1, "daemon()");
sz1 = snprintf(buf, sizeof buf, "%ld\n", (long)getpid());
isdemon = 1;
}