summaryrefslogtreecommitdiffstats
path: root/plugins/imuxsock
diff options
context:
space:
mode:
authorRainer Gerhards <rgerhards@adiscon.com>2007-12-20 10:39:58 +0000
committerRainer Gerhards <rgerhards@adiscon.com>2007-12-20 10:39:58 +0000
commit2583be41070979f4ffdf605b7442edc704e00660 (patch)
treef6b190288e6df7693f1fe5dcfe0dde39e0e2c45d /plugins/imuxsock
parentab94b8a7fdad8b00a957ec28ef0ab904184fd8fb (diff)
downloadrsyslog-2583be41070979f4ffdf605b7442edc704e00660.tar.gz
rsyslog-2583be41070979f4ffdf605b7442edc704e00660.tar.xz
rsyslog-2583be41070979f4ffdf605b7442edc704e00660.zip
moved unix socket code to its own module (imuxsock)
Diffstat (limited to 'plugins/imuxsock')
-rw-r--r--plugins/imuxsock/imuxsock.c144
1 files changed, 130 insertions, 14 deletions
diff --git a/plugins/imuxsock/imuxsock.c b/plugins/imuxsock/imuxsock.c
index cde1a8bc..924630fc 100644
--- a/plugins/imuxsock/imuxsock.c
+++ b/plugins/imuxsock/imuxsock.c
@@ -30,9 +30,11 @@
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
-#include <signal.h>
#include <string.h>
-#include <pthread.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/un.h>
#include "syslogd.h"
#include "cfsysline.h"
#include "module-template.h"
@@ -41,26 +43,91 @@ MODULE_TYPE_INPUT
TERM_SYNC_TYPE(eTermSync_SIGNAL)
/* defines */
+#define MAXFUNIX 20
+/* handle some defines missing on more than one platform */
+#ifndef SUN_LEN
+#define SUN_LEN(su) \
+ (sizeof(*(su)) - sizeof((su)->sun_path) + strlen((su)->sun_path))
+#endif
/* Module static data */
DEF_IMOD_STATIC_DATA
typedef struct _instanceData {
} instanceData;
-/* This function is called to gather input. It must terminate only
- * a) on failure (iRet set accordingly)
- * b) on termination of the input module (as part of the unload process)
- * Code begun 2007-12-12 rgerhards
- *
- * This code must simply spawn emit a mark message at each mark interval.
- * We are running on our own thread, so this is extremely easy: we just
- * sleep MarkInterval seconds and each time we awake, we inject the message.
- * Please note that we do not do the other fancy things that sysklogd
- * (and pre 1.20.2 releases of rsyslog) did in mark procesing. They simply
- * do not belong here.
+int startIndexUxLocalSockets = 0; /* process funix from that index on (used to
+ * suppress local logging. rgerhards 2005-08-01
+ * read-only after startup
+ */
+int funixParseHost[MAXFUNIX] = { 0, }; /* should parser parse host name? read-only after startup */
+char *funixn[MAXFUNIX] = { _PATH_LOG }; /* read-only after startup */
+int funix[MAXFUNIX] = { -1, }; /* read-only after startup */
+static int nfunix = 1; /* number of Unix sockets open / read-only after startup */
+
+
+static int create_unix_socket(const char *path)
+{
+ struct sockaddr_un sunx;
+ int fd;
+ char line[MAXLINE +1];
+
+ if (path[0] == '\0')
+ return -1;
+
+ (void) unlink(path);
+
+ memset(&sunx, 0, sizeof(sunx));
+ sunx.sun_family = AF_UNIX;
+ (void) strncpy(sunx.sun_path, path, sizeof(sunx.sun_path));
+ fd = socket(AF_UNIX, SOCK_DGRAM, 0);
+ if (fd < 0 || bind(fd, (struct sockaddr *) &sunx,
+ SUN_LEN(&sunx)) < 0 ||
+ chmod(path, 0666) < 0) {
+ snprintf(line, sizeof(line), "cannot create %s", path);
+ logerror(line);
+ dbgprintf("cannot create %s (%d).\n", path, errno);
+ close(fd);
+ return -1;
+ }
+ return fd;
+}
+
+
+/* This function receives data from a socket indicated to be ready
+ * to receive and submits the message received for processing.
+ * rgerhards, 2007-12-20
+ */
+static rsRetVal readSocket(int fd, int bParseHost)
+{
+ DEFiRet;
+ int iRcvd;
+ char line[MAXLINE +1];
+
+ iRcvd = recv(fd, line, MAXLINE - 1, 0);
+ dbgprintf("Message from UNIX socket: #%d\n", fd);
+ if (iRcvd > 0) {
+ printchopped(LocalHostName, line, iRcvd, fd, bParseHost);
+ } else if (iRcvd < 0 && errno != EINTR) {
+ char errStr[1024];
+ strerror_r(errno, errStr, sizeof(errStr));
+ dbgprintf("UNIX socket error: %d = %s.\n", \
+ errno, errStr);
+ logerror("recvfrom UNIX");
+ }
+
+ return iRet;
+}
+
+
+/* This function is called to gather input.
*/
BEGINrunInput
+ int maxfds;
+ int nfds;
+ int i;
+ int fd;
+ fd_set readfds;
CODESTARTrunInput
/* this is an endless loop - it is terminated when the thread is
* signalled to do so. This, however, is handled by the framework,
@@ -71,23 +138,72 @@ CODESTARTrunInput
* special because we just need to terminate. Cleanup is done
* during afterRun(). -- rgerhards 2007-12-20
*/
+ /* Add the Unix Domain Sockets to the list of read
+ * descriptors.
+ * rgerhards 2005-08-01: we must now check if there are
+ * any local sockets to listen to at all. If the -o option
+ * is given without -a, we do not need to listen at all..
+ */
+ maxfds = 0;
+ FD_ZERO (&readfds);
+ /* Copy master connections */
+ for (i = startIndexUxLocalSockets; i < nfunix; i++) {
+ if (funix[i] != -1) {
+ FD_SET(funix[i], &readfds);
+ if (funix[i]>maxfds) maxfds=funix[i];
+ }
+ }
+
/* select() here */
+ nfds = select(maxfds+1, (fd_set *) &readfds, NULL, NULL, NULL);
+
+ for (i = 0; i < nfunix && nfds > 0; i++) {
+ if ((fd = funix[i]) != -1 && FD_ISSET(fd, &readfds)) {
+ readSocket(fd, funixParseHost[i]);
+ --nfds; /* indicate we have processed one */
+ }
+ }
}
-finalize_it:
+
return iRet;
ENDrunInput
BEGINwillRun
CODESTARTwillRun
+ register int i;
+ for (i = 1; i < MAXFUNIX; i++) {
+ funixn[i] = "";
+ funix[i] = -1;
+ }
/* initialize and return if will run or not */
+ for (i = startIndexUxLocalSockets ; i < nfunix ; i++) {
+ if (funix[i] != -1)
+ /* Don't close the socket, preserve it instead
+ close(funix[i]);
+ */
+ continue;
+ if ((funix[i] = create_unix_socket(funixn[i])) != -1)
+ dbgprintf("Opened UNIX socket '%s' (fd %d).\n", funixn[i], funix[i]);
+ }
+
return RS_RET_OK;
ENDwillRun
BEGINafterRun
CODESTARTafterRun
+ int i;
/* do cleanup here */
+ /* Close the UNIX sockets. */
+ for (i = 0; i < nfunix; i++)
+ if (funix[i] != -1)
+ close(funix[i]);
+
+ /* Clean-up files. */
+ for (i = 0; i < nfunix; i++)
+ if (funixn[i] && funix[i] != -1)
+ (void)unlink(funixn[i]);
ENDafterRun