]> git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/logger.c
Imported from util-linux-2.10s tarball.
[thirdparty/util-linux.git] / misc-utils / logger.c
1 /*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
33 * 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@misiek.eu.org>
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
37 */
38
39 #include <errno.h>
40 #include <unistd.h>
41 #include <stdlib.h>
42 #include <time.h>
43 #include <stdio.h>
44 #include <ctype.h>
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/socket.h>
48 #include <sys/un.h>
49 #include "nls.h"
50
51 #define SYSLOG_NAMES
52 #include <syslog.h>
53
54 int decode __P((char *, CODE *));
55 int pencode __P((char *));
56 void usage __P((void));
57
58 static int optd = 0;
59
60 static int
61 myopenlog(const char *sock) {
62 int fd;
63 static struct sockaddr_un s_addr; /* AF_UNIX address of local logger */
64
65 if (strlen(sock) >= sizeof(s_addr.sun_path)) {
66 printf ("logger: openlog: pathname too long\n");
67 exit(1);
68 }
69
70 s_addr.sun_family = AF_UNIX;
71 (void)strcpy(s_addr.sun_path, sock);
72
73 if ((fd = socket(AF_UNIX, optd ? SOCK_DGRAM : SOCK_STREAM, 0)) == -1) {
74 printf ("socket: %s.\n", strerror(errno));
75 exit (1);
76 }
77
78 if (connect(fd, (struct sockaddr *) &s_addr, sizeof(s_addr)) == -1) {
79 printf ("connect: %s.\n", strerror(errno));
80 exit (1);
81 }
82 return fd;
83 }
84
85 static void
86 mysyslog(int fd, int logflags, int pri, char *tag, char *msg) {
87 char buf[1000], pid[30], *cp, *tp;
88 time_t now;
89
90 if (fd > -1) {
91 /* avoid snprintf - it does not exist on ancient systems */
92 if (logflags & LOG_PID)
93 sprintf (pid, "[%d]", getpid());
94 else
95 pid[0] = 0;
96 if (tag)
97 cp = tag;
98 else {
99 cp = getlogin();
100 if (!cp)
101 cp = "<someone>";
102 }
103 (void)time(&now);
104 tp = ctime(&now)+4;
105
106 /* do snprintf by hand - ugly, but for once... */
107 sprintf(buf, "<%d>%.15s %.200s%s: %.400s",
108 pri, tp, cp, pid, msg);
109
110 if (write(fd, buf, strlen(buf)+1) < 0)
111 return; /* error */
112 }
113 }
114
115 /*
116 * logger -- read and log utility
117 *
118 * Reads from an input and arranges to write the result on the system
119 * log.
120 */
121 int
122 main(int argc, char **argv) {
123 int ch, logflags, pri;
124 char *tag, buf[1024];
125 char *usock = NULL;
126 int LogSock = -1;
127
128 setlocale(LC_ALL, "");
129 bindtextdomain(PACKAGE, LOCALEDIR);
130 textdomain(PACKAGE);
131
132 tag = NULL;
133 pri = LOG_NOTICE;
134 logflags = 0;
135 while ((ch = getopt(argc, argv, "f:ip:st:u:d")) != EOF)
136 switch((char)ch) {
137 case 'f': /* file to log */
138 if (freopen(optarg, "r", stdin) == NULL) {
139 int errsv = errno;
140 (void)fprintf(stderr, _("logger: %s: %s.\n"),
141 optarg, strerror(errsv));
142 exit(1);
143 }
144 break;
145 case 'i': /* log process id also */
146 logflags |= LOG_PID;
147 break;
148 case 'p': /* priority */
149 pri = pencode(optarg);
150 break;
151 case 's': /* log to standard error */
152 logflags |= LOG_PERROR;
153 break;
154 case 't': /* tag */
155 tag = optarg;
156 break;
157 case 'u': /* unix socket */
158 usock = optarg;
159 break;
160 case 'd':
161 optd = 1; /* use datagrams */
162 break;
163 case '?':
164 default:
165 usage();
166 }
167 argc -= optind;
168 argv += optind;
169
170 /* setup for logging */
171 if (!usock)
172 openlog(tag ? tag : getlogin(), logflags, 0);
173 else
174 LogSock = myopenlog(usock);
175
176 (void) fclose(stdout);
177
178 /* log input line if appropriate */
179 if (argc > 0) {
180 register char *p, *endp;
181 int len;
182
183 for (p = buf, endp = buf + sizeof(buf) - 2; *argv;) {
184 len = strlen(*argv);
185 if (p + len > endp && p > buf) {
186 if (!usock)
187 syslog(pri, "%s", buf);
188 else
189 mysyslog(LogSock, logflags, pri, tag, buf);
190 p = buf;
191 }
192 if (len > sizeof(buf) - 1) {
193 if (!usock)
194 syslog(pri, "%s", *argv++);
195 else
196 mysyslog(LogSock, logflags, pri, tag, *argv++);
197 } else {
198 if (p != buf)
199 *p++ = ' ';
200 bcopy(*argv++, p, len);
201 *(p += len) = '\0';
202 }
203 }
204 if (p != buf) {
205 if (!usock)
206 syslog(pri, "%s", buf);
207 else
208 mysyslog(LogSock, logflags, pri, tag, buf);
209 }
210 } else
211 while (fgets(buf, sizeof(buf), stdin) != NULL) {
212 /* glibc is buggy and adds an additional newline,
213 so we have to remove it here until glibc is fixed */
214 int len = strlen(buf);
215
216 if (len > 0 && buf[len - 1] == '\n')
217 buf[len - 1] = '\0';
218
219 if (!usock)
220 syslog(pri, "%s", buf);
221 else
222 mysyslog(LogSock, logflags, pri, tag, buf);
223 }
224 if (!usock)
225 closelog();
226 else
227 close(LogSock);
228 exit(0);
229 }
230
231 /*
232 * Decode a symbolic name to a numeric value
233 */
234 int
235 pencode(s)
236 register char *s;
237 {
238 char *save;
239 int fac, lev;
240
241 for (save = s; *s && *s != '.'; ++s);
242 if (*s) {
243 *s = '\0';
244 fac = decode(save, facilitynames);
245 if (fac < 0) {
246 (void)fprintf(stderr,
247 _("logger: unknown facility name: %s.\n"), save);
248 exit(1);
249 }
250 *s++ = '.';
251 }
252 else {
253 fac = LOG_USER;
254 s = save;
255 }
256 lev = decode(s, prioritynames);
257 if (lev < 0) {
258 (void)fprintf(stderr,
259 _("logger: unknown priority name: %s.\n"), save);
260 exit(1);
261 }
262 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
263 }
264
265 int
266 decode(name, codetab)
267 char *name;
268 CODE *codetab;
269 {
270 register CODE *c;
271
272 if (isdigit(*name))
273 return (atoi(name));
274
275 for (c = codetab; c->c_name; c++)
276 if (!strcasecmp(name, c->c_name))
277 return (c->c_val);
278
279 return (-1);
280 }
281
282 void
283 usage()
284 {
285 (void)fprintf(stderr,
286 _("usage: logger [-is] [-f file] [-p pri] [-t tag] [-u socket] [ message ... ]\n"));
287 exit(1);
288 }