freebsd-head /usr.sbin/newsyslog/ptimes.c

Language C Lines 619
MD5 Hash d3faf08ac71476195c2762e5e2738e92 Estimated Cost $9,593 (why?)
Repository https://bitbucket.org/freebsd/freebsd-head/ View Raw File View Project SPDX
  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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
/*-
 * ------+---------+---------+---------+---------+---------+---------+---------*
 * Initial version of parse8601 was originally added to newsyslog.c in
 *     FreeBSD on Jan 22, 1999 by Garrett Wollman <wollman@FreeBSD.org>.
 * Initial version of parseDWM was originally added to newsyslog.c in
 *     FreeBSD on Apr  4, 2000 by Hellmuth Michaelis <hm@FreeBSD.org>.
 *
 * Copyright (c) 2003  - Garance Alistair Drosehn <gad@FreeBSD.org>.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * The views and conclusions contained in the software and documentation
 * are those of the authors and should not be interpreted as representing
 * official policies, either expressed or implied, of the FreeBSD Project.
 *
 * ------+---------+---------+---------+---------+---------+---------+---------*
 * This is intended to be a set of general-purpose routines to process times.
 * Right now it probably still has a number of assumptions in it, such that
 * it works fine for newsyslog but might not work for other uses.
 * ------+---------+---------+---------+---------+---------+---------+---------*
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <ctype.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "extern.h"

#define	SECS_PER_HOUR	3600

/*
 * Bit-values which indicate which components of time were specified
 * by the string given to parse8601 or parseDWM.  These are needed to
 * calculate what time-in-the-future will match that string.
 */
#define	TSPEC_YEAR		0x0001
#define	TSPEC_MONTHOFYEAR	0x0002
#define	TSPEC_LDAYOFMONTH	0x0004
#define	TSPEC_DAYOFMONTH	0x0008
#define	TSPEC_DAYOFWEEK		0x0010
#define	TSPEC_HOUROFDAY		0x0020

#define	TNYET_ADJ4DST		-10	/* DST has "not yet" been adjusted */

struct ptime_data {
	time_t		 basesecs;	/* Base point for relative times */
	time_t		 tsecs;		/* Time in seconds */
	struct tm	 basetm;	/* Base Time expanded into fields */
	struct tm	 tm;		/* Time expanded into fields */
	int		 did_adj4dst;	/* Track calls to ptime_adjust4dst */
	int		 parseopts;	/* Options given for parsing */
	int		 tmspec;	/* Indicates which time fields had
					 * been specified by the user */
};

static int	 days_pmonth(int month, int year);
static int	 parse8601(struct ptime_data *ptime, const char *str);
static int	 parseDWM(struct ptime_data *ptime, const char *str);

/*
 * Simple routine to calculate the number of days in a given month.
 */
static int
days_pmonth(int month, int year)
{
	static const int mtab[] = {31, 28, 31, 30, 31, 30, 31, 31,
	    30, 31, 30, 31};
	int ndays;

	ndays = mtab[month];

	if (month == 1) {
		/*
		 * We are usually called with a 'tm-year' value
		 * (ie, the value = the number of years past 1900).
		 */
		if (year < 1900)
			year += 1900;
		if (year % 4 == 0) {
			/*
			 * This is a leap year, as long as it is not a
			 * multiple of 100, or if it is a multiple of
			 * both 100 and 400.
			 */
			if (year % 100 != 0)
				ndays++;	/* not multiple of 100 */
			else if (year % 400 == 0)
				ndays++;	/* is multiple of 100 and 400 */
		}
	}
	return (ndays);
}

/*-
 * Parse a limited subset of ISO 8601. The specific format is as follows:
 *
 * [CC[YY[MM[DD]]]][THH[MM[SS]]]	(where `T' is the literal letter)
 *
 * We don't accept a timezone specification; missing fields (including timezone)
 * are defaulted to the current date but time zero.
 */
