xref: /illumos-gate/usr/src/cmd/sendmail/libsm/t-sem.c (revision e9af4bc0)
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: t-sem.c,v 1.17 2008/05/30 16:26:38 ca Exp $")
127c478bd9Sstevel@tonic-gate 
137c478bd9Sstevel@tonic-gate #include <stdio.h>
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #if SM_CONF_SEM
167c478bd9Sstevel@tonic-gate # include <stdlib.h>
177c478bd9Sstevel@tonic-gate # include <unistd.h>
187c478bd9Sstevel@tonic-gate # include <sysexits.h>
197c478bd9Sstevel@tonic-gate # include <sm/heap.h>
207c478bd9Sstevel@tonic-gate # include <sm/string.h>
217c478bd9Sstevel@tonic-gate # include <sm/signal.h>
227c478bd9Sstevel@tonic-gate # include <sm/test.h>
237c478bd9Sstevel@tonic-gate # include <sm/sem.h>
247c478bd9Sstevel@tonic-gate 
254aac33d3Sjbeck # define T_SM_SEM_KEY (4321L)
264aac33d3Sjbeck 
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
seminter(owner)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 
634aac33d3Sjbeck 	semid = sm_sem_start(T_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
sem_cleanup(sig)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 
130*e9af4bc0SJohn Beck static int
drop_priv(uid,gid)131*e9af4bc0SJohn Beck drop_priv(uid, gid)
132*e9af4bc0SJohn Beck 	uid_t uid;
133*e9af4bc0SJohn Beck 	gid_t gid;
134*e9af4bc0SJohn Beck {
135*e9af4bc0SJohn Beck 	int r;
136*e9af4bc0SJohn Beck 
137*e9af4bc0SJohn Beck 	r = setgid(gid);
138*e9af4bc0SJohn Beck 	if (r != 0)
139*e9af4bc0SJohn Beck 		return r;
140*e9af4bc0SJohn Beck 	r = setuid(uid);
141*e9af4bc0SJohn Beck 	return r;
142*e9af4bc0SJohn Beck }
143*e9af4bc0SJohn Beck 
1447c478bd9Sstevel@tonic-gate /*
1457c478bd9Sstevel@tonic-gate **  SEMTEST -- test of semaphores
1467c478bd9Sstevel@tonic-gate **
1477c478bd9Sstevel@tonic-gate **	Parameters:
1487c478bd9Sstevel@tonic-gate **		owner -- create semaphores.
1497c478bd9Sstevel@tonic-gate **
1507c478bd9Sstevel@tonic-gate **	Returns:
1517c478bd9Sstevel@tonic-gate **		0 on success
1527c478bd9Sstevel@tonic-gate **		< 0 on failure.
1537c478bd9Sstevel@tonic-gate */
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate # define MAX_CNT	10
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate static int
semtest(owner,uid,gid)158*e9af4bc0SJohn Beck semtest(owner, uid, gid)
1597c478bd9Sstevel@tonic-gate 	int owner;
160*e9af4bc0SJohn Beck 	uid_t uid;
161*e9af4bc0SJohn Beck 	gid_t gid;
1627c478bd9Sstevel@tonic-gate {
1637c478bd9Sstevel@tonic-gate 	int semid, r;
1647c478bd9Sstevel@tonic-gate 	int cnt = 0;
1657c478bd9Sstevel@tonic-gate 
166*e9af4bc0SJohn Beck 	if (!owner && uid != 0)
167*e9af4bc0SJohn Beck 	{
168*e9af4bc0SJohn Beck 		r = drop_priv(uid, gid);
169*e9af4bc0SJohn Beck 		if (r < 0)
170*e9af4bc0SJohn Beck 		{
171*e9af4bc0SJohn Beck 			perror("drop_priv child failed");
172*e9af4bc0SJohn Beck 			return -1;
173*e9af4bc0SJohn Beck 		}
174*e9af4bc0SJohn Beck 	}
1754aac33d3Sjbeck 	semid = sm_sem_start(T_SM_SEM_KEY, 1, 0, owner);
1767c478bd9Sstevel@tonic-gate 	if (semid < 0)
1777c478bd9Sstevel@tonic-gate 	{
1787c478bd9Sstevel@tonic-gate 		perror("sm_sem_start failed");
1797c478bd9Sstevel@tonic-gate 		return -1;
1807c478bd9Sstevel@tonic-gate 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	if (owner)
1837c478bd9Sstevel@tonic-gate 	{
184*e9af4bc0SJohn Beck 		if (uid != 0)
185*e9af4bc0SJohn Beck 		{
186*e9af4bc0SJohn Beck 			r = sm_semsetowner(semid, uid, gid, 0660);
187*e9af4bc0SJohn Beck 			if (r < 0)
188*e9af4bc0SJohn Beck 			{
189*e9af4bc0SJohn Beck 				perror("sm_semsetowner failed");
190*e9af4bc0SJohn Beck 				return -1;
191*e9af4bc0SJohn Beck 			}
192*e9af4bc0SJohn Beck 			r = drop_priv(uid, gid);
193*e9af4bc0SJohn Beck 			if (r < 0)
194*e9af4bc0SJohn Beck 			{
195*e9af4bc0SJohn Beck 				perror("drop_priv owner failed");
196*e9af4bc0SJohn Beck 				return -1;
197*e9af4bc0SJohn Beck 			}
198*e9af4bc0SJohn Beck 		}
199*e9af4bc0SJohn Beck 
2007c478bd9Sstevel@tonic-gate 		/* just in case someone kills the program... */
2017c478bd9Sstevel@tonic-gate 		semid_c = semid;
2027c478bd9Sstevel@tonic-gate 		(void) sm_signal(SIGHUP, sem_cleanup);
2037c478bd9Sstevel@tonic-gate 		(void) sm_signal(SIGINT, sem_cleanup);
2047c478bd9Sstevel@tonic-gate 		(void) sm_signal(SIGTERM, sem_cleanup);
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 		delay(1, "parent: acquire 1");
2077c478bd9Sstevel@tonic-gate 		cnt = 0;
2087c478bd9Sstevel@tonic-gate 		do
2097c478bd9Sstevel@tonic-gate 		{
2107c478bd9Sstevel@tonic-gate 			r = sm_sem_acq(semid, 0, 0);
2117c478bd9Sstevel@tonic-gate 			if (r < 0)
2127c478bd9Sstevel@tonic-gate 			{
2137c478bd9Sstevel@tonic-gate 				sleep(1);
2147c478bd9Sstevel@tonic-gate 				++cnt;
2157c478bd9Sstevel@tonic-gate 			}
2167c478bd9Sstevel@tonic-gate 		} while (r < 0 && cnt <= MAX_CNT);
2177c478bd9Sstevel@tonic-gate 		SM_TEST(r >= 0);
2187c478bd9Sstevel@tonic-gate 		if (r < 0)
2197c478bd9Sstevel@tonic-gate 			return r;
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 		delay(3, "parent: release 1");
2227c478bd9Sstevel@tonic-gate 		cnt = 0;
2237c478bd9Sstevel@tonic-gate 		do
2247c478bd9Sstevel@tonic-gate 		{
2257c478bd9Sstevel@tonic-gate 			r = sm_sem_rel(semid, 0, 0);
2267c478bd9Sstevel@tonic-gate 			if (r < 0)
2277c478bd9Sstevel@tonic-gate 			{
2287c478bd9Sstevel@tonic-gate 				sleep(1);
2297c478bd9Sstevel@tonic-gate 				++cnt;
2307c478bd9Sstevel@tonic-gate 			}
2317c478bd9Sstevel@tonic-gate 		} while (r < 0 && cnt <= MAX_CNT);
2327c478bd9Sstevel@tonic-gate 		SM_TEST(r >= 0);
2337c478bd9Sstevel@tonic-gate 		if (r < 0)
2347c478bd9Sstevel@tonic-gate 			return r;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 		delay(1, "parent: getval");
2377c478bd9Sstevel@tonic-gate 		cnt = 0;
2387c478bd9Sstevel@tonic-gate 		do
2397c478bd9Sstevel@tonic-gate 		{
2407c478bd9Sstevel@tonic-gate 			r = sm_sem_get(semid, 0);
2417c478bd9Sstevel@tonic-gate 			if (r <= 0)
2427c478bd9Sstevel@tonic-gate 			{
2437c478bd9Sstevel@tonic-gate 				sleep(1);
2447c478bd9Sstevel@tonic-gate 				++cnt;
2457c478bd9Sstevel@tonic-gate 			}
2467c478bd9Sstevel@tonic-gate 		} while (r <= 0 && cnt <= MAX_CNT);
2477c478bd9Sstevel@tonic-gate 		SM_TEST(r > 0);
2487c478bd9Sstevel@tonic-gate 		if (r <= 0)
2497c478bd9Sstevel@tonic-gate 			return r;
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 		delay(1, "parent: acquire 2");
2527c478bd9Sstevel@tonic-gate 		cnt = 0;
2537c478bd9Sstevel@tonic-gate 		do
2547c478bd9Sstevel@tonic-gate 		{
2557c478bd9Sstevel@tonic-gate 			r = sm_sem_acq(semid, 0, 0);
2567c478bd9Sstevel@tonic-gate 			if (r < 0)
2577c478bd9Sstevel@tonic-gate 			{
2587c478bd9Sstevel@tonic-gate 				sleep(1);
2597c478bd9Sstevel@tonic-gate 				++cnt;
2607c478bd9Sstevel@tonic-gate 			}
2617c478bd9Sstevel@tonic-gate 		} while (r < 0 && cnt <= MAX_CNT);
2627c478bd9Sstevel@tonic-gate 		SM_TEST(r >= 0);
2637c478bd9Sstevel@tonic-gate 		if (r < 0)
2647c478bd9Sstevel@tonic-gate 			return r;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		cnt = 0;
2677c478bd9Sstevel@tonic-gate 		do
2687c478bd9Sstevel@tonic-gate 		{
2697c478bd9Sstevel@tonic-gate 			r = sm_sem_rel(semid, 0, 0);
2707c478bd9Sstevel@tonic-gate 			if (r < 0)
2717c478bd9Sstevel@tonic-gate 			{
2727c478bd9Sstevel@tonic-gate 				sleep(1);
2737c478bd9Sstevel@tonic-gate 				++cnt;
2747c478bd9Sstevel@tonic-gate 			}
2757c478bd9Sstevel@tonic-gate 		} while (r < 0 && cnt <= MAX_CNT);
2767c478bd9Sstevel@tonic-gate 		SM_TEST(r >= 0);
2777c478bd9Sstevel@tonic-gate 		if (r < 0)
2787c478bd9Sstevel@tonic-gate 			return r;
2797c478bd9Sstevel@tonic-gate 	}
2807c478bd9Sstevel@tonic-gate 	else
2817c478bd9Sstevel@tonic-gate 	{
2827c478bd9Sstevel@tonic-gate 		delay(1, "child: acquire 1");
2837c478bd9Sstevel@tonic-gate 		cnt = 0;
2847c478bd9Sstevel@tonic-gate 		do
2857c478bd9Sstevel@tonic-gate 		{
2867c478bd9Sstevel@tonic-gate 			r = sm_sem_acq(semid, 0, 0);
2877c478bd9Sstevel@tonic-gate 			if (r < 0)
2887c478bd9Sstevel@tonic-gate 			{
2897c478bd9Sstevel@tonic-gate 				sleep(1);
2907c478bd9Sstevel@tonic-gate 				++cnt;
2917c478bd9Sstevel@tonic-gate 			}
2927c478bd9Sstevel@tonic-gate 		} while (r < 0 && cnt <= MAX_CNT);
2937c478bd9Sstevel@tonic-gate 		SM_TEST(r >= 0);
2947c478bd9Sstevel@tonic-gate 		if (r < 0)
2957c478bd9Sstevel@tonic-gate 			return r;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		delay(1, "child: release 1");
2987c478bd9Sstevel@tonic-gate 		cnt = 0;
2997c478bd9Sstevel@tonic-gate 		do
3007c478bd9Sstevel@tonic-gate 		{
3017c478bd9Sstevel@tonic-gate 			r = sm_sem_rel(semid, 0, 0);
3027c478bd9Sstevel@tonic-gate 			if (r < 0)
3037c478bd9Sstevel@tonic-gate 			{
3047c478bd9Sstevel@tonic-gate 				sleep(1);
3057c478bd9Sstevel@tonic-gate 				++cnt;
3067c478bd9Sstevel@tonic-gate 			}
3077c478bd9Sstevel@tonic-gate 		} while (r < 0 && cnt <= MAX_CNT);
3087c478bd9Sstevel@tonic-gate 		SM_TEST(r >= 0);
3097c478bd9Sstevel@tonic-gate 		if (r < 0)
3107c478bd9Sstevel@tonic-gate 			return r;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	if (owner)
3147c478bd9Sstevel@tonic-gate 		return sm_sem_stop(semid);
3157c478bd9Sstevel@tonic-gate 	return 0;
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate int
main(argc,argv)3197c478bd9Sstevel@tonic-gate main(argc, argv)
3207c478bd9Sstevel@tonic-gate 	int argc;
3217c478bd9Sstevel@tonic-gate 	char *argv[];
3227c478bd9Sstevel@tonic-gate {
3237c478bd9Sstevel@tonic-gate 	bool interactive = false;
3247c478bd9Sstevel@tonic-gate 	bool owner = false;
325*e9af4bc0SJohn Beck 	int ch, r;
326*e9af4bc0SJohn Beck 	uid_t uid;
327*e9af4bc0SJohn Beck 	gid_t gid;
328*e9af4bc0SJohn Beck 
329*e9af4bc0SJohn Beck 	uid = 0;
330*e9af4bc0SJohn Beck 	gid = 0;
331*e9af4bc0SJohn Beck 	r = 0;
3327c478bd9Sstevel@tonic-gate 
333*e9af4bc0SJohn Beck # define OPTIONS	"iog:u:"
3347c478bd9Sstevel@tonic-gate 	while ((ch = getopt(argc, argv, OPTIONS)) != -1)
3357c478bd9Sstevel@tonic-gate 	{
3367c478bd9Sstevel@tonic-gate 		switch ((char) ch)
3377c478bd9Sstevel@tonic-gate 		{
338*e9af4bc0SJohn Beck 		  case 'g':
339*e9af4bc0SJohn Beck 			gid = (gid_t)strtoul(optarg, 0, 0);
340*e9af4bc0SJohn Beck 			break;
341*e9af4bc0SJohn Beck 
3427c478bd9Sstevel@tonic-gate 		  case 'i':
3437c478bd9Sstevel@tonic-gate 			interactive = true;
3447c478bd9Sstevel@tonic-gate 			break;
3457c478bd9Sstevel@tonic-gate 
346*e9af4bc0SJohn Beck 		  case 'u':
347*e9af4bc0SJohn Beck 			uid = (uid_t)strtoul(optarg, 0, 0);
348*e9af4bc0SJohn Beck 			break;
349*e9af4bc0SJohn Beck 
3507c478bd9Sstevel@tonic-gate 		  case 'o':
3517c478bd9Sstevel@tonic-gate 			owner = true;
3527c478bd9Sstevel@tonic-gate 			break;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		  default:
3557c478bd9Sstevel@tonic-gate 			break;
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 	if (interactive)
3607c478bd9Sstevel@tonic-gate 		r = seminter(owner);
3617c478bd9Sstevel@tonic-gate 	else
3627c478bd9Sstevel@tonic-gate 	{
3637c478bd9Sstevel@tonic-gate 		pid_t pid;
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate 		printf("This test takes about 8 seconds.\n");
366445f2479Sjbeck 		printf("If it takes longer than 30 seconds, please interrupt it\n");
3677c478bd9Sstevel@tonic-gate 		printf("and compile again without semaphore support, i.e.,");
3687c478bd9Sstevel@tonic-gate 		printf("-DSM_CONF_SEM=0\n");
3697c478bd9Sstevel@tonic-gate 		if ((pid = fork()) < 0)
3707c478bd9Sstevel@tonic-gate 		{
3717c478bd9Sstevel@tonic-gate 			perror("fork failed\n");
3727c478bd9Sstevel@tonic-gate 			return -1;
3737c478bd9Sstevel@tonic-gate 		}
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		sm_test_begin(argc, argv, "test semaphores");
3767c478bd9Sstevel@tonic-gate 		if (pid == 0)
3777c478bd9Sstevel@tonic-gate 		{
3787c478bd9Sstevel@tonic-gate 			/* give the parent the chance to setup data */
3797c478bd9Sstevel@tonic-gate 			sleep(1);
380*e9af4bc0SJohn Beck 			r = semtest(false, uid, gid);
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 		else
3837c478bd9Sstevel@tonic-gate 		{
384*e9af4bc0SJohn Beck 			r = semtest(true, uid, gid);
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		SM_TEST(r == 0);
3877c478bd9Sstevel@tonic-gate 		return sm_test_end();
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 	return r;
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate #else /* SM_CONF_SEM */
3927c478bd9Sstevel@tonic-gate int
3937c478bd9Sstevel@tonic-gate main(argc, argv)
3947c478bd9Sstevel@tonic-gate 	int argc;
3957c478bd9Sstevel@tonic-gate 	char *argv[];
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	printf("No support for semaphores configured on this machine\n");
3987c478bd9Sstevel@tonic-gate 	return 0;
3997c478bd9Sstevel@tonic-gate }
4007c478bd9Sstevel@tonic-gate #endif /* SM_CONF_SEM */
401