2 * Mini logger implementation for busybox
4 * Copyright (C) 1999 by Lineo, inc.
5 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
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.
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.
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
25 #include <sys/socket.h>
29 #include <sys/types.h>
36 #if !defined BB_SYSLOGD
39 #include <sys/syslog.h>
42 /* We have to do this since the header file defines static
43 * structues. Argh.... bad libc, bad, bad...
45 #include <sys/syslog.h>
46 typedef struct _code {
50 extern CODE prioritynames[];
51 extern CODE facilitynames[];
54 static const char logger_usage[] =
55 "logger [OPTION]... [MESSAGE]\n\n"
56 "Write MESSAGE to the system log. If MESSAGE is '-', log stdin.\n\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";
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.
69 decode(char* name, CODE* codetab)
75 for (c = codetab; c->c_name; c++) {
76 if (!strcasecmp(name, c->c_name)) {
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.
93 int lev, fac=LOG_USER;
95 for (save = s; *s && *s != '.'; ++s);
98 fac = decode(save, facilitynames);
100 fprintf(stderr, "unknown facility name: %s\n", save);
108 lev = decode(s, prioritynames);
110 fprintf(stderr, "unknown priority name: %s\n", save);
113 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
117 extern int logger_main(int argc, char **argv)
119 struct sockaddr_un sunx;
120 int fd, pri = LOG_USER|LOG_NOTICE;
121 int fromStdinFlag=FALSE;
122 int toStdErrFlag=FALSE;
123 char *message, buf[1024], buf1[1024];
127 /* Parse any options */
128 while (--argc > 0 && **(++argv) == '-') {
129 if (*((*argv)+1) == '\0') {
132 while (*(++(*argv))) {
141 pri = pencode(*(++argv));
153 if (fromStdinFlag==TRUE) {
154 /* read from stdin */
157 while ((c = getc(stdin)) != EOF && i<sizeof(buf1)) {
165 fprintf(stderr, "No message\n");
170 memset(&sunx, 0, sizeof(sunx));
171 sunx.sun_family = AF_UNIX; /* Unix domain socket */
172 strncpy(sunx.sun_path, _PATH_LOG, sizeof(sunx.sun_path));
173 if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0 ) {
174 perror("Couldn't obtain descriptor for socket " _PATH_LOG);
178 addrLength = sizeof(sunx.sun_family) + strlen(sunx.sun_path);
180 if (connect(fd, (struct sockaddr *) &sunx, addrLength)) {
181 perror("Could not connect to socket " _PATH_LOG);
186 snprintf (buf, sizeof(buf), "<%d>%.15s %s", pri, ctime(&now)+4, message);
188 if (toStdErrFlag==TRUE)
189 fprintf(stderr, "%s\n", buf);
191 write( fd, buf, sizeof(buf));