xref: /netbsd-src/tests/lib/libpthread/dlopen/t_main_pthread_create.c (revision a4ddc2c8fb9af816efe3b1c375a5530aef0e89e9)
1*a4ddc2c8Schristos /*	$NetBSD: t_main_pthread_create.c,v 1.1 2013/03/21 16:50:21 christos Exp $ */
2*a4ddc2c8Schristos /*-
3*a4ddc2c8Schristos  * Copyright (c) 2013 The NetBSD Foundation, Inc.
4*a4ddc2c8Schristos  * All rights reserved.
5*a4ddc2c8Schristos  *
6*a4ddc2c8Schristos  * This code is derived from software contributed to The NetBSD Foundation
7*a4ddc2c8Schristos  * by Emmanuel Dreyfus
8*a4ddc2c8Schristos  *
9*a4ddc2c8Schristos  * Redistribution and use in source and binary forms, with or without
10*a4ddc2c8Schristos  * modification, are permitted provided that the following conditions
11*a4ddc2c8Schristos  * are met:
12*a4ddc2c8Schristos  *
13*a4ddc2c8Schristos  * 1. Redistributions of source code must retain the above copyright
14*a4ddc2c8Schristos  *    notice, this list of conditions and the following disclaimer.
15*a4ddc2c8Schristos  * 2. Redistributions in binary form must reproduce the above copyright
16*a4ddc2c8Schristos  *    notice, this list of conditions and the following disclaimer in
17*a4ddc2c8Schristos  *    the documentation and/or other materials provided with the
18*a4ddc2c8Schristos  *    distribution.
19*a4ddc2c8Schristos  *
20*a4ddc2c8Schristos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21*a4ddc2c8Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22*a4ddc2c8Schristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23*a4ddc2c8Schristos  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24*a4ddc2c8Schristos  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25*a4ddc2c8Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26*a4ddc2c8Schristos  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27*a4ddc2c8Schristos  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28*a4ddc2c8Schristos  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29*a4ddc2c8Schristos  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30*a4ddc2c8Schristos  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31*a4ddc2c8Schristos  * SUCH DAMAGE.
32*a4ddc2c8Schristos  */
33*a4ddc2c8Schristos 
34*a4ddc2c8Schristos #include <sys/cdefs.h>
35*a4ddc2c8Schristos __RCSID("$NetBSD: t_main_pthread_create.c,v 1.1 2013/03/21 16:50:21 christos Exp $");
36*a4ddc2c8Schristos 
37*a4ddc2c8Schristos #include <atf-c.h>
38*a4ddc2c8Schristos #include <dlfcn.h>
39*a4ddc2c8Schristos #include <pthread.h>
40*a4ddc2c8Schristos #include <unistd.h>
41*a4ddc2c8Schristos 
42*a4ddc2c8Schristos #define DSO TESTDIR "/h_pthread_dlopen.so"
43*a4ddc2c8Schristos 
44*a4ddc2c8Schristos void *
routine(void * arg)45*a4ddc2c8Schristos routine(void *arg)
46*a4ddc2c8Schristos {
47*a4ddc2c8Schristos 	ATF_REQUIRE((intptr_t)arg == 0xcafe);
48*a4ddc2c8Schristos 	return NULL;
49*a4ddc2c8Schristos }
50*a4ddc2c8Schristos 
51*a4ddc2c8Schristos ATF_TC(main_pthread_create_main);
52*a4ddc2c8Schristos 
ATF_TC_HEAD(main_pthread_create_main,tc)53*a4ddc2c8Schristos ATF_TC_HEAD(main_pthread_create_main, tc)
54*a4ddc2c8Schristos {
55*a4ddc2c8Schristos 	atf_tc_set_md_var(tc, "descr",
56*a4ddc2c8Schristos 	    "Test if -lpthread main can call pthread_create() in main()");
57*a4ddc2c8Schristos }
58*a4ddc2c8Schristos 
ATF_TC_BODY(main_pthread_create_main,tc)59*a4ddc2c8Schristos ATF_TC_BODY(main_pthread_create_main, tc)
60*a4ddc2c8Schristos {
61*a4ddc2c8Schristos 	int ret;
62*a4ddc2c8Schristos 	pthread_t thread;
63*a4ddc2c8Schristos 	void *arg = (void *)0xcafe;
64*a4ddc2c8Schristos 
65*a4ddc2c8Schristos 	ret = pthread_create(&thread, NULL, routine, arg);
66*a4ddc2c8Schristos 	ATF_REQUIRE(ret == 0);
67*a4ddc2c8Schristos }
68*a4ddc2c8Schristos 
69*a4ddc2c8Schristos ATF_TC(main_pthread_create_dso);
70*a4ddc2c8Schristos 
ATF_TC_HEAD(main_pthread_create_dso,tc)71*a4ddc2c8Schristos ATF_TC_HEAD(main_pthread_create_dso, tc)
72*a4ddc2c8Schristos {
73*a4ddc2c8Schristos 	atf_tc_set_md_var(tc, "descr",
74*a4ddc2c8Schristos 	    "Test if -lpthread main can call pthread_create() in DSO");
75*a4ddc2c8Schristos }
76*a4ddc2c8Schristos 
ATF_TC_BODY(main_pthread_create_dso,tc)77*a4ddc2c8Schristos ATF_TC_BODY(main_pthread_create_dso, tc)
78*a4ddc2c8Schristos {
79*a4ddc2c8Schristos 	int ret;
80*a4ddc2c8Schristos 	pthread_t thread;
81*a4ddc2c8Schristos 	void *arg = (void *)0xcafe;
82*a4ddc2c8Schristos 	void *handle;
83*a4ddc2c8Schristos 	int (*testf_dso_pthread_create)(pthread_t *, pthread_attr_t *,
84*a4ddc2c8Schristos 	    void *(*)(void *), void *);
85*a4ddc2c8Schristos 
86*a4ddc2c8Schristos 	handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
87*a4ddc2c8Schristos 	ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
88*a4ddc2c8Schristos 
89*a4ddc2c8Schristos 	testf_dso_pthread_create = dlsym(handle, "testf_dso_pthread_create");
90*a4ddc2c8Schristos 	ATF_REQUIRE_MSG(testf_dso_pthread_create != NULL,
91*a4ddc2c8Schristos 	    "dlsym fails: %s", dlerror());
92*a4ddc2c8Schristos 
93*a4ddc2c8Schristos 	ret = testf_dso_pthread_create(&thread, NULL, routine, arg);
94*a4ddc2c8Schristos 	ATF_REQUIRE(ret == 0);
95*a4ddc2c8Schristos 
96*a4ddc2c8Schristos 	ATF_REQUIRE(dlclose(handle) == 0);
97*a4ddc2c8Schristos 
98*a4ddc2c8Schristos }
99*a4ddc2c8Schristos 
ATF_TP_ADD_TCS(tp)100*a4ddc2c8Schristos ATF_TP_ADD_TCS(tp)
101*a4ddc2c8Schristos {
102*a4ddc2c8Schristos 	ATF_TP_ADD_TC(tp, main_pthread_create_main);
103*a4ddc2c8Schristos 	ATF_TP_ADD_TC(tp, main_pthread_create_dso);
104*a4ddc2c8Schristos 
105*a4ddc2c8Schristos 	return atf_no_error();
106*a4ddc2c8Schristos }
107