2001-03-16 00:35:59 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* circular buffer syslog implementation for busybox
|
|
|
|
*
|
2002-12-12 16:24:48 +05:30
|
|
|
* Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
|
2001-03-16 00:35:59 +05:30
|
|
|
*
|
2002-12-12 16:24:48 +05:30
|
|
|
* Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
|
2001-03-16 00:35:59 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2001-03-16 00:35:59 +05:30
|
|
|
*/
|
2015-10-18 22:12:03 +05:30
|
|
|
//config:config LOGREAD
|
|
|
|
//config: bool "logread"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on FEATURE_IPC_SYSLOG
|
|
|
|
//config: help
|
|
|
|
//config: If you enabled Circular Buffer support, you almost
|
|
|
|
//config: certainly want to enable this feature as well. This
|
|
|
|
//config: utility will allow you to read the messages that are
|
|
|
|
//config: stored in the syslogd circular buffer.
|
|
|
|
//config:
|
|
|
|
//config:config FEATURE_LOGREAD_REDUCED_LOCKING
|
|
|
|
//config: bool "Double buffering"
|
|
|
|
//config: default y
|
|
|
|
//config: depends on LOGREAD
|
|
|
|
//config: help
|
|
|
|
//config: 'logread' ouput to slow serial terminals can have
|
|
|
|
//config: side effects on syslog because of the semaphore.
|
|
|
|
//config: This option make logread to double buffer copy
|
|
|
|
//config: from circular buffer, minimizing semaphore
|
|
|
|
//config: contention at some minor memory expense.
|
|
|
|
//config:
|
|
|
|
|
|
|
|
//applet:IF_LOGREAD(APPLET(logread, BB_DIR_SBIN, BB_SUID_DROP))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_LOGREAD) += logread.o
|
2001-03-16 00:35:59 +05:30
|
|
|
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define logread_trivial_usage
|
2015-03-22 22:06:20 +05:30
|
|
|
//usage: "[-fF]"
|
2011-04-11 06:59:49 +05:30
|
|
|
//usage:#define logread_full_usage "\n\n"
|
|
|
|
//usage: "Show messages in syslogd's circular buffer\n"
|
|
|
|
//usage: "\n -f Output data as log grows"
|
2015-03-22 22:06:20 +05:30
|
|
|
//usage: "\n -F Same as -f, but dump buffer first"
|
2011-04-11 06:59:49 +05:30
|
|
|
|
2007-05-27 00:30:18 +05:30
|
|
|
#include "libbb.h"
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2001-03-16 00:35:59 +05:30
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/sem.h>
|
|
|
|
#include <sys/shm.h>
|
2007-01-04 23:29:59 +05:30
|
|
|
|
|
|
|
#define DEBUG 0
|
2001-03-16 00:35:59 +05:30
|
|
|
|
2008-06-11 21:13:19 +05:30
|
|
|
/* our shared key (syslogd.c and logread.c must be in sync) */
|
2007-01-11 04:05:54 +05:30
|
|
|
enum { KEY_ID = 0x414e4547 }; /* "GENA" */
|
2001-03-16 00:35:59 +05:30
|
|
|
|
2007-11-23 09:09:45 +05:30
|
|
|
struct shbuf_ds {
|
2007-08-13 03:03:06 +05:30
|
|
|
int32_t size; // size of data - 1
|
2007-01-11 04:05:54 +05:30
|
|
|
int32_t tail; // end of message list
|
2007-08-13 03:03:06 +05:30
|
|
|
char data[1]; // messages
|
2007-11-23 09:09:45 +05:30
|
|
|
};
|
2001-03-16 00:35:59 +05:30
|
|
|
|
2007-11-23 09:09:45 +05:30
|
|
|
static const struct sembuf init_sem[3] = {
|
|
|
|
{0, -1, IPC_NOWAIT | SEM_UNDO},
|
|
|
|
{1, 0}, {0, +1, SEM_UNDO}
|
|
|
|
};
|
2001-03-16 00:35:59 +05:30
|
|
|
|
2007-11-23 09:09:45 +05:30
|
|
|
struct globals {
|
|
|
|
struct sembuf SMrup[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
|
|
|
|
struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
|
|
|
|
struct shbuf_ds *shbuf;
|
2010-02-04 19:30:15 +05:30
|
|
|
} FIX_ALIASING;
|
2016-04-21 19:56:30 +05:30
|
|
|
#define G (*(struct globals*)bb_common_bufsiz1)
|
2007-11-23 09:09:45 +05:30
|
|
|
#define SMrup (G.SMrup)
|
|
|
|
#define SMrdn (G.SMrdn)
|
|
|
|
#define shbuf (G.shbuf)
|
2008-06-25 15:23:17 +05:30
|
|
|
#define INIT_G() do { \
|
2016-04-21 21:48:48 +05:30
|
|
|
setup_common_bufsiz(); \
|
2008-06-25 15:23:17 +05:30
|
|
|
memcpy(SMrup, init_sem, sizeof(init_sem)); \
|
|
|
|
} while (0)
|
2001-03-16 00:35:59 +05:30
|
|
|
|
2013-11-26 16:32:18 +05:30
|
|
|
#if 0
|
2008-07-05 14:48:54 +05:30
|
|
|
static void error_exit(const char *str) NORETURN;
|
2007-01-04 23:29:59 +05:30
|
|
|
static void error_exit(const char *str)
|
|
|
|
{
|
2013-07-29 02:47:00 +05:30
|
|
|
/* Release all acquired resources */
|
2007-08-13 03:03:06 +05:30
|
|
|
shmdt(shbuf);
|
2007-01-04 23:29:59 +05:30
|
|
|
bb_perror_msg_and_die(str);
|
|
|
|
}
|
2013-11-26 16:32:18 +05:30
|
|
|
#else
|
|
|
|
/* On Linux, shmdt is not mandatory on exit */
|
|
|
|
# define error_exit(str) bb_perror_msg_and_die(str)
|
|
|
|
#endif
|
2001-03-16 00:35:59 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* sem_up - up()'s a semaphore.
|
|
|
|
*/
|
2006-08-30 01:11:06 +05:30
|
|
|
static void sem_up(int semid)
|
2001-03-16 00:35:59 +05:30
|
|
|
{
|
2007-01-04 23:29:59 +05:30
|
|
|
if (semop(semid, SMrup, 1) == -1)
|
2001-03-16 00:35:59 +05:30
|
|
|
error_exit("semop[SMrup]");
|
|
|
|
}
|
|
|
|
|
2013-07-29 02:47:00 +05:30
|
|
|
static void interrupted(int sig)
|
2007-01-04 23:29:59 +05:30
|
|
|
{
|
2013-11-26 16:32:18 +05:30
|
|
|
/* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
|
2013-07-29 02:47:00 +05:30
|
|
|
kill_myself_with_sig(sig);
|
2007-01-04 23:29:59 +05:30
|
|
|
}
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int logread_main(int argc UNUSED_PARAM, char **argv)
|
2001-03-16 00:35:59 +05:30
|
|
|
{
|
2008-05-16 03:00:45 +05:30
|
|
|
unsigned cur;
|
2007-01-11 04:05:54 +05:30
|
|
|
int log_semid; /* ipc semaphore id */
|
|
|
|
int log_shmid; /* ipc shared memory id */
|
2015-03-22 22:06:20 +05:30
|
|
|
int follow = getopt32(argv, "fF");
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2007-11-23 09:11:20 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2007-01-04 23:29:59 +05:30
|
|
|
log_shmid = shmget(KEY_ID, 0, 0);
|
|
|
|
if (log_shmid == -1)
|
2013-11-26 16:32:18 +05:30
|
|
|
bb_perror_msg_and_die("can't %s syslogd buffer", "find");
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2007-08-13 03:03:06 +05:30
|
|
|
/* Attach shared memory to our char* */
|
|
|
|
shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
|
|
|
|
if (shbuf == NULL)
|
2013-11-26 16:32:18 +05:30
|
|
|
bb_perror_msg_and_die("can't %s syslogd buffer", "access");
|
2007-01-04 23:29:59 +05:30
|
|
|
|
|
|
|
log_semid = semget(KEY_ID, 0, 0);
|
|
|
|
if (log_semid == -1)
|
|
|
|
error_exit("can't get access to semaphores for syslogd buffer");
|
2001-03-16 00:35:59 +05:30
|
|
|
|
2013-07-29 02:47:00 +05:30
|
|
|
bb_signals(BB_FATAL_SIGS, interrupted);
|
2001-03-16 00:35:59 +05:30
|
|
|
|
2007-08-13 03:03:06 +05:30
|
|
|
/* Suppose atomic memory read */
|
|
|
|
/* Max possible value for tail is shbuf->size - 1 */
|
|
|
|
cur = shbuf->tail;
|
2003-09-26 06:33:16 +05:30
|
|
|
|
2015-03-22 22:06:20 +05:30
|
|
|
/* Loop for -f or -F, one pass otherwise */
|
2003-09-26 06:33:16 +05:30
|
|
|
do {
|
2007-08-13 03:03:06 +05:30
|
|
|
unsigned shbuf_size;
|
|
|
|
unsigned shbuf_tail;
|
|
|
|
const char *shbuf_data;
|
2007-01-11 04:05:54 +05:30
|
|
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
2007-08-13 03:03:06 +05:30
|
|
|
int i;
|
|
|
|
int len_first_part;
|
|
|
|
int len_total = len_total; /* for gcc */
|
|
|
|
char *copy = copy; /* for gcc */
|
2003-09-26 06:33:16 +05:30
|
|
|
#endif
|
2007-01-11 04:05:54 +05:30
|
|
|
if (semop(log_semid, SMrdn, 2) == -1)
|
|
|
|
error_exit("semop[SMrdn]");
|
2003-09-26 06:33:16 +05:30
|
|
|
|
2007-08-13 03:03:06 +05:30
|
|
|
/* Copy the info, helps gcc to realize that it doesn't change */
|
|
|
|
shbuf_size = shbuf->size;
|
|
|
|
shbuf_tail = shbuf->tail;
|
|
|
|
shbuf_data = shbuf->data; /* pointer! */
|
2007-01-04 23:29:59 +05:30
|
|
|
|
2007-08-13 03:03:06 +05:30
|
|
|
if (DEBUG)
|
2013-11-29 21:15:45 +05:30
|
|
|
printf("cur:%u tail:%u size:%u\n",
|
2007-08-13 03:03:06 +05:30
|
|
|
cur, shbuf_tail, shbuf_size);
|
|
|
|
|
2015-03-22 22:06:20 +05:30
|
|
|
if (!(follow & 1)) { /* not -f */
|
|
|
|
/* if -F, "convert" it to -f, so that we dont
|
|
|
|
* dump the entire buffer on each iteration
|
|
|
|
*/
|
|
|
|
follow >>= 1;
|
|
|
|
|
2007-08-13 03:03:06 +05:30
|
|
|
/* advance to oldest complete message */
|
|
|
|
/* find NUL */
|
|
|
|
cur += strlen(shbuf_data + cur);
|
|
|
|
if (cur >= shbuf_size) { /* last byte in buffer? */
|
|
|
|
cur = strnlen(shbuf_data, shbuf_tail);
|
|
|
|
if (cur == shbuf_tail)
|
|
|
|
goto unlock; /* no complete messages */
|
|
|
|
}
|
|
|
|
/* advance to first byte of the message */
|
|
|
|
cur++;
|
|
|
|
if (cur >= shbuf_size) /* last byte in buffer? */
|
|
|
|
cur = 0;
|
2015-03-22 22:06:20 +05:30
|
|
|
} else { /* -f */
|
2007-08-13 03:03:06 +05:30
|
|
|
if (cur == shbuf_tail) {
|
2003-09-26 06:33:16 +05:30
|
|
|
sem_up(log_semid);
|
2009-11-02 18:49:51 +05:30
|
|
|
fflush_all();
|
2007-01-11 04:05:54 +05:30
|
|
|
sleep(1); /* TODO: replace me with a sleep_on */
|
2003-09-26 06:33:16 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2007-08-13 03:03:06 +05:30
|
|
|
/* Read from cur to tail */
|
2007-01-11 04:05:54 +05:30
|
|
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
2007-08-13 03:03:06 +05:30
|
|
|
len_first_part = len_total = shbuf_tail - cur;
|
|
|
|
if (len_total < 0) {
|
|
|
|
/* message wraps: */
|
|
|
|
/* [SECOND PART.........FIRST PART] */
|
|
|
|
/* ^data ^tail ^cur ^size */
|
|
|
|
len_total += shbuf_size;
|
|
|
|
}
|
|
|
|
copy = xmalloc(len_total + 1);
|
|
|
|
if (len_first_part < 0) {
|
|
|
|
/* message wraps (see above) */
|
|
|
|
len_first_part = shbuf_size - cur;
|
|
|
|
memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
|
|
|
|
}
|
|
|
|
memcpy(copy, shbuf_data + cur, len_first_part);
|
|
|
|
copy[len_total] = '\0';
|
|
|
|
cur = shbuf_tail;
|
2003-09-26 06:33:16 +05:30
|
|
|
#else
|
2007-08-13 03:03:06 +05:30
|
|
|
while (cur != shbuf_tail) {
|
|
|
|
fputs(shbuf_data + cur, stdout);
|
|
|
|
cur += strlen(shbuf_data + cur) + 1;
|
|
|
|
if (cur >= shbuf_size)
|
2007-01-04 23:29:59 +05:30
|
|
|
cur = 0;
|
2003-09-26 06:33:16 +05:30
|
|
|
}
|
|
|
|
#endif
|
2007-08-13 03:03:06 +05:30
|
|
|
unlock:
|
|
|
|
/* release the lock on the log chain */
|
2003-09-26 06:33:16 +05:30
|
|
|
sem_up(log_semid);
|
|
|
|
|
2007-01-11 04:05:54 +05:30
|
|
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
2007-08-13 03:03:06 +05:30
|
|
|
for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
|
|
|
|
fputs(copy + i, stdout);
|
2003-09-26 06:33:16 +05:30
|
|
|
}
|
2007-08-13 03:03:06 +05:30
|
|
|
free(copy);
|
2003-09-26 06:33:16 +05:30
|
|
|
#endif
|
2013-11-26 16:14:27 +05:30
|
|
|
fflush_all();
|
2003-09-26 06:33:16 +05:30
|
|
|
} while (follow);
|
2001-03-16 00:35:59 +05:30
|
|
|
|
2013-11-26 16:32:18 +05:30
|
|
|
/* shmdt(shbuf); - on Linux, shmdt is not mandatory on exit */
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2007-01-11 04:05:54 +05:30
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
2001-03-16 00:35:59 +05:30
|
|
|
}
|