diff options
author | WUEBBELS, Josef \(Extern\) | 2011-01-28 14:15:20 +0100 |
---|---|---|
committer | Karel Zak | 2011-02-14 17:59:28 +0100 |
commit | 912d6b98925a34b05fb4791a2f52225b06c420af (patch) | |
tree | e2ffd9923b4018a70abbf74bd802e1c4f6829ec3 /misc-utils/logger.c | |
parent | build-sys: provide alternatives for err, errx, warn and warnx (diff) | |
download | kernel-qcow2-util-linux-912d6b98925a34b05fb4791a2f52225b06c420af.tar.gz kernel-qcow2-util-linux-912d6b98925a34b05fb4791a2f52225b06c420af.tar.xz kernel-qcow2-util-linux-912d6b98925a34b05fb4791a2f52225b06c420af.zip |
logger: support for logging to UDP socket / remote syslog server
It adds the ability to logger to log a message to a udp socket. The -n option
followed by the hostname of the remote host is mandatory to do this. The
optional -P option can be used to change the UDP destination port (default
514). The function udpopenlog is used to open the udp socket. After that
everything works in almost the same way like it does when logging to a UNIX
socket.
Signed-off-by: Josef Wuebbels <josef.wuebbels@mtu.de>
Diffstat (limited to 'misc-utils/logger.c')
-rw-r--r-- | misc-utils/logger.c | 47 |
1 files changed, 43 insertions, 4 deletions
diff --git a/misc-utils/logger.c b/misc-utils/logger.c index 95050a1b3..987339990 100644 --- a/misc-utils/logger.c +++ b/misc-utils/logger.c @@ -47,6 +47,8 @@ #include <sys/types.h> #include <sys/socket.h> #include <sys/un.h> +#include <arpa/inet.h> +#include <netdb.h> #include "nls.h" #define SYSLOG_NAMES @@ -57,6 +59,7 @@ int pencode __P((char *)); void usage __P((void)); static int optd = 0; +static int udpport = 514; static int myopenlog(const char *sock) { @@ -83,6 +86,32 @@ myopenlog(const char *sock) { return fd; } +static int +udpopenlog(const char *servername,int port) { + int fd; + struct sockaddr_in s_addr; + struct hostent *serverhost; + + if ((serverhost = gethostbyname(servername)) == NULL ){ + printf (_("unable to resolve '%s'\n"), servername); + exit(1); + } + + if ((fd = socket(AF_INET, SOCK_DGRAM , 0)) == -1) { + printf (_("socket: %s.\n"), strerror(errno)); + exit (1); + } + + bcopy(serverhost->h_addr,&s_addr.sin_addr,serverhost->h_length); + s_addr.sin_family=AF_INET; + s_addr.sin_port=htons(port); + + if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) { + printf (_("connect: %s.\n"), strerror(errno)); + exit(1); + } + return fd; +} static void mysyslog(int fd, int logflags, int pri, char *tag, char *msg) { char buf[1000], pid[30], *cp, *tp; @@ -122,6 +151,7 @@ main(int argc, char **argv) { int ch, logflags, pri; char *tag, buf[1024]; char *usock = NULL; + char *udpserver = NULL; int LogSock = -1; setlocale(LC_ALL, ""); @@ -131,7 +161,7 @@ main(int argc, char **argv) { tag = NULL; pri = LOG_NOTICE; logflags = 0; - while ((ch = getopt(argc, argv, "f:ip:st:u:d")) != -1) + while ((ch = getopt(argc, argv, "f:ip:st:u:dn:P:")) != -1) switch((char)ch) { case 'f': /* file to log */ if (freopen(optarg, "r", stdin) == NULL) { @@ -159,6 +189,13 @@ main(int argc, char **argv) { case 'd': optd = 1; /* use datagrams */ break; + case 'n': /* udp socket */ + optd = 1; /* use datagrams because udp */ + udpserver = optarg; + break; + case 'P': /* change udp port */ + udpport = atoi(optarg); + break; case '?': default: usage(); @@ -167,8 +204,10 @@ main(int argc, char **argv) { argv += optind; /* setup for logging */ - if (!usock) + if (!usock && !udpserver) openlog(tag ? tag : getlogin(), logflags, 0); + else if (udpserver) + LogSock = udpopenlog(udpserver,udpport); else LogSock = myopenlog(usock); @@ -182,14 +221,14 @@ main(int argc, char **argv) { for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) { len = strlen(*argv); if (p + len > endp && p > buf) { - if (!usock) + if (!usock && !udpserver) syslog(pri, "%s", buf); else mysyslog(LogSock, logflags, pri, tag, buf); p = buf; } if (len > sizeof(buf) - 1) { - if (!usock) + if (!usock && !udpserver) syslog(pri, "%s", *argv++); else mysyslog(LogSock, logflags, pri, tag, *argv++); |