140266059SGregory Neil Shapiro /*
25dd76dd0SGregory 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>
114313cc83SGregory 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>
18*2fb4f839SGregory Neil Shapiro # include <sm/conf.h>
1940266059SGregory Neil Shapiro # include <sm/shm.h>
2040266059SGregory Neil Shapiro
21a7ec597cSGregory Neil Shapiro
2240266059SGregory Neil Shapiro /*
2340266059SGregory Neil Shapiro ** SM_SHMSTART -- initialize shared memory segment.
2440266059SGregory Neil Shapiro **
2540266059SGregory Neil Shapiro ** Parameters:
2640266059SGregory Neil Shapiro ** key -- key for shared memory.
2740266059SGregory Neil Shapiro ** size -- size of segment.
2840266059SGregory Neil Shapiro ** shmflag -- initial flags.
2940266059SGregory Neil Shapiro ** shmid -- pointer to return id.
3040266059SGregory Neil Shapiro ** owner -- create segment.
3140266059SGregory Neil Shapiro **
3240266059SGregory Neil Shapiro ** Returns:
3340266059SGregory Neil Shapiro ** pointer to shared memory segment,
3440266059SGregory Neil Shapiro ** NULL on failure.
3540266059SGregory Neil Shapiro **
3640266059SGregory Neil Shapiro ** Side Effects:
3740266059SGregory Neil Shapiro ** attaches shared memory segment.
3840266059SGregory Neil Shapiro */
3940266059SGregory Neil Shapiro
4040266059SGregory Neil Shapiro void *
4140266059SGregory Neil Shapiro sm_shmstart(key, size, shmflg, shmid, owner)
4240266059SGregory Neil Shapiro key_t key;
4340266059SGregory Neil Shapiro int size;
4440266059SGregory Neil Shapiro int shmflg;
4540266059SGregory Neil Shapiro int *shmid;
4640266059SGregory Neil Shapiro bool owner;
4740266059SGregory Neil Shapiro {
4840266059SGregory Neil Shapiro int save_errno;
4940266059SGregory Neil Shapiro void *shm = SM_SHM_NULL;
5040266059SGregory Neil Shapiro
5140266059SGregory Neil Shapiro /* default: user/group accessible */
5240266059SGregory Neil Shapiro if (shmflg == 0)
5340266059SGregory Neil Shapiro shmflg = SHM_R|SHM_W|(SHM_R>>3)|(SHM_W>>3);
5440266059SGregory Neil Shapiro if (owner)
5540266059SGregory Neil Shapiro shmflg |= IPC_CREAT|IPC_EXCL;
5640266059SGregory Neil Shapiro *shmid = shmget(key, size, shmflg);
5740266059SGregory Neil Shapiro if (*shmid < 0)
5840266059SGregory Neil Shapiro goto error;
5940266059SGregory Neil Shapiro
6040266059SGregory Neil Shapiro shm = shmat(*shmid, (void *) 0, 0);
6140266059SGregory Neil Shapiro if (shm == SM_SHM_NULL)
6240266059SGregory Neil Shapiro goto error;
6340266059SGregory Neil Shapiro
6440266059SGregory Neil Shapiro return shm;
6540266059SGregory Neil Shapiro
6640266059SGregory Neil Shapiro error:
6740266059SGregory Neil Shapiro save_errno = errno;
6840266059SGregory Neil Shapiro if (shm != SM_SHM_NULL || *shmid >= 0)
6940266059SGregory Neil Shapiro sm_shmstop(shm, *shmid, owner);
7040266059SGregory Neil Shapiro *shmid = SM_SHM_NO_ID;
7140266059SGregory Neil Shapiro errno = save_errno;
7240266059SGregory Neil Shapiro return (void *) 0;
7340266059SGregory Neil Shapiro }
7440266059SGregory Neil Shapiro
75a7ec597cSGregory Neil Shapiro
7640266059SGregory Neil Shapiro /*
7740266059SGregory Neil Shapiro ** SM_SHMSTOP -- stop using shared memory segment.
7840266059SGregory Neil Shapiro **
7940266059SGregory Neil Shapiro ** Parameters:
8040266059SGregory Neil Shapiro ** shm -- pointer to shared memory.
8140266059SGregory Neil Shapiro ** shmid -- id.
8240266059SGregory Neil Shapiro ** owner -- delete segment.
8340266059SGregory Neil Shapiro **
8440266059SGregory Neil Shapiro ** Returns:
8540266059SGregory Neil Shapiro ** 0 on success.
8640266059SGregory Neil Shapiro ** < 0 on failure.
8740266059SGregory Neil Shapiro **
8840266059SGregory Neil Shapiro ** Side Effects:
8940266059SGregory Neil Shapiro ** detaches (and maybe removes) shared memory segment.
9040266059SGregory Neil Shapiro */
9140266059SGregory Neil Shapiro
92a7ec597cSGregory Neil Shapiro
9340266059SGregory Neil Shapiro int
sm_shmstop(shm,shmid,owner)9440266059SGregory Neil Shapiro sm_shmstop(shm, shmid, owner)
9540266059SGregory Neil Shapiro void *shm;
9640266059SGregory Neil Shapiro int shmid;
9740266059SGregory Neil Shapiro bool owner;
9840266059SGregory Neil Shapiro {
9940266059SGregory Neil Shapiro int r;
10040266059SGregory Neil Shapiro
10140266059SGregory Neil Shapiro if (shm != SM_SHM_NULL && (r = shmdt(shm)) < 0)
10240266059SGregory Neil Shapiro return r;
10340266059SGregory Neil Shapiro if (owner && shmid >= 0 && (r = shmctl(shmid, IPC_RMID, NULL)) < 0)
10440266059SGregory Neil Shapiro return r;
10540266059SGregory Neil Shapiro return 0;
10640266059SGregory Neil Shapiro }
107a7ec597cSGregory Neil Shapiro
108a7ec597cSGregory Neil Shapiro
10913d88268SGregory Neil Shapiro /*
11013d88268SGregory Neil Shapiro ** SM_SHMSETOWNER -- set owner/group/mode of shared memory segment.
11113d88268SGregory Neil Shapiro **
11213d88268SGregory Neil Shapiro ** Parameters:
11313d88268SGregory Neil Shapiro ** shmid -- id.
11413d88268SGregory Neil Shapiro ** uid -- uid to use
11513d88268SGregory Neil Shapiro ** gid -- gid to use
11613d88268SGregory Neil Shapiro ** mode -- mode to use
11713d88268SGregory Neil Shapiro **
11813d88268SGregory Neil Shapiro ** Returns:
11913d88268SGregory Neil Shapiro ** 0 on success.
12013d88268SGregory Neil Shapiro ** < 0 on failure.
12113d88268SGregory Neil Shapiro */
12213d88268SGregory Neil Shapiro
123*2fb4f839SGregory Neil Shapiro #ifdef __STDC__
124*2fb4f839SGregory Neil Shapiro int
sm_shmsetowner(int shmid,uid_t uid,gid_t gid,MODE_T mode)125*2fb4f839SGregory Neil Shapiro sm_shmsetowner(int shmid, uid_t uid, gid_t gid, MODE_T mode)
126*2fb4f839SGregory Neil Shapiro #else /* __STDC__ */
12713d88268SGregory Neil Shapiro int
12813d88268SGregory Neil Shapiro sm_shmsetowner(shmid, uid, gid, mode)
12913d88268SGregory Neil Shapiro int shmid;
13013d88268SGregory Neil Shapiro uid_t uid;
13113d88268SGregory Neil Shapiro gid_t gid;
132*2fb4f839SGregory Neil Shapiro MODE_T mode;
133*2fb4f839SGregory Neil Shapiro #endif /* __STDC__ */
13413d88268SGregory Neil Shapiro {
13513d88268SGregory Neil Shapiro int r;
13613d88268SGregory Neil Shapiro struct shmid_ds shmid_ds;
13713d88268SGregory Neil Shapiro
13813d88268SGregory Neil Shapiro memset(&shmid_ds, 0, sizeof(shmid_ds));
13913d88268SGregory Neil Shapiro if ((r = shmctl(shmid, IPC_STAT, &shmid_ds)) < 0)
14013d88268SGregory Neil Shapiro return r;
14113d88268SGregory Neil Shapiro shmid_ds.shm_perm.uid = uid;
14213d88268SGregory Neil Shapiro shmid_ds.shm_perm.gid = gid;
14313d88268SGregory Neil Shapiro shmid_ds.shm_perm.mode = mode;
14413d88268SGregory Neil Shapiro if ((r = shmctl(shmid, IPC_SET, &shmid_ds)) < 0)
14513d88268SGregory Neil Shapiro return r;
14613d88268SGregory Neil Shapiro return 0;
14713d88268SGregory Neil Shapiro }
14840266059SGregory Neil Shapiro #endif /* SM_CONF_SHM */
149