4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright (c) 2013 Gary Mills
23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 * University Copyright- Copyright (c) 1982, 1986, 1988
32 * The Regents of the University of California
35 * University Acknowledgment- Portions of this document are derived from
36 * software developed by the University of California, Berkeley, and its
41 #include <sys/types.h>
53 #define LOG_MARK (LOG_NFACILITIES << 3) /* mark "facility" */
54 #define LOGGER_BUFLEN 1024
61 static struct code PriNames
[] = {
69 "warning", LOG_WARNING
,
76 static struct code FacNames
[] = {
88 "altcron", LOG_ALTCRON
,
89 "authpriv", LOG_AUTHPRIV
,
93 "console", LOG_CONSOLE
,
100 "local5", LOG_LOCAL5
,
101 "local6", LOG_LOCAL6
,
102 "local7", LOG_LOCAL7
,
106 static int pencode(char *);
107 static int decode(char *, struct code
*);
108 static void bailout(char *, char *);
109 static void usage(void);
112 * LOGGER -- read and log utility
114 * This routine reads from an input and arranges to write the
115 * result on the system log, along with a useful tag.
119 main(int argc
, char **argv
)
126 int pri
= LOG_NOTICE
;
135 ptrdiff_t offset
= 0;
138 (void) setlocale(LC_ALL
, "");
139 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
140 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
142 (void) textdomain(TEXT_DOMAIN
);
145 while ((opt
= getopt(argc
, argv
, "it:p:f:")) != EOF
)
152 case 'p': /* priority */
153 pri
= pencode(optarg
);
156 case 'i': /* log process id also */
158 pid_len
= sprintf(tmp
, "%ld", (long)getpid());
159 pid_len
= (pid_len
<= 0) ? 0 : pid_len
+2;
162 case 'f': /* file to log */
163 if (strcmp(optarg
, "-") == 0)
166 if (freopen(infile
, "r", stdin
) == NULL
) {
167 (void) fprintf(stderr
, gettext("logger: "));
178 argv
= &argv
[optind
];
180 if ((tag
== NULL
) && ((tag
= getlogin()) == NULL
)) {
182 if ((pw
= getpwuid(u
)) == NULL
) {
183 (void) sprintf(fmt_uid
, "%u", u
);
189 /* setup for logging */
190 openlog(tag
, logflags
, 0);
191 (void) fclose(stdout
);
193 /* log input line if appropriate */
196 * Log arguments from command line
201 for (i
= 0; i
< argc
; i
++) {
202 len
+= strlen(argv
[i
]) + 1; /* add 1 for <space> */
204 if ((buf
= malloc(len
+ 1)) == NULL
) {
209 for (i
= 0; i
< argc
; i
++) {
211 (void) strcat(buf
, " ");
213 (void) strcat(buf
, argv
[i
]);
216 (void) fprintf(stderr
, "len=%d, buf >%s<\n", len
, buf
);
218 syslog(pri
, "%s", buf
);
221 * Log arguments from stdin (or input file).
222 * When reading from stdin, logger grows its buffer if
223 * needed, to handle long lines.
225 if ((buf
= malloc(LOGGER_BUFLEN
)) == NULL
) {
229 buflen
= LOGGER_BUFLEN
;
233 while (fgets(p
, endp
- p
, stdin
) != NULL
) {
235 if (p
[len
- 1] == '\n') {
237 (void) fprintf(stderr
,
238 "p-buf =%d, len=%d, buflen=%d, buf >%s<\n",
239 p
-buf
, len
, buflen
, buf
);
241 syslog(pri
, "%s", buf
);
244 } else if (len
< endp
- p
- 1) {
245 /* short read or line with no <newline> */
249 (void) fprintf(stderr
,
250 "p-buf=%d, len=%d, buflen=%d, buf >%s<\n",
251 p
-buf
, len
, buflen
, buf
);
255 /* line longer than buflen, so get larger buf */
256 buflen
+= LOGGER_BUFLEN
;
259 (void) fprintf(stderr
,
260 "Realloc endp-p=%d, len=%d, offset=%d, "
262 endp
- p
, len
, offset
, buflen
);
264 if ((buf
= realloc(buf
, buflen
)) == NULL
) {
275 /* the last line did not end with newline */
277 (void) fprintf(stderr
,
278 "(2) p-buf=%d, len=%d, buflen=%d, "
280 p
-buf
, len
, buflen
, buf
);
282 syslog(pri
, "%s", buf
);
286 * fgets() encountered an error. Log unlogged data
287 * from earlier fgets() (if any). Write null byte
288 * after last full read, in case the fgets() that
289 * encountered error removed it and failed to null
295 syslog(pri
, "%s", buf
);
299 } /* else !(argc > 0) */
305 * Decode a symbolic name to a numeric value
316 for (p
= s
; *s
&& *s
!= '.'; s
++)
320 fac
= decode(p
, FacNames
);
322 bailout("unknown facility name: ", p
);
326 lev
= decode(s
, PriNames
);
328 bailout("unknown priority name: ", s
);
330 return ((lev
& LOG_PRIMASK
) | (fac
& LOG_FACMASK
));
335 decode(char *name
, struct code
*codetab
)
342 for (c
= codetab
; c
->c_name
; c
++)
343 if (strcasecmp(name
, c
->c_name
) == 0)
351 bailout(char *a
, char *b
)
353 (void) fprintf(stderr
, gettext("logger: %s%s\n"), a
, b
);
361 (void) fprintf(stderr
, gettext(
362 "Usage:\tlogger string\n"
363 "\tlogger [-i] [-f filename] [-p priority] [-t tag] "