1 /* vi: set sw=4 ts=4: */
3 * circular buffer syslog implementation for busybox
5 * Copyright (C) 2000 by Gennady Feldman <gfeldman@gena01.com>
7 * Maintainer: Gennady Feldman <gfeldman@gena01.com> as of Mar 12, 2001
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
19 enum { KEY_ID = 0x414e4547 }; /* "GENA" */
21 static struct shbuf_ds {
22 int32_t size; // size of data - 1
23 int32_t tail; // end of message list
24 char data[1]; // messages
27 // Semaphore operation structures
28 static struct sembuf SMrup[1] = {{0, -1, IPC_NOWAIT | SEM_UNDO}}; // set SMrup
29 static struct sembuf SMrdn[2] = {{1, 0}, {0, +1, SEM_UNDO}}; // set SMrdn
32 static void error_exit(const char *str) ATTRIBUTE_NORETURN;
33 static void error_exit(const char *str)
35 //release all acquired resources
37 bb_perror_msg_and_die(str);
41 * sem_up - up()'s a semaphore.
43 static void sem_up(int semid)
45 if (semop(semid, SMrup, 1) == -1)
46 error_exit("semop[SMrup]");
49 static void interrupted(int sig ATTRIBUTE_UNUSED)
51 signal(SIGINT, SIG_IGN);
56 int logread_main(int argc, char **argv);
57 int logread_main(int argc, char **argv)
60 int log_semid; /* ipc semaphore id */
61 int log_shmid; /* ipc shared memory id */
62 smallint follow = getopt32(argv, "f");
64 log_shmid = shmget(KEY_ID, 0, 0);
66 bb_perror_msg_and_die("can't find syslogd buffer");
68 /* Attach shared memory to our char* */
69 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
71 bb_perror_msg_and_die("can't access syslogd buffer");
73 log_semid = semget(KEY_ID, 0, 0);
75 error_exit("can't get access to semaphores for syslogd buffer");
77 signal(SIGINT, interrupted);
79 /* Suppose atomic memory read */
80 /* Max possible value for tail is shbuf->size - 1 */
83 /* Loop for logread -f, one pass if there was no -f */
87 const char *shbuf_data;
88 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
91 int len_total = len_total; /* for gcc */
92 char *copy = copy; /* for gcc */
94 if (semop(log_semid, SMrdn, 2) == -1)
95 error_exit("semop[SMrdn]");
97 /* Copy the info, helps gcc to realize that it doesn't change */
98 shbuf_size = shbuf->size;
99 shbuf_tail = shbuf->tail;
100 shbuf_data = shbuf->data; /* pointer! */
103 printf("cur:%d tail:%i size:%i\n",
104 cur, shbuf_tail, shbuf_size);
107 /* advance to oldest complete message */
109 cur += strlen(shbuf_data + cur);
110 if (cur >= shbuf_size) { /* last byte in buffer? */
111 cur = strnlen(shbuf_data, shbuf_tail);
112 if (cur == shbuf_tail)
113 goto unlock; /* no complete messages */
115 /* advance to first byte of the message */
117 if (cur >= shbuf_size) /* last byte in buffer? */
119 } else { /* logread -f */
120 if (cur == shbuf_tail) {
123 sleep(1); /* TODO: replace me with a sleep_on */
128 /* Read from cur to tail */
129 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
130 len_first_part = len_total = shbuf_tail - cur;
133 /* [SECOND PART.........FIRST PART] */
134 /* ^data ^tail ^cur ^size */
135 len_total += shbuf_size;
137 copy = xmalloc(len_total + 1);
138 if (len_first_part < 0) {
139 /* message wraps (see above) */
140 len_first_part = shbuf_size - cur;
141 memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
143 memcpy(copy, shbuf_data + cur, len_first_part);
144 copy[len_total] = '\0';
147 while (cur != shbuf_tail) {
148 fputs(shbuf_data + cur, stdout);
149 cur += strlen(shbuf_data + cur) + 1;
150 if (cur >= shbuf_size)
155 /* release the lock on the log chain */
158 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
159 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
160 fputs(copy + i, stdout);
168 fflush_stdout_and_exit(EXIT_SUCCESS);