summaryrefslogtreecommitdiff
path: root/usr/src/cmd/streams/log/strclean.c
blob: 17d39b7ae3657f8784792821bda3acfb1e9135cd (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
/*
 * 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  	*/


#ident	"%Z%%M%	%I%	%E% SMI"	/* SVr4.0 1.2.1.2	*/

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

#define	MSGSIZE 128
#define	NSECDAY (60*60*24)
#define	LOGDELAY 2
#define	LOGDEFAULT "/var/adm/streams"
#define	AGEDEFAULT 3
#define	DIRECTORY 040000
#define	ACCESS 07

static char prefix[128];
static time_t cutoff;


static int clean(const char *name, const struct stat *stp, int info);

int
main(int ac, char *av[])
{
	int age;
	int c, errflg;
	int fd;
	struct stat stbuf;
	struct strbuf ctl, dat;
	struct log_ctl lctl;
	char msg[MSGSIZE];
	char *logname;

	logname = LOGDEFAULT;
	age = AGEDEFAULT;
	errflg = 0;

	while ((c = getopt(ac, av, "d:a:")) != EOF) {
		switch (c) {
		case 'd':
			if (*optarg == '\0')
				errflg++;
			else
				logname = optarg;
			break;

		case 'a':
			if (*optarg == '\0')
				errflg++;
			else
				age = atoi(optarg);
			break;

		default:
			errflg++;
			break;
		}
	}

	if (errflg) {
		fprintf(stderr, "Usage: strclean [-d <logdir>] [-a <age>]\n");
		return (1);
	}

	if (age < 1) {
		fprintf(stderr, "strclean: <age> must be at least 1\n");
		return (2);
	}

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

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

	cutoff = time(NULL) - age * NSECDAY;

	ctl.len = sizeof (struct log_ctl);
	ctl.maxlen = sizeof (struct log_ctl);
	ctl.buf = (caddr_t)&lctl;
	lctl.level = 0;
	lctl.flags = SL_ERROR|SL_NOTIFY;
	dat.buf = msg;
	dat.maxlen = MSGSIZE;
	sprintf(dat.buf,
	    "strclean - removing log files more than %d days old", age);
	dat.len = strlen(dat.buf) + 1;

	if ((fd = open("/dev/log", O_RDWR)) >= 0) {
		putmsg(fd, &ctl, &dat, 0);
		close(fd);
		sleep(LOGDELAY);
	}

	strcpy(prefix, logname);
	strcat(prefix, "/error.");

	ftw(logname, clean, 1);

	return (0);
}

/*
 * clean out all files in the log directory prefixed by 'prefix'
 * and that are older than 'cutoff' (these are globals above).
 */
static int
clean(const char *name, const struct stat *stp, int info)
{
	if (info != FTW_F)
		return (0);

	if (strncmp(name, prefix, strlen(prefix)) != 0)
		return (0);

	if (stp->st_mtime >= cutoff)
		return (0);

	if (unlink(name) < 0)
		fprintf(stderr, "strclean: unable to unlink file %s\n", name);

	return (0);
}