busybox/sysklogd/logread.c
<<
>>
Prefs
   1/* vi: set sw=4 ts=4: */
   2/*
   3 * circular buffer syslog implementation for busybox
   4 *
   5 * Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
   6 *
   7 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
   8 *
   9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  10 */
  11
  12//usage:#define logread_trivial_usage
  13//usage:       "[-f]"
  14//usage:#define logread_full_usage "\n\n"
  15//usage:       "Show messages in syslogd's circular buffer\n"
  16//usage:     "\n        -f      Output data as log grows"
  17
  18#include "libbb.h"
  19#include <sys/ipc.h>
  20#include <sys/sem.h>
  21#include <sys/shm.h>
  22
  23#define DEBUG 0
  24
  25/* our shared key (syslogd.c and logread.c must be in sync) */
  26enum { KEY_ID = 0x414e4547 }; /* "GENA" */
  27
  28struct shbuf_ds {
  29        int32_t size;           // size of data - 1
  30        int32_t tail;           // end of message list
  31        char data[1];           // messages
  32};
  33
  34static const struct sembuf init_sem[3] = {
  35        {0, -1, IPC_NOWAIT | SEM_UNDO},
  36        {1, 0}, {0, +1, SEM_UNDO}
  37};
  38
  39struct globals {
  40        struct sembuf SMrup[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
  41        struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
  42        struct shbuf_ds *shbuf;
  43} FIX_ALIASING;
  44#define G (*(struct globals*)&bb_common_bufsiz1)
  45#define SMrup (G.SMrup)
  46#define SMrdn (G.SMrdn)
  47#define shbuf (G.shbuf)
  48#define INIT_G() do { \
  49        memcpy(SMrup, init_sem, sizeof(init_sem)); \
  50} while (0)
  51
  52static void error_exit(const char *str) NORETURN;
  53static void error_exit(const char *str)
  54{
  55        //release all acquired resources
  56        shmdt(shbuf);
  57        bb_perror_msg_and_die(str);
  58}
  59
  60/*
  61 * sem_up - up()'s a semaphore.
  62 */
  63static void sem_up(int semid)
  64{
  65        if (semop(semid, SMrup, 1) == -1)
  66                error_exit("semop[SMrup]");
  67}
  68
  69static void interrupted(int sig UNUSED_PARAM)
  70{
  71        signal(SIGINT, SIG_IGN);
  72        shmdt(shbuf);
  73        exit(EXIT_SUCCESS);
  74}
  75
  76int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  77int logread_main(int argc UNUSED_PARAM, char **argv)
  78{
  79        unsigned cur;
  80        int log_semid; /* ipc semaphore id */
  81        int log_shmid; /* ipc shared memory id */
  82        smallint follow = getopt32(argv, "f");
  83
  84        INIT_G();
  85
  86        log_shmid = shmget(KEY_ID, 0, 0);
  87        if (log_shmid == -1)
  88                bb_perror_msg_and_die("can't find syslogd buffer");
  89
  90        /* Attach shared memory to our char* */
  91        shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
  92        if (shbuf == NULL)
  93                bb_perror_msg_and_die("can't access syslogd buffer");
  94
  95        log_semid = semget(KEY_ID, 0, 0);
  96        if (log_semid == -1)
  97                error_exit("can't get access to semaphores for syslogd buffer");
  98
  99        signal(SIGINT, interrupted);
 100
 101        /* Suppose atomic memory read */
 102        /* Max possible value for tail is shbuf->size - 1 */
 103        cur = shbuf->tail;
 104
 105        /* Loop for logread -f, one pass if there was no -f */
 106        do {
 107                unsigned shbuf_size;
 108                unsigned shbuf_tail;
 109                const char *shbuf_data;
 110#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
 111                int i;
 112                int len_first_part;
 113                int len_total = len_total; /* for gcc */
 114                char *copy = copy; /* for gcc */
 115#endif
 116                if (semop(log_semid, SMrdn, 2) == -1)
 117                        error_exit("semop[SMrdn]");
 118
 119                /* Copy the info, helps gcc to realize that it doesn't change */
 120                shbuf_size = shbuf->size;
 121                shbuf_tail = shbuf->tail;
 122                shbuf_data = shbuf->data; /* pointer! */
 123
 124                if (DEBUG)
 125                        printf("cur:%d tail:%i size:%i\n",
 126                                        cur, shbuf_tail, shbuf_size);
 127
 128                if (!follow) {
 129                        /* advance to oldest complete message */
 130                        /* find NUL */
 131                        cur += strlen(shbuf_data + cur);
 132                        if (cur >= shbuf_size) { /* last byte in buffer? */
 133                                cur = strnlen(shbuf_data, shbuf_tail);
 134                                if (cur == shbuf_tail)
 135                                        goto unlock; /* no complete messages */
 136                        }
 137                        /* advance to first byte of the message */
 138                        cur++;
 139                        if (cur >= shbuf_size) /* last byte in buffer? */
 140                                cur = 0;
 141                } else { /* logread -f */
 142                        if (cur == shbuf_tail) {
 143                                sem_up(log_semid);
 144                                fflush_all();
 145                                sleep(1); /* TODO: replace me with a sleep_on */
 146                                continue;
 147                        }
 148                }
 149
 150                /* Read from cur to tail */
 151#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
 152                len_first_part = len_total = shbuf_tail - cur;
 153                if (len_total < 0) {
 154                        /* message wraps: */
 155                        /* [SECOND PART.........FIRST PART] */
 156                        /*  ^data      ^tail    ^cur      ^size */
 157                        len_total += shbuf_size;
 158                }
 159                copy = xmalloc(len_total + 1);
 160                if (len_first_part < 0) {
 161                        /* message wraps (see above) */
 162                        len_first_part = shbuf_size - cur;
 163                        memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
 164                }
 165                memcpy(copy, shbuf_data + cur, len_first_part);
 166                copy[len_total] = '\0';
 167                cur = shbuf_tail;
 168#else
 169                while (cur != shbuf_tail) {
 170                        fputs(shbuf_data + cur, stdout);
 171                        cur += strlen(shbuf_data + cur) + 1;
 172                        if (cur >= shbuf_size)
 173                                cur = 0;
 174                }
 175#endif
 176 unlock:
 177                /* release the lock on the log chain */
 178                sem_up(log_semid);
 179
 180#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
 181                for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
 182                        fputs(copy + i, stdout);
 183                }
 184                free(copy);
 185#endif
 186        } while (follow);
 187
 188        shmdt(shbuf);
 189
 190        fflush_stdout_and_exit(EXIT_SUCCESS);
 191}
 192