1
0
mirror of https://github.com/openbsd/src.git synced 2025-01-03 06:45:37 -08:00

support new syslogd control protocol and add new -o option to detect memory

buffer overflow; idea & ok markus@

NB if you are using memory buffered logging make sure you update both syslogd
and syslogc _and_ restart syslogd because the protocol has changed
This commit is contained in:
djm 2004-06-25 19:11:38 +00:00
parent 35b969541e
commit 5b27f6176e
2 changed files with 55 additions and 8 deletions

View File

@ -1,4 +1,4 @@
.\" $OpenBSD: syslogc.8,v 1.1 2004/01/04 08:30:21 djm Exp $
.\" $OpenBSD: syslogc.8,v 1.2 2004/06/25 19:11:38 djm Exp $
.\"
.\" Copyright (c) 2004 Damien Miller
.\"
@ -21,7 +21,7 @@
.Nd collect messages from syslog memory buffer
.Sh SYNOPSIS
.Nm syslogc
.Op Fl Ccq
.Op Fl Ccoq
.Op Fl s Ar reporting_socket
.Ar logname
.Nm syslogc
@ -59,6 +59,11 @@ The options are as follows:
Request that the log buffer be cleared without reading it.
.It Fl c
Request that the log buffer be cleared once it has been read.
.It Fl o
Check whether the specified log has overflowed.
If the log has overflowed, then a message will be printed to
.Xr stdout 4
and the exit status will be set to 1.
.It Fl q
Request a list of available logs.
.It Fl s Ar reporting_socket

View File

@ -1,4 +1,4 @@
/* $OpenBSD: syslogc.c,v 1.5 2004/04/13 01:10:05 djm Exp $ */
/* $OpenBSD: syslogc.c,v 1.6 2004/06/25 19:11:38 djm Exp $ */
/*
* Copyright (c) 2004 Damien Miller
@ -31,15 +31,35 @@
#define MAX_MEMBUF_NAME 64 /* Max length of membuf log name */
/*
* Client protocol NB. all numeric fields in network byte order
*/
#define CTL_VERSION 0
/* Request */
struct ctl_cmd {
u_int32_t version;
#define CMD_READ 1 /* Read out log */
#define CMD_READ_CLEAR 2 /* Read and clear log */
#define CMD_CLEAR 3 /* Clear log */
#define CMD_LIST 4 /* List available logs */
int cmd;
char logname[MAX_MEMBUF_NAME];
#define CMD_FLAGS 5 /* Query flags only */
u_int32_t cmd;
char logname[MAX_MEMBUF_NAME];
};
/* Reply */
struct ctl_reply_hdr {
u_int32_t version;
#define CTL_HDR_FLAG_OVERFLOW 0x01
u_int32_t flags;
/* Reply text follows, up to MAX_MEMBUF long */
};
/* Protocol parameters - must match syslogd */
#define CTL_VERSION 0
#define CTL_HDR_LEN 8
static void
usage(void)
{
@ -56,16 +76,19 @@ main(int argc, char **argv)
char buf[8192];
struct sockaddr_un ctl;
socklen_t ctllen;
int ctlsock, ch;
int ctlsock, ch, oflag, rval;
FILE *ctlf;
extern char *optarg;
extern int optind;
struct ctl_cmd cc;
struct ctl_reply_hdr rr;
u_int32_t header[2];
memset(&cc, '\0', sizeof(cc));
ctlsock_path = DEFAULT_CTLSOCK;
while ((ch = getopt(argc, argv, "Cchqs:")) != -1) {
rval = oflag = 0;
while ((ch = getopt(argc, argv, "Cchoqs:")) != -1) {
switch (ch) {
case 'C':
cc.cmd = CMD_CLEAR;
@ -76,6 +99,10 @@ main(int argc, char **argv)
case 'h':
usage();
break;
case 'o':
cc.cmd = CMD_FLAGS;
oflag = 1;
break;
case 'q':
cc.cmd = CMD_LIST;
break;
@ -112,6 +139,9 @@ main(int argc, char **argv)
err(1, "connect: %s", ctl.sun_path);
if ((ctlf = fdopen(ctlsock, "r+")) == NULL)
err(1, "fdopen");
cc.version = htonl(CTL_VERSION);
cc.cmd = htonl(cc.cmd);
/* Send command */
if (fwrite(&cc, sizeof(cc), 1, ctlf) != 1)
err(1, "fwrite");
@ -119,12 +149,24 @@ main(int argc, char **argv)
fflush(ctlf);
setlinebuf(ctlf);
/* Fetch header */
if (fread(&rr, sizeof(rr), 1, ctlf) != 1)
err(1, "fread header");
if (ntohl(rr.version) != CTL_VERSION)
err(1, "unsupported syslogd version");
/* Write out reply */
while((fgets(buf, sizeof(buf), ctlf)) != NULL)
fputs(buf, stdout);
if (oflag && (ntohl(rr.flags) & CTL_HDR_FLAG_OVERFLOW)) {
printf("%s has overflowed\n", cc.logname);
rval = 1;
}
fclose(ctlf);
close(ctlsock);
exit(0);
exit(rval);
}