static int
parse8601(struct ptime_data *ptime, const char *s)
{
	char *t;
	long l;
	struct tm tm;

	l = strtol(s, &t, 10);
	if (l < 0 || l >= INT_MAX || (*t != '\0' && *t != 'T'))
		return (-1);

	/*
	 * Now t points either to the end of the string (if no time was
	 * provided) or to the letter `T' which separates date and time in
	 * ISO 8601.  The pointer arithmetic is the same for either case.
	 */
	tm = ptime->tm;
	ptime->tmspec = TSPEC_HOUROFDAY;
	switch (t - s) {
	case 8:
		tm.tm_year = ((l / 1000000) - 19) * 100;
		l = l % 1000000;
	case 6:
		ptime->tmspec |= TSPEC_YEAR;
		tm.tm_year -= tm.tm_year % 100;
		tm.tm_year += l / 10000;
		l = l % 10000;
	case 4:
		ptime->tmspec |= TSPEC_MONTHOFYEAR;
		tm.tm_mon = (l / 100) - 1;
		l = l % 100;
	case 2:
		ptime->tmspec |= TSPEC_DAYOFMONTH;
		tm.tm_mday = l;
	case 0:
		break;
	default:
		return (-1);
	}

	/* sanity check */
	if (tm.tm_year < 70 || tm.tm_mon < 0 || tm.tm_mon > 12
	    || tm.tm_mday < 1 || tm.tm_mday > 31)
		return (-1);

	if (*t != '\0') {
		s = ++t;
		l = strtol(s, &t, 10);
		if (l < 0 || l >= INT_MAX || (*t != '\0' && !isspace(*t)))
			return (-1);

		switch (t - s) {
		case 6:
			tm.tm_sec = l % 100;
			l /= 100;
		case 4:
			tm.tm_min = l % 100;
			l /= 100;
		case 2:
			ptime->tmspec |= TSPEC_HOUROFDAY;
			tm.tm_hour = l;
		case 0:
			break;
		default:
			return (-1);
		}

		/* sanity check */
		if (tm.tm_sec < 0 || tm.tm_sec > 60 || tm.tm_min < 0
		    || tm.tm_min > 59 || tm.tm_hour < 0 || tm.tm_hour > 23)
			return (-1);
	}

	ptime->tm = tm;
	return (0);
}

/*-
 * Parse a cyclic time specification, the format is as follows:
 *
 *	[Dhh] or [Wd[Dhh]] or [Mdd[Dhh]]
 *
 * to rotate a logfile cyclic at
 *
 *	- every day (D) within a specific hour (hh)	(hh = 0...23)
 *	- once a week (W) at a specific day (d)     OR	(d = 0..6, 0 = Sunday)
 *	- once a month (M) at a specific day (d)	(d = 1..31,l|L)
 *
 * We don't accept a timezone specification; missing fields
 * are defaulted to the current date but time zero.
 */
static int
parseDWM(struct ptime_data *ptime, const char *s)
{
	int daysmon, Dseen, WMseen;
	const char *endval;
	char *tmp;
	long l;
	struct tm tm;

	/* Save away the number of days in this month */
	tm = ptime->tm;
	daysmon = days_pmonth(tm.tm_mon, tm.tm_year);

	WMseen = Dseen = 0;
	ptime->tmspec = TSPEC_HOUROFDAY;
	for (;;) {
		endval = NULL;
		switch (*s) {
		case 'D':
			if (Dseen)
				return (-1);
			Dseen++;
			ptime->tmspec |= TSPEC_HOUROFDAY;
			s++;
			l = strtol(s, &tmp, 10);
			if (l < 0 || l > 23)
				return (-1);
			endval = tmp;
			tm.tm_hour = l;
			break;

		case 'W':
			if (WMseen)
				return (-1);
			WMseen++;
			ptime->tmspec |= TSPEC_DAYOFWEEK;
			s++;
			l = strtol(s, &tmp, 10);
			if (l < 0 || l > 6)
				return (-1);
			endval = tmp;
			if (l != tm.tm_wday) {
				int save;

				if (l < tm.tm_wday) {
					save = 6 - tm.tm_wday;
					save += (l + 1);
				} else {
					save = l - tm.tm_wday;
				}

				tm.tm_mday += save;

				if (tm.tm_mday > daysmon) {
					tm.tm_mon++;
					tm.tm_mday = tm.tm_mday - daysmon;
				}
			}
			break;

		case 'M':
			if (WMseen)
				return (-1);
			WMseen++;
			ptime->tmspec |= TSPEC_DAYOFMONTH;
			s++;
			if (tolower(*s) == 'l') {
				/* User wants the last day of the month. */
				ptime->tmspec |= TSPEC_LDAYOFMONTH;
				tm.tm_mday = daysmon;
				endval = s + 1;
			} else {
				l = strtol(s, &tmp, 10);
				if (l < 1 || l > 31)
					return (-1);

				if (l > daysmon)
					return (-1);
				endval = tmp;
				tm.tm_mday = l;
			}
			break;

		default:
			return (-1);
			break;
		}

		if (endval == NULL)
			return (-1);
		else if (*endval == '\0' || isspace(*endval))
			break;
		else
			s = endval;
	}

	ptime->tm = tm;
	return (0);
}

/*
 * Initialize a new ptime-related data area.
 */
