2 * Copyright (c) 2000-2001, 2005-2007 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.16 2007/03/21 23:22:10 ca Exp $")
18 # include <sysexits.h>
20 # include <sm/string.h>
21 # include <sm/signal.h>
25 # define T_SM_SEM_KEY (4321L)
35 fprintf(stderr
, "sleep(%d) before %s\n", t
, s
);
40 fprintf(stderr
, "%s\n", s
);
46 ** SEMINTER -- interactive testing of semaphores.
49 ** owner -- create semaphores.
63 semid
= sm_sem_start(T_SM_SEM_KEY
, SM_NSEM
, 0, owner
);
66 perror("sm_sem_start failed");
70 while ((t
= getchar()) != EOF
)
75 delay(0, "try to acq");
76 if (sm_sem_acq(semid
, 0, 2) < 0)
78 perror("sm_sem_acq failed");
85 delay(0, "try to rel");
86 if (sm_sem_rel(semid
, 0, 2) < 0)
88 perror("sm_sem_rel failed");
95 if ((t
= sm_sem_get(semid
, 0)) < 0)
97 perror("get_sem failed");
100 printf("semval: %d\n", t
);
106 return sm_sem_stop(semid
);
111 ** SEM_CLEANUP -- cleanup if something breaks
120 static int semid_c
= -1;
126 (void) sm_sem_stop(semid_c
);
127 exit(EX_UNAVAILABLE
);
131 ** SEMTEST -- test of semaphores
134 ** owner -- create semaphores.
150 semid
= sm_sem_start(T_SM_SEM_KEY
, 1, 0, owner
);
153 perror("sm_sem_start failed");
159 /* just in case someone kills the program... */
161 (void) sm_signal(SIGHUP
, sem_cleanup
);
162 (void) sm_signal(SIGINT
, sem_cleanup
);
163 (void) sm_signal(SIGTERM
, sem_cleanup
);
165 delay(1, "parent: acquire 1");
169 r
= sm_sem_acq(semid
, 0, 0);
175 } while (r
< 0 && cnt
<= MAX_CNT
);
180 delay(3, "parent: release 1");
184 r
= sm_sem_rel(semid
, 0, 0);
190 } while (r
< 0 && cnt
<= MAX_CNT
);
195 delay(1, "parent: getval");
199 r
= sm_sem_get(semid
, 0);
205 } while (r
<= 0 && cnt
<= MAX_CNT
);
210 delay(1, "parent: acquire 2");
214 r
= sm_sem_acq(semid
, 0, 0);
220 } while (r
< 0 && cnt
<= MAX_CNT
);
228 r
= sm_sem_rel(semid
, 0, 0);
234 } while (r
< 0 && cnt
<= MAX_CNT
);
241 delay(1, "child: acquire 1");
245 r
= sm_sem_acq(semid
, 0, 0);
251 } while (r
< 0 && cnt
<= MAX_CNT
);
256 delay(1, "child: release 1");
260 r
= sm_sem_rel(semid
, 0, 0);
266 } while (r
< 0 && cnt
<= MAX_CNT
);
273 return sm_sem_stop(semid
);
282 bool interactive
= false;
287 # define OPTIONS "io"
288 while ((ch
= getopt(argc
, argv
, OPTIONS
)) != -1)
311 printf("This test takes about 8 seconds.\n");
312 printf("If it takes longer than 30 seconds, please interrupt it\n");
313 printf("and compile again without semaphore support, i.e.,");
314 printf("-DSM_CONF_SEM=0\n");
315 if ((pid
= fork()) < 0)
317 perror("fork failed\n");
321 sm_test_begin(argc
, argv
, "test semaphores");
324 /* give the parent the chance to setup data */
333 return sm_test_end();
337 #else /* SM_CONF_SEM */
343 printf("No support for semaphores configured on this machine\n");
346 #endif /* SM_CONF_SEM */