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 source tree.
12 //usage:#define logread_trivial_usage
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"
25 /* our shared key (syslogd.c and logread.c must be in sync) */
26 enum { KEY_ID
= 0x414e4547 }; /* "GENA" */
29 int32_t size
; // size of data - 1
30 int32_t tail
; // end of message list
31 char data
[1]; // messages
34 static const struct sembuf init_sem
[3] = {
35 {0, -1, IPC_NOWAIT
| SEM_UNDO
},
36 {1, 0}, {0, +1, SEM_UNDO
}
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
;
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)); \
52 static void error_exit(const char *str
) NORETURN
;
53 static void error_exit(const char *str
)
55 //release all acquired resources
57 bb_perror_msg_and_die(str
);
61 * sem_up - up()'s a semaphore.
63 static void sem_up(int semid
)
65 if (semop(semid
, SMrup
, 1) == -1)
66 error_exit("semop[SMrup]");
69 static void interrupted(int sig UNUSED_PARAM
)
71 signal(SIGINT
, SIG_IGN
);
76 int logread_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
77 int logread_main(int argc UNUSED_PARAM
, char **argv
)
80 int log_semid
; /* ipc semaphore id */
81 int log_shmid
; /* ipc shared memory id */
82 smallint follow
= getopt32(argv
, "f");
86 log_shmid
= shmget(KEY_ID
, 0, 0);
88 bb_perror_msg_and_die("can't find syslogd buffer");
90 /* Attach shared memory to our char* */
91 shbuf
= shmat(log_shmid
, NULL
, SHM_RDONLY
);
93 bb_perror_msg_and_die("can't access syslogd buffer");
95 log_semid
= semget(KEY_ID
, 0, 0);
97 error_exit("can't get access to semaphores for syslogd buffer");
99 signal(SIGINT
, interrupted
);
101 /* Suppose atomic memory read */
102 /* Max possible value for tail is shbuf->size - 1 */
105 /* Loop for logread -f, one pass if there was no -f */
109 const char *shbuf_data
;
110 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
113 int len_total
= len_total
; /* for gcc */
114 char *copy
= copy
; /* for gcc */
116 if (semop(log_semid
, SMrdn
, 2) == -1)
117 error_exit("semop[SMrdn]");
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! */
125 printf("cur:%d tail:%i size:%i\n",
126 cur
, shbuf_tail
, shbuf_size
);
129 /* advance to oldest complete message */
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 */
137 /* advance to first byte of the message */
139 if (cur
>= shbuf_size
) /* last byte in buffer? */
141 } else { /* logread -f */
142 if (cur
== shbuf_tail
) {
145 sleep(1); /* TODO: replace me with a sleep_on */
150 /* Read from cur to tail */
151 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
152 len_first_part
= len_total
= shbuf_tail
- cur
;
155 /* [SECOND PART.........FIRST PART] */
156 /* ^data ^tail ^cur ^size */
157 len_total
+= shbuf_size
;
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
);
165 memcpy(copy
, shbuf_data
+ cur
, len_first_part
);
166 copy
[len_total
] = '\0';
169 while (cur
!= shbuf_tail
) {
170 fputs(shbuf_data
+ cur
, stdout
);
171 cur
+= strlen(shbuf_data
+ cur
) + 1;
172 if (cur
>= shbuf_size
)
177 /* release the lock on the log chain */
180 #if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
181 for (i
= 0; i
< len_total
; i
+= strlen(copy
+ i
) + 1) {
182 fputs(copy
+ i
, stdout
);
190 fflush_stdout_and_exit(EXIT_SUCCESS
);