logger.c revision ba91f08b676cdb873326906656bad68790a01301
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2013 Gary Mills
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* University Copyright- Copyright (c) 1982, 1986, 1988
* The Regents of the University of California
* All Rights Reserved
*
* University Acknowledgment- Portions of this document are derived from
* software developed by the University of California, Berkeley, and its
* contributors.
*/
#include <unistd.h>
#include <stdio.h>
#include <syslog.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <limits.h>
#include <pwd.h>
#include <errno.h>
#define LOGGER_BUFLEN 1024
struct code {
char *c_name;
int c_val;
};
"panic", LOG_EMERG,
"emerg", LOG_EMERG,
"alert", LOG_ALERT,
"crit", LOG_CRIT,
"err", LOG_ERR,
"error", LOG_ERR,
"warn", LOG_WARNING,
"warning", LOG_WARNING,
"notice", LOG_NOTICE,
"info", LOG_INFO,
"debug", LOG_DEBUG,
NULL, -1
};
"kern", LOG_KERN,
"user", LOG_USER,
"mail", LOG_MAIL,
"daemon", LOG_DAEMON,
"auth", LOG_AUTH,
"security", LOG_AUTH,
"mark", LOG_MARK,
"syslog", LOG_SYSLOG,
"lpr", LOG_LPR,
"news", LOG_NEWS,
"uucp", LOG_UUCP,
"altcron", LOG_ALTCRON,
"authpriv", LOG_AUTHPRIV,
"ftp", LOG_FTP,
"ntp", LOG_NTP,
"audit", LOG_AUDIT,
"console", LOG_CONSOLE,
"cron", LOG_CRON,
"local0", LOG_LOCAL0,
"local1", LOG_LOCAL1,
"local2", LOG_LOCAL2,
"local3", LOG_LOCAL3,
"local4", LOG_LOCAL4,
"local5", LOG_LOCAL5,
"local6", LOG_LOCAL6,
"local7", LOG_LOCAL7,
NULL, -1
};
static int pencode(register char *);
static void bailout(char *, char *);
static void usage(void);
/*
* LOGGER -- read and log utility
*
* This routine reads from an input and arranges to write the
* result on the system log, along with a useful tag.
*/
int
{
char tmp[23];
int pri = LOG_NOTICE;
int logflags = 0;
int opt;
int pid_len = 0;
uid_t u;
char fmt_uid[16];
char *p, *endp;
int status = 0;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
/* initialize */
switch (opt) {
case 't': /* tag */
break;
case 'p': /* priority */
break;
case 'i': /* log process id also */
break;
case 'f': /* file to log */
break;
exit(1);
}
break;
default:
usage();
}
u = getuid();
} else
}
/* setup for logging */
/* log input line if appropriate */
if (argc > 0) {
/*
* Log arguments from command line
*/
int i;
len = 0;
for (i = 0; i < argc; i++) {
}
perror("logger");
exit(1);
}
buf[0] = '\0';
for (i = 0; i < argc; i++) {
if (i != 0) {
}
}
#ifdef DEBUG
#endif
} else {
/*
* Log arguments from stdin (or input file).
* When reading from stdin, logger grows its buffer if
* needed, to handle long lines.
*/
perror("logger");
exit(1);
}
p = buf;
offset = 0;
#ifdef DEBUG
"p-buf =%d, len=%d, buflen=%d, buf >%s<\n",
#endif
p = buf;
offset = 0;
/* short read or line with no <newline> */
p += len;
#ifdef DEBUG
"p-buf=%d, len=%d, buflen=%d, buf >%s<\n",
#endif
continue;
} else {
/* line longer than buflen, so get larger buf */
buflen += LOGGER_BUFLEN;
#ifdef DEBUG
"Realloc endp-p=%d, len=%d, offset=%d, "
"buflen %d\n",
#endif
perror("logger");
exit(1);
}
}
} /* while */
if (p > buf) {
/* the last line did not end with newline */
#ifdef DEBUG
"(2) p-buf=%d, len=%d, buflen=%d, "
"buf >%s<\n",
#endif
}
} else {
/*
* fgets() encountered an error. Log unlogged data
* from earlier fgets() (if any). Write null byte
* after last full read, in case the fgets() that
* encountered error removed it and failed to null
* terminate.
*/
perror("logger");
if (p > buf) {
*p = '\0';
}
status = 1;
}
} /* else !(argc > 0) */
return (status);
}
/*
* Decode a symbolic name to a numeric value
*/
static int
pencode(s)
register char *s;
{
register char *p;
int lev;
int fac = 0;
for (p = s; *s && *s != '.'; s++);
if (*s) {
*s = '\0';
if (fac < 0)
bailout("unknown facility name: ", p);
*s++ = '.';
} else
s = p;
if (lev < 0)
bailout("unknown priority name: ", s);
}
static int
char *name;
{
register struct code *c;
return (c->c_val);
return (-1);
}
static void
bailout(a, b)
char *a, *b;
{
exit(1);
}
static void
usage(void)
{
"Usage:\tlogger string\n"
"\tlogger [-i] [-f filename] [-p priority] [-t tag] "
"[message] ...\n"));
exit(1);
}