summaryrefslogtreecommitdiff
path: root/usr/src/cmd/streams/log/strerr.c
blob: 0cc31e64760b5cb89508af3b5e8e9de485048951 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/


/*
 * Copyright (c) 1998 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <fcntl.h>
#include <time.h>
#include <string.h>
#include <stropts.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/strlog.h>
#include "sys/types.h"
#include "sys/stat.h"
#include "sys/stropts.h"
#include "sys/strlog.h"

#define	CTLSIZE sizeof (struct log_ctl)
#define	DATSIZE 8192
#define	ADMSTR "root"
#define	LOGDEV "/dev/log"
#define	ERRFILE "/var/adm/streams/error.xxxxx"
#define	NSECDAY 86400
#define	LOGDEFAULT "/var/adm/streams"
#define	DIRECTORY 040000
#define	ACCESS 07

static void prlog(FILE *log, struct log_ctl *lp, char *dp, int flag);
static void makefile(char *name, time_t time);
static FILE *logfile(FILE *log, struct log_ctl *lp);
static void prlog(FILE *log, struct log_ctl *lp, char *dp, int flag);

static char *errfile;

static void
makefile(char *name, time_t time)
{
	char *r;
	struct tm *tp;

	tp = localtime(&time);
	r = &(name[strlen(name) - 5]);
	(void) sprintf(r, "%02d-%02d", (tp->tm_mon+1), tp->tm_mday);
}

static FILE *
logfile(FILE *log, struct log_ctl *lp)
{
	static time_t lasttime = 0;
	time_t newtime;

	errfile = ERRFILE;
	newtime = lp->ttime - timezone;

	/*
	 * If it is a new day make a new log file
	 */
	if (((newtime/NSECDAY) != (lasttime/NSECDAY)) || !log) {
		if (log)
			(void) fclose(log);
		lasttime = newtime;
		makefile(errfile, lp->ttime);
		return (fopen(errfile, "a+"));
	}
	lasttime = newtime;
	return (log);
}


/*ARGSUSED*/
int
main(int ac, char *av[])
{
	int fd;
	char cbuf[CTLSIZE];
	char dbuf[DATSIZE];	/* must start on word boundary */
	char mailcmd[40];
	int flag;
	struct strbuf ctl;
	struct strbuf dat;
	struct strioctl istr;
	struct stat stbuf;
	struct log_ctl *lp;
	FILE *pfile;
	FILE *log;
	char *logname;

	ctl.buf = cbuf;
	ctl.maxlen = CTLSIZE;
	dat.buf = dbuf;
	dat.maxlen = dat.len = DATSIZE;
	fd = open(LOGDEV, O_RDWR);
	if (fd < 0) {
		fprintf(stderr, "ERROR: unable to open %s\n", LOGDEV);
		return (1);
	}

	logname = LOGDEFAULT;
	if (stat(logname, &stbuf) < 0 || !(stbuf.st_mode & DIRECTORY)) {
		fprintf(stderr, "ERROR: %s not a directory\n", logname);
		return (1);
	}

	if (access(logname, ACCESS) < 0) {
		fprintf(stderr, "ERROR: cannot access directory %s\n",
		    logname);
		return (1);
	}

	istr.ic_cmd = I_ERRLOG;
	istr.ic_timout = istr.ic_len = 0;
	istr.ic_dp = NULL;
	if (ioctl(fd, I_STR, &istr) < 0) {
		fprintf(stderr, "ERROR: error logger already exists\n");
		return (1);
	}

	log = NULL;
	flag = 0;
	while (getmsg(fd, &ctl, &dat, &flag) >= 0) {
		flag = 0;
		lp = (struct log_ctl *)cbuf;
		log = logfile(log, lp);
		if (log == NULL) {
			fprintf(stderr, "ERROR: unable to open %s\n", errfile);
			return (1);
		} else {
			prlog(log, lp, dbuf, 1);
			(void) fflush(log);
		}

		if (!(lp->flags & SL_NOTIFY)) continue;
		(void) sprintf(mailcmd, "mail %s", ADMSTR);
		if ((pfile = popen(mailcmd, "w")) != NULL) {
			fprintf(pfile, "Streams Error Logger message "
			    "notification:\n\n");
			prlog(pfile, lp, dbuf, 0);
			(void) pclose(pfile);
		}
	}

	return (0);
}

static void
prlog(FILE *log, struct log_ctl *lp, char *dp, int flag)
{
	char *ts;
	time_t t = (time_t)lp->ttime;

	ts = ctime(&t);
	ts[19] = '\0';
	if (flag) {
		fprintf(log, "%06d %s %08x %s%s%s ", lp->seq_no, (ts+11),
		    lp->ltime,
		    ((lp->flags & SL_FATAL) ? "F" : "."),
		    ((lp->flags & SL_NOTIFY) ? "N" : "."),
		    ((lp->flags & SL_TRACE) ? "T" : "."));
		fprintf(log, "%d %d %s\n", lp->mid, lp->sid, dp);
	} else {
		fprintf(log, "%06d %s\n", lp->seq_no, dp);
	}
}