diff options
Diffstat (limited to 'sysklogd')
-rw-r--r-- | sysklogd/syslogd.c | 104 |
1 files changed, 103 insertions, 1 deletions
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c index 264d899f3..508a6572b 100644 --- a/sysklogd/syslogd.c +++ b/sysklogd/syslogd.c | |||
@@ -61,6 +61,7 @@ static inline _syscall3(int, klogctl, int, type, char *, b, int, len); | |||
61 | /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */ | 61 | /* SYSLOG_NAMES defined to pull some extra junk from syslog.h */ |
62 | #define SYSLOG_NAMES | 62 | #define SYSLOG_NAMES |
63 | #include <sys/syslog.h> | 63 | #include <sys/syslog.h> |
64 | #include <sys/uio.h> | ||
64 | 65 | ||
65 | /* Path for the file where all log messages are written */ | 66 | /* Path for the file where all log messages are written */ |
66 | #define __LOG_FILE "/var/log/messages" | 67 | #define __LOG_FILE "/var/log/messages" |
@@ -76,6 +77,18 @@ static int MarkInterval = 20 * 60; | |||
76 | /* localhost's name */ | 77 | /* localhost's name */ |
77 | static char LocalHostName[32]; | 78 | static char LocalHostName[32]; |
78 | 79 | ||
80 | #ifdef BB_FEATURE_REMOTE_LOG | ||
81 | #include <netinet/in.h> | ||
82 | /* udp socket for logging to remote host */ | ||
83 | static int remotefd = -1; | ||
84 | /* where do we log? */ | ||
85 | static char *RemoteHost; | ||
86 | /* what port to log to? */ | ||
87 | static int RemotePort = 514; | ||
88 | /* To remote log or not to remote log, that is the question. */ | ||
89 | static int doRemoteLog = FALSE; | ||
90 | #endif | ||
91 | |||
79 | /* Note: There is also a function called "message()" in init.c */ | 92 | /* Note: There is also a function called "message()" in init.c */ |
80 | /* Print a message to the log file. */ | 93 | /* Print a message to the log file. */ |
81 | static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2))); | 94 | static void message (char *fmt, ...) __attribute__ ((format (printf, 1, 2))); |
@@ -151,6 +164,30 @@ static void logMessage (int pri, char *msg) | |||
151 | 164 | ||
152 | /* now spew out the message to wherever it is supposed to go */ | 165 | /* now spew out the message to wherever it is supposed to go */ |
153 | message("%s %s %s %s\n", timestamp, LocalHostName, res, msg); | 166 | message("%s %s %s %s\n", timestamp, LocalHostName, res, msg); |
167 | |||
168 | #ifdef BB_FEATURE_REMOTE_LOG | ||
169 | /* send message to remote logger */ | ||
170 | if ( -1 != remotefd){ | ||
171 | #define IOV_COUNT 2 | ||
172 | struct iovec iov[IOV_COUNT]; | ||
173 | struct iovec *v = iov; | ||
174 | |||
175 | bzero(&res, sizeof(res)); | ||
176 | snprintf(res, sizeof(res), "<%d>", pri); | ||
177 | v->iov_base = res ; | ||
178 | v->iov_len = strlen(res); | ||
179 | v++; | ||
180 | |||
181 | v->iov_base = msg; | ||
182 | v->iov_len = strlen(msg); | ||
183 | |||
184 | if ( -1 == writev(remotefd,iov, IOV_COUNT)){ | ||
185 | fatalError("syslogd: cannot write to remote file handle on" | ||
186 | "%s:%d\n",RemoteHost,RemotePort); | ||
187 | } | ||
188 | } | ||
189 | #endif | ||
190 | |||
154 | } | 191 | } |
155 | 192 | ||
156 | static void quit_signal(int sig) | 193 | static void quit_signal(int sig) |
@@ -191,8 +228,9 @@ static int serveConnection (int conn) | |||
191 | while (isdigit (*(++p))) { | 228 | while (isdigit (*(++p))) { |
192 | pri = 10 * pri + (*p - '0'); | 229 | pri = 10 * pri + (*p - '0'); |
193 | } | 230 | } |
194 | if (pri & ~(LOG_FACMASK | LOG_PRIMASK)) | 231 | if (pri & ~(LOG_FACMASK | LOG_PRIMASK)){ |
195 | pri = (LOG_USER | LOG_NOTICE); | 232 | pri = (LOG_USER | LOG_NOTICE); |
233 | } | ||
196 | } else if (c == '\n') { | 234 | } else if (c == '\n') { |
197 | *q++ = ' '; | 235 | *q++ = ' '; |
198 | } else if (iscntrl (c) && (c < 0177)) { | 236 | } else if (iscntrl (c) && (c < 0177)) { |
@@ -210,6 +248,43 @@ static int serveConnection (int conn) | |||
210 | return (0); | 248 | return (0); |
211 | } | 249 | } |
212 | 250 | ||
251 | |||
252 | #ifdef BB_FEATURE_REMOTE_LOG | ||
253 | static void init_RemoteLog (void){ | ||
254 | |||
255 | struct sockaddr_in remoteaddr; | ||
256 | struct hostent *hostinfo; | ||
257 | int len = sizeof(remoteaddr); | ||
258 | |||
259 | bzero(&remoteaddr, len); | ||
260 | |||
261 | remotefd = socket(AF_INET, SOCK_DGRAM, 0); | ||
262 | |||
263 | if (remotefd < 0) { | ||
264 | fatalError("syslogd: cannot create socket\n"); | ||
265 | } | ||
266 | |||
267 | hostinfo = (struct hostent *) gethostbyname(RemoteHost); | ||
268 | |||
269 | if (!hostinfo) { | ||
270 | fatalError("syslogd: cannot resolve remote host name [%s]\n", RemoteHost); | ||
271 | } | ||
272 | |||
273 | remoteaddr.sin_family = AF_INET; | ||
274 | remoteaddr.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list; | ||
275 | remoteaddr.sin_port = htons(RemotePort); | ||
276 | |||
277 | /* | ||
278 | Since we are using UDP sockets, connect just sets the default host and port | ||
279 | for future operations | ||
280 | */ | ||
281 | if ( 0 != (connect(remotefd, (struct sockaddr *) &remoteaddr, len))){ | ||
282 | fatalError("syslogd: cannot connect to remote host %s:%d\n", RemoteHost, RemotePort); | ||
283 | } | ||
284 | |||
285 | } | ||
286 | #endif | ||
287 | |||
213 | static void doSyslogd (void) __attribute__ ((noreturn)); | 288 | static void doSyslogd (void) __attribute__ ((noreturn)); |
214 | static void doSyslogd (void) | 289 | static void doSyslogd (void) |
215 | { | 290 | { |
@@ -254,6 +329,12 @@ static void doSyslogd (void) | |||
254 | FD_ZERO (&fds); | 329 | FD_ZERO (&fds); |
255 | FD_SET (sock_fd, &fds); | 330 | FD_SET (sock_fd, &fds); |
256 | 331 | ||
332 | #ifdef BB_FEATURE_REMOTE_LOG | ||
333 | if (doRemoteLog == TRUE){ | ||
334 | init_RemoteLog(); | ||
335 | } | ||
336 | #endif | ||
337 | |||
257 | logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")"); | 338 | logMessage (0, "syslogd started: BusyBox v" BB_VER " (" BB_BT ")"); |
258 | 339 | ||
259 | for (;;) { | 340 | for (;;) { |
@@ -322,6 +403,13 @@ static void doKlogd (void) | |||
322 | signal(SIGKILL, klogd_signal); | 403 | signal(SIGKILL, klogd_signal); |
323 | signal(SIGTERM, klogd_signal); | 404 | signal(SIGTERM, klogd_signal); |
324 | signal(SIGHUP, SIG_IGN); | 405 | signal(SIGHUP, SIG_IGN); |
406 | |||
407 | #ifdef BB_FEATURE_REMOTE_LOG | ||
408 | if (doRemoteLog == TRUE){ | ||
409 | init_RemoteLog(); | ||
410 | } | ||
411 | #endif | ||
412 | |||
325 | logMessage(0, "klogd started: " | 413 | logMessage(0, "klogd started: " |
326 | "BusyBox v" BB_VER " (" BB_BT ")"); | 414 | "BusyBox v" BB_VER " (" BB_BT ")"); |
327 | 415 | ||
@@ -423,6 +511,20 @@ extern int syslogd_main(int argc, char **argv) | |||
423 | logFilePath = *(++argv1); | 511 | logFilePath = *(++argv1); |
424 | stopDoingThat = TRUE; | 512 | stopDoingThat = TRUE; |
425 | break; | 513 | break; |
514 | #ifdef BB_FEATURE_REMOTE_LOG | ||
515 | case 'R': | ||
516 | if (--argc == 0) { | ||
517 | usage(syslogd_usage); | ||
518 | } | ||
519 | RemoteHost = *(++argv1); | ||
520 | if ( (p = strchr(RemoteHost, ':'))){ | ||
521 | RemotePort = atoi(p+1); | ||
522 | *p = '\0'; | ||
523 | } | ||
524 | doRemoteLog = TRUE; | ||
525 | stopDoingThat = TRUE; | ||
526 | break; | ||
527 | #endif | ||
426 | default: | 528 | default: |
427 | usage(syslogd_usage); | 529 | usage(syslogd_usage); |
428 | } | 530 | } |