xref: /openbsd-src/regress/lib/libpthread/fcntl/fcntl.c (revision b2ea75c1b17e1a9a339660e7ed45cd24946b230e)
1*b2ea75c1Sfgsch /*	$OpenBSD: fcntl.c,v 1.1.1.1 2001/08/15 14:37:11 fgsch Exp $	*/
2*b2ea75c1Sfgsch /*
3*b2ea75c1Sfgsch  * Copyright (c) 1993, 1994, 1995, 1996 by Chris Provenzano and contributors,
4*b2ea75c1Sfgsch  * proven@mit.edu All rights reserved.
5*b2ea75c1Sfgsch  *
6*b2ea75c1Sfgsch  * Redistribution and use in source and binary forms, with or without
7*b2ea75c1Sfgsch  * modification, are permitted provided that the following conditions
8*b2ea75c1Sfgsch  * are met:
9*b2ea75c1Sfgsch  * 1. Redistributions of source code must retain the above copyright
10*b2ea75c1Sfgsch  *    notice, this list of conditions and the following disclaimer.
11*b2ea75c1Sfgsch  * 2. Redistributions in binary form must reproduce the above copyright
12*b2ea75c1Sfgsch  *    notice, this list of conditions and the following disclaimer in the
13*b2ea75c1Sfgsch  *    documentation and/or other materials provided with the distribution.
14*b2ea75c1Sfgsch  * 3. All advertising materials mentioning features or use of this software
15*b2ea75c1Sfgsch  *    must display the following acknowledgement:
16*b2ea75c1Sfgsch  *	This product includes software developed by Chris Provenzano,
17*b2ea75c1Sfgsch  *	the University of California, Berkeley, and contributors.
18*b2ea75c1Sfgsch  * 4. Neither the name of Chris Provenzano, the University, nor the names of
19*b2ea75c1Sfgsch  *   contributors may be used to endorse or promote products derived
20*b2ea75c1Sfgsch  *   from this software without specific prior written permission.
21*b2ea75c1Sfgsch  *
22*b2ea75c1Sfgsch  * THIS SOFTWARE IS PROVIDED BY CHRIS PROVENZANO AND CONTRIBUTORS ``AS IS'' AND
23*b2ea75c1Sfgsch  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*b2ea75c1Sfgsch  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*b2ea75c1Sfgsch  * ARE DISCLAIMED.  IN NO EVENT SHALL CHRIS PROVENZANO, THE REGENTS OR
26*b2ea75c1Sfgsch  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27*b2ea75c1Sfgsch  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28*b2ea75c1Sfgsch  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
29*b2ea75c1Sfgsch  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
30*b2ea75c1Sfgsch  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
31*b2ea75c1Sfgsch  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
32*b2ea75c1Sfgsch  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33*b2ea75c1Sfgsch  */
34*b2ea75c1Sfgsch 
35*b2ea75c1Sfgsch /*
36*b2ea75c1Sfgsch  * Test fcntl() flag inheritance across a fork()
37*b2ea75c1Sfgsch  */
38*b2ea75c1Sfgsch 
39*b2ea75c1Sfgsch #include <sys/types.h>
40*b2ea75c1Sfgsch #include <sys/wait.h>
41*b2ea75c1Sfgsch #include <fcntl.h>
42*b2ea75c1Sfgsch #include <stdio.h>
43*b2ea75c1Sfgsch #include <unistd.h>
44*b2ea75c1Sfgsch #include "test.h"
45*b2ea75c1Sfgsch 
46*b2ea75c1Sfgsch int
main()47*b2ea75c1Sfgsch main()
48*b2ea75c1Sfgsch {
49*b2ea75c1Sfgsch 	int flags, newflags, child;
50*b2ea75c1Sfgsch 
51*b2ea75c1Sfgsch 	CHECKe(flags = fcntl(0, F_GETFL));
52*b2ea75c1Sfgsch 	printf("flags = %x\n", flags);
53*b2ea75c1Sfgsch 
54*b2ea75c1Sfgsch 	CHECKe(child = fork());
55*b2ea75c1Sfgsch 	switch(child) {
56*b2ea75c1Sfgsch 	case 0: /* child */
57*b2ea75c1Sfgsch 		CHECKe(execlp("./pthread_create", "./pthread_create",
58*b2ea75c1Sfgsch 		    (char *)NULL));
59*b2ea75c1Sfgsch 		/* NOTREACHED */
60*b2ea75c1Sfgsch 	default: /* parent */
61*b2ea75c1Sfgsch 		CHECKe(wait(NULL));
62*b2ea75c1Sfgsch 		break;
63*b2ea75c1Sfgsch 	}
64*b2ea75c1Sfgsch 
65*b2ea75c1Sfgsch 	while(1){
66*b2ea75c1Sfgsch 		CHECKe(newflags = fcntl(0, F_GETFL));
67*b2ea75c1Sfgsch 		printf ("parent %d flags = %x\n", child, newflags);
68*b2ea75c1Sfgsch 		sleep(1);
69*b2ea75c1Sfgsch 	}
70*b2ea75c1Sfgsch }
71