aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Andersen <andersen@codepoet.org>1999-11-25 07:30:46 +0000
committerEric Andersen <andersen@codepoet.org>1999-11-25 07:30:46 +0000
commit3843e96252a823b4fdfc89c61d00342bf011ad88 (patch)
treea177e322b9e77485a609429acfdc43824eca0222
parentb99df0fd65abe3245fa2d04115326100847f865e (diff)
downloadbusybox-w32-3843e96252a823b4fdfc89c61d00342bf011ad88.tar.gz
busybox-w32-3843e96252a823b4fdfc89c61d00342bf011ad88.tar.bz2
busybox-w32-3843e96252a823b4fdfc89c61d00342bf011ad88.zip
Stuf
-rw-r--r--Changelog5
-rw-r--r--Makefile2
-rw-r--r--applets/busybox.c14
-rw-r--r--busybox.c14
-rw-r--r--busybox.def.h2
-rw-r--r--internal.h2
-rw-r--r--logger.c186
-rw-r--r--sysklogd/logger.c186
-rw-r--r--sysklogd/syslogd.c316
-rw-r--r--syslogd.c316
10 files changed, 928 insertions, 115 deletions
diff --git a/Changelog b/Changelog
index 7a7f50ab6..cef86ec66 100644
--- a/Changelog
+++ b/Changelog
@@ -10,6 +10,11 @@
10 from createPath into mkdir where it belonged, thereby making tar work properly. 10 from createPath into mkdir where it belonged, thereby making tar work properly.
11 * Fixed an off-by-one bug in cat. Given a list of file it wouldn't cat out the 11 * Fixed an off-by-one bug in cat. Given a list of file it wouldn't cat out the
12 last file in the list. 12 last file in the list.
13 * Fixed 'ls -ln' so numeric group/uid are presented properly, and fixed 'ls -l'
14 so when uid/gid is not in /etc/{passwd,group} the numeric group/uid are
15 presented properly.
16 * Also added a TODO.
17
13 18
14 -Erik Andrsen 19 -Erik Andrsen
15 20
diff --git a/Makefile b/Makefile
index c715c0bd6..1413ea05d 100644
--- a/Makefile
+++ b/Makefile
@@ -22,7 +22,7 @@ BUILDTIME=$(shell date "+%Y%m%d-%H%M")
22 22
23# Comment out the following to make a debuggable build 23# Comment out the following to make a debuggable build
24# Leave this off for production use. 24# Leave this off for production use.
25DODEBUG=false 25DODEBUG=true
26# If you want a static binary, turn this on. I can't think 26# If you want a static binary, turn this on. I can't think
27# of many situations where anybody would ever want it static, 27# of many situations where anybody would ever want it static,
28# but... 28# but...
diff --git a/applets/busybox.c b/applets/busybox.c
index efa4ec72d..af81ad8a2 100644
--- a/applets/busybox.c
+++ b/applets/busybox.c
@@ -151,15 +151,21 @@ static const struct Applet applets[] = {
151#ifdef BB_SLEEP //bin 151#ifdef BB_SLEEP //bin
152 {"sleep", sleep_main}, 152 {"sleep", sleep_main},
153#endif 153#endif
154#ifdef BB_TAR //bin 154#ifdef BB_SYNC //bin
155 {"tar", tar_main}, 155 {"sync", sync_main},
156#endif
157#ifdef BB_SYSLOGD //sbin
158 {"syslogd", syslogd_main},
159#endif
160#ifdef BB_LOGGER //usr/bin
161 {"logger", logger_main},
156#endif 162#endif
157#ifdef BB_SWAPONOFF //sbin 163#ifdef BB_SWAPONOFF //sbin
158 {"swapon", swap_on_off_main}, 164 {"swapon", swap_on_off_main},
159 {"swapoff", swap_on_off_main}, 165 {"swapoff", swap_on_off_main},
160#endif 166#endif
161#ifdef BB_SYNC //bin 167#ifdef BB_TAR //bin
162 {"sync", sync_main}, 168 {"tar", tar_main},
163#endif 169#endif
164#ifdef BB_TOUCH //usr/bin 170#ifdef BB_TOUCH //usr/bin
165 {"touch", touch_main}, 171 {"touch", touch_main},
diff --git a/busybox.c b/busybox.c
index efa4ec72d..af81ad8a2 100644
--- a/busybox.c
+++ b/busybox.c
@@ -151,15 +151,21 @@ static const struct Applet applets[] = {
151#ifdef BB_SLEEP //bin 151#ifdef BB_SLEEP //bin
152 {"sleep", sleep_main}, 152 {"sleep", sleep_main},
153#endif 153#endif
154#ifdef BB_TAR //bin 154#ifdef BB_SYNC //bin
155 {"tar", tar_main}, 155 {"sync", sync_main},
156#endif
157#ifdef BB_SYSLOGD //sbin
158 {"syslogd", syslogd_main},
159#endif
160#ifdef BB_LOGGER //usr/bin
161 {"logger", logger_main},
156#endif 162#endif
157#ifdef BB_SWAPONOFF //sbin 163#ifdef BB_SWAPONOFF //sbin
158 {"swapon", swap_on_off_main}, 164 {"swapon", swap_on_off_main},
159 {"swapoff", swap_on_off_main}, 165 {"swapoff", swap_on_off_main},
160#endif 166#endif
161#ifdef BB_SYNC //bin 167#ifdef BB_TAR //bin
162 {"sync", sync_main}, 168 {"tar", tar_main},
163#endif 169#endif
164#ifdef BB_TOUCH //usr/bin 170#ifdef BB_TOUCH //usr/bin
165 {"touch", touch_main}, 171 {"touch", touch_main},
diff --git a/busybox.def.h b/busybox.def.h
index 77ab4b567..bf8f54556 100644
--- a/busybox.def.h
+++ b/busybox.def.h
@@ -28,6 +28,7 @@
28#define BB_LN 28#define BB_LN
29#define BB_LOADFONT 29#define BB_LOADFONT
30#define BB_LOADKMAP 30#define BB_LOADKMAP
31#define BB_LOGGER
31#define BB_LS 32#define BB_LS
32//#define BB_MAKEDEVS 33//#define BB_MAKEDEVS
33//#define BB_MATH 34//#define BB_MATH
@@ -53,6 +54,7 @@
53#define BB_SLEEP 54#define BB_SLEEP
54#define BB_SWAPONOFF 55#define BB_SWAPONOFF
55#define BB_SYNC 56#define BB_SYNC
57#define BB_SYSLOGD
56#define BB_TAR 58#define BB_TAR
57#define BB_TOUCH 59#define BB_TOUCH
58#define BB_TRUE_FALSE 60#define BB_TRUE_FALSE
diff --git a/internal.h b/internal.h
index feaea6339..5cca663b4 100644
--- a/internal.h
+++ b/internal.h
@@ -104,6 +104,8 @@ extern int sed_main(int argc, char** argv);
104extern int sleep_main(int argc, char** argv); 104extern int sleep_main(int argc, char** argv);
105extern int swap_on_off_main(int argc, char** argv); 105extern int swap_on_off_main(int argc, char** argv);
106extern int sync_main(int argc, char** argv); 106extern int sync_main(int argc, char** argv);
107extern int syslogd_main(int argc, char **argv);
108extern int logger_main(int argc, char **argv);
107extern int tar_main(int argc, char** argv); 109extern int tar_main(int argc, char** argv);
108extern int touch_main(int argc, char** argv); 110extern int touch_main(int argc, char** argv);
109extern int tput_main(int argc, char** argv); 111extern int tput_main(int argc, char** argv);
diff --git a/logger.c b/logger.c
new file mode 100644
index 000000000..0683838cc
--- /dev/null
+++ b/logger.c
@@ -0,0 +1,186 @@
1/*
2 * Mini logger implementation for busybox
3 *
4 * Copyright (C) 1999 by Lineo, inc.
5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include "internal.h"
24#include <stdio.h>
25#include <sys/socket.h>
26#include <sys/un.h>
27#include <unistd.h>
28#include <time.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <signal.h>
33#include <ctype.h>
34#include <netdb.h>
35
36#if !defined BB_SYSLOGD
37
38#define SYSLOG_NAMES
39#include <sys/syslog.h>
40
41#else
42/* We have to do this since the header file defines static
43 * structues. Argh.... bad libc, bad, bad...
44 */
45#include <sys/syslog.h>
46typedef struct _code {
47 char *c_name;
48 int c_val;
49} CODE;
50extern CODE prioritynames[];
51extern CODE facilitynames[];
52#endif
53
54static const char logger_usage[] =
55 "logger [OPTION]... [MESSAGE]\n\n"
56 "Write MESSAGE to the system log. If MESSAGE is '-', log stdin.\n\n"
57 "Options:\n"
58 "\t-s\tLog to stderr as well as the system log.\n"
59 "\t-p\tEnter the message with the specified priority.\n"
60 "\t\tThis may be numerical or a ``facility.level'' pair.\n";
61
62
63/* Decode a symbolic name to a numeric value
64 * this function is based on code
65 * Copyright (c) 1983, 1993
66 * The Regents of the University of California. All rights reserved.
67 */
68static int
69decode(char* name, CODE* codetab)
70{
71 CODE *c;
72
73 if (isdigit(*name))
74 return (atoi(name));
75 for (c = codetab; c->c_name; c++) {
76 if (!strcasecmp(name, c->c_name)) {
77 return (c->c_val);
78 }
79 }
80
81 return (-1);
82}
83
84/* Decode a symbolic name to a numeric value
85 * this function is based on code
86 * Copyright (c) 1983, 1993
87 * The Regents of the University of California. All rights reserved.
88 */
89static int
90pencode(char* s)
91{
92 char *save;
93 int lev, fac=LOG_USER;
94
95 for (save = s; *s && *s != '.'; ++s);
96 if (*s) {
97 *s = '\0';
98 fac = decode(save, facilitynames);
99 if (fac < 0) {
100 fprintf(stderr, "unknown facility name: %s\n", save);
101 exit( FALSE);
102 }
103 *s++ = '.';
104 }
105 else {
106 s = save;
107 }
108 lev = decode(s, prioritynames);
109 if (lev < 0) {
110 fprintf(stderr, "unknown priority name: %s\n", save);
111 exit( FALSE);
112 }
113 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
114}
115
116
117extern int logger_main(int argc, char **argv)
118{
119 struct sockaddr_un sunx;
120 int fd, pri = LOG_USER|LOG_NOTICE;
121 int toStdErrFlag=FALSE;
122 char *message, buf[1024];
123 time_t now;
124 size_t addrLength;
125
126 /* Parse any options */
127 while (--argc > 0 && **(++argv) == '-') {
128 while (*(++(*argv))) {
129 switch (**argv) {
130 case 's':
131 toStdErrFlag = TRUE;
132 break;
133 case 'p':
134 if (--argc == 0) {
135 usage(logger_usage);
136 }
137 pri = pencode(*(++argv));
138 if (--argc == 0) {
139 usage(logger_usage);
140 }
141 ++argv;
142 break;
143 default:
144 usage(logger_usage);
145 }
146 }
147 }
148
149 if (argc>=1)
150 if (**argv=='-') {
151 /* read from stdin */
152 } else {
153 message=*argv;
154 }
155 else {
156 fprintf(stderr, "No message\n");
157 exit( FALSE);
158 }
159
160 memset(&sunx, 0, sizeof(sunx));
161 sunx.sun_family = AF_UNIX; /* Unix domain socket */
162 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
163 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 ) {
164 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
165 exit( FALSE);
166 }
167
168 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
169
170 if (connect(fd, (struct sockaddr *) &sunx, addrLength)) {
171 perror("Could not connect to socket " _PATH_LOG);
172 exit( FALSE);
173 }
174
175 time(&now);
176 snprintf (buf, sizeof(buf), "<%d>%.15s %s", pri, ctime(&now)+4, message);
177
178 if (toStdErrFlag==TRUE)
179 fprintf(stderr, "%s\n", buf);
180
181 write( fd, buf, sizeof(buf));
182
183 close(fd);
184 exit( TRUE);
185}
186
diff --git a/sysklogd/logger.c b/sysklogd/logger.c
new file mode 100644
index 000000000..0683838cc
--- /dev/null
+++ b/sysklogd/logger.c
@@ -0,0 +1,186 @@
1/*
2 * Mini logger implementation for busybox
3 *
4 * Copyright (C) 1999 by Lineo, inc.
5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#include "internal.h"
24#include <stdio.h>
25#include <sys/socket.h>
26#include <sys/un.h>
27#include <unistd.h>
28#include <time.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <signal.h>
33#include <ctype.h>
34#include <netdb.h>
35
36#if !defined BB_SYSLOGD
37
38#define SYSLOG_NAMES
39#include <sys/syslog.h>
40
41#else
42/* We have to do this since the header file defines static
43 * structues. Argh.... bad libc, bad, bad...
44 */
45#include <sys/syslog.h>
46typedef struct _code {
47 char *c_name;
48 int c_val;
49} CODE;
50extern CODE prioritynames[];
51extern CODE facilitynames[];
52#endif
53
54static const char logger_usage[] =
55 "logger [OPTION]... [MESSAGE]\n\n"
56 "Write MESSAGE to the system log. If MESSAGE is '-', log stdin.\n\n"
57 "Options:\n"
58 "\t-s\tLog to stderr as well as the system log.\n"
59 "\t-p\tEnter the message with the specified priority.\n"
60 "\t\tThis may be numerical or a ``facility.level'' pair.\n";
61
62
63/* Decode a symbolic name to a numeric value
64 * this function is based on code
65 * Copyright (c) 1983, 1993
66 * The Regents of the University of California. All rights reserved.
67 */
68static int
69decode(char* name, CODE* codetab)
70{
71 CODE *c;
72
73 if (isdigit(*name))
74 return (atoi(name));
75 for (c = codetab; c->c_name; c++) {
76 if (!strcasecmp(name, c->c_name)) {
77 return (c->c_val);
78 }
79 }
80
81 return (-1);
82}
83
84/* Decode a symbolic name to a numeric value
85 * this function is based on code
86 * Copyright (c) 1983, 1993
87 * The Regents of the University of California. All rights reserved.
88 */
89static int
90pencode(char* s)
91{
92 char *save;
93 int lev, fac=LOG_USER;
94
95 for (save = s; *s && *s != '.'; ++s);
96 if (*s) {
97 *s = '\0';
98 fac = decode(save, facilitynames);
99 if (fac < 0) {
100 fprintf(stderr, "unknown facility name: %s\n", save);
101 exit( FALSE);
102 }
103 *s++ = '.';
104 }
105 else {
106 s = save;
107 }
108 lev = decode(s, prioritynames);
109 if (lev < 0) {
110 fprintf(stderr, "unknown priority name: %s\n", save);
111 exit( FALSE);
112 }
113 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
114}
115
116
117extern int logger_main(int argc, char **argv)
118{
119 struct sockaddr_un sunx;
120 int fd, pri = LOG_USER|LOG_NOTICE;
121 int toStdErrFlag=FALSE;
122 char *message, buf[1024];
123 time_t now;
124 size_t addrLength;
125
126 /* Parse any options */
127 while (--argc > 0 && **(++argv) == '-') {
128 while (*(++(*argv))) {
129 switch (**argv) {
130 case 's':
131 toStdErrFlag = TRUE;
132 break;
133 case 'p':
134 if (--argc == 0) {
135 usage(logger_usage);
136 }
137 pri = pencode(*(++argv));
138 if (--argc == 0) {
139 usage(logger_usage);
140 }
141 ++argv;
142 break;
143 default:
144 usage(logger_usage);
145 }
146 }
147 }
148
149 if (argc>=1)
150 if (**argv=='-') {
151 /* read from stdin */
152 } else {
153 message=*argv;
154 }
155 else {
156 fprintf(stderr, "No message\n");
157 exit( FALSE);
158 }
159
160 memset(&sunx, 0, sizeof(sunx));
161 sunx.sun_family = AF_UNIX; /* Unix domain socket */
162 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
163 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 ) {
164 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
165 exit( FALSE);
166 }
167
168 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
169
170 if (connect(fd, (struct sockaddr *) &sunx, addrLength)) {
171 perror("Could not connect to socket " _PATH_LOG);
172 exit( FALSE);
173 }
174
175 time(&now);
176 snprintf (buf, sizeof(buf), "<%d>%.15s %s", pri, ctime(&now)+4, message);
177
178 if (toStdErrFlag==TRUE)
179 fprintf(stderr, "%s\n", buf);
180
181 write( fd, buf, sizeof(buf));
182
183 close(fd);
184 exit( TRUE);
185}
186
diff --git a/sysklogd/syslogd.c b/sysklogd/syslogd.c
index 03fea9c76..736adf7d1 100644
--- a/sysklogd/syslogd.c
+++ b/sysklogd/syslogd.c
@@ -1,86 +1,296 @@
1/* userver.c - simple server for Unix domain sockets */ 1/*
2 2 * Mini syslogd implementation for busybox
3/* Waits for a connection on the ./sample-socket Unix domain 3 *
4 socket. Once a connection has been established, copy data 4 * Copyright (C) 1999 by Lineo, inc.
5 from the socket to stdout until the other end closes the 5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
6 connection, and then wait for another connection to the socket. */ 6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
7 22
23#include "internal.h"
8#include <stdio.h> 24#include <stdio.h>
9#include <sys/socket.h> 25#include <sys/socket.h>
10#include <sys/un.h> 26#include <sys/un.h>
11#include <unistd.h> 27#include <unistd.h>
28#include <time.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <signal.h>
33#include <ctype.h>
34#include <netdb.h>
35
36
37/* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
38#define SYSLOG_NAMES
39#include <sys/syslog.h>
40
41/* Path for the file where all log messages are written */
42#define __LOG_FILE "/var/log/messages"
43/* Path to the current console device */
44#define __DEV_CONSOLE "/dev/console"
45
46
47static char* logFilePath = __LOG_FILE;
48/* interval between marks in seconds */
49static int MarkInterval = 20*60;
50/* localhost's name */
51static char LocalHostName[32];
12 52
13#define _PATH_LOG "/dev/log" 53static const char syslogd_usage[] =
54 "syslogd [OPTION]...\n\n"
55 "Linux system logging utility.\n\n"
56 "Options:\n"
57 "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
58 "\t-n\tDo not fork into the background (for when run by init)\n"
59 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
14 60
15 61
16/* issue an error message via perror() and terminate the program */ 62
17void die(char * message) { 63/* try to open up the specified device */
18 perror(message); 64static int device_open(char *device, int mode)
19 exit(1); 65{
66 int m, f, fd = -1;
67
68 m = mode | O_NONBLOCK;
69
70 /* Retry up to 5 times */
71 for (f = 0; f < 5; f++)
72 if ((fd = open(device, m)) >= 0)
73 break;
74 if (fd < 0)
75 return fd;
76 /* Reset original flags. */
77 if (m != mode)
78 fcntl(fd, F_SETFL, mode);
79 return fd;
20} 80}
21 81
22/* Copies data from file descriptor 'from' to file descriptor 'to' 82/* print a message to the log file */
23 until nothing is left to be copied. Exits if an error occurs. */ 83static void message(char *fmt, ...)
24void copyData(int from, int to) { 84{
25 char buf[1024]; 85 int fd;
26 int amount; 86 va_list arguments;
27 87
28 while ((amount = read(from, buf, sizeof(buf))) > 0) { 88 if ((fd = device_open(logFilePath, O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND|O_NONBLOCK)) >= 0) {
29 if (write(to, buf, amount) != amount) { 89 va_start(arguments, fmt);
30 die("write"); 90 vdprintf(fd, fmt, arguments);
31 return; 91 va_end(arguments);
32 } 92 close(fd);
93 } else {
94 /* Always send console messages to /dev/console so people will see them. */
95 if ((fd = device_open(__DEV_CONSOLE, O_WRONLY|O_NOCTTY|O_NONBLOCK)) >= 0) {
96 va_start(arguments, fmt);
97 vdprintf(fd, fmt, arguments);
98 va_end(arguments);
99 close(fd);
100 } else {
101 fprintf(stderr, "Bummer, can't print: ");
102 va_start(arguments, fmt);
103 vfprintf(stderr, fmt, arguments);
104 fflush(stderr);
105 va_end(arguments);
106 }
107 }
108}
109
110static void logMessage( int pri, char* msg)
111{
112 time_t now;
113 char *timestamp;
114 static char res[20];
115 CODE *c_pri, *c_fac;
116
117 for (c_fac=facilitynames; c_fac->c_name && !(c_fac->c_val==LOG_FAC(pri)<<3); c_fac++);
118 for (c_pri=prioritynames; c_pri->c_name && !(c_pri->c_val==LOG_PRI(pri)); c_pri++);
119 if (*c_fac->c_name=='\0' || *c_pri->c_name=='\0')
120 snprintf (res, sizeof(res), "<%d>", pri);
121 else
122 snprintf (res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
123
124 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
125 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
126 {
127 time(&now);
128 timestamp = ctime(&now) + 4;
129 timestamp[15] = '\0';
130 } else {
131 timestamp = msg;
132 timestamp[15] = '\0';
133 msg += 16;
33 } 134 }
34 if (amount < 0) 135
35 die("read"); 136 /* todo: supress duplicates */
137
138 /* now spew out the message to wherever it is supposed to go */
139 message( "%s %s %s %s\n", timestamp, LocalHostName, res, msg);
36} 140}
37 141
142static void quit_signal(int sig)
143{
144 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd exiting");
145 exit( TRUE);
146}
38 147
39int main(void) { 148static void restart_signal(int sig)
40 struct sockaddr_un address; 149{
150 /* pretend to restart */
151 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd restarting");
152}
153
154static void domark(int sig)
155{
156 if (MarkInterval > 0) {
157 logMessage(LOG_SYSLOG|LOG_INFO, "-- MARK --");
158 signal(SIGALRM, domark);
159 alarm(MarkInterval);
160 }
161}
162
163static void doSyslogd(void)
164{
165 struct sockaddr_un sunx;
41 int fd, conn; 166 int fd, conn;
42 size_t addrLength; 167 size_t addrLength;
168 char buf[1024];
169 char *q, *p = buf;
170 int readSize;
43 171
44 /* Remove any preexisting socket (or other file) */ 172 /* Remove any preexisting socket/file */
45 unlink(_PATH_LOG); 173 unlink(_PATH_LOG);
46 174
47 memset(&address, 0, sizeof(address)); 175 /* Set up sig handlers */
48 address.sun_family = AF_UNIX; /* Unix domain socket */ 176 signal(SIGINT, quit_signal);
49 strncpy(address.sun_path, _PATH_LOG, sizeof(address.sun_path)); 177 signal(SIGTERM, quit_signal);
178 signal(SIGQUIT, quit_signal);
179 signal(SIGHUP, restart_signal);
180 signal(SIGALRM, domark);
181 alarm(MarkInterval);
50 182
51 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) 183 memset(&sunx, 0, sizeof(sunx));
52 die("socket"); 184 sunx.sun_family = AF_UNIX; /* Unix domain socket */
53 185 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
54 186 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 ) {
55 /* The total length of the address includes the sun_family 187 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
56 element */ 188 exit( FALSE);
57 addrLength = sizeof(address.sun_family) + 189 }
58 strlen(address.sun_path);
59 190
60 if (bind(fd, (struct sockaddr *) &address, addrLength)) 191 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
61 die("bind"); 192 if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
193 (chmod(_PATH_LOG, 0666) < 0) ||
194 (listen(fd, 5)) )
195 {
196 perror("Could not connect to socket " _PATH_LOG);
197 exit( FALSE);
198 }
199
200
201 /* Get localhost's name */
202 gethostname(LocalHostName, sizeof(LocalHostName));
203 if ( (p = strchr(LocalHostName, '.')) ) {
204 *p++ = '\0';
205 }
206
207 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
208 "BusyBox v" BB_VER " (" BB_BT ") multi-call binary");
62 209
63 if (chmod(_PATH_LOG, 0666) < 0)
64 die("chmod");
65 210
66 if (listen(fd, 5)) 211 while ((conn = accept(fd, (struct sockaddr *) &sunx,
67 die("listen"); 212 &addrLength)) >= 0)
213 {
214 while ((readSize=read(conn, buf, sizeof(buf))) > 0)
215 {
216 char line[1025];
217 unsigned char c;
218 int pri = (LOG_USER|LOG_NOTICE);
68 219
220 memset (line, 0, sizeof(line));
221 p = buf;
222 q = line;
223 while ( p && (c = *p) && q < &line[sizeof(line) - 1]) {
224 if (c == '<') {
225 /* Parse the magic priority number */
226 pri = 0;
227 while (isdigit(*(++p))) {
228 pri = 10 * pri + (*p - '0');
229 }
230 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
231 pri = (LOG_USER|LOG_NOTICE);
232 } else if (c == '\n') {
233 *q++ = ' ';
234 } else if (iscntrl(c)&&(c<0177)) {
235 *q++ = '^';
236 *q++ = c ^ 0100;
237 } else {
238 *q++ = c;
239 }
240 p++;
241 }
242 *q = '\0';
69 243
70 while ((conn = accept(fd, (struct sockaddr *) &address, 244 /* Now log it */
71 &addrLength)) >= 0) { 245 logMessage( pri, line);
72 printf("---- getting data\n"); 246 }
73 copyData(conn, fileno(stdout));
74 printf("---- done\n");
75 close(conn); 247 close(conn);
76 } 248 }
77 249
78 if (conn < 0)
79 die("accept");
80
81 close(fd); 250 close(fd);
82 return 0;
83} 251}
84 252
85 253
254extern int syslogd_main(int argc, char **argv)
255{
256 int pid;
257 int doFork = TRUE;
258
259 while (--argc > 0 && **(++argv) == '-') {
260 while (*(++(*argv))) {
261 switch (**argv) {
262 case 'm':
263 if (--argc == 0) {
264 usage(syslogd_usage);
265 }
266 MarkInterval = atoi(*(++argv))*60;
267 break;
268 case 'n':
269 doFork = FALSE;
270 break;
271 case 'O':
272 if (--argc == 0) {
273 usage(syslogd_usage);
274 }
275 logFilePath = *(++argv);
276 break;
277 default:
278 usage(syslogd_usage);
279 }
280 }
281 }
282
283 if (doFork == TRUE) {
284 pid = fork();
285 if ( pid < 0 )
286 exit( pid);
287 else if ( pid == 0 ) {
288 doSyslogd();
289 }
290 } else {
291 doSyslogd();
292 }
293 exit( TRUE);
294}
295
86 296
diff --git a/syslogd.c b/syslogd.c
index 03fea9c76..736adf7d1 100644
--- a/syslogd.c
+++ b/syslogd.c
@@ -1,86 +1,296 @@
1/* userver.c - simple server for Unix domain sockets */ 1/*
2 2 * Mini syslogd implementation for busybox
3/* Waits for a connection on the ./sample-socket Unix domain 3 *
4 socket. Once a connection has been established, copy data 4 * Copyright (C) 1999 by Lineo, inc.
5 from the socket to stdout until the other end closes the 5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
6 connection, and then wait for another connection to the socket. */ 6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
7 22
23#include "internal.h"
8#include <stdio.h> 24#include <stdio.h>
9#include <sys/socket.h> 25#include <sys/socket.h>
10#include <sys/un.h> 26#include <sys/un.h>
11#include <unistd.h> 27#include <unistd.h>
28#include <time.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <signal.h>
33#include <ctype.h>
34#include <netdb.h>
35
36
37/* SYSLOG_NAMES defined to pull some extra junk from syslog.h */
38#define SYSLOG_NAMES
39#include <sys/syslog.h>
40
41/* Path for the file where all log messages are written */
42#define __LOG_FILE "/var/log/messages"
43/* Path to the current console device */
44#define __DEV_CONSOLE "/dev/console"
45
46
47static char* logFilePath = __LOG_FILE;
48/* interval between marks in seconds */
49static int MarkInterval = 20*60;
50/* localhost's name */
51static char LocalHostName[32];
12 52
13#define _PATH_LOG "/dev/log" 53static const char syslogd_usage[] =
54 "syslogd [OPTION]...\n\n"
55 "Linux system logging utility.\n\n"
56 "Options:\n"
57 "\t-m\tChange the mark timestamp interval. default=20min. 0=off\n"
58 "\t-n\tDo not fork into the background (for when run by init)\n"
59 "\t-O\tSpecify an alternate log file. default=/var/log/messages\n";
14 60
15 61
16/* issue an error message via perror() and terminate the program */ 62
17void die(char * message) { 63/* try to open up the specified device */
18 perror(message); 64static int device_open(char *device, int mode)
19 exit(1); 65{
66 int m, f, fd = -1;
67
68 m = mode | O_NONBLOCK;
69
70 /* Retry up to 5 times */
71 for (f = 0; f < 5; f++)
72 if ((fd = open(device, m)) >= 0)
73 break;
74 if (fd < 0)
75 return fd;
76 /* Reset original flags. */
77 if (m != mode)
78 fcntl(fd, F_SETFL, mode);
79 return fd;
20} 80}
21 81
22/* Copies data from file descriptor 'from' to file descriptor 'to' 82/* print a message to the log file */
23 until nothing is left to be copied. Exits if an error occurs. */ 83static void message(char *fmt, ...)
24void copyData(int from, int to) { 84{
25 char buf[1024]; 85 int fd;
26 int amount; 86 va_list arguments;
27 87
28 while ((amount = read(from, buf, sizeof(buf))) > 0) { 88 if ((fd = device_open(logFilePath, O_WRONLY|O_CREAT|O_NOCTTY|O_APPEND|O_NONBLOCK)) >= 0) {
29 if (write(to, buf, amount) != amount) { 89 va_start(arguments, fmt);
30 die("write"); 90 vdprintf(fd, fmt, arguments);
31 return; 91 va_end(arguments);
32 } 92 close(fd);
93 } else {
94 /* Always send console messages to /dev/console so people will see them. */
95 if ((fd = device_open(__DEV_CONSOLE, O_WRONLY|O_NOCTTY|O_NONBLOCK)) >= 0) {
96 va_start(arguments, fmt);
97 vdprintf(fd, fmt, arguments);
98 va_end(arguments);
99 close(fd);
100 } else {
101 fprintf(stderr, "Bummer, can't print: ");
102 va_start(arguments, fmt);
103 vfprintf(stderr, fmt, arguments);
104 fflush(stderr);
105 va_end(arguments);
106 }
107 }
108}
109
110static void logMessage( int pri, char* msg)
111{
112 time_t now;
113 char *timestamp;
114 static char res[20];
115 CODE *c_pri, *c_fac;
116
117 for (c_fac=facilitynames; c_fac->c_name && !(c_fac->c_val==LOG_FAC(pri)<<3); c_fac++);
118 for (c_pri=prioritynames; c_pri->c_name && !(c_pri->c_val==LOG_PRI(pri)); c_pri++);
119 if (*c_fac->c_name=='\0' || *c_pri->c_name=='\0')
120 snprintf (res, sizeof(res), "<%d>", pri);
121 else
122 snprintf (res, sizeof(res), "%s.%s", c_fac->c_name, c_pri->c_name);
123
124 if (strlen(msg) < 16 || msg[3] != ' ' || msg[6] != ' ' ||
125 msg[9] != ':' || msg[12] != ':' || msg[15] != ' ')
126 {
127 time(&now);
128 timestamp = ctime(&now) + 4;
129 timestamp[15] = '\0';
130 } else {
131 timestamp = msg;
132 timestamp[15] = '\0';
133 msg += 16;
33 } 134 }
34 if (amount < 0) 135
35 die("read"); 136 /* todo: supress duplicates */
137
138 /* now spew out the message to wherever it is supposed to go */
139 message( "%s %s %s %s\n", timestamp, LocalHostName, res, msg);
36} 140}
37 141
142static void quit_signal(int sig)
143{
144 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd exiting");
145 exit( TRUE);
146}
38 147
39int main(void) { 148static void restart_signal(int sig)
40 struct sockaddr_un address; 149{
150 /* pretend to restart */
151 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd restarting");
152}
153
154static void domark(int sig)
155{
156 if (MarkInterval > 0) {
157 logMessage(LOG_SYSLOG|LOG_INFO, "-- MARK --");
158 signal(SIGALRM, domark);
159 alarm(MarkInterval);
160 }
161}
162
163static void doSyslogd(void)
164{
165 struct sockaddr_un sunx;
41 int fd, conn; 166 int fd, conn;
42 size_t addrLength; 167 size_t addrLength;
168 char buf[1024];
169 char *q, *p = buf;
170 int readSize;
43 171
44 /* Remove any preexisting socket (or other file) */ 172 /* Remove any preexisting socket/file */
45 unlink(_PATH_LOG); 173 unlink(_PATH_LOG);
46 174
47 memset(&address, 0, sizeof(address)); 175 /* Set up sig handlers */
48 address.sun_family = AF_UNIX; /* Unix domain socket */ 176 signal(SIGINT, quit_signal);
49 strncpy(address.sun_path, _PATH_LOG, sizeof(address.sun_path)); 177 signal(SIGTERM, quit_signal);
178 signal(SIGQUIT, quit_signal);
179 signal(SIGHUP, restart_signal);
180 signal(SIGALRM, domark);
181 alarm(MarkInterval);
50 182
51 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) 183 memset(&sunx, 0, sizeof(sunx));
52 die("socket"); 184 sunx.sun_family = AF_UNIX; /* Unix domain socket */
53 185 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
54 186 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 ) {
55 /* The total length of the address includes the sun_family 187 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
56 element */ 188 exit( FALSE);
57 addrLength = sizeof(address.sun_family) + 189 }
58 strlen(address.sun_path);
59 190
60 if (bind(fd, (struct sockaddr *) &address, addrLength)) 191 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
61 die("bind"); 192 if ( (bind(fd, (struct sockaddr *) &sunx, addrLength)) ||
193 (chmod(_PATH_LOG, 0666) < 0) ||
194 (listen(fd, 5)) )
195 {
196 perror("Could not connect to socket " _PATH_LOG);
197 exit( FALSE);
198 }
199
200
201 /* Get localhost's name */
202 gethostname(LocalHostName, sizeof(LocalHostName));
203 if ( (p = strchr(LocalHostName, '.')) ) {
204 *p++ = '\0';
205 }
206
207 logMessage(LOG_SYSLOG|LOG_INFO, "syslogd started: "
208 "BusyBox v" BB_VER " (" BB_BT ") multi-call binary");
62 209
63 if (chmod(_PATH_LOG, 0666) < 0)
64 die("chmod");
65 210
66 if (listen(fd, 5)) 211 while ((conn = accept(fd, (struct sockaddr *) &sunx,
67 die("listen"); 212 &addrLength)) >= 0)
213 {
214 while ((readSize=read(conn, buf, sizeof(buf))) > 0)
215 {
216 char line[1025];
217 unsigned char c;
218 int pri = (LOG_USER|LOG_NOTICE);
68 219
220 memset (line, 0, sizeof(line));
221 p = buf;
222 q = line;
223 while ( p && (c = *p) && q < &line[sizeof(line) - 1]) {
224 if (c == '<') {
225 /* Parse the magic priority number */
226 pri = 0;
227 while (isdigit(*(++p))) {
228 pri = 10 * pri + (*p - '0');
229 }
230 if (pri &~ (LOG_FACMASK|LOG_PRIMASK))
231 pri = (LOG_USER|LOG_NOTICE);
232 } else if (c == '\n') {
233 *q++ = ' ';
234 } else if (iscntrl(c)&&(c<0177)) {
235 *q++ = '^';
236 *q++ = c ^ 0100;
237 } else {
238 *q++ = c;
239 }
240 p++;
241 }
242 *q = '\0';
69 243
70 while ((conn = accept(fd, (struct sockaddr *) &address, 244 /* Now log it */
71 &addrLength)) >= 0) { 245 logMessage( pri, line);
72 printf("---- getting data\n"); 246 }
73 copyData(conn, fileno(stdout));
74 printf("---- done\n");
75 close(conn); 247 close(conn);
76 } 248 }
77 249
78 if (conn < 0)
79 die("accept");
80
81 close(fd); 250 close(fd);
82 return 0;
83} 251}
84 252
85 253
254extern int syslogd_main(int argc, char **argv)
255{
256 int pid;
257 int doFork = TRUE;
258
259 while (--argc > 0 && **(++argv) == '-') {
260 while (*(++(*argv))) {
261 switch (**argv) {
262 case 'm':
263 if (--argc == 0) {
264 usage(syslogd_usage);
265 }
266 MarkInterval = atoi(*(++argv))*60;
267 break;
268 case 'n':
269 doFork = FALSE;
270 break;
271 case 'O':
272 if (--argc == 0) {
273 usage(syslogd_usage);
274 }
275 logFilePath = *(++argv);
276 break;
277 default:
278 usage(syslogd_usage);
279 }
280 }
281 }
282
283 if (doFork == TRUE) {
284 pid = fork();
285 if ( pid < 0 )
286 exit( pid);
287 else if ( pid == 0 ) {
288 doSyslogd();
289 }
290 } else {
291 doSyslogd();
292 }
293 exit( TRUE);
294}
295
86 296