2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
34 * - added Native Language Support
35 * Sun Mar 21 1999 - Arnaldo Carvalho de Melo <acme@conectiva.com.br>
36 * - fixed strerr(errno) in gettext calls
47 #include <sys/types.h>
48 #include <sys/socket.h>
55 int decode __P((char *, CODE *));
56 int pencode __P((char *));
57 void usage __P((void));
62 myopenlog(const char *sock) {
64 static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
66 if (strlen(sock) >= sizeof(s_addr.sun_path)) {
67 printf (_("logger: openlog: pathname too long\n"));
71 s_addr.sun_family = AF_UNIX;
72 (void)strcpy(s_addr.sun_path, sock);
74 if ((fd = socket(AF_UNIX, optd ? SOCK_DGRAM : SOCK_STREAM, 0)) == -1) {
75 printf (_("socket: %s.\n"), strerror(errno));
79 if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) {
80 printf (_("connect: %s.\n"), strerror(errno));
87 mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
88 char buf[1000], pid[30], *cp, *tp;
92 if (logflags & LOG_PID)
93 snprintf (pid, sizeof(pid), "[%d]", getpid());
106 snprintf(buf, sizeof(buf), "<%d>%.15s %.200s%s: %.400s",
107 pri, tp, cp, pid, msg);
109 if (write(fd, buf, strlen(buf)+1) < 0)
115 * logger -- read and log utility
117 * Reads from an input and arranges to write the result on the system
121 main(int argc, char **argv) {
122 int ch, logflags, pri;
123 char *tag, buf[1024];
127 setlocale(LC_ALL, "");
128 bindtextdomain(PACKAGE, LOCALEDIR);
134 while ((ch = getopt(argc, argv, "f:ip:st:u:d")) != -1)
136 case 'f': /* file to log */
137 if (freopen(optarg, "r", stdin) == NULL) {
139 (void)fprintf(stderr, _("logger: %s: %s.\n"),
140 optarg, strerror(errsv));
144 case 'i': /* log process id also */
147 case 'p': /* priority */
148 pri = pencode(optarg);
150 case 's': /* log to standard error */
151 logflags |= LOG_PERROR;
156 case 'u': /* unix socket */
160 optd = 1; /* use datagrams */
169 /* setup for logging */
171 openlog(tag ? tag : getlogin(), logflags, 0);
173 LogSock = myopenlog(usock);
175 (void) fclose(stdout);
177 /* log input line if appropriate */
179 register char *p, *endp;
182 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
184 if (p + len > endp && p > buf) {
186 syslog(pri, "%s", buf);
188 mysyslog(LogSock, logflags, pri, tag, buf);
191 if (len > sizeof(buf) - 1) {
193 syslog(pri, "%s", *argv++);
195 mysyslog(LogSock, logflags, pri, tag, *argv++);
199 memmove(p, *argv++, len);
205 syslog(pri, "%s", buf);
207 mysyslog(LogSock, logflags, pri, tag, buf);
210 while (fgets(buf, sizeof(buf), stdin) != NULL) {
211 /* glibc is buggy and adds an additional newline,
212 so we have to remove it here until glibc is fixed */
213 int len = strlen(buf);
215 if (len > 0 && buf[len - 1] == '\n')
219 syslog(pri, "%s", buf);
221 mysyslog(LogSock, logflags, pri, tag, buf);
231 * Decode a symbolic name to a numeric value
240 for (save = s; *s && *s != '.'; ++s);
243 fac = decode(save, facilitynames);
245 (void)fprintf(stderr,
246 _("logger: unknown facility name: %s.\n"), save);
255 lev = decode(s, prioritynames);
257 (void)fprintf(stderr,
258 _("logger: unknown priority name: %s.\n"), save);
261 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
265 decode(name, codetab)
274 for (c = codetab; c->c_name; c++)
275 if (!strcasecmp(name, c->c_name))
284 (void)fprintf(stderr,
285 _("usage: logger [-is] [-f file] [-p pri] [-t tag] [-u socket] [ message ... ]\n"));