freebsd-skq/contrib/sendmail/libsm/shm.c
gshapiro e226f8e879 Import of post-8.12.3 bug fixes from vendor repository.
These are being imported for the upcoming FreeBSD 4.6 release.
2002-04-20 20:31:51 +00:00

103 lines
2.0 KiB
C

/*
* Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*/
#include <sm/gen.h>
SM_RCSID("@(#)$Id: shm.c,v 1.1.1.1 2002/02/17 21:56:43 gshapiro Exp $")
#if SM_CONF_SHM
# include <stdlib.h>
# include <unistd.h>
# include <errno.h>
# include <sm/shm.h>
/*
** SM_SHMSTART -- initialize shared memory segment.
**
** Parameters:
** key -- key for shared memory.
** size -- size of segment.
** shmflag -- initial flags.
** shmid -- pointer to return id.
** owner -- create segment.
**
** Returns:
** pointer to shared memory segment,
** NULL on failure.
**
** Side Effects:
** attaches shared memory segment.
*/
void *
sm_shmstart(key, size, shmflg, shmid, owner)
key_t key;
int size;
int shmflg;
int *shmid;
bool owner;
{
int save_errno;
void *shm = SM_SHM_NULL;
/* default: user/group accessible */
if (shmflg == 0)
shmflg = SHM_R|SHM_W|(SHM_R>>3)|(SHM_W>>3);
if (owner)
shmflg |= IPC_CREAT|IPC_EXCL;
*shmid = shmget(key, size, shmflg);
if (*shmid < 0)
goto error;
shm = shmat(*shmid, (void *) 0, 0);
if (shm == SM_SHM_NULL)
goto error;
return shm;
error:
save_errno = errno;
if (shm != SM_SHM_NULL || *shmid >= 0)
sm_shmstop(shm, *shmid, owner);
*shmid = SM_SHM_NO_ID;
errno = save_errno;
return (void *) 0;
}
/*
** SM_SHMSTOP -- stop using shared memory segment.
**
** Parameters:
** shm -- pointer to shared memory.
** shmid -- id.
** owner -- delete segment.
**
** Returns:
** 0 on success.
** < 0 on failure.
**
** Side Effects:
** detaches (and maybe removes) shared memory segment.
*/
int
sm_shmstop(shm, shmid, owner)
void *shm;
int shmid;
bool owner;
{
int r;
if (shm != SM_SHM_NULL && (r = shmdt(shm)) < 0)
return r;
if (owner && shmid >= 0 && (r = shmctl(shmid, IPC_RMID, NULL)) < 0)
return r;
return 0;
}
#endif /* SM_CONF_SHM */