diff options
author | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-03-15 19:50:46 +0000 |
---|---|---|
committer | vda <vda@69ca8d6d-28ef-0310-b511-8ec308f3f277> | 2007-03-15 19:50:46 +0000 |
commit | ca274d1bbe798fbf3950c31e92c9f33d29cef48c (patch) | |
tree | 4a5dad014c464885001faaaa10dd0db4c50e67cd | |
parent | d6361d08a794e0e761dc06ea330eb7113496d72c (diff) | |
download | busybox-w32-ca274d1bbe798fbf3950c31e92c9f33d29cef48c.tar.gz busybox-w32-ca274d1bbe798fbf3950c31e92c9f33d29cef48c.tar.bz2 busybox-w32-ca274d1bbe798fbf3950c31e92c9f33d29cef48c.zip |
syslogd: reduce data/bss usage. Code size is practically the same.
git-svn-id: svn://busybox.net/trunk/busybox@18122 69ca8d6d-28ef-0310-b511-8ec308f3f277
-rw-r--r-- | sysklogd/syslogd.c | 293 |
1 files changed, 167 insertions, 126 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index 53cb4ba5e..8ae75c974 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c | |||
@@ -17,46 +17,106 @@ | |||
17 | #include <paths.h> | 17 | #include <paths.h> |
18 | #include <sys/un.h> | 18 | #include <sys/un.h> |
19 | 19 | ||
20 | /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */ | 20 | /* SYSLOG_NAMES defined to pull some extra junk from syslog.h: */ |
21 | /* prioritynames[] and facilitynames[]. uclibc pulls those in _rwdata_! :( */ | ||
22 | |||
21 | #define SYSLOG_NAMES | 23 | #define SYSLOG_NAMES |
22 | #include <sys/syslog.h> | 24 | #include <sys/syslog.h> |
23 | #include <sys/uio.h> | 25 | #include <sys/uio.h> |
24 | 26 | ||
27 | #if ENABLE_FEATURE_REMOTE_LOG | ||
28 | #include <netinet/in.h> | ||
29 | #endif | ||
30 | |||
31 | #if ENABLE_FEATURE_IPC_SYSLOG | ||
32 | #include <sys/ipc.h> | ||
33 | #include <sys/sem.h> | ||
34 | #include <sys/shm.h> | ||
35 | #endif | ||
36 | |||
37 | |||
25 | #define DEBUG 0 | 38 | #define DEBUG 0 |
26 | 39 | ||
27 | /* Path for the file where all log messages are written */ | 40 | // Semaphore operation structures |
28 | static const char *logFilePath = "/var/log/messages"; | 41 | struct shbuf_ds { |
29 | static int logFD = -1; | 42 | int32_t size; // size of data written |
43 | int32_t head; // start of message list | ||
44 | int32_t tail; // end of message list | ||
45 | char data[1]; // data/messages | ||
46 | }; // shared memory pointer | ||
47 | |||
48 | struct globals { | ||
49 | |||
50 | const char *logFilePath; | ||
51 | int logFD; | ||
30 | 52 | ||
31 | /* This is not very useful, is bloat, and broken: | 53 | /* This is not very useful, is bloat, and broken: |
32 | * can deadlock if alarmed to make MARK while writing to IPC buffer | 54 | * can deadlock if alarmed to make MARK while writing to IPC buffer |
33 | * (semaphores are down but do_mark routine tries to down them again) */ | 55 | * (semaphores are down but do_mark routine tries to down them again) */ |
34 | #ifdef SYSLOGD_MARK | 56 | #ifdef SYSLOGD_MARK |
35 | /* interval between marks in seconds */ | 57 | /* interval between marks in seconds */ |
36 | static int markInterval = 20 * 60; | 58 | int markInterval; |
37 | #endif | 59 | #endif |
38 | 60 | ||
39 | /* level of messages to be locally logged */ | 61 | /* level of messages to be locally logged */ |
40 | static int logLevel = 8; | 62 | int logLevel; |
41 | |||
42 | /* localhost's name */ | ||
43 | static char localHostName[64]; | ||
44 | 63 | ||
45 | #if ENABLE_FEATURE_ROTATE_LOGFILE | 64 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
46 | /* max size of message file before being rotated */ | 65 | /* max size of message file before being rotated */ |
47 | static unsigned logFileSize = 200 * 1024; | 66 | unsigned logFileSize; |
48 | /* number of rotated message files */ | 67 | /* number of rotated message files */ |
49 | static unsigned logFileRotate = 1; | 68 | unsigned logFileRotate; |
50 | static unsigned curFileSize; | 69 | unsigned curFileSize; |
51 | static smallint isRegular; | 70 | smallint isRegular; |
52 | #endif | 71 | #endif |
53 | 72 | ||
54 | #if ENABLE_FEATURE_REMOTE_LOG | 73 | #if ENABLE_FEATURE_REMOTE_LOG |
55 | #include <netinet/in.h> | 74 | /* udp socket for logging to remote host */ |
56 | /* udp socket for logging to remote host */ | 75 | int remoteFD; |
57 | static int remoteFD = -1; | 76 | len_and_sockaddr* remoteAddr; |
58 | static len_and_sockaddr* remoteAddr; | 77 | #endif |
78 | |||
79 | #if ENABLE_FEATURE_IPC_SYSLOG | ||
80 | int shmid; // ipc shared memory id | ||
81 | int s_semid; // ipc semaphore id | ||
82 | int shm_size; | ||
83 | struct sembuf SMwup[1]; | ||
84 | struct sembuf SMwdn[3]; | ||
85 | struct shbuf_ds *shbuf; | ||
86 | #endif | ||
87 | |||
88 | time_t last_log_time; | ||
89 | /* localhost's name */ | ||
90 | char localHostName[64]; | ||
91 | |||
92 | }; /* struct globals */ | ||
93 | |||
94 | static const struct globals init_globals = { | ||
95 | .logFilePath = "/var/log/messages", | ||
96 | .logFD = -1, | ||
97 | #ifdef SYSLOGD_MARK | ||
98 | .markInterval = 20 * 60, | ||
99 | #endif | ||
100 | .logLevel = 8, | ||
101 | #if ENABLE_FEATURE_ROTATE_LOGFILE | ||
102 | .logFileSize = 200 * 1024, | ||
103 | .logFileRotate = 1, | ||
104 | #endif | ||
105 | #if ENABLE_FEATURE_REMOTE_LOG | ||
106 | .remoteFD = -1, | ||
59 | #endif | 107 | #endif |
108 | #if ENABLE_FEATURE_IPC_SYSLOG | ||
109 | .shmid = -1, | ||
110 | .s_semid = -1, | ||
111 | .shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024), // default shm size | ||
112 | .SMwup = { {1, -1, IPC_NOWAIT} }, | ||
113 | .SMwdn = { {0, 0}, {1, 0}, {1, +1} }, | ||
114 | #endif | ||
115 | // FIXME: hidden tail with lotsa zeroes is here.... | ||
116 | }; | ||
117 | |||
118 | #define G (*ptr_to_globals) | ||
119 | |||
60 | 120 | ||
61 | /* We are using bb_common_bufsiz1 for buffering: */ | 121 | /* We are using bb_common_bufsiz1 for buffering: */ |
62 | enum { MAX_READ = (BUFSIZ/6) & ~0xf }; | 122 | enum { MAX_READ = (BUFSIZ/6) & ~0xf }; |
@@ -109,7 +169,7 @@ enum { | |||
109 | USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \ | 169 | USE_FEATURE_ROTATE_LOGFILE(,*opt_b) \ |
110 | USE_FEATURE_REMOTE_LOG( ,*opt_R) \ | 170 | USE_FEATURE_REMOTE_LOG( ,*opt_R) \ |
111 | USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL) | 171 | USE_FEATURE_IPC_SYSLOG( ,*opt_C = NULL) |
112 | #define OPTION_PARAM &opt_m, &logFilePath, &opt_l \ | 172 | #define OPTION_PARAM &opt_m, &G.logFilePath, &opt_l \ |
113 | USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \ | 173 | USE_FEATURE_ROTATE_LOGFILE(,&opt_s) \ |
114 | USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \ | 174 | USE_FEATURE_ROTATE_LOGFILE(,&opt_b) \ |
115 | USE_FEATURE_REMOTE_LOG( ,&opt_R) \ | 175 | USE_FEATURE_REMOTE_LOG( ,&opt_R) \ |
@@ -119,68 +179,51 @@ enum { | |||
119 | /* circular buffer variables/structures */ | 179 | /* circular buffer variables/structures */ |
120 | #if ENABLE_FEATURE_IPC_SYSLOG | 180 | #if ENABLE_FEATURE_IPC_SYSLOG |
121 | 181 | ||
122 | |||
123 | #if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4 | 182 | #if CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE < 4 |
124 | #error Sorry, you must set the syslogd buffer size to at least 4KB. | 183 | #error Sorry, you must set the syslogd buffer size to at least 4KB. |
125 | #error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE | 184 | #error Please check CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE |
126 | #endif | 185 | #endif |
127 | 186 | ||
128 | #include <sys/ipc.h> | ||
129 | #include <sys/sem.h> | ||
130 | #include <sys/shm.h> | ||
131 | |||
132 | /* our shared key */ | 187 | /* our shared key */ |
133 | #define KEY_ID ((long)0x414e4547) /* "GENA" */ | 188 | #define KEY_ID ((long)0x414e4547) /* "GENA" */ |
134 | 189 | ||
135 | // Semaphore operation structures | ||
136 | static struct shbuf_ds { | ||
137 | int32_t size; // size of data written | ||
138 | int32_t head; // start of message list | ||
139 | int32_t tail; // end of message list | ||
140 | char data[1]; // data/messages | ||
141 | } *shbuf; // shared memory pointer | ||
142 | |||
143 | static int shmid = -1; // ipc shared memory id | ||
144 | static int s_semid = -1; // ipc semaphore id | ||
145 | static int shm_size = ((CONFIG_FEATURE_IPC_SYSLOG_BUFFER_SIZE)*1024); // default shm size | ||
146 | |||
147 | static void ipcsyslog_cleanup(void) | 190 | static void ipcsyslog_cleanup(void) |
148 | { | 191 | { |
149 | if (shmid != -1) { | 192 | if (G.shmid != -1) { |
150 | shmdt(shbuf); | 193 | shmdt(G.shbuf); |
151 | } | 194 | } |
152 | if (shmid != -1) { | 195 | if (G.shmid != -1) { |
153 | shmctl(shmid, IPC_RMID, NULL); | 196 | shmctl(G.shmid, IPC_RMID, NULL); |
154 | } | 197 | } |
155 | if (s_semid != -1) { | 198 | if (G.s_semid != -1) { |
156 | semctl(s_semid, 0, IPC_RMID, 0); | 199 | semctl(G.s_semid, 0, IPC_RMID, 0); |
157 | } | 200 | } |
158 | } | 201 | } |
159 | 202 | ||
160 | static void ipcsyslog_init(void) | 203 | static void ipcsyslog_init(void) |
161 | { | 204 | { |
162 | if (DEBUG) | 205 | if (DEBUG) |
163 | printf("shmget(%lx, %d,...)\n", KEY_ID, shm_size); | 206 | printf("shmget(%lx, %d,...)\n", KEY_ID, G.shm_size); |
164 | 207 | ||
165 | shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023); | 208 | G.shmid = shmget(KEY_ID, G.shm_size, IPC_CREAT | 1023); |
166 | if (shmid == -1) { | 209 | if (G.shmid == -1) { |
167 | bb_perror_msg_and_die("shmget"); | 210 | bb_perror_msg_and_die("shmget"); |
168 | } | 211 | } |
169 | 212 | ||
170 | shbuf = shmat(shmid, NULL, 0); | 213 | G.shbuf = shmat(G.shmid, NULL, 0); |
171 | if (!shbuf) { | 214 | if (!G.shbuf) { |
172 | bb_perror_msg_and_die("shmat"); | 215 | bb_perror_msg_and_die("shmat"); |
173 | } | 216 | } |
174 | 217 | ||
175 | shbuf->size = shm_size - offsetof(struct shbuf_ds, data); | 218 | G.shbuf->size = G.shm_size - offsetof(struct shbuf_ds, data); |
176 | shbuf->head = shbuf->tail = 0; | 219 | G.shbuf->head = G.shbuf->tail = 0; |
177 | 220 | ||
178 | // we'll trust the OS to set initial semval to 0 (let's hope) | 221 | // we'll trust the OS to set initial semval to 0 (let's hope) |
179 | s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023); | 222 | G.s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023); |
180 | if (s_semid == -1) { | 223 | if (G.s_semid == -1) { |
181 | if (errno == EEXIST) { | 224 | if (errno == EEXIST) { |
182 | s_semid = semget(KEY_ID, 2, 0); | 225 | G.s_semid = semget(KEY_ID, 2, 0); |
183 | if (s_semid != -1) | 226 | if (G.s_semid != -1) |
184 | return; | 227 | return; |
185 | } | 228 | } |
186 | bb_perror_msg_and_die("semget"); | 229 | bb_perror_msg_and_die("semget"); |
@@ -190,14 +233,10 @@ static void ipcsyslog_init(void) | |||
190 | /* Write message to shared mem buffer */ | 233 | /* Write message to shared mem buffer */ |
191 | static void log_to_shmem(const char *msg, int len) | 234 | static void log_to_shmem(const char *msg, int len) |
192 | { | 235 | { |
193 | /* Why libc insists on these being rw? */ | ||
194 | static struct sembuf SMwup[1] = { {1, -1, IPC_NOWAIT} }; | ||
195 | static struct sembuf SMwdn[3] = { {0, 0}, {1, 0}, {1, +1} }; | ||
196 | |||
197 | int old_tail, new_tail; | 236 | int old_tail, new_tail; |
198 | char *c; | 237 | char *c; |
199 | 238 | ||
200 | if (semop(s_semid, SMwdn, 3) == -1) { | 239 | if (semop(G.s_semid, G.SMwdn, 3) == -1) { |
201 | bb_perror_msg_and_die("SMwdn"); | 240 | bb_perror_msg_and_die("SMwdn"); |
202 | } | 241 | } |
203 | 242 | ||
@@ -212,53 +251,53 @@ static void log_to_shmem(const char *msg, int len) | |||
212 | */ | 251 | */ |
213 | len++; /* length with NUL included */ | 252 | len++; /* length with NUL included */ |
214 | again: | 253 | again: |
215 | old_tail = shbuf->tail; | 254 | old_tail = G.shbuf->tail; |
216 | new_tail = old_tail + len; | 255 | new_tail = old_tail + len; |
217 | if (new_tail < shbuf->size) { | 256 | if (new_tail < G.shbuf->size) { |
218 | /* No need to move head if shbuf->head <= old_tail, | 257 | /* No need to move head if shbuf->head <= old_tail, |
219 | * else... */ | 258 | * else... */ |
220 | if (old_tail < shbuf->head && shbuf->head <= new_tail) { | 259 | if (old_tail < G.shbuf->head && G.shbuf->head <= new_tail) { |
221 | /* ...need to move head forward */ | 260 | /* ...need to move head forward */ |
222 | c = memchr(shbuf->data + new_tail, '\0', | 261 | c = memchr(G.shbuf->data + new_tail, '\0', |
223 | shbuf->size - new_tail); | 262 | G.shbuf->size - new_tail); |
224 | if (!c) /* no NUL ahead of us, wrap around */ | 263 | if (!c) /* no NUL ahead of us, wrap around */ |
225 | c = memchr(shbuf->data, '\0', old_tail); | 264 | c = memchr(G.shbuf->data, '\0', old_tail); |
226 | if (!c) { /* still nothing? point to this msg... */ | 265 | if (!c) { /* still nothing? point to this msg... */ |
227 | shbuf->head = old_tail; | 266 | G.shbuf->head = old_tail; |
228 | } else { | 267 | } else { |
229 | /* convert pointer to offset + skip NUL */ | 268 | /* convert pointer to offset + skip NUL */ |
230 | shbuf->head = c - shbuf->data + 1; | 269 | G.shbuf->head = c - G.shbuf->data + 1; |
231 | } | 270 | } |
232 | } | 271 | } |
233 | /* store message, set new tail */ | 272 | /* store message, set new tail */ |
234 | memcpy(shbuf->data + old_tail, msg, len); | 273 | memcpy(G.shbuf->data + old_tail, msg, len); |
235 | shbuf->tail = new_tail; | 274 | G.shbuf->tail = new_tail; |
236 | } else { | 275 | } else { |
237 | /* we need to break up the message and wrap it around */ | 276 | /* we need to break up the message and wrap it around */ |
238 | /* k == available buffer space ahead of old tail */ | 277 | /* k == available buffer space ahead of old tail */ |
239 | int k = shbuf->size - old_tail - 1; | 278 | int k = G.shbuf->size - old_tail - 1; |
240 | if (shbuf->head > old_tail) { | 279 | if (G.shbuf->head > old_tail) { |
241 | /* we are going to overwrite head, need to | 280 | /* we are going to overwrite head, need to |
242 | * move it out of the way */ | 281 | * move it out of the way */ |
243 | c = memchr(shbuf->data, '\0', old_tail); | 282 | c = memchr(G.shbuf->data, '\0', old_tail); |
244 | if (!c) { /* nothing? point to this msg... */ | 283 | if (!c) { /* nothing? point to this msg... */ |
245 | shbuf->head = old_tail; | 284 | G.shbuf->head = old_tail; |
246 | } else { /* convert pointer to offset + skip NUL */ | 285 | } else { /* convert pointer to offset + skip NUL */ |
247 | shbuf->head = c - shbuf->data + 1; | 286 | G.shbuf->head = c - G.shbuf->data + 1; |
248 | } | 287 | } |
249 | } | 288 | } |
250 | /* copy what fits to the end of buffer, and repeat */ | 289 | /* copy what fits to the end of buffer, and repeat */ |
251 | memcpy(shbuf->data + old_tail, msg, k); | 290 | memcpy(G.shbuf->data + old_tail, msg, k); |
252 | msg += k; | 291 | msg += k; |
253 | len -= k; | 292 | len -= k; |
254 | shbuf->tail = 0; | 293 | G.shbuf->tail = 0; |
255 | goto again; | 294 | goto again; |
256 | } | 295 | } |
257 | if (semop(s_semid, SMwup, 1) == -1) { | 296 | if (semop(G.s_semid, G.SMwup, 1) == -1) { |
258 | bb_perror_msg_and_die("SMwup"); | 297 | bb_perror_msg_and_die("SMwup"); |
259 | } | 298 | } |
260 | if (DEBUG) | 299 | if (DEBUG) |
261 | printf("head:%d tail:%d\n", shbuf->head, shbuf->tail); | 300 | printf("head:%d tail:%d\n", G.shbuf->head, G.shbuf->tail); |
262 | } | 301 | } |
263 | #else | 302 | #else |
264 | void ipcsyslog_cleanup(void); | 303 | void ipcsyslog_cleanup(void); |
@@ -270,29 +309,28 @@ void log_to_shmem(const char *msg); | |||
270 | /* Print a message to the log file. */ | 309 | /* Print a message to the log file. */ |
271 | static void log_locally(char *msg) | 310 | static void log_locally(char *msg) |
272 | { | 311 | { |
273 | static time_t last; | ||
274 | struct flock fl; | 312 | struct flock fl; |
275 | int len = strlen(msg); | 313 | int len = strlen(msg); |
276 | 314 | ||
277 | #if ENABLE_FEATURE_IPC_SYSLOG | 315 | #if ENABLE_FEATURE_IPC_SYSLOG |
278 | if ((option_mask32 & OPT_circularlog) && shbuf) { | 316 | if ((option_mask32 & OPT_circularlog) && G.shbuf) { |
279 | log_to_shmem(msg, len); | 317 | log_to_shmem(msg, len); |
280 | return; | 318 | return; |
281 | } | 319 | } |
282 | #endif | 320 | #endif |
283 | if (logFD >= 0) { | 321 | if (G.logFD >= 0) { |
284 | time_t cur; | 322 | time_t cur; |
285 | time(&cur); | 323 | time(&cur); |
286 | if (last != cur) { | 324 | if (G.last_log_time != cur) { |
287 | last = cur; /* reopen log file every second */ | 325 | G.last_log_time = cur; /* reopen log file every second */ |
288 | close(logFD); | 326 | close(G.logFD); |
289 | goto reopen; | 327 | goto reopen; |
290 | } | 328 | } |
291 | } else { | 329 | } else { |
292 | reopen: | 330 | reopen: |
293 | logFD = device_open(logFilePath, O_WRONLY | O_CREAT | 331 | G.logFD = device_open(G.logFilePath, O_WRONLY | O_CREAT |
294 | | O_NOCTTY | O_APPEND | O_NONBLOCK); | 332 | | O_NOCTTY | O_APPEND | O_NONBLOCK); |
295 | if (logFD < 0) { | 333 | if (G.logFD < 0) { |
296 | /* cannot open logfile? - print to /dev/console then */ | 334 | /* cannot open logfile? - print to /dev/console then */ |
297 | int fd = device_open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK); | 335 | int fd = device_open(_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_NONBLOCK); |
298 | if (fd < 0) | 336 | if (fd < 0) |
@@ -306,9 +344,9 @@ static void log_locally(char *msg) | |||
306 | { | 344 | { |
307 | struct stat statf; | 345 | struct stat statf; |
308 | 346 | ||
309 | isRegular = (fstat(logFD, &statf) == 0 && (statf.st_mode & S_IFREG)); | 347 | G.isRegular = (fstat(G.logFD, &statf) == 0 && (statf.st_mode & S_IFREG)); |
310 | /* bug (mostly harmless): can wrap around if file > 4gb */ | 348 | /* bug (mostly harmless): can wrap around if file > 4gb */ |
311 | curFileSize = statf.st_size; | 349 | G.curFileSize = statf.st_size; |
312 | } | 350 | } |
313 | #endif | 351 | #endif |
314 | } | 352 | } |
@@ -317,36 +355,36 @@ static void log_locally(char *msg) | |||
317 | fl.l_start = 0; | 355 | fl.l_start = 0; |
318 | fl.l_len = 1; | 356 | fl.l_len = 1; |
319 | fl.l_type = F_WRLCK; | 357 | fl.l_type = F_WRLCK; |
320 | fcntl(logFD, F_SETLKW, &fl); | 358 | fcntl(G.logFD, F_SETLKW, &fl); |
321 | 359 | ||
322 | #if ENABLE_FEATURE_ROTATE_LOGFILE | 360 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
323 | if (logFileSize && isRegular && curFileSize > logFileSize) { | 361 | if (G.logFileSize && G.isRegular && G.curFileSize > G.logFileSize) { |
324 | if (logFileRotate) { /* always 0..99 */ | 362 | if (G.logFileRotate) { /* always 0..99 */ |
325 | int i = strlen(logFilePath) + 3 + 1; | 363 | int i = strlen(G.logFilePath) + 3 + 1; |
326 | char oldFile[i]; | 364 | char oldFile[i]; |
327 | char newFile[i]; | 365 | char newFile[i]; |
328 | i = logFileRotate - 1; | 366 | i = G.logFileRotate - 1; |
329 | /* rename: f.8 -> f.9; f.7 -> f.8; ... */ | 367 | /* rename: f.8 -> f.9; f.7 -> f.8; ... */ |
330 | while (1) { | 368 | while (1) { |
331 | sprintf(newFile, "%s.%d", logFilePath, i); | 369 | sprintf(newFile, "%s.%d", G.logFilePath, i); |
332 | if (i == 0) break; | 370 | if (i == 0) break; |
333 | sprintf(oldFile, "%s.%d", logFilePath, --i); | 371 | sprintf(oldFile, "%s.%d", G.logFilePath, --i); |
334 | rename(oldFile, newFile); | 372 | rename(oldFile, newFile); |
335 | } | 373 | } |
336 | /* newFile == "f.0" now */ | 374 | /* newFile == "f.0" now */ |
337 | rename(logFilePath, newFile); | 375 | rename(G.logFilePath, newFile); |
338 | fl.l_type = F_UNLCK; | 376 | fl.l_type = F_UNLCK; |
339 | fcntl(logFD, F_SETLKW, &fl); | 377 | fcntl(G.logFD, F_SETLKW, &fl); |
340 | close(logFD); | 378 | close(G.logFD); |
341 | goto reopen; | 379 | goto reopen; |
342 | } | 380 | } |
343 | ftruncate(logFD, 0); | 381 | ftruncate(G.logFD, 0); |
344 | } | 382 | } |
345 | curFileSize += | 383 | G.curFileSize += |
346 | #endif | 384 | #endif |
347 | full_write(logFD, msg, len); | 385 | full_write(G.logFD, msg, len); |
348 | fl.l_type = F_UNLCK; | 386 | fl.l_type = F_UNLCK; |
349 | fcntl(logFD, F_SETLKW, &fl); | 387 | fcntl(G.logFD, F_SETLKW, &fl); |
350 | } | 388 | } |
351 | 389 | ||
352 | static void parse_fac_prio_20(int pri, char *res20) | 390 | static void parse_fac_prio_20(int pri, char *res20) |
@@ -397,13 +435,13 @@ static void timestamp_and_log(int pri, char *msg, int len) | |||
397 | 435 | ||
398 | /* Log message locally (to file or shared mem) */ | 436 | /* Log message locally (to file or shared mem) */ |
399 | if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) { | 437 | if (!ENABLE_FEATURE_REMOTE_LOG || (option_mask32 & OPT_locallog)) { |
400 | if (LOG_PRI(pri) < logLevel) { | 438 | if (LOG_PRI(pri) < G.logLevel) { |
401 | if (option_mask32 & OPT_small) | 439 | if (option_mask32 & OPT_small) |
402 | sprintf(PRINTBUF, "%s %s\n", timestamp, msg); | 440 | sprintf(PRINTBUF, "%s %s\n", timestamp, msg); |
403 | else { | 441 | else { |
404 | char res[20]; | 442 | char res[20]; |
405 | parse_fac_prio_20(pri, res); | 443 | parse_fac_prio_20(pri, res); |
406 | sprintf(PRINTBUF, "%s %s %s %s\n", timestamp, localHostName, res, msg); | 444 | sprintf(PRINTBUF, "%s %s %s %s\n", timestamp, G.localHostName, res, msg); |
407 | } | 445 | } |
408 | log_locally(PRINTBUF); | 446 | log_locally(PRINTBUF); |
409 | } | 447 | } |
@@ -456,9 +494,9 @@ static void quit_signal(int sig) | |||
456 | #ifdef SYSLOGD_MARK | 494 | #ifdef SYSLOGD_MARK |
457 | static void do_mark(int sig) | 495 | static void do_mark(int sig) |
458 | { | 496 | { |
459 | if (markInterval) { | 497 | if (G.markInterval) { |
460 | timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"-- MARK --", 0); | 498 | timestamp_and_log(LOG_SYSLOG | LOG_INFO, (char*)"-- MARK --", 0); |
461 | alarm(markInterval); | 499 | alarm(G.markInterval); |
462 | } | 500 | } |
463 | } | 501 | } |
464 | #endif | 502 | #endif |
@@ -482,7 +520,7 @@ static void do_syslogd(void) | |||
482 | #endif | 520 | #endif |
483 | #ifdef SYSLOGD_MARK | 521 | #ifdef SYSLOGD_MARK |
484 | signal(SIGALRM, do_mark); | 522 | signal(SIGALRM, do_mark); |
485 | alarm(markInterval); | 523 | alarm(G.markInterval); |
486 | #endif | 524 | #endif |
487 | 525 | ||
488 | memset(&sunx, 0, sizeof(sunx)); | 526 | memset(&sunx, 0, sizeof(sunx)); |
@@ -541,14 +579,14 @@ static void do_syslogd(void) | |||
541 | #if ENABLE_FEATURE_REMOTE_LOG | 579 | #if ENABLE_FEATURE_REMOTE_LOG |
542 | /* We are not modifying log messages in any way before send */ | 580 | /* We are not modifying log messages in any way before send */ |
543 | /* Remote site cannot trust _us_ anyway and need to do validation again */ | 581 | /* Remote site cannot trust _us_ anyway and need to do validation again */ |
544 | if (remoteAddr) { | 582 | if (G.remoteAddr) { |
545 | if (-1 == remoteFD) { | 583 | if (-1 == G.remoteFD) { |
546 | remoteFD = socket(remoteAddr->sa.sa_family, SOCK_DGRAM, 0); | 584 | G.remoteFD = socket(G.remoteAddr->sa.sa_family, SOCK_DGRAM, 0); |
547 | } | 585 | } |
548 | if (-1 != remoteFD) { | 586 | if (-1 != G.remoteFD) { |
549 | /* send message to remote logger, ignore possible error */ | 587 | /* send message to remote logger, ignore possible error */ |
550 | sendto(remoteFD, RECVBUF, i, MSG_DONTWAIT, | 588 | sendto(G.remoteFD, RECVBUF, i, MSG_DONTWAIT, |
551 | &remoteAddr->sa, remoteAddr->len); | 589 | &G.remoteAddr->sa, G.remoteAddr->len); |
552 | } | 590 | } |
553 | } | 591 | } |
554 | #endif | 592 | #endif |
@@ -564,33 +602,36 @@ int syslogd_main(int argc, char **argv) | |||
564 | char OPTION_DECL; | 602 | char OPTION_DECL; |
565 | char *p; | 603 | char *p; |
566 | 604 | ||
605 | PTR_TO_GLOBALS = xzalloc(sizeof(G)); | ||
606 | memcpy(ptr_to_globals, &init_globals, sizeof(init_globals)); | ||
607 | |||
567 | /* do normal option parsing */ | 608 | /* do normal option parsing */ |
568 | opt_complementary = "=0"; /* no non-option params */ | 609 | opt_complementary = "=0"; /* no non-option params */ |
569 | getopt32(argc, argv, OPTION_STR, OPTION_PARAM); | 610 | getopt32(argc, argv, OPTION_STR, OPTION_PARAM); |
570 | #ifdef SYSLOGD_MARK | 611 | #ifdef SYSLOGD_MARK |
571 | if (option_mask32 & OPT_mark) // -m | 612 | if (option_mask32 & OPT_mark) // -m |
572 | markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60; | 613 | G.markInterval = xatou_range(opt_m, 0, INT_MAX/60) * 60; |
573 | #endif | 614 | #endif |
574 | //if (option_mask32 & OPT_nofork) // -n | 615 | //if (option_mask32 & OPT_nofork) // -n |
575 | //if (option_mask32 & OPT_outfile) // -O | 616 | //if (option_mask32 & OPT_outfile) // -O |
576 | if (option_mask32 & OPT_loglevel) // -l | 617 | if (option_mask32 & OPT_loglevel) // -l |
577 | logLevel = xatou_range(opt_l, 1, 8); | 618 | G.logLevel = xatou_range(opt_l, 1, 8); |
578 | //if (option_mask32 & OPT_small) // -S | 619 | //if (option_mask32 & OPT_small) // -S |
579 | #if ENABLE_FEATURE_ROTATE_LOGFILE | 620 | #if ENABLE_FEATURE_ROTATE_LOGFILE |
580 | if (option_mask32 & OPT_filesize) // -s | 621 | if (option_mask32 & OPT_filesize) // -s |
581 | logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024; | 622 | G.logFileSize = xatou_range(opt_s, 0, INT_MAX/1024) * 1024; |
582 | if (option_mask32 & OPT_rotatecnt) // -b | 623 | if (option_mask32 & OPT_rotatecnt) // -b |
583 | logFileRotate = xatou_range(opt_b, 0, 99); | 624 | G.logFileRotate = xatou_range(opt_b, 0, 99); |
584 | #endif | 625 | #endif |
585 | #if ENABLE_FEATURE_REMOTE_LOG | 626 | #if ENABLE_FEATURE_REMOTE_LOG |
586 | if (option_mask32 & OPT_remotelog) { // -R | 627 | if (option_mask32 & OPT_remotelog) { // -R |
587 | remoteAddr = xhost2sockaddr(opt_R, 514); | 628 | G.remoteAddr = xhost2sockaddr(opt_R, 514); |
588 | } | 629 | } |
589 | //if (option_mask32 & OPT_locallog) // -L | 630 | //if (option_mask32 & OPT_locallog) // -L |
590 | #endif | 631 | #endif |
591 | #if ENABLE_FEATURE_IPC_SYSLOG | 632 | #if ENABLE_FEATURE_IPC_SYSLOG |
592 | if (opt_C) // -Cn | 633 | if (opt_C) // -Cn |
593 | shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024; | 634 | G.shm_size = xatoul_range(opt_C, 4, INT_MAX/1024) * 1024; |
594 | #endif | 635 | #endif |
595 | 636 | ||
596 | /* If they have not specified remote logging, then log locally */ | 637 | /* If they have not specified remote logging, then log locally */ |
@@ -598,8 +639,8 @@ int syslogd_main(int argc, char **argv) | |||
598 | option_mask32 |= OPT_locallog; | 639 | option_mask32 |= OPT_locallog; |
599 | 640 | ||
600 | /* Store away localhost's name before the fork */ | 641 | /* Store away localhost's name before the fork */ |
601 | gethostname(localHostName, sizeof(localHostName)); | 642 | gethostname(G.localHostName, sizeof(G.localHostName)); |
602 | p = strchr(localHostName, '.'); | 643 | p = strchr(G.localHostName, '.'); |
603 | if (p) { | 644 | if (p) { |
604 | *p = '\0'; | 645 | *p = '\0'; |
605 | } | 646 | } |