[83b242c] | 1 | #include "schedule.h" |
---|
| 2 | #include "list.h" |
---|
| 3 | #include <sys/types.h> |
---|
| 4 | #include <stdbool.h> |
---|
| 5 | #include <stdint.h> |
---|
| 6 | #include <stdlib.h> |
---|
| 7 | #include <syslog.h> |
---|
| 8 | #include <time.h> |
---|
| 9 | #include <ev.h> |
---|
| 10 | |
---|
| 11 | struct og_schedule *current_schedule = NULL; |
---|
| 12 | static LIST_HEAD(schedule_list); |
---|
| 13 | |
---|
| 14 | static void og_schedule_add(struct og_schedule *new) |
---|
| 15 | { |
---|
| 16 | struct og_schedule *schedule, *next; |
---|
| 17 | time_t now; |
---|
| 18 | |
---|
| 19 | now = time(NULL); |
---|
| 20 | if (new->seconds < now) { |
---|
| 21 | free(new); |
---|
| 22 | return; |
---|
| 23 | } |
---|
| 24 | list_for_each_entry_safe(schedule, next, &schedule_list, list) { |
---|
| 25 | if (new->seconds < schedule->seconds) { |
---|
| 26 | list_add_tail(&new->list, &schedule->list); |
---|
| 27 | return; |
---|
| 28 | } |
---|
| 29 | } |
---|
| 30 | list_add_tail(&new->list, &schedule_list); |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | /* Returns the days in a month from the weekday. */ |
---|
| 34 | static void get_days_from_weekday(struct tm *tm, int wday, int *days, int *j) |
---|
| 35 | { |
---|
| 36 | int i, mday = 0; |
---|
| 37 | |
---|
| 38 | tm->tm_mday = 1; |
---|
| 39 | |
---|
| 40 | //Shift week to start on Sunday instead of Monday |
---|
| 41 | if (wday == 6) |
---|
| 42 | wday = 0; |
---|
| 43 | else |
---|
| 44 | wday++; |
---|
| 45 | |
---|
| 46 | /* A bit bruteforce, but simple. */ |
---|
| 47 | for (i = 0; i <= 30; i++) { |
---|
| 48 | mktime(tm); |
---|
| 49 | /* Not this weekday, skip. */ |
---|
| 50 | if (tm->tm_wday != wday) { |
---|
| 51 | tm->tm_mday++; |
---|
| 52 | continue; |
---|
| 53 | } |
---|
| 54 | /* Not interested in next month. */ |
---|
| 55 | if (tm->tm_mday < mday) |
---|
| 56 | break; |
---|
| 57 | |
---|
| 58 | /* Found a matching. */ |
---|
| 59 | mday = tm->tm_mday; |
---|
| 60 | days[(*j)++] = tm->tm_mday; |
---|
| 61 | tm->tm_mday++; |
---|
| 62 | } |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | /* Returns the days in the given week. */ |
---|
| 66 | static void get_days_from_week(struct tm *tm, int week, int *days, int *k) |
---|
| 67 | { |
---|
| 68 | int i, j, week_counter = 0; |
---|
| 69 | bool week_over = false; |
---|
| 70 | |
---|
| 71 | tm->tm_mday = 1; |
---|
| 72 | |
---|
| 73 | /* Remaining days of this month. */ |
---|
| 74 | for (i = 0; i <= 30; i++) { |
---|
| 75 | mktime(tm); |
---|
| 76 | |
---|
| 77 | /* Last day of this week? */ |
---|
| 78 | if (tm->tm_wday == 6) |
---|
| 79 | week_over = true; |
---|
| 80 | |
---|
| 81 | /* Not the week we are searching for. */ |
---|
| 82 | if (week != week_counter) { |
---|
| 83 | tm->tm_mday++; |
---|
| 84 | if (week_over) { |
---|
| 85 | week_counter++; |
---|
| 86 | week_over = false; |
---|
| 87 | } |
---|
| 88 | continue; |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | /* Found matching. */ |
---|
| 92 | for (j = tm->tm_wday; j <= 6; j++) { |
---|
| 93 | days[(*k)++] = tm->tm_mday++; |
---|
| 94 | mktime(tm); |
---|
| 95 | } |
---|
| 96 | break; |
---|
| 97 | } |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | static int monthdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; |
---|
| 101 | |
---|
| 102 | static int last_month_day(struct tm *tm) |
---|
| 103 | { |
---|
| 104 | /* Leap year? Adjust it. */ |
---|
| 105 | if (tm->tm_mon == 1) { |
---|
| 106 | tm->tm_mday = 29; |
---|
| 107 | mktime(tm); |
---|
| 108 | if (tm->tm_mday == 29) |
---|
| 109 | return 29; |
---|
| 110 | |
---|
| 111 | tm->tm_mon = 1; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | return monthdays[tm->tm_mon]; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /* Returns the days in the given week. */ |
---|
| 118 | static void get_last_week(struct tm *tm, int *days, int *j) |
---|
| 119 | { |
---|
| 120 | int i, last_day; |
---|
| 121 | |
---|
| 122 | last_day = last_month_day(tm); |
---|
| 123 | tm->tm_mday = last_day; |
---|
| 124 | |
---|
| 125 | for (i = last_day; i >= last_day - 6; i--) { |
---|
| 126 | mktime(tm); |
---|
| 127 | |
---|
| 128 | days[(*j)++] = tm->tm_mday; |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | /* Last day of this week? */ |
---|
[130b6ff] | 132 | if (tm->tm_wday == 1) |
---|
[83b242c] | 133 | break; |
---|
| 134 | |
---|
| 135 | tm->tm_mday--; |
---|
| 136 | } |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | static void og_parse_years(uint16_t years_mask, int years[]) |
---|
| 140 | { |
---|
| 141 | int i, j = 0; |
---|
| 142 | |
---|
| 143 | for (i = 0; i < 16; i++) { |
---|
| 144 | if ((1 << i) & years_mask) |
---|
| 145 | years[j++] = 2010 + i - 1900; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | static void og_parse_months(uint16_t months_mask, int months[]) |
---|
| 150 | { |
---|
| 151 | int i, j = 0; |
---|
| 152 | |
---|
| 153 | for (i = 0; i < 12; i++) { |
---|
| 154 | if ((1 << i) & months_mask) |
---|
| 155 | months[j++] = i + 1; |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | static void og_parse_days(uint32_t days_mask, int *days) |
---|
| 160 | { |
---|
| 161 | int i, j = 0; |
---|
| 162 | |
---|
| 163 | for (i = 0; i < 31; i++) { |
---|
| 164 | if ((1 << i) & days_mask) |
---|
| 165 | days[j++] = i + 1; |
---|
| 166 | } |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | static void og_parse_hours(uint16_t hours_mask, uint8_t am_pm, int hours[]) |
---|
| 170 | { |
---|
| 171 | int pm = 12 * am_pm; |
---|
| 172 | int i, j = 0; |
---|
| 173 | |
---|
| 174 | for (i = 0; i < 12; i++) { |
---|
| 175 | if ((1 << i) & hours_mask) |
---|
| 176 | hours[j++] = i + pm + 1; |
---|
| 177 | } |
---|
| 178 | } |
---|
| 179 | |
---|
| 180 | static void og_schedule_remove_duplicates() |
---|
| 181 | { |
---|
| 182 | struct og_schedule *schedule, *next, *prev = NULL; |
---|
| 183 | |
---|
| 184 | list_for_each_entry_safe(schedule, next, &schedule_list, list) { |
---|
| 185 | if (!prev) { |
---|
| 186 | prev = schedule; |
---|
| 187 | continue; |
---|
| 188 | } |
---|
| 189 | if (prev->seconds == schedule->seconds && |
---|
| 190 | prev->task_id == schedule->task_id) { |
---|
| 191 | list_del(&prev->list); |
---|
| 192 | free(prev); |
---|
| 193 | } |
---|
| 194 | prev = schedule; |
---|
| 195 | } |
---|
| 196 | } |
---|
| 197 | |
---|
[130b6ff] | 198 | static void og_schedule_create_weekdays(int month, int year, |
---|
| 199 | int *hours, int minutes, int week_days, |
---|
[63c4ea5] | 200 | uint32_t task_id, uint32_t schedule_id, |
---|
| 201 | enum og_schedule_type type) |
---|
[130b6ff] | 202 | { |
---|
| 203 | struct og_schedule *schedule; |
---|
| 204 | int month_days[5]; |
---|
| 205 | int n_month_days; |
---|
| 206 | uint32_t wday; |
---|
| 207 | struct tm tm; |
---|
| 208 | int k, l; |
---|
| 209 | |
---|
| 210 | for (wday = 0; wday < 7; wday++) { |
---|
| 211 | if (!((1 << wday) & week_days)) |
---|
| 212 | continue; |
---|
| 213 | |
---|
| 214 | memset(&tm, 0, sizeof(tm)); |
---|
| 215 | tm.tm_mon = month; |
---|
| 216 | tm.tm_year = year; |
---|
| 217 | |
---|
| 218 | n_month_days = 0; |
---|
| 219 | memset(month_days, 0, sizeof(month_days)); |
---|
| 220 | get_days_from_weekday(&tm, wday, month_days, &n_month_days); |
---|
| 221 | |
---|
| 222 | for (k = 0; month_days[k] != 0 && k < n_month_days; k++) { |
---|
| 223 | for (l = 0; hours[l] != 0 && l < 31; l++) { |
---|
| 224 | schedule = (struct og_schedule *) |
---|
| 225 | calloc(1, sizeof(struct og_schedule)); |
---|
| 226 | if (!schedule) |
---|
| 227 | return; |
---|
| 228 | |
---|
| 229 | memset(&tm, 0, sizeof(tm)); |
---|
| 230 | tm.tm_year = year; |
---|
| 231 | tm.tm_mon = month; |
---|
| 232 | tm.tm_mday = month_days[k]; |
---|
| 233 | tm.tm_hour = hours[l] - 1; |
---|
| 234 | tm.tm_min = minutes; |
---|
| 235 | |
---|
| 236 | schedule->seconds = mktime(&tm); |
---|
| 237 | schedule->task_id = task_id; |
---|
| 238 | schedule->schedule_id = schedule_id; |
---|
[63c4ea5] | 239 | schedule->type = type; |
---|
[130b6ff] | 240 | og_schedule_add(schedule); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | } |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | static void og_schedule_create_weeks(int month, int year, |
---|
| 247 | int *hours, int minutes, int weeks, |
---|
[63c4ea5] | 248 | uint32_t task_id, uint32_t schedule_id, |
---|
| 249 | enum og_schedule_type type) |
---|
[130b6ff] | 250 | { |
---|
| 251 | struct og_schedule *schedule; |
---|
| 252 | int month_days[7]; |
---|
| 253 | int n_month_days; |
---|
| 254 | struct tm tm; |
---|
| 255 | int week; |
---|
| 256 | int k, l; |
---|
| 257 | |
---|
| 258 | for (week = 0; week < 5; week++) { |
---|
| 259 | if (!((1 << week) & weeks)) |
---|
| 260 | continue; |
---|
| 261 | |
---|
| 262 | memset(&tm, 0, sizeof(tm)); |
---|
| 263 | tm.tm_mon = month; |
---|
| 264 | tm.tm_year = year; |
---|
| 265 | |
---|
| 266 | n_month_days = 0; |
---|
| 267 | memset(month_days, 0, sizeof(month_days)); |
---|
| 268 | if (week == 5) |
---|
| 269 | get_last_week(&tm, month_days, &n_month_days); |
---|
| 270 | else |
---|
| 271 | get_days_from_week(&tm, week, month_days, &n_month_days); |
---|
| 272 | |
---|
| 273 | for (k = 0; month_days[k] != 0 && k < n_month_days; k++) { |
---|
| 274 | for (l = 0; hours[l] != 0 && l < 31; l++) { |
---|
| 275 | schedule = (struct og_schedule *) |
---|
| 276 | calloc(1, sizeof(struct og_schedule)); |
---|
| 277 | if (!schedule) |
---|
| 278 | return; |
---|
| 279 | |
---|
| 280 | memset(&tm, 0, sizeof(tm)); |
---|
| 281 | tm.tm_year = year; |
---|
| 282 | tm.tm_mon = month; |
---|
| 283 | tm.tm_mday = month_days[k]; |
---|
| 284 | tm.tm_hour = hours[l] - 1; |
---|
| 285 | tm.tm_min = minutes; |
---|
| 286 | |
---|
| 287 | schedule->seconds = mktime(&tm); |
---|
| 288 | schedule->task_id = task_id; |
---|
| 289 | schedule->schedule_id = schedule_id; |
---|
[63c4ea5] | 290 | schedule->type = type; |
---|
[130b6ff] | 291 | og_schedule_add(schedule); |
---|
| 292 | } |
---|
| 293 | } |
---|
| 294 | } |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | static void og_schedule_create_days(int month, int year, |
---|
| 298 | int *hours, int minutes, int *days, |
---|
[63c4ea5] | 299 | uint32_t task_id, uint32_t schedule_id, |
---|
| 300 | enum og_schedule_type type) |
---|
[130b6ff] | 301 | { |
---|
| 302 | struct og_schedule *schedule; |
---|
| 303 | struct tm tm; |
---|
| 304 | int k, l; |
---|
| 305 | |
---|
| 306 | for (k = 0; days[k] != 0 && k < 31; k++) { |
---|
| 307 | for (l = 0; hours[l] != 0 && l < 31; l++) { |
---|
| 308 | schedule = (struct og_schedule *) |
---|
| 309 | calloc(1, sizeof(struct og_schedule)); |
---|
| 310 | if (!schedule) |
---|
| 311 | return; |
---|
| 312 | |
---|
| 313 | memset(&tm, 0, sizeof(tm)); |
---|
| 314 | tm.tm_year = year; |
---|
| 315 | tm.tm_mon = month; |
---|
| 316 | tm.tm_mday = days[k]; |
---|
| 317 | tm.tm_hour = hours[l] - 1; |
---|
| 318 | tm.tm_min = minutes; |
---|
| 319 | |
---|
| 320 | schedule->seconds = mktime(&tm); |
---|
| 321 | schedule->task_id = task_id; |
---|
| 322 | schedule->schedule_id = schedule_id; |
---|
[63c4ea5] | 323 | schedule->type = type; |
---|
[130b6ff] | 324 | og_schedule_add(schedule); |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | } |
---|
| 328 | |
---|
[83b242c] | 329 | void og_schedule_create(unsigned int schedule_id, unsigned int task_id, |
---|
[63c4ea5] | 330 | enum og_schedule_type type, |
---|
[83b242c] | 331 | struct og_schedule_time *time) |
---|
| 332 | { |
---|
[130b6ff] | 333 | int year, month, minutes; |
---|
[83b242c] | 334 | int months[12] = {}; |
---|
| 335 | int years[12] = {}; |
---|
| 336 | int hours[12] = {}; |
---|
| 337 | int days[31] = {}; |
---|
[130b6ff] | 338 | int i, j; |
---|
[83b242c] | 339 | |
---|
| 340 | og_parse_years(time->years, years); |
---|
| 341 | og_parse_months(time->months, months); |
---|
| 342 | og_parse_days(time->days, days); |
---|
| 343 | og_parse_hours(time->hours, time->am_pm, hours); |
---|
| 344 | minutes = time->minutes; |
---|
| 345 | |
---|
| 346 | for (i = 0; years[i] != 0 && i < 12; i++) { |
---|
| 347 | for (j = 0; months[j] != 0 && j < 12; j++) { |
---|
[130b6ff] | 348 | month = months[j] - 1; |
---|
| 349 | year = years[i]; |
---|
| 350 | |
---|
| 351 | if (time->week_days) |
---|
| 352 | og_schedule_create_weekdays(month, year, |
---|
| 353 | hours, minutes, |
---|
| 354 | time->week_days, |
---|
| 355 | task_id, |
---|
[63c4ea5] | 356 | schedule_id, |
---|
| 357 | type); |
---|
[130b6ff] | 358 | |
---|
| 359 | if (time->weeks) |
---|
| 360 | og_schedule_create_weeks(month, year, |
---|
| 361 | hours, minutes, |
---|
| 362 | time->weeks, |
---|
| 363 | task_id, |
---|
[63c4ea5] | 364 | schedule_id, |
---|
| 365 | type); |
---|
[130b6ff] | 366 | |
---|
| 367 | if (time->days) |
---|
| 368 | og_schedule_create_days(month, year, |
---|
| 369 | hours, minutes, |
---|
| 370 | days, |
---|
| 371 | task_id, |
---|
[63c4ea5] | 372 | schedule_id, |
---|
| 373 | type); |
---|
[83b242c] | 374 | } |
---|
| 375 | } |
---|
| 376 | |
---|
| 377 | og_schedule_remove_duplicates(); |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | void og_schedule_delete(struct ev_loop *loop, uint32_t schedule_id) |
---|
| 381 | { |
---|
| 382 | struct og_schedule *schedule, *next; |
---|
| 383 | |
---|
| 384 | list_for_each_entry_safe(schedule, next, &schedule_list, list) { |
---|
| 385 | if (schedule->schedule_id != schedule_id) |
---|
| 386 | continue; |
---|
| 387 | |
---|
| 388 | list_del(&schedule->list); |
---|
| 389 | if (current_schedule == schedule) { |
---|
| 390 | ev_timer_stop(loop, &schedule->timer); |
---|
| 391 | current_schedule = NULL; |
---|
| 392 | og_schedule_refresh(loop); |
---|
| 393 | } |
---|
| 394 | free(schedule); |
---|
| 395 | } |
---|
| 396 | } |
---|
| 397 | |
---|
| 398 | void og_schedule_update(struct ev_loop *loop, unsigned int schedule_id, |
---|
| 399 | unsigned int task_id, struct og_schedule_time *time) |
---|
| 400 | { |
---|
| 401 | og_schedule_delete(loop, schedule_id); |
---|
[63c4ea5] | 402 | og_schedule_create(schedule_id, task_id, OG_SCHEDULE_TASK, time); |
---|
[83b242c] | 403 | } |
---|
| 404 | |
---|
| 405 | static void og_agent_timer_cb(struct ev_loop *loop, ev_timer *timer, int events) |
---|
| 406 | { |
---|
| 407 | struct og_schedule *current; |
---|
| 408 | |
---|
| 409 | current = container_of(timer, struct og_schedule, timer); |
---|
[85b345d] | 410 | og_dbi_schedule_task(current->task_id, current->schedule_id); |
---|
[83b242c] | 411 | |
---|
| 412 | ev_timer_stop(loop, timer); |
---|
| 413 | list_del(¤t->list); |
---|
| 414 | free(current); |
---|
| 415 | |
---|
| 416 | og_schedule_next(loop); |
---|
| 417 | } |
---|
| 418 | |
---|
| 419 | void og_schedule_next(struct ev_loop *loop) |
---|
| 420 | { |
---|
| 421 | struct og_schedule *schedule; |
---|
| 422 | time_t now, seconds; |
---|
| 423 | |
---|
| 424 | if (list_empty(&schedule_list)) { |
---|
| 425 | current_schedule = NULL; |
---|
| 426 | return; |
---|
| 427 | } |
---|
| 428 | |
---|
| 429 | schedule = list_first_entry(&schedule_list, struct og_schedule, list); |
---|
| 430 | now = time(NULL); |
---|
| 431 | if (schedule->seconds <= now) |
---|
| 432 | seconds = 0; |
---|
| 433 | else |
---|
| 434 | seconds = schedule->seconds - now; |
---|
| 435 | |
---|
| 436 | ev_timer_init(&schedule->timer, og_agent_timer_cb, seconds, 0.); |
---|
| 437 | ev_timer_start(loop, &schedule->timer); |
---|
| 438 | current_schedule = schedule; |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | void og_schedule_refresh(struct ev_loop *loop) |
---|
| 442 | { |
---|
| 443 | if (current_schedule) |
---|
| 444 | ev_timer_stop(loop, ¤t_schedule->timer); |
---|
| 445 | |
---|
| 446 | og_schedule_next(loop); |
---|
| 447 | } |
---|