1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate *
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate * with the License.
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate *
20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate */
22*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */
24*7c478bd9Sstevel@tonic-gate
25*7c478bd9Sstevel@tonic-gate
26*7c478bd9Sstevel@tonic-gate /*
27*7c478bd9Sstevel@tonic-gate * Copyright (c) 1998 by Sun Microsystems, Inc.
28*7c478bd9Sstevel@tonic-gate * All rights reserved.
29*7c478bd9Sstevel@tonic-gate */
30*7c478bd9Sstevel@tonic-gate
31*7c478bd9Sstevel@tonic-gate #include <stdio.h>
32*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
33*7c478bd9Sstevel@tonic-gate #include <time.h>
34*7c478bd9Sstevel@tonic-gate #include <string.h>
35*7c478bd9Sstevel@tonic-gate #include <stropts.h>
36*7c478bd9Sstevel@tonic-gate #include <unistd.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
39*7c478bd9Sstevel@tonic-gate #include "sys/types.h"
40*7c478bd9Sstevel@tonic-gate #include "sys/stat.h"
41*7c478bd9Sstevel@tonic-gate #include "sys/stropts.h"
42*7c478bd9Sstevel@tonic-gate #include "sys/strlog.h"
43*7c478bd9Sstevel@tonic-gate
44*7c478bd9Sstevel@tonic-gate #define CTLSIZE sizeof (struct log_ctl)
45*7c478bd9Sstevel@tonic-gate #define DATSIZE 8192
46*7c478bd9Sstevel@tonic-gate #define ADMSTR "root"
47*7c478bd9Sstevel@tonic-gate #define LOGDEV "/dev/log"
48*7c478bd9Sstevel@tonic-gate #define ERRFILE "/var/adm/streams/error.xxxxx"
49*7c478bd9Sstevel@tonic-gate #define NSECDAY 86400
50*7c478bd9Sstevel@tonic-gate #define LOGDEFAULT "/var/adm/streams"
51*7c478bd9Sstevel@tonic-gate #define DIRECTORY 040000
52*7c478bd9Sstevel@tonic-gate #define ACCESS 07
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate static void prlog(FILE *log, struct log_ctl *lp, char *dp, int flag);
55*7c478bd9Sstevel@tonic-gate static void makefile(char *name, time_t time);
56*7c478bd9Sstevel@tonic-gate static FILE *logfile(FILE *log, struct log_ctl *lp);
57*7c478bd9Sstevel@tonic-gate static void prlog(FILE *log, struct log_ctl *lp, char *dp, int flag);
58*7c478bd9Sstevel@tonic-gate
59*7c478bd9Sstevel@tonic-gate static char *errfile;
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate static void
makefile(char * name,time_t time)62*7c478bd9Sstevel@tonic-gate makefile(char *name, time_t time)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate char *r;
65*7c478bd9Sstevel@tonic-gate struct tm *tp;
66*7c478bd9Sstevel@tonic-gate
67*7c478bd9Sstevel@tonic-gate tp = localtime(&time);
68*7c478bd9Sstevel@tonic-gate r = &(name[strlen(name) - 5]);
69*7c478bd9Sstevel@tonic-gate (void) sprintf(r, "%02d-%02d", (tp->tm_mon+1), tp->tm_mday);
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate
72*7c478bd9Sstevel@tonic-gate static FILE *
logfile(FILE * log,struct log_ctl * lp)73*7c478bd9Sstevel@tonic-gate logfile(FILE *log, struct log_ctl *lp)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate static time_t lasttime = 0;
76*7c478bd9Sstevel@tonic-gate time_t newtime;
77*7c478bd9Sstevel@tonic-gate
78*7c478bd9Sstevel@tonic-gate errfile = ERRFILE;
79*7c478bd9Sstevel@tonic-gate newtime = lp->ttime - timezone;
80*7c478bd9Sstevel@tonic-gate
81*7c478bd9Sstevel@tonic-gate /*
82*7c478bd9Sstevel@tonic-gate * If it is a new day make a new log file
83*7c478bd9Sstevel@tonic-gate */
84*7c478bd9Sstevel@tonic-gate if (((newtime/NSECDAY) != (lasttime/NSECDAY)) || !log) {
85*7c478bd9Sstevel@tonic-gate if (log)
86*7c478bd9Sstevel@tonic-gate (void) fclose(log);
87*7c478bd9Sstevel@tonic-gate lasttime = newtime;
88*7c478bd9Sstevel@tonic-gate makefile(errfile, lp->ttime);
89*7c478bd9Sstevel@tonic-gate return (fopen(errfile, "a+"));
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate lasttime = newtime;
92*7c478bd9Sstevel@tonic-gate return (log);
93*7c478bd9Sstevel@tonic-gate }
94*7c478bd9Sstevel@tonic-gate
95*7c478bd9Sstevel@tonic-gate
96*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
97*7c478bd9Sstevel@tonic-gate int
main(int ac,char * av[])98*7c478bd9Sstevel@tonic-gate main(int ac, char *av[])
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate int fd;
101*7c478bd9Sstevel@tonic-gate char cbuf[CTLSIZE];
102*7c478bd9Sstevel@tonic-gate char dbuf[DATSIZE]; /* must start on word boundary */
103*7c478bd9Sstevel@tonic-gate char mailcmd[40];
104*7c478bd9Sstevel@tonic-gate int flag;
105*7c478bd9Sstevel@tonic-gate struct strbuf ctl;
106*7c478bd9Sstevel@tonic-gate struct strbuf dat;
107*7c478bd9Sstevel@tonic-gate struct strioctl istr;
108*7c478bd9Sstevel@tonic-gate struct stat stbuf;
109*7c478bd9Sstevel@tonic-gate struct log_ctl *lp;
110*7c478bd9Sstevel@tonic-gate FILE *pfile;
111*7c478bd9Sstevel@tonic-gate FILE *log;
112*7c478bd9Sstevel@tonic-gate char *logname;
113*7c478bd9Sstevel@tonic-gate
114*7c478bd9Sstevel@tonic-gate ctl.buf = cbuf;
115*7c478bd9Sstevel@tonic-gate ctl.maxlen = CTLSIZE;
116*7c478bd9Sstevel@tonic-gate dat.buf = dbuf;
117*7c478bd9Sstevel@tonic-gate dat.maxlen = dat.len = DATSIZE;
118*7c478bd9Sstevel@tonic-gate fd = open(LOGDEV, O_RDWR);
119*7c478bd9Sstevel@tonic-gate if (fd < 0) {
120*7c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: unable to open %s\n", LOGDEV);
121*7c478bd9Sstevel@tonic-gate return (1);
122*7c478bd9Sstevel@tonic-gate }
123*7c478bd9Sstevel@tonic-gate
124*7c478bd9Sstevel@tonic-gate logname = LOGDEFAULT;
125*7c478bd9Sstevel@tonic-gate if (stat(logname, &stbuf) < 0 || !(stbuf.st_mode & DIRECTORY)) {
126*7c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: %s not a directory\n", logname);
127*7c478bd9Sstevel@tonic-gate return (1);
128*7c478bd9Sstevel@tonic-gate }
129*7c478bd9Sstevel@tonic-gate
130*7c478bd9Sstevel@tonic-gate if (access(logname, ACCESS) < 0) {
131*7c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: cannot access directory %s\n",
132*7c478bd9Sstevel@tonic-gate logname);
133*7c478bd9Sstevel@tonic-gate return (1);
134*7c478bd9Sstevel@tonic-gate }
135*7c478bd9Sstevel@tonic-gate
136*7c478bd9Sstevel@tonic-gate istr.ic_cmd = I_ERRLOG;
137*7c478bd9Sstevel@tonic-gate istr.ic_timout = istr.ic_len = 0;
138*7c478bd9Sstevel@tonic-gate istr.ic_dp = NULL;
139*7c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &istr) < 0) {
140*7c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: error logger already exists\n");
141*7c478bd9Sstevel@tonic-gate return (1);
142*7c478bd9Sstevel@tonic-gate }
143*7c478bd9Sstevel@tonic-gate
144*7c478bd9Sstevel@tonic-gate log = NULL;
145*7c478bd9Sstevel@tonic-gate flag = 0;
146*7c478bd9Sstevel@tonic-gate while (getmsg(fd, &ctl, &dat, &flag) >= 0) {
147*7c478bd9Sstevel@tonic-gate flag = 0;
148*7c478bd9Sstevel@tonic-gate lp = (struct log_ctl *)cbuf;
149*7c478bd9Sstevel@tonic-gate log = logfile(log, lp);
150*7c478bd9Sstevel@tonic-gate if (log == NULL) {
151*7c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: unable to open %s\n", errfile);
152*7c478bd9Sstevel@tonic-gate return (1);
153*7c478bd9Sstevel@tonic-gate } else {
154*7c478bd9Sstevel@tonic-gate prlog(log, lp, dbuf, 1);
155*7c478bd9Sstevel@tonic-gate (void) fflush(log);
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate
158*7c478bd9Sstevel@tonic-gate if (!(lp->flags & SL_NOTIFY)) continue;
159*7c478bd9Sstevel@tonic-gate (void) sprintf(mailcmd, "mail %s", ADMSTR);
160*7c478bd9Sstevel@tonic-gate if ((pfile = popen(mailcmd, "w")) != NULL) {
161*7c478bd9Sstevel@tonic-gate fprintf(pfile, "Streams Error Logger message "
162*7c478bd9Sstevel@tonic-gate "notification:\n\n");
163*7c478bd9Sstevel@tonic-gate prlog(pfile, lp, dbuf, 0);
164*7c478bd9Sstevel@tonic-gate (void) pclose(pfile);
165*7c478bd9Sstevel@tonic-gate }
166*7c478bd9Sstevel@tonic-gate }
167*7c478bd9Sstevel@tonic-gate
168*7c478bd9Sstevel@tonic-gate return (0);
169*7c478bd9Sstevel@tonic-gate }
170*7c478bd9Sstevel@tonic-gate
171*7c478bd9Sstevel@tonic-gate static void
prlog(FILE * log,struct log_ctl * lp,char * dp,int flag)172*7c478bd9Sstevel@tonic-gate prlog(FILE *log, struct log_ctl *lp, char *dp, int flag)
173*7c478bd9Sstevel@tonic-gate {
174*7c478bd9Sstevel@tonic-gate char *ts;
175*7c478bd9Sstevel@tonic-gate time_t t = (time_t)lp->ttime;
176*7c478bd9Sstevel@tonic-gate
177*7c478bd9Sstevel@tonic-gate ts = ctime(&t);
178*7c478bd9Sstevel@tonic-gate ts[19] = '\0';
179*7c478bd9Sstevel@tonic-gate if (flag) {
180*7c478bd9Sstevel@tonic-gate fprintf(log, "%06d %s %08x %s%s%s ", lp->seq_no, (ts+11),
181*7c478bd9Sstevel@tonic-gate lp->ltime,
182*7c478bd9Sstevel@tonic-gate ((lp->flags & SL_FATAL) ? "F" : "."),
183*7c478bd9Sstevel@tonic-gate ((lp->flags & SL_NOTIFY) ? "N" : "."),
184*7c478bd9Sstevel@tonic-gate ((lp->flags & SL_TRACE) ? "T" : "."));
185*7c478bd9Sstevel@tonic-gate fprintf(log, "%d %d %s\n", lp->mid, lp->sid, dp);
186*7c478bd9Sstevel@tonic-gate } else {
187*7c478bd9Sstevel@tonic-gate fprintf(log, "%06d %s\n", lp->seq_no, dp);
188*7c478bd9Sstevel@tonic-gate }
189*7c478bd9Sstevel@tonic-gate }
190