1 /* Copyright 1988,1990,1993,1994 by Paul Vixie
4 * Distribute freely, except: don't remove my name from the source or
5 * documentation (don't take credit for my work), mark your changes (don't
6 * get me blamed for your possible bugs), don't alter or remove this
7 * notice. May be sold if buildable source is provided to buyer. No
8 * warrantee of any kind, express or implied, is included with this
9 * software; use at your own risk, responsibility for damages (if any) to
10 * anyone resulting from the use of this software rests entirely with the
13 * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
14 * I'll try to keep a version up to date. I can be reached as follows:
15 * Paul Vixie <paul@vix.com> uunet!decwrl!vixie!paul
18 #if !defined(lint) && !defined(LINT)
19 static const char rcsid[] =
25 #include <sys/signal.h>
27 # include <sys/universe.h>
32 #if defined(LOGIN_CAP)
33 # include <login_cap.h>
37 static void child_process __P((entry *, user *)),
38 do_univ __P((user *));
46 Debug(DPROC, ("[%d] do_command(%s, (%s,%d,%d))\n",
47 getpid(), e->cmd, u->name, e->uid, e->gid))
49 /* fork to become asynchronous -- parent process is done immediately,
50 * and continues to run the normal cron code, which means return to
51 * tick(). the child and grandchild don't leave this function, alive.
53 * vfork() is unsuitable, since we have much to do, and the parent
54 * needs to be able to run off and fork other processes.
58 log_it("CRON",getpid(),"error","can't fork");
62 acquire_daemonlock(1);
64 Debug(DPROC, ("[%d] child process done, exiting\n", getpid()))
71 Debug(DPROC, ("[%d] main process returning to work\n", getpid()))
80 int stdin_pipe[2], stdout_pipe[2];
81 register char *input_data;
82 char *usernm, *mailto;
84 # if defined(LOGIN_CAP)
89 Debug(DPROC, ("[%d] child_process('%s')\n", getpid(), e->cmd))
91 /* mark ourselves as different to PS command watchers by upshifting
92 * our program name. This has no effect on some kernels.
94 setproctitle("running job");
96 /* discover some useful and important environment settings
98 usernm = env_get("LOGNAME", e->envp);
99 mailto = env_get("MAILTO", e->envp);
102 /* our parent is watching for our death by catching SIGCHLD. we
103 * do not care to watch for our children's deaths this way -- we
104 * use wait() explictly. so we have to disable the signal (which
105 * was inherited from the parent).
107 (void) signal(SIGCHLD, SIG_DFL);
109 /* on system-V systems, we are ignoring SIGCLD. we have to stop
110 * ignoring it now or the wait() in cron_pclose() won't work.
111 * because of this, we have to wait() for our children here, as well.
113 (void) signal(SIGCLD, SIG_DFL);
116 /* create some pipes to talk to our future child
118 pipe(stdin_pipe); /* child's stdin */
119 pipe(stdout_pipe); /* child's stdout */
121 /* since we are a forked process, we can diddle the command string
122 * we were passed -- nobody else is going to use it again, right?
124 * if a % is present in the command, previous characters are the
125 * command, and subsequent characters are the additional input to
126 * the command. Subsequent %'s will be transformed into newlines,
127 * but that happens later.
129 * If there are escaped %'s, remove the escape character.
132 register int escaped = FALSE;
136 for (input_data = p = e->cmd; (ch = *input_data);
141 if (ch == '%' || ch == '\\')
151 *input_data++ = '\0';
158 /* fork again, this time so we can exec the user's command.
162 log_it("CRON",getpid(),"error","can't vfork");
166 Debug(DPROC, ("[%d] grandchild process Vfork()'ed\n",
169 /* write a log message. we've waited this long to do it
170 * because it was not until now that we knew the PID that
171 * the actual user command shell was going to get and the
172 * PID is part of the log message.
175 char *x = mkprints((u_char *)e->cmd, strlen(e->cmd));
177 log_it(usernm, getpid(), "CMD", x);
181 /* that's the last thing we'll log. close the log files.
187 /* get new pgrp, void tty, etc.
191 /* close the pipe ends that we won't use. this doesn't affect
192 * the parent, who has to read and write them; it keeps the
193 * kernel from recording us as a potential client TWICE --
194 * which would keep it from sending SIGPIPE in otherwise
195 * appropriate circumstances.
197 close(stdin_pipe[WRITE_PIPE]);
198 close(stdout_pipe[READ_PIPE]);
200 /* grandchild process. make std{in,out} be the ends of
201 * pipes opened by our daddy; make stderr go to stdout.
203 close(STDIN); dup2(stdin_pipe[READ_PIPE], STDIN);
204 close(STDOUT); dup2(stdout_pipe[WRITE_PIPE], STDOUT);
205 close(STDERR); dup2(STDOUT, STDERR);
207 /* close the pipes we just dup'ed. The resources will remain.
209 close(stdin_pipe[READ_PIPE]);
210 close(stdout_pipe[WRITE_PIPE]);
212 /* set our login universe. Do this in the grandchild
213 * so that the child can invoke /usr/lib/sendmail
218 # if defined(LOGIN_CAP)
219 /* Set user's entire context, but skip the environment
220 * as cron provides a separate interface for this
222 if ((pwd = getpwnam(usernm)) == NULL)
223 pwd = getpwuid(e->uid);
226 pwd->pw_gid = e->gid;
227 if (e->class != NULL)
228 lc = login_getclass(e->class);
231 setusercontext(lc, pwd, e->uid,
232 LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETENV)) == 0)
235 /* fall back to the old method */
238 /* set our directory, uid and gid. Set gid first,
239 * since once we set uid, we've lost root privledges.
243 initgroups(usernm, e->gid);
246 setuid(e->uid); /* we aren't root after this..*/
247 #if defined(LOGIN_CAP)
252 chdir(env_get("HOME", e->envp));
257 char *shell = env_get("SHELL", e->envp);
260 if (DebugFlags & DTEST) {
262 "debug DTEST is on, not exec'ing command.\n");
264 "\tcmd='%s' shell='%s'\n", e->cmd, shell);
267 # endif /*DEBUGGING*/
268 execle(shell, shell, "-c", e->cmd, (char *)0, e->envp);
269 warn("execl: couldn't exec `%s'", shell);
280 /* middle process, child of original cron, parent of process running
281 * the user's command.
284 Debug(DPROC, ("[%d] child continues, closing pipes\n", getpid()))
286 /* close the ends of the pipe that will only be referenced in the
287 * grandchild process...
289 close(stdin_pipe[READ_PIPE]);
290 close(stdout_pipe[WRITE_PIPE]);
293 * write, to the pipe connected to child's stdin, any input specified
294 * after a % in the crontab entry. while we copy, convert any
295 * additional %'s to newlines. when done, if some characters were
296 * written and the last one wasn't a newline, write a newline.
298 * Note that if the input data won't fit into one pipe buffer (2K
299 * or 4K on most BSD systems), and the child doesn't read its stdin,
300 * we would block here. thus we must fork again.
303 if (*input_data && fork() == 0) {
304 register FILE *out = fdopen(stdin_pipe[WRITE_PIPE], "w");
305 register int need_newline = FALSE;
306 register int escaped = FALSE;
310 warn("fdopen failed in child2");
314 Debug(DPROC, ("[%d] child2 sending data to grandchild\n", getpid()))
316 /* close the pipe we don't use, since we inherited it and
317 * are part of its reference count now.
319 close(stdout_pipe[READ_PIPE]);
324 * \x -> \x for all x != %
326 while ((ch = *input_data++)) {
335 if (!(escaped = (ch == '\\'))) {
337 need_newline = (ch != '\n');
345 /* close the pipe, causing an EOF condition. fclose causes
346 * stdin_pipe[WRITE_PIPE] to be closed, too.
350 Debug(DPROC, ("[%d] child2 done sending to grandchild\n", getpid()))
354 /* close the pipe to the grandkiddie's stdin, since its wicked uncle
355 * ernie back there has it open and will close it when he's done.
357 close(stdin_pipe[WRITE_PIPE]);
362 * read output from the grandchild. it's stderr has been redirected to
363 * it's stdout, which has been redirected to our pipe. if there is any
364 * output, we'll be mailing it to the user whose crontab this is...
365 * when the grandchild exits, we'll get EOF.
368 Debug(DPROC, ("[%d] child reading output from grandchild\n", getpid()))
371 register FILE *in = fdopen(stdout_pipe[READ_PIPE], "r");
375 warn("fdopen failed in child");
382 register int bytes = 1;
386 ("[%d] got data (%x:%c) from grandchild\n",
389 /* get name of recipient. this is MAILTO if set to a
390 * valid local username; USER otherwise.
393 /* MAILTO was present in the environment
396 /* ... but it's empty. set to NULL
401 /* MAILTO not present, set to USER.
406 /* if we are supposed to be mailing, MAILTO will
407 * be non-NULL. only in this case should we set
408 * up the mail command and subjects and stuff...
413 auto char mailcmd[MAX_COMMAND];
414 auto char hostname[MAXHOSTNAMELEN];
416 (void) gethostname(hostname, MAXHOSTNAMELEN);
417 (void) snprintf(mailcmd, sizeof(mailcmd),
419 if (!(mail = cron_popen(mailcmd, "w", e))) {
421 (void) _exit(ERROR_EXIT);
423 fprintf(mail, "From: %s (Cron Daemon)\n", usernm);
424 fprintf(mail, "To: %s\n", mailto);
425 fprintf(mail, "Subject: Cron <%s@%s> %s\n",
426 usernm, first_word(hostname, "."),
428 # if defined(MAIL_DATE)
429 fprintf(mail, "Date: %s\n",
430 arpadate(&TargetTime));
431 # endif /* MAIL_DATE */
432 for (env = e->envp; *env; env++)
433 fprintf(mail, "X-Cron-Env: <%s>\n",
437 /* this was the first char from the pipe
442 /* we have to read the input pipe no matter whether
443 * we mail or not, but obviously we only write to
444 * mail pipe if we ARE mailing.
447 while (EOF != (ch = getc(in))) {
453 /* only close pipe if we opened it -- i.e., we're
458 Debug(DPROC, ("[%d] closing pipe to mail\n",
460 /* Note: the pclose will probably see
461 * the termination of the grandchild
462 * in addition to the mail process, since
463 * it (the grandchild) is likely to exit
464 * after closing its stdout.
466 status = cron_pclose(mail);
469 /* if there was output and we could not mail it,
470 * log the facts so the poor user can figure out
473 if (mailto && status) {
474 char buf[MAX_TEMPSTR];
476 snprintf(buf, sizeof(buf),
477 "mailed %d byte%s of output but got status 0x%04x\n",
478 bytes, (bytes==1)?"":"s",
480 log_it(usernm, getpid(), "MAIL", buf);
483 } /*if data from grandchild*/
485 Debug(DPROC, ("[%d] got EOF from grandchild\n", getpid()))
487 fclose(in); /* also closes stdout_pipe[READ_PIPE] */
490 /* wait for children to die.
492 for (; children > 0; children--)
497 Debug(DPROC, ("[%d] waiting for grandchild #%d to finish\n",
501 Debug(DPROC, ("[%d] no more grandchildren--mail written?\n",
505 Debug(DPROC, ("[%d] grandchild #%d finished, status=%04x",
506 getpid(), pid, WEXITSTATUS(waiter)))
507 if (WIFSIGNALED(waiter) && WCOREDUMP(waiter))
508 Debug(DPROC, (", dumped core"))
519 /* Dynix (Sequent) hack to put the user associated with
520 * the passed user structure into the ATT universe if
521 * necessary. We have to dig the gecos info out of
522 * the user's password entry to see if the magic
523 * "universe(att)" string is present.
530 p = getpwuid(u->uid);
538 for (i = 0; i < 4; i++)
540 if ((s = strchr(s, ',')) == NULL)
544 if (strcmp(s, "universe(att)"))
547 (void) universe(U_ATT);