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