2001-03-15 19:05:59 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* circular buffer syslog implementation for busybox
|
|
|
|
*
|
2002-12-12 10:54:48 +00:00
|
|
|
* Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
|
2001-03-15 19:05:59 +00:00
|
|
|
*
|
2002-12-12 10:54:48 +00:00
|
|
|
* Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
|
2001-03-15 19:05:59 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2001-03-15 19:05:59 +00:00
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2001-03-15 19:05:59 +00:00
|
|
|
#include <sys/ipc.h>
|
|
|
|
#include <sys/sem.h>
|
|
|
|
#include <sys/shm.h>
|
2007-01-04 17:59:59 +00:00
|
|
|
|
|
|
|
#define DEBUG 0
|
2001-03-15 19:05:59 +00:00
|
|
|
|
2008-06-11 15:43:19 +00:00
|
|
|
/* our shared key (syslogd.c and logread.c must be in sync) */
|
2007-01-10 22:35:54 +00:00
|
|
|
enum { KEY_ID = 0x414e4547 }; /* "GENA" */
|
2001-03-15 19:05:59 +00:00
|
|
|
|
2007-11-23 03:39:45 +00:00
|
|
|
struct shbuf_ds {
|
2007-08-12 21:33:06 +00:00
|
|
|
int32_t size; // size of data - 1
|
2007-01-10 22:35:54 +00:00
|
|
|
int32_t tail; // end of message list
|
2007-08-12 21:33:06 +00:00
|
|
|
char data[1]; // messages
|
2007-11-23 03:39:45 +00:00
|
|
|
};
|
2001-03-15 19:05:59 +00:00
|
|
|
|
2007-11-23 03:39:45 +00:00
|
|
|
static const struct sembuf init_sem[3] = {
|
|
|
|
{0, -1, IPC_NOWAIT | SEM_UNDO},
|
|
|
|
{1, 0}, {0, +1, SEM_UNDO}
|
|
|
|
};
|
2001-03-15 19:05:59 +00:00
|
|
|
|
2007-11-23 03:39:45 +00:00
|
|
|
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 14:00:15 +00:00
|
|
|
} FIX_ALIASING;
|
2007-11-23 03:39:45 +00:00
|
|
|
#define G (*(struct globals*)&bb_common_bufsiz1)
|
|
|
|
#define SMrup (G.SMrup)
|
|
|
|
#define SMrdn (G.SMrdn)
|
|
|
|
#define shbuf (G.shbuf)
|
2008-06-25 09:53:17 +00:00
|
|
|
#define INIT_G() do { \
|
|
|
|
memcpy(SMrup, init_sem, sizeof(init_sem)); \
|
|
|
|
} while (0)
|
2001-03-15 19:05:59 +00:00
|
|
|
|
2008-07-05 09:18:54 +00:00
|
|
|
static void error_exit(const char *str) NORETURN;
|
2007-01-04 17:59:59 +00:00
|
|
|
static void error_exit(const char *str)
|
|
|
|
{
|
|
|
|
//release all acquired resources
|
2007-08-12 21:33:06 +00:00
|
|
|
shmdt(shbuf);
|
2007-01-04 17:59:59 +00:00
|
|
|
bb_perror_msg_and_die(str);
|
|
|
|
}
|
2001-03-15 19:05:59 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* sem_up - up()'s a semaphore.
|
|
|
|
*/
|
2006-08-29 19:41:06 +00:00
|
|
|
static void sem_up(int semid)
|
2001-03-15 19:05:59 +00:00
|
|
|
{
|
2007-01-04 17:59:59 +00:00
|
|
|
if (semop(semid, SMrup, 1) == -1)
|
2001-03-15 19:05:59 +00:00
|
|
|
error_exit("semop[SMrup]");
|
|
|
|
}
|
|
|
|
|
2008-07-05 09:18:54 +00:00
|
|
|
static void interrupted(int sig UNUSED_PARAM)
|
2007-01-04 17:59:59 +00:00
|
|
|
{
|
|
|
|
signal(SIGINT, SIG_IGN);
|
2007-08-12 21:33:06 +00:00
|
|
|
shmdt(shbuf);
|
2008-05-19 09:29:47 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2007-01-04 17:59:59 +00:00
|
|
|
}
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int logread_main(int argc UNUSED_PARAM, char **argv)
|
2001-03-15 19:05:59 +00:00
|
|
|
{
|
2008-05-15 21:30:45 +00:00
|
|
|
unsigned cur;
|
2007-01-10 22:35:54 +00:00
|
|
|
int log_semid; /* ipc semaphore id */
|
|
|
|
int log_shmid; /* ipc shared memory id */
|
2007-08-18 15:32:12 +00:00
|
|
|
smallint follow = getopt32(argv, "f");
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2007-11-23 03:41:20 +00:00
|
|
|
INIT_G();
|
|
|
|
|
2007-01-04 17:59:59 +00:00
|
|
|
log_shmid = shmget(KEY_ID, 0, 0);
|
|
|
|
if (log_shmid == -1)
|
2007-01-10 22:35:54 +00:00
|
|
|
bb_perror_msg_and_die("can't find syslogd buffer");
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2007-08-12 21:33:06 +00:00
|
|
|
/* Attach shared memory to our char* */
|
|
|
|
shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
|
|
|
|
if (shbuf == NULL)
|
2007-01-10 22:35:54 +00:00
|
|
|
bb_perror_msg_and_die("can't access syslogd buffer");
|
2007-01-04 17:59:59 +00:00
|
|
|
|
|
|
|
log_semid = semget(KEY_ID, 0, 0);
|
|
|
|
if (log_semid == -1)
|
|
|
|
error_exit("can't get access to semaphores for syslogd buffer");
|
2001-03-15 19:05:59 +00:00
|
|
|
|
2007-01-04 17:59:59 +00:00
|
|
|
signal(SIGINT, interrupted);
|
2001-03-15 19:05:59 +00:00
|
|
|
|
2007-08-12 21:33:06 +00:00
|
|
|
/* Suppose atomic memory read */
|
|
|
|
/* Max possible value for tail is shbuf->size - 1 */
|
|
|
|
cur = shbuf->tail;
|
2003-09-26 01:03:16 +00:00
|
|
|
|
2007-08-12 21:33:06 +00:00
|
|
|
/* Loop for logread -f, one pass if there was no -f */
|
2003-09-26 01:03:16 +00:00
|
|
|
do {
|
2007-08-12 21:33:06 +00:00
|
|
|
unsigned shbuf_size;
|
|
|
|
unsigned shbuf_tail;
|
|
|
|
const char *shbuf_data;
|
2007-01-10 22:35:54 +00:00
|
|
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
2007-08-12 21:33:06 +00:00
|
|
|
int i;
|
|
|
|
int len_first_part;
|
|
|
|
int len_total = len_total; /* for gcc */
|
|
|
|
char *copy = copy; /* for gcc */
|
2003-09-26 01:03:16 +00:00
|
|
|
#endif
|
2007-01-10 22:35:54 +00:00
|
|
|
if (semop(log_semid, SMrdn, 2) == -1)
|
|
|
|
error_exit("semop[SMrdn]");
|
2003-09-26 01:03:16 +00:00
|
|
|
|
2007-08-12 21:33:06 +00:00
|
|
|
/* 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 17:59:59 +00:00
|
|
|
|
2007-08-12 21:33:06 +00:00
|
|
|
if (DEBUG)
|
|
|
|
printf("cur:%d tail:%i size:%i\n",
|
|
|
|
cur, shbuf_tail, shbuf_size);
|
|
|
|
|
|
|
|
if (!follow) {
|
|
|
|
/* 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;
|
|
|
|
} else { /* logread -f */
|
|
|
|
if (cur == shbuf_tail) {
|
2003-09-26 01:03:16 +00:00
|
|
|
sem_up(log_semid);
|
2009-11-02 13:19:51 +00:00
|
|
|
fflush_all();
|
2007-01-10 22:35:54 +00:00
|
|
|
sleep(1); /* TODO: replace me with a sleep_on */
|
2003-09-26 01:03:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2007-08-12 21:33:06 +00:00
|
|
|
/* Read from cur to tail */
|
2007-01-10 22:35:54 +00:00
|
|
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
2007-08-12 21:33:06 +00:00
|
|
|
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 01:03:16 +00:00
|
|
|
#else
|
2007-08-12 21:33:06 +00:00
|
|
|
while (cur != shbuf_tail) {
|
|
|
|
fputs(shbuf_data + cur, stdout);
|
|
|
|
cur += strlen(shbuf_data + cur) + 1;
|
|
|
|
if (cur >= shbuf_size)
|
2007-01-04 17:59:59 +00:00
|
|
|
cur = 0;
|
2003-09-26 01:03:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
2007-08-12 21:33:06 +00:00
|
|
|
unlock:
|
|
|
|
/* release the lock on the log chain */
|
2003-09-26 01:03:16 +00:00
|
|
|
sem_up(log_semid);
|
|
|
|
|
2007-01-10 22:35:54 +00:00
|
|
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
2007-08-12 21:33:06 +00:00
|
|
|
for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
|
|
|
|
fputs(copy + i, stdout);
|
2003-09-26 01:03:16 +00:00
|
|
|
}
|
2007-08-12 21:33:06 +00:00
|
|
|
free(copy);
|
2003-09-26 01:03:16 +00:00
|
|
|
#endif
|
|
|
|
} while (follow);
|
2001-03-15 19:05:59 +00:00
|
|
|
|
2007-08-12 21:33:06 +00:00
|
|
|
shmdt(shbuf);
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2007-01-10 22:35:54 +00:00
|
|
|
fflush_stdout_and_exit(EXIT_SUCCESS);
|
2001-03-15 19:05:59 +00:00
|
|
|
}
|