1adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
2adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *
3adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *   Copyright (c) International Business Machines  Corp., 2001
4adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *
5adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *   This program is free software;  you can redistribute it and/or modify
6adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *   it under the terms of the GNU General Public License as published by
7adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *   the Free Software Foundation; either version 2 of the License, or
8adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *   (at your option) any later version.
9adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *
10adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *   This program is distributed in the hope that it will be useful,
11adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *   the GNU General Public License for more details.
14adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *
15adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *   You should have received a copy of the GNU General Public License
16adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *   along with this program;  if not, write to the Free Software
174548c6cf9bcdd96d8303caa4130ab638b61f8a30Wanlong Gao *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
19adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
206c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper#include <sys/mman.h>
21b832005393fde0ee8df5fa6c8aa71105971ff61arobbiew#include <ctype.h>
22adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <errno.h>
23adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <signal.h>
24adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <stdarg.h>
25adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <stdio.h>
26adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <stdlib.h>
27adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <string.h>
28adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <time.h>
29adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <unistd.h>
30adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
314bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak#ifndef _LINUX
32adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			/* LINUX INCLUDES */
33adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/mode.h>
34adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/timers.h>
35adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
36adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/stat.h>
37adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <time.h>
38adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/time.h>
39adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/ipc.h>
40adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
41adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/msg.h>
42adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/resource.h>
43adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/select.h>
44adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/sem.h>
45adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/shm.h>
46adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/types.h>
47adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#include <sys/wait.h>
48b681672752634a70ba1bd6b1c6b5ae2967ac13e5Stanislav Kholmanskikh#include "lapi/semun.h"
49adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
50adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/* indexes into environment variable array */
51adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define ADBG 0
52adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define BNDX 1
53adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define DNDX 2
54adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define TNDX 3
55adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define MAXBVAL 70
56adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define MAXDVAL 11
57adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define SLOTDIR "./slot/"
58adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
59adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef _LINUX
60adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			/* LINUX #defnes */
61adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifndef TRUE
62adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define TRUE 1
63adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
64adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifndef FALSE
65adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define FALSE 0
66adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
67adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
68adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
69adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#if defined _LINUX && defined DEBUG
70adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define prtln()	printf("At line number: %d\n", __LINE__); \
71adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		fflush(NULL)
7240fbd7139dab2bba62034224e66c92cf6ea00f82Markos Chandras#define dprt(fmt, args...) printf(fmt, ## args)
73adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
74adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define prtln()
754bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak#define dprt(fmt, args...)
76adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
77adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
78adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/* aliases for environment variable entries */
79adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define    AUSDEBUG  (*edat[ADBG].eval.vint)	/* debug value */
80adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define    BVAL  (*edat[BNDX].eval.vint)	/* # of childern per parent */
81adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define    DVAL  (*edat[DNDX].eval.vint)	/* depth of process tree */
82adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define    TVAL  (*edat[TNDX].eval.vint)	/* timer value */
83adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
84adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef _LINUX
85354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaotypedef long mtyp_t;
86adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
87adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
88adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/* structure of information stored about each process in shared memory */
89adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojtypedef struct proc_info {
90adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef __64LDT__
91354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	pid_t pid;		/* process id */
92354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	pid_t ppid;		/* parent process id */
93adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
94adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int pid;		/* process id */
95adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int ppid;		/* parent process id */
96adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
97adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int msg;		/* parent process id */
98adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int err;		/* error indicator */
99adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int *list;		/* pointer to list of parent and sibling slot locations */
100adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj} Pinfo;
101adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
102adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojtypedef struct messagebuf {
103354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	mtyp_t mtyp;		/* message type */
104354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	char mtext[80];		/* message text */
105adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj} Msgbuf;
106adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
107b681672752634a70ba1bd6b1c6b5ae2967ac13e5Stanislav Kholmanskikhunion semun semarg;
108adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
109adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/* structure of all environment variable used by program */
1104bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modakstruct envstruct {
111adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	char *env_name;
112adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	union {
113adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		char *chptr;
114354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		int *vint;
115adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	} eval;
116adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj} envdata[] = {
117354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	{
118354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		"AUSDBG", {
119354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	"0"}}, {
120354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		"BVAL", {
121354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	"3"}}, {
122354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		"DVAL", {
123354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	"2"}}, {
124354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		"FORCE", {
125354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	"0"}}, {
126354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		"TVAL", {
127354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	"1"}}, {
128354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		"", {
129354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	""}}
130adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj};
131adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
132354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaochar *errfile;			/* pointer to errfile name */
133adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
134354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint msgid;			/* message queue for leaf nodes */
135354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint msgerr;			/* message queue for errors */
136354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint nodesum;			/* total number of process to be created */
137354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint sem_count;			/* counter semaphore */
138354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint sem_lock;			/* locks access to counter semaphore */
139354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint shmid;			/* global shared memory id varible */
140354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint procgrp;			/* process group id */
141adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
142354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaotimer_t timer;			/* timer structure */
143adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
144354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong GaoPinfo *shmaddr;			/* Start address  of shared memory */
145adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
146adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifndef _LINUX
147adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojFILE *errfp = stderr;		/* error file pointer, probably not necessary */
148adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojFILE *debugfp = stderr;		/* debug file pointer, used if AUSDEBUG set */
149adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
150adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define errfp stderr
151adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#define debugfp stderr
152adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
153adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
154adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojstruct envstruct *edat = envdata;	/* pointer to environment data */
155adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
156adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/* external function declarations */
157354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoextern int killpg(int procgrp, int sig);
158adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojextern timer_t gettimerid(int Timer_type, int Notify_type);
159354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoextern int reltimerid(timer_t timer);
160adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
161adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/* internal function declarations */
162354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid cleanup(int sig, int code, struct sigcontext *scp);
163354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid nextofkin(int sig, int code, struct sigcontext *scp);
164354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid doit(void);
165354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid debugout(char *fmt, ...);
166354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint getenv_val(void);
167354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid messenger(void);
168354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid nextofkin(int sig, int code, struct sigcontext *scp);
169354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint notify(int slot);
170354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid parse_args(int argc, char *argv[]);
171354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid print_shm(void);
172354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong GaoPinfo *put_proc_info(int tval);
173354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid rm_msgqueue(void);
174354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid rm_semseg(void);
175354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid rm_shmseg(void);
176354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint semoper(int slot, int smid, int opval);
177354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint send_message(int id, mtyp_t type, char *text);
178354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid set_timer(void);
179354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid set_signals(void *sighandler());
180354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid setup_msgqueue(void);
181354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid setup_semaphores(void);
182354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid setup_shm(void);
183354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid severe(char *fmt, ...);
184354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong GaoPinfo *shmgetseg(void);
185354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaoint spawn(int val);
186354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaounsigned long sumit(int B, int D);
187adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
188adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
189adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  Prints out the data structures in shared memory.
190adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
191adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid print_shm(void)
192adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
193354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	extern int nodesum;	/* total number of nodes created */
194354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	extern Pinfo *shmaddr;	/* shared memory pointer */
195354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	extern int shmid;	/* shared memory id */
196adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
197354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	Pinfo *pinfo;		/* pointer to process info in shared memory */
198354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int *listp;		/* pointer to sibling info in shared memory */
199354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int i, j;		/* counters */
200adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	struct shmid_ds buf;
201adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
202adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (shmctl(shmid, IPC_STAT, &buf))
203adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		return;
204adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
205adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	for (pinfo = shmaddr, i = 0; i < nodesum; i++, pinfo++) {
206adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		fprintf(errfp,
207354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			"slot: %-4d pid: %-6d ppid: %-6d msg: %-2d err: %-2d lst:",
208354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			i, pinfo->pid, pinfo->ppid, pinfo->msg, pinfo->err);
209adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		for (j = 0, listp = pinfo->list; j < BVAL; j++, listp++)
210adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			fprintf(errfp, " %d", *listp);
211354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(errfp, "\n");
212adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
213adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
214adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
215adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
216adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  Generalized send routine.  Sends a message on message queue.
217adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
218adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojint send_message(int id, mtyp_t type, char *text)
219adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
220adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int rc;
221adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
222adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	Msgbuf sndbuf;
223adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
224adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	strcpy(sndbuf.mtext, text);
225adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	sndbuf.mtyp = type;
226df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper	while (TRUE) {
227adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		rc = msgsnd(id, &sndbuf, sizeof(struct messagebuf), IPC_NOWAIT);
2288fb1cdb0538640f295691929650408688537fb7fGarrett Cooper		if (rc == -1 && errno == EAGAIN) {
229354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			debugout("msgqueue %d of mtyp %d not ready to send\n",
230354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				 msgid, type);
231adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			errno = 0;
232354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		} else
233354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			return (rc);
234adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
235adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
236adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
237adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
238adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  Sends error message to initial parent (messenger).i
239adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
240adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid severe(char *fmt, ...)
241adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
242adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	va_list args;
243adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int rc;
244adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	char mtext[80];
245adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int msgerr;
246adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
247adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	va_start(args, fmt);
248adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	vsprintf(mtext, fmt, args);
249adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	va_end(args);
250adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
251adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	rc = send_message(msgerr, 2, mtext);
252adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (rc == -1) {
253adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("cannot send message to msgerr");
254adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		exit(1);
255adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
256adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
257adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
258adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
259adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  if AUSDEBUG set will print information to file associated with slot number.
260adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
261adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid debugout(char *fmt, ...)
262adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
263adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	va_list args;
264adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
265adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (AUSDEBUG) {
266adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		va_start(args, fmt);
267adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		vfprintf(debugfp, fmt, args);
268adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		va_end(args);
269adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
270adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
271adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
272adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
273adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  Remove message queues.
274adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
275adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid rm_msgqueue(void)
276adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
277adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int msgid;
278adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
279adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* remove message queue id. */
280adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (msgctl(msgid, IPC_RMID, NULL) && errno != EINVAL) {
281adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		fprintf(errfp, "msgctl failed msgid: errno %d\n", errno);
282adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("msgctl failed");
283adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
284adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
285adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* remove message queue id. */
286adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (msgctl(msgerr, IPC_RMID, NULL) && errno != EINVAL) {
287adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		fprintf(errfp, "msgctl failed msgerr: errno %d\n", errno);
288adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("msgctl failed");
289adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
290adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
291adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
292adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
293adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  Remove shared memory segment.
294adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
295adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid rm_shmseg(void)
296adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
297354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	extern int shmid;	/* Global shared memory id */
298adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern Pinfo *shmaddr;	/* Global shared memory address */
299adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
300adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* remove shared memory id (and shared memory segment). */
301adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (shmctl(shmid, IPC_RMID, NULL) && errno != EINVAL) {
302adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		fprintf(errfp, "shmctl failed: errno %d\n", errno);
303adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("shmctl failed");
304adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
305adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
306adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
307adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
308adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  Remove semaphores.
309adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
310adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid rm_semseg(void)
311adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
312adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int sem_lock;
313adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int sem_count;
314adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
315adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* remove sem_lock semaphore id */
316354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	semarg.val = 0;		/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
317d3e790c773507320805b650cb541972b0bdc6691robbiew	if (semctl(sem_lock, 0, IPC_RMID, semarg.val) && errno != EINVAL) {
318adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		fprintf(errfp, "semctl failed: errno %d\n", errno);
319adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("semctl failed");
320adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
321adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* remove sem_count semaphore id. */
322354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	semarg.val = 0;		/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
323d3e790c773507320805b650cb541972b0bdc6691robbiew	if (semctl(sem_count, 0, IPC_RMID, semarg.val) && errno != EINVAL) {
324adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		fprintf(errfp, "semctl failed: errno %d\n", errno);
325adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("semctl failed");
326adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
327adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
328adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
329adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
330adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * Routine to clean up shared memory and return exit status (CHILD handler).
331adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
332354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid cleanup(int sig, int code, struct sigcontext *scp)
333adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
334adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int rc;
335adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	char mtext[80];
336adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
337adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	killpg(procgrp, SIGTERM);
338adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	sprintf(mtext, "%d", sig);
339adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	rc = send_message(msgerr, 3, mtext);
340adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (rc == -1) {
341adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		severe("msgsnd failed: %d msgid %d mtyp %d mtext %d\n",
342354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		       errno, msgerr, 3, mtext);
343adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
344adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
345adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
346adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
347adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * Routine to clean up shared memory and return exit status (PARENT handler).
348adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
349354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid nextofkin(int sig, int code, struct sigcontext *scp)
350adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
351adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int rc;
352adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	char mtext[80];
353adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
354adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	sprintf(mtext, "%d", sig);
355adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	rc = send_message(msgerr, 3, mtext);
356adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (rc == -1) {
357adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		severe("msgsnd failed: %d msgid %d mtyp %d mtext %d\n",
358354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		       errno, msgerr, 3, mtext);
359adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
360adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifndef _LINUX
361354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	reltimerid(timer);
362adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
363adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	exit(1);
364adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
365adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
366adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/* given breadth and depth of a tree, sum up total number of nodes created */
367adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojunsigned long sumit(int B, int D)
368adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
36989af32a63ce8a780ea39337339e14caae244b5a4Cyril Hrubis	int i;
370354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int exp = 1;		/* exponent of breadth */
371adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	unsigned long sum = 1;	/* running sum of nodes */
372adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
373adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	for (sum = 1, i = 1; i <= D; i++) {
374adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		exp = B * exp;
375354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		sum += (int)exp;
376adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
377354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	return (sum);
378adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
379adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
380adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/* Finds correct slot for current process in shared memory and stores
381adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * information about process in it.
382adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
383354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong GaoPinfo *put_proc_info(int tval)
384adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
385adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int nodesum;
386adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern Pinfo *shmaddr;
387adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
388adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int sibslot = 0;	/* sibling slot number */
389adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int *listp;		/* ptr to sibling info for current proc */
390adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	Pinfo *smp;		/* ptr to current process data slot */
391adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
392adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	smp = shmaddr + tval;
393354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	smp->pid = getpid();
394354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	smp->ppid = getppid();
395354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	smp->err = 0;
396354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	smp->msg = 0;
397adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
398adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* if very first process (slot 0), dont fill in info about siblings
399adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	 *  and parent.  Sibling and parent info is irrevelant in this case.
400adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	 */
401adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (!tval)
402354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		return (smp);
403adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
404adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* find parent of current process and store slot location */
405354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	smp->list = (int *)(Pinfo *) (shmaddr + nodesum) + (BVAL * tval);
406354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	*smp->list = (tval - 1) / BVAL;
407adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	listp = smp->list + 1;
408adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
409adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* calculate and store sibling slot numbers of current process */
410354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	for (sibslot = *smp->list * BVAL + 1; listp < smp->list + BVAL;
411354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	     sibslot++) {
412df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper		if (tval != sibslot)
413adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			*(listp++) = sibslot;
414adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
415354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	return (smp);
416adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
417adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
418adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/* This routine sends a message from the current process to all of her
419adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * siblings and then waits to receive responses from them.  A timer is
420adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * set so that if a message is lost or not received for some reason
421adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * we can exit gracefully.
422adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
423adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojint notify(int slot)
424adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
425adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int msgid;
426adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern Pinfo *shmaddr;
427adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
428adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int i;
429adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int rc;
430adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int tslot;
431adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int *listp = (shmaddr + slot)->list;
432adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int cldcnt = 1;
433adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int ndx = 0;
434adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef __64LDT__
435354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	pid_t pid = 0;
436adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
437354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int pid = 0;
438adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
439354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	char mtext[80];
440adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
441adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	Msgbuf rcvbuf;
442adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
443adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	for (i = 1, listp++; i < BVAL; i++, listp++) {
444354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		sprintf(mtext, "%d %d %d", i, slot, (shmaddr + slot)->pid);
445354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		rc = send_message(msgid, (mtyp_t) * listp, mtext);
446adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		if (rc == -1) {
447354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			severe
448354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			    ("notify: send_message Failed: %d msgid %d mtyp %d mtext %d\n",
449354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			     errno, msgid, *listp, mtext);
450adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			exit(1);
451adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
452adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
453bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
454adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	while (cldcnt < BVAL) {
455354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		rc = msgrcv(msgid, &rcvbuf, sizeof(struct messagebuf), slot, 0);
456adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		if (rc == -1) {
457adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			switch (errno) {
458adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			case EAGAIN:
459354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				printf("msgqueue %d not ready to receive\n",
460354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				       msgid);
461adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				fflush(stdout);
462adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				errno = 0;
463adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				break;
464adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			case ENOMSG:
465adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				printf("msgqueue %d no message\n", msgid);
466adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				fflush(stdout);
467adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				errno = 0;
468adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				break;
469adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			default:
470adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				perror("msgrcv failed");
471adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				severe("msgrcv failed, errno: %d\n", errno);
472adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				exit(1);
473adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			}
474354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		} else {
475354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			sscanf(rcvbuf.mtext, "%d %d %d", &ndx, &tslot, &pid);
476adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			if (*((shmaddr + tslot)->list + ndx) == slot &&
477354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			    (shmaddr + tslot)->pid == pid) {
478354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				debugout
479354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				    ("MSGRCV:slot: %d ndx: %d tslot: %d pid: %d\n",
480354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				     slot, ndx, tslot, pid);
481adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				(shmaddr + slot)->msg++;
482adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				cldcnt++;
483354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			} else {
484adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				(shmaddr + slot)->err--;
485354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				debugout
486354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				    ("MSGRCV: slot: %d ndx: %d tslot: %d pid: %d\n",
487354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				     slot, ndx, tslot, pid);
488adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			}
489adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
490adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
491b832005393fde0ee8df5fa6c8aa71105971ff61arobbiew	return 0;
492adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
493adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
494adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
495adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * Calculates semaphore number and sets semaphore (lock).
496adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
4974bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modakint semoper(int slot, int smid, int opval)
498adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
499354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int pslot;		/* parent slot */
500354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	struct sembuf smop;	/* semaphore operator */
501adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
502354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	pslot = (slot - 1) / BVAL;	/* calculate parent node */
503adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	smop.sem_num = pslot;
504adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	smop.sem_op = opval;
505adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	smop.sem_flg = 0;
506adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	semop(smid, &smop, 1);
507354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	return (pslot);
508adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
509adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
510adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
511adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * This is the meat and potatoes of the program.  Spawn creates a tree
512adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * of processes with Dval depth and Bval breadth.  Each parent will spawn
513adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * Bval children.  Each child will store information about themselves
514adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * in shared memory.  The leaf nodes will communicate the existence
515adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * of one another through message queues, once each leaf node has
516adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * received communication from all of her siblings she will reduce
517adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * the semaphore count and exit.  Meanwhile all parents are waiting
518adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * to hear from their children through the use of semaphores.  When
5194bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * the semaphore count reaches zero then the parent knows all the
520adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * children have talked to one another.  Locking of the connter semaphore
5214bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak * is provided by the use of another (binary) semaphore.
522adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
523adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojint spawn(int val)
524adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
525adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int sem_count;	/* used to keep track of childern */
526adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int sem_lock;	/* used to lock access to sem_count semaphore */
527adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
528354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int i;			/* Breadth counter */
529adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	static int level = 0;	/* level counter */
530354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int lvlflg = 0;		/* level toggle, limits parental spawning
531354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				   to one generation */
532354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int pslot = 0;
533adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef __64LDT__
534354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	pid_t pid;		/* pid of child process */
535adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
536354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int pid;		/* pid of child process */
537adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
538354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	Pinfo *pinfo;		/* pointer to process information in shared mem */
539354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int semval;		/* value of semaphore ( equals BVAL initially */
540adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	static int tval = 1;	/* tree node value of child. */
541adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
542adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	char foo[1024];
543adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
544354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	level++;
545adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
546354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	for (i = 1; i <= BVAL; i++) {
547354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		tval = (val * BVAL) + i;
548354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if (!lvlflg) {
549adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			pid = fork();
550354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			if (!pid) {	/* CHILD */
551adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				if (AUSDEBUG) {
552adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					sprintf(foo, "%sslot%d", SLOTDIR, tval);
553adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					debugfp = fopen(foo, "a+");
554adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				}
555adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				pinfo = put_proc_info(tval);
556adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
557354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				debugout
558354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				    ("pid: %-6d ppid: %-6d lev: %-2d i: %-2d val: %-3d\n",
559354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				     pinfo->pid, pinfo->ppid, level, i, tval);
560adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
561adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				set_timer();	/* set up signal handlers and initialize pgrp */
5628fb1cdb0538640f295691929650408688537fb7fGarrett Cooper				if (level < DVAL) {
5638fb1cdb0538640f295691929650408688537fb7fGarrett Cooper					if (spawn(tval) == -1) {
564354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						pslot =
565354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						    semoper(tval, sem_lock, -1);
566354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						semarg.val = 0;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
567354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						semval =
568354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						    semctl(sem_count, pslot,
569354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao							   GETVAL, semarg);
570354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						semarg.val = --semval;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
571354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						semctl(sem_count, pslot, SETVAL,
572354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						       semarg);
573354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						semarg.val = 1;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
574354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						semctl(sem_lock, pslot, SETVAL,
575354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						       semarg);
576adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					}
577adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					lvlflg++;
578354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				} else {	/* leaf node */
579adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					notify(tval);
580354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					return (-1);
581adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				}
582adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			}
583adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef __64LDT__
584354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			else if (pid > 0 && i >= BVAL) {	/* PARENT */
585adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
586354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			else if (pid > (pid_t) 0 && i >= BVAL) {	/* PARENT */
587adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
588354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				pslot = semoper(tval, sem_count, 0);
589adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				pslot = semoper(pslot, sem_lock, -1);
590354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				semarg.val = 0;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
591354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				semval =
592354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				    semctl(sem_count, pslot, GETVAL, semarg);
593354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				semarg.val = --semval;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
5944bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak				semctl(sem_count, pslot, SETVAL, semarg);
595354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				semarg.val = 1;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
5964bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak				semctl(sem_lock, pslot, SETVAL, semarg);
597354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				(shmaddr + val)->msg++;
598adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			}
599adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef __64LDT__
600354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			else if (pid < (pid_t) 0) {
601adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
602354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			else if (pid < 0) {
603adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
604adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				perror("spawn: fork failed");
605354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				severe
606354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				    ("spawn: fork failed, exiting with errno %d\n",
607354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				     errno);
608adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				exit(1);
609354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			} else
610354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				(shmaddr + val)->msg++;
611adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
612354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	}
613354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	return (pslot);
614adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
615adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
616adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
617adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * Allocate message queues.
618adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
619adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid setup_msgqueue(void)
620adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
621adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int msgid;
622adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int msgerr;
623adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
624adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	msgid = msgget(IPC_PRIVATE,
625354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		       IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP |
626354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		       S_IWGRP);
6278fb1cdb0538640f295691929650408688537fb7fGarrett Cooper	if (msgid == -1) {
628adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("msgget msgid failed");
629354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, " SEVERE : msgget msgid failed: errno %d\n",
630354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			errno);
6316c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper		exit(1);
632adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
633adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
634adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	msgerr = msgget(IPC_PRIVATE,
635354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP |
636354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			S_IWGRP);
6378fb1cdb0538640f295691929650408688537fb7fGarrett Cooper	if (msgerr == -1) {
638adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("msgget msgerr failed");
639354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, " SEVERE : msgget msgerr failed: errno %d\n",
640354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			errno);
6416c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper		exit(1);
642adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
643adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
644adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
645adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
646adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * Set up and initialize all semaphores
647adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
648adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid setup_semaphores(void)
649adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
650adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int sem_count;
651adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int sem_lock;
652adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
653adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int i;
654adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int rc;
655adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
656354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	prtln();
657354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	sem_lock = semget(IPC_PRIVATE, nodesum - 1,
658354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			  IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP |
659354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			  S_IWGRP);
660354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	dprt("nodesum = %d, sem_lock = %d\n", nodesum, sem_lock);
661adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
662354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	prtln();
663adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (sem_lock == -1) {
664adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("semget failed for sem_lock");
665354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr,
666354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			" SEVERE : semget failed for sem_lock, errno: %d\n",
667354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			errno);
668adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		rm_shmseg();
6696c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper		exit(1);
670adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
671adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
672354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	prtln();
673adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	sem_count = semget(IPC_PRIVATE, nodesum - 1,
674354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			   IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP |
675354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			   S_IWGRP);
676adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
677adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (sem_count == -1) {
678adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("semget failed for sem_count");
679354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr,
680354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			" SEVERE : semget failed for sem_count, errno: %d\n",
681354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			errno);
682adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		rm_shmseg();
6836c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper		exit(1);
684adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
685354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	prtln();
686adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
687adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	for (i = 0; i < (nodesum - 1); i++) {
688354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		semarg.val = 1;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
689adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		rc = semctl(sem_lock, i, SETVAL, semarg);
690adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		prtln();
691adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		if (rc == -1) {
692adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			perror("semctl failed for sem_lock failed");
693354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			fprintf(stderr,
694354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				" SEVERE : semctl failed for sem_lock, errno: %d\n",
695354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				errno);
696adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			rm_shmseg();
6976c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper			exit(1);
698adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
699adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
700354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		semarg.val = BVAL;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
701adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		rc = semctl(sem_count, i, SETVAL, semarg);
702adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		prtln();
703adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		if (rc == -1) {
704adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			perror("semctl failed for sem_lock failed");
705354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			fprintf(stderr,
706354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				" SEVERE : semctl failed for sem_lock, errno: %d\n",
707354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				errno);
708adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			rm_shmseg();
7096c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper			exit(1);
710adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
711adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
712adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
713adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
714adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
715adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * Set up and allocate shared memory.
716adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
717adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid setup_shm(void)
718adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
719354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	extern int nodesum;	/* global shared memory id */
720354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	extern int shmid;	/* global shared memory id */
721adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern Pinfo *shmaddr;
722adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
723354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int i, j;		/* counters */
724354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	Pinfo *shmad = NULL;	/* ptr to start of shared memory. */
725354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	Pinfo *pinfo = NULL;	/* ptr to struct in shared memory. */
726adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
727adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	debugout("size = %d, size (in hex) =  %#x  nodes: %d\n",
728354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		 sizeof(Pinfo) * nodesum + (nodesum * BVAL * sizeof(int)),
729354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		 sizeof(Pinfo) * nodesum + (nodesum * BVAL * sizeof(int)),
730354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		 nodesum);
731adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
732adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* Get shared memory id */
7334bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak	shmid = shmget(IPC_PRIVATE,
734adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		       sizeof(Pinfo) * nodesum + (nodesum * BVAL * sizeof(int)),
735354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		       IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP |
736354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		       S_IWGRP);
737adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (shmid < 0) {
738adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("shmget failed");
739354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, " SEVERE : shmget failed: errno %d\n", errno);
7406c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper		exit(1);
741adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
742adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
743adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* allocate shared memory */
744adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
745d2db48030ce485af049d78a1690afc6df99bb689Cyril Hrubis	if ((shmad = shmat(shmid, (char *)shmad, 0)) == MAP_FAILED) {
746454482bcda44abd6bf1bc382bd7abc4ff729e329subrata_modak		printf("SEVERE : shmat failed\n");
7476c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper		exit(1);
748354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	} else {
749454482bcda44abd6bf1bc382bd7abc4ff729e329subrata_modak		shmctl(shmid, IPC_RMID, NULL);
750454482bcda44abd6bf1bc382bd7abc4ff729e329subrata_modak	}
751adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
752adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* set all fields in shared memory to -1 */
753adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	for (pinfo = shmad, i = 0; i < nodesum; i++, pinfo++) {
754adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef __64LDT__
755354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		pinfo->pid = (pid_t) - 1;
756354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		pinfo->ppid = (pid_t) - 1;
757adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
758354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		pinfo->pid = -1;
759354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		pinfo->ppid = -1;
760adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
761adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		pinfo->msg = -1;
762adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		pinfo->err = -1;
763adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
764adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		/* Changed 10/9/97 */
765354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		/* pinfo->list = (int *)((ulong)shmad + nodesum * sizeof(Pinfo)
766adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		   + (sizeof(int) * BVAL * i)); */
767354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		pinfo->list =
768354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		    (int *)((long)shmad + nodesum * sizeof(Pinfo) +
769354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			    (sizeof(int) * BVAL * i));
770adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		for (j = 0; j < BVAL; j++)
771adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			*(pinfo->list + j) = -1;
772adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
773adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	shmaddr = shmad;
774adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
775adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
776adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
777adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * Set up Signal handler and which signals to catch
778adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
779adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid set_signals(void *sighandler())
780adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
781adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int i;
782adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int rc;
783adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
784354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	struct sigaction action;
785adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
786354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	/* list of signals we want to catch */
787adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	static struct signalinfo {
788adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		int signum;
789adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		char *signame;
790adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	} siginfo[] = {
791354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		{
792354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		SIGHUP, "SIGHUP"}, {
793354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		SIGINT, "SIGINT"}, {
794354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		SIGQUIT, "SIGQUIT"}, {
795354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		SIGABRT, "SIGABRT"}, {
796354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		SIGBUS, "SIGBUS"}, {
797354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		SIGSEGV, "SIGSEGV"}, {
798354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		SIGALRM, "SIGALRM"}, {
799354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		SIGUSR1, "SIGUSR1"}, {
800354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		SIGUSR2, "SIGUSR2"}, {
801354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		-1, "ENDSIG"}
802b832005393fde0ee8df5fa6c8aa71105971ff61arobbiew	};
803adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
804adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	char tmpstr[1024];
805adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
806354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	action.sa_handler = (void *)sighandler;
807adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
808adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef _LINUX
809adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	sigfillset(&action.sa_mask);
810adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
811354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	SIGINITSET(action.sa_mask);
812adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
813354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	action.sa_flags = 0;
814adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
815adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/* Set the signal handler up */
816adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef _LINUX
817adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	sigaddset(&action.sa_mask, SIGTERM);
818adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
819354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	SIGADDSET(action.sa_mask, SIGTERM);
820adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
821adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	for (i = 0; siginfo[i].signum != -1; i++) {
822adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef _LINUX
823adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		sigaddset(&action.sa_mask, siginfo[i].signum);
824adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
825adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		SIGADDSET(action.sa_mask, siginfo[i].signum);
826adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
827df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper		rc = sigaction(siginfo[i].signum, &action, NULL);
828adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		if (rc == -1) {
829adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			sprintf(tmpstr, "sigaction: %s\n", siginfo[i].signame);
830adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			perror(tmpstr);
831354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			fprintf(stderr,
832354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				" SEVERE : Could not set %s signal action, errno=%d.",
833354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				siginfo[i].signame, errno);
8346c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper			exit(1);
835adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
836adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
837adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
838adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
839adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
840adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj* Get and set a timer for current process.
841adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj*/
842adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifndef _LINUX
843adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid set_timer(void)
844adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
845354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	struct itimerstruc_t itimer, old_itimer;
846adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
847354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if ((timer = gettimerid(TIMERID_REAL, DELIVERY_SIGNALS)) == -1) {
848354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		perror("gettimerid");
849354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, " SEVERE : Could not get timer id, errno=%d.",
850354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			errno);
8516c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper		exit(1);
852adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
853adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
854adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/*
855adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	 * Start the timer.
856adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	 */
857adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	itimer.it_interval.tv_nsec = 0;
858adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	itimer.it_interval.tv_sec = 0;
859adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	itimer.it_value.tv_nsec = 0;
860354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	itimer.it_value.tv_sec = (time_t) (TVAL * 60.0);
861adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (incinterval(timer, &itimer, &old_itimer) == -1) {
862354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		perror("incinterval");
863354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr,
864354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			" SEVERE : Could not set timer interval, errno=%d.",
865354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			errno);
866354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		(void)reltimerid(timer);
8676c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper		exit(1);
868adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
869adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
870adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
871adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
872adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid set_timer(void)
873adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
874b832005393fde0ee8df5fa6c8aa71105971ff61arobbiew	struct itimerval itimer;
875bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak
876adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	memset(&itimer, 0, sizeof(struct itimerval));
877adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/*
878adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	 * Start the timer.
879adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	 */
880adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	itimer.it_interval.tv_usec = 0;
881adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	itimer.it_interval.tv_sec = 0;
882adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	itimer.it_value.tv_usec = 0;
883354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	itimer.it_value.tv_sec = (time_t) (TVAL * 60.0);
884adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
885adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (setitimer(ITIMER_REAL, &itimer, NULL)) {
886adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("setitimer");
887354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		exit(1);
888adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
889adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
890adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
891adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
892adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
893adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * parse_args
894adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *
895adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * Parse command line arguments.  Any errors cause the program to exit
896adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * at this point.
897adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
898354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid parse_args(int argc, char *argv[])
899adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
900adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int i;
901354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int opt, errflag = 0;
902354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int dflag = 0, bflag = 0, fflag = 0, tflag = 0;
903354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	extern int optind;
904354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	extern char *optarg;
905354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao
906354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	/* DVAL:        0  1     2      3   4  5  6  7  8  9  10 11 */
907354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	int limits[] = { -1, -1, MAXBVAL, 17, 8, 5, 4, 3, 2, 2, 2, 2 };
908354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao
909354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	while ((opt = getopt(argc, argv, "b:d:ft:D?")) != EOF) {
910354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		switch (opt) {
911354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		case 'b':
912354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			if (bflag)
913354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				errflag++;
914354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			else {
915354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				bflag++;
916354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				errno = 0;
917354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				BVAL = atoi(optarg);
918354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				if (errno) {
919354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					perror("atoi");
920354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					fprintf(stderr,
921354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						" ERROR : atoi - errno %d.",
922354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						errno);
923adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					errflag++;
924adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				}
925354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			}
926354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			break;
927354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		case 'd':
928354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			if (dflag)
929354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				errflag++;
930354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			else {
931354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				dflag++;
932354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				errno = 0;
933354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				DVAL = atoi(optarg);
934354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				if (errno) {
935354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					perror("atoi");
936354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					fprintf(stderr,
937354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						" ERROR : atoi - errno %d.",
938354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						errno);
939adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					errflag++;
940adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				}
941354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			}
942354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			break;
943354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		case 'f':
944354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			fflag = 1;
945354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			break;
946354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		case 'D':
947354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			AUSDEBUG = 1;
948354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			break;
949354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		case 't':
950354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			if (tflag)
951354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				errflag++;
952354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			else {
953354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				tflag++;
954354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				errno = 0;
955354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				TVAL = atoi(optarg);
956354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				if (!TVAL || errno) {
957354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					perror("atoi");
958354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					fprintf(stderr,
959354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						" ERROR : atoi - errno %d.",
960354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						errno);
961adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					errflag++;
962adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				}
963354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			}
964354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			break;
965354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		case '?':
966354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			errflag++;
967354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			break;
968adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
969adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
970adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
971adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (BVAL < 2) {
972354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		errflag++;
973354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, "The value of b must be greater than 1\n");
974adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	} else if (DVAL < 2) {
975adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		errflag++;
976354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, "The depth value must be greater than 1\n");
9778fb1cdb0538640f295691929650408688537fb7fGarrett Cooper	} else if (!fflag && (DVAL > MAXDVAL)) {
978adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/* || BVAL > limits[DVAL])) { */
979354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, "\tExceeded process creation limits.   \
980adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj\n\tParameters will generate %lu processes.  \n\tThe preset limits are as \
981354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaofollows:\n\t\tdepth\tbreadth\ttotal\n", sumit(BVAL, DVAL));
982df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper		for (i = 2; i <= MAXDVAL; i++)
983354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			fprintf(stderr, "\t\t %-3d\t  %-5d\t%-5lu\n", i,
984354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				limits[i], sumit(limits[i], i));
985354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		exit(1);
986adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
987adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
988df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper	if (errflag) {
989354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr,
990354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			"usage: %s [-b number] [-d number] [-t number] \n",
991354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			argv[0]);
992354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, "where:\n");
993354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr,
994354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			"\t-b number\tnumber of children each parent will spawn ( > 1)\n");
995354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, "\t-d number\tdepth of process tree ( > 1)\n");
996354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, "\t-t\t\tset timeout value\n");
997354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, " SEVERE : Command line parameter error.\n");
9986c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper		exit(1);
999adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
1000adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
1001adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1002adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
1003adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * Initializes environment variables, using defaults if not set in env.
1004adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
1005adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojint getenv_val(void)
1006adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
1007354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	char *c;		/* character pointer */
1008adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	struct envstruct *envd = envdata;	/* pointer to environment data */
1009adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1010adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	union {
1011354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		int *vint;
1012354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		char *chptr;
1013adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	} val;
1014adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1015adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/*
1016adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	 * Loop through envdata, set default first then set environment
10174bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak	 * variable value if present.
1018adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	 */
1019adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	for (; *envd->env_name != '\0'; envd++) {
1020354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		if ((val.chptr = getenv(envd->env_name)) == NULL)
1021adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			val.chptr = envd->eval.chptr;
1022adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1023adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		c = val.chptr;
1024df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper		while (isdigit(*c))
1025adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			c++;
1026adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1027df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper		if (*c == '\0') {
1028d218f348c12b42a78fa0306d9a033bfa4f67238bCyril Hrubis			(envd->eval.vint) = malloc(sizeof(int));
1029adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			*(envd->eval.vint) = atoi(val.chptr);
1030354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		} else {
1031adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			envd->eval.chptr = malloc(strlen(val.chptr) + 1);
1032adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			strcpy(envd->eval.chptr, val.chptr);
1033adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
1034adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
1035b832005393fde0ee8df5fa6c8aa71105971ff61arobbiew	return 0;
1036adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
1037adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1038adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/*
1039adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * Prints all errors coming from the children and terminates execution if
1040adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * an error execption is received.  In addition messenger() is sent the
1041adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * process group id of the children so it can terminate all children.
1042adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj * This routine uses message queues to receive all communications.
10434bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak */
1044354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gaovoid messenger(void)
1045354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao{				/* AKA Assassin */
1046adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	Msgbuf rcvbuf;
1047adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1048adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int discrim = 0;
1049adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int rc;			/* generic return code var */
10504bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak	int sig = -1;		/* type of signal received */
1051adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int msgerr;	/* message queue used to send error messages */
1052bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak	extern int procgrp;	/* process group of children (used to kill them) */
1053adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1054adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	/*
1055adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	 *  Infinite loop used to receive error messages from children and
1056adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	 *  to terminate process tree.
1057adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	 */
1058df3eb16e38c6a163b0a7367c885679eed6140964Garrett Cooper	while (TRUE) {
1059adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		rc = msgrcv(msgerr, &rcvbuf, sizeof(struct messagebuf), 0, 0);
1060adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		if (rc == -1) {
1061adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			switch (errno) {
1062adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			case EAGAIN:
1063354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				printf("msgqueue %d not ready to receive\n",
1064354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				       msgid);
1065adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				fflush(stdout);
1066adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				errno = 0;
1067adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				break;
1068adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			case ENOMSG:
1069adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				printf("msgqueue %d no message\n", msgid);
1070adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				fflush(stdout);
1071adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				errno = 0;
1072adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				break;
1073adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			default:
1074adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				perror("msgrcv failed");
1075354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				fprintf(stderr,
1076354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					" SEVERE : messenger - msgrcv failed, errno: %d\n",
1077354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					errno);
1078adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				errno = 0;
1079adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				break;
1080adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			}
1081354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		} else {
1082354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			switch ((int)rcvbuf.mtyp) {
1083354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			case 1:	/* type 1: we received the process group id */
1084354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				sscanf(rcvbuf.mtext, "%d", &procgrp);
1085adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				break;
1086adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1087354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			case 2:	/*  type 2: we received an error */
1088354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				fprintf(stderr, " SEVERE : %s ", rcvbuf.mtext);
1089adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				/* rcvbuf.mtext type %s ou %d ??? */
1090adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				break;
1091adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1092354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			case 3:	/* type 3: somebody got a signal, now we terminate */
1093354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				sscanf(rcvbuf.mtext, "%d", &sig);
1094adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1095354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				switch (sig) {
10964bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak				case SIGALRM:
1097354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					/* a process is hung, we will terminate */
1098adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					killpg(procgrp, sig);
1099354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					fprintf(errfp,
1100354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						"ALERT! ALERT! WE HAVE TIMED OUT\n");
1101354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					fprintf(stderr,
1102354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						" SEVERE : SIGALRM: A process timed out, we failed\n");
1103adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					shmaddr->err++;
1104adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					break;
1105adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1106bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak				case SIGUSR1:
1107354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					/* Special: means everything went ok */
1108354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					discrim = 1;
1109adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					break;
1110adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
11114bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak				default:
1112354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					/* somebody sent a signal, we will terminate */
1113adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					killpg(procgrp, sig);
1114354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					fprintf(errfp,
1115354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						"We received signal %d\n", sig);
1116354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao					fprintf(stderr,
1117354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						" SEVERE : signal %d received, A proc was killed\n",
1118354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao						sig);
1119adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					break;
1120adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				}
1121adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				/* clean up and exit with status */
1122adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				rm_msgqueue();
1123adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				rm_semseg();
1124adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				if (AUSDEBUG)
1125adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					print_shm();
1126adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				prtln();
1127adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				rm_shmseg();
1128adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				prtln();
1129adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				if (discrim) {
1130adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					prtln();
11314b3e904aadb7536585e7595ad01c070467851f35Cyril Hrubis					printf("Test exiting with SUCCESS\n");
1132adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj					exit(0);
1133adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				}
11346c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper				exit(1);
11352c28215423293e443469a07ae7011135d058b671Garrett Cooper
1136adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				break;
1137adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			}
1138adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
1139adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
1140adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
1141adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
11424bb656a129f7507823e9e6d6b98b1a02fd80ef89subrata_modak/*
1143adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  This routine spawns off the first child (node 0) of the process tree.
1144adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  This child set up the signal handler for all of the children and also
1145adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  sets up a process group so that all children can be terminated easily.
1146adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  The child then calls spawn which creates the process tree.  After spawn
1147adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  has returned the child contacts the parent and the parent exits.
1148adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj *  The parent sets her own signal handler and then calls messenger.
1149adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj */
1150adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanojvoid doit(void)
1151adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
1152adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	pid_t pid;		/* process id */
1153adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	int rc;
1154354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	char mtext[80];		/* message text */
1155bdbaec51a423e715c2b03ed9e497e9a1fba6103esubrata_modak	extern int msgerr;
1156adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern int procgrp;
1157adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1158adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	pid = fork();
1159adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef __64LDT__
1160354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	if (pid == (pid_t) 0) {
1161adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
1162adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (pid == 0) {
1163adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
1164adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		/* set the process group so we can terminate all children */
1165adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		set_signals((void *)nextofkin);	/* set up signal handlers and initialize pgrp */
1166adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifndef _LINUX
1167adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		procgrp = setpgrp(0, 0);
1168adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
1169adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		procgrp = setpgrp();
1170adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
1171adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		if (AUSDEBUG) {
11722c28215423293e443469a07ae7011135d058b671Garrett Cooper			fprintf(stderr, "process group: %d\n", procgrp);
1173354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			fflush(stderr);
1174adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
1175adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		if (procgrp == -1) {
1176adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			perror("setpgid failed");
1177354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			fprintf(stderr, " SEVERE : setpgid failed, errno: %d\n",
1178354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				errno);
11796c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper			exit(1);
1180adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
1181354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		sprintf(mtext, "%d", procgrp);
1182adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		rc = send_message(msgerr, 1, mtext);
1183adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		if (rc == -1) {
1184adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			perror("send_message failed");
1185354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			fprintf(stderr,
1186354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				" SEVERE : send_message failed, errno: %d\n",
1187354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				errno);
11886c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper			exit(1);
1189adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
1190adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1191354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		put_proc_info(0);	/* store process info for this (root) process */
1192adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		spawn(0);
1193adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		if (shmaddr->pid == getpid()) {
1194adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			sprintf(mtext, "%d", SIGUSR1);
1195adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			rc = send_message(msgerr, 3, mtext);
1196adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			if (rc == -1) {
1197354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				severe
1198354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				    ("msgsnd failed: %d msgid %d mtyp %d mtext %d\n",
1199354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao				     errno, msgerr, 3, mtext);
1200adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj				exit(1);
1201adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1202adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj			}
1203adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		}
1204adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		exit(0);
1205354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	}
1206adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef __64LDT__
1207354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	else if (pid > (pid_t) 0) {
1208adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#else
1209354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	else if (pid > 0) {
1210adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
1211adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		set_signals((void *)cleanup);	/* set up signal handlers and initialize pgrp */
1212354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		messenger();	/* receives and acts upon messages */
1213adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		exit(1);
1214354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	} else {
1215adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		perror("fork failed");
1216354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr,
1217354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			" SEVERE : fork failed, exiting with errno %d\n",
1218354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			errno);
12196c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper		exit(1);
1220adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
1221adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj}
1222adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1223adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj/* main */
1224b832005393fde0ee8df5fa6c8aa71105971ff61arobbiewint main(int argc, char *argv[])
1225adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj{
1226adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	extern Pinfo *shmaddr;	/* start address of shared memory */
1227adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1228adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	prtln();
1229adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	getenv_val();		/* Get and initialize all environment variables */
1230adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	prtln();
1231adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1232adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (argc < 2) {
1233354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr,
1234354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			"usage: %s [-b number] [-d number] [-t number] \n",
1235354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			argv[0]);
1236354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, "where:\n");
1237354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr,
1238354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao			"\t-b number\tnumber of children each parent will spawn ( > 1)\n");
1239354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, "\t-d number\tdepth of process tree ( > 1)\n");
1240354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, "\t-t\t\tset timeout value\n");
1241354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		fprintf(stderr, " SEVERE : Command line parameter error.\n");
1242354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao		exit(1);
1243354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	}
1244adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1245adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	parse_args(argc, argv);	/* Get all command line arguments */
1246354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	dprt("value of BVAL = %d, value of DVAL = %d\n", BVAL, DVAL);
1247adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	nodesum = sumit(BVAL, DVAL);
1248adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#ifdef _LINUX
1249adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	if (nodesum > 250) {
1250adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		printf("total number of process to be created "
1251adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		       "nodesum (%d) is greater\n than the allowed "
1252adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		       "SEMMSL value (250)\n", nodesum);
1253adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		printf("reseting the value of nodesum to SEMMSL\n");
1254adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj		nodesum = 250;
1255adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	}
1256adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj#endif
1257adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1258354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	dprt("value of nodesum is initiallized to: %d\n", nodesum);
1259adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1260adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	prtln();
1261adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	setup_shm();		/* Set up, allocate and initialize shared memory */
1262adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	prtln();
1263adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	setup_semaphores();	/* Set up, allocate and initialize semaphores */
1264adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	prtln();
1265adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	setup_msgqueue();	/* Set up, allocate and initialize message queues */
1266adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	prtln();
1267adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj
1268354ebb48db8e66a853a58379a4808d5dcd1ceac3Wanlong Gao	doit();			/* spawn off processes */
1269adfcc1c17d5d3ff6314f416790e2d1969658888aiyermanoj	prtln();
12706c5023af88822c34c0a8ddfcf4ad32e2b54fac1fGarrett Cooper	return 0;
12712c28215423293e443469a07ae7011135d058b671Garrett Cooper
127240fbd7139dab2bba62034224e66c92cf6ea00f82Markos Chandras}
1273