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 #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.2.1.2 */
27*7c478bd9Sstevel@tonic-gate
28*7c478bd9Sstevel@tonic-gate #include <stdio.h>
29*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <unistd.h>
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <stropts.h>
34*7c478bd9Sstevel@tonic-gate #include <ftw.h>
35*7c478bd9Sstevel@tonic-gate #include "sys/types.h"
36*7c478bd9Sstevel@tonic-gate #include "sys/stat.h"
37*7c478bd9Sstevel@tonic-gate #include "sys/stropts.h"
38*7c478bd9Sstevel@tonic-gate #include "sys/strlog.h"
39*7c478bd9Sstevel@tonic-gate
40*7c478bd9Sstevel@tonic-gate #define MSGSIZE 128
41*7c478bd9Sstevel@tonic-gate #define NSECDAY (60*60*24)
42*7c478bd9Sstevel@tonic-gate #define LOGDELAY 2
43*7c478bd9Sstevel@tonic-gate #define LOGDEFAULT "/var/adm/streams"
44*7c478bd9Sstevel@tonic-gate #define AGEDEFAULT 3
45*7c478bd9Sstevel@tonic-gate #define DIRECTORY 040000
46*7c478bd9Sstevel@tonic-gate #define ACCESS 07
47*7c478bd9Sstevel@tonic-gate
48*7c478bd9Sstevel@tonic-gate static char prefix[128];
49*7c478bd9Sstevel@tonic-gate static time_t cutoff;
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate
52*7c478bd9Sstevel@tonic-gate static int clean(const char *name, const struct stat *stp, int info);
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate int
main(int ac,char * av[])55*7c478bd9Sstevel@tonic-gate main(int ac, char *av[])
56*7c478bd9Sstevel@tonic-gate {
57*7c478bd9Sstevel@tonic-gate int age;
58*7c478bd9Sstevel@tonic-gate int c, errflg;
59*7c478bd9Sstevel@tonic-gate int fd;
60*7c478bd9Sstevel@tonic-gate struct stat stbuf;
61*7c478bd9Sstevel@tonic-gate struct strbuf ctl, dat;
62*7c478bd9Sstevel@tonic-gate struct log_ctl lctl;
63*7c478bd9Sstevel@tonic-gate char msg[MSGSIZE];
64*7c478bd9Sstevel@tonic-gate char *logname;
65*7c478bd9Sstevel@tonic-gate
66*7c478bd9Sstevel@tonic-gate logname = LOGDEFAULT;
67*7c478bd9Sstevel@tonic-gate age = AGEDEFAULT;
68*7c478bd9Sstevel@tonic-gate errflg = 0;
69*7c478bd9Sstevel@tonic-gate
70*7c478bd9Sstevel@tonic-gate while ((c = getopt(ac, av, "d:a:")) != EOF) {
71*7c478bd9Sstevel@tonic-gate switch (c) {
72*7c478bd9Sstevel@tonic-gate case 'd':
73*7c478bd9Sstevel@tonic-gate if (*optarg == '\0')
74*7c478bd9Sstevel@tonic-gate errflg++;
75*7c478bd9Sstevel@tonic-gate else
76*7c478bd9Sstevel@tonic-gate logname = optarg;
77*7c478bd9Sstevel@tonic-gate break;
78*7c478bd9Sstevel@tonic-gate
79*7c478bd9Sstevel@tonic-gate case 'a':
80*7c478bd9Sstevel@tonic-gate if (*optarg == '\0')
81*7c478bd9Sstevel@tonic-gate errflg++;
82*7c478bd9Sstevel@tonic-gate else
83*7c478bd9Sstevel@tonic-gate age = atoi(optarg);
84*7c478bd9Sstevel@tonic-gate break;
85*7c478bd9Sstevel@tonic-gate
86*7c478bd9Sstevel@tonic-gate default:
87*7c478bd9Sstevel@tonic-gate errflg++;
88*7c478bd9Sstevel@tonic-gate break;
89*7c478bd9Sstevel@tonic-gate }
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate
92*7c478bd9Sstevel@tonic-gate if (errflg) {
93*7c478bd9Sstevel@tonic-gate fprintf(stderr, "Usage: strclean [-d <logdir>] [-a <age>]\n");
94*7c478bd9Sstevel@tonic-gate return (1);
95*7c478bd9Sstevel@tonic-gate }
96*7c478bd9Sstevel@tonic-gate
97*7c478bd9Sstevel@tonic-gate if (age < 1) {
98*7c478bd9Sstevel@tonic-gate fprintf(stderr, "strclean: <age> must be at least 1\n");
99*7c478bd9Sstevel@tonic-gate return (2);
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate
102*7c478bd9Sstevel@tonic-gate if ((stat(logname, &stbuf) < 0) || !(stbuf.st_mode & DIRECTORY)) {
103*7c478bd9Sstevel@tonic-gate fprintf(stderr, "strclean: %s not a directory\n", logname);
104*7c478bd9Sstevel@tonic-gate return (3);
105*7c478bd9Sstevel@tonic-gate }
106*7c478bd9Sstevel@tonic-gate
107*7c478bd9Sstevel@tonic-gate if (access(logname, ACCESS) < 0) {
108*7c478bd9Sstevel@tonic-gate fprintf(stderr, "strclean: cannot access directory %s\n",
109*7c478bd9Sstevel@tonic-gate logname);
110*7c478bd9Sstevel@tonic-gate return (4);
111*7c478bd9Sstevel@tonic-gate }
112*7c478bd9Sstevel@tonic-gate
113*7c478bd9Sstevel@tonic-gate cutoff = time(NULL) - age * NSECDAY;
114*7c478bd9Sstevel@tonic-gate
115*7c478bd9Sstevel@tonic-gate ctl.len = sizeof (struct log_ctl);
116*7c478bd9Sstevel@tonic-gate ctl.maxlen = sizeof (struct log_ctl);
117*7c478bd9Sstevel@tonic-gate ctl.buf = (caddr_t)&lctl;
118*7c478bd9Sstevel@tonic-gate lctl.level = 0;
119*7c478bd9Sstevel@tonic-gate lctl.flags = SL_ERROR|SL_NOTIFY;
120*7c478bd9Sstevel@tonic-gate dat.buf = msg;
121*7c478bd9Sstevel@tonic-gate dat.maxlen = MSGSIZE;
122*7c478bd9Sstevel@tonic-gate sprintf(dat.buf,
123*7c478bd9Sstevel@tonic-gate "strclean - removing log files more than %d days old", age);
124*7c478bd9Sstevel@tonic-gate dat.len = strlen(dat.buf) + 1;
125*7c478bd9Sstevel@tonic-gate
126*7c478bd9Sstevel@tonic-gate if ((fd = open("/dev/log", O_RDWR)) >= 0) {
127*7c478bd9Sstevel@tonic-gate putmsg(fd, &ctl, &dat, 0);
128*7c478bd9Sstevel@tonic-gate close(fd);
129*7c478bd9Sstevel@tonic-gate sleep(LOGDELAY);
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate
132*7c478bd9Sstevel@tonic-gate strcpy(prefix, logname);
133*7c478bd9Sstevel@tonic-gate strcat(prefix, "/error.");
134*7c478bd9Sstevel@tonic-gate
135*7c478bd9Sstevel@tonic-gate ftw(logname, clean, 1);
136*7c478bd9Sstevel@tonic-gate
137*7c478bd9Sstevel@tonic-gate return (0);
138*7c478bd9Sstevel@tonic-gate }
139*7c478bd9Sstevel@tonic-gate
140*7c478bd9Sstevel@tonic-gate /*
141*7c478bd9Sstevel@tonic-gate * clean out all files in the log directory prefixed by 'prefix'
142*7c478bd9Sstevel@tonic-gate * and that are older than 'cutoff' (these are globals above).
143*7c478bd9Sstevel@tonic-gate */
144*7c478bd9Sstevel@tonic-gate static int
clean(const char * name,const struct stat * stp,int info)145*7c478bd9Sstevel@tonic-gate clean(const char *name, const struct stat *stp, int info)
146*7c478bd9Sstevel@tonic-gate {
147*7c478bd9Sstevel@tonic-gate if (info != FTW_F)
148*7c478bd9Sstevel@tonic-gate return (0);
149*7c478bd9Sstevel@tonic-gate
150*7c478bd9Sstevel@tonic-gate if (strncmp(name, prefix, strlen(prefix)) != 0)
151*7c478bd9Sstevel@tonic-gate return (0);
152*7c478bd9Sstevel@tonic-gate
153*7c478bd9Sstevel@tonic-gate if (stp->st_mtime >= cutoff)
154*7c478bd9Sstevel@tonic-gate return (0);
155*7c478bd9Sstevel@tonic-gate
156*7c478bd9Sstevel@tonic-gate if (unlink(name) < 0)
157*7c478bd9Sstevel@tonic-gate fprintf(stderr, "strclean: unable to unlink file %s\n", name);
158*7c478bd9Sstevel@tonic-gate
159*7c478bd9Sstevel@tonic-gate return (0);
160*7c478bd9Sstevel@tonic-gate }
161