Ejemplo n.º 1
0
static int rm_shared_mem(key_t shm_id,	/* id of shared memory segment to be removed  */
                         char *shm_addr,	/* address of shared mem seg to be removed    */
                         int cmd)
{   /* remove id only or remove id and detach seg */
    struct shmid *shmbuf = NULL;	/* info about the segment pointed by shmkey   */

    dprt("pid[%d]: rm_shared_mem(): shm_id = %d shm_addr = %#x cmd = %d\n",
         getpid(), shm_id, shm_addr, cmd);
    if (shmctl(shm_id, IPC_RMID, (struct shmid_ds *)shmbuf) == -1) {
        dprt("pid[%d]: rm_shared_mem(): shmctl unable to remove shm_id[%d]\n", getpid(), shm_id);
        perror("rm_shared_mem(): shmctl()");
        return -1;
    }

    if (cmd) {
        if (shmdt((void *)shm_addr) == -1) {
            dprt("pid[%d]:rm_shared_mem(): shmdt unable to detach addr = %#x\n", getpid(), shm_addr);
            perror("rm_shared_mem(): shmdt()");
            return -1;
        }
    }
    return 0;
}
Ejemplo n.º 2
0
int main(int argc,		/* number of input parameters                 */
         char **argv)
{   /* pointer to the command line arguments.     */
    int c;			/* command line options                       */
    int num_thrd = MAXT;	/* number of threads to create                */
    int num_reps = MAXR;	/* number of repatitions the test is run      */
    int thrd_ndx;		/* index into the array of thread ids         */
    void *th_status;	/* exit status of LWP's                       */
    int map_size;		/* size of the file mapped.                   */
    int shmkey = 1969;	/* key used to generate shmid by shmget()     */
    pthread_t thrdid[30];	/* maxinum of 30 threads allowed              */
    long chld_args[4];	/* arguments to the thread function           */
    char *map_address = NULL;
    /* address in memory of the mapped file       */
    extern int optopt;	/* options to the program                     */

    while ((c = getopt(argc, argv, "hl:t:")) != -1) {
        switch (c) {
        case 'h':
            usage(argv[0]);
            break;
        case 'l':	/* how many repetitions of the test to exec   */
            if ((num_reps = atoi(optarg)) == 0)
                OPT_MISSING(argv[0], optopt);
            else if (num_reps < 0) {
                fprintf(stdout,
                        "WARNING: bad argument. Using default\n");
                num_reps = MAXR;
            }
            break;
        case 't':
            if ((num_thrd = atoi(optarg)) == 0)
                OPT_MISSING(argv[0], optopt);
            else if (num_thrd < 0) {
                fprintf(stdout,
                        "WARNING: bad argument. Using default\n");
                num_thrd = MAXT;
            }
            break;
        default:
            usage(argv[0]);
            break;
        }
    }

    chld_args[0] = num_reps;

    for (thrd_ndx = 0; thrd_ndx < num_thrd; thrd_ndx += 2) {
        srand(time(NULL) % 100);
        map_size =
            (1 + (int)(1000.0 * rand() / (RAND_MAX + 1.0))) * 4096;

        chld_args[1] = shmkey++;
        chld_args[2] = map_size;

        dprt("main(): thrd_ndx = %d map_address = %#x map_size = %d\n",
             thrd_ndx, map_address, map_size);

        chld_args[3] = WRITER;

        if (pthread_create
                (&thrdid[thrd_ndx], NULL, shmat_rd_wr, chld_args)) {
            perror("shmat_rd_wr(): pthread_create()");
            exit(-1);
        }

        chld_args[3] = READER;

        if (pthread_create
                (&thrdid[thrd_ndx + 1], NULL, shmat_rd_wr, chld_args)) {
            perror("shmat_rd_wr(): pthread_create()");
            exit(-1);
        }
    }

    sync();

    for (thrd_ndx = 0; thrd_ndx < num_thrd; thrd_ndx++) {
        if (pthread_join(thrdid[thrd_ndx], &th_status) != 0) {
            perror("shmat_rd_wr(): pthread_join()");
            exit(-1);
        } else {
            dprt("WE ARE HERE %d\n", __LINE__);
            if (th_status == (void *)-1) {
                fprintf(stderr,
                        "thread [%ld] - process exited with errors\n",
                        (long)thrdid[thrd_ndx]);
                exit(-1);
            }
        }
    }
    exit(0);
}
Ejemplo n.º 3
0
static void *shmat_rd_wr(void *args)
{   /* arguments to the thread function             */
    int shmndx = 0;		/* index to the number of attach and detach   */
    int index = 0;		/* index to the number of blocks touched      */
    int reader = 0;		/* this thread is a reader thread if set to 1 */
    key_t shm_id = 0;	/* shared memory id                           */
    long *locargs =		/* local pointer to arguments                 */
        (long *)args;
    volatile int exit_val = 0;	/* exit value of the pthread                  */
    char *read_from_mem;	/* ptr to touch each (4096) block in memory   */
    char *write_to_mem;	/* ptr to touch each (4096) block in memory   */
    char *shmat_addr;	/* address of the attached memory             */
    char buff;		/* temporary buffer                           */

    reader = (int)locargs[3];
    while (shmndx++ < (int)locargs[0]) {
        dprt("pid[%d]: shmat_rd_wr(): locargs[1] = %#x\n",
             getpid(), (int)locargs[1]);

        /* get shared memory id */
        if ((shm_id =
                    shmget((int)locargs[1], (int)locargs[2], IPC_CREAT | 0666))
                == -1) {
            dprt("pid[%d]: shmat_rd_wr(): shmget failed\n",
                 getpid());
            perror("do_shmat_shmadt(): shmget()");
            PTHREAD_EXIT(-1);
        }

        fprintf(stdout, "pid[%d]: shmat_rd_wr(): shmget():"
                "success got segment id %d\n", getpid(), shm_id);

        /* get shared memory segment */
        if ((shmat_addr = shmat(shm_id, NULL, 0)) == (void *)-1) {
            rm_shared_mem(shm_id, shmat_addr, 0);
            fprintf(stderr,
                    "pid[%d]: do_shmat_shmadt(): shmat_addr = %#lx\n",
                    getpid(), (long)shmat_addr);
            perror("do_shmat_shmadt(): shmat()");
            PTHREAD_EXIT(-1);
        }
        dprt("pid[%d]: do_shmat_shmadt(): content of memory shmat_addr = %s\n", getpid(), shmat_addr);

        fprintf(stdout,
                "pid[%d]: do_shmat_shmadt(): got shmat address = %#lx\n",
                getpid(), (long)shmat_addr);

        if (!reader) {
            /* write character 'Y' to that memory area */
            index = 0;
            write_to_mem = shmat_addr;
            while (index < (int)locargs[2]) {
                dprt("pid[%d]: do_shmat_shmatd(): write_to_mem = %#x\n", getpid(), write_to_mem);
                *write_to_mem = 'Y';
                index++;
                write_to_mem++;
                sched_yield();
            }
        } else {
            /* read from the memory area */
            index = 0;
            read_from_mem = shmat_addr;
            while (index < (int)locargs[2]) {
                buff = *read_from_mem;
                index++;
                read_from_mem++;
                sched_yield();
            }
        }

        sched_yield();

        /* remove the shared memory */
        if (rm_shared_mem(shm_id, shmat_addr, 1) == -1) {
            fprintf(stderr,
                    "pid[%d]: do_shmat_shmatd(): rm_shared_mem(): faild to rm id\n",
                    getpid());
            PTHREAD_EXIT(-1);
        }
    }

    PTHREAD_EXIT(0);
}
Ejemplo n.º 4
0
static void *
crte_mk_rm(void *args)
{
    int 	dircnt;		/* index to the number of subdirectories      */
    int		fd;		/* file discriptor of the files genetated     */
    int		filecnt;	/* index to the number of ".c" files created  */
    int		numchar[2];	/* number of characters written to buffer     */
    char 	*dirname;	/* name of the directory/idirectory tree      */
    char 	*tmpdirname;	/* name of a temporary directory, for swaping */
    char	*cfilename;     /* name of the ".c" file created	      */
    char	*mkfilename;	/* name of the makefile - which is "makefile" */
    char	*hostname;	/* hostname of the client machine             */
    char	*prog_buf;	/* buffer containing contents of the ".c" file*/
    char	*make_buf;	/* buffer the contents of the makefile        */
    char	*pwd;	        /* contains the current working directory     */
    long	*locargptr =	/* local pointer to arguments                 */
                             (long *)args;
    volatile int exit_val = 0;  /* exit value of the pthreads		      */

    if ((dirname = malloc(sizeof(char) * 2048)) == NULL) /* just paranoid */
    {
        perror("crte_mk_rm(): dirname malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((tmpdirname = malloc(sizeof(char) * 2048)) == NULL)
    {
        perror("crte_mk_rm(): tmpdirname malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((cfilename = malloc(sizeof(char) * 2048)) == NULL)
    {
        perror("crte_mk_rm(): cfilename malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((mkfilename = malloc(sizeof(char) * 2048)) == NULL)
    {
        perror("crte_mk_rm(): mkfilename malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((prog_buf = malloc(sizeof(char) * 4096)) == NULL)
    {
        perror("crte_mk_rm(): prog_buf malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((pwd = malloc(PATH_MAX)) == NULL)
    {
        perror("crte_mk_rm(): pwd malloc()");
	PTHREAD_EXIT(-1);
    }

    if ((hostname = malloc(sizeof(char) * 1024)) == NULL)
    {
        perror("crte_mk_rm(): hostname malloc()");
	PTHREAD_EXIT(-1);
    }

    if (gethostname(hostname, 255) == -1)
    {
        perror("crte_mk_rm(): gethostname()");
        PTHREAD_EXIT(-1);
    }
    if (!getcwd(pwd, PATH_MAX))
    {
        perror("crte_mk_rm(): getcwd()");
	PTHREAD_EXIT(-1);
    }

    numchar[0] = sprintf(prog_buf,
                "main()\n{\n\t printf(\"hello world\");\n}\n");

    for (dircnt = 0; dircnt < (int)locargptr[0]; dircnt++)
    {
        /* First create the base directory, then create the subdirectories   */
        if (dircnt == 0)
            sprintf(dirname, "%s.%ld", hostname, gettid());
        else
        {
            sprintf(tmpdirname, "%s/%ld.%d", dirname, gettid(), dircnt);
            sprintf(dirname, "%s", tmpdirname);
        }
        sync();

        dprt("pid[%d] creating directory: %s\n", gettid(), dirname);
        if (mkdir(dirname, 0777) == -1)
        {
            perror("crte_mk_rm(): mkdir()");
	    PTHREAD_EXIT(-1);
        }
    }

    sync();
    usleep(10);
    for (dircnt = 0; dircnt < (int)locargptr[0]; dircnt++)
    {
        if (dircnt == 0)
            sprintf(dirname, "%s/%s.%ld", pwd, hostname, gettid());
        else
        {
            sprintf(tmpdirname, "%s/%ld.%d", dirname, gettid(), dircnt);
            sprintf(dirname, "%s", tmpdirname);
        }
        sync();
        if ((make_buf = malloc(sizeof(char) * 4096)) == NULL)
        {
            perror("crte_mk_rm(): make_buf malloc()");
            PTHREAD_EXIT(-1);
        }
        sprintf(mkfilename, "%s/makefile", dirname);
        {
            /* HACK! I could not write "%.c" to the makefile */
            /* there is probably a correct way to do it      */
            char *dotc = malloc(10);
            dotc = ".c";
            sync();
            usleep(10);
	    if (dircnt == (locargptr[0] - 1))
            {
                numchar[1] = sprintf(make_buf,
                "CFLAGS := -O -w -g\n"
                "SUBDIRS = %ld.%d\n"
                "SRCS=$(wildcard *.c)\n"
                "TARGETS=$(patsubst %%%s,\%%,$(SRCS))\n"
                "all:\t $(TARGETS)\n"
                "clean:\n"
                "\trm -f $(TARGETS)\n",
                       gettid(), dircnt + 1, dotc);
	    }
            else
            {
	        numchar[1] = sprintf(make_buf,
	        "CFLAGS := -O -w -g\n"
	        "SUBDIRS = %ld.%d\n"
	        "SRCS=$(wildcard *.c)\n"
	        "TARGETS=$(patsubst %%%s,\%%,$(SRCS))\n\n\n"
	        "all:\t $(TARGETS)\n"
                "\t@for i in $(SUBDIRS); do $(MAKE) -C $$i ; done\n\n"
	        "clean:\n"
	        "\trm -f $(TARGETS)\n"
                "\t@for i in $(SUBDIRS); do $(MAKE) -C $$i clean ; done\n",
                       gettid(), dircnt + 1, dotc);
            }
        }
Ejemplo n.º 5
0
static int
rm_file_dir( int  numsdir,		/* how many subdirs to remove         */
	     int  numfiles,		/* number of files to remove per dir  */
             char *hname,		/* hostname of the client machine     */
	     char *base_dir)            /* directory where the test is located*/
{
    int		filecnt;		/* index to the num of files to remove*/
    int		dircnt;		        /* index into directory tree          */
    int		sindex = numsdir;       /* num subdirectory tree to remove    */
    char	*dirname;		/* name of the directory to chdir()   */
    char	*tmpdirname;		/* temp name for directory, for swap  */
    char	*filename;		/* name of the cfile to remove        */
    char	*subdir;		/* name of the sub dir to remove      */

    if ((dirname = malloc(sizeof(char) * 2048)) == NULL) /* just paranoid */
    {
        perror("crte_mk_rm(): dirname malloc()");
	return 1;
    }

    if ((tmpdirname = malloc(sizeof(char) * 2048)) == NULL) /* just paranoid */
    {
        perror("crte_mk_rm(): tmpdirname malloc()");
	return 1;
    }

    if ((filename = malloc(sizeof(char) * 2048)) == NULL) /* just paranoid */
    {
        perror("crte_mk_rm(): filename malloc()");
	return 1;
    }

    if ((subdir = malloc(sizeof(char) * 2048)) == NULL) /* just paranoid */
    {
        perror("crte_mk_rm(): subdir malloc()");
	return 1;
    }

    dprt("pid[%d]: base directory: %s\n", gettid(), base_dir);
    while (sindex)
    {
        /* get the name of the last directory created. */
        for (dircnt = 0; dircnt < sindex; dircnt++)
        {
            if (dircnt == 0)
                sprintf(dirname, "%s/%s.%ld", base_dir, hname, gettid());
            else
            {
                 sprintf(tmpdirname, "%s/%ld.%d", dirname, gettid(), dircnt);
                 sprintf(dirname, "%s", tmpdirname);
            }
            sync();
        }

        dprt("pid[%d]: cd'ing to last created dir: %s\n", gettid(), dirname);

        sindex--;

        /* remove all the ".c" files and makefile in this directory */
        for (filecnt = 0; filecnt < numfiles; filecnt++)
        {
            sprintf(filename, "%s/%ld.%d.%d.c", dirname, gettid(), dircnt - 1,
		filecnt);
            dprt("pid[%d]: removing file: %s\n", gettid(), filename);

	    if (unlink(filename))
            {
                dprt("pid[%d]: failed removing file: %s\n", gettid(), filename);
                perror("rm_file_dir(): unlink()");
                free(tmpdirname);
                free(dirname);
                free(filename);
                free(subdir);
	        return 1;
            }
            sync();
        }

        sprintf(filename, "%s/%s", dirname, "makefile");
        dprt("pid[%d]: removing %s\n", gettid(), filename);
        if (unlink(filename))
        {
            perror("rm_file_dir() cound not remove makefile unlink()");
            free(tmpdirname);
            free(dirname);
            free(filename);
            free(subdir);
            return 1;
        }
        sync();

        /* the last directory does not have any more sub directories */
        /* nothing to remove.				         */
        dprt("pid[%d]: in directory count(dircnt): %d\n", gettid(), dircnt);
        dprt("pid[%d]: last directory(numsdir): %d\n", gettid(), numsdir);
        if (dircnt < numsdir)
        {
            /* remove the sub directory */
            sprintf(subdir, "%s/%ld.%d", dirname, gettid(), dircnt);
            dprt("pid[%d]: removing subdirectory: %s\n", gettid(), subdir);
            if (rmdir(subdir) == -1)
            {
                perror("rm_file_dir() rmdir()");
                free(tmpdirname);
                free(dirname);
                free(filename);
                free(subdir);
	        return 1;
            }
            sync();
        }
    }

    free(tmpdirname);
    free(dirname);
    free(filename);
    free(subdir);
        return 0;
}
Ejemplo n.º 6
0
static int
init_compile( int  what_todo,		 /* do a compile or clean             */
              char *base_dir,            /* base directory of the test        */
              char *hname)		 /* hostname of the machine           */
{
    int 	status;		/* return status of execve process            */
    pid_t	pid;		/* pid of the process that does compile       */
    char	*dirname;	/* location where compile is initated         */
    char	*command;	/* make or make clean command.                */

    if ((dirname = malloc(sizeof(char) * 2048)) == NULL) /* just paranoid */
    {
        perror("init_compile(): dirname malloc()");
        return 1;
    }

    if ((command = malloc(sizeof(char) * 1024)) == NULL) /* just paranoid */
    {
        perror("init_compile(): dirname malloc()");
        return 1;
    }

    what_todo ? sprintf(command, "make -s") : sprintf(command, "make -s clean");

    sprintf(dirname, "%s/%s.%ld", base_dir, hname, gettid());

    if (chdir(dirname) == -1)
    {
        dprt("pid[%d]: init_compile(): dir name = %s\n", gettid(), dirname);
        perror("init_compile() chdir()");
        free(dirname);
        return 1;
    }

    dprt("pid[%d]: init_compile(): command = %s\n", gettid(), command);

    if ((pid = fork()) == -1)
    {
        perror("init_compile(): fork()");
        return 1;
    }
    if (!pid)
    {
        char *argv[4];

        argv[0] = "/bin/sh";
        argv[1] = "-c";
        argv[2] = command;
        argv[3] = 0;

	if (execv("/bin/sh", argv) == -1)
        {
	  perror("init_compile(): execv()");
            return 1;
        }
    }
    do
    {
        if (waitpid(pid, &status, 0) == -1)
        {
            if (errno != EINTR)
            {
                fprintf(stderr, "init_compile(): waitpid() failed\n");
                return 1;
            }
        }
        else
        {
            if (chdir(base_dir) == -1)
            {
                dprt("pid[%d]: init_compile(): dir = %s\n", gettid(), dirname);
                perror("init_compile(): chdir()");
                return 1;
            }

            dprt("pid[%d]: init_compile(): status = %d\n", status);
            dprt("we are here %d\n", __LINE__);
            return status;
        }

    } while (1);
}
Ejemplo n.º 7
0
CLIENT *
getkwarnd_handle(void)
{
	void *localhandle;
	struct netconfig *nconf;
	struct netconfig *tpconf;
	struct timeval wait_time;
	struct utsname u;
	static char *hostname;
	static bool_t first_time = TRUE;

/*
 * Total timeout (in seconds) talking to kwarnd.
 */
#define	TOTAL_TIMEOUT	5

	if (kwarn_clnt)
		return (kwarn_clnt);
	if (!(localhandle = setnetconfig()))
		return (NULL);
	tpconf = NULL;
	if (first_time == TRUE) {
		if (uname(&u) == -1) {
			(void) endnetconfig(localhandle);
			return ((CLIENT *)NULL);
		}
		if ((hostname = strdup(u.nodename)) == (char *)NULL) {
			(void) endnetconfig(localhandle);
			return ((CLIENT *)NULL);
		}
		first_time = FALSE;
	}
	while (nconf = getnetconfig(localhandle)) {
		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
			if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
				kwarn_clnt = clnt_tp_create(hostname,
				    KWARNPROG, KWARNVERS, nconf);
				if (kwarn_clnt) {
					dprt("got COTS_ORD\n");
					break;
				}
			} else {
				tpconf = nconf;
			}
		}
	}
	if ((kwarn_clnt == NULL) && (tpconf)) {

		/* Now, try the connection-oriented loopback transport */

		kwarn_clnt = clnt_tp_create(hostname, KWARNPROG, KWARNVERS,
		    tpconf);
#ifdef DEBUG
		if (kwarn_clnt) {
			dprt("got COTS\n");
		}
#endif	/* DEBUG */
	}
	(void) endnetconfig(localhandle);

	/*
	 * This bit of code uses an as yet unimplemented argument to
	 * clnt_control(). CLSET_SVC_PRIV specifies that the underlying
	 * loopback transport should be checked to ensure it is
	 * connected to a process running as root. If so, the clnt_control()
	 * call returns TRUE. If not, it returns FALSE.
	 */

#ifdef CLSET_SVC_PRIV

	if (clnt_control(kwarn_clnt, CLSET_SVC_PRIV, NULL) != TRUE) {
		clnt_destroy(kwarn_clnt);
		kwarn_clnt = NULL;
		return (NULL);
	{
#endif
	if (kwarn_clnt == NULL)
		return (NULL);

	kwarn_clnt->cl_auth = authsys_create("", getuid(), 0, 0, NULL);
	if (kwarn_clnt->cl_auth == NULL) {
		clnt_destroy(kwarn_clnt);
		kwarn_clnt = NULL;
		return (NULL);
	}
	wait_time.tv_sec = TOTAL_TIMEOUT;
	wait_time.tv_usec = 0;
	(void) clnt_control(kwarn_clnt, CLSET_TIMEOUT, (char *)&wait_time);

	return (kwarn_clnt);
}

void
resetkwarnd_handle(void)
{
	auth_destroy(kwarn_clnt->cl_auth);
	clnt_destroy(kwarn_clnt);
	kwarn_clnt = NULL;
}
Ejemplo n.º 8
0
Archivo: process.c Proyecto: kraj/ltp
/*
 * This is the meat and potatoes of the program.  Spawn creates a tree
 * of processes with Dval depth and Bval breadth.  Each parent will spawn
 * Bval children.  Each child will store information about themselves
 * in shared memory.  The leaf nodes will communicate the existence
 * of one another through message queues, once each leaf node has
 * received communication from all of her siblings she will reduce
 * the semaphore count and exit.  Meanwhile all parents are waiting
 * to hear from their children through the use of semaphores.  When
 * the semaphore count reaches zero then the parent knows all the
 * children have talked to one another.  Locking of the connter semaphore
 * is provided by the use of another (binary) semaphore.
 */
int spawn(int val)
{
	extern int sem_count;	/* used to keep track of childern */
	extern int sem_lock;	/* used to lock access to sem_count semaphore */

	int i;			/* Breadth counter */
	static int level = 0;	/* level counter */
	int lvlflg = 0;		/* level toggle, limits parental spawning
				   to one generation */
	int pslot = 0;
#ifdef __64LDT__
	pid_t pid;		/* pid of child process */
#else
	int pid;		/* pid of child process */
#endif
	Pinfo *pinfo;		/* pointer to process information in shared mem */
	int semval;		/* value of semaphore ( equals BVAL initially */
	static int tval = 1;	/* tree node value of child. */

	char foo[1024];

	level++;

	for (i = 1; i <= BVAL; i++) {
		tval = (val * BVAL) + i;
		if (!lvlflg) {
			pid = fork();
			if (!pid) {	/* CHILD */
				if (AUSDEBUG) {
					sprintf(foo, "%sslot%d", SLOTDIR, tval);
					debugfp = fopen(foo, "a+");
				}
				pinfo = put_proc_info(tval);

				debugout
				    ("pid: %-6d ppid: %-6d lev: %-2d i: %-2d val: %-3d\n",
				     pinfo->pid, pinfo->ppid, level, i, tval);

				set_timer();	/* set up signal handlers and initialize pgrp */
				if (level < DVAL) {
					if (spawn(tval) == -1) {
						pslot =
						    semoper(tval, sem_lock, -1);
						semarg.val = 0;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
						semval =
						    semctl(sem_count, pslot,
							   GETVAL, semarg);
						semarg.val = --semval;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
						semctl(sem_count, pslot, SETVAL,
						       semarg);
						semarg.val = 1;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
						semctl(sem_lock, pslot, SETVAL,
						       semarg);
					}
					lvlflg++;
				} else {	/* leaf node */
					notify(tval);
					return (-1);
				}
			}
#ifdef __64LDT__
			else if (pid > 0 && i >= BVAL) {	/* PARENT */
#else
			else if (pid > (pid_t) 0 && i >= BVAL) {	/* PARENT */
#endif
				pslot = semoper(tval, sem_count, 0);
				pslot = semoper(pslot, sem_lock, -1);
				semarg.val = 0;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
				semval =
				    semctl(sem_count, pslot, GETVAL, semarg);
				semarg.val = --semval;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
				semctl(sem_count, pslot, SETVAL, semarg);
				semarg.val = 1;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
				semctl(sem_lock, pslot, SETVAL, semarg);
				(shmaddr + val)->msg++;
			}
#ifdef __64LDT__
			else if (pid < (pid_t) 0) {
#else
			else if (pid < 0) {
#endif
				perror("spawn: fork failed");
				severe
				    ("spawn: fork failed, exiting with errno %d\n",
				     errno);
				exit(1);
			} else
				(shmaddr + val)->msg++;
		}
	}
	return (pslot);
}

/*
 * Allocate message queues.
 */
void setup_msgqueue(void)
{
	extern int msgid;
	extern int msgerr;

	msgid = msgget(IPC_PRIVATE,
		       IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP |
		       S_IWGRP);
	if (msgid == -1) {
		perror("msgget msgid failed");
		fprintf(stderr, " SEVERE : msgget msgid failed: errno %d\n",
			errno);
		exit(1);
	}

	msgerr = msgget(IPC_PRIVATE,
			IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP |
			S_IWGRP);
	if (msgerr == -1) {
		perror("msgget msgerr failed");
		fprintf(stderr, " SEVERE : msgget msgerr failed: errno %d\n",
			errno);
		exit(1);
	}
}

/*
 * Set up and initialize all semaphores
 */
void setup_semaphores(void)
{
	extern int sem_count;
	extern int sem_lock;

	int i;
	int rc;

	prtln();
	sem_lock = semget(IPC_PRIVATE, nodesum - 1,
			  IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP |
			  S_IWGRP);
	dprt("nodesum = %d, sem_lock = %d\n", nodesum, sem_lock);

	prtln();
	if (sem_lock == -1) {
		perror("semget failed for sem_lock");
		fprintf(stderr,
			" SEVERE : semget failed for sem_lock, errno: %d\n",
			errno);
		rm_shmseg();
		exit(1);
	}

	prtln();
	sem_count = semget(IPC_PRIVATE, nodesum - 1,
			   IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP |
			   S_IWGRP);

	if (sem_count == -1) {
		perror("semget failed for sem_count");
		fprintf(stderr,
			" SEVERE : semget failed for sem_count, errno: %d\n",
			errno);
		rm_shmseg();
		exit(1);
	}
	prtln();

	for (i = 0; i < (nodesum - 1); i++) {
		semarg.val = 1;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
		rc = semctl(sem_lock, i, SETVAL, semarg);
		prtln();
		if (rc == -1) {
			perror("semctl failed for sem_lock failed");
			fprintf(stderr,
				" SEVERE : semctl failed for sem_lock, errno: %d\n",
				errno);
			rm_shmseg();
			exit(1);
		}

		semarg.val = BVAL;	/* to fix problem with 4th arg of semctl in 64 bits MARIOG */
		rc = semctl(sem_count, i, SETVAL, semarg);
		prtln();
		if (rc == -1) {
			perror("semctl failed for sem_lock failed");
			fprintf(stderr,
				" SEVERE : semctl failed for sem_lock, errno: %d\n",
				errno);
			rm_shmseg();
			exit(1);
		}
	}
}

/*
 * Set up and allocate shared memory.
 */
void setup_shm(void)
{
	extern int nodesum;	/* global shared memory id */
	extern int shmid;	/* global shared memory id */
	extern Pinfo *shmaddr;

	int i, j;		/* counters */
	Pinfo *shmad = NULL;	/* ptr to start of shared memory. */
	Pinfo *pinfo = NULL;	/* ptr to struct in shared memory. */

	debugout("size = %d, size (in hex) =  %#x  nodes: %d\n",
		 sizeof(Pinfo) * nodesum + (nodesum * BVAL * sizeof(int)),
		 sizeof(Pinfo) * nodesum + (nodesum * BVAL * sizeof(int)),
		 nodesum);

	/* Get shared memory id */
	shmid = shmget(IPC_PRIVATE,
		       sizeof(Pinfo) * nodesum + (nodesum * BVAL * sizeof(int)),
		       IPC_CREAT | IPC_EXCL | S_IRUSR | S_IWUSR | S_IRGRP |
		       S_IWGRP);
	if (shmid < 0) {
		perror("shmget failed");
		fprintf(stderr, " SEVERE : shmget failed: errno %d\n", errno);
		exit(1);
	}

	/* allocate shared memory */

	if ((shmad = shmat(shmid, (char *)shmad, 0)) == MAP_FAILED) {
		printf("SEVERE : shmat failed\n");
		exit(1);
	} else {
		shmctl(shmid, IPC_RMID, NULL);
	}

	/* set all fields in shared memory to -1 */
	for (pinfo = shmad, i = 0; i < nodesum; i++, pinfo++) {
#ifdef __64LDT__
		pinfo->pid = (pid_t) - 1;
		pinfo->ppid = (pid_t) - 1;
#else
		pinfo->pid = -1;
		pinfo->ppid = -1;
#endif
		pinfo->msg = -1;
		pinfo->err = -1;

		/* Changed 10/9/97 */
		/* pinfo->list = (int *)((ulong)shmad + nodesum * sizeof(Pinfo)
		   + (sizeof(int) * BVAL * i)); */
		pinfo->list =
		    (int *)((long)shmad + nodesum * sizeof(Pinfo) +
			    (sizeof(int) * BVAL * i));
		for (j = 0; j < BVAL; j++)
			*(pinfo->list + j) = -1;
	}
	shmaddr = shmad;
}

/*
 * Set up Signal handler and which signals to catch
 */
void set_signals(void *sighandler())
{
	int i;
	int rc;

	struct sigaction action;

	/* list of signals we want to catch */
	static struct signalinfo {
		int signum;
		char *signame;
	} siginfo[] = {
		{
		SIGHUP, "SIGHUP"}, {
		SIGINT, "SIGINT"}, {
		SIGQUIT, "SIGQUIT"}, {
		SIGABRT, "SIGABRT"}, {
		SIGBUS, "SIGBUS"}, {
		SIGSEGV, "SIGSEGV"}, {
		SIGALRM, "SIGALRM"}, {
		SIGUSR1, "SIGUSR1"}, {
		SIGUSR2, "SIGUSR2"}, {
		-1, "ENDSIG"}
	};

	char tmpstr[1024];

	action.sa_handler = (void *)sighandler;

#ifdef _LINUX
	sigfillset(&action.sa_mask);
#else
	SIGINITSET(action.sa_mask);
#endif
	action.sa_flags = 0;

	/* Set the signal handler up */
#ifdef _LINUX
	sigaddset(&action.sa_mask, SIGTERM);
#else
	SIGADDSET(action.sa_mask, SIGTERM);
#endif
	for (i = 0; siginfo[i].signum != -1; i++) {
#ifdef _LINUX
		sigaddset(&action.sa_mask, siginfo[i].signum);
#else
		SIGADDSET(action.sa_mask, siginfo[i].signum);
#endif
		rc = sigaction(siginfo[i].signum, &action, NULL);
		if (rc == -1) {
			sprintf(tmpstr, "sigaction: %s\n", siginfo[i].signame);
			perror(tmpstr);
			fprintf(stderr,
				" SEVERE : Could not set %s signal action, errno=%d.",
				siginfo[i].signame, errno);
			exit(1);
		}
	}
}