10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6812Sraf * Common Development and Distribution License (the "License").
6*6812Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
21*6812Sraf
220Sstevel@tonic-gate /*
23*6812Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
30*6812Sraf #pragma ident "%Z%%M% %I% %E% SMI"
310Sstevel@tonic-gate
320Sstevel@tonic-gate /* lfmt_log() - log info */
33*6812Sraf
34*6812Sraf #include "lint.h"
350Sstevel@tonic-gate #include <mtlib.h>
360Sstevel@tonic-gate #include <pfmt.h>
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/types32.h>
400Sstevel@tonic-gate #include <sys/stropts.h>
410Sstevel@tonic-gate #include <sys/strlog.h>
420Sstevel@tonic-gate #include <fcntl.h>
430Sstevel@tonic-gate #include <errno.h>
440Sstevel@tonic-gate #include <synch.h>
450Sstevel@tonic-gate #include <thread.h>
460Sstevel@tonic-gate #include "pfmt_data.h"
470Sstevel@tonic-gate #include <time.h>
480Sstevel@tonic-gate #include <stropts.h>
490Sstevel@tonic-gate #include <unistd.h>
500Sstevel@tonic-gate #include <strings.h>
510Sstevel@tonic-gate #include <sys/uio.h>
520Sstevel@tonic-gate
530Sstevel@tonic-gate #define MAXMSG 1024
540Sstevel@tonic-gate #define LOGNAME "/dev/conslog"
550Sstevel@tonic-gate #define LOG_CONSOLE "/dev/console"
560Sstevel@tonic-gate
570Sstevel@tonic-gate int
__lfmt_log(const char * text,const char * sev,va_list args,long flag,int ret)580Sstevel@tonic-gate __lfmt_log(const char *text, const char *sev, va_list args, long flag, int ret)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate static int fd = -1;
610Sstevel@tonic-gate struct strbuf dat;
620Sstevel@tonic-gate int msg_offset;
630Sstevel@tonic-gate long len;
640Sstevel@tonic-gate union {
650Sstevel@tonic-gate long flag;
660Sstevel@tonic-gate char buf[MAXMSG];
670Sstevel@tonic-gate } msg;
680Sstevel@tonic-gate int err;
690Sstevel@tonic-gate int fdd;
700Sstevel@tonic-gate
710Sstevel@tonic-gate len = ret + sizeof (long) + 3;
720Sstevel@tonic-gate
730Sstevel@tonic-gate if (len > sizeof (msg)) {
740Sstevel@tonic-gate errno = ERANGE;
750Sstevel@tonic-gate return (-2);
760Sstevel@tonic-gate }
770Sstevel@tonic-gate
780Sstevel@tonic-gate msg.flag = flag;
790Sstevel@tonic-gate msg_offset = (int)sizeof (long);
800Sstevel@tonic-gate
810Sstevel@tonic-gate lrw_rdlock(&_rw_pfmt_label);
820Sstevel@tonic-gate if (*__pfmt_label)
830Sstevel@tonic-gate msg_offset += strlcpy(msg.buf + msg_offset, __pfmt_label,
840Sstevel@tonic-gate sizeof (msg.buf) - msg_offset);
850Sstevel@tonic-gate lrw_unlock(&_rw_pfmt_label);
860Sstevel@tonic-gate
870Sstevel@tonic-gate if (sev)
880Sstevel@tonic-gate msg_offset += sprintf(msg.buf + msg_offset, sev, flag & 0xff);
890Sstevel@tonic-gate
900Sstevel@tonic-gate msg_offset += 1 + vsprintf(msg.buf + msg_offset, text, args);
910Sstevel@tonic-gate msg.buf[msg_offset++] = '\0';
920Sstevel@tonic-gate
930Sstevel@tonic-gate if (fd == -1 &&
94*6812Sraf ((fd = open(LOGNAME, O_WRONLY)) == -1 ||
95*6812Sraf fcntl(fd, F_SETFD, 1) == -1))
960Sstevel@tonic-gate return (-2);
970Sstevel@tonic-gate
980Sstevel@tonic-gate dat.maxlen = MAXMSG;
990Sstevel@tonic-gate dat.len = (int)msg_offset;
1000Sstevel@tonic-gate dat.buf = msg.buf;
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate if (putmsg(fd, 0, &dat, 0) == -1) {
1030Sstevel@tonic-gate (void) close(fd);
1040Sstevel@tonic-gate return (-2);
1050Sstevel@tonic-gate }
1060Sstevel@tonic-gate
1070Sstevel@tonic-gate /*
1080Sstevel@tonic-gate * Display it to a console
1090Sstevel@tonic-gate */
1100Sstevel@tonic-gate if ((flag & MM_CONSOLE) != 0) {
1110Sstevel@tonic-gate char *p;
1120Sstevel@tonic-gate time_t t;
1130Sstevel@tonic-gate char buf[128];
1140Sstevel@tonic-gate err = errno;
1150Sstevel@tonic-gate fdd = open(LOG_CONSOLE, O_WRONLY);
1160Sstevel@tonic-gate if (fdd != -1) {
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate * Use C locale for time stamp.
1190Sstevel@tonic-gate */
1200Sstevel@tonic-gate (void) time(&t);
1210Sstevel@tonic-gate (void) ctime_r(&t, buf, sizeof (buf));
1220Sstevel@tonic-gate p = (char *)strrchr(buf, '\n');
1230Sstevel@tonic-gate if (p != NULL)
1240Sstevel@tonic-gate *p = ':';
1250Sstevel@tonic-gate (void) write(fdd, buf, strlen(buf));
1260Sstevel@tonic-gate (void) write(fdd, msg.buf + sizeof (long),
127*6812Sraf msg_offset - sizeof (long));
1280Sstevel@tonic-gate (void) write(fdd, "\n", 1);
1290Sstevel@tonic-gate } else
1300Sstevel@tonic-gate return (-2);
1310Sstevel@tonic-gate (void) close(fdd);
1320Sstevel@tonic-gate errno = err;
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate return (ret);
1350Sstevel@tonic-gate }
136