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