17c478bd9Sstevel@tonic-gate /* 2*4aac33d3Sjbeck * Copyright (c) 2000-2001, 2005-2007 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 #pragma ident "%Z%%M% %I% %E% SMI" 117c478bd9Sstevel@tonic-gate 127c478bd9Sstevel@tonic-gate #include <sm/gen.h> 13*4aac33d3Sjbeck SM_RCSID("@(#)$Id: t-sem.c,v 1.16 2007/03/21 23:22:10 ca Exp $") 147c478bd9Sstevel@tonic-gate 157c478bd9Sstevel@tonic-gate #include <stdio.h> 167c478bd9Sstevel@tonic-gate 177c478bd9Sstevel@tonic-gate #if SM_CONF_SEM 187c478bd9Sstevel@tonic-gate # include <stdlib.h> 197c478bd9Sstevel@tonic-gate # include <unistd.h> 207c478bd9Sstevel@tonic-gate # include <sysexits.h> 217c478bd9Sstevel@tonic-gate # include <sm/heap.h> 227c478bd9Sstevel@tonic-gate # include <sm/string.h> 237c478bd9Sstevel@tonic-gate # include <sm/signal.h> 247c478bd9Sstevel@tonic-gate # include <sm/test.h> 257c478bd9Sstevel@tonic-gate # include <sm/sem.h> 267c478bd9Sstevel@tonic-gate 27*4aac33d3Sjbeck # define T_SM_SEM_KEY (4321L) 28*4aac33d3Sjbeck 297c478bd9Sstevel@tonic-gate static void 307c478bd9Sstevel@tonic-gate delay(t, s) 317c478bd9Sstevel@tonic-gate int t; 327c478bd9Sstevel@tonic-gate char *s; 337c478bd9Sstevel@tonic-gate { 347c478bd9Sstevel@tonic-gate if (t > 0) 357c478bd9Sstevel@tonic-gate { 367c478bd9Sstevel@tonic-gate #if DEBUG 377c478bd9Sstevel@tonic-gate fprintf(stderr, "sleep(%d) before %s\n", t, s); 387c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 397c478bd9Sstevel@tonic-gate sleep(t); 407c478bd9Sstevel@tonic-gate } 417c478bd9Sstevel@tonic-gate #if DEBUG 427c478bd9Sstevel@tonic-gate fprintf(stderr, "%s\n", s); 437c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 447c478bd9Sstevel@tonic-gate } 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate /* 487c478bd9Sstevel@tonic-gate ** SEMINTER -- interactive testing of semaphores. 497c478bd9Sstevel@tonic-gate ** 507c478bd9Sstevel@tonic-gate ** Parameters: 517c478bd9Sstevel@tonic-gate ** owner -- create semaphores. 527c478bd9Sstevel@tonic-gate ** 537c478bd9Sstevel@tonic-gate ** Returns: 547c478bd9Sstevel@tonic-gate ** 0 on success 557c478bd9Sstevel@tonic-gate ** < 0 on failure. 567c478bd9Sstevel@tonic-gate */ 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate static int 597c478bd9Sstevel@tonic-gate seminter(owner) 607c478bd9Sstevel@tonic-gate bool owner; 617c478bd9Sstevel@tonic-gate { 627c478bd9Sstevel@tonic-gate int semid; 637c478bd9Sstevel@tonic-gate int t; 647c478bd9Sstevel@tonic-gate 65*4aac33d3Sjbeck semid = sm_sem_start(T_SM_SEM_KEY, SM_NSEM, 0, owner); 667c478bd9Sstevel@tonic-gate if (semid < 0) 677c478bd9Sstevel@tonic-gate { 687c478bd9Sstevel@tonic-gate perror("sm_sem_start failed"); 697c478bd9Sstevel@tonic-gate return 1; 707c478bd9Sstevel@tonic-gate } 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate while ((t = getchar()) != EOF) 737c478bd9Sstevel@tonic-gate { 747c478bd9Sstevel@tonic-gate switch (t) 757c478bd9Sstevel@tonic-gate { 767c478bd9Sstevel@tonic-gate case 'a': 777c478bd9Sstevel@tonic-gate delay(0, "try to acq"); 787c478bd9Sstevel@tonic-gate if (sm_sem_acq(semid, 0, 2) < 0) 797c478bd9Sstevel@tonic-gate { 807c478bd9Sstevel@tonic-gate perror("sm_sem_acq failed"); 817c478bd9Sstevel@tonic-gate return 1; 827c478bd9Sstevel@tonic-gate } 837c478bd9Sstevel@tonic-gate delay(0, "acquired"); 847c478bd9Sstevel@tonic-gate break; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate case 'r': 877c478bd9Sstevel@tonic-gate delay(0, "try to rel"); 887c478bd9Sstevel@tonic-gate if (sm_sem_rel(semid, 0, 2) < 0) 897c478bd9Sstevel@tonic-gate { 907c478bd9Sstevel@tonic-gate perror("sm_sem_rel failed"); 917c478bd9Sstevel@tonic-gate return 1; 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate delay(0, "released"); 947c478bd9Sstevel@tonic-gate break; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate case 'v': 977c478bd9Sstevel@tonic-gate if ((t = sm_sem_get(semid, 0)) < 0) 987c478bd9Sstevel@tonic-gate { 997c478bd9Sstevel@tonic-gate perror("get_sem failed"); 1007c478bd9Sstevel@tonic-gate return 1; 1017c478bd9Sstevel@tonic-gate } 1027c478bd9Sstevel@tonic-gate printf("semval: %d\n", t); 1037c478bd9Sstevel@tonic-gate break; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate } 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate if (owner) 1087c478bd9Sstevel@tonic-gate return sm_sem_stop(semid); 1097c478bd9Sstevel@tonic-gate return 0; 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate /* 1137c478bd9Sstevel@tonic-gate ** SEM_CLEANUP -- cleanup if something breaks 1147c478bd9Sstevel@tonic-gate ** 1157c478bd9Sstevel@tonic-gate ** Parameters: 1167c478bd9Sstevel@tonic-gate ** sig -- signal. 1177c478bd9Sstevel@tonic-gate ** 1187c478bd9Sstevel@tonic-gate ** Returns: 1197c478bd9Sstevel@tonic-gate ** none. 1207c478bd9Sstevel@tonic-gate */ 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate static int semid_c = -1; 1237c478bd9Sstevel@tonic-gate void 1247c478bd9Sstevel@tonic-gate sem_cleanup(sig) 1257c478bd9Sstevel@tonic-gate int sig; 1267c478bd9Sstevel@tonic-gate { 1277c478bd9Sstevel@tonic-gate if (semid_c >= 0) 1287c478bd9Sstevel@tonic-gate (void) sm_sem_stop(semid_c); 1297c478bd9Sstevel@tonic-gate exit(EX_UNAVAILABLE); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate /* 1337c478bd9Sstevel@tonic-gate ** SEMTEST -- test of semaphores 1347c478bd9Sstevel@tonic-gate ** 1357c478bd9Sstevel@tonic-gate ** Parameters: 1367c478bd9Sstevel@tonic-gate ** owner -- create semaphores. 1377c478bd9Sstevel@tonic-gate ** 1387c478bd9Sstevel@tonic-gate ** Returns: 1397c478bd9Sstevel@tonic-gate ** 0 on success 1407c478bd9Sstevel@tonic-gate ** < 0 on failure. 1417c478bd9Sstevel@tonic-gate */ 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate # define MAX_CNT 10 1447c478bd9Sstevel@tonic-gate 1457c478bd9Sstevel@tonic-gate static int 1467c478bd9Sstevel@tonic-gate semtest(owner) 1477c478bd9Sstevel@tonic-gate int owner; 1487c478bd9Sstevel@tonic-gate { 1497c478bd9Sstevel@tonic-gate int semid, r; 1507c478bd9Sstevel@tonic-gate int cnt = 0; 1517c478bd9Sstevel@tonic-gate 152*4aac33d3Sjbeck semid = sm_sem_start(T_SM_SEM_KEY, 1, 0, owner); 1537c478bd9Sstevel@tonic-gate if (semid < 0) 1547c478bd9Sstevel@tonic-gate { 1557c478bd9Sstevel@tonic-gate perror("sm_sem_start failed"); 1567c478bd9Sstevel@tonic-gate return -1; 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate if (owner) 1607c478bd9Sstevel@tonic-gate { 1617c478bd9Sstevel@tonic-gate /* just in case someone kills the program... */ 1627c478bd9Sstevel@tonic-gate semid_c = semid; 1637c478bd9Sstevel@tonic-gate (void) sm_signal(SIGHUP, sem_cleanup); 1647c478bd9Sstevel@tonic-gate (void) sm_signal(SIGINT, sem_cleanup); 1657c478bd9Sstevel@tonic-gate (void) sm_signal(SIGTERM, sem_cleanup); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate delay(1, "parent: acquire 1"); 1687c478bd9Sstevel@tonic-gate cnt = 0; 1697c478bd9Sstevel@tonic-gate do 1707c478bd9Sstevel@tonic-gate { 1717c478bd9Sstevel@tonic-gate r = sm_sem_acq(semid, 0, 0); 1727c478bd9Sstevel@tonic-gate if (r < 0) 1737c478bd9Sstevel@tonic-gate { 1747c478bd9Sstevel@tonic-gate sleep(1); 1757c478bd9Sstevel@tonic-gate ++cnt; 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate } while (r < 0 && cnt <= MAX_CNT); 1787c478bd9Sstevel@tonic-gate SM_TEST(r >= 0); 1797c478bd9Sstevel@tonic-gate if (r < 0) 1807c478bd9Sstevel@tonic-gate return r; 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate delay(3, "parent: release 1"); 1837c478bd9Sstevel@tonic-gate cnt = 0; 1847c478bd9Sstevel@tonic-gate do 1857c478bd9Sstevel@tonic-gate { 1867c478bd9Sstevel@tonic-gate r = sm_sem_rel(semid, 0, 0); 1877c478bd9Sstevel@tonic-gate if (r < 0) 1887c478bd9Sstevel@tonic-gate { 1897c478bd9Sstevel@tonic-gate sleep(1); 1907c478bd9Sstevel@tonic-gate ++cnt; 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate } while (r < 0 && cnt <= MAX_CNT); 1937c478bd9Sstevel@tonic-gate SM_TEST(r >= 0); 1947c478bd9Sstevel@tonic-gate if (r < 0) 1957c478bd9Sstevel@tonic-gate return r; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate delay(1, "parent: getval"); 1987c478bd9Sstevel@tonic-gate cnt = 0; 1997c478bd9Sstevel@tonic-gate do 2007c478bd9Sstevel@tonic-gate { 2017c478bd9Sstevel@tonic-gate r = sm_sem_get(semid, 0); 2027c478bd9Sstevel@tonic-gate if (r <= 0) 2037c478bd9Sstevel@tonic-gate { 2047c478bd9Sstevel@tonic-gate sleep(1); 2057c478bd9Sstevel@tonic-gate ++cnt; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate } while (r <= 0 && cnt <= MAX_CNT); 2087c478bd9Sstevel@tonic-gate SM_TEST(r > 0); 2097c478bd9Sstevel@tonic-gate if (r <= 0) 2107c478bd9Sstevel@tonic-gate return r; 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate delay(1, "parent: acquire 2"); 2137c478bd9Sstevel@tonic-gate cnt = 0; 2147c478bd9Sstevel@tonic-gate do 2157c478bd9Sstevel@tonic-gate { 2167c478bd9Sstevel@tonic-gate r = sm_sem_acq(semid, 0, 0); 2177c478bd9Sstevel@tonic-gate if (r < 0) 2187c478bd9Sstevel@tonic-gate { 2197c478bd9Sstevel@tonic-gate sleep(1); 2207c478bd9Sstevel@tonic-gate ++cnt; 2217c478bd9Sstevel@tonic-gate } 2227c478bd9Sstevel@tonic-gate } while (r < 0 && cnt <= MAX_CNT); 2237c478bd9Sstevel@tonic-gate SM_TEST(r >= 0); 2247c478bd9Sstevel@tonic-gate if (r < 0) 2257c478bd9Sstevel@tonic-gate return r; 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate cnt = 0; 2287c478bd9Sstevel@tonic-gate do 2297c478bd9Sstevel@tonic-gate { 2307c478bd9Sstevel@tonic-gate r = sm_sem_rel(semid, 0, 0); 2317c478bd9Sstevel@tonic-gate if (r < 0) 2327c478bd9Sstevel@tonic-gate { 2337c478bd9Sstevel@tonic-gate sleep(1); 2347c478bd9Sstevel@tonic-gate ++cnt; 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate } while (r < 0 && cnt <= MAX_CNT); 2377c478bd9Sstevel@tonic-gate SM_TEST(r >= 0); 2387c478bd9Sstevel@tonic-gate if (r < 0) 2397c478bd9Sstevel@tonic-gate return r; 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate else 2427c478bd9Sstevel@tonic-gate { 2437c478bd9Sstevel@tonic-gate delay(1, "child: acquire 1"); 2447c478bd9Sstevel@tonic-gate cnt = 0; 2457c478bd9Sstevel@tonic-gate do 2467c478bd9Sstevel@tonic-gate { 2477c478bd9Sstevel@tonic-gate r = sm_sem_acq(semid, 0, 0); 2487c478bd9Sstevel@tonic-gate if (r < 0) 2497c478bd9Sstevel@tonic-gate { 2507c478bd9Sstevel@tonic-gate sleep(1); 2517c478bd9Sstevel@tonic-gate ++cnt; 2527c478bd9Sstevel@tonic-gate } 2537c478bd9Sstevel@tonic-gate } while (r < 0 && cnt <= MAX_CNT); 2547c478bd9Sstevel@tonic-gate SM_TEST(r >= 0); 2557c478bd9Sstevel@tonic-gate if (r < 0) 2567c478bd9Sstevel@tonic-gate return r; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate delay(1, "child: release 1"); 2597c478bd9Sstevel@tonic-gate cnt = 0; 2607c478bd9Sstevel@tonic-gate do 2617c478bd9Sstevel@tonic-gate { 2627c478bd9Sstevel@tonic-gate r = sm_sem_rel(semid, 0, 0); 2637c478bd9Sstevel@tonic-gate if (r < 0) 2647c478bd9Sstevel@tonic-gate { 2657c478bd9Sstevel@tonic-gate sleep(1); 2667c478bd9Sstevel@tonic-gate ++cnt; 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate } while (r < 0 && cnt <= MAX_CNT); 2697c478bd9Sstevel@tonic-gate SM_TEST(r >= 0); 2707c478bd9Sstevel@tonic-gate if (r < 0) 2717c478bd9Sstevel@tonic-gate return r; 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate } 2747c478bd9Sstevel@tonic-gate if (owner) 2757c478bd9Sstevel@tonic-gate return sm_sem_stop(semid); 2767c478bd9Sstevel@tonic-gate return 0; 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate int 2807c478bd9Sstevel@tonic-gate main(argc, argv) 2817c478bd9Sstevel@tonic-gate int argc; 2827c478bd9Sstevel@tonic-gate char *argv[]; 2837c478bd9Sstevel@tonic-gate { 2847c478bd9Sstevel@tonic-gate bool interactive = false; 2857c478bd9Sstevel@tonic-gate bool owner = false; 2867c478bd9Sstevel@tonic-gate int ch; 2877c478bd9Sstevel@tonic-gate int r = 0; 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate # define OPTIONS "io" 2907c478bd9Sstevel@tonic-gate while ((ch = getopt(argc, argv, OPTIONS)) != -1) 2917c478bd9Sstevel@tonic-gate { 2927c478bd9Sstevel@tonic-gate switch ((char) ch) 2937c478bd9Sstevel@tonic-gate { 2947c478bd9Sstevel@tonic-gate case 'i': 2957c478bd9Sstevel@tonic-gate interactive = true; 2967c478bd9Sstevel@tonic-gate break; 2977c478bd9Sstevel@tonic-gate 2987c478bd9Sstevel@tonic-gate case 'o': 2997c478bd9Sstevel@tonic-gate owner = true; 3007c478bd9Sstevel@tonic-gate break; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate default: 3037c478bd9Sstevel@tonic-gate break; 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate } 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate if (interactive) 3087c478bd9Sstevel@tonic-gate r = seminter(owner); 3097c478bd9Sstevel@tonic-gate else 3107c478bd9Sstevel@tonic-gate { 3117c478bd9Sstevel@tonic-gate pid_t pid; 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate printf("This test takes about 8 seconds.\n"); 314445f2479Sjbeck printf("If it takes longer than 30 seconds, please interrupt it\n"); 3157c478bd9Sstevel@tonic-gate printf("and compile again without semaphore support, i.e.,"); 3167c478bd9Sstevel@tonic-gate printf("-DSM_CONF_SEM=0\n"); 3177c478bd9Sstevel@tonic-gate if ((pid = fork()) < 0) 3187c478bd9Sstevel@tonic-gate { 3197c478bd9Sstevel@tonic-gate perror("fork failed\n"); 3207c478bd9Sstevel@tonic-gate return -1; 3217c478bd9Sstevel@tonic-gate } 3227c478bd9Sstevel@tonic-gate 3237c478bd9Sstevel@tonic-gate sm_test_begin(argc, argv, "test semaphores"); 3247c478bd9Sstevel@tonic-gate if (pid == 0) 3257c478bd9Sstevel@tonic-gate { 3267c478bd9Sstevel@tonic-gate /* give the parent the chance to setup data */ 3277c478bd9Sstevel@tonic-gate sleep(1); 3287c478bd9Sstevel@tonic-gate r = semtest(false); 3297c478bd9Sstevel@tonic-gate } 3307c478bd9Sstevel@tonic-gate else 3317c478bd9Sstevel@tonic-gate { 3327c478bd9Sstevel@tonic-gate r = semtest(true); 3337c478bd9Sstevel@tonic-gate } 3347c478bd9Sstevel@tonic-gate SM_TEST(r == 0); 3357c478bd9Sstevel@tonic-gate return sm_test_end(); 3367c478bd9Sstevel@tonic-gate } 3377c478bd9Sstevel@tonic-gate return r; 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate #else /* SM_CONF_SEM */ 3407c478bd9Sstevel@tonic-gate int 3417c478bd9Sstevel@tonic-gate main(argc, argv) 3427c478bd9Sstevel@tonic-gate int argc; 3437c478bd9Sstevel@tonic-gate char *argv[]; 3447c478bd9Sstevel@tonic-gate { 3457c478bd9Sstevel@tonic-gate printf("No support for semaphores configured on this machine\n"); 3467c478bd9Sstevel@tonic-gate return 0; 3477c478bd9Sstevel@tonic-gate } 3487c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SEM */ 349