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