Add init script for ceod

This commit is contained in:
Michael Spang 2009-09-08 17:45:09 -04:00
parent c931a6bedb
commit 80ac98531f
5 changed files with 96 additions and 7 deletions

55
debian/ceo-daemon.ceod.init vendored Executable file
View File

@ -0,0 +1,55 @@
#! /bin/sh
### BEGIN INIT INFO
# Provides: ceod
# Required-Start: $remote_fs $syslog $network
# Required-Stop: $remote_fs $syslog $network
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: CEO Daemon
### END INIT INFO
set -e
test -x /usr/sbin/ceod || exit 0
. /lib/lsb/init-functions
case "$1" in
start)
log_daemon_msg "Starting CEO Daemon" "ceod"
if start-stop-daemon --start --quiet --oknodo --pidfile /var/run/ceod.pid --exec /usr/sbin/ceod -- -dq; then
log_end_msg 0
else
log_end_msg 1
fi
;;
stop)
log_daemon_msg "Stopping CEO Daemon" "ceod"
if start-stop-daemon --stop --quiet --oknodo --pidfile /var/run/ceod.pid; then
log_end_msg 0
else
log_end_msg 1
fi
;;
restart|force-reload)
log_daemon_msg "Restarting CEO Daemon" "ceod"
start-stop-daemon --stop --quiet --oknodo --retry 30 --pidfile /var/run/ceod.pid
if start-stop-daemon --start --quiet --oknodo --pidfile /var/run/ceod.pid --exec /usr/sbin/ceod -- -dq; then
log_end_msg 0
else
log_end_msg 1
fi
;;
status)
status_of_proc -p /var/run/ceod.pid /usr/sbin/ceod ceod && exit 0 || exit $?
;;
*)
log_action_msg "Usage: /etc/init.d/ceod {start|stop|force-reload|restart|status}"
exit 1
esac
exit 0

1
debian/rules vendored
View File

@ -28,6 +28,7 @@ binary-arch: build install
dh_installchangelogs
dh_installdocs
dh_installexamples
dh_installinit --name ceod -- start 95 2 3 4 5 . stop 05 0 1 6 .
dh_install
dh_installman
dh_link

View File

@ -9,6 +9,7 @@
#include <errno.h>
#include <netdb.h>
#include <alloca.h>
#include <fcntl.h>
#include "util.h"
#include "net.h"
@ -22,6 +23,7 @@
static struct option opts[] = {
{ "detach", 0, NULL, 'd' },
{ "quiet", 0, NULL, 'q' },
{ NULL, 0, NULL, '\0' },
};
@ -67,6 +69,25 @@ static void setup_signals(void) {
signal(SIGCHLD, SIG_IGN);
}
static void setup_pidfile(void) {
int fd;
size_t pidlen;
char pidbuf[1024];
const char *pidfile = "/var/run/ceod.pid";
fd = open(pidfile, O_CREAT|O_RDWR, 0644);
if (fd < 0)
fatalpe("open: %s", pidfile);
if (lockf(fd, F_TLOCK, 0))
fatalpe("lockf: %s", pidfile);
if (ftruncate(fd, 0))
fatalpe("ftruncate: %s", pidfile);
pidlen = snprintf(pidbuf, sizeof(pidbuf), "%d\n", getpid());
if (pidlen >= sizeof(pidbuf))
fatal("pid too long");
full_write(fd, pidbuf, pidlen);
}
static void setup_daemon(void) {
if (detach) {
if (chdir("/"))
@ -78,6 +99,9 @@ static void setup_daemon(void) {
exit(0);
if (setsid() < 0)
fatalpe("setsid");
setup_pidfile();
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
@ -131,11 +155,11 @@ static int master_main(void) {
if (listen(sock, 128))
fatalpe("listen");
setup_daemon();
setup_fqdn();
setup_signals();
setup_auth();
setup_ops();
setup_daemon();
notice("now accepting connections");
@ -156,13 +180,14 @@ int main(int argc, char *argv[]) {
prog = xstrdup(basename(argv[0]));
init_log(prog, LOG_PID, LOG_DAEMON, 0);
configure();
while ((opt = getopt_long(argc, argv, "", opts, NULL)) != -1) {
while ((opt = getopt_long(argc, argv, "dq", opts, NULL)) != -1) {
switch (opt) {
case 'd':
detach = 1;
break;
case 'q':
log_set_maxprio(LOG_WARNING);
break;
case '?':
usage();
break;
@ -171,6 +196,8 @@ int main(int argc, char *argv[]) {
}
}
configure();
if (argc != optind)
usage();

View File

@ -13,12 +13,17 @@
#include "strbuf.h"
static int log_stderr = 1;
static int log_maxprio = LOG_DEBUG;
void init_log(const char *ident, int option, int facility, int lstderr) {
openlog(ident, option, facility);
log_stderr = lstderr || isatty(STDERR_FILENO);
}
void log_set_maxprio(int prio) {
log_maxprio = prio;
}
static void errmsg(int prio, const char *prefix, const char *fmt, va_list args) {
struct strbuf msg = STRBUF_INIT;
@ -27,7 +32,7 @@ static void errmsg(int prio, const char *prefix, const char *fmt, va_list args)
strbuf_addch(&msg, '\n');
syslog(prio, "%s", msg.buf);
if (log_stderr)
if (log_stderr && prio <= log_maxprio)
fputs(msg.buf, stderr);
strbuf_release(&msg);
@ -41,7 +46,7 @@ static void errmsgpe(int prio, const char *prefix, const char *fmt, va_list args
strbuf_addf(&msg, ": %s\n", strerror(errno));
syslog(prio, "%s", msg.buf);
if (log_stderr)
if (log_stderr && prio <= log_maxprio)
fputs(msg.buf, stderr);
strbuf_release(&msg);
@ -98,7 +103,7 @@ void logmsg(int priority, const char *msg, ...) {
vsyslog(priority, msg, args);
va_end(args);
va_start(args, msg);
if (log_stderr) {
if (log_stderr && priority <= log_maxprio) {
vfprintf(stderr, msg, args);
fputc('\n', stderr);
}

View File

@ -35,6 +35,7 @@ void make_env(char **envp, ...);
void free_env(char **envp);
void init_log(const char *ident, int option, int facility, int lstderr);
int check_group(char *username, char *group);
void log_set_maxprio(int prio);
PRINTF_LIKE(0) NORETURN void fatal(const char *, ...);
PRINTF_LIKE(0) NORETURN void fatalpe(const char *, ...);