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