2 * FCRON - periodic command scheduler
4 * Copyright 2000-2013 Thibault Godouet <fcron@free.fr>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * The GNU General Public License can also be found in the file
21 * `LICENSE' that comes with the fcron source distribution.
28 extern char debug_opt;
30 int write_buf_to_disk(int fd, char *write_buf, int *buf_used);
31 int save_type(int fd, short int type, char *write_buf, int *buf_used);
32 int save_str(int fd, short int type, char *str, char *write_buf, int *buf_used);
33 int save_strn(int fd, short int type, char *str, short int size,
34 char *write_buf, int *buf_used);
35 int save_lint(int fd, short int type, long int value, char *write_buf,
37 int save_one_file(cf_t * file, char *filename, uid_t own_uid, gid_t own_gid,
42 save_type(int fd, short int type, char *write_buf, int *buf_used)
43 /* save a single type (with no data attached) in a binary fcrontab file */
46 int write_len = sizeof(type) + sizeof(size);
48 if (write_len > WRITE_BUF_LEN - *buf_used)
49 if (write_buf_to_disk(fd, write_buf, buf_used) == ERR)
52 memcpy((write_buf + *buf_used), &type, sizeof(type));
53 *buf_used += sizeof(type);
54 memcpy((write_buf + *buf_used), &size, sizeof(size));
55 *buf_used += sizeof(size);
62 save_str(int fd, short int type, char *str, char *write_buf, int *buf_used)
63 /* save a string of type "type" in a binary fcrontab file */
65 short int size = strlen(str);
66 int write_len = sizeof(type) + sizeof(size) + size;
68 if (write_len > WRITE_BUF_LEN - *buf_used)
69 if (write_buf_to_disk(fd, write_buf, buf_used) == ERR)
72 memcpy((write_buf + *buf_used), &type, sizeof(type));
73 *buf_used += sizeof(type);
74 memcpy((write_buf + *buf_used), &size, sizeof(size));
75 *buf_used += sizeof(size);
76 memcpy((write_buf + *buf_used), str, size);
83 save_strn(int fd, short int type, char *str, short int size, char *write_buf,
85 /* save a "size"-length string of type "type" in a binary fcrontab file */
87 int write_len = sizeof(type) + sizeof(size) + size;
89 if (write_len > WRITE_BUF_LEN - *buf_used)
90 if (write_buf_to_disk(fd, write_buf, buf_used) == ERR)
93 memcpy((write_buf + *buf_used), &type, sizeof(type));
94 *buf_used += sizeof(type);
95 memcpy((write_buf + *buf_used), &size, sizeof(size));
96 *buf_used += sizeof(size);
97 memcpy((write_buf + *buf_used), str, size);
104 save_lint(int fd, short int type, long int value, char *write_buf,
106 /* save an integer of type "type" in a binary fcrontab file */
108 short int size = sizeof(value);
109 int write_len = sizeof(type) + sizeof(size) + size;
111 if (write_len > WRITE_BUF_LEN - *buf_used)
112 if (write_buf_to_disk(fd, write_buf, buf_used) == ERR)
115 memcpy((write_buf + *buf_used), &type, sizeof(type));
116 *buf_used += sizeof(type);
117 memcpy((write_buf + *buf_used), &size, sizeof(size));
118 *buf_used += sizeof(size);
119 memcpy((write_buf + *buf_used), &value, size);
127 write_buf_to_disk(int fd, char *write_buf, int *buf_used)
128 /* write the buffer to disk */
130 ssize_t to_write = *buf_used;
135 while (written < to_write) {
136 if (num_retries++ > (int)(to_write / 2)) {
137 error("too many retries (%d) to write buf to disk : giving up.",
141 return_val = write(fd, (write_buf + written), to_write - written);
142 if (return_val == -1) {
143 error_e("could not write() buf to disk");
146 written += return_val;
150 debug("write_buf_to_disk() : written %d/%d, %d (re)try(ies)", written,
151 to_write, num_retries);
154 if (written == to_write) {
159 error("write_buf_to_disk() : written %d bytes for %d requested.",
166 /* write_file_to_disk() error management */
167 #define Save_type(FD, TYPE, BUF, BUF_USED) \
169 if ( save_type(FD, TYPE, BUF, BUF_USED) != OK ) { \
170 error_e("Could not write type : file %s has not been saved.", \
176 #define Save_str(FD, TYPE, STR, BUF, BUF_USED) \
178 if ( save_str(FD, TYPE, STR, BUF, BUF_USED) != OK ) { \
179 error_e("Could not write str : file %s has not been saved.", \
185 #define Save_strn(FD, TYPE, STR, SIZE, BUF, BUF_USED) \
187 if ( save_strn(FD, TYPE, STR, SIZE, BUF, BUF_USED) != OK ) { \
188 error_e("Could not write strn : file %s has not been saved.", \
194 #define Save_lint(FD, TYPE, VALUE, BUF, BUF_USED) \
196 if ( save_lint(FD, TYPE, VALUE, BUF, BUF_USED) != OK ) { \
197 error_e("Could not write lint : file %s has not been saved.", \
204 write_file_to_disk(int fd, struct cf_t *file, time_t time_date)
205 /* write the data on the disk */
209 char write_buf[WRITE_BUF_LEN];
210 int write_buf_used = 0;
212 /* put program's version : it permits to daemon not to load
213 * a file which he won't understand the syntax, for exemple
214 * a file using a depreciated format generated by an old fcrontab,
215 * if the syntax has changed */
216 /* an binary fcrontab *must* start by such a header */
217 Save_lint(fd, S_HEADER_T, S_FILEVERSION, write_buf, &write_buf_used);
219 /* put the user's name : needed to check if his uid has not changed */
220 /* S_USER_T *must* be the 2nd field of a binary fcrontab */
221 Save_str(fd, S_USER_T, file->cf_user, write_buf, &write_buf_used);
223 /* put the time & date of saving : this is use for calcutating
224 * the system down time. As it is a new file, we set it to 0 */
225 /* S_USER_T *must* be the 3rd field of a binary fcrontab */
226 Save_lint(fd, S_TIMEDATE_T, time_date, write_buf, &write_buf_used);
228 /* Save the time diff between local (real) and system hour (if any) */
229 if (file->cf_tzdiff != 0)
230 Save_lint(fd, S_TZDIFF_T, file->cf_tzdiff, write_buf, &write_buf_used);
233 for (env = env_list_first(file->cf_env_list);
234 env != NULL; env = env_list_next(file->cf_env_list)) {
235 Save_str(fd, S_ENVVAR_T, env->e_envvar, write_buf, &write_buf_used);
239 for (line = file->cf_line_base; line; line = line->cl_next) {
241 /* this ones are saved for every lines */
242 Save_str(fd, S_SHELL_T, line->cl_shell, write_buf, &write_buf_used);
243 Save_str(fd, S_RUNAS_T, line->cl_runas, write_buf, &write_buf_used);
244 Save_str(fd, S_MAILTO_T, line->cl_mailto, write_buf, &write_buf_used);
245 Save_strn(fd, S_OPTION_T, (char *)line->cl_option, OPTION_SIZE,
246 write_buf, &write_buf_used);
247 Save_lint(fd, S_NEXTEXE_T, line->cl_nextexe, write_buf,
250 /* the following are saved only if needed */
252 Save_strn(fd, S_NUMEXE_T, (char *)&line->cl_numexe, 1, write_buf,
254 if (is_lavg(line->cl_option))
255 Save_strn(fd, S_LAVG_T, (char *)line->cl_lavg, LAVG_SIZE,
256 write_buf, &write_buf_used);
257 if (line->cl_until > 0)
258 Save_lint(fd, S_UNTIL_T, line->cl_until, write_buf,
260 if (line->cl_nice != 0)
261 Save_strn(fd, S_NICE_T, &line->cl_nice, 1, write_buf,
263 if (line->cl_runfreq > 0) {
264 Save_lint(fd, S_RUNFREQ_T, line->cl_runfreq, write_buf,
266 Save_lint(fd, S_REMAIN_T, line->cl_remain, write_buf,
269 if (line->cl_tz != NULL) {
270 Save_str(fd, S_TZ_T, line->cl_tz, write_buf, &write_buf_used);
272 if (line->cl_jitter > 0) {
273 Save_lint(fd, S_JITTER_T, line->cl_jitter, write_buf,
277 if (is_freq(line->cl_option)) {
278 /* save the frequency to run the line */
279 Save_lint(fd, S_FIRST_T, line->cl_first, write_buf,
281 Save_lint(fd, S_TIMEFREQ_T, line->cl_timefreq, write_buf,
285 /* save the time and date bit fields */
286 Save_strn(fd, S_MINS_T, (char *)line->cl_mins, bitstr_size(60),
287 write_buf, &write_buf_used);
288 Save_strn(fd, S_HRS_T, (char *)line->cl_hrs, bitstr_size(24),
289 write_buf, &write_buf_used);
290 Save_strn(fd, S_DAYS_T, (char *)line->cl_days, bitstr_size(32),
291 write_buf, &write_buf_used);
292 Save_strn(fd, S_MONS_T, (char *)line->cl_mons, bitstr_size(12),
293 write_buf, &write_buf_used);
294 Save_strn(fd, S_DOW_T, (char *)line->cl_dow, bitstr_size(8),
295 write_buf, &write_buf_used);
298 /* This field *must* be the last of each line */
299 Save_type(fd, S_ENDLINE_T, write_buf, &write_buf_used);
302 if (write_buf_to_disk(fd, write_buf, &write_buf_used) == ERR) {
304 ("Could not write final buffer content to disk: file %s has not been saved.",
314 save_one_file(cf_t * file, char *filename, uid_t own_uid, gid_t own_gid,
316 /* save a given file to disk */
322 if (is_selinux_enabled() && setfscreatecon(file->cf_file_context)) {
323 error_e("Could not set create context for file %s", filename);
327 fd = open_as_user(filename, own_uid, own_gid,
328 O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, S_IRUSR | S_IWUSR);
330 if (is_selinux_enabled())
331 setfscreatecon(NULL);
334 error_e("Could not open %s", filename);
338 if (fchown(fd, own_uid, own_gid) != 0) {
339 error_e("Could not fchown %s to uid:%d gid:%d", filename, own_uid,
342 error_e("save_one_file(%s): could not xclose(fd)", filename);
343 remove_as_user(filename, own_uid, own_gid);
348 if (write_file_to_disk(fd, file, save_date) == ERR) {
350 error_e("save_one_file(%s): could not xclose(fd)", filename);
351 remove_as_user(filename, own_uid, own_gid);
356 error_e("save_one_file(%s): could not xclose(fd)", filename);
363 save_file_safe(cf_t * file, char *final_path, char *prog_name, uid_t own_uid,
364 gid_t own_gid, time_t save_date)
365 /* save a file to a temp path, and then rename it (safely) to avoid loss of data
366 * if a system crash, hardware failure, etc happens. */
368 char temp_path[PATH_LEN + 4];
369 int final_path_len, temp_path_index;
370 char *tmp_str = ".tmp";
372 final_path_len = strlen(final_path);
373 strncpy(temp_path, final_path, sizeof(temp_path) - sizeof(tmp_str));
374 temp_path_index = (final_path_len > sizeof(temp_path) - sizeof(tmp_str)) ?
375 sizeof(temp_path) - sizeof(tmp_str) : final_path_len;
376 strcpy(&temp_path[temp_path_index], tmp_str);
378 if (save_one_file(file, temp_path, own_uid, own_gid, save_date) == OK) {
379 if (rename_as_user(temp_path, final_path, own_uid, own_gid) != 0) {
380 error_e("Cannot rename %s to %s", temp_path, final_path);
381 error("%s will try to save the name to its definitive filename "
382 "directly.", prog_name);
384 ("If there is an error, root may consider to replace %s (which is "
385 "a valid copy) by %s manually.", final_path, temp_path);
386 if (save_one_file(file, final_path, own_uid, own_gid, save_date) ==
392 error("Since %s has not been able to save %s's file, it will keep "
393 "the previous version (if any) of %s.", prog_name, final_path,