2 * Copyright (c) 2000-2001, 2005-2006 Sendmail, Inc. and its suppliers.
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.
11 SM_RCSID("@(#)$Id: t-sem.c,v 1.15 2006/03/13 20:40:43 msk Exp $")
18 # include <sysexits.h>
20 # include <sm/string.h>
21 # include <sm/signal.h>
33 fprintf(stderr, "sleep(%d) before %s\n", t, s);
38 fprintf(stderr, "%s\n", s);
44 ** SEMINTER -- interactive testing of semaphores.
47 ** owner -- create semaphores.
61 semid = sm_sem_start(SM_SEM_KEY, SM_NSEM, 0, owner);
64 perror("sm_sem_start failed");
68 while ((t = getchar()) != EOF)
73 delay(0, "try to acq");
74 if (sm_sem_acq(semid, 0, 2) < 0)
76 perror("sm_sem_acq failed");
83 delay(0, "try to rel");
84 if (sm_sem_rel(semid, 0, 2) < 0)
86 perror("sm_sem_rel failed");
93 if ((t = sm_sem_get(semid, 0)) < 0)
95 perror("get_sem failed");
98 printf("semval: %d\n", t);
104 return sm_sem_stop(semid);
109 ** SEM_CLEANUP -- cleanup if something breaks
118 static int semid_c = -1;
124 (void) sm_sem_stop(semid_c);
125 exit(EX_UNAVAILABLE);
129 ** SEMTEST -- test of semaphores
132 ** owner -- create semaphores.
148 semid = sm_sem_start(SM_SEM_KEY, 1, 0, owner);
151 perror("sm_sem_start failed");
157 /* just in case someone kills the program... */
159 (void) sm_signal(SIGHUP, sem_cleanup);
160 (void) sm_signal(SIGINT, sem_cleanup);
161 (void) sm_signal(SIGTERM, sem_cleanup);
163 delay(1, "parent: acquire 1");
167 r = sm_sem_acq(semid, 0, 0);
173 } while (r < 0 && cnt <= MAX_CNT);
178 delay(3, "parent: release 1");
182 r = sm_sem_rel(semid, 0, 0);
188 } while (r < 0 && cnt <= MAX_CNT);
193 delay(1, "parent: getval");
197 r = sm_sem_get(semid, 0);
203 } while (r <= 0 && cnt <= MAX_CNT);
208 delay(1, "parent: acquire 2");
212 r = sm_sem_acq(semid, 0, 0);
218 } while (r < 0 && cnt <= MAX_CNT);
226 r = sm_sem_rel(semid, 0, 0);
232 } while (r < 0 && cnt <= MAX_CNT);
239 delay(1, "child: acquire 1");
243 r = sm_sem_acq(semid, 0, 0);
249 } while (r < 0 && cnt <= MAX_CNT);
254 delay(1, "child: release 1");
258 r = sm_sem_rel(semid, 0, 0);
264 } while (r < 0 && cnt <= MAX_CNT);
271 return sm_sem_stop(semid);
280 bool interactive = false;
285 # define OPTIONS "io"
286 while ((ch = getopt(argc, argv, OPTIONS)) != -1)
309 printf("This test takes about 8 seconds.\n");
310 printf("If it takes longer than 30 seconds, please interrupt it\n");
311 printf("and compile again without semaphore support, i.e.,");
312 printf("-DSM_CONF_SEM=0\n");
313 if ((pid = fork()) < 0)
315 perror("fork failed\n");
319 sm_test_begin(argc, argv, "test semaphores");
322 /* give the parent the chance to setup data */
331 return sm_test_end();
335 #else /* SM_CONF_SEM */
341 printf("No support for semaphores configured on this machine\n");
344 #endif /* SM_CONF_SEM */