xref: /freebsd/contrib/sendmail/libsm/shm.c (revision 5dd76dd0cc19450133aa379ce0ce4a68ae07fb39)
140266059SGregory Neil Shapiro /*
2*5dd76dd0SGregory Neil Shapiro  * Copyright (c) 2000-2001, 2003, 2005 Proofpoint, Inc. and its suppliers.
340266059SGregory Neil Shapiro  *      All rights reserved.
440266059SGregory Neil Shapiro  *
540266059SGregory Neil Shapiro  * By using this file, you agree to the terms and conditions set
640266059SGregory Neil Shapiro  * forth in the LICENSE file which can be found at the top level of
740266059SGregory Neil Shapiro  * the sendmail distribution.
840266059SGregory Neil Shapiro  */
940266059SGregory Neil Shapiro 
1040266059SGregory Neil Shapiro #include <sm/gen.h>
11*5dd76dd0SGregory Neil Shapiro SM_RCSID("@(#)$Id: shm.c,v 1.20 2013/11/22 20:51:43 ca Exp $")
1240266059SGregory Neil Shapiro 
1340266059SGregory Neil Shapiro #if SM_CONF_SHM
1440266059SGregory Neil Shapiro # include <stdlib.h>
1540266059SGregory Neil Shapiro # include <unistd.h>
1640266059SGregory Neil Shapiro # include <errno.h>
174e4196cbSGregory Neil Shapiro # include <sm/string.h>
1840266059SGregory Neil Shapiro # include <sm/shm.h>
1940266059SGregory Neil Shapiro 
20a7ec597cSGregory Neil Shapiro 
2140266059SGregory Neil Shapiro /*
2240266059SGregory Neil Shapiro **  SM_SHMSTART -- initialize shared memory segment.
2340266059SGregory Neil Shapiro **
2440266059SGregory Neil Shapiro **	Parameters:
2540266059SGregory Neil Shapiro **		key -- key for shared memory.
2640266059SGregory Neil Shapiro **		size -- size of segment.
2740266059SGregory Neil Shapiro **		shmflag -- initial flags.
2840266059SGregory Neil Shapiro **		shmid -- pointer to return id.
2940266059SGregory Neil Shapiro **		owner -- create segment.
3040266059SGregory Neil Shapiro **
3140266059SGregory Neil Shapiro **	Returns:
3240266059SGregory Neil Shapiro **		pointer to shared memory segment,
3340266059SGregory Neil Shapiro **		NULL on failure.
3440266059SGregory Neil Shapiro **
3540266059SGregory Neil Shapiro **	Side Effects:
3640266059SGregory Neil Shapiro **		attaches shared memory segment.
3740266059SGregory Neil Shapiro */
3840266059SGregory Neil Shapiro 
3940266059SGregory Neil Shapiro void *
4040266059SGregory Neil Shapiro sm_shmstart(key, size, shmflg, shmid, owner)
4140266059SGregory Neil Shapiro 	key_t key;
4240266059SGregory Neil Shapiro 	int size;
4340266059SGregory Neil Shapiro 	int shmflg;
4440266059SGregory Neil Shapiro 	int *shmid;
4540266059SGregory Neil Shapiro 	bool owner;
4640266059SGregory Neil Shapiro {
4740266059SGregory Neil Shapiro 	int save_errno;
4840266059SGregory Neil Shapiro 	void *shm = SM_SHM_NULL;
4940266059SGregory Neil Shapiro 
5040266059SGregory Neil Shapiro 	/* default: user/group accessible */
5140266059SGregory Neil Shapiro 	if (shmflg == 0)
5240266059SGregory Neil Shapiro 		shmflg = SHM_R|SHM_W|(SHM_R>>3)|(SHM_W>>3);
5340266059SGregory Neil Shapiro 	if (owner)
5440266059SGregory Neil Shapiro 		shmflg |= IPC_CREAT|IPC_EXCL;
5540266059SGregory Neil Shapiro 	*shmid = shmget(key, size, shmflg);
5640266059SGregory Neil Shapiro 	if (*shmid < 0)
5740266059SGregory Neil Shapiro 		goto error;
5840266059SGregory Neil Shapiro 
5940266059SGregory Neil Shapiro 	shm = shmat(*shmid, (void *) 0, 0);
6040266059SGregory Neil Shapiro 	if (shm == SM_SHM_NULL)
6140266059SGregory Neil Shapiro 		goto error;
6240266059SGregory Neil Shapiro 
6340266059SGregory Neil Shapiro 	return shm;
6440266059SGregory Neil Shapiro 
6540266059SGregory Neil Shapiro   error:
6640266059SGregory Neil Shapiro 	save_errno = errno;
6740266059SGregory Neil Shapiro 	if (shm != SM_SHM_NULL || *shmid >= 0)
6840266059SGregory Neil Shapiro 		sm_shmstop(shm, *shmid, owner);
6940266059SGregory Neil Shapiro 	*shmid = SM_SHM_NO_ID;
7040266059SGregory Neil Shapiro 	errno = save_errno;
7140266059SGregory Neil Shapiro 	return (void *) 0;
7240266059SGregory Neil Shapiro }
7340266059SGregory Neil Shapiro 
74a7ec597cSGregory Neil Shapiro 
7540266059SGregory Neil Shapiro /*
7640266059SGregory Neil Shapiro **  SM_SHMSTOP -- stop using shared memory segment.
7740266059SGregory Neil Shapiro **
7840266059SGregory Neil Shapiro **	Parameters:
7940266059SGregory Neil Shapiro **		shm -- pointer to shared memory.
8040266059SGregory Neil Shapiro **		shmid -- id.
8140266059SGregory Neil Shapiro **		owner -- delete segment.
8240266059SGregory Neil Shapiro **
8340266059SGregory Neil Shapiro **	Returns:
8440266059SGregory Neil Shapiro **		0 on success.
8540266059SGregory Neil Shapiro **		< 0 on failure.
8640266059SGregory Neil Shapiro **
8740266059SGregory Neil Shapiro **	Side Effects:
8840266059SGregory Neil Shapiro **		detaches (and maybe removes) shared memory segment.
8940266059SGregory Neil Shapiro */
9040266059SGregory Neil Shapiro 
91a7ec597cSGregory Neil Shapiro 
9240266059SGregory Neil Shapiro int
9340266059SGregory Neil Shapiro sm_shmstop(shm, shmid, owner)
9440266059SGregory Neil Shapiro 	void *shm;
9540266059SGregory Neil Shapiro 	int shmid;
9640266059SGregory Neil Shapiro 	bool owner;
9740266059SGregory Neil Shapiro {
9840266059SGregory Neil Shapiro 	int r;
9940266059SGregory Neil Shapiro 
10040266059SGregory Neil Shapiro 	if (shm != SM_SHM_NULL && (r = shmdt(shm)) < 0)
10140266059SGregory Neil Shapiro 		return r;
10240266059SGregory Neil Shapiro 	if (owner && shmid >= 0 && (r = shmctl(shmid, IPC_RMID, NULL)) < 0)
10340266059SGregory Neil Shapiro 		return r;
10440266059SGregory Neil Shapiro 	return 0;
10540266059SGregory Neil Shapiro }
106a7ec597cSGregory Neil Shapiro 
107a7ec597cSGregory Neil Shapiro 
10813d88268SGregory Neil Shapiro /*
10913d88268SGregory Neil Shapiro **  SM_SHMSETOWNER -- set owner/group/mode of shared memory segment.
11013d88268SGregory Neil Shapiro **
11113d88268SGregory Neil Shapiro **	Parameters:
11213d88268SGregory Neil Shapiro **		shmid -- id.
11313d88268SGregory Neil Shapiro **		uid -- uid to use
11413d88268SGregory Neil Shapiro **		gid -- gid to use
11513d88268SGregory Neil Shapiro **		mode -- mode to use
11613d88268SGregory Neil Shapiro **
11713d88268SGregory Neil Shapiro **	Returns:
11813d88268SGregory Neil Shapiro **		0 on success.
11913d88268SGregory Neil Shapiro **		< 0 on failure.
12013d88268SGregory Neil Shapiro */
12113d88268SGregory Neil Shapiro 
12213d88268SGregory Neil Shapiro int
12313d88268SGregory Neil Shapiro sm_shmsetowner(shmid, uid, gid, mode)
12413d88268SGregory Neil Shapiro 	int shmid;
12513d88268SGregory Neil Shapiro 	uid_t uid;
12613d88268SGregory Neil Shapiro 	gid_t gid;
12713d88268SGregory Neil Shapiro 	mode_t mode;
12813d88268SGregory Neil Shapiro {
12913d88268SGregory Neil Shapiro 	int r;
13013d88268SGregory Neil Shapiro 	struct shmid_ds shmid_ds;
13113d88268SGregory Neil Shapiro 
13213d88268SGregory Neil Shapiro 	memset(&shmid_ds, 0, sizeof(shmid_ds));
13313d88268SGregory Neil Shapiro 	if ((r = shmctl(shmid, IPC_STAT, &shmid_ds)) < 0)
13413d88268SGregory Neil Shapiro 		return r;
13513d88268SGregory Neil Shapiro 	shmid_ds.shm_perm.uid = uid;
13613d88268SGregory Neil Shapiro 	shmid_ds.shm_perm.gid = gid;
13713d88268SGregory Neil Shapiro 	shmid_ds.shm_perm.mode = mode;
13813d88268SGregory Neil Shapiro 	if ((r = shmctl(shmid, IPC_SET, &shmid_ds)) < 0)
13913d88268SGregory Neil Shapiro 		return r;
14013d88268SGregory Neil Shapiro 	return 0;
14113d88268SGregory Neil Shapiro }
14240266059SGregory Neil Shapiro #endif /* SM_CONF_SHM */
143