Move everything from /var/adm to /var/log
[unleashed/lotheac.git] / usr / src / cmd / streams / log / strerr.c
blob642dfcee3f947a25990eb91cd97d6a2a9e8a5246
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
22 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
23 /* All Rights Reserved */
27 * Copyright (c) 1998 by Sun Microsystems, Inc.
28 * All rights reserved.
31 #pragma ident "%Z%%M% %I% %E% SMI"
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"
46 #define CTLSIZE sizeof (struct log_ctl)
47 #define DATSIZE 8192
48 #define ADMSTR "root"
49 #define LOGDEV "/dev/log"
50 #define ERRFILE "/var/log/streams/error.xxxxx"
51 #define NSECDAY 86400
52 #define LOGDEFAULT "/var/log/streams"
53 #define DIRECTORY 040000
54 #define ACCESS 07
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);
61 static char *errfile;
63 static void
64 makefile(char *name, time_t time)
66 char *r;
67 struct tm *tp;
69 tp = localtime(&time);
70 r = &(name[strlen(name) - 5]);
71 (void) sprintf(r, "%02d-%02d", (tp->tm_mon+1), tp->tm_mday);
74 static FILE *
75 logfile(FILE *log, struct log_ctl *lp)
77 static time_t lasttime = 0;
78 time_t newtime;
80 errfile = ERRFILE;
81 newtime = lp->ttime - timezone;
84 * If it is a new day make a new log file
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+"));
93 lasttime = newtime;
94 return (log);
98 /*ARGSUSED*/
99 int
100 main(int ac, char *av[])
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;
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);
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);
132 if (access(logname, ACCESS) < 0) {
133 fprintf(stderr, "ERROR: cannot access directory %s\n",
134 logname);
135 return (1);
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);
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);
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);
170 return (0);
173 static void
174 prlog(FILE *log, struct log_ctl *lp, char *dp, int flag)
176 char *ts;
177 time_t t = (time_t)lp->ttime;
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);