*
*/
+#define _GNU_SOURCE
+
+#include "config.h"
+#include "storage.h"
+#include "oop.h"
+#include "oop-read.h"
+
+#include <sys/uio.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <sys/stat.h>
+#include <sys/socket.h>
+#include <unistd.h>
+#include <string.h>
+#include <signal.h>
+#include <stdio.h>
+#include <errno.h>
+#include <syslog.h>
/*----- general definitions, probably best not changed -----*/
#define INNDCOMMCHILD_ESTATUS_FAIL 6
#define INNDCOMMCHILD_ESTATUS_NONESUCH 7
+typedef struct Conn Conn;
+typedef struct Article Article;
/*----- configuration options -----*/
static char *sitename, *feedfile;
static const char *remote_host;
-static int quiet_multiple=0;
+static int quiet_multiple=0, become_daemon=1;
static int max_connections=10, max_queue_per_conn=200;
, counts[art_Unsolicited] x
typedef enum {
-#define RC_INDEX(x) RCI_##x
+#define RC_INDEX(x) RCI_##x,
RESULT_COUNTS(RC_INDEX, RC_INDEX)
RCI_max
} ResultCountIndex;
#define CONNIOVS 128
typedef enum {
- xk_Malloc, xk_Const, xk_Artdata;
+ xk_Malloc, xk_Const, xk_Artdata
} XmitKind;
typedef struct {
/*----- core operational data structure types -----*/
-struct Article {
- ArtState state;
- int midlen;
- InputFile *ipf;
- TOKEN token;
- off_t offset;
- int blanklen;
- char messageid[1];
-};
-
typedef struct InputFile {
/* This is an instance of struct oop_readable */
struct oop_readable readable; /* first */
char path[];
} InputFile;
+struct Article {
+ ArtState state;
+ int midlen;
+ InputFile *ipf;
+ TOKEN token;
+ off_t offset;
+ int blanklen;
+ char messageid[1];
+};
+
#define SMS_LIST(X) \
X(NORMAL) \
X(FLUSHING) \
/*----- operational variables -----*/
+static oop_source *loop;
+
static int nconns;
static LIST(Conn) idle, working, full;
static LIST(Article) *queue;
static void statemc_setstate(StateMachineState newsms, int periods,
const char *forlog, const char *why);
+/*========== logging ==========*/
+
+static void logcore(int sysloglevel, const char *fmt, ...)
+ __attribute__((__format__(printf,2,3)));
+static void logcore(int sysloglevel, const char *fmt, ...) {
+ va_list al;
+ va_start(al,fmt);
+ if (become_daemon) {
+ vsyslog(sysloglevel,fmt,al);
+ } else {
+ vfprintf(stderr,fmt,al);
+ putc('\n',stderr);
+ }
+ va_end(al);
+}
+
+static void logv(int sysloglevel, const char *pfx, int errnoval,
+ int exitstatus, const char *fmt, va_list al)
+ __attribute__((__format__(printf,5,0)));
+static void logv(int sysloglevel, const char *pfx, int errnoval,
+ int exitstatus, const char *fmt, va_list al) {
+ char msgbuf[256];
+ vsnprintf(msgbuf,sizeof(msgbuf), fmt,al);
+ msgbuf[sizeof(msgbuf)-1]= 0;
+
+ if (sysloglevel >= LOG_ERR && (errnoval==EACCES || errnoval==EPERM))
+ sysloglevel= LOG_ERR; /* run by wrong user, probably */
+
+ logcore(sysloglevel, "<%s>%s: %s%s%s",
+ sitename, pfx, msgbuf,
+ errnoval>=0 ? ": " : "",
+ errnoval>=0 ? strerror(errnoval) : "");
+}
+
+#define logwrap(fn, pfx, sysloglevel, err, estatus) \
+ static void fn(const char *fmt, ...) \
+ __attribute__((__format__(printf,1,2))); \
+ static void fn(const char *fmt, ...) { \
+ va_list al; \
+ va_start(al,fmt); \
+ logv(sysloglevel, pfx, err, estatus, fmt, al); \
+ }
+
+logwrap(sysdie, " critical", LOG_CRIT, errno, 16);
+logwrap(die, " critical", LOG_CRIT, -1, 16);
+
+logwrap(sysfatal, " fatal", LOG_ERR, errno, 12);
+logwrap(fatal, " fatal", LOG_ERR, -1, 12);
+
+logwrap(syswarn, " warning", LOG_WARNING, errno, 0);
+logwrap(warn, " warning", LOG_WARNING, -1, 0);
+
+logwrap(notice, "", LOG_NOTICE, -1, 0);
+logwrap(info, " info", LOG_INFO, -1, 0);
+logwrap(debug, " debug", LOG_DEBUG, -1, 0);
+
+
/*========== utility functions etc. ==========*/
static void perhaps_close(int *fd) { if (*fd) { close(*fd); fd=0; } }
+static void *xmalloc(size_t sz) {
+ if (!sz) return 0;
+ void *r= malloc(sz);
+ if (r) return r;
+ sysdie("malloc (%ld bytes) failed", (unsigned long)sz);
+}
+
static pid_t xfork(const char *what) {
pid_t child;
static int xwaitpid(pid_t *pid, const char *what) {
int status;
- r= kill(*pid, SIGKILL);
+ int r= kill(*pid, SIGKILL);
if (r) sysdie("cannot kill %s child", what);
pid_t got= waitpid(*pid, &status, WNOHANG);
}
static void xfstat(int fd, struct stat *stab_r, const char *what) {
- int r= fstab(path, stab);
- if (r) sysdie("could not fstat %s %s", what, path);
+ int r= fstab(fd, stab_r);
+ if (r) sysdie("could not fstat %s", what);
}
-static void xfstat_isreg(int fd, struct stat *stab_r, const char *what) {
+static void xfstat_isreg(int fd, struct stat *stab_r,
+ const char *path, const char *what) {
xfstat(fd, stab_r, what);
- check_isreg(stab, path, what);
+ check_isreg(stab_r, path, what);
}
static void xlstat_isreg(const char *path, struct stat *stab,
- int *enoent_r /* 0 means ENOENT is fatal */
+ int *enoent_r /* 0 means ENOENT is fatal */,
const char *what) {
int r= lstat(path, stab);
if (r) {
a->st_dev == b->st_dev);
}
-/*========== logging ==========*/
-
-static void logcore(int sysloglevel, const char *fmt, ...)
- __attribute__((printf,2,3))
-static void logcore(int sysloglevel, const char *fmt, ...) {
- va_list al;
- va_start(al,fmt);
- if (become_daemon) {
- vsyslog(sysloglevel,fmt,al);
- } else {
- vfprintf(stderr,fmt,al);
- putc('\n',stderr);
- }
- va_end(al);
-}
-
-static void logv(int sysloglevel, const char *pfx, int errnoval,
- int exitstatus, const char *fmt, va_list al)
- __attribute__((printf,4,0))
-static void logv(int sysloglevel, const char *pfx, int errnoval,
- int exitstatus, const char *fmt, va_list al) {
- char msgbuf[256];
- vsnprintf(msgbuf,sizeof(msgbuf), fmt,al);
- msgbuf[sizeof(msgbuf)-1]= 0;
-
- if (sysloglevel >= LOG_ERR && (errnoval==EACCES || errnoval==EPERM))
- sysloglevel= LOG_ERR; /* run by wrong user, probably */
-
- logcore(sysloglevel, "<%s>%s: %s%s%s",
- sitename, pfx, msgbuf,
- errnoval>=0 ? ": " : "",
- errnoval>=0 ? strerror(errnoval) : "");
-}
-
-#define logwrap(fn, pfx, sysloglevel, errno, estatus) \
- static void fn(const char *fmt, ...) \
- __attribute__((printf,1,2)); \
- static void fn(const char *fmt, ...) { \
- va_list al; \
- va_start(al,fmt); \
- logv(sysloglevel, pfx, errno, estatus, fmt, al); \
- }
-
-logwrap(sysdie, " critical", LOG_CRIT, errno, 16);
-logwrap(die, " critical", LOG_CRIT, -1, 16);
-
-logwrap(sysfatal, " fatal", LOG_ERR, errno, 12);
-logwrap(fatal, " fatal", LOG_ERR, -1, 12);
-
-logwrap(syswarn, " warning", LOG_WARN, errno, 0);
-logwrap(warn, " warning", LOG_WARN, -1, 0);
-
-logwrap(notice, "", LOG_NOTICE, -1, 0);
-logwrap(info, " info", LOG_INFO, -1, 0);
-logwrap(debug, " debug", LOG_DEBUG, -1, 0);
-
-
/*========== making new connections ==========*/
static int connecting_sockets[2]= {-1,-1};
perhaps_close(&connecting_sockets[1]);
if (connecting_child) {
- r= kill(connecting_child, SIGTERM);
+ int r= kill(connecting_child, SIGTERM);
if (r) syswarn("failed to kill connecting child");
int status= xwaitpid(&connecting_child, "connect");
conn= xmalloc(sizeof(*conn));
memset(conn,0,sizeof(*conn));
- DECL_MSG_CMSG(msg);
+ PREP_DECL_MSG_CMSG(msg);
struct cmsghdr *h= 0;
ssize_t rs= recvmsg(fd, &msg, MSG_DONTWAIT);
if (rs >= 0) h= CMSG_FIRSTHDR(&msg);
if (strchr(feedfile, c))
badusage("feed filename may not contain metacharacter %c",c);
+ loop= oop_sys_new();
+ if (!loop) sysdie("could not create liboop event loop");
+
if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
sysdie("could not ignore SIGPIPE");