File: expirectl.c

package info (click to toggle)
inn2 2.5.4-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 11,720 kB
  • ctags: 8,983
  • sloc: ansic: 92,499; sh: 13,509; perl: 12,921; makefile: 2,985; yacc: 842; python: 342; lex: 255
file content (306 lines) | stat: -rw-r--r-- 7,430 bytes parent folder | download | duplicates (4)
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*
 * EXPIRECTL.C
 *
 * expirectl
 *
 * This program uses expire.ctl.ctl as input; please see the end of this
 * file for an example of such a file.
 */

/*
 * Date: Mon, 21 Nov 1994 12:29:52 -0801
 * From: Matthew Dillon <dillon@apollo.west.oic.com>
 * Message-Id: <199411212030.MAA21835@apollo.west.oic.com>
 * To: rsalz@uunet.uu.net
 * Subject: Re:  INN is great, bug fix for BSDI
 * 
 * [...]
 *     Oh, while I'm at it, I also wrote a cute program that builds the 
 *     expire.ctl file dynamically based on available space.   Feel free
 *     to include this in the dist (or not) as you please.
 * 
 *     Basically, the expirectl programs determines the amount of disk blocks
 *     and inodes free in the spool and creates a new expire.ctl file based
 *     on an expire.ctl.ctl template.  The template specifies expiration times
 *     as a fraction of nominal.  expirectl adjusts the nominal expiration
 *     up or down based on available disk space.
 * 
 *     The idea is to make expiration as hands off as possible.  I tested
 *     it on a smaller spool and it appeared to work fine.  Currently it
 *     only works for single-partition news spools tho.  The above spool
 *     will not really exercise the program for another 14 days or so :-).
 */


#include <sys/types.h>
#include <sys/mount.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define EXPIRE_CTL_DIR	"/home/news"
#define NEWS_SPOOL	"/home/news/spool/news/."

#define EXPIRE_DAYS	EXPIRE_CTL_DIR "/expire.days"
#define EXPIRE_CTL	EXPIRE_CTL_DIR "/expire.ctl"
#define EXPIRE_CTL_CTL	EXPIRE_CTL_DIR "/expire.ctl.ctl"

