aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorvg <vgm+dev@devys.org>2024-02-23 14:37:47 +0100
committervg <vgm+dev@devys.org>2024-02-23 14:37:47 +0100
commitc18dd2b30e2dc95043492d2b8495ecca3d1b8e40 (patch)
tree5edc99eb8641cb36d605c475b179e0e541af762c
parent82ca7fb1dab090f0999ce93cf3540e5840c4a91f (diff)
downloadscripts-c18dd2b30e2dc95043492d2b8495ecca3d1b8e40.tar.gz
scripts-c18dd2b30e2dc95043492d2b8495ecca3d1b8e40.tar.bz2
scripts-c18dd2b30e2dc95043492d2b8495ecca3d1b8e40.zip
add new syslog-listen program to repository
-rw-r--r--small-codes/syslog-listen.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/small-codes/syslog-listen.c b/small-codes/syslog-listen.c
new file mode 100644
index 0000000..c878076
--- /dev/null
+++ b/small-codes/syslog-listen.c
@@ -0,0 +1,47 @@
+// This program is able to listen to a datagram unix socket displaying
+// a linefeed at each receive. Useful when other program wants scrutinize
+// syslog forwarded outputs for example (like in omuxsock module by rsyslog
+// does).
+//
+// compile with gcc/clang -o syslog-listen syslog-listen.c
+
+
+#include <unistd.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <errno.h>
+
+#define PERROR(code, msg) { if ((code) == -1) { perror((msg)); exit(1); } }
+
+int main(int argc, const char** argv)
+{
+ if (argc < 2) {
+ fprintf(stderr, "Usage: syslog-listen </path>\n");
+ exit(1);
+ }
+
+ struct sockaddr_un sock_file;
+ memset(&sock_file, 0, sizeof(sock_file));
+ sock_file.sun_family = AF_UNIX;
+ strncpy(sock_file.sun_path, argv[1], sizeof(sock_file.sun_path));
+
+ int sock = socket(AF_UNIX, SOCK_DGRAM, 0);
+ PERROR(sock, "socket");
+ unlink(argv[1]); // no need to test ret as bind will crash if needed
+ int ret = bind(sock, (struct sockaddr*)&sock_file, sizeof(sock_file));
+ PERROR(ret, "bind");
+
+ char buf[8192];
+ int receive;
+ while (1) {
+ receive = recv(sock, buf, sizeof(buf), 0);
+ PERROR(receive, "recv");
+ buf[receive] = '\0';
+ printf("received: %s\n", buf);
+ }
+
+ return 0;
+}