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