|
Packit |
081926 |
/*
|
|
Packit |
081926 |
* daemon.c : Handle daemon initialization
|
|
Packit |
081926 |
* Copyright (C) 1996 Thomas Koenig
|
|
Packit |
081926 |
*
|
|
Packit |
081926 |
* This program is free software; you can redistribute it and/or modify
|
|
Packit |
081926 |
* it under the terms of the GNU General Public License as published by
|
|
Packit |
081926 |
* the Free Software Foundation; either version 2 of the License, or
|
|
Packit |
081926 |
* (at your option) any later version.
|
|
Packit |
081926 |
*
|
|
Packit |
081926 |
* This program is distributed in the hope that it will be useful,
|
|
Packit |
081926 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit |
081926 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
Packit |
081926 |
* GNU General Public License for more details.
|
|
Packit |
081926 |
*
|
|
Packit |
081926 |
* You should have received a copy of the GNU General Public License
|
|
Packit |
081926 |
* along with this program; if not, write to the Free Software
|
|
Packit |
081926 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
Packit |
081926 |
*/
|
|
Packit |
081926 |
|
|
Packit |
081926 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
081926 |
#include "config.h"
|
|
Packit |
081926 |
#endif
|
|
Packit |
081926 |
|
|
Packit |
081926 |
#include <stdio.h>
|
|
Packit |
081926 |
#include <stdlib.h>
|
|
Packit |
081926 |
|
|
Packit |
081926 |
#ifdef HAVE_STDARG_H
|
|
Packit |
081926 |
#include <stdarg.h>
|
|
Packit |
081926 |
#endif
|
|
Packit |
081926 |
|
|
Packit |
081926 |
#include <sys/types.h>
|
|
Packit |
081926 |
#include <sys/stat.h>
|
|
Packit |
081926 |
#include <signal.h>
|
|
Packit |
081926 |
#include <fcntl.h>
|
|
Packit |
081926 |
|
|
Packit |
081926 |
#ifdef HAVE_UNISTD_H
|
|
Packit |
081926 |
#include <unistd.h>
|
|
Packit |
081926 |
#endif
|
|
Packit |
081926 |
|
|
Packit |
081926 |
#ifdef HAVE_SYSLOG_H
|
|
Packit |
081926 |
#include <syslog.h>
|
|
Packit |
081926 |
#endif
|
|
Packit |
081926 |
|
|
Packit |
081926 |
#ifdef HAVE_ERRNO_H
|
|
Packit |
081926 |
#include <errno.h>
|
|
Packit |
081926 |
#endif
|
|
Packit |
081926 |
|
|
Packit |
081926 |
#include "daemon.h"
|
|
Packit |
081926 |
#include "privs.h"
|
|
Packit |
081926 |
|
|
Packit |
081926 |
int daemon_debug = 0;
|
|
Packit |
081926 |
int daemon_foreground = 0;
|
|
Packit |
081926 |
|
|
Packit |
081926 |
static int
|
|
Packit |
081926 |
lock_fd(int fd)
|
|
Packit |
081926 |
{
|
|
Packit |
081926 |
struct flock lock;
|
|
Packit |
081926 |
|
|
Packit |
081926 |
lock.l_type = F_WRLCK;
|
|
Packit |
081926 |
lock.l_whence = SEEK_SET;
|
|
Packit |
081926 |
lock.l_start = 0;
|
|
Packit |
081926 |
lock.l_len = 0;
|
|
Packit |
081926 |
|
|
Packit |
081926 |
return fcntl(fd, F_SETLK, &lock);
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
|
|
Packit |
081926 |
void
|
|
Packit |
081926 |
perr(const char *fmt,...)
|
|
Packit |
081926 |
{
|
|
Packit |
081926 |
char buf[1024];
|
|
Packit |
081926 |
va_list args;
|
|
Packit |
081926 |
|
|
Packit |
081926 |
va_start(args, fmt);
|
|
Packit |
081926 |
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
Packit |
081926 |
va_end(args);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
if (daemon_debug) {
|
|
Packit |
081926 |
perror(buf);
|
|
Packit |
081926 |
} else
|
|
Packit |
081926 |
syslog(LOG_ERR, "%s: %m", buf);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
exit(EXIT_FAILURE);
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
|
|
Packit |
081926 |
void
|
|
Packit |
081926 |
lerr(const char *fmt,...)
|
|
Packit |
081926 |
{
|
|
Packit |
081926 |
char buf[1024];
|
|
Packit |
081926 |
va_list args;
|
|
Packit |
081926 |
|
|
Packit |
081926 |
va_start(args, fmt);
|
|
Packit |
081926 |
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
Packit |
081926 |
va_end(args);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
if (daemon_debug) {
|
|
Packit |
081926 |
perror(buf);
|
|
Packit |
081926 |
} else
|
|
Packit |
081926 |
syslog(LOG_ERR, "%s: %m", buf);
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
|
|
Packit |
081926 |
void
|
|
Packit |
081926 |
pabort(const char *fmt,...)
|
|
Packit |
081926 |
{
|
|
Packit |
081926 |
char buf[1024];
|
|
Packit |
081926 |
va_list args;
|
|
Packit |
081926 |
|
|
Packit |
081926 |
va_start(args, fmt);
|
|
Packit |
081926 |
vsnprintf(buf, sizeof(buf), fmt, args);
|
|
Packit |
081926 |
va_end(args);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
if (daemon_debug) {
|
|
Packit |
081926 |
fprintf(stderr, "%s\n", buf);
|
|
Packit |
081926 |
} else
|
|
Packit |
081926 |
syslog(LOG_ERR, "%s", buf);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
exit(EXIT_FAILURE);
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
|
|
Packit |
081926 |
void
|
|
Packit |
081926 |
daemon_setup()
|
|
Packit |
081926 |
{
|
|
Packit |
081926 |
/* Set up standard daemon environment */
|
|
Packit |
081926 |
pid_t pid;
|
|
Packit |
081926 |
mode_t old_umask;
|
|
Packit |
081926 |
int fd;
|
|
Packit |
081926 |
FILE *fp;
|
|
Packit |
081926 |
|
|
Packit |
081926 |
if (!daemon_debug) {
|
|
Packit |
081926 |
close(0);
|
|
Packit |
081926 |
close(1);
|
|
Packit |
081926 |
close(2);
|
|
Packit |
081926 |
if ((open("/dev/null", O_RDWR) != 0) ||
|
|
Packit |
081926 |
(open("/dev/null", O_RDWR) != 1) ||
|
|
Packit |
081926 |
(open("/dev/null", O_RDWR) != 2)) {
|
|
Packit |
081926 |
perr("Error redirecting I/O");
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
|
|
Packit |
081926 |
if (daemon_foreground)
|
|
Packit |
081926 |
pid = getpid();
|
|
Packit |
081926 |
else {
|
|
Packit |
081926 |
pid = fork();
|
|
Packit |
081926 |
if (pid == -1) {
|
|
Packit |
081926 |
perr("Cannot fork");
|
|
Packit |
081926 |
} else if (pid != 0) {
|
|
Packit |
081926 |
exit(0);
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
(void) setsid();
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
old_umask = umask(S_IWGRP | S_IWOTH);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
PRIV_START
|
|
Packit |
081926 |
|
|
Packit |
081926 |
fd = open(PIDFILE, O_RDWR | O_CREAT | O_EXCL, S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
if (fd == -1) {
|
|
Packit |
081926 |
|
|
Packit |
081926 |
if (errno != EEXIST)
|
|
Packit |
081926 |
perr("Cannot open " PIDFILE);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
if ((fd = open(PIDFILE, O_RDWR)) < 0)
|
|
Packit |
081926 |
perr("Cannot open " PIDFILE);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
fp = fdopen(fd, "rw");
|
|
Packit |
081926 |
if (fp == NULL) {
|
|
Packit |
081926 |
perr("Cannot open " PIDFILE " for reading");
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
pid = -1;
|
|
Packit |
081926 |
if ((fscanf(fp, "%d", &pid) != 1) || (pid == getpid())
|
|
Packit |
081926 |
|| (lock_fd(fileno(fp)) == 0)) {
|
|
Packit |
081926 |
int rc;
|
|
Packit |
081926 |
|
|
Packit |
081926 |
syslog(LOG_NOTICE, "Removing stale lockfile for pid %d", pid);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
rc = unlink(PIDFILE);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
if (rc == -1) {
|
|
Packit |
081926 |
perr("Cannot unlink " PIDFILE);
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
} else {
|
|
Packit |
081926 |
pabort("Another atd already running with pid %d", pid);
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
fclose(fp);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
unlink(PIDFILE);
|
|
Packit |
081926 |
fd = open(PIDFILE, O_RDWR | O_CREAT | O_EXCL,
|
|
Packit |
081926 |
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
|
|
Packit |
081926 |
if (fd == -1)
|
|
Packit |
081926 |
perr("Cannot open " PIDFILE " the second time round");
|
|
Packit |
081926 |
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
|
|
Packit |
081926 |
if (lock_fd(fd) == -1)
|
|
Packit |
081926 |
perr("Cannot lock " PIDFILE);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
fp = fdopen(fd, "w");
|
|
Packit |
081926 |
if (fp == NULL)
|
|
Packit |
081926 |
perr("Special weirdness: fdopen failed");
|
|
Packit |
081926 |
|
|
Packit |
081926 |
fprintf(fp, "%d\n", getpid());
|
|
Packit |
081926 |
|
|
Packit |
081926 |
/* We do NOT close fd, since we want to keep the lock. However, we don't
|
|
Packit |
081926 |
* want to keep the file descriptor in case of an exec().
|
|
Packit |
081926 |
*/
|
|
Packit |
081926 |
fflush(fp);
|
|
Packit |
081926 |
fcntl(fd, F_SETFD, (long) 1);
|
|
Packit |
081926 |
PRIV_END
|
|
Packit |
081926 |
|
|
Packit |
081926 |
return;
|
|
Packit |
081926 |
}
|
|
Packit |
081926 |
|
|
Packit |
081926 |
void
|
|
Packit |
081926 |
daemon_cleanup()
|
|
Packit |
081926 |
{
|
|
Packit |
081926 |
PRIV_START
|
|
Packit |
081926 |
|
|
Packit |
081926 |
unlink(PIDFILE);
|
|
Packit |
081926 |
|
|
Packit |
081926 |
PRIV_END
|
|
Packit |
081926 |
}
|