Created
November 6, 2019 11:09
-
-
Save proditis/333779b3e49946cd56c98af1a13d1ffb to your computer and use it in GitHub Desktop.
OpenBSD sendsyslogd example taken from https://github.com/syslog-ng/syslog-ng/issues/1786#issuecomment-348819466
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
https://github.com/syslog-ng/syslog-ng/issues/1786#issuecomment-348819466 | |
*/ | |
#include <sys/socket.h> | |
#include <sys/ioctl.h> | |
#include <sys/un.h> | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <syslog.h> | |
struct NewSyslogListener | |
{ | |
int klog; | |
int pair[2]; | |
}; | |
int newsysloglistener_init(struct NewSyslogListener *this) | |
{ | |
if (socketpair(AF_UNIX, SOCK_DGRAM, PF_UNSPEC, this->pair) == -1) { | |
printf("cannot bind socket fd\n"); | |
return 1; | |
} | |
if ((this->klog = open("/dev/klog", O_RDONLY, 0)) == -1) { | |
printf("cannot open /dev/klog %d\n",errno); | |
return 2; | |
} | |
if (ioctl(this->klog, LIOCSFD, &(this->pair[1])) == -1) { | |
printf("cannot ioctl klog LIOCSFD\n"); | |
return 3; | |
} | |
close(this->pair[1]); | |
return 0; | |
}; | |
void newsysloglistener_deinit(struct NewSyslogListener *this) | |
{ | |
close(this->pair[0]); | |
close(this->klog); | |
} | |
int main() | |
{ | |
struct NewSyslogListener newsyslog; | |
int res = -1; | |
res = newsysloglistener_init(&newsyslog); | |
if (res) { | |
return res; | |
} | |
struct sockaddr_un sa; | |
socklen_t salen; | |
ssize_t n; | |
char linebuf[1025]; | |
salen = sizeof(sa); | |
while (1) { | |
n = recvfrom(newsyslog.pair[0], linebuf, 1024, 0, (struct sockaddr*)&sa, &salen); | |
if (n>0) printf("msg: %.*s\n", (int)n, linebuf); | |
else printf("nothing to print\n"); | |
}; | |
newsysloglistener_deinit(&newsyslog); | |
return 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment