- execl(prog, prog, cmd, arg, NULL);
- log("\024unable to exec, user %s cmd %s %s %s\n", user,
- prog, cmd, arg);
- if(mailf)
- fdprintf(1, "Exec failed: %s -c %s\n", prog, arg);
- exit(0);
- } else if (pid < 0) {
- /*
- * FORK FAILED
- */
- log("\024couldn't fork, user %s\n", user);
- line->cl_Pid = 0;
- if(mailf)
- remove(mailf);
- } else if(mailf) {
- /*
- * PARENT, FORK SUCCESS
- *
- * rename mail-file based on pid of process
- */
- char mailFile2[128];
-
- snprintf(mailFile2, sizeof(mailFile2), TMPDIR "/cron.%s.%d",
- user, pid);
- rename(mailf, mailFile2);
- }
- /*
- * Close the mail file descriptor.. we can't just leave it open in
- * a structure, closing it later, because we might run out of descriptors
- */
-
- if (mailFd >= 0)
- close(mailFd);
-}
-
-static void
-RunJob(const char *user, CronLine *line)
-{
- char mailFile[128];
- int mailFd;
-
- line->cl_Pid = 0;
- line->cl_MailFlag = 0;
-
- /*
- * open mail file - owner root so nobody can screw with it.
- */
-
- snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d",
- user, getpid());
- mailFd = open(mailFile, O_CREAT|O_TRUNC|O_WRONLY|O_EXCL|O_APPEND, 0600);
-
- if (mailFd >= 0) {
- line->cl_MailFlag = 1;
- fdprintf(mailFd, "To: %s\nSubject: cron: %s\n\n", user,
- line->cl_Shell);
- line->cl_MailPos = lseek(mailFd, 0, 1);
- } else {
- log("\024unable to create mail file user %s file %s, output to /dev/null\n",
- user, mailFile);
- }
-
- ForkJob(user, line, mailFd, def_sh, "-c", line->cl_Shell, mailFile);
-}
-
-/*
- * EndJob - called when job terminates and when mail terminates
- */
-
-static void
-EndJob(const char *user, CronLine *line)
-{
- int mailFd;
- char mailFile[128];
- struct stat sbuf;
-
- /*
- * No job
- */
-
- if (line->cl_Pid <= 0) {
- line->cl_Pid = 0;
- return;
- }
-
- /*
- * End of job and no mail file
- * End of sendmail job
- */
-
- snprintf(mailFile, sizeof(mailFile), TMPDIR "/cron.%s.%d",
- user, line->cl_Pid);
- line->cl_Pid = 0;
-
- if (line->cl_MailFlag != 1)
- return;
-
- line->cl_MailFlag = 0;
-
- /*
- * End of primary job - check for mail file. If size has increased and
- * the file is still valid, we sendmail it.
- */
-
- mailFd = open(mailFile, O_RDONLY);
- remove(mailFile);
- if (mailFd < 0) {
- return;
- }
-
- if (fstat(mailFd, &sbuf) < 0 ||
- sbuf.st_uid != DaemonUid ||
- sbuf.st_nlink != 0 ||
- sbuf.st_size == line->cl_MailPos ||
- !S_ISREG(sbuf.st_mode)
- ) {
- close(mailFd);
- return;
- }
- ForkJob(user, line, mailFd, SENDMAIL, SENDMAIL_ARGS, NULL);