3
0

crond.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  1. /*
  2. * crond -d[#] -c <crondir> -f -b
  3. *
  4. * run as root, but NOT setuid root
  5. *
  6. * Copyright 1994 Matthew Dillon (dillon@apollo.west.oic.com)
  7. * May be distributed under the GNU General Public License
  8. *
  9. * Vladimir Oleynik <dzo@simtreas.ru> (C) 2002 to be used in busybox
  10. */
  11. #define VERSION "2.3.2"
  12. #undef FEATURE_DEBUG_OPT
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdarg.h>
  16. #include <string.h>
  17. #include <errno.h>
  18. #include <time.h>
  19. #include <dirent.h>
  20. #include <fcntl.h>
  21. #include <unistd.h>
  22. #include <syslog.h>
  23. #include <signal.h>
  24. #include <getopt.h>
  25. #include <sys/ioctl.h>
  26. #include <sys/wait.h>
  27. #include <sys/stat.h>
  28. #include <sys/resource.h>
  29. #include "busybox.h"
  30. #define arysize(ary) (sizeof(ary)/sizeof((ary)[0]))
  31. #ifndef CRONTABS
  32. #define CRONTABS "/var/spool/cron/crontabs"
  33. #endif
  34. #ifndef TMPDIR
  35. #define TMPDIR "/var/spool/cron"
  36. #endif
  37. #ifndef SENDMAIL
  38. #define SENDMAIL "/usr/sbin/sendmail"
  39. #endif
  40. #ifndef SENDMAIL_ARGS
  41. #define SENDMAIL_ARGS "-ti", "oem"
  42. #endif
  43. #ifndef CRONUPDATE
  44. #define CRONUPDATE "cron.update"
  45. #endif
  46. #ifndef MAXLINES
  47. #define MAXLINES 256 /* max lines in non-root crontabs */
  48. #endif
  49. typedef struct CronFile {
  50. struct CronFile *cf_Next;
  51. struct CronLine *cf_LineBase;
  52. char *cf_User; /* username */
  53. int cf_Ready; /* bool: one or more jobs ready */
  54. int cf_Running; /* bool: one or more jobs running */
  55. int cf_Deleted; /* marked for deletion, ignore */
  56. } CronFile;
  57. typedef struct CronLine {
  58. struct CronLine *cl_Next;
  59. char *cl_Shell; /* shell command */
  60. pid_t cl_Pid; /* running pid, 0, or armed (-1) */
  61. int cl_MailFlag; /* running pid is for mail */
  62. int cl_MailPos; /* 'empty file' size */
  63. char cl_Mins[60]; /* 0-59 */
  64. char cl_Hrs[24]; /* 0-23 */
  65. char cl_Days[32]; /* 1-31 */
  66. char cl_Mons[12]; /* 0-11 */
  67. char cl_Dow[7]; /* 0-6, beginning sunday */
  68. } CronLine;
  69. #define RUN_RANOUT 1
  70. #define RUN_RUNNING 2
  71. #define RUN_FAILED 3
  72. #define DaemonUid 0
  73. #ifdef FEATURE_DEBUG_OPT
  74. static short DebugOpt;
  75. #endif
  76. static short LogLevel = 8;
  77. static const char *LogFile;
  78. static const char *CDir = CRONTABS;
  79. static void startlogger(void);
  80. static void CheckUpdates(void);
  81. static void SynchronizeDir(void);
  82. static int TestJobs(time_t t1, time_t t2);
  83. static void RunJobs(void);
  84. static int CheckJobs(void);
  85. static void RunJob(const char *user, CronLine * line);
  86. #ifdef CONFIG_FEATURE_CROND_CALL_SENDMAIL
  87. static void EndJob(const char *user, CronLine * line);
  88. #else
  89. #define EndJob(user, line) line->cl_Pid = 0
  90. #endif
  91. static void DeleteFile(const char *userName);
  92. static CronFile *FileBase;
  93. static void crondlog(const char *ctl, ...)
  94. {
  95. va_list va;
  96. const char *fmt;
  97. int level = (int) (ctl[0] & 0xf);
  98. int type = level == 20 ?
  99. LOG_ERR : ((ctl[0] & 0100) ? LOG_WARNING : LOG_NOTICE);
  100. va_start(va, ctl);
  101. fmt = ctl + 1;
  102. if (level >= LogLevel) {
  103. #ifdef FEATURE_DEBUG_OPT
  104. if (DebugOpt) {
  105. vfprintf(stderr, fmt, va);
  106. } else
  107. #endif
  108. if (LogFile == 0) {
  109. vsyslog(type, fmt, va);
  110. } else {
  111. int logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 600);
  112. if (logfd >= 0) {
  113. vdprintf(logfd, fmt, va);
  114. close(logfd);
  115. #ifdef FEATURE_DEBUG_OPT
  116. } else {
  117. bb_perror_msg("Can't open log file");
  118. #endif
  119. }
  120. }
  121. }
  122. va_end(va);
  123. if (ctl[0] & 0200) {
  124. exit(20);
  125. }
  126. }
  127. int crond_main(int ac, char **av)
  128. {
  129. unsigned long opt;
  130. char *lopt, *Lopt, *copt;
  131. #ifdef FEATURE_DEBUG_OPT
  132. char *dopt;
  133. bb_opt_complementaly = "f-b:b-f:S-L:L-S:d-l";
  134. #else
  135. bb_opt_complementaly = "f-b:b-f:S-L:L-S";
  136. #endif
  137. opterr = 0; /* disable getopt 'errors' message. */
  138. opt = bb_getopt_ulflags(ac, av, "l:L:fbSc:"
  139. #ifdef FEATURE_DEBUG_OPT
  140. "d:"
  141. #endif
  142. , &lopt, &Lopt, &copt
  143. #ifdef FEATURE_DEBUG_OPT
  144. , &dopt
  145. #endif
  146. );
  147. if (opt & 1) {
  148. LogLevel = atoi(lopt);
  149. }
  150. if (opt & 2) {
  151. if (*Lopt != 0) {
  152. LogFile = Lopt;
  153. }
  154. }
  155. if (opt & 32) {
  156. if (*copt != 0) {
  157. CDir = copt;
  158. }
  159. }
  160. #ifdef FEATURE_DEBUG_OPT
  161. if (opt & 64) {
  162. DebugOpt = atoi(dopt);
  163. LogLevel = 0;
  164. }
  165. #endif
  166. /*
  167. * change directory
  168. */
  169. if (chdir(CDir) != 0) {
  170. bb_perror_msg_and_die("%s", CDir);
  171. }
  172. signal(SIGHUP, SIG_IGN); /* hmm.. but, if kill -HUP original
  173. * version - his died. ;(
  174. */
  175. /*
  176. * close stdin and stdout, stderr.
  177. * close unused descriptors - don't need.
  178. * optional detach from controlling terminal
  179. */
  180. if (!(opt & 4)) {
  181. #if defined(__uClinux__)
  182. /* reexec for vfork() do continue parent */
  183. vfork_daemon_rexec(1, 0, ac, av, "-f");
  184. #else /* uClinux */
  185. if (daemon(1, 0) < 0) {
  186. bb_perror_msg_and_die("daemon");
  187. }
  188. #endif /* uClinux */
  189. }
  190. (void) startlogger(); /* need if syslog mode selected */
  191. /*
  192. * main loop - synchronize to 1 second after the minute, minimum sleep
  193. * of 1 second.
  194. */
  195. crondlog("\011%s " VERSION " dillon, started, log level %d\n",
  196. bb_applet_name, LogLevel);
  197. SynchronizeDir();
  198. {
  199. time_t t1 = time(NULL);
  200. time_t t2;
  201. long dt;
  202. short rescan = 60;
  203. short sleep_time = 60;
  204. for (;;) {
  205. sleep((sleep_time + 1) - (short) (time(NULL) % sleep_time));
  206. t2 = time(NULL);
  207. dt = t2 - t1;
  208. /*
  209. * The file 'cron.update' is checked to determine new cron
  210. * jobs. The directory is rescanned once an hour to deal
  211. * with any screwups.
  212. *
  213. * check for disparity. Disparities over an hour either way
  214. * result in resynchronization. A reverse-indexed disparity
  215. * less then an hour causes us to effectively sleep until we
  216. * match the original time (i.e. no re-execution of jobs that
  217. * have just been run). A forward-indexed disparity less then
  218. * an hour causes intermediate jobs to be run, but only once
  219. * in the worst case.
  220. *
  221. * when running jobs, the inequality used is greater but not
  222. * equal to t1, and less then or equal to t2.
  223. */
  224. if (--rescan == 0) {
  225. rescan = 60;
  226. SynchronizeDir();
  227. }
  228. CheckUpdates();
  229. #ifdef FEATURE_DEBUG_OPT
  230. if (DebugOpt)
  231. crondlog("\005Wakeup dt=%d\n", dt);
  232. #endif
  233. if (dt < -60 * 60 || dt > 60 * 60) {
  234. t1 = t2;
  235. crondlog("\111time disparity of %d minutes detected\n", dt / 60);
  236. } else if (dt > 0) {
  237. TestJobs(t1, t2);
  238. RunJobs();
  239. sleep(5);
  240. if (CheckJobs() > 0) {
  241. sleep_time = 10;
  242. } else {
  243. sleep_time = 60;
  244. }
  245. t1 = t2;
  246. }
  247. }
  248. }
  249. /* not reached */
  250. }
  251. #if defined(FEATURE_DEBUG_OPT) || defined(CONFIG_FEATURE_CROND_CALL_SENDMAIL)
  252. /*
  253. write to temp file..
  254. */
  255. static void fdprintf(int fd, const char *ctl, ...)
  256. {
  257. va_list va;
  258. va_start(va, ctl);
  259. vdprintf(fd, ctl, va);
  260. va_end(va);
  261. }
  262. #endif
  263. static int ChangeUser(const char *user)
  264. {
  265. struct passwd *pas;
  266. const char *err_msg;
  267. /*
  268. * Obtain password entry and change privileges
  269. */
  270. pas = getpwnam(user);
  271. if (pas == 0) {
  272. crondlog("\011failed to get uid for %s", user);
  273. return (-1);
  274. }
  275. setenv("USER", pas->pw_name, 1);
  276. setenv("HOME", pas->pw_dir, 1);
  277. setenv("SHELL", DEFAULT_SHELL, 1);
  278. /*
  279. * Change running state to the user in question
  280. */
  281. err_msg = change_identity_e2str(pas);
  282. if (err_msg) {
  283. crondlog("\011%s for user %s", err_msg, user);
  284. return (-1);
  285. }
  286. if (chdir(pas->pw_dir) < 0) {
  287. crondlog("\011chdir failed: %s: %m", pas->pw_dir);
  288. if (chdir(TMPDIR) < 0) {
  289. crondlog("\011chdir failed: %s: %m", TMPDIR);
  290. return (-1);
  291. }
  292. }
  293. return (pas->pw_uid);
  294. }
  295. static void startlogger(void)
  296. {
  297. if (LogFile == 0) {
  298. openlog(bb_applet_name, LOG_CONS | LOG_PID, LOG_CRON);
  299. }
  300. #ifdef FEATURE_DEBUG_OPT
  301. else { /* test logfile */
  302. int logfd;
  303. if ((logfd = open(LogFile, O_WRONLY | O_CREAT | O_APPEND, 600)) >= 0) {
  304. close(logfd);
  305. } else {
  306. bb_perror_msg("Failed to open log file '%s' reason", LogFile);
  307. }
  308. }
  309. #endif
  310. }
  311. static const char *const DowAry[] = {
  312. "sun",
  313. "mon",
  314. "tue",
  315. "wed",
  316. "thu",
  317. "fri",
  318. "sat",
  319. "Sun",
  320. "Mon",
  321. "Tue",
  322. "Wed",
  323. "Thu",
  324. "Fri",
  325. "Sat",
  326. NULL
  327. };
  328. static const char *const MonAry[] = {
  329. "jan",
  330. "feb",
  331. "mar",
  332. "apr",
  333. "may",
  334. "jun",
  335. "jul",
  336. "aug",
  337. "sep",
  338. "oct",
  339. "nov",
  340. "dec",
  341. "Jan",
  342. "Feb",
  343. "Mar",
  344. "Apr",
  345. "May",
  346. "Jun",
  347. "Jul",
  348. "Aug",
  349. "Sep",
  350. "Oct",
  351. "Nov",
  352. "Dec",
  353. NULL
  354. };
  355. static char *ParseField(char *user, char *ary, int modvalue, int off,
  356. const char *const *names, char *ptr)
  357. {
  358. char *base = ptr;
  359. int n1 = -1;
  360. int n2 = -1;
  361. if (base == NULL) {
  362. return (NULL);
  363. }
  364. while (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
  365. int skip = 0;
  366. /* Handle numeric digit or symbol or '*' */
  367. if (*ptr == '*') {
  368. n1 = 0; /* everything will be filled */
  369. n2 = modvalue - 1;
  370. skip = 1;
  371. ++ptr;
  372. } else if (*ptr >= '0' && *ptr <= '9') {
  373. if (n1 < 0) {
  374. n1 = strtol(ptr, &ptr, 10) + off;
  375. } else {
  376. n2 = strtol(ptr, &ptr, 10) + off;
  377. }
  378. skip = 1;
  379. } else if (names) {
  380. int i;
  381. for (i = 0; names[i]; ++i) {
  382. if (strncmp(ptr, names[i], strlen(names[i])) == 0) {
  383. break;
  384. }
  385. }
  386. if (names[i]) {
  387. ptr += strlen(names[i]);
  388. if (n1 < 0) {
  389. n1 = i;
  390. } else {
  391. n2 = i;
  392. }
  393. skip = 1;
  394. }
  395. }
  396. /* handle optional range '-' */
  397. if (skip == 0) {
  398. crondlog("\111failed user %s parsing %s\n", user, base);
  399. return (NULL);
  400. }
  401. if (*ptr == '-' && n2 < 0) {
  402. ++ptr;
  403. continue;
  404. }
  405. /*
  406. * collapse single-value ranges, handle skipmark, and fill
  407. * in the character array appropriately.
  408. */
  409. if (n2 < 0) {
  410. n2 = n1;
  411. }
  412. if (*ptr == '/') {
  413. skip = strtol(ptr + 1, &ptr, 10);
  414. }
  415. /*
  416. * fill array, using a failsafe is the easiest way to prevent
  417. * an endless loop
  418. */
  419. {
  420. int s0 = 1;
  421. int failsafe = 1024;
  422. --n1;
  423. do {
  424. n1 = (n1 + 1) % modvalue;
  425. if (--s0 == 0) {
  426. ary[n1 % modvalue] = 1;
  427. s0 = skip;
  428. }
  429. }
  430. while (n1 != n2 && --failsafe);
  431. if (failsafe == 0) {
  432. crondlog("\111failed user %s parsing %s\n", user, base);
  433. return (NULL);
  434. }
  435. }
  436. if (*ptr != ',') {
  437. break;
  438. }
  439. ++ptr;
  440. n1 = -1;
  441. n2 = -1;
  442. }
  443. if (*ptr != ' ' && *ptr != '\t' && *ptr != '\n') {
  444. crondlog("\111failed user %s parsing %s\n", user, base);
  445. return (NULL);
  446. }
  447. while (*ptr == ' ' || *ptr == '\t' || *ptr == '\n') {
  448. ++ptr;
  449. }
  450. #ifdef FEATURE_DEBUG_OPT
  451. if (DebugOpt) {
  452. int i;
  453. for (i = 0; i < modvalue; ++i) {
  454. crondlog("\005%d", ary[i]);
  455. }
  456. crondlog("\005\n");
  457. }
  458. #endif
  459. return (ptr);
  460. }
  461. static void FixDayDow(CronLine * line)
  462. {
  463. short i;
  464. short weekUsed = 0;
  465. short daysUsed = 0;
  466. for (i = 0; i < arysize(line->cl_Dow); ++i) {
  467. if (line->cl_Dow[i] == 0) {
  468. weekUsed = 1;
  469. break;
  470. }
  471. }
  472. for (i = 0; i < arysize(line->cl_Days); ++i) {
  473. if (line->cl_Days[i] == 0) {
  474. daysUsed = 1;
  475. break;
  476. }
  477. }
  478. if (weekUsed && !daysUsed) {
  479. memset(line->cl_Days, 0, sizeof(line->cl_Days));
  480. }
  481. if (daysUsed && !weekUsed) {
  482. memset(line->cl_Dow, 0, sizeof(line->cl_Dow));
  483. }
  484. }
  485. static void SynchronizeFile(const char *fileName)
  486. {
  487. int maxEntries = MAXLINES;
  488. int maxLines;
  489. char buf[1024];
  490. if (strcmp(fileName, "root") == 0) {
  491. maxEntries = 65535;
  492. }
  493. maxLines = maxEntries * 10;
  494. if (fileName) {
  495. FILE *fi;
  496. DeleteFile(fileName);
  497. fi = fopen(fileName, "r");
  498. if (fi != NULL) {
  499. struct stat sbuf;
  500. if (fstat(fileno(fi), &sbuf) == 0 && sbuf.st_uid == DaemonUid) {
  501. CronFile *file = calloc(1, sizeof(CronFile));
  502. CronLine **pline;
  503. file->cf_User = strdup(fileName);
  504. pline = &file->cf_LineBase;
  505. while (fgets(buf, sizeof(buf), fi) != NULL && --maxLines) {
  506. CronLine line;
  507. char *ptr;
  508. if (buf[0]) {
  509. buf[strlen(buf) - 1] = 0;
  510. }
  511. if (buf[0] == 0 || buf[0] == '#' || buf[0] == ' ' || buf[0] == '\t') {
  512. continue;
  513. }
  514. if (--maxEntries == 0) {
  515. break;
  516. }
  517. memset(&line, 0, sizeof(line));
  518. #ifdef FEATURE_DEBUG_OPT
  519. if (DebugOpt) {
  520. crondlog("\111User %s Entry %s\n", fileName, buf);
  521. }
  522. #endif
  523. /* parse date ranges */
  524. ptr = ParseField(file->cf_User, line.cl_Mins, 60, 0, NULL, buf);
  525. ptr = ParseField(file->cf_User, line.cl_Hrs, 24, 0, NULL, ptr);
  526. ptr = ParseField(file->cf_User, line.cl_Days, 32, 0, NULL, ptr);
  527. ptr = ParseField(file->cf_User, line.cl_Mons, 12, -1, MonAry, ptr);
  528. ptr = ParseField(file->cf_User, line.cl_Dow, 7, 0, DowAry, ptr);
  529. /* check failure */
  530. if (ptr == NULL) {
  531. continue;
  532. }
  533. /*
  534. * fix days and dow - if one is not * and the other
  535. * is *, the other is set to 0, and vise-versa
  536. */
  537. FixDayDow(&line);
  538. *pline = calloc(1, sizeof(CronLine));
  539. **pline = line;
  540. /* copy command */
  541. (*pline)->cl_Shell = strdup(ptr);
  542. #ifdef FEATURE_DEBUG_OPT
  543. if (DebugOpt) {
  544. crondlog("\111 Command %s\n", ptr);
  545. }
  546. #endif
  547. pline = &((*pline)->cl_Next);
  548. }
  549. *pline = NULL;
  550. file->cf_Next = FileBase;
  551. FileBase = file;
  552. if (maxLines == 0 || maxEntries == 0) {
  553. crondlog("\111Maximum number of lines reached for user %s\n", fileName);
  554. }
  555. }
  556. fclose(fi);
  557. }
  558. }
  559. }
  560. static void CheckUpdates(void)
  561. {
  562. FILE *fi;
  563. char buf[256];
  564. fi = fopen(CRONUPDATE, "r");
  565. if (fi != NULL) {
  566. remove(CRONUPDATE);
  567. while (fgets(buf, sizeof(buf), fi) != NULL) {
  568. SynchronizeFile(strtok(buf, " \t\r\n"));
  569. }
  570. fclose(fi);
  571. }
  572. }
  573. static void SynchronizeDir(void)
  574. {
  575. /* Attempt to delete the database. */
  576. for (;;) {
  577. CronFile *file;
  578. for (file = FileBase; file && file->cf_Deleted; file = file->cf_Next);
  579. if (file == NULL) {
  580. break;
  581. }
  582. DeleteFile(file->cf_User);
  583. }
  584. /*
  585. * Remove cron update file
  586. *
  587. * Re-chdir, in case directory was renamed & deleted, or otherwise
  588. * screwed up.
  589. *
  590. * scan directory and add associated users
  591. */
  592. remove(CRONUPDATE);
  593. if (chdir(CDir) < 0) {
  594. crondlog("\311unable to find %s\n", CDir);
  595. }
  596. {
  597. DIR *dir = opendir(".");
  598. struct dirent *den;
  599. if (dir) {
  600. while ((den = readdir(dir))) {
  601. if (strchr(den->d_name, '.') != NULL) {
  602. continue;
  603. }
  604. if (getpwnam(den->d_name)) {
  605. SynchronizeFile(den->d_name);
  606. } else {
  607. crondlog("\007ignoring %s\n", den->d_name);
  608. }
  609. }
  610. closedir(dir);
  611. } else {
  612. crondlog("\311Unable to open current dir!\n");
  613. }
  614. }
  615. }
  616. /*
  617. * DeleteFile() - delete user database
  618. *
  619. * Note: multiple entries for same user may exist if we were unable to
  620. * completely delete a database due to running processes.
  621. */
  622. static void DeleteFile(const char *userName)
  623. {
  624. CronFile **pfile = &FileBase;
  625. CronFile *file;
  626. while ((file = *pfile) != NULL) {
  627. if (strcmp(userName, file->cf_User) == 0) {
  628. CronLine **pline = &file->cf_LineBase;
  629. CronLine *line;
  630. file->cf_Running = 0;
  631. file->cf_Deleted = 1;
  632. while ((line = *pline) != NULL) {
  633. if (line->cl_Pid > 0) {
  634. file->cf_Running = 1;
  635. pline = &line->cl_Next;
  636. } else {
  637. *pline = line->cl_Next;
  638. free(line->cl_Shell);
  639. free(line);
  640. }
  641. }
  642. if (file->cf_Running == 0) {
  643. *pfile = file->cf_Next;
  644. free(file->cf_User);
  645. free(file);
  646. } else {
  647. pfile = &file->cf_Next;
  648. }
  649. } else {
  650. pfile = &file->cf_Next;
  651. }
  652. }
  653. }
  654. /*
  655. * TestJobs()
  656. *
  657. * determine which jobs need to be run. Under normal conditions, the
  658. * period is about a minute (one scan). Worst case it will be one
  659. * hour (60 scans).
  660. */
  661. static int TestJobs(time_t t1, time_t t2)
  662. {
  663. short nJobs = 0;
  664. time_t t;
  665. /* Find jobs > t1 and <= t2 */
  666. for (t = t1 - t1 % 60; t <= t2; t += 60) {
  667. if (t > t1) {
  668. struct tm *tp = localtime(&t);
  669. CronFile *file;
  670. CronLine *line;
  671. for (file = FileBase; file; file = file->cf_Next) {
  672. #ifdef FEATURE_DEBUG_OPT
  673. if (DebugOpt)
  674. crondlog("\005FILE %s:\n", file->cf_User);
  675. #endif
  676. if (file->cf_Deleted)
  677. continue;
  678. for (line = file->cf_LineBase; line; line = line->cl_Next) {
  679. #ifdef FEATURE_DEBUG_OPT
  680. if (DebugOpt)
  681. crondlog("\005 LINE %s\n", line->cl_Shell);
  682. #endif
  683. if (line->cl_Mins[tp->tm_min] && line->cl_Hrs[tp->tm_hour] &&
  684. (line->cl_Days[tp->tm_mday] || line->cl_Dow[tp->tm_wday])
  685. && line->cl_Mons[tp->tm_mon]) {
  686. #ifdef FEATURE_DEBUG_OPT
  687. if (DebugOpt) {
  688. crondlog("\005 JobToDo: %d %s\n",
  689. line->cl_Pid, line->cl_Shell);
  690. }
  691. #endif
  692. if (line->cl_Pid > 0) {
  693. crondlog("\010 process already running: %s %s\n",
  694. file->cf_User, line->cl_Shell);
  695. } else if (line->cl_Pid == 0) {
  696. line->cl_Pid = -1;
  697. file->cf_Ready = 1;
  698. ++nJobs;
  699. }
  700. }
  701. }
  702. }
  703. }
  704. }
  705. return (nJobs);
  706. }
  707. static void RunJobs(void)
  708. {
  709. CronFile *file;
  710. CronLine *line;
  711. for (file = FileBase; file; file = file->cf_Next) {
  712. if (file->cf_Ready) {
  713. file->cf_Ready = 0;
  714. for (line = file->cf_LineBase; line; line = line->cl_Next) {
  715. if (line->cl_Pid < 0) {
  716. RunJob(file->cf_User, line);
  717. crondlog("\010USER %s pid %3d cmd %s\n",
  718. file->cf_User, line->cl_Pid, line->cl_Shell);
  719. if (line->cl_Pid < 0) {
  720. file->cf_Ready = 1;
  721. }
  722. else if (line->cl_Pid > 0) {
  723. file->cf_Running = 1;
  724. }
  725. }
  726. }
  727. }
  728. }
  729. }
  730. /*
  731. * CheckJobs() - check for job completion
  732. *
  733. * Check for job completion, return number of jobs still running after
  734. * all done.
  735. */
  736. static int CheckJobs(void)
  737. {
  738. CronFile *file;
  739. CronLine *line;
  740. int nStillRunning = 0;
  741. for (file = FileBase; file; file = file->cf_Next) {
  742. if (file->cf_Running) {
  743. file->cf_Running = 0;
  744. for (line = file->cf_LineBase; line; line = line->cl_Next) {
  745. if (line->cl_Pid > 0) {
  746. int status;
  747. int r = wait4(line->cl_Pid, &status, WNOHANG, NULL);
  748. if (r < 0 || r == line->cl_Pid) {
  749. EndJob(file->cf_User, line);
  750. if (line->cl_Pid) {
  751. file->cf_Running = 1;
  752. }
  753. } else if (r == 0) {
  754. file->cf_Running = 1;
  755. }
  756. }
  757. }
  758. }
  759. nStillRunning += file->cf_Running;
  760. }
  761. return (nStillRunning);
  762. }
  763. #ifdef CONFIG_FEATURE_CROND_CALL_SENDMAIL
  764. static void
  765. ForkJob(const char *user, CronLine * line, int mailFd,
  766. const char *prog, const char *cmd, const char *arg, const char *mailf)
  767. {
  768. /* Fork as the user in question and run program */
  769. pid_t pid = fork();
  770. line->cl_Pid = pid;
  771. if (pid == 0) {
  772. /* CHILD */
  773. /* Change running state to the user in question */
  774. if (ChangeUser(user) < 0) {
  775. exit(0);
  776. }
  777. #ifdef FEATURE_DEBUG_OPT
  778. if (DebugOpt) {
  779. crondlog("\005Child Running %s\n", prog);
  780. }
  781. #endif
  782. if (mailFd >= 0) {
  783. dup2(mailFd, mailf != NULL);
  784. dup2((mailf ? mailFd : 1), 2);
  785. close(mailFd);
  786. }
  787. execl(prog, prog, cmd, arg, NULL);
  788. crondlog("\024unable to exec, user %s cmd %s %s %s\n", user, prog, cmd, arg);
  789. if (mailf) {
  790. fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
  791. }
  792. exit(0);
  793. } else if (pid < 0) {
  794. /* FORK FAILED */
  795. crondlog("\024couldn't fork, user %s\n", user);
  796. line->cl_Pid = 0;
  797. if (mailf) {
  798. remove(mailf);
  799. }
  800. } else if (mailf) {
  801. /* PARENT, FORK SUCCESS
  802. * rename mail-file based on pid of process
  803. */
  804. char mailFile2[128];
  805. snprintf(mailFile2, sizeof(mailFile2), TMPDIR "/cron.%s.%d", user, pid);
  806. rename(mailf, mailFile2);
  807. }
  808. /*
  809. * Close the mail file descriptor.. we can't just leave it open in
  810. * a structure, closing it later, because we might run out of descriptors
  811. */
  812. if (mailFd >= 0) {
  813. close(mailFd);
  814. }
  815. }
  816. static void RunJob(const char *user, CronLine * line)
  817. {
  818. char mailFile[128];
  819. int mailFd;
  820. line->cl_Pid = 0;
  821. line->cl_MailFlag = 0;
  822. /* open mail file - owner root so nobody can screw with it. */
  823. snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, getpid());
  824. mailFd = open(mailFile, O_CREAT | O_TRUNC | O_WRONLY | O_EXCL | O_APPEND, 0600);
  825. if (mailFd >= 0) {
  826. line->cl_MailFlag = 1;
  827. fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", user,
  828. line->cl_Shell);
  829. line->cl_MailPos = lseek(mailFd, 0, 1);
  830. } else {
  831. crondlog("\024unable to create mail file user %s file %s, output to /dev/null\n", user, mailFile);
  832. }
  833. ForkJob(user, line, mailFd, DEFAULT_SHELL, "-c", line->cl_Shell, mailFile);
  834. }
  835. /*
  836. * EndJob - called when job terminates and when mail terminates
  837. */
  838. static void EndJob(const char *user, CronLine * line)
  839. {
  840. int mailFd;
  841. char mailFile[128];
  842. struct stat sbuf;
  843. /* No job */
  844. if (line->cl_Pid <= 0) {
  845. line->cl_Pid = 0;
  846. return;
  847. }
  848. /*
  849. * End of job and no mail file
  850. * End of sendmail job
  851. */
  852. snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d", user, line->cl_Pid);
  853. line->cl_Pid = 0;
  854. if (line->cl_MailFlag != 1) {
  855. return;
  856. }
  857. line->cl_MailFlag = 0;
  858. /*
  859. * End of primary job - check for mail file. If size has increased and
  860. * the file is still valid, we sendmail it.
  861. */
  862. mailFd = open(mailFile, O_RDONLY);
  863. remove(mailFile);
  864. if (mailFd < 0) {
  865. return;
  866. }
  867. if (fstat(mailFd, &sbuf) < 0 || sbuf.st_uid != DaemonUid || sbuf.st_nlink != 0 ||
  868. sbuf.st_size == line->cl_MailPos || !S_ISREG(sbuf.st_mode)) {
  869. close(mailFd);
  870. return;
  871. }
  872. ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL);
  873. }
  874. #else
  875. /* crond without sendmail */
  876. static void RunJob(const char *user, CronLine * line)
  877. {
  878. /* Fork as the user in question and run program */
  879. pid_t pid = fork();
  880. if (pid == 0) {
  881. /* CHILD */
  882. /* Change running state to the user in question */
  883. if (ChangeUser(user) < 0) {
  884. exit(0);
  885. }
  886. #ifdef FEATURE_DEBUG_OPT
  887. if (DebugOpt) {
  888. crondlog("\005Child Running %s\n", DEFAULT_SHELL);
  889. }
  890. #endif
  891. execl(DEFAULT_SHELL, DEFAULT_SHELL, "-c", line->cl_Shell, NULL);
  892. crondlog("\024unable to exec, user %s cmd %s -c %s\n", user,
  893. DEFAULT_SHELL, line->cl_Shell);
  894. exit(0);
  895. } else if (pid < 0) {
  896. /* FORK FAILED */
  897. crondlog("\024couldn't fork, user %s\n", user);
  898. pid = 0;
  899. }
  900. line->cl_Pid = pid;
  901. }
  902. #endif /* CONFIG_FEATURE_CROND_CALL_SENDMAIL */