aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGlenn L McGrath <bug1@ihug.co.nz>2002-11-10 22:46:45 +0000
committerGlenn L McGrath <bug1@ihug.co.nz>2002-11-10 22:46:45 +0000
commit912d8f4cbc724ce6ef9d8bd3f73d16d7ef4cf069 (patch)
tree00102b264a0dd0870f9cf6503eed715c7e5e6a8a
parentf234e7c755f30cf2cf7f48762370b2bcde76f68f (diff)
downloadbusybox-w32-912d8f4cbc724ce6ef9d8bd3f73d16d7ef4cf069.tar.gz
busybox-w32-912d8f4cbc724ce6ef9d8bd3f73d16d7ef4cf069.tar.bz2
busybox-w32-912d8f4cbc724ce6ef9d8bd3f73d16d7ef4cf069.zip
run through indent and manually check result
-rw-r--r--sysklogd/syslogd.c481
1 files changed, 253 insertions, 228 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index e13ebfc34..a546bdbd7 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -69,17 +69,20 @@ static char LocalHostName[64];
69#include <netinet/in.h> 69#include <netinet/in.h>
70/* udp socket for logging to remote host */ 70/* udp socket for logging to remote host */
71static int remotefd = -1; 71static int remotefd = -1;
72
72/* where do we log? */ 73/* where do we log? */
73static char *RemoteHost; 74static char *RemoteHost;
75
74/* what port to log to? */ 76/* what port to log to? */
75static int RemotePort = 514; 77static int RemotePort = 514;
78
76/* To remote log or not to remote log, that is the question. */ 79/* To remote log or not to remote log, that is the question. */
77static int doRemoteLog = FALSE; 80static int doRemoteLog = FALSE;
78static int local_logging = FALSE; 81static int local_logging = FALSE;
79#endif 82#endif
80 83
81 84
82#define MAXLINE 1024 /* maximum line length */ 85#define MAXLINE 1024 /* maximum line length */
83 86
84 87
85/* circular buffer variables/structures */ 88/* circular buffer variables/structures */
@@ -95,23 +98,23 @@ static int local_logging = FALSE;
95#include <sys/shm.h> 98#include <sys/shm.h>
96 99
97/* our shared key */ 100/* our shared key */
98static const long KEY_ID = 0x414e4547; /*"GENA"*/ 101static const long KEY_ID = 0x414e4547; /*"GENA" */
99 102
100// Semaphore operation structures 103// Semaphore operation structures
101static struct shbuf_ds { 104static struct shbuf_ds {
102 int size; // size of data written 105 int size; // size of data written
103 int head; // start of message list 106 int head; // start of message list
104 int tail; // end of message list 107 int tail; // end of message list
105 char data[1]; // data/messages 108 char data[1]; // data/messages
106} *buf = NULL; // shared memory pointer 109} *buf = NULL; // shared memory pointer
107 110
108static struct sembuf SMwup[1] = {{1, -1, IPC_NOWAIT}}; // set SMwup 111static struct sembuf SMwup[1] = { {1, -1, IPC_NOWAIT} }; // set SMwup
109static struct sembuf SMwdn[3] = {{0, 0}, {1, 0}, {1, +1}}; // set SMwdn 112static struct sembuf SMwdn[3] = { {0, 0}, {1, 0}, {1, +1} }; // set SMwdn
110 113
111static int shmid = -1; // ipc shared memory id 114static int shmid = -1; // ipc shared memory id
112static int s_semid = -1; // ipc semaphore id 115static int s_semid = -1; // ipc semaphore id
113int data_size = 16000; // data size 116int data_size = 16000; // data size
114int shm_size = 16000 + sizeof(*buf); // our buffer size 117int shm_size = 16000 + sizeof(*buf); // our buffer size
115static int circular_logging = FALSE; 118static int circular_logging = FALSE;
116 119
117/* 120/*
@@ -119,8 +122,9 @@ static int circular_logging = FALSE;
119 */ 122 */
120static inline void sem_up(int semid) 123static inline void sem_up(int semid)
121{ 124{
122 if ( semop(semid, SMwup, 1) == -1 ) 125 if (semop(semid, SMwup, 1) == -1) {
123 perror_msg_and_die("semop[SMwup]"); 126 perror_msg_and_die("semop[SMwup]");
127 }
124} 128}
125 129
126/* 130/*
@@ -128,51 +132,60 @@ static inline void sem_up(int semid)
128 */ 132 */
129static inline void sem_down(int semid) 133static inline void sem_down(int semid)
130{ 134{
131 if ( semop(semid, SMwdn, 3) == -1 ) 135 if (semop(semid, SMwdn, 3) == -1) {
132 perror_msg_and_die("semop[SMwdn]"); 136 perror_msg_and_die("semop[SMwdn]");
137 }
133} 138}
134 139
135 140
136void ipcsyslog_cleanup(void){ 141void ipcsyslog_cleanup(void)
142{
137 printf("Exiting Syslogd!\n"); 143 printf("Exiting Syslogd!\n");
138 if (shmid != -1) 144 if (shmid != -1) {
139 shmdt(buf); 145 shmdt(buf);
146 }
140 147
141 if (shmid != -1) 148 if (shmid != -1) {
142 shmctl(shmid, IPC_RMID, NULL); 149 shmctl(shmid, IPC_RMID, NULL);
143 if (s_semid != -1) 150 }
151 if (s_semid != -1) {
144 semctl(s_semid, 0, IPC_RMID, 0); 152 semctl(s_semid, 0, IPC_RMID, 0);
153 }
145} 154}
146 155
147void ipcsyslog_init(void){ 156void ipcsyslog_init(void)
148 if (buf == NULL){ 157{
149 if ((shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023)) == -1) 158 if (buf == NULL) {
159 if ((shmid = shmget(KEY_ID, shm_size, IPC_CREAT | 1023)) == -1) {
150 perror_msg_and_die("shmget"); 160 perror_msg_and_die("shmget");
161 }
151 162
152 163 if ((buf = shmat(shmid, NULL, 0)) == NULL) {
153 if ((buf = shmat(shmid, NULL, 0)) == NULL)
154 perror_msg_and_die("shmat"); 164 perror_msg_and_die("shmat");
165 }
155 166
167 buf->size = data_size;
168 buf->head = buf->tail = 0;
156 169
157 buf->size=data_size; 170 // we'll trust the OS to set initial semval to 0 (let's hope)
158 buf->head=buf->tail=0; 171 if ((s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023)) == -1) {
159 172 if (errno == EEXIST) {
160 // we'll trust the OS to set initial semval to 0 (let's hope) 173 if ((s_semid = semget(KEY_ID, 2, 0)) == -1) {
161 if ((s_semid = semget(KEY_ID, 2, IPC_CREAT | IPC_EXCL | 1023)) == -1){ 174 perror_msg_and_die("semget");
162 if (errno == EEXIST){ 175 }
163 if ((s_semid = semget(KEY_ID, 2, 0)) == -1) 176 } else {
164 perror_msg_and_die("semget"); 177 perror_msg_and_die("semget");
165 }else 178 }
166 perror_msg_and_die("semget"); 179 }
167 } 180 } else {
168 }else{
169 printf("Buffer already allocated just grab the semaphore?"); 181 printf("Buffer already allocated just grab the semaphore?");
170 } 182 }
171} 183}
172 184
173/* write message to buffer */ 185/* write message to buffer */
174void circ_message(const char *msg){ 186void circ_message(const char *msg)
175 int l=strlen(msg)+1; /* count the whole message w/ '\0' included */ 187{
188 int l = strlen(msg) + 1; /* count the whole message w/ '\0' included */
176 189
177 sem_down(s_semid); 190 sem_down(s_semid);
178 191
@@ -199,126 +212,133 @@ void circ_message(const char *msg){
199 * Note: This algorithm uses Linux IPC mechanism w/ shared memory and semaphores to provide 212 * Note: This algorithm uses Linux IPC mechanism w/ shared memory and semaphores to provide
200 * a threasafe way of handling shared memory operations. 213 * a threasafe way of handling shared memory operations.
201 */ 214 */
202 if ( (buf->tail + l) < buf->size ){ 215 if ((buf->tail + l) < buf->size) {
203 /* before we append the message we need to check the HEAD so that we won't 216 /* before we append the message we need to check the HEAD so that we won't
204 overwrite any of the message that we still need and adjust HEAD to point 217 overwrite any of the message that we still need and adjust HEAD to point
205 to the next message! */ 218 to the next message! */
206 if ( buf->tail < buf->head){ 219 if (buf->tail < buf->head) {
207 if ( (buf->tail + l) >= buf->head ){ 220 if ((buf->tail + l) >= buf->head) {
208 /* we need to move the HEAD to point to the next message 221 /* we need to move the HEAD to point to the next message
209 * Theoretically we have enough room to add the whole message to the 222 * Theoretically we have enough room to add the whole message to the
210 * buffer, because of the first outer IF statement, so we don't have 223 * buffer, because of the first outer IF statement, so we don't have
211 * to worry about overflows here! 224 * to worry about overflows here!
212 */ 225 */
213 int k= buf->tail + l - buf->head; /* we need to know how many bytes 226 int k = buf->tail + l - buf->head; /* we need to know how many bytes
214 we are overwriting to make 227 we are overwriting to make
215 enough room */ 228 enough room */
216 char *c=memchr(buf->data+buf->head + k,'\0',buf->size - (buf->head + k)); 229 char *c =
217 if (c != NULL) {/* do a sanity check just in case! */ 230 memchr(buf->data + buf->head + k, '\0',
218 buf->head = c - buf->data + 1; /* we need to convert pointer to 231 buf->size - (buf->head + k));
219 offset + skip the '\0' since 232 if (c != NULL) { /* do a sanity check just in case! */
220 we need to point to the beginning 233 buf->head = c - buf->data + 1; /* we need to convert pointer to
221 of the next message */ 234 offset + skip the '\0' since
222 /* Note: HEAD is only used to "retrieve" messages, it's not used 235 we need to point to the beginning
223 when writing messages into our buffer */ 236 of the next message */
224 }else{ /* show an error message to know we messed up? */ 237 /* Note: HEAD is only used to "retrieve" messages, it's not used
225 printf("Weird! Can't find the terminator token??? \n"); 238 when writing messages into our buffer */
226 buf->head=0; 239 } else { /* show an error message to know we messed up? */
227 } 240 printf("Weird! Can't find the terminator token??? \n");
241 buf->head = 0;
242 }
228 } 243 }
229 } /* in other cases no overflows have been done yet, so we don't care! */ 244 }
230 245
246 /* in other cases no overflows have been done yet, so we don't care! */
231 /* we should be ok to append the message now */ 247 /* we should be ok to append the message now */
232 strncpy(buf->data + buf->tail,msg,l); /* append our message */ 248 strncpy(buf->data + buf->tail, msg, l); /* append our message */
233 buf->tail+=l; /* count full message w/ '\0' terminating char */ 249 buf->tail += l; /* count full message w/ '\0' terminating char */
234 }else{ 250 } else {
235 /* we need to break up the message and "circle" it around */ 251 /* we need to break up the message and "circle" it around */
236 char *c; 252 char *c;
237 int k=buf->tail + l - buf->size; /* count # of bytes we don't fit */ 253 int k = buf->tail + l - buf->size; /* count # of bytes we don't fit */
238 254
239 /* We need to move HEAD! This is always the case since we are going 255 /* We need to move HEAD! This is always the case since we are going
240 * to "circle" the message. 256 * to "circle" the message.
241 */ 257 */
242 c=memchr(buf->data + k ,'\0', buf->size - k); 258 c = memchr(buf->data + k, '\0', buf->size - k);
243 259
244 if (c != NULL) /* if we don't have '\0'??? weird!!! */{ 260 if (c != NULL) { /* if we don't have '\0'??? weird!!! */
245 /* move head pointer*/ 261 /* move head pointer */
246 buf->head=c-buf->data+1; 262 buf->head = c - buf->data + 1;
247 263
248 /* now write the first part of the message */ 264 /* now write the first part of the message */
249 strncpy(buf->data + buf->tail, msg, l - k - 1); 265 strncpy(buf->data + buf->tail, msg, l - k - 1);
250 266
251 /* ALWAYS terminate end of buffer w/ '\0' */ 267 /* ALWAYS terminate end of buffer w/ '\0' */
252 buf->data[buf->size-1]='\0'; 268 buf->data[buf->size - 1] = '\0';
253 269
254 /* now write out the rest of the string to the beginning of the buffer */ 270 /* now write out the rest of the string to the beginning of the buffer */
255 strcpy(buf->data, &msg[l-k-1]); 271 strcpy(buf->data, &msg[l - k - 1]);
256 272
257 /* we need to place the TAIL at the end of the message */ 273 /* we need to place the TAIL at the end of the message */
258 buf->tail = k + 1; 274 buf->tail = k + 1;
259 }else{ 275 } else {
260 printf("Weird! Can't find the terminator token from the beginning??? \n"); 276 printf
261 buf->head = buf->tail = 0; /* reset buffer, since it's probably corrupted */ 277 ("Weird! Can't find the terminator token from the beginning??? \n");
278 buf->head = buf->tail = 0; /* reset buffer, since it's probably corrupted */
262 } 279 }
263 280
264 } 281 }
265 sem_up(s_semid); 282 sem_up(s_semid);
266} 283}
267#endif /* CONFIG_FEATURE_IPC_SYSLOG */ 284#endif /* CONFIG_FEATURE_IPC_SYSLOG */
268 285
269/* Note: There is also a function called "message()" in init.c */ 286/* Note: There is also a function called "message()" in init.c */
270/* Print a message to the log file. */ 287/* Print a message to the log file. */
271static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2))); 288static void message(char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
272static void message (char *fmt, ...) 289static void message(char *fmt, ...)
273{ 290{
274 int fd; 291 int fd;
275 struct flock fl; 292 struct flock fl;
276 va_list arguments; 293 va_list arguments;
277 294
278 fl.l_whence = SEEK_SET; 295 fl.l_whence = SEEK_SET;
279 fl.l_start = 0; 296 fl.l_start = 0;
280 fl.l_len = 1; 297 fl.l_len = 1;
281 298
282#ifdef CONFIG_FEATURE_IPC_SYSLOG 299#ifdef CONFIG_FEATURE_IPC_SYSLOG
283 if ((circular_logging == TRUE) && (buf != NULL)){ 300 if ((circular_logging == TRUE) && (buf != NULL)) {
284 char b[1024]; 301 char b[1024];
285 va_start (arguments, fmt); 302
286 vsnprintf (b, sizeof(b)-1, fmt, arguments); 303 va_start(arguments, fmt);
287 va_end (arguments); 304 vsnprintf(b, sizeof(b) - 1, fmt, arguments);
288 circ_message(b); 305 va_end(arguments);
289 306 circ_message(b);
290 }else 307
308 } else
291#endif 309#endif
292 if ((fd = device_open (logFilePath, 310 if ((fd =
293 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND | 311 device_open(logFilePath,
294 O_NONBLOCK)) >= 0) { 312 O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND |
313 O_NONBLOCK)) >= 0) {
295 fl.l_type = F_WRLCK; 314 fl.l_type = F_WRLCK;
296 fcntl (fd, F_SETLKW, &fl); 315 fcntl(fd, F_SETLKW, &fl);
297 va_start (arguments, fmt); 316 va_start(arguments, fmt);
298 vdprintf (fd, fmt, arguments); 317 vdprintf(fd, fmt, arguments);
299 va_end (arguments); 318 va_end(arguments);
300 fl.l_type = F_UNLCK; 319 fl.l_type = F_UNLCK;
301 fcntl (fd, F_SETLKW, &fl); 320 fcntl(fd, F_SETLKW, &fl);
302 close (fd); 321 close(fd);
303 } else { 322 } else {
304 /* Always send console messages to /dev/console so people will see them. */ 323 /* Always send console messages to /dev/console so people will see them. */
305 if ((fd = device_open (_PATH_CONSOLE, 324 if ((fd =
306 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) { 325 device_open(_PATH_CONSOLE,
307 va_start (arguments, fmt); 326 O_WRONLY | O_NOCTTY | O_NONBLOCK)) >= 0) {
308 vdprintf (fd, fmt, arguments); 327 va_start(arguments, fmt);
309 va_end (arguments); 328 vdprintf(fd, fmt, arguments);
310 close (fd); 329 va_end(arguments);
330 close(fd);
311 } else { 331 } else {
312 fprintf (stderr, "Bummer, can't print: "); 332 fprintf(stderr, "Bummer, can't print: ");
313 va_start (arguments, fmt); 333 va_start(arguments, fmt);
314 vfprintf (stderr, fmt, arguments); 334 vfprintf(stderr, fmt, arguments);
315 fflush (stderr); 335 fflush(stderr);
316 va_end (arguments); 336 va_end(arguments);
317 } 337 }
318 } 338 }
319} 339}
320 340
321static void logMessage (int pri, char *msg) 341static void logMessage(int pri, char *msg)
322{ 342{
323 time_t now; 343 time_t now;
324 char *timestamp; 344 char *timestamp;
@@ -327,17 +347,18 @@ static void logMessage (int pri, char *msg)
327 347
328 if (pri != 0) { 348 if (pri != 0) {
329 for (c_fac = facilitynames; 349 for (c_fac = facilitynames;
330 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++); 350 c_fac->c_name && !(c_fac->c_val == LOG_FAC(pri) << 3); c_fac++);
331 for (c_pri = prioritynames; 351 for (c_pri = prioritynames;
332 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++); 352 c_pri->c_name && !(c_pri->c_val == LOG_PRI(pri)); c_pri++);
333 if (c_fac->c_name == NULL || c_pri->c_name == NULL) 353 if (c_fac->c_name == NULL || c_pri->c_name == NULL) {
334 snprintf(res, sizeof(res), "<%d>", pri); 354 snprintf(res, sizeof(res), "<%d>", pri);
335 else 355 } else {
336 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name); 356 snprintf(res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
357 }
337 } 358 }
338 359
339 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' || 360 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
340 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') { 361 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ') {
341 time(&now); 362 time(&now);
342 timestamp = ctime(&now) + 4; 363 timestamp = ctime(&now) + 4;
343 timestamp[15] = '\0'; 364 timestamp[15] = '\0';
@@ -351,32 +372,32 @@ static void logMessage (int pri, char *msg)
351 372
352#ifdef CONFIG_FEATURE_REMOTE_LOG 373#ifdef CONFIG_FEATURE_REMOTE_LOG
353 /* send message to remote logger */ 374 /* send message to remote logger */
354 if ( -1 != remotefd){ 375 if (-1 != remotefd) {
355static const int IOV_COUNT = 2; 376 static const int IOV_COUNT = 2;
356 struct iovec iov[IOV_COUNT]; 377 struct iovec iov[IOV_COUNT];
357 struct iovec *v = iov; 378 struct iovec *v = iov;
358 379
359 memset(&res, 0, sizeof(res)); 380 memset(&res, 0, sizeof(res));
360 snprintf(res, sizeof(res), "<%d>", pri); 381 snprintf(res, sizeof(res), "<%d>", pri);
361 v->iov_base = res ; 382 v->iov_base = res;
362 v->iov_len = strlen(res); 383 v->iov_len = strlen(res);
363 v++; 384 v++;
364 385
365 v->iov_base = msg; 386 v->iov_base = msg;
366 v->iov_len = strlen(msg); 387 v->iov_len = strlen(msg);
367writev_retry: 388 writev_retry:
368 if ( -1 == writev(remotefd,iov, IOV_COUNT)){ 389 if (-1 == writev(remotefd, iov, IOV_COUNT)) {
369 if (errno == EINTR) goto writev_retry; 390 if (errno == EINTR) {
370 error_msg_and_die("cannot write to remote file handle on" 391 goto writev_retry;
371 "%s:%d",RemoteHost,RemotePort); 392 }
393 error_msg_and_die("cannot write to remote file handle on %s:%d",
394 RemoteHost, RemotePort);
372 } 395 }
373 } 396 }
374 if (local_logging == TRUE) 397 if (local_logging == TRUE)
375#endif 398#endif
376 /* now spew out the message to wherever it is supposed to go */ 399 /* now spew out the message to wherever it is supposed to go */
377 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg); 400 message("%s %s %s %s\n", timestamp, LocalHostName, res, msg);
378
379
380} 401}
381 402
382static void quit_signal(int sig) 403static void quit_signal(int sig)
@@ -400,31 +421,31 @@ static void domark(int sig)
400 421
401/* This must be a #define, since when DODEBUG and BUFFERS_GO_IN_BSS are 422/* This must be a #define, since when DODEBUG and BUFFERS_GO_IN_BSS are
402 * enabled, we otherwise get a "storage size isn't constant error. */ 423 * enabled, we otherwise get a "storage size isn't constant error. */
403static int serveConnection (char* tmpbuf, int n_read) 424static int serveConnection(char *tmpbuf, int n_read)
404{ 425{
405 char *p = tmpbuf; 426 char *p = tmpbuf;
406 427
407 while (p < tmpbuf + n_read) { 428 while (p < tmpbuf + n_read) {
408 429
409 int pri = (LOG_USER | LOG_NOTICE); 430 int pri = (LOG_USER | LOG_NOTICE);
410 char line[ MAXLINE + 1 ]; 431 char line[MAXLINE + 1];
411 unsigned char c; 432 unsigned char c;
412 433
413 char *q = line; 434 char *q = line;
414 435
415 while ( (c = *p) && q < &line[ sizeof (line) - 1 ]) { 436 while ((c = *p) && q < &line[sizeof(line) - 1]) {
416 if (c == '<') { 437 if (c == '<') {
417 /* Parse the magic priority number. */ 438 /* Parse the magic priority number. */
418 pri = 0; 439 pri = 0;
419 while (isdigit (*(++p))) { 440 while (isdigit(*(++p))) {
420 pri = 10 * pri + (*p - '0'); 441 pri = 10 * pri + (*p - '0');
421 } 442 }
422 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)){ 443 if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) {
423 pri = (LOG_USER | LOG_NOTICE); 444 pri = (LOG_USER | LOG_NOTICE);
424 } 445 }
425 } else if (c == '\n') { 446 } else if (c == '\n') {
426 *q++ = ' '; 447 *q++ = ' ';
427 } else if (iscntrl (c) && (c < 0177)) { 448 } else if (iscntrl(c) && (c < 0177)) {
428 *q++ = '^'; 449 *q++ = '^';
429 *q++ = c ^ 0100; 450 *q++ = c ^ 0100;
430 } else { 451 } else {
@@ -435,47 +456,47 @@ static int serveConnection (char* tmpbuf, int n_read)
435 *q = '\0'; 456 *q = '\0';
436 p++; 457 p++;
437 /* Now log it */ 458 /* Now log it */
438 logMessage (pri, line); 459 logMessage(pri, line);
439 } 460 }
440 return n_read; 461 return n_read;
441} 462}
442 463
443 464
444#ifdef CONFIG_FEATURE_REMOTE_LOG 465#ifdef CONFIG_FEATURE_REMOTE_LOG
445static void init_RemoteLog (void) 466static void init_RemoteLog(void)
446{ 467{
447 468
448 struct sockaddr_in remoteaddr; 469 struct sockaddr_in remoteaddr;
449 struct hostent *hostinfo; 470 struct hostent *hostinfo;
450 int len = sizeof(remoteaddr); 471 int len = sizeof(remoteaddr);
451 472
452 memset(&remoteaddr, 0, len); 473 memset(&remoteaddr, 0, len);
453 474
454 remotefd = socket(AF_INET, SOCK_DGRAM, 0); 475 remotefd = socket(AF_INET, SOCK_DGRAM, 0);
455 476
456 if (remotefd < 0) { 477 if (remotefd < 0) {
457 error_msg_and_die("cannot create socket"); 478 error_msg_and_die("cannot create socket");
458 } 479 }
459 480
460 hostinfo = xgethostbyname(RemoteHost); 481 hostinfo = xgethostbyname(RemoteHost);
461 482
462 remoteaddr.sin_family = AF_INET; 483 remoteaddr.sin_family = AF_INET;
463 remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; 484 remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
464 remoteaddr.sin_port = htons(RemotePort); 485 remoteaddr.sin_port = htons(RemotePort);
465 486
466 /* 487 /* Since we are using UDP sockets, connect just sets the default host and port
467 Since we are using UDP sockets, connect just sets the default host and port 488 * for future operations
468 for future operations 489 */
469 */ 490 if (0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))) {
470 if ( 0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))){ 491 error_msg_and_die("cannot connect to remote host %s:%d", RemoteHost,
471 error_msg_and_die("cannot connect to remote host %s:%d", RemoteHost, RemotePort); 492 RemotePort);
472 } 493 }
473 494
474} 495}
475#endif 496#endif
476 497
477static void doSyslogd (void) __attribute__ ((noreturn)); 498static void doSyslogd(void) __attribute__ ((noreturn));
478static void doSyslogd (void) 499static void doSyslogd(void)
479{ 500{
480 struct sockaddr_un sunx; 501 struct sockaddr_un sunx;
481 socklen_t addrLength; 502 socklen_t addrLength;
@@ -484,80 +505,85 @@ static void doSyslogd (void)
484 fd_set fds; 505 fd_set fds;
485 506
486 /* Set up signal handlers. */ 507 /* Set up signal handlers. */
487 signal (SIGINT, quit_signal); 508 signal(SIGINT, quit_signal);
488 signal (SIGTERM, quit_signal); 509 signal(SIGTERM, quit_signal);
489 signal (SIGQUIT, quit_signal); 510 signal(SIGQUIT, quit_signal);
490 signal (SIGHUP, SIG_IGN); 511 signal(SIGHUP, SIG_IGN);
491 signal (SIGCHLD, SIG_IGN); 512 signal(SIGCHLD, SIG_IGN);
492#ifdef SIGCLD 513#ifdef SIGCLD
493 signal (SIGCLD, SIG_IGN); 514 signal(SIGCLD, SIG_IGN);
494#endif 515#endif
495 signal (SIGALRM, domark); 516 signal(SIGALRM, domark);
496 alarm (MarkInterval); 517 alarm(MarkInterval);
497 518
498 /* Create the syslog file so realpath() can work. */ 519 /* Create the syslog file so realpath() can work. */
499 if (realpath (_PATH_LOG, lfile) != NULL) 520 if (realpath(_PATH_LOG, lfile) != NULL) {
500 unlink (lfile); 521 unlink(lfile);
522 }
501 523
502 memset (&sunx, 0, sizeof (sunx)); 524 memset(&sunx, 0, sizeof(sunx));
503 sunx.sun_family = AF_UNIX; 525 sunx.sun_family = AF_UNIX;
504 strncpy (sunx.sun_path, lfile, sizeof (sunx.sun_path)); 526 strncpy(sunx.sun_path, lfile, sizeof(sunx.sun_path));
505 if ((sock_fd = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0) 527 if ((sock_fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) {
506 perror_msg_and_die ("Couldn't get file descriptor for socket " _PATH_LOG); 528 perror_msg_and_die("Couldn't get file descriptor for socket "
507 529 _PATH_LOG);
508 addrLength = sizeof (sunx.sun_family) + strlen (sunx.sun_path); 530 }
509 if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0)
510 perror_msg_and_die ("Could not connect to socket " _PATH_LOG);
511
512 if (chmod (lfile, 0666) < 0)
513 perror_msg_and_die ("Could not set permission on " _PATH_LOG);
514 531
532 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
533 if (bind(sock_fd, (struct sockaddr *) &sunx, addrLength) < 0) {
534 perror_msg_and_die("Could not connect to socket " _PATH_LOG);
535 }
515 536
537 if (chmod(lfile, 0666) < 0) {
538 perror_msg_and_die("Could not set permission on " _PATH_LOG);
539 }
516#ifdef CONFIG_FEATURE_IPC_SYSLOG 540#ifdef CONFIG_FEATURE_IPC_SYSLOG
517 if (circular_logging == TRUE ){ 541 if (circular_logging == TRUE) {
518 ipcsyslog_init(); 542 ipcsyslog_init();
519 } 543 }
520#endif 544#endif
521 545
522#ifdef CONFIG_FEATURE_REMOTE_LOG 546#ifdef CONFIG_FEATURE_REMOTE_LOG
523 if (doRemoteLog == TRUE){ 547 if (doRemoteLog == TRUE) {
524 init_RemoteLog(); 548 init_RemoteLog();
525 } 549 }
526#endif 550#endif
527 551
528 logMessage (LOG_SYSLOG | LOG_INFO, "syslogd started: " BB_BANNER); 552 logMessage(LOG_SYSLOG | LOG_INFO, "syslogd started: " BB_BANNER);
529 553
530 for (;;) { 554 for (;;) {
531 555
532 FD_ZERO (&fds); 556 FD_ZERO(&fds);
533 FD_SET (sock_fd, &fds); 557 FD_SET(sock_fd, &fds);
534 558
535 if (select (sock_fd+1, &fds, NULL, NULL, NULL) < 0) { 559 if (select(sock_fd + 1, &fds, NULL, NULL, NULL) < 0) {
536 if (errno == EINTR) { 560 if (errno == EINTR) {
537 /* alarm may have happened. */ 561 /* alarm may have happened. */
538 continue; 562 continue;
539 } 563 }
540 perror_msg_and_die ("select error"); 564 perror_msg_and_die("select error");
541 } 565 }
542 566
543 if (FD_ISSET (sock_fd, &fds)) { 567 if (FD_ISSET(sock_fd, &fds)) {
544 int i; 568 int i;
545 RESERVE_CONFIG_BUFFER(tmpbuf, BUFSIZ + 1); 569
546 570 RESERVE_CONFIG_BUFFER(tmpbuf, BUFSIZ + 1);
547 memset(tmpbuf, '\0', BUFSIZ+1); 571
548 if ( (i = recv(sock_fd, tmpbuf, BUFSIZ, 0)) > 0) { 572 memset(tmpbuf, '\0', BUFSIZ + 1);
549 serveConnection(tmpbuf, i); 573 if ((i = recv(sock_fd, tmpbuf, BUFSIZ, 0)) > 0) {
550 } else { 574 serveConnection(tmpbuf, i);
551 perror_msg_and_die ("UNIX socket error"); 575 } else {
552 } 576 perror_msg_and_die("UNIX socket error");
553 RELEASE_CONFIG_BUFFER (tmpbuf); 577 }
554 }/* FD_ISSET() */ 578 RELEASE_CONFIG_BUFFER(tmpbuf);
555 } /* for main loop */ 579 } /* FD_ISSET() */
580 } /* for main loop */
556} 581}
557 582
558extern int syslogd_main(int argc, char **argv) 583extern int syslogd_main(int argc, char **argv)
559{ 584{
560 int opt; 585 int opt;
586
561#if ! defined(__uClinux__) 587#if ! defined(__uClinux__)
562 int doFork = TRUE; 588 int doFork = TRUE;
563#endif 589#endif
@@ -567,37 +593,37 @@ extern int syslogd_main(int argc, char **argv)
567 /* do normal option parsing */ 593 /* do normal option parsing */
568 while ((opt = getopt(argc, argv, "m:nO:R:LC")) > 0) { 594 while ((opt = getopt(argc, argv, "m:nO:R:LC")) > 0) {
569 switch (opt) { 595 switch (opt) {
570 case 'm': 596 case 'm':
571 MarkInterval = atoi(optarg) * 60; 597 MarkInterval = atoi(optarg) * 60;
572 break; 598 break;
573#if ! defined(__uClinux__) 599#if ! defined(__uClinux__)
574 case 'n': 600 case 'n':
575 doFork = FALSE; 601 doFork = FALSE;
576 break; 602 break;
577#endif 603#endif
578 case 'O': 604 case 'O':
579 logFilePath = xstrdup(optarg); 605 logFilePath = xstrdup(optarg);
580 break; 606 break;
581#ifdef CONFIG_FEATURE_REMOTE_LOG 607#ifdef CONFIG_FEATURE_REMOTE_LOG
582 case 'R': 608 case 'R':
583 RemoteHost = xstrdup(optarg); 609 RemoteHost = xstrdup(optarg);
584 if ( (p = strchr(RemoteHost, ':'))){ 610 if ((p = strchr(RemoteHost, ':'))) {
585 RemotePort = atoi(p+1); 611 RemotePort = atoi(p + 1);
586 *p = '\0'; 612 *p = '\0';
587 } 613 }
588 doRemoteLog = TRUE; 614 doRemoteLog = TRUE;
589 break; 615 break;
590 case 'L': 616 case 'L':
591 local_logging = TRUE; 617 local_logging = TRUE;
592 break; 618 break;
593#endif 619#endif
594#ifdef CONFIG_FEATURE_IPC_SYSLOG 620#ifdef CONFIG_FEATURE_IPC_SYSLOG
595 case 'C': 621 case 'C':
596 circular_logging = TRUE; 622 circular_logging = TRUE;
597 break; 623 break;
598#endif 624#endif
599 default: 625 default:
600 show_usage(); 626 show_usage();
601 } 627 }
602 } 628 }
603 629
@@ -617,9 +643,8 @@ extern int syslogd_main(int argc, char **argv)
617 umask(0); 643 umask(0);
618 644
619#if ! defined(__uClinux__) 645#if ! defined(__uClinux__)
620 if (doFork == TRUE) { 646 if ((doFork == TRUE) && (daemon(0, 1) < 0)) {
621 if (daemon(0, 1) < 0) 647 perror_msg_and_die("daemon");
622 perror_msg_and_die("daemon");
623 } 648 }
624#endif 649#endif
625 doSyslogd(); 650 doSyslogd();