/*
 * $Header: /smsa/bsd:include/sys/wait.h:bsd  1.13  $
 * $Source: /smsa/bsd:include/sys/wait.h: $
 *
 * Copyright (c) 1988 Acorn Computers Ltd., Cambridge, England
 * 
 */
/*	@(#)wait.h	2.3 88/05/26 4.0NFSSRC SMI;	*/
/*
 * Copyright (c) 1982, 1986 Regents of the University of California.
 * All rights reserved.  The Berkeley software License Agreement
 * specifies the terms and conditions for redistribution.
 *
 *	@(#)wait.h	7.1 (Berkeley) 6/4/86
 */

#ifndef	__sys_wait_h
#define	__sys_wait_h

/*
 * This file holds definitions relevent to the wait system call.
 * Some of the options here are available only through the ``wait3''
 * entry point; the old entry point with one argument has more fixed
 * semantics, never returning status of unstopped children, hanging until
 * a process terminates if any are outstanding, and never returns
 * detailed information about process resource utilization (<vtimes.h>).
 */

/*
 * Structure of the information in the first word returned by both
 * wait and wait3.  If w_stopval==WSTOPPED, then the second structure
 * describes the information returned, else the first.  See WUNTRACED below.
 */

#if defined (_BSD_C) || defined (_XOPEN_C)
#ifndef __sys_types_h
#include "types.h"
#endif
#endif

#if defined (_BSD_C)
union wait	{
	int	w_status;		/* used in syscall */
	/*
	 * Terminated process status.
	 */
	struct {
		unsigned short	w_Termsig:7;	/* termination signal */
		unsigned short	w_Coredump:1;	/* core dump indicator */
		unsigned short	w_Retcode:8;	/* exit code if w_termsig==0 */
	} w_T;
	/*
	 * Stopped process status.  Returned
	 * only for traced children unless requested
	 * with the WUNTRACED option bit.
	 */
	struct {
		unsigned short	w_Stopval:8;	/* == W_STOPPED if stopped */
		unsigned short	w_Stopsig:8;	/* signal that stopped us */
	} w_S;
};
#define	w_termsig	w_T.w_Termsig
#define w_coredump	w_T.w_Coredump
#define w_retcode	w_T.w_Retcode
#define w_stopval	w_S.w_Stopval
#define w_stopsig	w_S.w_Stopsig

#define	WSTOPPED	0177	/* value of s.stopval if process is stopped */
#endif

/*
 * Option bits for the second argument of wait3.  WNOHANG causes the
 * wait to not hang if there are no stopped or terminated processes, rather
 * returning an error indication in this case (pid==0).  WUNTRACED
 * indicates that the caller should receive status about untraced children
 * which stop due to signals.  If children are stopped and a wait without
 * this option is done, it is as though they were still running... nothing
 * about them is returned.
 */
#define WNOHANG		1	/* dont hang in wait */
#define WUNTRACED	2	/* tell about stopped, untraced children */

#if defined (_BSD_C)
#define WIFSTOPPED(x)	((*(union wait *)&(x)).w_stopval == WSTOPPED)
#define WIFSIGNALED(x)	(!WIFSTOPPED(x) && (*(union wait *)&(x)).w_termsig != 0)
#define WIFEXITED(x)	(!WIFSTOPPED(x) && (*(union wait *)&(x)).w_termsig == 0)
#define WEXITSTATUS(x)	(WIFEXITED(x) ? (*(union wait *)&(x)).w_retcode : 0)
#define WSTOPSIG(x)	(WIFSTOPPED(x) ? (*(union wait *)&(x)).w_stopsig : 0)
#define WTERMSIG(x)	(WIFSIGNALED(x) ? (*(union wait *)&(x)).w_termsig : 0)
#endif

#if defined (_XOPEN_C)
#define WIFSTOPPED(x)	(((x) & 0xff) == 0x7f)
#define WIFSIGNALED(x)	(!WIFSTOPPED(x) && ((x) & 0x7f) != 0)
#define WIFEXITED(x)	(!WIFSTOPPED(x) && ((x) & 0x7f) == 0)
#define WEXITSTATUS(x)	(WIFEXITED(x) ? (((x) >> 8) & 0xff) : 0)
#define WSTOPSIG(x)	(WIFSTOPPED(x) ? (((x) >> 8) & 0xff) : 0)
#define WTERMSIG(x)	(WIFSIGNALED(x) ? ((x) & 0x7f) : 0)
#endif

#ifdef	_KERNEL
/* 
 * Arguments to wait4() system call, included here so it may be called by
 * other routines in the kernel
 */
struct wait4_args {
	int pid;
	union wait *status;
	int options;
	struct rusage *rusage;
};
#else /* _KERNEL */
typedef struct rusage *__wait3arg2;	/* dummy to globalise struct tag rusage */
#if defined (_BSD_C)
extern int wait (union wait * /*status*/);
extern int wait3 (union wait * /*status*/, int /*options*/, struct rusage * /*rusage*/);
extern int waitpid (int /*pid_t pid*/, union wait * /*status*/, int /*options*/);
extern int wait4 (int /*pid_t pid*/, union wait * /*status*/, int /*options*/,
		  struct rusage * /*rusage*/);
#endif
#if defined (_XOPEN_C)
extern pid_t wait (int * /*status*/);
extern pid_t waitpid (int /*pid_t pid*/, int * /*status*/, int /*options*/);
#endif

#endif  /* _KERNEL */

#endif/*__sys_wait_h*/

/* EOF wait.h */
