1272343Sngie/*	$NetBSD: t_dso_pthread_create.c,v 1.1 2013/03/21 16:50:21 christos Exp $ */
2272343Sngie/*-
3272343Sngie * Copyright (c) 2013 The NetBSD Foundation, Inc.
4272343Sngie * All rights reserved.
5272343Sngie *
6272343Sngie * This code is derived from software contributed to The NetBSD Foundation
7272343Sngie * by Emmanuel Dreyfus
8272343Sngie *
9272343Sngie * Redistribution and use in source and binary forms, with or without
10272343Sngie * modification, are permitted provided that the following conditions
11272343Sngie * are met:
12272343Sngie *
13272343Sngie * 1. Redistributions of source code must retain the above copyright
14272343Sngie *    notice, this list of conditions and the following disclaimer.
15272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
16272343Sngie *    notice, this list of conditions and the following disclaimer in
17272343Sngie *    the documentation and/or other materials provided with the
18272343Sngie *    distribution.
19272343Sngie *
20272343Sngie * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22272343Sngie * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23272343Sngie * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
24272343Sngie * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25272343Sngie * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26272343Sngie * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27272343Sngie * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28272343Sngie * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29272343Sngie * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30272343Sngie * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31272343Sngie * SUCH DAMAGE.
32272343Sngie */
33272343Sngie
34272343Sngie#include <sys/cdefs.h>
35272343Sngie__RCSID("$NetBSD: t_dso_pthread_create.c,v 1.1 2013/03/21 16:50:21 christos Exp $");
36272343Sngie
37272343Sngie#include <sys/resource.h>
38272343Sngie#include <atf-c.h>
39272343Sngie#include <dlfcn.h>
40272343Sngie#include <pthread.h>
41272343Sngie#include <unistd.h>
42272343Sngie
43272343Sngie#define DSO TESTDIR "/h_pthread_dlopen.so"
44272343Sngie
45272343Sngievoid *
46272343Sngieroutine(void *arg)
47272343Sngie{
48272343Sngie	ATF_REQUIRE((intptr_t)arg == 0xcafe);
49272343Sngie	return NULL;
50272343Sngie}
51272343Sngie
52272343SngieATF_TC(dso_pthread_create_dso);
53272343Sngie
54272343SngieATF_TC_HEAD(dso_pthread_create_dso, tc)
55272343Sngie{
56272343Sngie	atf_tc_set_md_var(tc, "descr",
57272343Sngie	    "Test if non -lpthread main can call pthread_create() "
58272343Sngie	    "in -lpthread DSO");
59272343Sngie}
60272343Sngie
61272343SngieATF_TC_BODY(dso_pthread_create_dso, tc)
62272343Sngie{
63272343Sngie	int ret;
64272343Sngie	pthread_t thread;
65272343Sngie	void *arg = (void *)0xcafe;
66272343Sngie	void *handle;
67272343Sngie	int (*testf_dso_pthread_create)(pthread_t *, pthread_attr_t *,
68272343Sngie	    void *(*)(void *), void *);
69272343Sngie	struct rlimit rl;
70272343Sngie
71272343Sngie	atf_tc_expect_signal(6,
72272343Sngie	    "calling pthread_create() requires -lpthread main");
73272343Sngie
74272343Sngie	rl.rlim_max = rl.rlim_cur = 0;
75272343Sngie	ATF_REQUIRE_EQ(setrlimit(RLIMIT_CORE, &rl), 0);
76272343Sngie
77272343Sngie	handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
78272343Sngie	ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
79272343Sngie
80272343Sngie	testf_dso_pthread_create = dlsym(handle, "testf_dso_pthread_create");
81272343Sngie	ATF_REQUIRE_MSG(testf_dso_pthread_create != NULL,
82272343Sngie	    "dlsym fails: %s", dlerror());
83272343Sngie
84272343Sngie	ret = testf_dso_pthread_create(&thread, NULL, routine, arg);
85272343Sngie	ATF_REQUIRE(ret == 0);
86272343Sngie
87272343Sngie	ATF_REQUIRE(dlclose(handle) == 0);
88272343Sngie
89272343Sngie}
90272343Sngie
91272343SngieATF_TP_ADD_TCS(tp)
92272343Sngie{
93272343Sngie	ATF_TP_ADD_TC(tp, dso_pthread_create_dso);
94272343Sngie
95272343Sngie	return atf_no_error();
96272343Sngie}
97