1*a4ddc2c8Schristos /* $NetBSD: t_dso_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_dso_pthread_create.c,v 1.1 2013/03/21 16:50:21 christos Exp $");
36*a4ddc2c8Schristos
37*a4ddc2c8Schristos #include <sys/resource.h>
38*a4ddc2c8Schristos #include <atf-c.h>
39*a4ddc2c8Schristos #include <dlfcn.h>
40*a4ddc2c8Schristos #include <pthread.h>
41*a4ddc2c8Schristos #include <unistd.h>
42*a4ddc2c8Schristos
43*a4ddc2c8Schristos #define DSO TESTDIR "/h_pthread_dlopen.so"
44*a4ddc2c8Schristos
45*a4ddc2c8Schristos void *
routine(void * arg)46*a4ddc2c8Schristos routine(void *arg)
47*a4ddc2c8Schristos {
48*a4ddc2c8Schristos ATF_REQUIRE((intptr_t)arg == 0xcafe);
49*a4ddc2c8Schristos return NULL;
50*a4ddc2c8Schristos }
51*a4ddc2c8Schristos
52*a4ddc2c8Schristos ATF_TC(dso_pthread_create_dso);
53*a4ddc2c8Schristos
ATF_TC_HEAD(dso_pthread_create_dso,tc)54*a4ddc2c8Schristos ATF_TC_HEAD(dso_pthread_create_dso, tc)
55*a4ddc2c8Schristos {
56*a4ddc2c8Schristos atf_tc_set_md_var(tc, "descr",
57*a4ddc2c8Schristos "Test if non -lpthread main can call pthread_create() "
58*a4ddc2c8Schristos "in -lpthread DSO");
59*a4ddc2c8Schristos }
60*a4ddc2c8Schristos
ATF_TC_BODY(dso_pthread_create_dso,tc)61*a4ddc2c8Schristos ATF_TC_BODY(dso_pthread_create_dso, tc)
62*a4ddc2c8Schristos {
63*a4ddc2c8Schristos int ret;
64*a4ddc2c8Schristos pthread_t thread;
65*a4ddc2c8Schristos void *arg = (void *)0xcafe;
66*a4ddc2c8Schristos void *handle;
67*a4ddc2c8Schristos int (*testf_dso_pthread_create)(pthread_t *, pthread_attr_t *,
68*a4ddc2c8Schristos void *(*)(void *), void *);
69*a4ddc2c8Schristos struct rlimit rl;
70*a4ddc2c8Schristos
71*a4ddc2c8Schristos atf_tc_expect_signal(6,
72*a4ddc2c8Schristos "calling pthread_create() requires -lpthread main");
73*a4ddc2c8Schristos
74*a4ddc2c8Schristos rl.rlim_max = rl.rlim_cur = 0;
75*a4ddc2c8Schristos ATF_REQUIRE_EQ(setrlimit(RLIMIT_CORE, &rl), 0);
76*a4ddc2c8Schristos
77*a4ddc2c8Schristos handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
78*a4ddc2c8Schristos ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
79*a4ddc2c8Schristos
80*a4ddc2c8Schristos testf_dso_pthread_create = dlsym(handle, "testf_dso_pthread_create");
81*a4ddc2c8Schristos ATF_REQUIRE_MSG(testf_dso_pthread_create != NULL,
82*a4ddc2c8Schristos "dlsym fails: %s", dlerror());
83*a4ddc2c8Schristos
84*a4ddc2c8Schristos ret = testf_dso_pthread_create(&thread, NULL, routine, arg);
85*a4ddc2c8Schristos ATF_REQUIRE(ret == 0);
86*a4ddc2c8Schristos
87*a4ddc2c8Schristos ATF_REQUIRE(dlclose(handle) == 0);
88*a4ddc2c8Schristos
89*a4ddc2c8Schristos }
90*a4ddc2c8Schristos
ATF_TP_ADD_TCS(tp)91*a4ddc2c8Schristos ATF_TP_ADD_TCS(tp)
92*a4ddc2c8Schristos {
93*a4ddc2c8Schristos ATF_TP_ADD_TC(tp, dso_pthread_create_dso);
94*a4ddc2c8Schristos
95*a4ddc2c8Schristos return atf_no_error();
96*a4ddc2c8Schristos }
97