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 #pragma ident "%Z%%M% %I% %E% SMI" 32*7c478bd9Sstevel@tonic-gate 33*7c478bd9Sstevel@tonic-gate #include <stdio.h> 34*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 35*7c478bd9Sstevel@tonic-gate #include <time.h> 36*7c478bd9Sstevel@tonic-gate #include <string.h> 37*7c478bd9Sstevel@tonic-gate #include <stropts.h> 38*7c478bd9Sstevel@tonic-gate #include <unistd.h> 39*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 40*7c478bd9Sstevel@tonic-gate #include <sys/strlog.h> 41*7c478bd9Sstevel@tonic-gate #include "sys/types.h" 42*7c478bd9Sstevel@tonic-gate #include "sys/stat.h" 43*7c478bd9Sstevel@tonic-gate #include "sys/stropts.h" 44*7c478bd9Sstevel@tonic-gate #include "sys/strlog.h" 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate #define CTLSIZE sizeof (struct log_ctl) 47*7c478bd9Sstevel@tonic-gate #define DATSIZE 8192 48*7c478bd9Sstevel@tonic-gate #define ADMSTR "root" 49*7c478bd9Sstevel@tonic-gate #define LOGDEV "/dev/log" 50*7c478bd9Sstevel@tonic-gate #define ERRFILE "/var/adm/streams/error.xxxxx" 51*7c478bd9Sstevel@tonic-gate #define NSECDAY 86400 52*7c478bd9Sstevel@tonic-gate #define LOGDEFAULT "/var/adm/streams" 53*7c478bd9Sstevel@tonic-gate #define DIRECTORY 040000 54*7c478bd9Sstevel@tonic-gate #define ACCESS 07 55*7c478bd9Sstevel@tonic-gate 56*7c478bd9Sstevel@tonic-gate static void prlog(FILE *log, struct log_ctl *lp, char *dp, int flag); 57*7c478bd9Sstevel@tonic-gate static void makefile(char *name, time_t time); 58*7c478bd9Sstevel@tonic-gate static FILE *logfile(FILE *log, struct log_ctl *lp); 59*7c478bd9Sstevel@tonic-gate static void prlog(FILE *log, struct log_ctl *lp, char *dp, int flag); 60*7c478bd9Sstevel@tonic-gate 61*7c478bd9Sstevel@tonic-gate static char *errfile; 62*7c478bd9Sstevel@tonic-gate 63*7c478bd9Sstevel@tonic-gate static void 64*7c478bd9Sstevel@tonic-gate makefile(char *name, time_t time) 65*7c478bd9Sstevel@tonic-gate { 66*7c478bd9Sstevel@tonic-gate char *r; 67*7c478bd9Sstevel@tonic-gate struct tm *tp; 68*7c478bd9Sstevel@tonic-gate 69*7c478bd9Sstevel@tonic-gate tp = localtime(&time); 70*7c478bd9Sstevel@tonic-gate r = &(name[strlen(name) - 5]); 71*7c478bd9Sstevel@tonic-gate (void) sprintf(r, "%02d-%02d", (tp->tm_mon+1), tp->tm_mday); 72*7c478bd9Sstevel@tonic-gate } 73*7c478bd9Sstevel@tonic-gate 74*7c478bd9Sstevel@tonic-gate static FILE * 75*7c478bd9Sstevel@tonic-gate logfile(FILE *log, struct log_ctl *lp) 76*7c478bd9Sstevel@tonic-gate { 77*7c478bd9Sstevel@tonic-gate static time_t lasttime = 0; 78*7c478bd9Sstevel@tonic-gate time_t newtime; 79*7c478bd9Sstevel@tonic-gate 80*7c478bd9Sstevel@tonic-gate errfile = ERRFILE; 81*7c478bd9Sstevel@tonic-gate newtime = lp->ttime - timezone; 82*7c478bd9Sstevel@tonic-gate 83*7c478bd9Sstevel@tonic-gate /* 84*7c478bd9Sstevel@tonic-gate * If it is a new day make a new log file 85*7c478bd9Sstevel@tonic-gate */ 86*7c478bd9Sstevel@tonic-gate if (((newtime/NSECDAY) != (lasttime/NSECDAY)) || !log) { 87*7c478bd9Sstevel@tonic-gate if (log) 88*7c478bd9Sstevel@tonic-gate (void) fclose(log); 89*7c478bd9Sstevel@tonic-gate lasttime = newtime; 90*7c478bd9Sstevel@tonic-gate makefile(errfile, lp->ttime); 91*7c478bd9Sstevel@tonic-gate return (fopen(errfile, "a+")); 92*7c478bd9Sstevel@tonic-gate } 93*7c478bd9Sstevel@tonic-gate lasttime = newtime; 94*7c478bd9Sstevel@tonic-gate return (log); 95*7c478bd9Sstevel@tonic-gate } 96*7c478bd9Sstevel@tonic-gate 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 99*7c478bd9Sstevel@tonic-gate int 100*7c478bd9Sstevel@tonic-gate main(int ac, char *av[]) 101*7c478bd9Sstevel@tonic-gate { 102*7c478bd9Sstevel@tonic-gate int fd; 103*7c478bd9Sstevel@tonic-gate char cbuf[CTLSIZE]; 104*7c478bd9Sstevel@tonic-gate char dbuf[DATSIZE]; /* must start on word boundary */ 105*7c478bd9Sstevel@tonic-gate char mailcmd[40]; 106*7c478bd9Sstevel@tonic-gate int flag; 107*7c478bd9Sstevel@tonic-gate struct strbuf ctl; 108*7c478bd9Sstevel@tonic-gate struct strbuf dat; 109*7c478bd9Sstevel@tonic-gate struct strioctl istr; 110*7c478bd9Sstevel@tonic-gate struct stat stbuf; 111*7c478bd9Sstevel@tonic-gate struct log_ctl *lp; 112*7c478bd9Sstevel@tonic-gate FILE *pfile; 113*7c478bd9Sstevel@tonic-gate FILE *log; 114*7c478bd9Sstevel@tonic-gate char *logname; 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate ctl.buf = cbuf; 117*7c478bd9Sstevel@tonic-gate ctl.maxlen = CTLSIZE; 118*7c478bd9Sstevel@tonic-gate dat.buf = dbuf; 119*7c478bd9Sstevel@tonic-gate dat.maxlen = dat.len = DATSIZE; 120*7c478bd9Sstevel@tonic-gate fd = open(LOGDEV, O_RDWR); 121*7c478bd9Sstevel@tonic-gate if (fd < 0) { 122*7c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: unable to open %s\n", LOGDEV); 123*7c478bd9Sstevel@tonic-gate return (1); 124*7c478bd9Sstevel@tonic-gate } 125*7c478bd9Sstevel@tonic-gate 126*7c478bd9Sstevel@tonic-gate logname = LOGDEFAULT; 127*7c478bd9Sstevel@tonic-gate if (stat(logname, &stbuf) < 0 || !(stbuf.st_mode & DIRECTORY)) { 128*7c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: %s not a directory\n", logname); 129*7c478bd9Sstevel@tonic-gate return (1); 130*7c478bd9Sstevel@tonic-gate } 131*7c478bd9Sstevel@tonic-gate 132*7c478bd9Sstevel@tonic-gate if (access(logname, ACCESS) < 0) { 133*7c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: cannot access directory %s\n", 134*7c478bd9Sstevel@tonic-gate logname); 135*7c478bd9Sstevel@tonic-gate return (1); 136*7c478bd9Sstevel@tonic-gate } 137*7c478bd9Sstevel@tonic-gate 138*7c478bd9Sstevel@tonic-gate istr.ic_cmd = I_ERRLOG; 139*7c478bd9Sstevel@tonic-gate istr.ic_timout = istr.ic_len = 0; 140*7c478bd9Sstevel@tonic-gate istr.ic_dp = NULL; 141*7c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &istr) < 0) { 142*7c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: error logger already exists\n"); 143*7c478bd9Sstevel@tonic-gate return (1); 144*7c478bd9Sstevel@tonic-gate } 145*7c478bd9Sstevel@tonic-gate 146*7c478bd9Sstevel@tonic-gate log = NULL; 147*7c478bd9Sstevel@tonic-gate flag = 0; 148*7c478bd9Sstevel@tonic-gate while (getmsg(fd, &ctl, &dat, &flag) >= 0) { 149*7c478bd9Sstevel@tonic-gate flag = 0; 150*7c478bd9Sstevel@tonic-gate lp = (struct log_ctl *)cbuf; 151*7c478bd9Sstevel@tonic-gate log = logfile(log, lp); 152*7c478bd9Sstevel@tonic-gate if (log == NULL) { 153*7c478bd9Sstevel@tonic-gate fprintf(stderr, "ERROR: unable to open %s\n", errfile); 154*7c478bd9Sstevel@tonic-gate return (1); 155*7c478bd9Sstevel@tonic-gate } else { 156*7c478bd9Sstevel@tonic-gate prlog(log, lp, dbuf, 1); 157*7c478bd9Sstevel@tonic-gate (void) fflush(log); 158*7c478bd9Sstevel@tonic-gate } 159*7c478bd9Sstevel@tonic-gate 160*7c478bd9Sstevel@tonic-gate if (!(lp->flags & SL_NOTIFY)) continue; 161*7c478bd9Sstevel@tonic-gate (void) sprintf(mailcmd, "mail %s", ADMSTR); 162*7c478bd9Sstevel@tonic-gate if ((pfile = popen(mailcmd, "w")) != NULL) { 163*7c478bd9Sstevel@tonic-gate fprintf(pfile, "Streams Error Logger message " 164*7c478bd9Sstevel@tonic-gate "notification:\n\n"); 165*7c478bd9Sstevel@tonic-gate prlog(pfile, lp, dbuf, 0); 166*7c478bd9Sstevel@tonic-gate (void) pclose(pfile); 167*7c478bd9Sstevel@tonic-gate } 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate return (0); 171*7c478bd9Sstevel@tonic-gate } 172*7c478bd9Sstevel@tonic-gate 173*7c478bd9Sstevel@tonic-gate static void 174*7c478bd9Sstevel@tonic-gate prlog(FILE *log, struct log_ctl *lp, char *dp, int flag) 175*7c478bd9Sstevel@tonic-gate { 176*7c478bd9Sstevel@tonic-gate char *ts; 177*7c478bd9Sstevel@tonic-gate time_t t = (time_t)lp->ttime; 178*7c478bd9Sstevel@tonic-gate 179*7c478bd9Sstevel@tonic-gate ts = ctime(&t); 180*7c478bd9Sstevel@tonic-gate ts[19] = '\0'; 181*7c478bd9Sstevel@tonic-gate if (flag) { 182*7c478bd9Sstevel@tonic-gate fprintf(log, "%06d %s %08x %s%s%s ", lp->seq_no, (ts+11), 183*7c478bd9Sstevel@tonic-gate lp->ltime, 184*7c478bd9Sstevel@tonic-gate ((lp->flags & SL_FATAL) ? "F" : "."), 185*7c478bd9Sstevel@tonic-gate ((lp->flags & SL_NOTIFY) ? "N" : "."), 186*7c478bd9Sstevel@tonic-gate ((lp->flags & SL_TRACE) ? "T" : ".")); 187*7c478bd9Sstevel@tonic-gate fprintf(log, "%d %d %s\n", lp->mid, lp->sid, dp); 188*7c478bd9Sstevel@tonic-gate } else { 189*7c478bd9Sstevel@tonic-gate fprintf(log, "%06d %s\n", lp->seq_no, dp); 190*7c478bd9Sstevel@tonic-gate } 191*7c478bd9Sstevel@tonic-gate } 192