job.h revision 1.24
11.24Sdsl/*	$NetBSD: job.h,v 1.24 2006/01/22 19:54:55 dsl Exp $	*/
21.4Schristos
31.1Scgd/*
41.1Scgd * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
51.20Sagc * All rights reserved.
61.20Sagc *
71.20Sagc * This code is derived from software contributed to Berkeley by
81.20Sagc * Adam de Boor.
91.20Sagc *
101.20Sagc * Redistribution and use in source and binary forms, with or without
111.20Sagc * modification, are permitted provided that the following conditions
121.20Sagc * are met:
131.20Sagc * 1. Redistributions of source code must retain the above copyright
141.20Sagc *    notice, this list of conditions and the following disclaimer.
151.20Sagc * 2. Redistributions in binary form must reproduce the above copyright
161.20Sagc *    notice, this list of conditions and the following disclaimer in the
171.20Sagc *    documentation and/or other materials provided with the distribution.
181.20Sagc * 3. Neither the name of the University nor the names of its contributors
191.20Sagc *    may be used to endorse or promote products derived from this software
201.20Sagc *    without specific prior written permission.
211.20Sagc *
221.20Sagc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
231.20Sagc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
241.20Sagc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
251.20Sagc * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
261.20Sagc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
271.20Sagc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
281.20Sagc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
291.20Sagc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
301.20Sagc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
311.20Sagc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
321.20Sagc * SUCH DAMAGE.
331.20Sagc *
341.20Sagc *	from: @(#)job.h	8.1 (Berkeley) 6/6/93
351.20Sagc */
361.20Sagc
371.20Sagc/*
381.1Scgd * Copyright (c) 1988, 1989 by Adam de Boor
391.1Scgd * Copyright (c) 1989 by Berkeley Softworks
401.1Scgd * All rights reserved.
411.1Scgd *
421.1Scgd * This code is derived from software contributed to Berkeley by
431.1Scgd * Adam de Boor.
441.1Scgd *
451.1Scgd * Redistribution and use in source and binary forms, with or without
461.1Scgd * modification, are permitted provided that the following conditions
471.1Scgd * are met:
481.1Scgd * 1. Redistributions of source code must retain the above copyright
491.1Scgd *    notice, this list of conditions and the following disclaimer.
501.1Scgd * 2. Redistributions in binary form must reproduce the above copyright
511.1Scgd *    notice, this list of conditions and the following disclaimer in the
521.1Scgd *    documentation and/or other materials provided with the distribution.
531.1Scgd * 3. All advertising materials mentioning features or use of this software
541.1Scgd *    must display the following acknowledgement:
551.1Scgd *	This product includes software developed by the University of
561.1Scgd *	California, Berkeley and its contributors.
571.1Scgd * 4. Neither the name of the University nor the names of its contributors
581.1Scgd *    may be used to endorse or promote products derived from this software
591.1Scgd *    without specific prior written permission.
601.1Scgd *
611.1Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
621.1Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
631.1Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
641.1Scgd * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
651.1Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
661.1Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
671.1Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
681.1Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
691.1Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
701.1Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
711.1Scgd * SUCH DAMAGE.
721.1Scgd *
731.5Schristos *	from: @(#)job.h	8.1 (Berkeley) 6/6/93
741.1Scgd */
751.1Scgd
761.1Scgd/*-
771.1Scgd * job.h --
781.1Scgd *	Definitions pertaining to the running of jobs in parallel mode.
791.1Scgd *	Exported from job.c for the use of remote-execution modules.
801.1Scgd */
811.1Scgd#ifndef _JOB_H_
821.1Scgd#define _JOB_H_
831.1Scgd
841.8Schristos#define TMPPAT	"/tmp/makeXXXXXX"
851.1Scgd
861.10Ssommerfe#ifdef USE_SELECT
871.1Scgd/*
881.17Sgson * Emulate poll() in terms of select().  This is not a complete
891.17Sgson * emulation but it is sufficient for make's purposes.
901.1Scgd */
911.17Sgson
921.17Sgson#define poll emul_poll
931.17Sgson#define pollfd emul_pollfd
941.17Sgson
951.17Sgsonstruct emul_pollfd {
961.17Sgson    int fd;
971.17Sgson    short events;
981.17Sgson    short revents;
991.17Sgson};
1001.17Sgson
1011.17Sgson#define	POLLIN		0x0001
1021.17Sgson#define	POLLOUT		0x0004
1031.17Sgson
1041.17Sgsonint
1051.17Sgsonemul_poll(struct pollfd *fd, int nfd, int timeout);
1061.17Sgson#endif
1071.17Sgson
1081.10Ssommerfe/*
1091.10Ssommerfe * The POLL_MSEC constant determines the maximum number of milliseconds spent
1101.10Ssommerfe * in poll before coming out to see if a child has finished.
1111.10Ssommerfe */
1121.10Ssommerfe#define POLL_MSEC	5000
1131.1Scgd
1141.1Scgd
1151.1Scgd/*-
1161.5Schristos * Job Table definitions.
1171.1Scgd *
1181.1Scgd * Each job has several things associated with it:
1191.1Scgd *	1) The process id of the child shell
1201.1Scgd *	2) The graph node describing the target being made by this job
1211.1Scgd *	3) A LstNode for the first command to be saved after the job
1221.1Scgd *	   completes. This is NILLNODE if there was no "..." in the job's
1231.1Scgd *	   commands.
1241.1Scgd *	4) An FILE* for writing out the commands. This is only
1251.1Scgd *	   used before the job is actually started.
1261.1Scgd *	5) A union of things used for handling the shell's output. Different
1271.1Scgd *	   parts of the union are used based on the value of the usePipes
1281.1Scgd *	   flag. If it is true, the output is being caught via a pipe and
1291.1Scgd *	   the descriptors of our pipe, an array in which output is line
1301.1Scgd *	   buffered and the current position in that buffer are all
1311.1Scgd *	   maintained for each job. If, on the other hand, usePipes is false,
1321.1Scgd *	   the output is routed to a temporary file and all that is kept
1331.1Scgd *	   is the name of the file and the descriptor open to the file.
1341.1Scgd *	6) An identifier provided by and for the exclusive use of the
1351.1Scgd *	   Rmt module.
1361.1Scgd *	7) A word of flags which determine how the module handles errors,
1371.5Schristos *	   echoing, etc. for the job
1381.1Scgd *
1391.1Scgd * The job "table" is kept as a linked Lst in 'jobs', with the number of
1401.1Scgd * active jobs maintained in the 'nJobs' variable. At no time will this
1411.5Schristos * exceed the value of 'maxJobs', initialized by the Job_Init function.
1421.1Scgd *
1431.1Scgd * When a job is finished, the Make_Update function is called on each of the
1441.1Scgd * parents of the node which was just remade. This takes care of the upward
1451.1Scgd * traversal of the dependency graph.
1461.1Scgd */
1471.9Schristos#ifndef RMT_WILL_WATCH
1481.9Schristosstruct pollfd;
1491.9Schristos#endif
1501.9Schristos
1511.1Scgd#define JOB_BUFSIZE	1024
1521.1Scgdtypedef struct Job {
1531.1Scgd    int       	pid;	    /* The child's process ID */
1541.1Scgd    GNode    	*node;      /* The target the child is making */
1551.1Scgd    LstNode 	tailCmds;   /* The node of the first command to be
1561.1Scgd			     * saved when the job has been run */
1571.1Scgd    FILE 	*cmdFILE;   /* When creating the shell script, this is
1581.1Scgd			     * where the commands go */
1591.3Scgd    int    	rmtID;     /* ID returned from Rmt module */
1601.1Scgd    short      	flags;	    /* Flags to control treatment of job */
1611.1Scgd#define	JOB_IGNERR	0x001	/* Ignore non-zero exits */
1621.1Scgd#define	JOB_SILENT	0x002	/* no output */
1631.1Scgd#define JOB_SPECIAL	0x004	/* Target is a special one. i.e. run it locally
1641.1Scgd				 * if we can't export it and maxLocal is 0 */
1651.1Scgd#define JOB_IGNDOTS	0x008  	/* Ignore "..." lines when processing
1661.1Scgd				 * commands */
1671.5Schristos#define JOB_REMOTE	0x010	/* Job is running remotely */
1681.1Scgd#define JOB_FIRST	0x020	/* Job is first job for the node */
1691.1Scgd#define JOB_REMIGRATE	0x040	/* Job needs to be remigrated */
1701.1Scgd#define JOB_RESTART	0x080	/* Job needs to be completely restarted */
1711.1Scgd#define JOB_RESUME	0x100	/* Job needs to be resumed b/c it stopped,
1721.1Scgd				 * for some reason */
1731.1Scgd#define JOB_CONTINUING	0x200	/* We are in the process of resuming this job.
1741.1Scgd				 * Used to avoid infinite recursion between
1751.1Scgd				 * JobFinish and JobRestart */
1761.15Ssjg#define JOB_TRACED	0x400	/* we've sent 'set -x' */
1771.15Ssjg
1781.1Scgd    union {
1791.1Scgd	struct {
1801.1Scgd	    int	  	op_inPipe;	/* Input side of pipe associated
1811.1Scgd					 * with job's output channel */
1821.9Schristos#ifndef RMT_WILL_WATCH
1831.9Schristos	    struct pollfd *op_inPollfd;	/* pollfd associated with inPipe */
1841.9Schristos#endif
1851.1Scgd	    int   	op_outPipe;	/* Output side of pipe associated with
1861.1Scgd					 * job's output channel */
1871.1Scgd	    char  	op_outBuf[JOB_BUFSIZE + 1];
1881.1Scgd	    	  	    	    	/* Buffer for storing the output of the
1891.1Scgd					 * job, line by line */
1901.1Scgd	    int   	op_curPos;	/* Current position in op_outBuf */
1911.1Scgd	}   	    o_pipe;	    /* data used when catching the output via
1921.1Scgd				     * a pipe */
1931.1Scgd	struct {
1941.1Scgd	    char  	of_outFile[sizeof(TMPPAT)+2];
1951.1Scgd	    	  	    	    	/* Name of file to which shell output
1961.1Scgd					 * was rerouted */
1971.1Scgd	    int	    	of_outFd;	/* Stream open to the output
1981.1Scgd					 * file. Used to funnel all
1991.1Scgd					 * from a single job to one file
2001.1Scgd					 * while still allowing
2011.1Scgd					 * multiple shell invocations */
2021.1Scgd	}   	    o_file;	    /* Data used when catching the output in
2031.1Scgd				     * a temporary file */
2041.1Scgd    }       	output;	    /* Data for tracking a shell's output */
2051.1Scgd} Job;
2061.1Scgd
2071.1Scgd#define outPipe	  	output.o_pipe.op_outPipe
2081.1Scgd#define inPipe	  	output.o_pipe.op_inPipe
2091.9Schristos#define inPollfd	output.o_pipe.op_inPollfd
2101.1Scgd#define outBuf		output.o_pipe.op_outBuf
2111.1Scgd#define curPos		output.o_pipe.op_curPos
2121.1Scgd#define outFile		output.o_file.of_outFile
2131.1Scgd#define outFd	  	output.o_file.of_outFd
2141.1Scgd
2151.1Scgd
2161.1Scgd/*-
2171.1Scgd * Shell Specifications:
2181.1Scgd * Each shell type has associated with it the following information:
2191.1Scgd *	1) The string which must match the last character of the shell name
2201.1Scgd *	   for the shell to be considered of this type. The longest match
2211.1Scgd *	   wins.
2221.1Scgd *	2) A command to issue to turn off echoing of command lines
2231.1Scgd *	3) A command to issue to turn echoing back on again
2241.1Scgd *	4) What the shell prints, and its length, when given the echo-off
2251.1Scgd *	   command. This line will not be printed when received from the shell
2261.1Scgd *	5) A boolean to tell if the shell has the ability to control
2271.1Scgd *	   error checking for individual commands.
2281.1Scgd *	6) The string to turn this checking on.
2291.1Scgd *	7) The string to turn it off.
2301.1Scgd *	8) The command-flag to give to cause the shell to start echoing
2311.1Scgd *	   commands right away.
2321.1Scgd *	9) The command-flag to cause the shell to Lib_Exit when an error is
2331.1Scgd *	   detected in one of the commands.
2341.1Scgd *
2351.1Scgd * Some special stuff goes on if a shell doesn't have error control. In such
2361.1Scgd * a case, errCheck becomes a printf template for echoing the command,
2371.1Scgd * should echoing be on and ignErr becomes another printf template for
2381.21Sjmc * executing the command while ignoring the return status. Finally errOut
2391.21Sjmc * is a printf template for running the command and causing the shell to
2401.21Sjmc * exit on error. If any of these strings are empty when hasErrCtl is FALSE,
2411.21Sjmc * the command will be executed anyway as is and if it causes an error, so be
2421.21Sjmc * it. Any templates setup to echo the command will escape any '$ ` \ "'i
2431.21Sjmc * characters in the command string to avoid common problems with
2441.21Sjmc * echo "%s\n" as a template.
2451.1Scgd */
2461.1Scgdtypedef struct Shell {
2471.18Schristos    const char	 *name;		/* the name of the shell. For Bourne and C
2481.1Scgd				 * shells, this is used only to find the
2491.1Scgd				 * shell description when used as the single
2501.1Scgd				 * source of a .SHELL target. For user-defined
2511.1Scgd				 * shells, this is the full path of the shell.
2521.1Scgd				 */
2531.1Scgd    Boolean 	  hasEchoCtl;	/* True if both echoOff and echoOn defined */
2541.18Schristos    const char   *echoOff;	/* command to turn off echo */
2551.18Schristos    const char   *echoOn;	/* command to turn it back on again */
2561.18Schristos    const char   *noPrint;	/* command to skip when printing output from
2571.1Scgd				 * shell. This is usually the command which
2581.1Scgd				 * was executed to turn off echoing */
2591.1Scgd    int           noPLen;	/* length of noPrint command */
2601.1Scgd    Boolean	  hasErrCtl;	/* set if can control error checking for
2611.1Scgd				 * individual commands */
2621.18Schristos    const char	 *errCheck;	/* string to turn error checking on */
2631.18Schristos    const char	 *ignErr;	/* string to turn off error checking */
2641.21Sjmc    const char	 *errOut;	/* string to use for testing exit code */
2651.21Sjmc    char   commentChar;		/* character used by shell for comment lines */
2661.21Sjmc
2671.1Scgd    /*
2681.5Schristos     * command-line flags
2691.1Scgd     */
2701.18Schristos    const char   *echo;		/* echo commands */
2711.18Schristos    const char   *exit;		/* exit on error */
2721.1Scgd}               Shell;
2731.1Scgd
2741.19Ssjgextern const char *shellPath;
2751.19Ssjgextern const char *shellName;
2761.19Ssjg
2771.11Ssommerfeextern int	job_pipe[2];	/* token pipe for jobs. */
2781.14Ssommerfeextern int	jobTokensRunning; /* tokens currently "out" */
2791.24Sdslextern int	not_parallel;	/* => only run one job */
2801.11Ssommerfe
2811.7Sdrochner#ifdef REMOTE
2821.1Scgdextern char 	*targFmt;   	/* Format string for banner that separates
2831.1Scgd				 * output from multiple jobs. Contains a
2841.1Scgd				 * single %s where the name of the node being
2851.1Scgd				 * made should be put. */
2861.1Scgdextern GNode	*lastNode;  	/* Last node for which a banner was printed.
2871.1Scgd				 * If Rmt module finds it necessary to print
2881.1Scgd				 * a banner, it should set this to the node
2891.1Scgd				 * for which the banner was printed */
2901.1Scgdextern int  	nJobs;	    	/* Number of jobs running (local and remote) */
2911.1Scgdextern int  	nLocal;	    	/* Number of jobs running locally */
2921.1Scgdextern Lst  	jobs;	    	/* List of active job descriptors */
2931.1Scgdextern Lst  	stoppedJobs;	/* List of jobs that are stopped or didn't
2941.1Scgd				 * quite get started */
2951.7Sdrochner#endif
2961.3Scgd
2971.19Ssjgvoid Shell_Init(void);
2981.16Swizvoid Job_Touch(GNode *, Boolean);
2991.18SchristosBoolean Job_CheckCommands(GNode *, void (*abortProc )(const char *, ...));
3001.16Swizvoid Job_CatchChildren(Boolean);
3011.16Swizvoid Job_CatchOutput(void);
3021.16Swizvoid Job_Make(GNode *);
3031.16Swizvoid Job_Init(int, int);
3041.16SwizBoolean Job_Full(void);
3051.16SwizBoolean Job_Empty(void);
3061.16SwizReturnStatus Job_ParseShell(char *);
3071.16Swizint Job_Finish(void);
3081.16Swizvoid Job_End(void);
3091.16Swizvoid Job_Wait(void);
3101.16Swizvoid Job_AbortAll(void);
3111.16Swizvoid JobFlagForMigration(int);
3121.16Swizvoid Job_TokenReturn(void);
3131.16SwizBoolean Job_TokenWithdraw(void);
3141.16Swizvoid Job_ServerStart(int);
3151.3Scgd
3161.1Scgd#endif /* _JOB_H_ */
317