1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include "libbb.h"
20#include <sys/ipc.h>
21#include <sys/sem.h>
22#include <sys/shm.h>
23
24#define DEBUG 0
25
26
27enum { KEY_ID = 0x414e4547 };
28
29struct shbuf_ds {
30 int32_t size;
31 int32_t tail;
32 char data[1];
33};
34
35static const struct sembuf init_sem[3] = {
36 {0, -1, IPC_NOWAIT | SEM_UNDO},
37 {1, 0}, {0, +1, SEM_UNDO}
38};
39
40struct globals {
41 struct sembuf SMrup[1];
42 struct sembuf SMrdn[2];
43 struct shbuf_ds *shbuf;
44} FIX_ALIASING;
45#define G (*(struct globals*)&bb_common_bufsiz1)
46#define SMrup (G.SMrup)
47#define SMrdn (G.SMrdn)
48#define shbuf (G.shbuf)
49#define INIT_G() do { \
50 memcpy(SMrup, init_sem, sizeof(init_sem)); \
51} while (0)
52
53#if 0
54static void error_exit(const char *str) NORETURN;
55static void error_exit(const char *str)
56{
57
58 shmdt(shbuf);
59 bb_perror_msg_and_die(str);
60}
61#else
62
63# define error_exit(str) bb_perror_msg_and_die(str)
64#endif
65
66
67
68
69static void sem_up(int semid)
70{
71 if (semop(semid, SMrup, 1) == -1)
72 error_exit("semop[SMrup]");
73}
74
75static void interrupted(int sig)
76{
77
78 kill_myself_with_sig(sig);
79}
80
81int logread_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
82int logread_main(int argc UNUSED_PARAM, char **argv)
83{
84 unsigned cur;
85 int log_semid;
86 int log_shmid;
87 int follow = getopt32(argv, "fF");
88
89 INIT_G();
90
91 log_shmid = shmget(KEY_ID, 0, 0);
92 if (log_shmid == -1)
93 bb_perror_msg_and_die("can't %s syslogd buffer", "find");
94
95
96 shbuf = shmat(log_shmid, NULL, SHM_RDONLY);
97 if (shbuf == NULL)
98 bb_perror_msg_and_die("can't %s syslogd buffer", "access");
99
100 log_semid = semget(KEY_ID, 0, 0);
101 if (log_semid == -1)
102 error_exit("can't get access to semaphores for syslogd buffer");
103
104 bb_signals(BB_FATAL_SIGS, interrupted);
105
106
107
108 cur = shbuf->tail;
109
110
111 do {
112 unsigned shbuf_size;
113 unsigned shbuf_tail;
114 const char *shbuf_data;
115#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
116 int i;
117 int len_first_part;
118 int len_total = len_total;
119 char *copy = copy;
120#endif
121 if (semop(log_semid, SMrdn, 2) == -1)
122 error_exit("semop[SMrdn]");
123
124
125 shbuf_size = shbuf->size;
126 shbuf_tail = shbuf->tail;
127 shbuf_data = shbuf->data;
128
129 if (DEBUG)
130 printf("cur:%u tail:%u size:%u\n",
131 cur, shbuf_tail, shbuf_size);
132
133 if (!(follow & 1)) {
134
135
136
137 follow >>= 1;
138
139
140
141 cur += strlen(shbuf_data + cur);
142 if (cur >= shbuf_size) {
143 cur = strnlen(shbuf_data, shbuf_tail);
144 if (cur == shbuf_tail)
145 goto unlock;
146 }
147
148 cur++;
149 if (cur >= shbuf_size)
150 cur = 0;
151 } else {
152 if (cur == shbuf_tail) {
153 sem_up(log_semid);
154 fflush_all();
155 sleep(1);
156 continue;
157 }
158 }
159
160
161#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
162 len_first_part = len_total = shbuf_tail - cur;
163 if (len_total < 0) {
164
165
166
167 len_total += shbuf_size;
168 }
169 copy = xmalloc(len_total + 1);
170 if (len_first_part < 0) {
171
172 len_first_part = shbuf_size - cur;
173 memcpy(copy + len_first_part, shbuf_data, shbuf_tail);
174 }
175 memcpy(copy, shbuf_data + cur, len_first_part);
176 copy[len_total] = '\0';
177 cur = shbuf_tail;
178#else
179 while (cur != shbuf_tail) {
180 fputs(shbuf_data + cur, stdout);
181 cur += strlen(shbuf_data + cur) + 1;
182 if (cur >= shbuf_size)
183 cur = 0;
184 }
185#endif
186 unlock:
187
188 sem_up(log_semid);
189
190#if ENABLE_FEATURE_LOGREAD_REDUCED_LOCKING
191 for (i = 0; i < len_total; i += strlen(copy + i) + 1) {
192 fputs(copy + i, stdout);
193 }
194 free(copy);
195#endif
196 fflush_all();
197 } while (follow);
198
199
200
201 fflush_stdout_and_exit(EXIT_SUCCESS);
202}
203