File:  [DragonFly] / src / usr.sbin / cron / cron / do_command.c
Revision 1.5: download - view: text, annotated - select for diffs
Wed Mar 10 18:27:26 2004 UTC (10 years, 7 months ago) by dillon
Branches: MAIN
CVS tags: HEAD, DragonFly_Snap29Sep2004, DragonFly_Snap13Sep2004, DragonFly_1_0_REL, DragonFly_1_0_RC1, DragonFly_1_0A_REL
ANSIfication, remove 'register' and 'auto' use, convert K&R procedure decls.
Fix one bug where getc() was being called on a FILE returned by fdopen()
before the a FILE was checked for NULL.

Submitted-by: Chris Pressey <cpressey@catseye.mine.nu>

    1: /* Copyright 1988,1990,1993,1994 by Paul Vixie
    2:  * All rights reserved
    3:  *
    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
   11:  * user.
   12:  *
   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
   16:  *
   17:  * $FreeBSD: src/usr.sbin/cron/cron/do_command.c,v 1.15.2.5 2001/05/04 00:59:40 peter Exp $
   18:  * $DragonFly: src/usr.sbin/cron/cron/do_command.c,v 1.5 2004/03/10 18:27:26 dillon Exp $
   19:  */
   20: 
   21: #include "cron.h"
   22: #include <sys/signal.h>
   23: #if defined(sequent)
   24: # include <sys/universe.h>
   25: #endif
   26: #if defined(SYSLOG)
   27: # include <syslog.h>
   28: #endif
   29: #if defined(LOGIN_CAP)
   30: # include <login_cap.h>
   31: #endif
   32: 
   33: 
   34: static void		child_process(entry *, user *),
   35: 			do_univ(user *);
   36: 
   37: 
   38: void
   39: do_command(entry *e, user *u)
   40: {
   41: 	Debug(DPROC, ("[%d] do_command(%s, (%s,%d,%d))\n",
   42: 		getpid(), e->cmd, u->name, e->uid, e->gid))
   43: 
   44: 	/* fork to become asynchronous -- parent process is done immediately,
   45: 	 * and continues to run the normal cron code, which means return to
   46: 	 * tick().  the child and grandchild don't leave this function, alive.
   47: 	 *
   48: 	 * vfork() is unsuitable, since we have much to do, and the parent
   49: 	 * needs to be able to run off and fork other processes.
   50: 	 */
   51: 	switch (fork()) {
   52: 	case -1:
   53: 		log_it("CRON",getpid(),"error","can't fork");
   54: 		break;
   55: 	case 0:
   56: 		/* child process */
   57: 		acquire_daemonlock(1);
   58: 		child_process(e, u);
   59: 		Debug(DPROC, ("[%d] child process done, exiting\n", getpid()))
   60: 		_exit(OK_EXIT);
   61: 		break;
   62: 	default:
   63: 		/* parent process */
   64: 		break;
   65: 	}
   66: 	Debug(DPROC, ("[%d] main process returning to work\n", getpid()))
   67: }
   68: 
   69: 
   70: static void
   71: child_process(entry *e, user *u)
   72: {
   73: 	int stdin_pipe[2], stdout_pipe[2];
   74: 	char *input_data;
   75: 	char *usernm, *mailto;
   76: 	int children = 0;
   77: # if defined(LOGIN_CAP)
   78: 	struct passwd *pwd;
   79: 	login_cap_t *lc;
   80: # endif
   81: 
   82: 	Debug(DPROC, ("[%d] child_process('%s')\n", getpid(), e->cmd))
   83: 
   84: 	/* mark ourselves as different to PS command watchers by upshifting
   85: 	 * our program name.  This has no effect on some kernels.
   86: 	 */
   87: 	setproctitle("running job");
   88: 
   89: 	/* discover some useful and important environment settings
   90: 	 */
   91: 	usernm = env_get("LOGNAME", e->envp);
   92: 	mailto = env_get("MAILTO", e->envp);
   93: 
   94: #ifdef USE_SIGCHLD
   95: 	/* our parent is watching for our death by catching SIGCHLD.  we
   96: 	 * do not care to watch for our children's deaths this way -- we
   97: 	 * use wait() explictly.  so we have to disable the signal (which
   98: 	 * was inherited from the parent).
   99: 	 */
  100: 	(void) signal(SIGCHLD, SIG_DFL);
  101: #else
  102: 	/* on system-V systems, we are ignoring SIGCLD.  we have to stop
  103: 	 * ignoring it now or the wait() in cron_pclose() won't work.
  104: 	 * because of this, we have to wait() for our children here, as well.
  105: 	 */
  106: 	(void) signal(SIGCLD, SIG_DFL);
  107: #endif /*BSD*/
  108: 
  109: 	/* create some pipes to talk to our future child
  110: 	 */
  111: 	pipe(stdin_pipe);	/* child's stdin */
  112: 	pipe(stdout_pipe);	/* child's stdout */
  113: 
  114: 	/* since we are a forked process, we can diddle the command string
  115: 	 * we were passed -- nobody else is going to use it again, right?
  116: 	 *
  117: 	 * if a % is present in the command, previous characters are the
  118: 	 * command, and subsequent characters are the additional input to
  119: 	 * the command.  Subsequent %'s will be transformed into newlines,
  120: 	 * but that happens later.
  121: 	 *
  122: 	 * If there are escaped %'s, remove the escape character.
  123: 	 */
  124: 	/*local*/{
  125: 		int escaped = FALSE;
  126: 		int ch;
  127: 		char *p;
  128: 
  129: 		for (input_data = p = e->cmd; (ch = *input_data);
  130: 		     input_data++, p++) {
  131: 			if (p != input_data)
  132: 			    *p = ch;
  133: 			if (escaped) {
  134: 				if (ch == '%' || ch == '\\')
  135: 					*--p = ch;
  136: 				escaped = FALSE;
  137: 				continue;
  138: 			}
  139: 			if (ch == '\\') {
  140: 				escaped = TRUE;
  141: 				continue;
  142: 			}
  143: 			if (ch == '%') {
  144: 				*input_data++ = '\0';
  145: 				break;
  146: 			}
  147: 		}
  148: 		*p = '\0';
  149: 	}
  150: 
  151: 	/* fork again, this time so we can exec the user's command.
  152: 	 */
  153: 	switch (vfork()) {
  154: 	case -1:
  155: 		log_it("CRON",getpid(),"error","can't vfork");
  156: 		exit(ERROR_EXIT);
  157: 		/*NOTREACHED*/
  158: 	case 0:
  159: 		Debug(DPROC, ("[%d] grandchild process Vfork()'ed\n",
  160: 			      getpid()))
  161: 
  162: 		/* write a log message.  we've waited this long to do it
  163: 		 * because it was not until now that we knew the PID that
  164: 		 * the actual user command shell was going to get and the
  165: 		 * PID is part of the log message.
  166: 		 */
  167: 		/*local*/{
  168: 			char *x = mkprints((u_char *)e->cmd, strlen(e->cmd));
  169: 
  170: 			log_it(usernm, getpid(), "CMD", x);
  171: 			free(x);
  172: 		}
  173: 
  174: 		/* that's the last thing we'll log.  close the log files.
  175: 		 */
  176: #ifdef SYSLOG
  177: 		closelog();
  178: #endif
  179: 
  180: 		/* get new pgrp, void tty, etc.
  181: 		 */
  182: 		(void) setsid();
  183: 
  184: 		/* close the pipe ends that we won't use.  this doesn't affect
  185: 		 * the parent, who has to read and write them; it keeps the
  186: 		 * kernel from recording us as a potential client TWICE --
  187: 		 * which would keep it from sending SIGPIPE in otherwise
  188: 		 * appropriate circumstances.
  189: 		 */
  190: 		close(stdin_pipe[WRITE_PIPE]);
  191: 		close(stdout_pipe[READ_PIPE]);
  192: 
  193: 		/* grandchild process.  make std{in,out} be the ends of
  194: 		 * pipes opened by our daddy; make stderr go to stdout.
  195: 		 */
  196: 		close(STDIN);	dup2(stdin_pipe[READ_PIPE], STDIN);
  197: 		close(STDOUT);	dup2(stdout_pipe[WRITE_PIPE], STDOUT);
  198: 		close(STDERR);	dup2(STDOUT, STDERR);
  199: 
  200: 		/* close the pipes we just dup'ed.  The resources will remain.
  201: 		 */
  202: 		close(stdin_pipe[READ_PIPE]);
  203: 		close(stdout_pipe[WRITE_PIPE]);
  204: 
  205: 		/* set our login universe.  Do this in the grandchild
  206: 		 * so that the child can invoke /usr/lib/sendmail
  207: 		 * without surprises.
  208: 		 */
  209: 		do_univ(u);
  210: 
  211: # if defined(LOGIN_CAP)
  212: 		/* Set user's entire context, but skip the environment
  213: 		 * as cron provides a separate interface for this
  214: 		 */
  215: 		if ((pwd = getpwnam(usernm)) == NULL)
  216: 			pwd = getpwuid(e->uid);
  217: 		lc = NULL;
  218: 		if (pwd != NULL) {
  219: 			pwd->pw_gid = e->gid;
  220: 			if (e->class != NULL)
  221: 				lc = login_getclass(e->class);
  222: 		}
  223: 		if (pwd &&
  224: 		    setusercontext(lc, pwd, e->uid,
  225: 			    LOGIN_SETALL & ~(LOGIN_SETPATH|LOGIN_SETENV)) == 0)
  226: 			(void) endpwent();
  227: 		else {
  228: 			/* fall back to the old method */
  229: 			(void) endpwent();
  230: # endif
  231: 			/* set our directory, uid and gid.  Set gid first,
  232: 			 * since once we set uid, we've lost root privledges.
  233: 			 */
  234: 			setgid(e->gid);
  235: # if defined(BSD)
  236: 			initgroups(usernm, e->gid);
  237: # endif
  238: 			setlogin(usernm);
  239: 			setuid(e->uid);		/* we aren't root after this..*/
  240: #if defined(LOGIN_CAP)
  241: 		}
  242: 		if (lc != NULL)
  243: 			login_close(lc);
  244: #endif
  245: 		chdir(env_get("HOME", e->envp));
  246: 
  247: 		/* exec the command.
  248: 		 */
  249: 		{
  250: 			char	*shell = env_get("SHELL", e->envp);
  251: 
  252: # if DEBUGGING
  253: 			if (DebugFlags & DTEST) {
  254: 				fprintf(stderr,
  255: 				"debug DTEST is on, not exec'ing command.\n");
  256: 				fprintf(stderr,
  257: 				"\tcmd='%s' shell='%s'\n", e->cmd, shell);
  258: 				_exit(OK_EXIT);
  259: 			}
  260: # endif /*DEBUGGING*/
  261: 			execle(shell, shell, "-c", e->cmd, (char *)0, e->envp);
  262: 			warn("execl: couldn't exec `%s'", shell);
  263: 			_exit(ERROR_EXIT);
  264: 		}
  265: 		break;
  266: 	default:
  267: 		/* parent process */
  268: 		break;
  269: 	}
  270: 
  271: 	children++;
  272: 
  273: 	/* middle process, child of original cron, parent of process running
  274: 	 * the user's command.
  275: 	 */
  276: 
  277: 	Debug(DPROC, ("[%d] child continues, closing pipes\n", getpid()))
  278: 
  279: 	/* close the ends of the pipe that will only be referenced in the
  280: 	 * grandchild process...
  281: 	 */
  282: 	close(stdin_pipe[READ_PIPE]);
  283: 	close(stdout_pipe[WRITE_PIPE]);
  284: 
  285: 	/*
  286: 	 * write, to the pipe connected to child's stdin, any input specified
  287: 	 * after a % in the crontab entry.  while we copy, convert any
  288: 	 * additional %'s to newlines.  when done, if some characters were
  289: 	 * written and the last one wasn't a newline, write a newline.
  290: 	 *
  291: 	 * Note that if the input data won't fit into one pipe buffer (2K
  292: 	 * or 4K on most BSD systems), and the child doesn't read its stdin,
  293: 	 * we would block here.  thus we must fork again.
  294: 	 */
  295: 
  296: 	if (*input_data && fork() == 0) {
  297: 		FILE *out;
  298: 		int need_newline = FALSE;
  299: 		int escaped = FALSE;
  300: 		int ch;
  301: 
  302: 		out = fdopen(stdin_pipe[WRITE_PIPE], "w");
  303: 		if (out == NULL) {
  304: 			warn("fdopen failed in child2");
  305: 			_exit(ERROR_EXIT);
  306: 		}
  307: 
  308: 		Debug(DPROC, ("[%d] child2 sending data to grandchild\n", getpid()))
  309: 
  310: 		/* close the pipe we don't use, since we inherited it and
  311: 		 * are part of its reference count now.
  312: 		 */
  313: 		close(stdout_pipe[READ_PIPE]);
  314: 
  315: 		/* translation:
  316: 		 *	\% -> %
  317: 		 *	%  -> \n
  318: 		 *	\x -> \x	for all x != %
  319: 		 */
  320: 		while ((ch = *input_data++)) {
  321: 			if (escaped) {
  322: 				if (ch != '%')
  323: 					putc('\\', out);
  324: 			} else {
  325: 				if (ch == '%')
  326: 					ch = '\n';
  327: 			}
  328: 
  329: 			if (!(escaped = (ch == '\\'))) {
  330: 				putc(ch, out);
  331: 				need_newline = (ch != '\n');
  332: 			}
  333: 		}
  334: 		if (escaped)
  335: 			putc('\\', out);
  336: 		if (need_newline)
  337: 			putc('\n', out);
  338: 
  339: 		/* close the pipe, causing an EOF condition.  fclose causes
  340: 		 * stdin_pipe[WRITE_PIPE] to be closed, too.
  341: 		 */
  342: 		fclose(out);
  343: 
  344: 		Debug(DPROC, ("[%d] child2 done sending to grandchild\n", getpid()))
  345: 		exit(0);
  346: 	}
  347: 
  348: 	/* close the pipe to the grandkiddie's stdin, since its wicked uncle
  349: 	 * ernie back there has it open and will close it when he's done.
  350: 	 */
  351: 	close(stdin_pipe[WRITE_PIPE]);
  352: 
  353: 	children++;
  354: 
  355: 	/*
  356: 	 * read output from the grandchild.  it's stderr has been redirected to
  357: 	 * it's stdout, which has been redirected to our pipe.  if there is any
  358: 	 * output, we'll be mailing it to the user whose crontab this is...
  359: 	 * when the grandchild exits, we'll get EOF.
  360: 	 */
  361: 
  362: 	Debug(DPROC, ("[%d] child reading output from grandchild\n", getpid()))
  363: 
  364: 	/*local*/{
  365: 		FILE *in;
  366: 		int ch;
  367: 
  368: 		in = fdopen(stdout_pipe[READ_PIPE], "r");
  369: 		if (in == NULL) {
  370: 			warn("fdopen failed in child");
  371: 			_exit(ERROR_EXIT);
  372: 		}
  373: 
  374: 		ch = getc(in);
  375: 		if (ch != EOF) {
  376: 			FILE *mail;
  377: 			int bytes = 1;
  378: 			int status = 0;
  379: 
  380: 			Debug(DPROC|DEXT,
  381: 				("[%d] got data (%x:%c) from grandchild\n",
  382: 					getpid(), ch, ch))
  383: 
  384: 			/* get name of recipient.  this is MAILTO if set to a
  385: 			 * valid local username; USER otherwise.
  386: 			 */
  387: 			if (mailto) {
  388: 				/* MAILTO was present in the environment
  389: 				 */
  390: 				if (!*mailto) {
  391: 					/* ... but it's empty. set to NULL
  392: 					 */
  393: 					mailto = NULL;
  394: 				}
  395: 			} else {
  396: 				/* MAILTO not present, set to USER.
  397: 				 */
  398: 				mailto = usernm;
  399: 			}
  400: 
  401: 			/* if we are supposed to be mailing, MAILTO will
  402: 			 * be non-NULL.  only in this case should we set
  403: 			 * up the mail command and subjects and stuff...
  404: 			 */
  405: 
  406: 			if (mailto) {
  407: 				char **env;
  408: 				char mailcmd[MAX_COMMAND];
  409: 				char hostname[MAXHOSTNAMELEN];
  410: 
  411: 				(void) gethostname(hostname, MAXHOSTNAMELEN);
  412: 				(void) snprintf(mailcmd, sizeof(mailcmd),
  413: 					       MAILARGS, MAILCMD);
  414: 				if (!(mail = cron_popen(mailcmd, "w", e))) {
  415: 					warn("%s", MAILCMD);
  416: 					(void) _exit(ERROR_EXIT);
  417: 				}
  418: 				fprintf(mail, "From: %s (Cron Daemon)\n", usernm);
  419: 				fprintf(mail, "To: %s\n", mailto);
  420: 				fprintf(mail, "Subject: Cron <%s@%s> %s\n",
  421: 					usernm, first_word(hostname, "."),
  422: 					e->cmd);
  423: # if defined(MAIL_DATE)
  424: 				fprintf(mail, "Date: %s\n",
  425: 					arpadate(&TargetTime));
  426: # endif /* MAIL_DATE */
  427: 				for (env = e->envp;  *env;  env++)
  428: 					fprintf(mail, "X-Cron-Env: <%s>\n",
  429: 						*env);
  430: 				fprintf(mail, "\n");
  431: 
  432: 				/* this was the first char from the pipe
  433: 				 */
  434: 				putc(ch, mail);
  435: 			}
  436: 
  437: 			/* we have to read the input pipe no matter whether
  438: 			 * we mail or not, but obviously we only write to
  439: 			 * mail pipe if we ARE mailing.
  440: 			 */
  441: 
  442: 			while (EOF != (ch = getc(in))) {
  443: 				bytes++;
  444: 				if (mailto)
  445: 					putc(ch, mail);
  446: 			}
  447: 
  448: 			/* only close pipe if we opened it -- i.e., we're
  449: 			 * mailing...
  450: 			 */
  451: 
  452: 			if (mailto) {
  453: 				Debug(DPROC, ("[%d] closing pipe to mail\n",
  454: 					getpid()))
  455: 				/* Note: the pclose will probably see
  456: 				 * the termination of the grandchild
  457: 				 * in addition to the mail process, since
  458: 				 * it (the grandchild) is likely to exit
  459: 				 * after closing its stdout.
  460: 				 */
  461: 				status = cron_pclose(mail);
  462: 			}
  463: 
  464: 			/* if there was output and we could not mail it,
  465: 			 * log the facts so the poor user can figure out
  466: 			 * what's going on.
  467: 			 */
  468: 			if (mailto && status) {
  469: 				char buf[MAX_TEMPSTR];
  470: 
  471: 				snprintf(buf, sizeof(buf),
  472: 			"mailed %d byte%s of output but got status 0x%04x\n",
  473: 					bytes, (bytes==1)?"":"s",
  474: 					status);
  475: 				log_it(usernm, getpid(), "MAIL", buf);
  476: 			}
  477: 
  478: 		} /*if data from grandchild*/
  479: 
  480: 		Debug(DPROC, ("[%d] got EOF from grandchild\n", getpid()))
  481: 
  482: 		fclose(in);	/* also closes stdout_pipe[READ_PIPE] */
  483: 	}
  484: 
  485: 	/* wait for children to die.
  486: 	 */
  487: 	for (;  children > 0;  children--)
  488: 	{
  489: 		WAIT_T		waiter;
  490: 		PID_T		pid;
  491: 
  492: 		Debug(DPROC, ("[%d] waiting for grandchild #%d to finish\n",
  493: 			getpid(), children))
  494: 		pid = wait(&waiter);
  495: 		if (pid < OK) {
  496: 			Debug(DPROC, ("[%d] no more grandchildren--mail written?\n",
  497: 				getpid()))
  498: 			break;
  499: 		}
  500: 		Debug(DPROC, ("[%d] grandchild #%d finished, status=%04x",
  501: 			getpid(), pid, WEXITSTATUS(waiter)))
  502: 		if (WIFSIGNALED(waiter) && WCOREDUMP(waiter))
  503: 			Debug(DPROC, (", dumped core"))
  504: 		Debug(DPROC, ("\n"))
  505: 	}
  506: }
  507: 
  508: 
  509: static void
  510: do_univ(user *u)
  511: {
  512: #if defined(sequent)
  513: /* Dynix (Sequent) hack to put the user associated with
  514:  * the passed user structure into the ATT universe if
  515:  * necessary.  We have to dig the gecos info out of
  516:  * the user's password entry to see if the magic
  517:  * "universe(att)" string is present.
  518:  */
  519: 
  520: 	struct	passwd	*p;
  521: 	char	*s;
  522: 	int	i;
  523: 
  524: 	p = getpwuid(u->uid);
  525: 	(void) endpwent();
  526: 
  527: 	if (p == NULL)
  528: 		return;
  529: 
  530: 	s = p->pw_gecos;
  531: 
  532: 	for (i = 0; i < 4; i++)
  533: 	{
  534: 		if ((s = strchr(s, ',')) == NULL)
  535: 			return;
  536: 		s++;
  537: 	}
  538: 	if (strcmp(s, "universe(att)"))
  539: 		return;
  540: 
  541: 	(void) universe(U_ATT);
  542: #endif
  543: }