8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sendmail / libsm / shm.c
blob59d879e61bcb0b481ff2976065afe3e835e49c3d
1 /*
2 * Copyright (c) 2000-2001, 2003, 2005 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
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 */
10 #pragma ident "%Z%%M% %I% %E% SMI"
12 #include <sm/gen.h>
13 SM_RCSID("@(#)$Id: shm.c,v 1.19 2005/07/14 22:34:28 ca Exp $")
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>
24 ** SM_SHMSTART -- initialize shared memory segment.
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.
33 ** Returns:
34 ** pointer to shared memory segment,
35 ** NULL on failure.
37 ** Side Effects:
38 ** attaches shared memory segment.
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;
49 int save_errno;
50 void *shm = SM_SHM_NULL;
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;
61 shm = shmat(*shmid, (void *) 0, 0);
62 if (shm == SM_SHM_NULL)
63 goto error;
65 return shm;
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;
78 ** SM_SHMSTOP -- stop using shared memory segment.
80 ** Parameters:
81 ** shm -- pointer to shared memory.
82 ** shmid -- id.
83 ** owner -- delete segment.
85 ** Returns:
86 ** 0 on success.
87 ** < 0 on failure.
89 ** Side Effects:
90 ** detaches (and maybe removes) shared memory segment.
94 int
95 sm_shmstop(shm, shmid, owner)
96 void *shm;
97 int shmid;
98 bool owner;
100 int r;
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;
111 ** SM_SHMSETOWNER -- set owner/group/mode of shared memory segment.
113 ** Parameters:
114 ** shmid -- id.
115 ** uid -- uid to use
116 ** gid -- gid to use
117 ** mode -- mode to use
119 ** Returns:
120 ** 0 on success.
121 ** < 0 on failure.
125 sm_shmsetowner(shmid, uid, gid, mode)
126 int shmid;
127 uid_t uid;
128 gid_t gid;
129 mode_t mode;
131 int r;
132 struct shmid_ds shmid_ds;
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;
144 #endif /* SM_CONF_SHM */