void
main(int ac, char **av)
{
    struct statfs sfs;
    long minFree = 100 * 1024 * 1024;
    long minIFree = 20 * 1024;
    long expireDays = 2;
    time_t expireIncTime = time(NULL) - 24 * 60 * 60;
    int modified = 0;
    int verbose = 0;

    /*
     * options
     */

    {
	int i;

	for (i = 1; i < ac; ++i) {
	    char *ptr = av[i];

	    if (*ptr == '-') {
		ptr += 2;
		switch(ptr[-1]) {
		case 'v':
		    verbose = 1;
		    break;
		case 'f':
		    modified = 1;
		    break;
		case 'n':
		    modified = -1;
		    break;
		case 'b':
		    minFree = strtol(((*ptr) ? ptr : av[++i]), &ptr, 0);
		    if (*ptr == 'k')
			minFree *= 1024;
		    if (*ptr == 'm')
			minFree *= 1024 * 1024;
		    break;
		case 'i':
		    minIFree = strtol(((*ptr) ? ptr : av[++i]), NULL, 0);
		    if (*ptr == 'k')
			minIFree *= 1024;
		    if (*ptr == 'm')
			minIFree *= 1024 * 1024;
		    break;
		default:
		    fprintf(stderr, "bad option: %s\n", ptr - 2);
		    exit(1);
		}
	    } else {
		fprintf(stderr, "bad option: %s\n", ptr);
		exit(1);
	    }
	}
    }

    if (statfs("/home/news/spool/news/.", &sfs) != 0) {
	fprintf(stderr, "expirectl: couldn't fsstat /home/news/spool/news/.\n");
	exit(1);
    }

    /*
     * Load /home/news/expire.days
     */
    
    {
	FILE *fi;
	char buf[256];

	if ((fi = fopen(EXPIRE_DAYS, "r")) != NULL) {
	    while (fgets(buf, sizeof(buf), fi) != NULL) {
		if (strncmp(buf, "time", 4) == 0) {
		    expireIncTime = strtol(buf + 4, NULL, 0);
		} else if (strncmp(buf, "days", 4) == 0) {
		    expireDays = strtol(buf + 4, NULL, 0);
		}
	    }
	    fclose(fi);
	} else {
	    if (modified >= 0)
		modified = 1;
	    printf("creating %s\n", EXPIRE_DAYS);
	}
    }

    /*
     * print status
     */

    if (verbose) {
	printf("spool: %4.2lfM / %3.2lfKinode free\n",
	    (double)sfs.f_fsize * (double)sfs.f_bavail / (1024.0 * 1024.0),
	    (double)sfs.f_ffree / 1024.0
	);
	printf("decrs: %4.2lfM / %3.2lfKinode\n",
	    (double)(minFree) / (double)(1024*1024),
	    (double)(minIFree) / (double)(1024)
	);
	printf("incrs: %4.2lfM / %3.2lfKinode\n",
	    (double)(minFree * 2) / (double)(1024*1024),
	    (double)(minIFree * 2) / (double)(1024)
	);
    }

    /*
     * Check limits, update as appropriate
     */

    {
	double bytes;
	long inodes;

	bytes = (double)sfs.f_fsize * (double)sfs.f_bavail;
	inodes = sfs.f_ffree;

	if (bytes < (double)minFree || inodes < minIFree) {
	    if (--expireDays <= 0) {
		expireDays = 1;
		expireIncTime = time(NULL) - 24 * 60 * 60;
	    }
	    if (modified >= 0)
		modified = 1;
	    printf("decrement expiration to %d days\n", expireDays);
	} else if (bytes >= (double)minFree * 2.0 && inodes >= minIFree * 2) {
	    long dt = (long)(time(NULL) - expireIncTime);

	    if (dt >= 60 * 60 * 24 || dt < -60) {
		++expireDays;
		expireIncTime = time(NULL);
		if (modified >= 0)
		    modified = 1;
		printf("increment expiration to %d days\n", expireDays);
	    } else {
		printf("will increment expiration later\n");
	    }
	} else if (verbose) {
	    printf("expiration unchanged: %d\n", expireDays);
	}
    }

    /*
     * Write EXPIRE_CTL file from EXPIRE_CTL_CTL template
     */

    if (modified > 0) {
	FILE *fi;
	FILE *fo;

	if ((fi = fopen(EXPIRE_CTL_CTL, "r")) != NULL) {
	    if ((fo = fopen(EXPIRE_CTL ".tmp", "w")) != NULL) {
		char sbuf[2048];
		char dbuf[4096];

		while (fgets(sbuf, sizeof(sbuf), fi) != NULL) {
		    char *base = sbuf;
		    char *sptr;
		    char *dptr = dbuf;

		    while ((sptr = strchr(base, '[')) != NULL) {
			double d;
			int m = 0;

			bcopy(base, dptr, sptr - base);
			dptr += sptr - base;
			base = sptr;

			d = strtod(sptr + 1, &sptr);
			if (*sptr == '/')
			    m = strtol(sptr + 1, &sptr, 0);
			if (*sptr == ']') {
			    long v = (long)((double)expireDays * d + 0.5);
			    if (v < 1)
				v = 1;
			    if (v < m)
				v = m;
			    sprintf(dptr, "%d", v);
			    dptr += strlen(dptr);
			    ++sptr;
			}
			base = sptr;
		    }
		    strcpy(dptr, base);
		    fputs(dbuf, fo);
		}
		fclose(fo);
		if (rename(EXPIRE_CTL ".tmp", EXPIRE_CTL) != 0) {
		    fprintf(stderr, "rename(%s,%s): %s\n",
			EXPIRE_CTL ".tmp",
			EXPIRE_CTL,
			strerror(errno)
		    );
		}
	    }
	    fclose(fi);
	}
    }

    /*
     * Write EXPIRE_DAYS file
     */
    
    if (modified > 0) {
	FILE *fo;

	if ((fo = fopen(EXPIRE_DAYS, "w")) != NULL) {
	    fprintf(fo, "time 0x%08lx\n", expireIncTime);
	    fprintf(fo, "days %d\n", expireDays);
	    fclose(fo);
	} else {
	    fprintf(stderr, "unable to create %s\n", EXPIRE_DAYS);
	}
    }
    exit(0);
}


/*

# Start of sample expire.ctl.ctl file.

# EXPIRE.CTL.CTL (EXPIRE.CTL GENERATED FROM EXPIRE.CTL.CTL !!!)
#
# The expire.ctl file is generated by the expirectl program from the
# expire.ctl.ctl file.  The expirectl program calculates the proper
# expiration based on the number of free inodes and free bytes available.
#
# This file is exactly expire.ctl but with the multiplier [N] replaced by 
# a calculated value, where a multiplier of '1' nominally fills the whole
# disk.
#
# Any field [N] is substituted after being multiplied by the expiration
# time (in days).  A integer minimum can also be specified with a slash,
# as in [N/minimum].
#
# expirectl is normally run just after expire is run.  Note that expirectl
# isn't very useful for the case where you are 'catching up' on news after
# a long period of downtime UNLESS you use the -p option to expire.

/remember/:[1.2/20]

##  Keep for 1-10 days, allow Expires headers to work.
#
*:A:1:[1.0]:[6.0]
*.advocacy:A:1:[0.5]:[2.0]
alt.binaries.pictures.erotica:A:1:[0.8]:[2.0]

# permanent, semi-permanent
#
best.intro:A:never:never:never
best.announce:A:5:60:120
best.general:A:never:never:never
best.bugs:A:never:never:never

# End of sample expire.ctl.ctl file.

*/