1 /*-
2  * Copyright (c) 2008 Yahoo!, Inc.
3  * All rights reserved.
4  * Written by: John Baldwin <jhb@FreeBSD.org>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the author nor the names of any co-contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 #ifndef _COMMON_H_
32 #define _COMMON_H_
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <errno.h>
37 #include <fcntl.h>
38 #include <semaphore.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include <sys/param.h>
43 #include <sys/types.h>
44 #include <sys/stat.h>
45 
46 #define	ELAPSED(elapsed, limit)		(abs((elapsed) - (limit)) < 100)
47 
48 #define	TEST_PATH	"/posixsem_regression_test"
49 
50 extern sem_t *alarm_id;
51 extern int alarm_errno;
52 extern int alarm_handler_installed;
53 
54 /* Cut and pasted from kernel header, bah! */
55 
56 /* Operations on timespecs */
57 #define	timespecclear(tvp)	((tvp)->tv_sec = (tvp)->tv_nsec = 0)
58 #define	timespecisset(tvp)	((tvp)->tv_sec || (tvp)->tv_nsec)
59 #define	timespeccmp(tvp, uvp, cmp)					\
60 	(((tvp)->tv_sec == (uvp)->tv_sec) ?				\
61 	    ((tvp)->tv_nsec cmp (uvp)->tv_nsec) :			\
62 	    ((tvp)->tv_sec cmp (uvp)->tv_sec))
63 #define timespecadd(vvp, uvp)						\
64 	do {								\
65 		(vvp)->tv_sec += (uvp)->tv_sec;				\
66 		(vvp)->tv_nsec += (uvp)->tv_nsec;			\
67 		if ((vvp)->tv_nsec >= 1000000000) {			\
68 			(vvp)->tv_sec++;				\
69 			(vvp)->tv_nsec -= 1000000000;			\
70 		}							\
71 	} while (0)
72 #define timespecsub(vvp, uvp)						\
73 	do {								\
74 		(vvp)->tv_sec -= (uvp)->tv_sec;				\
75 		(vvp)->tv_nsec -= (uvp)->tv_nsec;			\
76 		if ((vvp)->tv_nsec < 0) {				\
77 			(vvp)->tv_sec--;				\
78 			(vvp)->tv_nsec += 1000000000;			\
79 		}							\
80 	} while (0)
81 
82 
83 /* Macros for passing child status to parent over a pipe. */
84 #define	CSTAT(class, error)		((class) << 16 | (error))
85 #define	CSTAT_CLASS(stat)		((stat) >> 16)
86 #define	CSTAT_ERROR(stat)		((stat) & 0xffff)
87 
88 int checkvalue(sem_t *, int);
89 sem_t *construct_shared_unnamed_sem(unsigned int);
90 void destruct_shared_unnamed_sem(sem_t *);
91 int testwait(sem_t *, u_int *);
92 int timedwait(sem_t *, u_int, u_int *, int);
93 int schedule_post(sem_t *, u_int);
94 int check_alarm(int);
95 int child_worker(int (*)(void *), void *, int *);
96 int wait_twoproc_child(void *);
97 int sem_open_should_fail(const char *, int, mode_t, unsigned int, int);
98 int sem_init_should_fail(unsigned int, int);
99 int sem_unlink_should_fail(const char *, int);
100 int sem_destroy_should_fail(sem_t *, int);
101 int sem_close_should_fail(sem_t *, int);
102 
103 #endif
104