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" */
22 int32_t size; // size of data - 1
23 int32_t tail; // end of message list
24 char data[1]; // messages
27 static const struct sembuf init_sem[3] = {
28 {0, -1, IPC_NOWAIT | SEM_UNDO},
29 {1, 0}, {0, +1, SEM_UNDO}
33 struct sembuf SMrup[1]; // {0, -1, IPC_NOWAIT | SEM_UNDO},
34 struct sembuf SMrdn[2]; // {1, 0}, {0, +1, SEM_UNDO}
35 struct shbuf_ds *shbuf;
37 #define G (*(struct globals*)&bb_common_bufsiz1)
38 #define SMrup (G.SMrup)
39 #define SMrdn (G.SMrdn)
40 #define shbuf (G.shbuf)
43 memcpy(SMrup, init_sem, sizeof(init_sem)); \
46 static void error_exit(const char *str) ATTRIBUTE_NORETURN;
47 static void error_exit(const char *str)
49 //release all acquired resources
51 bb_perror_msg_and_die(str);
55 * sem_up - up()'s a semaphore.
57 static void sem_up(int semid)
59 if (semop(semid, SMrup, 1) == -1)
60 error_exit("semop[SMrup]");
63 static void interrupted(int sig ATTRIBUTE_UNUSED)
65 signal(SIGINT, SIG_IGN);
70 int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
71 int logread_main(int argc, char **argv)
74 int log_semid; /* ipc semaphore id */
75 int log_shmid; /* ipc shared memory id */
76 smallint follow = getopt32(argv, "f");
78 log_shmid = shmget(KEY_ID, 0, 0);
80 bb_perror_msg_and_die("can't find syslogd buffer");
82 /* Attach shared memory to our char* */
83 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
85 bb_perror_msg_and_die("can't access syslogd buffer");
87 log_semid = semget(KEY_ID, 0, 0);
89 error_exit("can't get access to semaphores for syslogd buffer");
91 signal(SIGINT, interrupted);
93 /* Suppose atomic memory read */
94 /* Max possible value for tail is shbuf->size - 1 */
97 /* Loop for logread -f, one pass if there was no -f */
101 const char *shbuf_data;
102 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
105 int len_total = len_total; /* for gcc */
106 char *copy = copy; /* for gcc */
108 if (semop(log_semid, SMrdn, 2) == -1)
109 error_exit("semop[SMrdn]");
111 /* Copy the info, helps gcc to realize that it doesn't change */
112 shbuf_size = shbuf->size;
113 shbuf_tail = shbuf->tail;
114 shbuf_data = shbuf->data; /* pointer! */
117 printf("cur:%d tail:%i size:%i\n",
118 cur, shbuf_tail, shbuf_size);
121 /* advance to oldest complete message */
123 cur += strlen(shbuf_data + cur);
124 if (cur >= shbuf_size) { /* last byte in buffer? */
125 cur = strnlen(shbuf_data, shbuf_tail);
126 if (cur == shbuf_tail)
127 goto unlock; /* no complete messages */
129 /* advance to first byte of the message */
131 if (cur >= shbuf_size) /* last byte in buffer? */
133 } else { /* logread -f */
134 if (cur == shbuf_tail) {
137 sleep(1); /* TODO: replace me with a sleep_on */
142 /* Read from cur to tail */
143 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
144 len_first_part = len_total = shbuf_tail - cur;
147 /* [SECOND PART.........FIRST PART] */
148 /* ^data ^tail ^cur ^size */
149 len_total += shbuf_size;
151 copy = xmalloc(len_total + 1);
152 if (len_first_part < 0) {
153 /* message wraps (see above) */
154 len_first_part = shbuf_size - cur;
155 memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
157 memcpy(copy, shbuf_data + cur, len_first_part);
158 copy[len_total] = '\0';
161 while (cur != shbuf_tail) {
162 fputs(shbuf_data + cur, stdout);
163 cur += strlen(shbuf_data + cur) + 1;
164 if (cur >= shbuf_size)
169 /* release the lock on the log chain */
172 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
173 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
174 fputs(copy + i, stdout);
182 fflush_stdout_and_exit(EXIT_SUCCESS);