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