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
|
|
|
*
|
2006-06-01 14:36:14 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
2001-03-15 19:05:59 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-01 14:36:14 +00:00
|
|
|
#include "busybox.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
|
|
|
|
2007-01-10 22:35:54 +00:00
|
|
|
enum { KEY_ID = 0x414e4547 }; /* "GENA" */
|
2001-03-15 19:05:59 +00:00
|
|
|
|
|
|
|
static struct shbuf_ds {
|
2007-01-10 22:35:54 +00:00
|
|
|
int32_t size; // size of data written
|
|
|
|
int32_t head; // start of message list
|
|
|
|
int32_t tail; // end of message list
|
|
|
|
char data[1]; // data/messages
|
|
|
|
} *buf; // shared memory pointer
|
2001-03-15 19:05:59 +00:00
|
|
|
|
|
|
|
// Semaphore operation structures
|
|
|
|
static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
|
|
|
|
static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
|
|
|
|
|
|
|
|
|
2007-01-04 17:59:59 +00:00
|
|
|
static void error_exit(const char *str) ATTRIBUTE_NORETURN;
|
|
|
|
static void error_exit(const char *str)
|
|
|
|
{
|
|
|
|
//release all acquired resources
|
2007-01-10 22:35:54 +00:00
|
|
|
shmdt(buf);
|
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]");
|
|
|
|
}
|
|
|
|
|
2007-01-10 22:35:54 +00:00
|
|
|
static void interrupted(int sig ATTRIBUTE_UNUSED)
|
2007-01-04 17:59:59 +00:00
|
|
|
{
|
|
|
|
signal(SIGINT, SIG_IGN);
|
|
|
|
shmdt(buf);
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2007-02-03 17:28:39 +00:00
|
|
|
int logread_main(int argc, char **argv);
|
2006-03-06 20:47:33 +00:00
|
|
|
int logread_main(int argc, char **argv)
|
2001-03-15 19:05:59 +00:00
|
|
|
{
|
2007-01-04 17:59:59 +00:00
|
|
|
int cur;
|
2007-01-10 22:35:54 +00:00
|
|
|
int log_semid; /* ipc semaphore id */
|
|
|
|
int log_shmid; /* ipc shared memory id */
|
|
|
|
smallint follow = getopt32(argc, argv, "f");
|
2004-03-15 08:29:22 +00:00
|
|
|
|
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
|
|
|
|
2001-03-15 19:05:59 +00:00
|
|
|
// Attach shared memory to our char*
|
2007-01-04 17:59:59 +00:00
|
|
|
buf = shmat(log_shmid, NULL, SHM_RDONLY);
|
|
|
|
if (buf == 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
|
|
|
// attempt to redefine ^C signal
|
|
|
|
signal(SIGINT, interrupted);
|
2001-03-15 19:05:59 +00:00
|
|
|
|
2003-09-26 01:03:16 +00:00
|
|
|
// Suppose atomic memory move
|
2007-01-04 17:59:59 +00:00
|
|
|
cur = follow ? buf->tail : buf->head;
|
2003-09-26 01:03:16 +00:00
|
|
|
|
|
|
|
do {
|
2007-01-10 22:35:54 +00:00
|
|
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
2003-09-26 01:03:16 +00:00
|
|
|
char *buf_data;
|
2007-01-04 17:59:59 +00:00
|
|
|
int log_len, j;
|
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-01-04 17:59:59 +00:00
|
|
|
if (DEBUG)
|
2007-01-10 22:35:54 +00:00
|
|
|
printf("head:%i cur:%d tail:%i size:%i\n",
|
|
|
|
buf->head, cur, buf->tail, buf->size);
|
2007-01-04 17:59:59 +00:00
|
|
|
|
|
|
|
if (buf->head == buf->tail || cur == buf->tail) {
|
2003-09-26 01:03:16 +00:00
|
|
|
if (follow) {
|
|
|
|
sem_up(log_semid);
|
2007-01-10 22:35:54 +00:00
|
|
|
fflush(stdout);
|
|
|
|
sleep(1); /* TODO: replace me with a sleep_on */
|
2003-09-26 01:03:16 +00:00
|
|
|
continue;
|
|
|
|
}
|
2007-01-10 22:35:54 +00:00
|
|
|
puts("<empty syslog>");
|
2003-09-26 01:03:16 +00:00
|
|
|
}
|
2004-03-15 08:29:22 +00:00
|
|
|
|
|
|
|
// Read Memory
|
2007-01-10 22:35:54 +00:00
|
|
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
2007-01-04 17:59:59 +00:00
|
|
|
log_len = buf->tail - cur;
|
2003-09-26 01:03:16 +00:00
|
|
|
if (log_len < 0)
|
|
|
|
log_len += buf->size;
|
2006-12-19 23:36:04 +00:00
|
|
|
buf_data = xmalloc(log_len);
|
2003-09-26 01:03:16 +00:00
|
|
|
|
2007-01-10 22:35:54 +00:00
|
|
|
if (buf->tail >= cur)
|
|
|
|
j = log_len;
|
|
|
|
else
|
|
|
|
j = buf->size - cur;
|
|
|
|
memcpy(buf_data, buf->data + cur, j);
|
|
|
|
|
|
|
|
if (buf->tail < cur)
|
2007-01-04 17:59:59 +00:00
|
|
|
memcpy(buf_data + buf->size - cur, buf->data, buf->tail);
|
|
|
|
cur = buf->tail;
|
2003-09-26 01:03:16 +00:00
|
|
|
#else
|
2007-01-04 17:59:59 +00:00
|
|
|
while (cur != buf->tail) {
|
|
|
|
fputs(buf->data + cur, stdout);
|
|
|
|
cur += strlen(buf->data + cur) + 1;
|
|
|
|
if (cur >= buf->size)
|
|
|
|
cur = 0;
|
2003-09-26 01:03:16 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
// release the lock on the log chain
|
|
|
|
sem_up(log_semid);
|
|
|
|
|
2007-01-10 22:35:54 +00:00
|
|
|
#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
|
2007-01-04 17:59:59 +00:00
|
|
|
for (j = 0; j < log_len; j += strlen(buf_data+j) + 1) {
|
|
|
|
fputs(buf_data + j, stdout);
|
2003-09-26 01:03:16 +00:00
|
|
|
}
|
|
|
|
free(buf_data);
|
|
|
|
#endif
|
|
|
|
} while (follow);
|
2001-03-15 19:05:59 +00:00
|
|
|
|
2007-01-04 17:59:59 +00:00
|
|
|
shmdt(buf);
|
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
|
|
|
}
|