struct ptime_data *
ptime_init(const struct ptime_data *optsrc)
{
	struct ptime_data *newdata;

	newdata = malloc(sizeof(struct ptime_data));
	if (optsrc != NULL) {
		memcpy(newdata, optsrc, sizeof(struct ptime_data));
	} else {
		memset(newdata, '\0', sizeof(struct ptime_data));
		newdata->did_adj4dst = TNYET_ADJ4DST;
	}

	return (newdata);
}

/*
 * Adjust a given time if that time is in a different timezone than
 * some other time.
 */
int
ptime_adjust4dst(struct ptime_data *ptime, const struct ptime_data *dstsrc)
{
	struct ptime_data adjtime;

	if (ptime == NULL)
		return (-1);

	/*
	 * Changes are not made to the given time until after all
	 * of the calculations have been successful.
	 */
	adjtime = *ptime;

	/* Check to see if this adjustment was already made */
	if ((adjtime.did_adj4dst != TNYET_ADJ4DST) &&
	    (adjtime.did_adj4dst == dstsrc->tm.tm_isdst))
		return (0);		/* yes, so don't make it twice */

	/* See if daylight-saving has changed between the two times. */
	if (dstsrc->tm.tm_isdst != adjtime.tm.tm_isdst) {
		if (adjtime.tm.tm_isdst == 1)
			adjtime.tsecs -= SECS_PER_HOUR;
		else if (adjtime.tm.tm_isdst == 0)
			adjtime.tsecs += SECS_PER_HOUR;
		adjtime.tm = *(localtime(&adjtime.tsecs));
		/* Remember that this adjustment has been made */
		adjtime.did_adj4dst = dstsrc->tm.tm_isdst;
		/*
		 * XXX - Should probably check to see if changing the
		 *	hour also changed the value of is_dst.  What
		 *	should we do in that case?
		 */
	}

	*ptime = adjtime;
	return (0);
}

int
ptime_relparse(struct ptime_data *ptime, int parseopts, time_t basetime,
    const char *str)
{
	int dpm, pres;
	struct tm temp_tm;

	ptime->parseopts = parseopts;
	ptime->basesecs = basetime;
	ptime->basetm = *(localtime(&ptime->basesecs));
	ptime->tm = ptime->basetm;
	ptime->tm.tm_hour = ptime->tm.tm_min = ptime->tm.tm_sec = 0;

	/*
	 * Call a routine which sets ptime.tm and ptime.tspecs based
	 * on the given string and parsing-options.  Note that the
	 * routine should not call mktime to set ptime.tsecs.
	 */
	if (parseopts & PTM_PARSE_DWM)
		pres = parseDWM(ptime, str);
	else
		pres = parse8601(ptime, str);
	if (pres < 0) {
		ptime->tsecs = (time_t)pres;
		return (pres);
	}

	/*
	 * Before calling mktime, check to see if we ended up with a
	 * "day-of-month" that does not exist in the selected month.
	 * If we did call mktime with that info, then mktime will
	 * make it look like the user specifically requested a day
	 * in the following month (eg: Feb 31 turns into Mar 3rd).
	 */
	dpm = days_pmonth(ptime->tm.tm_mon, ptime->tm.tm_year);
	if ((parseopts & PTM_PARSE_MATCHDOM) &&
	    (ptime->tmspec & TSPEC_DAYOFMONTH) &&
	    (ptime->tm.tm_mday> dpm)) {
		/*
		 * ptime_nxtime() will want a ptime->tsecs value,
		 * but we need to avoid mktime resetting all the
		 * ptime->tm values.
		 */
		if (verbose && dbg_at_times > 1)
			fprintf(stderr,
			    "\t-- dom fixed: %4d/%02d/%02d %02d:%02d (%02d)",
			    ptime->tm.tm_year, ptime->tm.tm_mon,
			    ptime->tm.tm_mday, ptime->tm.tm_hour,
			    ptime->tm.tm_min, dpm);
		temp_tm = ptime->tm;
		ptime->tsecs = mktime(&temp_tm);
		if (ptime->tsecs > (time_t)-1)
			ptimeset_nxtime(ptime);
		if (verbose && dbg_at_times > 1)
			fprintf(stderr,
			    " to: %4d/%02d/%02d %02d:%02d\n",
			    ptime->tm.tm_year, ptime->tm.tm_mon,
			    ptime->tm.tm_mday, ptime->tm.tm_hour,
			    ptime->tm.tm_min);
	}

	/*
	 * Convert the ptime.tm into standard time_t seconds.  Check
	 * for invalid times, which includes things like the hour lost
	 * when switching from "standard time" to "daylight saving".
	 */
	ptime->tsecs = mktime(&ptime->tm);
	if (ptime->tsecs == (time_t)-1) {
		ptime->tsecs = (time_t)-2;
		return (-2);
	}

	return (0);
}

