|
| 1 | +/* SPDX-License-Identifier: BSD-3-Clause */ |
| 2 | + |
| 3 | +#include <signal.h> |
| 4 | +#include <stdio.h> |
| 5 | +#include <stdlib.h> |
| 6 | + |
| 7 | +#include <libite/lite.h> |
| 8 | +#include <srx/common.h> |
| 9 | +#include <srx/lyx.h> |
| 10 | +#include <srx/srx_val.h> |
| 11 | +#include "core.h" |
| 12 | + |
| 13 | +#define XPATH_BASE "/ietf-system:system/infix-schedule:schedules" |
| 14 | +#define CRONTAB_FILE "/var/spool/cron/crontabs/admin" |
| 15 | + |
| 16 | +static const char *action_to_cmd(const char *action) |
| 17 | +{ |
| 18 | + if (!action) |
| 19 | + return NULL; |
| 20 | + if (strstr(action, "reboot")) |
| 21 | + return "/usr/sbin/reboot"; |
| 22 | + if (strstr(action, "check-update")) |
| 23 | + return "/usr/sbin/infix-check-update"; |
| 24 | + return NULL; |
| 25 | +} |
| 26 | + |
| 27 | +/* |
| 28 | + * Convert ietf-schedule recurrence to a 5-field cron expression. |
| 29 | + * |
| 30 | + * Frequency mapping: |
| 31 | + * minutely/N → *\/N * * * * |
| 32 | + * hourly/N → 0 *\/N * * * |
| 33 | + * daily/N → 0 0 *\/N * * |
| 34 | + * weekly/N → 0 0 * * *\/N |
| 35 | + * monthly/N → 0 0 1 *\/N * |
| 36 | + * |
| 37 | + * Optional by-* leaves refine the expression: |
| 38 | + * byminute → replaces the minute field |
| 39 | + * byhour → replaces the hour field |
| 40 | + * byday → replaces the day-of-week field |
| 41 | + * bymonthday → replaces the day-of-month field (positive values only, 1-31) |
| 42 | + * byyearmonth → replaces the month field |
| 43 | + */ |
| 44 | +static void build_cron_expr(struct lyd_node *recurrence, char *expr, size_t sz) |
| 45 | +{ |
| 46 | + const char *freq, *ivstr; |
| 47 | + struct lyd_node *node; |
| 48 | + char min[64], hr[64], dom[64], mon[64], dow[64]; |
| 49 | + int iv, first; |
| 50 | + |
| 51 | + snprintf(min, sizeof(min), "*"); |
| 52 | + snprintf(hr, sizeof(hr), "*"); |
| 53 | + snprintf(dom, sizeof(dom), "*"); |
| 54 | + snprintf(mon, sizeof(mon), "*"); |
| 55 | + snprintf(dow, sizeof(dow), "*"); |
| 56 | + |
| 57 | + freq = lydx_get_cattr(recurrence, "frequency"); |
| 58 | + ivstr = lydx_get_cattr(recurrence, "interval"); |
| 59 | + if (!freq || !ivstr) |
| 60 | + goto done; |
| 61 | + |
| 62 | + iv = atoi(ivstr); |
| 63 | + if (iv <= 0) |
| 64 | + iv = 1; |
| 65 | + |
| 66 | + if (strstr(freq, "minutely")) { |
| 67 | + if (iv == 1) |
| 68 | + snprintf(min, sizeof(min), "*"); |
| 69 | + else |
| 70 | + snprintf(min, sizeof(min), "*/%d", iv); |
| 71 | + } else if (strstr(freq, "hourly")) { |
| 72 | + snprintf(min, sizeof(min), "0"); |
| 73 | + if (iv == 1) |
| 74 | + snprintf(hr, sizeof(hr), "*"); |
| 75 | + else |
| 76 | + snprintf(hr, sizeof(hr), "*/%d", iv); |
| 77 | + } else if (strstr(freq, "daily")) { |
| 78 | + snprintf(min, sizeof(min), "0"); |
| 79 | + snprintf(hr, sizeof(hr), "0"); |
| 80 | + if (iv > 1) |
| 81 | + snprintf(dom, sizeof(dom), "*/%d", iv); |
| 82 | + } else if (strstr(freq, "weekly")) { |
| 83 | + snprintf(min, sizeof(min), "0"); |
| 84 | + snprintf(hr, sizeof(hr), "0"); |
| 85 | + if (iv == 1) |
| 86 | + snprintf(dow, sizeof(dow), "*"); |
| 87 | + else |
| 88 | + snprintf(dow, sizeof(dow), "*/%d", iv); |
| 89 | + } else if (strstr(freq, "monthly")) { |
| 90 | + snprintf(min, sizeof(min), "0"); |
| 91 | + snprintf(hr, sizeof(hr), "0"); |
| 92 | + snprintf(dom, sizeof(dom), "1"); |
| 93 | + if (iv > 1) |
| 94 | + snprintf(mon, sizeof(mon), "*/%d", iv); |
| 95 | + } |
| 96 | + |
| 97 | + /* byminute: override minute field with explicit list */ |
| 98 | + first = 1; |
| 99 | + LYX_LIST_FOR_EACH(lyd_child(recurrence), node, "byminute") { |
| 100 | + const char *val = lyd_get_value(node); |
| 101 | + if (!val) continue; |
| 102 | + if (first) { snprintf(min, sizeof(min), "%s", val); first = 0; } |
| 103 | + else strncat(min, ",", sizeof(min) - strlen(min) - 1), |
| 104 | + strncat(min, val, sizeof(min) - strlen(min) - 1); |
| 105 | + } |
| 106 | + |
| 107 | + /* byhour: override hour field with explicit list */ |
| 108 | + first = 1; |
| 109 | + LYX_LIST_FOR_EACH(lyd_child(recurrence), node, "byhour") { |
| 110 | + const char *val = lyd_get_value(node); |
| 111 | + if (!val) continue; |
| 112 | + if (first) { snprintf(hr, sizeof(hr), "%s", val); first = 0; } |
| 113 | + else strncat(hr, ",", sizeof(hr) - strlen(hr) - 1), |
| 114 | + strncat(hr, val, sizeof(hr) - strlen(hr) - 1); |
| 115 | + } |
| 116 | + |
| 117 | + /* bymonthday: override day-of-month field */ |
| 118 | + first = 1; |
| 119 | + LYX_LIST_FOR_EACH(lyd_child(recurrence), node, "bymonthday") { |
| 120 | + const char *val = lyd_get_value(node); |
| 121 | + if (!val) continue; |
| 122 | + if (first) { snprintf(dom, sizeof(dom), "%s", val); first = 0; } |
| 123 | + else strncat(dom, ",", sizeof(dom) - strlen(dom) - 1), |
| 124 | + strncat(dom, val, sizeof(dom) - strlen(dom) - 1); |
| 125 | + } |
| 126 | + |
| 127 | + /* byyearmonth: override month field */ |
| 128 | + first = 1; |
| 129 | + LYX_LIST_FOR_EACH(lyd_child(recurrence), node, "byyearmonth") { |
| 130 | + const char *val = lyd_get_value(node); |
| 131 | + if (!val) continue; |
| 132 | + if (first) { snprintf(mon, sizeof(mon), "%s", val); first = 0; } |
| 133 | + else strncat(mon, ",", sizeof(mon) - strlen(mon) - 1), |
| 134 | + strncat(mon, val, sizeof(mon) - strlen(mon) - 1); |
| 135 | + } |
| 136 | + |
| 137 | + /* byday: override day-of-week field */ |
| 138 | + first = 1; |
| 139 | + LYX_LIST_FOR_EACH(lyd_child(recurrence), node, "byday") { |
| 140 | + const char *val = lydx_get_cattr(node, "weekday"); |
| 141 | + const char *num = NULL; |
| 142 | + if (!val) continue; |
| 143 | + /* map YANG weekday names to cron numbers (0=sunday) */ |
| 144 | + if (!strcmp(val, "sunday")) num = "0"; |
| 145 | + else if (!strcmp(val, "monday")) num = "1"; |
| 146 | + else if (!strcmp(val, "tuesday")) num = "2"; |
| 147 | + else if (!strcmp(val, "wednesday")) num = "3"; |
| 148 | + else if (!strcmp(val, "thursday")) num = "4"; |
| 149 | + else if (!strcmp(val, "friday")) num = "5"; |
| 150 | + else if (!strcmp(val, "saturday")) num = "6"; |
| 151 | + if (!num) continue; |
| 152 | + if (first) { snprintf(dow, sizeof(dow), "%s", num); first = 0; } |
| 153 | + else strncat(dow, ",", sizeof(dow) - strlen(dow) - 1), |
| 154 | + strncat(dow, num, sizeof(dow) - strlen(dow) - 1); |
| 155 | + } |
| 156 | + |
| 157 | +done: |
| 158 | + snprintf(expr, sz, "%s %s %s %s %s", min, hr, dom, mon, dow); |
| 159 | +} |
| 160 | + |
| 161 | +static void reload_crond(void) |
| 162 | +{ |
| 163 | + char *args[] = { "pkill", "-HUP", "crond", NULL }; |
| 164 | + |
| 165 | + runbg(args, 0); |
| 166 | +} |
| 167 | + |
| 168 | +static void apply_schedules(struct lyd_node *config) |
| 169 | +{ |
| 170 | + struct lyd_node *schedules, *sched; |
| 171 | + FILE *fp; |
| 172 | + int count = 0; |
| 173 | + |
| 174 | + makepath("/var/spool/cron/crontabs"); |
| 175 | + fp = fopen(CRONTAB_FILE, "w"); |
| 176 | + if (!fp) { |
| 177 | + ERROR("schedule: failed to open %s", CRONTAB_FILE); |
| 178 | + return; |
| 179 | + } |
| 180 | + fprintf(fp, "# Managed by infix-schedule\n"); |
| 181 | + |
| 182 | + schedules = config ? lydx_get_xpathf(config, XPATH_BASE) : NULL; |
| 183 | + if (!schedules) |
| 184 | + goto out; |
| 185 | + |
| 186 | + LYX_LIST_FOR_EACH(lyd_child(schedules), sched, "schedule") { |
| 187 | + struct lyd_node *recurrence; |
| 188 | + const char *name, *action, *cmd; |
| 189 | + char expr[128]; |
| 190 | + |
| 191 | + if (!lydx_is_enabled(sched, "enabled")) |
| 192 | + continue; |
| 193 | + |
| 194 | + name = lydx_get_cattr(sched, "name"); |
| 195 | + |
| 196 | + recurrence = lydx_get_child(sched, "recurrence"); |
| 197 | + if (!recurrence) |
| 198 | + continue; |
| 199 | + |
| 200 | + build_cron_expr(recurrence, expr, sizeof(expr)); |
| 201 | + |
| 202 | + action = lydx_get_cattr(sched, "predefined-action"); |
| 203 | + cmd = action_to_cmd(action); |
| 204 | + if (!cmd) |
| 205 | + continue; |
| 206 | + |
| 207 | + fprintf(fp, "# %s\n%s %s\n", name ?: "unnamed", expr, cmd); |
| 208 | + NOTE("schedule: %s → cron '%s %s'", name ?: "unnamed", expr, cmd); |
| 209 | + count++; |
| 210 | + } |
| 211 | + |
| 212 | +out: |
| 213 | + fclose(fp); |
| 214 | + reload_crond(); |
| 215 | + NOTE("schedule: %d active job(s) written to crontab", count); |
| 216 | +} |
| 217 | + |
| 218 | +int schedule_change(sr_session_ctx_t *session, struct lyd_node *config, |
| 219 | + struct lyd_node *diff, sr_event_t event, struct confd *confd) |
| 220 | +{ |
| 221 | + if (event != SR_EV_DONE) |
| 222 | + return SR_ERR_OK; |
| 223 | + |
| 224 | + apply_schedules(config); |
| 225 | + return SR_ERR_OK; |
| 226 | +} |
| 227 | + |
| 228 | +int schedule_init(struct confd *confd) |
| 229 | +{ |
| 230 | + sr_data_t *data = NULL; |
| 231 | + |
| 232 | + /* |
| 233 | + * Sync the crontab with current running config at startup so |
| 234 | + * scheduled jobs survive across reboots. |
| 235 | + */ |
| 236 | + sr_get_data(confd->session, "//.", 0, 0, 0, &data); |
| 237 | + apply_schedules(data ? data->tree : NULL); |
| 238 | + if (data) |
| 239 | + sr_release_data(data); |
| 240 | + |
| 241 | + return SR_ERR_OK; |
| 242 | +} |
0 commit comments