Print this page
4211 Some syslog facility names and symbols are missing
1762 Syslogd man page: missing reference.
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/logger/logger.c
+++ new/usr/src/cmd/logger/logger.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 + * Copyright (c) 2013 Gary Mills
22 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
23 24 * Use is subject to license terms.
24 25 */
25 26
26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 28 /* All Rights Reserved */
28 29
29 30 /*
30 31 * University Copyright- Copyright (c) 1982, 1986, 1988
31 32 * The Regents of the University of California
32 33 * All Rights Reserved
33 34 *
34 35 * University Acknowledgment- Portions of this document are derived from
35 36 * software developed by the University of California, Berkeley, and its
36 37 * contributors.
37 38 */
38 39
39 -#pragma ident "%Z%%M% %I% %E% SMI"
40 40
41 41 #include <sys/types.h>
42 42 #include <unistd.h>
43 43 #include <stdio.h>
44 44 #include <syslog.h>
45 45 #include <ctype.h>
46 46 #include <stdlib.h>
47 47 #include <string.h>
48 48 #include <locale.h>
49 49 #include <limits.h>
50 50 #include <pwd.h>
51 51 #include <errno.h>
52 52
53 53 #define LOG_MARK (LOG_NFACILITIES << 3) /* mark "facility" */
54 54 #define LOGGER_BUFLEN 1024
55 55
56 56 struct code {
57 57 char *c_name;
58 58 int c_val;
59 59 };
60 60
61 61 static struct code PriNames[] = {
62 62 "panic", LOG_EMERG,
63 63 "emerg", LOG_EMERG,
64 64 "alert", LOG_ALERT,
65 65 "crit", LOG_CRIT,
66 66 "err", LOG_ERR,
67 67 "error", LOG_ERR,
68 68 "warn", LOG_WARNING,
69 69 "warning", LOG_WARNING,
70 70 "notice", LOG_NOTICE,
71 71 "info", LOG_INFO,
72 72 "debug", LOG_DEBUG,
73 73 NULL, -1
74 74 };
75 75
76 76 static struct code FacNames[] = {
77 77 "kern", LOG_KERN,
↓ open down ↓ |
28 lines elided |
↑ open up ↑ |
78 78 "user", LOG_USER,
79 79 "mail", LOG_MAIL,
80 80 "daemon", LOG_DAEMON,
81 81 "auth", LOG_AUTH,
82 82 "security", LOG_AUTH,
83 83 "mark", LOG_MARK,
84 84 "syslog", LOG_SYSLOG,
85 85 "lpr", LOG_LPR,
86 86 "news", LOG_NEWS,
87 87 "uucp", LOG_UUCP,
88 - "cron", LOG_CRON,
88 + "bsdcron", LOG_BSDCRON,
89 + "authpriv", LOG_AUTHPRIV,
90 + "ftp", LOG_FTP,
91 + "ntp", LOG_NTP,
89 92 "audit", LOG_AUDIT,
93 + "console", LOG_CONSOLE,
94 + "cron", LOG_CRON,
90 95 "local0", LOG_LOCAL0,
91 96 "local1", LOG_LOCAL1,
92 97 "local2", LOG_LOCAL2,
93 98 "local3", LOG_LOCAL3,
94 99 "local4", LOG_LOCAL4,
95 100 "local5", LOG_LOCAL5,
96 101 "local6", LOG_LOCAL6,
97 102 "local7", LOG_LOCAL7,
98 103 NULL, -1
99 104 };
100 105
101 106 static int pencode(register char *);
102 107 static int decode(char *, struct code *);
103 108 static void bailout(char *, char *);
104 109 static void usage(void);
105 110
106 111 /*
107 112 * LOGGER -- read and log utility
108 113 *
109 114 * This routine reads from an input and arranges to write the
110 115 * result on the system log, along with a useful tag.
111 116 */
112 117
113 118 int
114 119 main(int argc, char **argv)
115 120 {
116 121 char tmp[23];
117 122 char *tag = NULL;
118 123 char *infile = NULL;
119 124 char *buf = NULL;
120 125 size_t buflen;
121 126 int pri = LOG_NOTICE;
122 127 int logflags = 0;
123 128 int opt;
124 129 int pid_len = 0;
125 130 struct passwd *pw;
126 131 uid_t u;
127 132 char fmt_uid[16];
128 133 char *p, *endp;
129 134 size_t len;
130 135 ptrdiff_t offset = 0;
131 136 int status = 0;
132 137
↓ open down ↓ |
33 lines elided |
↑ open up ↑ |
133 138 (void) setlocale(LC_ALL, "");
134 139 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
135 140 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
136 141 #endif
137 142 (void) textdomain(TEXT_DOMAIN);
138 143 /* initialize */
139 144
140 145 while ((opt = getopt(argc, argv, "it:p:f:")) != EOF)
141 146 switch (opt) {
142 147
143 - case 't': /* tag */
148 + case 't': /* tag */
144 149 tag = optarg;
145 150 break;
146 151
147 - case 'p': /* priority */
152 + case 'p': /* priority */
148 153 pri = pencode(optarg);
149 154 break;
150 155
151 - case 'i': /* log process id also */
156 + case 'i': /* log process id also */
152 157 logflags |= LOG_PID;
153 158 pid_len = sprintf(tmp, "%ld", (long)getpid());
154 159 pid_len = (pid_len <= 0) ? 0 : pid_len +2;
155 160 break;
156 161
157 - case 'f': /* file to log */
162 + case 'f': /* file to log */
158 163 if (strcmp(optarg, "-") == 0)
159 164 break;
160 165 infile = optarg;
161 166 if (freopen(infile, "r", stdin) == NULL) {
162 167 (void) fprintf(stderr, gettext("logger: "));
163 168 perror(infile);
164 169 exit(1);
165 170 }
166 171 break;
167 172
168 - default:
173 + default:
169 174 usage();
170 175 }
171 176
172 177 argc -= optind;
173 178 argv = &argv[optind];
174 179
175 180 if ((tag == NULL) && ((tag = getlogin()) == NULL)) {
176 181 u = getuid();
177 182 if ((pw = getpwuid(u)) == NULL) {
178 183 (void) sprintf(fmt_uid, "%u", u);
179 184 tag = fmt_uid;
180 185 } else
181 186 tag = pw->pw_name;
182 187 }
183 188
184 189 /* setup for logging */
185 190 openlog(tag, logflags, 0);
186 191 (void) fclose(stdout);
187 192
188 193 /* log input line if appropriate */
189 194 if (argc > 0) {
190 195 /*
191 196 * Log arguments from command line
192 197 */
193 198 int i;
194 199
195 200 len = 0;
196 201 for (i = 0; i < argc; i++) {
197 202 len += strlen(argv[i]) + 1; /* add 1 for <space> */
198 203 }
199 204 if ((buf = malloc(len + 1)) == NULL) {
200 205 perror("logger");
201 206 exit(1);
202 207 }
203 208 buf[0] = '\0';
204 209 for (i = 0; i < argc; i++) {
205 210 if (i != 0) {
206 211 (void) strcat(buf, " ");
207 212 }
208 213 (void) strcat(buf, argv[i]);
209 214 }
210 215 #ifdef DEBUG
211 216 (void) fprintf(stderr, "len=%d, buf >%s<\n", len, buf);
212 217 #endif
213 218 syslog(pri, "%s", buf);
214 219 } else {
215 220 /*
216 221 * Log arguments from stdin (or input file).
217 222 * When reading from stdin, logger grows its buffer if
218 223 * needed, to handle long lines.
219 224 */
220 225 if ((buf = malloc(LOGGER_BUFLEN)) == NULL) {
221 226 perror("logger");
222 227 exit(1);
223 228 }
224 229 buflen = LOGGER_BUFLEN;
225 230 p = buf;
226 231 endp = buf + buflen;
227 232 offset = 0;
228 233 while (fgets(p, endp - p, stdin) != NULL) {
229 234 len = strlen(p);
230 235 if (p[len - 1] == '\n') {
231 236 #ifdef DEBUG
232 237 (void) fprintf(stderr,
233 238 "p-buf =%d, len=%d, buflen=%d, buf >%s<\n",
234 239 p-buf, len, buflen, buf);
235 240 #endif
236 241 syslog(pri, "%s", buf);
237 242 p = buf;
238 243 offset = 0;
239 244 } else if (len < endp - p - 1) {
240 245 /* short read or line with no <newline> */
241 246 p += len;
242 247 offset += len;
243 248 #ifdef DEBUG
244 249 (void) fprintf(stderr,
245 250 "p-buf=%d, len=%d, buflen=%d, buf >%s<\n",
246 251 p-buf, len, buflen, buf);
247 252 #endif
248 253 continue;
249 254 } else {
250 255 /* line longer than buflen, so get larger buf */
251 256 buflen += LOGGER_BUFLEN;
252 257 offset += len;
253 258 #ifdef DEBUG
254 259 (void) fprintf(stderr,
255 260 "Realloc endp-p=%d, len=%d, offset=%d, "
256 261 "buflen %d\n",
257 262 endp - p, len, offset, buflen);
258 263 #endif
259 264 if ((buf = realloc(buf, buflen)) == NULL) {
260 265 perror("logger");
261 266 exit(1);
262 267 }
263 268 p = buf + offset;
264 269 endp = buf + buflen;
265 270 }
266 271 } /* while */
267 272
268 273 if (feof(stdin)) {
269 274 if (p > buf) {
270 275 /* the last line did not end with newline */
271 276 #ifdef DEBUG
272 277 (void) fprintf(stderr,
273 278 "(2) p-buf=%d, len=%d, buflen=%d, "
274 279 "buf >%s<\n",
275 280 p-buf, len, buflen, buf);
276 281 #endif
277 282 syslog(pri, "%s", buf);
278 283 }
279 284 } else {
280 285 /*
281 286 * fgets() encountered an error. Log unlogged data
282 287 * from earlier fgets() (if any). Write null byte
283 288 * after last full read, in case the fgets() that
284 289 * encountered error removed it and failed to null
285 290 * terminate.
286 291 */
287 292 perror("logger");
288 293 if (p > buf) {
289 294 *p = '\0';
290 295 syslog(pri, "%s", buf);
291 296 }
292 297 status = 1;
293 298 }
294 299 } /* else !(argc > 0) */
295 300 free(buf);
296 301 return (status);
297 302 }
298 303
299 304 /*
300 305 * Decode a symbolic name to a numeric value
301 306 */
302 307
303 308
304 309 static int
305 310 pencode(s)
306 311 register char *s;
307 312 {
308 313 register char *p;
309 314 int lev;
310 315 int fac = 0;
311 316
312 317 for (p = s; *s && *s != '.'; s++);
313 318 if (*s) {
314 319 *s = '\0';
315 320 fac = decode(p, FacNames);
316 321 if (fac < 0)
317 322 bailout("unknown facility name: ", p);
318 323 *s++ = '.';
319 324 } else
320 325 s = p;
321 326 lev = decode(s, PriNames);
322 327 if (lev < 0)
323 328 bailout("unknown priority name: ", s);
324 329
325 330 return ((lev & LOG_PRIMASK) | (fac & LOG_FACMASK));
326 331 }
327 332
328 333
329 334 static int
330 335 decode(name, codetab)
331 336 char *name;
332 337 struct code *codetab;
333 338 {
334 339 register struct code *c;
335 340
336 341 if (isdigit(*name))
337 342 return (atoi(name));
338 343
339 344 for (c = codetab; c->c_name; c++)
340 345 if (strcasecmp(name, c->c_name) == 0)
341 346 return (c->c_val);
342 347
343 348 return (-1);
344 349 }
345 350
346 351
347 352 static void
348 353 bailout(a, b)
349 354 char *a, *b;
350 355 {
351 356 (void) fprintf(stderr, gettext("logger: %s%s\n"), a, b);
↓ open down ↓ |
173 lines elided |
↑ open up ↑ |
352 357 exit(1);
353 358 }
354 359
355 360
356 361 static void
357 362 usage(void)
358 363 {
359 364 (void) fprintf(stderr, gettext(
360 365 "Usage:\tlogger string\n"
361 366 "\tlogger [-i] [-f filename] [-p priority] [-t tag] "
362 - "[message] ...\n"));
367 + "[message] ...\n"));
363 368 exit(1);
364 369 }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX