Move everything from /var/adm to /var/log
[unleashed.git] / usr / src / cmd / streams / log / strclean.c
blob85e7cc2f437f34d5586705224e709219517f1c5b
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 */
26 #ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.2.1.2 */
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"
40 #define MSGSIZE 128
41 #define NSECDAY (60*60*24)
42 #define LOGDELAY 2
43 #define LOGDEFAULT "/var/log/streams"
44 #define AGEDEFAULT 3
45 #define DIRECTORY 040000
46 #define ACCESS 07
48 static char prefix[128];
49 static time_t cutoff;
52 static int clean(const char *name, const struct stat *stp, int info);
54 int
55 main(int ac, char *av[])
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;
66 logname = LOGDEFAULT;
67 age = AGEDEFAULT;
68 errflg = 0;
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;
79 case 'a':
80 if (*optarg == '\0')
81 errflg++;
82 else
83 age = atoi(optarg);
84 break;
86 default:
87 errflg++;
88 break;
92 if (errflg) {
93 fprintf(stderr, "Usage: strclean [-d <logdir>] [-a <age>]\n");
94 return (1);
97 if (age < 1) {
98 fprintf(stderr, "strclean: <age> must be at least 1\n");
99 return (2);
102 if ((stat(logname, &stbuf) < 0) || !(stbuf.st_mode & DIRECTORY)) {
103 fprintf(stderr, "strclean: %s not a directory\n", logname);
104 return (3);
107 if (access(logname, ACCESS) < 0) {
108 fprintf(stderr, "strclean: cannot access directory %s\n",
109 logname);
110 return (4);
113 cutoff = time(NULL) - age * NSECDAY;
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;
126 if ((fd = open("/dev/log", O_RDWR)) >= 0) {
127 putmsg(fd, &ctl, &dat, 0);
128 close(fd);
129 sleep(LOGDELAY);
132 strcpy(prefix, logname);
133 strcat(prefix, "/error.");
135 ftw(logname, clean, 1);
137 return (0);
141 * clean out all files in the log directory prefixed by 'prefix'
142 * and that are older than 'cutoff' (these are globals above).
144 static int
145 clean(const char *name, const struct stat *stp, int info)
147 if (info != FTW_F)
148 return (0);
150 if (strncmp(name, prefix, strlen(prefix)) != 0)
151 return (0);
153 if (stp->st_mtime >= cutoff)
154 return (0);
156 if (unlink(name) < 0)
157 fprintf(stderr, "strclean: unable to unlink file %s\n", name);
159 return (0);