va_end(al); \
}
-diewrap(sysdie, " critical", LOG_CRIT, errno, 16);
-diewrap(die, " critical", LOG_CRIT, -1, 16);
+#define INNLOGWRAP_DECLARE(fn, pfx, sysloglevel) \
+ static void duct_log_##fn(int errval, const char *fmt, va_list al, int l) { \
+ logv(sysloglevel, pfx, errnoval ? errnoval : -1, fmt, al); \
+ }
+#define INNLOGWRAP_CALL(fn, pfx, sysloglevel) \
+ fn##_set_handlers(1, duct_log_##fn);
+
+
+static int innduct_fatal_cleanup(void) { return 12; } /* used for libinn die */
+
+/* We want to extend the set of logging functions from inn, and we
+ * want to prepend the site name to all our messages. */
-diewrap(sysfatal, " fatal", LOG_ERR, errno, 12);
-diewrap(fatal, " fatal", LOG_ERR, -1, 12);
+diewrap(syscrash, " critical", LOG_CRIT, errno, 16);
+diewrap(crash, " critical", LOG_CRIT, -1, 16);
-logwrap(syswarn, " warning", LOG_WARNING, errno);
-logwrap(warn, " warning", LOG_WARNING, -1);
+diewrap(sysfatal, " fatal", LOG_ERR, errno, 12);
+diewrap(fatal, " fatal", LOG_ERR, -1, 12);
-logwrap(notice, " notice", LOG_NOTICE, -1);
-logwrap(info, " info", LOG_INFO, -1);
-logwrap(debug, " debug", LOG_DEBUG, -1);
+#define INNLOGWRAPS \
+ INNLOGWRAP(warn, " warning", LOG_WARNING, errno) \
+ INNLOGWRAP(notice, " notice", LOG_NOTICE, -1)
+INNLOGWRAPS(INNLOGWRAP_DECLARE)
+
+logwrap(info, " info", LOG_INFO, -1);
+logwrap(debug, " debug", LOG_DEBUG, -1);
/*========== utility functions etc. ==========*/
+/* error trapping wrappers are called mfoo rather than the more
+ * conventional xfoo because we don't want to clash with the existing
+ * xfoo functions in INN libs which use different error handlers
+ */
+
static char *mvasprintf(const char *fmt, va_list al) PRINTF(1,0);
static char *mvasprintf(const char *fmt, va_list al) {
char *str;
int rc= vasprintf(&str,fmt,al);
- if (rc<0) sysdie("vasprintf(\"%s\",...) failed", fmt);
+ if (rc<0) sysfatal("vasprintf(\"%s\",...) failed", fmt);
return str;
}
static char *masprintf(const char *fmt, ...) PRINTF(1,2);
}
static void mclose(int fd, const char *what, const char *what2) {
int r= close(fd);
- if (r) sysdie("close %s%s",what,what2?what2:"");
+ if (r) syscrash("close %s%s",what,what2?what2:"");
}
static void mclose_perhaps(int *fd, const char *what, const char *what2) {
if (*fd <= 0) return;
int status;
int r= kill(*pid, SIGKILL);
- if (r) sysdie("cannot kill %s child", what);
+ if (r) syscrash("cannot kill %s child", what);
pid_t got= waitpid(*pid, &status, 0);
- if (got==-1) sysdie("cannot reap %s child", what);
- if (got==0) die("cannot reap %s child", what);
+ if (got==-1) syscrash("cannot reap %s child", what);
+ if (got==0) crash("cannot reap %s child", what);
*pid= 0;
static void *mmalloc(size_t sz) {
void *p= malloc(sz);
- if (!p) sysdie("unable to malloc %lu bytes",(unsigned long)sz);
+ if (!p) sysfatal("unable to malloc %lu bytes",(unsigned long)sz);
return p;
}
static void munlink(const char *path, const char *what) {
int r= unlink(path);
- if (r) sysdie("can't unlink %s %s", path, what);
+ if (r) syscrash("can't unlink %s %s", path, what);
}
static time_t mtime(void) {
time_t now= time(0);
- if (now==-1) sysdie("time(2) failed");
+ if (now==-1) syscrash("time(2) failed");
return now;
}
static void msigaction(int signo, const struct sigaction *sa) {
int r= sigaction(signo,sa,0);
- if (r) sysdie("sigaction failed for \"%s\"", strsignal(signo));
+ if (r) syscrash("sigaction failed for \"%s\"", strsignal(signo));
}
static void msigsetdefault(int signo) {
static void mgettimeofday(struct timeval *tv_r) {
int r= gettimeofday(tv_r,0);
- if (r) sysdie("gettimeofday(2) failed");
+ if (r) syscrash("gettimeofday(2) failed");
}
static void msetnonblock(int fd, int nonblocking) {
int errnoval= oop_fd_nonblock(fd, nonblocking);
- if (errnoval) { errno= errnoval; sysdie("setnonblocking"); }
+ if (errnoval) { errno= errnoval; syscrash("setnonblocking"); }
}
static void check_isreg(const struct stat *stab, const char *path,
const char *what) {
if (!S_ISREG(stab->st_mode))
- die("%s %s not a plain file (mode 0%lo)",
- what, path, (unsigned long)stab->st_mode);
+ crash("%s %s not a plain file (mode 0%lo)",
+ what, path, (unsigned long)stab->st_mode);
}
static void mfstat(int fd, struct stat *stab_r, const char *what) {
int r= fstat(fd, stab_r);
- if (r) sysdie("could not fstat %s", what);
+ if (r) syscrash("could not fstat %s", what);
}
static void mfstat_isreg(int fd, struct stat *stab_r,
int r= lstat(path, stab);
if (r) {
if (errno==ENOENT && enoent_r) { *enoent_r=1; return; }
- sysdie("could not lstat %s %s", what, path);
+ syscrash("could not lstat %s %s", what, path);
}
if (enoent_r) *enoent_r= 0;
check_isreg(stab, path, what);
goto x;
}
-#define CHK(field, val) \
- if (h->cmsg_##field != val) { \
- die("connect: child sent cmsg with cmsg_" #field "=%d, expected %d", \
- h->cmsg_##field, val); \
- goto x; \
+#define CHK(field, val) \
+ if (h->cmsg_##field != val) { \
+ crash("connect: child sent cmsg with cmsg_" #field "=%d, expected %d", \
+ h->cmsg_##field, val); \
+ goto x; \
}
CHK(level, SOL_SOCKET);
CHK(type, SCM_RIGHTS);
CHK(len, CMSG_LEN(sizeof(conn->fd)));
#undef CHK
- if (CMSG_NXTHDR(&msg,h)) die("connect: child sent many cmsgs");
+ if (CMSG_NXTHDR(&msg,h)) crash("connect: child sent many cmsgs");
memcpy(&conn->fd, CMSG_DATA(h), sizeof(conn->fd));
int status;
pid_t got= waitpid(connecting_child, &status, 0);
- if (got==-1) sysdie("connect: real wait for child");
+ if (got==-1) syscrash("connect: real wait for child");
assert(got == connecting_child);
connecting_child= 0;
loop->on_fd(loop, conn->fd, OOP_EXCEPTION, conn_exception, conn);
conn->rd= oop_rd_new_fd(loop,conn->fd, 0, 0); /* sets nonblocking, too */
- if (!conn->fd) die("oop_rd_new_fd conn failed (fd=%d)",conn->fd);
+ if (!conn->fd) crash("oop_rd_new_fd conn failed (fd=%d)",conn->fd);
int r= oop_rd_read(conn->rd, &peer_rd_style, NNTP_STRLEN,
&peer_rd_ok, conn,
&peer_rd_err, conn);
- if (r) sysdie("oop_rd_read for peer (fd=%d)",conn->fd);
+ if (r) syscrash("oop_rd_read for peer (fd=%d)",conn->fd);
LIST_ADDHEAD(conns, conn);
notice("C%d (now %d) connected %s",
msg.msg_controllen= cmsg->cmsg_len;
r= sendmsg(socks[1], &msg, 0);
- if (r<0) sysdie("sendmsg failed for new connection");
- if (r!=1) die("sendmsg for new connection gave wrong result %d",r);
+ if (r<0) syscrash("sendmsg failed for new connection");
+ if (r!=1) crash("sendmsg for new connection gave wrong result %d",r);
_exit(exitstatus);
}
int r= pwrite(ipf->fd, spaces, w, art->offset);
if (r==-1) {
if (errno==EINTR) continue;
- sysdie("failed to blank entry for %s (length %d at offset %lu) in %s",
- art->messageid, art->blanklen,
- (unsigned long)art->offset, ipf->path);
+ syscrash("failed to blank entry for %s (length %d at offset %lu) in %s",
+ art->messageid, art->blanklen,
+ (unsigned long)art->offset, ipf->path);
}
assert(r>=0 && r<=w);
art->blanklen -= w;
ipf->readcount_err++;
if (ipf->readcount_err > max_bad_data_initial +
(ipf->readcount_ok+ipf->readcount_blank) / max_bad_data_ratio)
- die("too much garbage in input file! (%d errs, %d ok, %d blank)",
- ipf->readcount_err, ipf->readcount_ok, ipf->readcount_blank);
+ crash("too much garbage in input file! (%d errs, %d ok, %d blank)",
+ ipf->readcount_err, ipf->readcount_ok, ipf->readcount_blank);
return OOP_CONTINUE;
}
InputFile *ipf= ipf_v;
assert(ev == OOP_RD_SYSTEM);
errno= errnoval;
- sysdie("error reading input file: %s, offset %lu",
- ipf->path, (unsigned long)ipf->offset);
+ syscrash("error reading input file: %s, offset %lu",
+ ipf->path, (unsigned long)ipf->offset);
}
static void *feedfile_got_article(oop_source *lp, oop_read *rd,
int wd= pf->wd;
debug("filemon inotify stopfile %p wd=%d", ipf, wd);
int r= inotify_rm_watch(filemon_inotify_fd, wd);
- if (r) sysdie("inotify_rm_watch");
+ if (r) syscrash("inotify_rm_watch");
filemon_inotify_wd2ipf[wd]= 0;
}
int r= read(filemon_inotify_fd, &iev, sizeof(iev));
if (r==-1) {
if (isewouldblock(errno)) break;
- sysdie("read from inotify master");
+ syscrash("read from inotify master");
} else if (r==sizeof(iev)) {
assert(iev.wd >= 0 && iev.wd < filemon_inotify_wdmax);
} else {
- die("inotify read %d bytes wanted struct of %d", r, (int)sizeof(iev));
+ crash("inotify read %d bytes wanted struct of %d", r, (int)sizeof(iev));
}
InputFile *ipf= filemon_inotify_wd2ipf[iev.wd];
/*debug("filemon inotify readable read %p wd=%d", ipf, iev.wd);*/
int r= oop_rd_read(ipf->rd, &feedfile_rdstyle, MAX_LINE_FEEDFILE,
feedfile_got_article,ipf, feedfile_read_err, ipf);
- if (r) sysdie("unable start reading feedfile %s",ipf->path);
+ if (r) syscrash("unable start reading feedfile %s",ipf->path);
ipf->paused= 0;
}
}
FILE *lockfile= fdopen(lockfd, "w");
- if (!lockfile) sysdie("fdopen lockfile");
+ if (!lockfile) syscrash("fdopen lockfile");
int r= ftruncate(lockfd, 0);
- if (r) sysdie("truncate lockfile to write new info");
+ if (r) syscrash("truncate lockfile to write new info");
if (fprintf(lockfile, "pid %ld\nsite %s\nfeedfile %s\nfqdn %s\n",
(unsigned long)self_pid,
" (presumably hardlink to backlog file)");
break;
default:
- die("defer file %s has unexpected link count %d",
- path_defer, stabdefer.st_nlink);
+ crash("defer file %s has unexpected link count %d",
+ path_defer, stabdefer.st_nlink);
}
}
startup_set_input_file(file_d);
flushing_input_file= main_input_file;
main_input_file= open_input_file(feedfile);
- if (!main_input_file) die("feedfile vanished during startup");
+ if (!main_input_file) crash("feedfile vanished during startup");
SMS(SEPARATED, max_separated_periods,
"found both old and current feed files");
} else {
debug("startup: F exists, D ENOENT => Normal");
InputFile *file_f= open_input_file(feedfile);
- if (!file_f) die("feed file vanished during startup");
+ if (!file_f) crash("feed file vanished during startup");
startup_set_input_file(file_f);
SMS(NORMAL, spontaneous_flush_periods, "normal startup");
}
close_input_file(ipf);
if (unlink(ipf->path)) {
if (errno != ENOENT)
- sysdie("could not unlink processed backlog file %s", ipf->path);
+ syscrash("could not unlink processed backlog file %s", ipf->path);
warn("backlog file %s vanished while we were reading it"
" so we couldn't remove it (but it's done now, anyway)",
ipf->path);
mfstat_isreg(fileno(defer), &stab, path_defer, "newly opened defer file");
if (stab.st_size > LONG_MAX)
- die("defer file %s size is far too large", path_defer);
+ crash("defer file %s size is far too large", path_defer);
if (!stab.st_size)
return;
for (;;) {
if (!truncto) break; /* was only (if anything) one half-truncated record */
if (fseek(defer, truncto-1, SEEK_SET) < 0)
- sysdie("seek in defer file %s while truncating partial", path_defer);
+ syscrash("seek in defer file %s while truncating partial", path_defer);
int r= getc(defer);
if (r==EOF) {
if (ferror(defer))
- sysdie("failed read from defer file %s", path_defer);
+ syscrash("failed read from defer file %s", path_defer);
else
- die("defer file %s shrank while we were checking it!", path_defer);
+ crash("defer file %s shrank while we were checking it!", path_defer);
}
if (r=='\n') break;
truncto--;
if (fflush(defer))
sysfatal("could not flush defer file %s", path_defer);
if (ftruncate(fileno(defer), truncto))
- sysdie("could not truncate defer file %s", path_defer);
+ syscrash("could not truncate defer file %s", path_defer);
} else {
info("continuing existing defer file %s (%ld bytes)",
path_defer, orgsize);
}
if (fseek(defer, truncto, SEEK_SET))
- sysdie("could not seek to new end of defer file %s", path_defer);
+ syscrash("could not seek to new end of defer file %s", path_defer);
}
static void close_defer(void) {
(unsigned long)stab.st_ino);
if (link(path_defer, backlog))
sysfatal("could not install defer file %s as backlog file %s",
- path_defer, backlog);
+ path_defer, backlog);
if (unlink(path_defer))
- sysdie("could not unlink old defer link %s to backlog file %s",
- path_defer, backlog);
+ syscrash("could not unlink old defer link %s to backlog file %s",
+ path_defer, backlog);
free(backlog);
case GLOB_NOMATCH: /* fall through */
break;
default:
- sysdie("glob expansion of backlog pattern %s gave unexpected"
- " nonzero (error?) return value %d", globpat_backlog, r);
+ syscrash("glob expansion of backlog pattern %s gave unexpected"
+ " nonzero (error?) return value %d", globpat_backlog, r);
}
if (!oldest_path) {
assert(fd=signal_self_pipe[0]);
char buf[PIPE_BUF];
int r= read(signal_self_pipe[0], buf, sizeof(buf));
- if (r<0 && !isewouldblock(errno)) sysdie("failed to read signal self pipe");
- if (r==0) die("eof on signal self pipe");
+ if (r<0 && !isewouldblock(errno))
+ syscrash("failed to read signal self pipe");
+ if (r==0) crash("eof on signal self pipe");
if (terminate_sig_flag) {
preterminate();
notice("terminating (%s)", strsignal(terminate_sig_flag));
static void init_signals(void) {
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
- sysdie("could not ignore SIGPIPE");
+ syscrash("could not ignore SIGPIPE");
if (pipe(signal_self_pipe)) sysfatal("create self-pipe for signals");
main_input_file= open_input_file(feedfile);
if (!main_input_file)
- die("flush succeeded but feedfile %s does not exist!"
- " (this probably means feedfile does not correspond"
- " to site %s in newsfeeds)", feedfile, sitename);
+ crash("flush succeeded but feedfile %s does not exist!"
+ " (this probably means feedfile does not correspond"
+ " to site %s in newsfeeds)", feedfile, sitename);
if (flushing_input_file) {
SMS(SEPARATED, max_separated_periods, "flush complete");
static void postfork_stdio(FILE *f, const char *what, const char *what2) {
/* we have no stdio streams that are buffered long-term */
if (!f) return;
- if (fclose(f)) sysdie("(in child) close %s%s", what, what2?what2:0);
+ if (fclose(f)) syscrash("(in child) close %s%s", what, what2?what2:0);
}
static void postfork(void) {
"say --help for help, or read the manpage\n",
m);
if (interactive < 2)
- syslog(LOG_CRIT,"innduct: invoked with bad usage: %s",m);
+ syslog(LOG_CRASH,"innduct: invoked with bad usage: %s",m);
exit(8);
}
}
int main(int argc, char **argv) {
+ /* set up libinn logging */
+ error_program_name= "innduct";
+ message_fatal_cleanup= innduct_fatal_cleanup;
+ INNLOGWRAPS(INNLOGWRAP_CALL)
+
if (!argv[1]) {
printusage(stderr);
exit(8);
/* defaults */
int r= innconf_read(inndconffile);
- if (!r) badusage("could not read inn.conf (more info on stderr)");
+ if (!r) badusage("could not read inn.conf");
if (!remote_host) remote_host= sitename;
globpat_backlog= masprintf("%s_backlog*", feedfile);
oop_source_sys *sysloop= oop_sys_new();
- if (!sysloop) sysdie("could not create liboop event loop");
+ if (!sysloop) syscrash("could not create liboop event loop");
loop= (oop_source*)sysloop;
LIST_INIT(conns);
}
self_pid= getpid();
- if (self_pid==-1) sysdie("getpid");
+ if (self_pid==-1) syscrash("getpid");
r= chdir(path_run);
- if (r) sysdie("could not chdir to pathrun %s", path_run);
+ if (r) sysfatal("could not chdir to pathrun %s", path_run);
statemc_lock();
int val= 1;
r= SMsetup(SM_PREOPEN, &val); if (!r) warn("SMsetup SM_PREOPEN failed");
- r= SMinit(); if (!r) die("storage manager initialisation (SMinit) failed");
+ r= SMinit(); if (!r) fatal("storage manager initialisation (SMinit) failed");
if (interactive >= 2)
cli_stdio();
void *run= oop_sys_run(sysloop);
assert(run == OOP_ERROR);
- sysdie("event loop failed");
+ syscrash("event loop failed");
}