xref: /titanic_51/usr/src/cmd/sendmail/libsm/shm.c (revision ace1a5f11236a072fca1b5e0ea1416a083a9f2aa)
1 /*
2  * Copyright (c) 2000-2001, 2003, 2005 Sendmail, Inc. and its suppliers.
3  *      All rights reserved.
4  *
5  * By using this file, you agree to the terms and conditions set
6  * forth in the LICENSE file which can be found at the top level of
7  * the sendmail distribution.
8  */
9 
10 #pragma ident	"%Z%%M%	%I%	%E% SMI"
11 
12 #include <sm/gen.h>
13 SM_RCSID("@(#)$Id: shm.c,v 1.19 2005/07/14 22:34:28 ca Exp $")
14 
15 #if SM_CONF_SHM
16 # include <stdlib.h>
17 # include <unistd.h>
18 # include <errno.h>
19 # include <sm/string.h>
20 # include <sm/shm.h>
21 
22 
23 /*
24 **  SM_SHMSTART -- initialize shared memory segment.
25 **
26 **	Parameters:
27 **		key -- key for shared memory.
28 **		size -- size of segment.
29 **		shmflag -- initial flags.
30 **		shmid -- pointer to return id.
31 **		owner -- create segment.
32 **
33 **	Returns:
34 **		pointer to shared memory segment,
35 **		NULL on failure.
36 **
37 **	Side Effects:
38 **		attaches shared memory segment.
39 */
40 
41 void *
42 sm_shmstart(key, size, shmflg, shmid, owner)
43 	key_t key;
44 	int size;
45 	int shmflg;
46 	int *shmid;
47 	bool owner;
48 {
49 	int save_errno;
50 	void *shm = SM_SHM_NULL;
51 
52 	/* default: user/group accessible */
53 	if (shmflg == 0)
54 		shmflg = SHM_R|SHM_W|(SHM_R>>3)|(SHM_W>>3);
55 	if (owner)
56 		shmflg |= IPC_CREAT|IPC_EXCL;
57 	*shmid = shmget(key, size, shmflg);
58 	if (*shmid < 0)
59 		goto error;
60 
61 	shm = shmat(*shmid, (void *) 0, 0);
62 	if (shm == SM_SHM_NULL)
63 		goto error;
64 
65 	return shm;
66 
67   error:
68 	save_errno = errno;
69 	if (shm != SM_SHM_NULL || *shmid >= 0)
70 		sm_shmstop(shm, *shmid, owner);
71 	*shmid = SM_SHM_NO_ID;
72 	errno = save_errno;
73 	return (void *) 0;
74 }
75 
76 
77 /*
78 **  SM_SHMSTOP -- stop using shared memory segment.
79 **
80 **	Parameters:
81 **		shm -- pointer to shared memory.
82 **		shmid -- id.
83 **		owner -- delete segment.
84 **
85 **	Returns:
86 **		0 on success.
87 **		< 0 on failure.
88 **
89 **	Side Effects:
90 **		detaches (and maybe removes) shared memory segment.
91 */
92 
93 
94 int
95 sm_shmstop(shm, shmid, owner)
96 	void *shm;
97 	int shmid;
98 	bool owner;
99 {
100 	int r;
101 
102 	if (shm != SM_SHM_NULL && (r = shmdt(shm)) < 0)
103 		return r;
104 	if (owner && shmid >= 0 && (r = shmctl(shmid, IPC_RMID, NULL)) < 0)
105 		return r;
106 	return 0;
107 }
108 
109 
110 /*
111 **  SM_SHMSETOWNER -- set owner/group/mode of shared memory segment.
112 **
113 **	Parameters:
114 **		shmid -- id.
115 **		uid -- uid to use
116 **		gid -- gid to use
117 **		mode -- mode to use
118 **
119 **	Returns:
120 **		0 on success.
121 **		< 0 on failure.
122 */
123 
124 int
125 sm_shmsetowner(shmid, uid, gid, mode)
126 	int shmid;
127 	uid_t uid;
128 	gid_t gid;
129 	mode_t mode;
130 {
131 	int r;
132 	struct shmid_ds shmid_ds;
133 
134 	memset(&shmid_ds, 0, sizeof(shmid_ds));
135 	if ((r = shmctl(shmid, IPC_STAT, &shmid_ds)) < 0)
136 		return r;
137 	shmid_ds.shm_perm.uid = uid;
138 	shmid_ds.shm_perm.gid = gid;
139 	shmid_ds.shm_perm.mode = mode;
140 	if ((r = shmctl(shmid, IPC_SET, &shmid_ds)) < 0)
141 		return r;
142 	return 0;
143 }
144 #endif /* SM_CONF_SHM */
145