17c478bd9Sstevel@tonic-gate /*
2*e9af4bc0SJohn Beck * Copyright (c) 2000-2001, 2005, 2008 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 #include <sm/gen.h>
11*e9af4bc0SJohn Beck SM_RCSID("@(#)$Id: sem.c,v 1.14 2008/05/30 16:26:38 ca Exp $")
127c478bd9Sstevel@tonic-gate
137c478bd9Sstevel@tonic-gate #if SM_CONF_SEM
147c478bd9Sstevel@tonic-gate # include <stdlib.h>
157c478bd9Sstevel@tonic-gate # include <unistd.h>
16*e9af4bc0SJohn Beck # include <sm/string.h>
177c478bd9Sstevel@tonic-gate # include <sm/sem.h>
187c478bd9Sstevel@tonic-gate # include <sm/heap.h>
1949218d4fSjbeck # include <errno.h>
207c478bd9Sstevel@tonic-gate
217c478bd9Sstevel@tonic-gate /*
227c478bd9Sstevel@tonic-gate ** SM_SEM_START -- initialize semaphores
237c478bd9Sstevel@tonic-gate **
247c478bd9Sstevel@tonic-gate ** Parameters:
257c478bd9Sstevel@tonic-gate ** key -- key for semaphores.
267c478bd9Sstevel@tonic-gate ** nsem -- number of semaphores.
277c478bd9Sstevel@tonic-gate ** semflg -- flag for semget(), if 0, use a default.
287c478bd9Sstevel@tonic-gate ** owner -- create semaphores.
297c478bd9Sstevel@tonic-gate **
307c478bd9Sstevel@tonic-gate ** Returns:
317c478bd9Sstevel@tonic-gate ** id for semaphores.
327c478bd9Sstevel@tonic-gate ** < 0 on failure.
337c478bd9Sstevel@tonic-gate */
347c478bd9Sstevel@tonic-gate
357c478bd9Sstevel@tonic-gate int
367c478bd9Sstevel@tonic-gate sm_sem_start(key, nsem, semflg, owner)
377c478bd9Sstevel@tonic-gate key_t key;
387c478bd9Sstevel@tonic-gate int nsem;
397c478bd9Sstevel@tonic-gate int semflg;
407c478bd9Sstevel@tonic-gate bool owner;
417c478bd9Sstevel@tonic-gate {
4249218d4fSjbeck int semid, i, err;
437c478bd9Sstevel@tonic-gate unsigned short *semvals;
447c478bd9Sstevel@tonic-gate
457c478bd9Sstevel@tonic-gate semvals = NULL;
467c478bd9Sstevel@tonic-gate if (semflg == 0)
477c478bd9Sstevel@tonic-gate semflg = (SEM_A|SEM_R)|((SEM_A|SEM_R) >> 3);
487c478bd9Sstevel@tonic-gate if (owner)
497c478bd9Sstevel@tonic-gate semflg |= IPC_CREAT|IPC_EXCL;
507c478bd9Sstevel@tonic-gate semid = semget(key, nsem, semflg);
517c478bd9Sstevel@tonic-gate if (semid < 0)
527c478bd9Sstevel@tonic-gate goto error;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate if (owner)
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate union semun semarg;
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate semvals = (unsigned short *) sm_malloc(nsem * sizeof semvals);
597c478bd9Sstevel@tonic-gate if (semvals == NULL)
607c478bd9Sstevel@tonic-gate goto error;
617c478bd9Sstevel@tonic-gate semarg.array = semvals;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate /* initialize semaphore values to be available */
647c478bd9Sstevel@tonic-gate for (i = 0; i < nsem; i++)
657c478bd9Sstevel@tonic-gate semvals[i] = 1;
667c478bd9Sstevel@tonic-gate if (semctl(semid, 0, SETALL, semarg) < 0)
677c478bd9Sstevel@tonic-gate goto error;
687c478bd9Sstevel@tonic-gate }
697c478bd9Sstevel@tonic-gate return semid;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate error:
7249218d4fSjbeck err = errno;
737c478bd9Sstevel@tonic-gate if (semvals != NULL)
747c478bd9Sstevel@tonic-gate sm_free(semvals);
757c478bd9Sstevel@tonic-gate if (semid >= 0)
767c478bd9Sstevel@tonic-gate sm_sem_stop(semid);
7749218d4fSjbeck return (err > 0) ? (0 - err) : -1;
787c478bd9Sstevel@tonic-gate }
797c478bd9Sstevel@tonic-gate
807c478bd9Sstevel@tonic-gate /*
817c478bd9Sstevel@tonic-gate ** SM_SEM_STOP -- stop using semaphores.
827c478bd9Sstevel@tonic-gate **
837c478bd9Sstevel@tonic-gate ** Parameters:
847c478bd9Sstevel@tonic-gate ** semid -- id for semaphores.
857c478bd9Sstevel@tonic-gate **
867c478bd9Sstevel@tonic-gate ** Returns:
877c478bd9Sstevel@tonic-gate ** 0 on success.
887c478bd9Sstevel@tonic-gate ** < 0 on failure.
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate
917c478bd9Sstevel@tonic-gate int
sm_sem_stop(semid)927c478bd9Sstevel@tonic-gate sm_sem_stop(semid)
937c478bd9Sstevel@tonic-gate int semid;
947c478bd9Sstevel@tonic-gate {
957c478bd9Sstevel@tonic-gate return semctl(semid, 0, IPC_RMID, NULL);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /*
997c478bd9Sstevel@tonic-gate ** SM_SEM_ACQ -- acquire semaphore.
1007c478bd9Sstevel@tonic-gate **
1017c478bd9Sstevel@tonic-gate ** Parameters:
1027c478bd9Sstevel@tonic-gate ** semid -- id for semaphores.
1037c478bd9Sstevel@tonic-gate ** semnum -- number of semaphore.
1047c478bd9Sstevel@tonic-gate ** timeout -- how long to wait for operation to succeed.
1057c478bd9Sstevel@tonic-gate **
1067c478bd9Sstevel@tonic-gate ** Returns:
1077c478bd9Sstevel@tonic-gate ** 0 on success.
1087c478bd9Sstevel@tonic-gate ** < 0 on failure.
1097c478bd9Sstevel@tonic-gate */
1107c478bd9Sstevel@tonic-gate
1117c478bd9Sstevel@tonic-gate int
sm_sem_acq(semid,semnum,timeout)1127c478bd9Sstevel@tonic-gate sm_sem_acq(semid, semnum, timeout)
1137c478bd9Sstevel@tonic-gate int semid;
1147c478bd9Sstevel@tonic-gate int semnum;
1157c478bd9Sstevel@tonic-gate int timeout;
1167c478bd9Sstevel@tonic-gate {
1177c478bd9Sstevel@tonic-gate int r;
1187c478bd9Sstevel@tonic-gate struct sembuf semops[1];
1197c478bd9Sstevel@tonic-gate
1207c478bd9Sstevel@tonic-gate semops[0].sem_num = semnum;
1217c478bd9Sstevel@tonic-gate semops[0].sem_op = -1;
1227c478bd9Sstevel@tonic-gate semops[0].sem_flg = SEM_UNDO |
1237c478bd9Sstevel@tonic-gate (timeout != SM_TIME_FOREVER ? 0 : IPC_NOWAIT);
1247c478bd9Sstevel@tonic-gate if (timeout == SM_TIME_IMMEDIATE || timeout == SM_TIME_FOREVER)
1257c478bd9Sstevel@tonic-gate return semop(semid, semops, 1);
1267c478bd9Sstevel@tonic-gate do
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate r = semop(semid, semops, 1);
1297c478bd9Sstevel@tonic-gate if (r == 0)
1307c478bd9Sstevel@tonic-gate return r;
1317c478bd9Sstevel@tonic-gate sleep(1);
1327c478bd9Sstevel@tonic-gate --timeout;
1337c478bd9Sstevel@tonic-gate } while (timeout > 0);
1347c478bd9Sstevel@tonic-gate return r;
1357c478bd9Sstevel@tonic-gate }
1367c478bd9Sstevel@tonic-gate
1377c478bd9Sstevel@tonic-gate /*
1387c478bd9Sstevel@tonic-gate ** SM_SEM_REL -- release semaphore.
1397c478bd9Sstevel@tonic-gate **
1407c478bd9Sstevel@tonic-gate ** Parameters:
1417c478bd9Sstevel@tonic-gate ** semid -- id for semaphores.
1427c478bd9Sstevel@tonic-gate ** semnum -- number of semaphore.
1437c478bd9Sstevel@tonic-gate ** timeout -- how long to wait for operation to succeed.
1447c478bd9Sstevel@tonic-gate **
1457c478bd9Sstevel@tonic-gate ** Returns:
1467c478bd9Sstevel@tonic-gate ** 0 on success.
1477c478bd9Sstevel@tonic-gate ** < 0 on failure.
1487c478bd9Sstevel@tonic-gate */
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate int
sm_sem_rel(semid,semnum,timeout)1517c478bd9Sstevel@tonic-gate sm_sem_rel(semid, semnum, timeout)
1527c478bd9Sstevel@tonic-gate int semid;
1537c478bd9Sstevel@tonic-gate int semnum;
1547c478bd9Sstevel@tonic-gate int timeout;
1557c478bd9Sstevel@tonic-gate {
1567c478bd9Sstevel@tonic-gate int r;
1577c478bd9Sstevel@tonic-gate struct sembuf semops[1];
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate #if PARANOID
1607c478bd9Sstevel@tonic-gate /* XXX should we check whether the value is already 0 ? */
1617c478bd9Sstevel@tonic-gate SM_REQUIRE(sm_get_sem(semid, semnum) > 0);
1627c478bd9Sstevel@tonic-gate #endif /* PARANOID */
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate semops[0].sem_num = semnum;
1657c478bd9Sstevel@tonic-gate semops[0].sem_op = 1;
1667c478bd9Sstevel@tonic-gate semops[0].sem_flg = SEM_UNDO |
1677c478bd9Sstevel@tonic-gate (timeout != SM_TIME_FOREVER ? 0 : IPC_NOWAIT);
1687c478bd9Sstevel@tonic-gate if (timeout == SM_TIME_IMMEDIATE || timeout == SM_TIME_FOREVER)
1697c478bd9Sstevel@tonic-gate return semop(semid, semops, 1);
1707c478bd9Sstevel@tonic-gate do
1717c478bd9Sstevel@tonic-gate {
1727c478bd9Sstevel@tonic-gate r = semop(semid, semops, 1);
1737c478bd9Sstevel@tonic-gate if (r == 0)
1747c478bd9Sstevel@tonic-gate return r;
1757c478bd9Sstevel@tonic-gate sleep(1);
1767c478bd9Sstevel@tonic-gate --timeout;
1777c478bd9Sstevel@tonic-gate } while (timeout > 0);
1787c478bd9Sstevel@tonic-gate return r;
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate ** SM_SEM_GET -- get semaphore value.
1837c478bd9Sstevel@tonic-gate **
1847c478bd9Sstevel@tonic-gate ** Parameters:
1857c478bd9Sstevel@tonic-gate ** semid -- id for semaphores.
1867c478bd9Sstevel@tonic-gate ** semnum -- number of semaphore.
1877c478bd9Sstevel@tonic-gate **
1887c478bd9Sstevel@tonic-gate ** Returns:
1897c478bd9Sstevel@tonic-gate ** value of semaphore on success.
1907c478bd9Sstevel@tonic-gate ** < 0 on failure.
1917c478bd9Sstevel@tonic-gate */
1927c478bd9Sstevel@tonic-gate
1937c478bd9Sstevel@tonic-gate int
sm_sem_get(semid,semnum)1947c478bd9Sstevel@tonic-gate sm_sem_get(semid, semnum)
1957c478bd9Sstevel@tonic-gate int semid;
1967c478bd9Sstevel@tonic-gate int semnum;
1977c478bd9Sstevel@tonic-gate {
1987c478bd9Sstevel@tonic-gate int semval;
1997c478bd9Sstevel@tonic-gate
2007c478bd9Sstevel@tonic-gate if ((semval = semctl(semid, semnum, GETVAL, NULL)) < 0)
2017c478bd9Sstevel@tonic-gate return -1;
2027c478bd9Sstevel@tonic-gate return semval;
2037c478bd9Sstevel@tonic-gate }
204*e9af4bc0SJohn Beck
205*e9af4bc0SJohn Beck /*
206*e9af4bc0SJohn Beck ** SM_SEMSETOWNER -- set owner/group/mode of semaphores.
207*e9af4bc0SJohn Beck **
208*e9af4bc0SJohn Beck ** Parameters:
209*e9af4bc0SJohn Beck ** semid -- id for semaphores.
210*e9af4bc0SJohn Beck ** uid -- uid to use
211*e9af4bc0SJohn Beck ** gid -- gid to use
212*e9af4bc0SJohn Beck ** mode -- mode to use
213*e9af4bc0SJohn Beck **
214*e9af4bc0SJohn Beck ** Returns:
215*e9af4bc0SJohn Beck ** 0 on success.
216*e9af4bc0SJohn Beck ** < 0 on failure.
217*e9af4bc0SJohn Beck */
218*e9af4bc0SJohn Beck
219*e9af4bc0SJohn Beck int
sm_semsetowner(semid,uid,gid,mode)220*e9af4bc0SJohn Beck sm_semsetowner(semid, uid, gid, mode)
221*e9af4bc0SJohn Beck int semid;
222*e9af4bc0SJohn Beck uid_t uid;
223*e9af4bc0SJohn Beck gid_t gid;
224*e9af4bc0SJohn Beck mode_t mode;
225*e9af4bc0SJohn Beck {
226*e9af4bc0SJohn Beck int r;
227*e9af4bc0SJohn Beck struct semid_ds semidds;
228*e9af4bc0SJohn Beck union semun {
229*e9af4bc0SJohn Beck int val;
230*e9af4bc0SJohn Beck struct semid_ds *buf;
231*e9af4bc0SJohn Beck ushort *array;
232*e9af4bc0SJohn Beck } arg;
233*e9af4bc0SJohn Beck
234*e9af4bc0SJohn Beck memset(&semidds, 0, sizeof(semidds));
235*e9af4bc0SJohn Beck arg.buf = &semidds;
236*e9af4bc0SJohn Beck if ((r = semctl(semid, 1, IPC_STAT, arg)) < 0)
237*e9af4bc0SJohn Beck return r;
238*e9af4bc0SJohn Beck semidds.sem_perm.uid = uid;
239*e9af4bc0SJohn Beck semidds.sem_perm.gid = gid;
240*e9af4bc0SJohn Beck semidds.sem_perm.mode = mode;
241*e9af4bc0SJohn Beck if ((r = semctl(semid, 1, IPC_SET, arg)) < 0)
242*e9af4bc0SJohn Beck return r;
243*e9af4bc0SJohn Beck return 0;
244*e9af4bc0SJohn Beck }
2457c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SEM */
246