1 /*-
2  * Copyright (c) 2003 Andrey Simonenko
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *  @(#)$Id: ipa_time.h,v 1.3 2011/01/23 18:42:35 simon Exp $
27  */
28 
29 #ifndef IPA_TIME_H
30 #define IPA_TIME_H
31 
32 /* update_time parameter default value. */
33 #define UPDATE_TIME_DEF		(1 * SECONDS_IN_MINUTE)
34 
35 /* wakeup_time parameter default value. */
36 #define WAKEUP_TIME_DEF		(10 * SECONDS_IN_MINUTE)
37 
38 /* sensitive_time parameter default value. */
39 #define SENSITIVE_TIME_DEF	(SECONDS_IN_MINUTE / 2)
40 
41 #define TIME_TO_SEC(tm) (			\
42 	(tm)->tm_hour * SECONDS_IN_HOUR +	\
43 	(tm)->tm_min * SECONDS_IN_MINUTE +	\
44 	(tm)->tm_sec				\
45 )
46 
47 /* Mark event as not scheduled. */
48 #define EVENT_NOT_SCHEDULED	(1000000)
49 
50 /*
51  * If time of scheduled event is less than current time
52  * no more than FAKE_TIME_DELTA, then fake current time.
53  * To disable this feature, set this macro to zero.
54  */
55 #ifndef FAKE_TIME_DELTA
56 # define FAKE_TIME_DELTA	5
57 #endif
58 
59 /*
60  * One time event.
61  */
62 struct tevent {
63 	SLIST_ENTRY(tevent) link;
64 	unsigned int	event_sec;	/* Next event time. */
65 	unsigned int	event_step;	/* Interval between events. */
66 };
67 
68 #define TEVENT_NSIZE	10
69 #define TEVENT_NALLOC	10
70 
71 /*
72  * List of all time events.
73  */
74 SLIST_HEAD(tevents_list, tevent);
75 
76 /*
77  * One time interval in "worktime" parameter in seconds
78  * starting from midnight.
79  */
80 struct tint {
81 	STAILQ_ENTRY(tint) link;
82 	unsigned int	sec1;		/* From sec1. */
83 	unsigned int	sec2;		/* Till sec2. */
84 };
85 
86 #define TINT_NSIZE	20
87 #define TINT_NALLOC	20
88 
89 /*
90  * List of all time intervals for one day.
91  */
92 STAILQ_HEAD(tint_list, tint);
93 
94 /*
95  * List of all tint_lists.
96  */
97 struct tint_set {
98 	STAILQ_ENTRY(tint_set) link;	/* For list of all tint_sets. */
99 	struct tint_list list;		/* One tint_list. */
100 };
101 
102 STAILQ_HEAD(tint_sets, tint_set);
103 
104 #define WT_FLAG_ACTIVE		0x1	/* Worktime is active. */
105 
106 #define WT_IS_ACTIVE(x)		((x)->wt_flags == WT_FLAG_ACTIVE)
107 #define WT_IS_INACTIVE(x)	(!WT_IS_ACTIVE(x))
108 
109 #define WT_SET_ACTIVE(x)	((x)->wt_flags = WT_FLAG_ACTIVE)
110 #define WT_SET_INACTIVE(x)	((x)->wt_flags = 0)
111 
112 /*
113  * One worktime.
114  */
115 struct worktime {
116 	SLIST_ENTRY(worktime) link;	/* For list building. */
117 	const struct tint_list *tint_list[DAYS_IN_WEEK];
118 	const struct tint *curtint;	/* Current time interval. */
119 	unsigned int	active_sec;	/* Time when to make wt active. */
120 	unsigned int	inactive_sec;	/* Time when to make wt inactive. */
121 	unsigned int	wt_flags;	/* 0 or WT_FLAG_ACTIVE. */
122 };
123 
124 #define WORKTIME_NSIZE	5
125 #define WORKTIME_NALLOC	5
126 
127 /*
128  * List of all worktimes.
129  */
130 SLIST_HEAD(worktimes_list, worktime);
131 
132 #ifdef WITH_LIMITS
133 /*
134  * [+<X>] <time> or <time> +<X> like parameter value.
135  */
136 struct texp {
137 	unsigned int	seconds;	/* Number of seconds. */
138 	char		side;		/* 0 if +X <time>, 1 if <time> +X. */
139 	char		upto;		/* TEXP_UPTO_xxx. */
140 };
141 
142 #define TEXP_UPTO_NOTSET	'-'
143 #define TEXP_UPTO_SIMPLE	' '
144 #define TEXP_UPTO_MINUTE	'm'
145 #define TEXP_UPTO_HOUR		'h'
146 #define TEXP_UPTO_DAY		'D'
147 #define TEXP_UPTO_WEEK		'W'
148 #define TEXP_UPTO_MONTH		'M'
149 
150 #endif /* WITH_LIMITS */
151 
152 /* Buffer length for representing seconds as time. */
153 #define SEC_STR_SIZE	(sizeof("xx:xx:xx"))
154 
155 /* Buffer length for representing uint64_t seconds. */
156 #define TIME_STR_SIZE	17
157 
158 /* Buffer length for representing date and time in struct tm{}. */
159 #define TM_STR_SIZE	(sizeof("xxxx.xx.xx/xx:xx:xx") + 10)
160 
161 extern unsigned int wakeup_time;
162 extern unsigned int freeze_time;
163 extern unsigned int sleep_after_dump;
164 extern unsigned int sensitive_time;
165 extern signed char debug_time;
166 extern signed char debug_worktime;
167 
168 extern const struct worktime *global_worktime;
169 extern struct worktime worktime_default;
170 
171 extern const struct tevent *global_update_tevent;
172 extern const struct tevent *global_append_tevent;
173 
174 extern const char *const wdays[];
175 
176 extern const char *const active_msg[];
177 
178 extern ipa_mzone *tint_mzone;
179 extern struct tint_sets tint_sets;
180 
181 extern ipa_mzone *worktime_mzone;
182 extern struct worktimes_list worktimes_list;
183 
184 extern ipa_mzone *tevent_mzone;
185 extern struct tevents_list tevents_list;
186 
187 extern time_t	curtime;
188 extern ipa_tm	curdate, curdate_new;
189 extern unsigned int cursec, cursec_new;
190 extern unsigned int curwday;
191 extern char	newday_flag;
192 
193 extern void	sec_to_time(unsigned int, ipa_tm *);
194 extern const char *time_str(unsigned int);
195 extern const char *sec_str(unsigned int);
196 extern const char *tint_str(const struct tint *);
197 
198 extern void	free_tint_set(struct tint_set *);
199 extern void	free_worktimes(void);
200 extern void	init_worktime_default(void);
201 extern const struct worktime *find_worktime(struct worktime *);
202 
203 extern unsigned int worktimes_check_sec;
204 
205 extern void	worktimes_newday(int);
206 extern void	worktimes_check(void);
207 
208 #ifdef WITH_LIMITS
209 extern int	set_wday(ipa_tm *);
210 extern void	ipa_tm_texp(ipa_tm *, const struct texp *);
211 #endif
212 
213 #ifdef WITH_THRESHOLDS
214 extern void	ipa_tm_sub(ipa_tm *, unsigned int);
215 #endif
216 
217 #ifdef WITH_ANY_LIMITS
218 extern void	fix_240000(ipa_tm *);
219 extern int	cmp_ipa_tm(const ipa_tm *, const ipa_tm *);
220 extern int	check_ipa_tm(const ipa_tm *);
221 extern unsigned int ipa_tm_diff(const ipa_tm *, const ipa_tm *);
222 extern const char *tm_str(const ipa_tm *);
223 extern const char *tm_str2(const ipa_tm *);
224 extern int	check_worktime_subset(const struct worktime *,
225 		    const struct worktime *);
226 #endif
227 
228 extern int	init_time_values(void);
229 extern int	new_time_values(void);
230 
231 #endif /* !IPA_TIME_H */
232