xref: /titanic_44/usr/src/cmd/sendmail/libsm/shm.c (revision 49218d4f8e4d84d1c08aeb267bcf6e451f2056dc)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2000-2001, 2003, 2005 Sendmail, Inc. and its suppliers.
37c478bd9Sstevel@tonic-gate  *      All rights reserved.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
67c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
77c478bd9Sstevel@tonic-gate  * the sendmail distribution.
87c478bd9Sstevel@tonic-gate  */
97c478bd9Sstevel@tonic-gate 
107c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
117c478bd9Sstevel@tonic-gate 
127c478bd9Sstevel@tonic-gate #include <sm/gen.h>
13*49218d4fSjbeck SM_RCSID("@(#)$Id: shm.c,v 1.19 2005/07/14 22:34:28 ca Exp $")
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #if SM_CONF_SHM
167c478bd9Sstevel@tonic-gate # include <stdlib.h>
177c478bd9Sstevel@tonic-gate # include <unistd.h>
187c478bd9Sstevel@tonic-gate # include <errno.h>
19*49218d4fSjbeck # include <sm/string.h>
207c478bd9Sstevel@tonic-gate # include <sm/shm.h>
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
247c478bd9Sstevel@tonic-gate **  SM_SHMSTART -- initialize shared memory segment.
257c478bd9Sstevel@tonic-gate **
267c478bd9Sstevel@tonic-gate **	Parameters:
277c478bd9Sstevel@tonic-gate **		key -- key for shared memory.
287c478bd9Sstevel@tonic-gate **		size -- size of segment.
297c478bd9Sstevel@tonic-gate **		shmflag -- initial flags.
307c478bd9Sstevel@tonic-gate **		shmid -- pointer to return id.
317c478bd9Sstevel@tonic-gate **		owner -- create segment.
327c478bd9Sstevel@tonic-gate **
337c478bd9Sstevel@tonic-gate **	Returns:
347c478bd9Sstevel@tonic-gate **		pointer to shared memory segment,
357c478bd9Sstevel@tonic-gate **		NULL on failure.
367c478bd9Sstevel@tonic-gate **
377c478bd9Sstevel@tonic-gate **	Side Effects:
387c478bd9Sstevel@tonic-gate **		attaches shared memory segment.
397c478bd9Sstevel@tonic-gate */
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate void *
427c478bd9Sstevel@tonic-gate sm_shmstart(key, size, shmflg, shmid, owner)
437c478bd9Sstevel@tonic-gate 	key_t key;
447c478bd9Sstevel@tonic-gate 	int size;
457c478bd9Sstevel@tonic-gate 	int shmflg;
467c478bd9Sstevel@tonic-gate 	int *shmid;
477c478bd9Sstevel@tonic-gate 	bool owner;
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	int save_errno;
507c478bd9Sstevel@tonic-gate 	void *shm = SM_SHM_NULL;
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate 	/* default: user/group accessible */
537c478bd9Sstevel@tonic-gate 	if (shmflg == 0)
547c478bd9Sstevel@tonic-gate 		shmflg = SHM_R|SHM_W|(SHM_R>>3)|(SHM_W>>3);
557c478bd9Sstevel@tonic-gate 	if (owner)
567c478bd9Sstevel@tonic-gate 		shmflg |= IPC_CREAT|IPC_EXCL;
577c478bd9Sstevel@tonic-gate 	*shmid = shmget(key, size, shmflg);
587c478bd9Sstevel@tonic-gate 	if (*shmid < 0)
597c478bd9Sstevel@tonic-gate 		goto error;
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	shm = shmat(*shmid, (void *) 0, 0);
627c478bd9Sstevel@tonic-gate 	if (shm == SM_SHM_NULL)
637c478bd9Sstevel@tonic-gate 		goto error;
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	return shm;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate   error:
687c478bd9Sstevel@tonic-gate 	save_errno = errno;
697c478bd9Sstevel@tonic-gate 	if (shm != SM_SHM_NULL || *shmid >= 0)
707c478bd9Sstevel@tonic-gate 		sm_shmstop(shm, *shmid, owner);
717c478bd9Sstevel@tonic-gate 	*shmid = SM_SHM_NO_ID;
727c478bd9Sstevel@tonic-gate 	errno = save_errno;
737c478bd9Sstevel@tonic-gate 	return (void *) 0;
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate **  SM_SHMSTOP -- stop using shared memory segment.
797c478bd9Sstevel@tonic-gate **
807c478bd9Sstevel@tonic-gate **	Parameters:
817c478bd9Sstevel@tonic-gate **		shm -- pointer to shared memory.
827c478bd9Sstevel@tonic-gate **		shmid -- id.
837c478bd9Sstevel@tonic-gate **		owner -- delete segment.
847c478bd9Sstevel@tonic-gate **
857c478bd9Sstevel@tonic-gate **	Returns:
867c478bd9Sstevel@tonic-gate **		0 on success.
877c478bd9Sstevel@tonic-gate **		< 0 on failure.
887c478bd9Sstevel@tonic-gate **
897c478bd9Sstevel@tonic-gate **	Side Effects:
907c478bd9Sstevel@tonic-gate **		detaches (and maybe removes) shared memory segment.
917c478bd9Sstevel@tonic-gate */
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate int
sm_shmstop(shm,shmid,owner)957c478bd9Sstevel@tonic-gate sm_shmstop(shm, shmid, owner)
967c478bd9Sstevel@tonic-gate 	void *shm;
977c478bd9Sstevel@tonic-gate 	int shmid;
987c478bd9Sstevel@tonic-gate 	bool owner;
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	int r;
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	if (shm != SM_SHM_NULL && (r = shmdt(shm)) < 0)
1037c478bd9Sstevel@tonic-gate 		return r;
1047c478bd9Sstevel@tonic-gate 	if (owner && shmid >= 0 && (r = shmctl(shmid, IPC_RMID, NULL)) < 0)
1057c478bd9Sstevel@tonic-gate 		return r;
1067c478bd9Sstevel@tonic-gate 	return 0;
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate **  SM_SHMSETOWNER -- set owner/group/mode of shared memory segment.
1127c478bd9Sstevel@tonic-gate **
1137c478bd9Sstevel@tonic-gate **	Parameters:
1147c478bd9Sstevel@tonic-gate **		shmid -- id.
1157c478bd9Sstevel@tonic-gate **		uid -- uid to use
1167c478bd9Sstevel@tonic-gate **		gid -- gid to use
1177c478bd9Sstevel@tonic-gate **		mode -- mode to use
1187c478bd9Sstevel@tonic-gate **
1197c478bd9Sstevel@tonic-gate **	Returns:
1207c478bd9Sstevel@tonic-gate **		0 on success.
1217c478bd9Sstevel@tonic-gate **		< 0 on failure.
1227c478bd9Sstevel@tonic-gate */
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate int
sm_shmsetowner(shmid,uid,gid,mode)1257c478bd9Sstevel@tonic-gate sm_shmsetowner(shmid, uid, gid, mode)
1267c478bd9Sstevel@tonic-gate 	int shmid;
1277c478bd9Sstevel@tonic-gate 	uid_t uid;
1287c478bd9Sstevel@tonic-gate 	gid_t gid;
1297c478bd9Sstevel@tonic-gate 	mode_t mode;
1307c478bd9Sstevel@tonic-gate {
1317c478bd9Sstevel@tonic-gate 	int r;
1327c478bd9Sstevel@tonic-gate 	struct shmid_ds shmid_ds;
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate 	memset(&shmid_ds, 0, sizeof(shmid_ds));
1357c478bd9Sstevel@tonic-gate 	if ((r = shmctl(shmid, IPC_STAT, &shmid_ds)) < 0)
1367c478bd9Sstevel@tonic-gate 		return r;
1377c478bd9Sstevel@tonic-gate 	shmid_ds.shm_perm.uid = uid;
1387c478bd9Sstevel@tonic-gate 	shmid_ds.shm_perm.gid = gid;
1397c478bd9Sstevel@tonic-gate 	shmid_ds.shm_perm.mode = mode;
1407c478bd9Sstevel@tonic-gate 	if ((r = shmctl(shmid, IPC_SET, &shmid_ds)) < 0)
1417c478bd9Sstevel@tonic-gate 		return r;
1427c478bd9Sstevel@tonic-gate 	return 0;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SHM */
145