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