Changeset 1765 in MondoRescue for branches/2.2.5/mindi-busybox/miscutils/crond.c
- Timestamp:
- Nov 4, 2007, 3:16:40 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2.2.5/mindi-busybox/miscutils/crond.c
r821 r1765 6 6 * 7 7 * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com) 8 * (version 2.3.2) 8 9 * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002 9 10 * … … 11 12 */ 12 13 13 #define VERSION "2.3.2" 14 15 #include "busybox.h" 16 #include <stdio.h> 17 #include <stdlib.h> 18 #include <stdarg.h> 19 #include <string.h> 20 #include <errno.h> 21 #include <time.h> 22 #include <dirent.h> 23 #include <fcntl.h> 24 #include <unistd.h> 25 #include <syslog.h> 26 #include <signal.h> 27 #include <getopt.h> 28 #include <sys/ioctl.h> 29 #include <sys/wait.h> 30 #include <sys/stat.h> 31 #include <sys/resource.h> 32 33 #define arysize(ary) (sizeof(ary)/sizeof((ary)[0])) 14 #include <sys/syslog.h> 15 #include "libbb.h" 34 16 35 17 #ifndef CRONTABS … … 40 22 #endif 41 23 #ifndef SENDMAIL 42 #define SENDMAIL " /usr/sbin/sendmail"24 #define SENDMAIL "sendmail" 43 25 #endif 44 26 #ifndef SENDMAIL_ARGS … … 81 63 82 64 #if ENABLE_DEBUG_CROND_OPTION 83 static shortDebugOpt;84 #endif 85 86 static shortLogLevel = 8;65 static unsigned DebugOpt; 66 #endif 67 68 static unsigned LogLevel = 8; 87 69 static const char *LogFile; 88 70 static const char *CDir = CRONTABS; … … 130 112 vsyslog(type, fmt, va); 131 113 } else { 114 #if !ENABLE_DEBUG_CROND_OPTION 132 115 int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600); 116 #else 117 int logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600); 118 #endif 133 119 if (logfd >= 0) { 134 120 vdprintf(logfd, fmt, va); 135 121 close(logfd); 136 #if ENABLE_DEBUG_CROND_OPTION137 } else {138 bb_perror_msg("Can't open log file");139 #endif140 122 } 141 123 } … … 147 129 } 148 130 131 int crond_main(int ac, char **av); 149 132 int crond_main(int ac, char **av) 150 133 { 151 unsigned longopt;134 unsigned opt; 152 135 char *lopt, *Lopt, *copt; 153 154 #if ENABLE_DEBUG_CROND_OPTION 155 char *dopt; 156 157 bb_opt_complementally = "f-b:b-f:S-L:L-S:d-l"; 158 #else 159 bb_opt_complementally = "f-b:b-f:S-L:L-S"; 160 #endif 161 136 USE_DEBUG_CROND_OPTION(char *dopt;) 137 138 opt_complementary = "f-b:b-f:S-L:L-S" USE_DEBUG_CROND_OPTION(":d-l"); 162 139 opterr = 0; /* disable getopt 'errors' message. */ 163 opt = bb_getopt_ulflags(ac, av, "l:L:fbSc:" 164 #if ENABLE_DEBUG_CROND_OPTION 165 "d:" 166 #endif 167 , &lopt, &Lopt, &copt 168 #if ENABLE_DEBUG_CROND_OPTION 169 , &dopt 170 #endif 171 ); 172 if (opt & 1) { 173 LogLevel = atoi(lopt); 174 } 175 if (opt & 2) { 176 if (*Lopt != 0) { 140 opt = getopt32(av, "l:L:fbSc:" USE_DEBUG_CROND_OPTION("d:"), 141 &lopt, &Lopt, &copt USE_DEBUG_CROND_OPTION(, &dopt)); 142 if (opt & 1) /* -l */ 143 LogLevel = xatou(lopt); 144 if (opt & 2) /* -L */ 145 if (*Lopt) 177 146 LogFile = Lopt; 178 } 179 } 180 if (opt & 32) { 181 if (*copt != 0) { 147 if (opt & 32) /* -c */ 148 if (*copt) 182 149 CDir = copt; 183 } 184 } 185 #if ENABLE_DEBUG_CROND_OPTION 186 if (opt & 64) { 187 DebugOpt = atoi(dopt); 150 #if ENABLE_DEBUG_CROND_OPTION 151 if (opt & 64) { /* -d */ 152 DebugOpt = xatou(dopt); 188 153 LogLevel = 0; 189 154 } 190 155 #endif 191 156 192 /* 193 * change directory 194 */ 195 196 bb_xchdir(CDir); 197 signal(SIGHUP, SIG_IGN); /* hmm.. but, if kill -HUP original 198 * version - his died. ;( 199 */ 200 /* 201 * close stdin and stdout, stderr. 157 /* close stdin and stdout, stderr. 202 158 * close unused descriptors - don't need. 203 159 * optional detach from controlling terminal 204 160 */ 205 206 if (!(opt & 4)) { 207 #ifdef BB_NOMMU 208 /* reexec for vfork() do continue parent */ 209 vfork_daemon_rexec(1, 0, ac, av, "-f"); 210 #else 211 bb_xdaemon(1, 0); 212 #endif 213 } 214 215 (void) startlogger(); /* need if syslog mode selected */ 161 if (!(opt & 4)) 162 bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, av); 163 164 xchdir(CDir); 165 signal(SIGHUP, SIG_IGN); /* ? original crond dies on HUP... */ 166 167 startlogger(); /* need if syslog mode selected */ 216 168 217 169 /* … … 219 171 * of 1 second. 220 172 */ 221 222 crondlog("\011%s " VERSION " dillon, started, log level %d\n", 223 bb_applet_name, LogLevel); 173 crondlog("\011%s " BB_VER " started, log level %d\n", 174 applet_name, LogLevel); 224 175 225 176 SynchronizeDir(); … … 232 183 short sleep_time = 60; 233 184 185 write_pidfile("/var/run/crond.pid"); 234 186 for (;;) { 235 187 sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time)); … … 280 232 } 281 233 } 282 bb_fflush_stdout_and_exit(EXIT_SUCCESS); /* not reached */234 return 0; /* not reached */ 283 235 } 284 236 … … 294 246 if (pas == 0) { 295 247 crondlog("\011failed to get uid for %s", user); 296 return (-1);248 return -1; 297 249 } 298 250 setenv("USER", pas->pw_name, 1); … … 306 258 if (err_msg) { 307 259 crondlog("\011%s for user %s", err_msg, user); 308 return (-1);260 return -1; 309 261 } 310 262 if (chdir(pas->pw_dir) < 0) { … … 312 264 if (chdir(TMPDIR) < 0) { 313 265 crondlog("\011chdir failed: %s: %m", TMPDIR); 314 return (-1);315 } 316 } 317 return (pas->pw_uid);266 return -1; 267 } 268 } 269 return pas->pw_uid; 318 270 } 319 271 … … 321 273 { 322 274 if (LogFile == 0) { 323 openlog( bb_applet_name, LOG_CONS | LOG_PID, LOG_CRON);275 openlog(applet_name, LOG_CONS | LOG_PID, LOG_CRON); 324 276 } 325 277 #if ENABLE_DEBUG_CROND_OPTION … … 327 279 int logfd; 328 280 329 if ((logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600)) >= 0) { 281 logfd = open3_or_warn(LogFile, O_WRONLY | O_CREAT | O_APPEND, 0600); 282 if (logfd >= 0) { 330 283 close(logfd); 331 } else { 332 bb_perror_msg("Failed to open log file '%s' reason", LogFile); 333 } 334 } 335 #endif 336 } 337 338 339 static const char *const DowAry[] = { 340 "sun", 341 "mon", 342 "tue", 343 "wed", 344 "thu", 345 "fri", 346 "sat", 347 348 "Sun", 349 "Mon", 350 "Tue", 351 "Wed", 352 "Thu", 353 "Fri", 354 "Sat", 355 NULL 356 }; 357 358 static const char *const MonAry[] = { 359 "jan", 360 "feb", 361 "mar", 362 "apr", 363 "may", 364 "jun", 365 "jul", 366 "aug", 367 "sep", 368 "oct", 369 "nov", 370 "dec", 371 372 "Jan", 373 "Feb", 374 "Mar", 375 "Apr", 376 "May", 377 "Jun", 378 "Jul", 379 "Aug", 380 "Sep", 381 "Oct", 382 "Nov", 383 "Dec", 384 NULL 385 }; 284 } 285 } 286 #endif 287 } 288 289 290 static const char DowAry[] ALIGN1 = 291 "sun""mon""tue""wed""thu""fri""sat" 292 /* "Sun""Mon""Tue""Wed""Thu""Fri""Sat" */ 293 ; 294 295 static const char MonAry[] ALIGN1 = 296 "jan""feb""mar""apr""may""jun""jul""aug""sep""oct""nov""dec" 297 /* "Jan""Feb""Mar""Apr""May""Jun""Jul""Aug""Sep""Oct""Nov""Dec" */ 298 ; 386 299 387 300 static char *ParseField(char *user, char *ary, int modvalue, int off, 388 const char *const *names, char *ptr) 301 const char *names, char *ptr) 302 /* 'names' is a pointer to a set of 3-char abbreviations */ 389 303 { 390 304 char *base = ptr; … … 393 307 394 308 if (base == NULL) { 395 return (NULL);309 return NULL; 396 310 } 397 311 … … 416 330 int i; 417 331 418 for (i = 0; names[i]; ++i) { 419 if (strncmp(ptr, names[i], strlen(names[i])) == 0) { 332 for (i = 0; names[i]; i += 3) { 333 /* was using strncmp before... */ 334 if (strncasecmp(ptr, &names[i], 3) == 0) { 335 ptr += 3; 336 if (n1 < 0) { 337 n1 = i / 3; 338 } else { 339 n2 = i / 3; 340 } 341 skip = 1; 420 342 break; 421 343 } 422 }423 if (names[i]) {424 ptr += strlen(names[i]);425 if (n1 < 0) {426 n1 = i;427 } else {428 n2 = i;429 }430 skip = 1;431 344 } 432 345 } … … 436 349 if (skip == 0) { 437 350 crondlog("\111failed user %s parsing %s\n", user, base); 438 return (NULL);351 return NULL; 439 352 } 440 353 if (*ptr == '-' && n2 < 0) { … … 476 389 if (failsafe == 0) { 477 390 crondlog("\111failed user %s parsing %s\n", user, base); 478 return (NULL);391 return NULL; 479 392 } 480 393 } … … 489 402 if (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') { 490 403 crondlog("\111failed user %s parsing %s\n", user, base); 491 return (NULL);404 return NULL; 492 405 } 493 406 … … 506 419 #endif 507 420 508 return (ptr);421 return ptr; 509 422 } 510 423 … … 515 428 int daysUsed = 0; 516 429 517 for (i = 0; i < (int)( arysize(line->cl_Dow)); ++i) {430 for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Dow)); ++i) { 518 431 if (line->cl_Dow[i] == 0) { 519 432 weekUsed = 1; … … 521 434 } 522 435 } 523 for (i = 0; i < (int)( arysize(line->cl_Days)); ++i) {436 for (i = 0; i < (int)(ARRAY_SIZE(line->cl_Days)); ++i) { 524 437 if (line->cl_Days[i] == 0) { 525 438 daysUsed = 1; … … 558 471 559 472 if (fstat(fileno(fi), &sbuf) == 0 && sbuf.st_uid == DaemonUid) { 560 CronFile *file = calloc(1,sizeof(CronFile));473 CronFile *file = xzalloc(sizeof(CronFile)); 561 474 CronLine **pline; 562 475 … … 602 515 FixDayDow(&line); 603 516 604 *pline = calloc(1,sizeof(CronLine));517 *pline = xzalloc(sizeof(CronLine)); 605 518 **pline = line; 606 519 … … 670 583 remove(CRONUPDATE); 671 584 if (chdir(CDir) < 0) { 672 crondlog("\311 unable tofind %s\n", CDir);585 crondlog("\311cannot find %s\n", CDir); 673 586 } 674 587 { … … 689 602 closedir(dir); 690 603 } else { 691 crondlog("\311 Unable toopen current dir!\n");604 crondlog("\311cannot open current dir!\n"); 692 605 } 693 606 } … … 793 706 } 794 707 } 795 return (nJobs);708 return nJobs; 796 709 } 797 710 … … 859 772 nStillRunning += file->cf_Running; 860 773 } 861 return (nStillRunning);774 return nStillRunning; 862 775 } 863 776 … … 892 805 } 893 806 execl(prog, prog, cmd, arg, NULL); 894 crondlog("\024 unable toexec, user %s cmd %s %s %s\n", user, prog, cmd, arg);807 crondlog("\024cannot exec, user %s cmd %s %s %s\n", user, prog, cmd, arg); 895 808 if (mailf) { 896 809 fdprintf(1, "Exec failed: %s -c %s\n", prog, arg); … … 899 812 } else if (pid < 0) { 900 813 /* FORK FAILED */ 901 crondlog("\024c ouldn't fork, user %s\n", user);814 crondlog("\024cannot fork, user %s\n", user); 902 815 line->cl_Pid = 0; 903 816 if (mailf) { … … 940 853 fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", user, 941 854 line->cl_Shell); 942 line->cl_MailPos = lseek(mailFd, 0, 1);855 line->cl_MailPos = lseek(mailFd, 0, SEEK_CUR); 943 856 } else { 944 crondlog("\024 unable tocreate mail file user %s file %s, output to /dev/null\n", user, mailFile);857 crondlog("\024cannot create mail file user %s file %s, output to /dev/null\n", user, mailFile); 945 858 } 946 859 … … 989 902 } 990 903 991 if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid || sbuf.st_nlink != 0 || 992 sbuf.st_size == line->cl_MailPos || !S_ISREG(sbuf.st_mode)) { 904 if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid 905 || sbuf.st_nlink != 0 || sbuf.st_size == line->cl_MailPos 906 || !S_ISREG(sbuf.st_mode) 907 ) { 993 908 close(mailFd); 994 909 return; … … 1019 934 1020 935 execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL); 1021 crondlog("\024 unable toexec, user %s cmd %s -c %s\n", user,936 crondlog("\024cannot exec, user %s cmd %s -c %s\n", user, 1022 937 DEFAULT_SHELL, line->cl_Shell); 1023 938 exit(0); 1024 939 } else if (pid < 0) { 1025 940 /* FORK FAILED */ 1026 crondlog("\024c ouldn't fork, user %s\n", user);941 crondlog("\024cannot, user %s\n", user); 1027 942 pid = 0; 1028 943 }
Note:
See TracChangeset
for help on using the changeset viewer.