]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - misc-utils/logger.c
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@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
46 #include <sys/types.h>
47 #include <sys/socket.h>
54 int decode
__P((char *, CODE
*));
55 int pencode
__P((char *));
56 void usage
__P((void));
61 myopenlog(const char *sock
) {
63 static struct sockaddr_un s_addr
; /* AF_UNIX address of local logger */
65 if (strlen(sock
) >= sizeof(s_addr
.sun_path
)) {
66 printf ("logger: openlog: pathname too long\n");
70 s_addr
.sun_family
= AF_UNIX
;
71 (void)strcpy(s_addr
.sun_path
, sock
);
73 if ((fd
= socket(AF_UNIX
, optd
? SOCK_DGRAM
: SOCK_STREAM
, 0)) == -1) {
74 printf ("socket: %s.\n", strerror(errno
));
78 if (connect(fd
, (struct sockaddr
*) &s_addr
, sizeof(s_addr
)) == -1) {
79 printf ("connect: %s.\n", strerror(errno
));
86 mysyslog(int fd
, int logflags
, int pri
, char *tag
, char *msg
) {
87 char buf
[1000], pid
[30], *cp
, *tp
;
91 /* avoid snprintf - it does not exist on ancient systems */
92 if (logflags
& LOG_PID
)
93 sprintf (pid
, "[%d]", getpid());
106 /* do snprintf by hand - ugly, but for once... */
107 sprintf(buf
, "<%d>%.15s %.200s%s: %.400s",
108 pri
, tp
, cp
, pid
, msg
);
110 if (write(fd
, buf
, strlen(buf
)+1) < 0)
116 * logger -- read and log utility
118 * Reads from an input and arranges to write the result on the system
122 main(int argc
, char **argv
) {
123 int ch
, logflags
, pri
;
124 char *tag
, buf
[1024];
128 setlocale(LC_ALL
, "");
129 bindtextdomain(PACKAGE
, LOCALEDIR
);
135 while ((ch
= getopt(argc
, argv
, "f:ip:st:u:d")) != EOF
)
137 case 'f': /* file to log */
138 if (freopen(optarg
, "r", stdin
) == NULL
) {
140 (void)fprintf(stderr
, _("logger: %s: %s.\n"),
141 optarg
, strerror(errsv
));
145 case 'i': /* log process id also */
148 case 'p': /* priority */
149 pri
= pencode(optarg
);
151 case 's': /* log to standard error */
152 logflags
|= LOG_PERROR
;
157 case 'u': /* unix socket */
161 optd
= 1; /* use datagrams */
170 /* setup for logging */
172 openlog(tag
? tag
: getlogin(), logflags
, 0);
174 LogSock
= myopenlog(usock
);
176 (void) fclose(stdout
);
178 /* log input line if appropriate */
180 register char *p
, *endp
;
183 for (p
= buf
, endp
= buf
+ sizeof(buf
) - 2; *argv
;) {
185 if (p
+ len
> endp
&& p
> buf
) {
187 syslog(pri
, "%s", buf
);
189 mysyslog(LogSock
, logflags
, pri
, tag
, buf
);
192 if (len
> sizeof(buf
) - 1) {
194 syslog(pri
, "%s", *argv
++);
196 mysyslog(LogSock
, logflags
, pri
, tag
, *argv
++);
200 bcopy(*argv
++, p
, len
);
206 syslog(pri
, "%s", buf
);
208 mysyslog(LogSock
, logflags
, pri
, tag
, buf
);
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
);
216 if (len
> 0 && buf
[len
- 1] == '\n')
220 syslog(pri
, "%s", buf
);
222 mysyslog(LogSock
, logflags
, pri
, tag
, buf
);
232 * Decode a symbolic name to a numeric value
241 for (save
= s
; *s
&& *s
!= '.'; ++s
);
244 fac
= decode(save
, facilitynames
);
246 (void)fprintf(stderr
,
247 _("logger: unknown facility name: %s.\n"), save
);
256 lev
= decode(s
, prioritynames
);
258 (void)fprintf(stderr
,
259 _("logger: unknown priority name: %s.\n"), save
);
262 return ((lev
& LOG_PRIMASK
) | (fac
& LOG_FACMASK
));
266 decode(name
, codetab
)
275 for (c
= codetab
; c
->c_name
; c
++)
276 if (!strcasecmp(name
, c
->c_name
))
285 (void)fprintf(stderr
,
286 _("usage: logger [-is] [-f file] [-p pri] [-t tag] [-u socket] [ message ... ]\n"));