1 | /* Copyright (c) 1983, 1988, 1993
|
---|
2 | * The Regents of the University of California. All rights reserved.
|
---|
3 | *
|
---|
4 | * Redistribution and use in source and binary forms, with or without
|
---|
5 | * modification, are permitted provided that the following conditions
|
---|
6 | * are met:
|
---|
7 | * 1. Redistributions of source code must retain the above copyright
|
---|
8 | * notice, this list of conditions and the following disclaimer.
|
---|
9 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer in the
|
---|
11 | * documentation and/or other materials provided with the distribution.
|
---|
12 | * 3. All advertising materials mentioning features or use of this software
|
---|
13 | * must display the following acknowledgement:
|
---|
14 | * This product includes software developed by the University of
|
---|
15 | * California, Berkeley and its contributors.
|
---|
16 | * 4. Neither the name of the University nor the names of its contributors
|
---|
17 | * may be used to endorse or promote products derived from this software
|
---|
18 | * without specific prior written permission.
|
---|
19 | *
|
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
21 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
22 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
24 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
25 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
26 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
28 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
29 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
30 | * SUCH DAMAGE.
|
---|
31 | *
|
---|
32 | * #ifndef lint
|
---|
33 | * char copyright[] =
|
---|
34 | * "@(#) Copyright (c) 1983 Regents of the University of California.\n\
|
---|
35 | * All rights reserved.\n";
|
---|
36 | * #endif
|
---|
37 | *
|
---|
38 | * #ifndef lint
|
---|
39 | * static char sccsid[] = "@(#)logger.c 6.8 (Berkeley) 6/29/88";
|
---|
40 | * #endif
|
---|
41 | *
|
---|
42 | * Porting to Minix by G. Falzoni <gfalzoni@inwind.it>
|
---|
43 | * $Id: logger.c,v 1.1 2006/04/03 13:07:42 beng Exp $
|
---|
44 | */
|
---|
45 |
|
---|
46 | #include <sys/types.h>
|
---|
47 | #include <unistd.h>
|
---|
48 | #include <errno.h>
|
---|
49 | #include <stdio.h>
|
---|
50 | #include <ctype.h>
|
---|
51 | #include <stdlib.h>
|
---|
52 | #include <string.h>
|
---|
53 |
|
---|
54 | /*
|
---|
55 | ** LOGGER -- read and log utility
|
---|
56 | **
|
---|
57 | ** This program reads from an input and arranges to write the
|
---|
58 | ** result on the system log, along with a useful tag.
|
---|
59 | */
|
---|
60 |
|
---|
61 | #define SYSLOG_NAMES
|
---|
62 | #include <syslog.h>
|
---|
63 |
|
---|
64 | /*
|
---|
65 | ** Name: void bailout(char *msg, char *arg);
|
---|
66 | ** Function: Handles error exit.
|
---|
67 | */
|
---|
68 | void bailout(const char *msg, const char *arg)
|
---|
69 | {
|
---|
70 |
|
---|
71 | fprintf(stderr, "logger: %s %s\n", msg, arg);
|
---|
72 | exit(EXIT_FAILURE);
|
---|
73 | }
|
---|
74 |
|
---|
75 | /*
|
---|
76 | ** Name: int decode(char *name, struct code * codetab);
|
---|
77 | ** Function: Decodes a name to the equivalent priority/facility.
|
---|
78 | */
|
---|
79 | int decode(char *name, const struct _code * codetab)
|
---|
80 | {
|
---|
81 | const struct _code *c;
|
---|
82 |
|
---|
83 | if (isdigit(*name)) return(atoi(name));
|
---|
84 |
|
---|
85 | for (c = codetab; c->c_name; c++)
|
---|
86 | if (!strcasecmp(name, c->c_name)) return(c->c_val);
|
---|
87 |
|
---|
88 | return(-1);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /*
|
---|
92 | ** Name: int pencode(char *s);
|
---|
93 | ** Function: Decode a symbolic name (facility/priority)
|
---|
94 | ** to a numeric value.
|
---|
95 | */
|
---|
96 | int pencode(char *s)
|
---|
97 | {
|
---|
98 | char *save;
|
---|
99 | int fac, lev;
|
---|
100 |
|
---|
101 | for (save = s; *s && *s != '.'; ++s);
|
---|
102 | if (*s) {
|
---|
103 | *s = '\0';
|
---|
104 | fac = decode(save, FacNames);
|
---|
105 | if (fac < 0) bailout("unknown facility name:", save);
|
---|
106 | *s++ = '.';
|
---|
107 | } else {
|
---|
108 | fac = 0;
|
---|
109 | s = save;
|
---|
110 | }
|
---|
111 | lev = decode(s, PriNames);
|
---|
112 | if (lev < 0) bailout("unknown priority name:", save);
|
---|
113 | return((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
|
---|
114 | }
|
---|
115 |
|
---|
116 | /*
|
---|
117 | ** Name: int main(int argc, char **argv);
|
---|
118 | ** Function: Main entry for logger.
|
---|
119 | */
|
---|
120 | int main(int argc, char **argv)
|
---|
121 | {
|
---|
122 | int pri = LOG_NOTICE;
|
---|
123 | int ch, logflags = 0;
|
---|
124 | char *tag, buf[200];
|
---|
125 | static const char usage[] =
|
---|
126 | "[-i] [-f file] [-p pri] [-t tag] [ message ... ]";
|
---|
127 |
|
---|
128 | tag = NULL;
|
---|
129 | while ((ch = getopt(argc, argv, "f:ip:t:")) != EOF) {
|
---|
130 | switch ((char) ch) {
|
---|
131 | case 'f': /* file to log */
|
---|
132 | if (freopen(optarg, "r", stdin) == NULL) {
|
---|
133 | bailout(strerror(errno), optarg);
|
---|
134 | }
|
---|
135 | break;
|
---|
136 | case 'i': /* log process id also */
|
---|
137 | logflags |= LOG_PID;
|
---|
138 | break;
|
---|
139 | case 'p': /* priority */
|
---|
140 | pri = pencode(optarg);
|
---|
141 | break;
|
---|
142 | case 't': /* tag */
|
---|
143 | tag = optarg;
|
---|
144 | break;
|
---|
145 | case '?':
|
---|
146 | default: bailout(usage, ""); break;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | argc -= optind;
|
---|
150 | argv += optind;
|
---|
151 |
|
---|
152 | /* Setup for logging */
|
---|
153 | openlog(tag ? tag : getlogin(), logflags, 0);
|
---|
154 | fclose(stdout);
|
---|
155 |
|
---|
156 | if (argc > 0) { /* Log input line if appropriate */
|
---|
157 | char *p, *endp;
|
---|
158 | int len;
|
---|
159 |
|
---|
160 | for (p = buf, endp = buf + sizeof(buf) - 1;;) {
|
---|
161 | len = strlen(*argv);
|
---|
162 | if (p + len < endp && p > buf) {
|
---|
163 | *--p = '\0';
|
---|
164 | syslog(pri, buf);
|
---|
165 | p = buf;
|
---|
166 | }
|
---|
167 | if (len > sizeof(buf) - 1) {
|
---|
168 | syslog(pri, *argv++);
|
---|
169 | if (!--argc) break;
|
---|
170 | } else {
|
---|
171 | memcpy(p, *argv++, len);
|
---|
172 | p += len;
|
---|
173 | if (!--argc) break;
|
---|
174 | *p++ = ' ';
|
---|
175 | *--p = '\0';
|
---|
176 | }
|
---|
177 | }
|
---|
178 | if (p != buf) {
|
---|
179 | *p = '\0';
|
---|
180 | syslog(pri, buf);
|
---|
181 | }
|
---|
182 | } else /* Main loop */
|
---|
183 | while (fgets(buf, sizeof(buf), stdin) != NULL) syslog(pri, buf);
|
---|
184 |
|
---|
185 | return EXIT_SUCCESS;
|
---|
186 | }
|
---|
187 |
|
---|
188 | /** logger.c **/
|
---|