int
ptime_free(struct ptime_data *ptime)
{

	if (ptime == NULL)
		return (-1);

	free(ptime);
	return (0);
}

/*
 * Some trivial routines so ptime_data can remain a completely
 * opaque type.
 */
const char *
ptimeget_ctime(const struct ptime_data *ptime)
{

	if (ptime == NULL)
		return ("Null time in ptimeget_ctime()\n");

	return (ctime(&ptime->tsecs));
}

double
ptimeget_diff(const struct ptime_data *minuend, const struct
    ptime_data *subtrahend)
{

	/* Just like difftime(), we have no good error-return */
	if (minuend == NULL || subtrahend == NULL)
		return (0.0);

	return (difftime(minuend->tsecs, subtrahend->tsecs));
}

time_t
ptimeget_secs(const struct ptime_data *ptime)
{

	if (ptime == NULL)
		return (-1);

	return (ptime->tsecs);
}

/*
 * Generate an approximate timestamp for the next event, based on
 * what parts of time were specified by the original parameter to
 * ptime_relparse(). The result may be -1 if there is no obvious
 * "next time" which will work.
 */
int
ptimeset_nxtime(struct ptime_data *ptime)
{
	int moredays, tdpm, tmon, tyear;
	struct ptime_data nextmatch;

	if (ptime == NULL)
		return (-1);

	/*
	 * Changes are not made to the given time until after all
	 * of the calculations have been successful.
	 */
	nextmatch = *ptime;
	/*
	 * If the user specified a year and we're already past that
	 * time, then there will never be another one!
	 */
	if (ptime->tmspec & TSPEC_YEAR)
		return (-1);

	/*
	 * The caller gave us a time in the past.  Calculate how much
	 * time is needed to go from that valid rotate time to the
	 * next valid rotate time.  We only need to get to the nearest
	 * hour, because newsyslog is only run once per hour.
	 */
	moredays = 0;
	if (ptime->tmspec & TSPEC_MONTHOFYEAR) {
		/* Special case: Feb 29th does not happen every year. */
		if (ptime->tm.tm_mon == 1 && ptime->tm.tm_mday == 29) {
			nextmatch.tm.tm_year += 4;
			if (days_pmonth(1, nextmatch.tm.tm_year) < 29)
				nextmatch.tm.tm_year += 4;
		} else {
			nextmatch.tm.tm_year += 1;
		}
		nextmatch.tm.tm_isdst = -1;
		nextmatch.tsecs = mktime(&nextmatch.tm);

	} else if (ptime->tmspec & TSPEC_LDAYOFMONTH) {
		/*
		 * Need to get to the last day of next month.  Origtm is
		 * already at the last day of this month, so just add to
		 * it number of days in the next month.
		 */
		if (ptime->tm.tm_mon < 11)
			moredays = days_pmonth(ptime->tm.tm_mon + 1,
			    ptime->tm.tm_year);
		else
			moredays = days_pmonth(0, ptime->tm.tm_year + 1);

	} else if (ptime->tmspec & TSPEC_DAYOFMONTH) {
		/* Jump to the same day in the next month */
		moredays = days_pmonth(ptime->tm.tm_mon, ptime->tm.tm_year);
		/*
		 * In some cases, the next month may not *have* the
		 * desired day-of-the-month.  If that happens, then
		 * move to the next month that does have enough days.
		 */
		tmon = ptime->tm.tm_mon;
		tyear = ptime->tm.tm_year;
		for (;;) {
			if (tmon < 11)
				tmon += 1;
			else {
				tmon = 0;
				tyear += 1;
			}
			tdpm = days_pmonth(tmon, tyear);
			if (tdpm >= ptime->tm.tm_mday)
				break;
			moredays += tdpm;
		}

	} else if (ptime->tmspec & TSPEC_DAYOFWEEK) {
		moredays = 7;
	} else if (ptime->tmspec & TSPEC_HOUROFDAY) {
		moredays = 1;
	}

	if (moredays != 0) {
		nextmatch.tsecs += SECS_PER_HOUR * 24 * moredays;
		nextmatch.tm = *(localtime(&nextmatch.tsecs));
	}

	/*
	 * The new time will need to be adjusted if the setting of
	 * daylight-saving has changed between the two times.
	 */
	ptime_adjust4dst(&nextmatch, ptime);

	/* Everything worked.  Update the given time and return. */
	*ptime = nextmatch;
	return (0);
}

int
ptimeset_time(struct ptime_data *ptime, time_t secs)
{

	if (ptime == NULL)
		return (-1);

	ptime->tsecs = secs;
	ptime->tm = *(localtime(&ptime->tsecs));
	ptime->parseopts = 0;
	/* ptime->tmspec = ? */
	return (0);
}
Back to Top