/* $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

#ident "$Header: dumpsema.c 1.1 89/04/19 $"

/*
 * dumpsema.c
 *	Provide interface to System V semaphores.
 *
 * The interface is a little more familiar to someone used to
 * Dynix semaphore primatives.
 */

#include <sys/termio.h> 
#include <sys/types.h> 
#include <sys/ipc.h> 
#include <sys/sem.h> 
#include <unistd.h> 

static int	Sem;
static struct 	sembuf Sops[3];

/*
 * Create a semphore to synchronise child to child.
 */

setup_semas(n)
{
	if ((Sem = semget(IPC_PRIVATE,n,IPC_CREAT)) == -1) {
		msg("Getting new semaphores failed:  ");
		(void)perror("semget ");
		exit(1);
	}
}

/*
 * set semaphore to an initial value.
 */

init_sema(n,v)
{
	if (semctl(Sem,n,SETVAL,v) == -1) {
		msg("Initializing semaphores failed:  ");
		(void)perror("semctl ");
		exit(1);
	}
}

p_sema(n)
{
	Sops[0].sem_num = n;
	Sops[0].sem_op = -1;
	Sops[0].sem_flg = SEM_UNDO;
	if (semop(Sem, Sops, 1) == -1) {
		msg("Failure to acquire semaphore.  Dump probably corrupt. ");
		(void)perror("semop");
	}
}

v_sema(n)
{
	Sops[0].sem_num = n;
	Sops[0].sem_op = 1;
	Sops[0].sem_flg = SEM_UNDO;
	if (semop(Sem, Sops, 1) == -1) {
		msg("Failure to release semaphore.  Dump probably corrupt. ");
		(void)perror("semop");
	}
}

/*
 * deletes all the semaphores.
 */

rm_sema() 
{
	semctl(Sem,0,IPC_RMID,0);
}
