source: MondoRescue/branches/3.2/mindi-busybox/sysklogd/logread.c@ 3232

Last change on this file since 3232 was 3232, checked in by Bruno Cornec, 10 years ago
  • Update mindi-busybox to 1.21.1
File size: 4.7 KB
RevLine 
[821]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 *
[2725]9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
[821]10 */
11
[3232]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
[1765]18#include "libbb.h"
[821]19#include <sys/ipc.h>
20#include <sys/sem.h>
21#include <sys/shm.h>
22
[1765]23#define DEBUG 0
[821]24
[2725]25/* our shared key (syslogd.c and logread.c must be in sync) */
[1765]26enum { KEY_ID = 0x414e4547 }; /* "GENA" */
27
[2725]28struct shbuf_ds {
[1765]29 int32_t size; // size of data - 1
30 int32_t tail; // end of message list
31 char data[1]; // messages
[2725]32};
[821]33
[2725]34static const struct sembuf init_sem[3] = {
35 {0, -1, IPC_NOWAIT | SEM_UNDO},
36 {1, 0}, {0, +1, SEM_UNDO}
37};
[821]38
[2725]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)
[821]51
[2725]52static void error_exit(const char *str) NORETURN;
[1765]53static void error_exit(const char *str)
54{
55 //release all acquired resources
56 shmdt(shbuf);
57 bb_perror_msg_and_die(str);
58}
[821]59
60/*
61 * sem_up - up()'s a semaphore.
62 */
[1765]63static void sem_up(int semid)
[821]64{
[1765]65 if (semop(semid, SMrup, 1) == -1)
[821]66 error_exit("semop[SMrup]");
67}
68
[2725]69static void interrupted(int sig UNUSED_PARAM)
[821]70{
[1765]71 signal(SIGINT, SIG_IGN);
72 shmdt(shbuf);
[2725]73 exit(EXIT_SUCCESS);
[821]74}
75
[2725]76int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
77int logread_main(int argc UNUSED_PARAM, char **argv)
[821]78{
[2725]79 unsigned cur;
[1765]80 int log_semid; /* ipc semaphore id */
81 int log_shmid; /* ipc shared memory id */
82 smallint follow = getopt32(argv, "f");
[821]83
[2725]84 INIT_G();
85
[1765]86 log_shmid = shmget(KEY_ID, 0, 0);
87 if (log_shmid == -1)
88 bb_perror_msg_and_die("can't find syslogd buffer");
[821]89
[1765]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");
[821]94
[1765]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
[821]99 signal(SIGINT, interrupted);
100
[1765]101 /* Suppose atomic memory read */
102 /* Max possible value for tail is shbuf->size - 1 */
103 cur = shbuf->tail;
[821]104
[1765]105 /* Loop for logread -f, one pass if there was no -f */
[821]106 do {
[1765]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 */
[821]115#endif
[1765]116 if (semop(log_semid, SMrdn, 2) == -1)
117 error_exit("semop[SMrdn]");
[821]118
[1765]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! */
[821]123
[1765]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) {
[821]143 sem_up(log_semid);
[2725]144 fflush_all();
[1765]145 sleep(1); /* TODO: replace me with a sleep_on */
[821]146 continue;
147 }
148 }
149
[1765]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;
[821]158 }
[1765]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;
[821]168#else
[1765]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;
[821]174 }
175#endif
[1765]176 unlock:
177 /* release the lock on the log chain */
[821]178 sem_up(log_semid);
179
[1765]180#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
181 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
182 fputs(copy + i, stdout);
[821]183 }
[1765]184 free(copy);
[821]185#endif
186 } while (follow);
187
[1765]188 shmdt(shbuf);
[821]189
[1765]190 fflush_stdout_and_exit(EXIT_SUCCESS);
[821]191}
Note: See TracBrowser for help on using the repository browser.