X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=sysklogd%2Flogread.c;h=ae0b2194b80b0b6cf33175a63608344eaa7dddeb;hb=5bc8c005a8e15c43285bc595a8d404de67a482ac;hp=6567df374fd7e9c4fefe3cc33f801c2fdf9084dc;hpb=9b49a5ed8551e46892af3f676e5d96d21b540e3c;p=oweals%2Fbusybox.git diff --git a/sysklogd/logread.c b/sysklogd/logread.c index 6567df374..ae0b2194b 100644 --- a/sysklogd/logread.c +++ b/sysklogd/logread.c @@ -6,9 +6,16 @@ * * Maintainer: Gennady Feldman as of Mar 12, 2001 * - * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + * Licensed under GPLv2 or later, see file LICENSE in this source tree. */ +//usage:#define logread_trivial_usage +//usage: "[-f]" +//usage:#define logread_full_usage "\n\n" +//usage: "Show messages in syslogd's circular buffer\n" +//usage: "\nOptions:" +//usage: "\n -f Output data as log grows" + #include "libbb.h" #include #include @@ -16,20 +23,34 @@ #define DEBUG 0 +/* our shared key (syslogd.c and logread.c must be in sync) */ enum { KEY_ID = 0x414e4547 }; /* "GENA" */ -static struct shbuf_ds { +struct shbuf_ds { int32_t size; // size of data - 1 int32_t tail; // end of message list char data[1]; // messages -} *shbuf; - -// 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 - - -static void error_exit(const char *str) ATTRIBUTE_NORETURN; +}; + +static const struct sembuf init_sem[3] = { + {0, -1, IPC_NOWAIT | SEM_UNDO}, + {1, 0}, {0, +1, SEM_UNDO} +}; + +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; +} FIX_ALIASING; +#define G (*(struct globals*)&bb_common_bufsiz1) +#define SMrup (G.SMrup) +#define SMrdn (G.SMrdn) +#define shbuf (G.shbuf) +#define INIT_G() do { \ + memcpy(SMrup, init_sem, sizeof(init_sem)); \ +} while (0) + +static void error_exit(const char *str) NORETURN; static void error_exit(const char *str) { //release all acquired resources @@ -46,21 +67,23 @@ static void sem_up(int semid) error_exit("semop[SMrup]"); } -static void interrupted(int sig ATTRIBUTE_UNUSED) +static void interrupted(int sig UNUSED_PARAM) { signal(SIGINT, SIG_IGN); shmdt(shbuf); - exit(0); + exit(EXIT_SUCCESS); } int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; -int logread_main(int argc, char **argv) +int logread_main(int argc UNUSED_PARAM, char **argv) { - int cur; + unsigned cur; int log_semid; /* ipc semaphore id */ int log_shmid; /* ipc shared memory id */ smallint follow = getopt32(argv, "f"); + INIT_G(); + log_shmid = shmget(KEY_ID, 0, 0); if (log_shmid == -1) bb_perror_msg_and_die("can't find syslogd buffer"); @@ -119,7 +142,7 @@ int logread_main(int argc, char **argv) } else { /* logread -f */ if (cur == shbuf_tail) { sem_up(log_semid); - fflush(stdout); + fflush_all(); sleep(1); /* TODO: replace me with a sleep_on */ continue; }