11.1Schristos/*	$NetBSD: t_main_pthread_create.c,v 1.1 2013/03/21 16:50:21 christos Exp $ */
21.1Schristos/*-
31.1Schristos * Copyright (c) 2013 The NetBSD Foundation, Inc.
41.1Schristos * All rights reserved.
51.1Schristos *
61.1Schristos * This code is derived from software contributed to The NetBSD Foundation
71.1Schristos * by Emmanuel Dreyfus
81.1Schristos *
91.1Schristos * Redistribution and use in source and binary forms, with or without
101.1Schristos * modification, are permitted provided that the following conditions
111.1Schristos * are met:
121.1Schristos *
131.1Schristos * 1. Redistributions of source code must retain the above copyright
141.1Schristos *    notice, this list of conditions and the following disclaimer.
151.1Schristos * 2. Redistributions in binary form must reproduce the above copyright
161.1Schristos *    notice, this list of conditions and the following disclaimer in
171.1Schristos *    the documentation and/or other materials provided with the
181.1Schristos *    distribution.
191.1Schristos *
201.1Schristos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
211.1Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221.1Schristos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231.1Schristos * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
241.1Schristos * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
251.1Schristos * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
261.1Schristos * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
271.1Schristos * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
281.1Schristos * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
291.1Schristos * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
301.1Schristos * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311.1Schristos * SUCH DAMAGE.
321.1Schristos */
331.1Schristos
341.1Schristos#include <sys/cdefs.h>
351.1Schristos__RCSID("$NetBSD: t_main_pthread_create.c,v 1.1 2013/03/21 16:50:21 christos Exp $");
361.1Schristos
371.1Schristos#include <atf-c.h>
381.1Schristos#include <dlfcn.h>
391.1Schristos#include <pthread.h>
401.1Schristos#include <unistd.h>
411.1Schristos
421.1Schristos#define DSO TESTDIR "/h_pthread_dlopen.so"
431.1Schristos
441.1Schristosvoid *
451.1Schristosroutine(void *arg)
461.1Schristos{
471.1Schristos	ATF_REQUIRE((intptr_t)arg == 0xcafe);
481.1Schristos	return NULL;
491.1Schristos}
501.1Schristos
511.1SchristosATF_TC(main_pthread_create_main);
521.1Schristos
531.1SchristosATF_TC_HEAD(main_pthread_create_main, tc)
541.1Schristos{
551.1Schristos	atf_tc_set_md_var(tc, "descr",
561.1Schristos	    "Test if -lpthread main can call pthread_create() in main()");
571.1Schristos}
581.1Schristos
591.1SchristosATF_TC_BODY(main_pthread_create_main, tc)
601.1Schristos{
611.1Schristos	int ret;
621.1Schristos	pthread_t thread;
631.1Schristos	void *arg = (void *)0xcafe;
641.1Schristos
651.1Schristos	ret = pthread_create(&thread, NULL, routine, arg);
661.1Schristos	ATF_REQUIRE(ret == 0);
671.1Schristos}
681.1Schristos
691.1SchristosATF_TC(main_pthread_create_dso);
701.1Schristos
711.1SchristosATF_TC_HEAD(main_pthread_create_dso, tc)
721.1Schristos{
731.1Schristos	atf_tc_set_md_var(tc, "descr",
741.1Schristos	    "Test if -lpthread main can call pthread_create() in DSO");
751.1Schristos}
761.1Schristos
771.1SchristosATF_TC_BODY(main_pthread_create_dso, tc)
781.1Schristos{
791.1Schristos	int ret;
801.1Schristos	pthread_t thread;
811.1Schristos	void *arg = (void *)0xcafe;
821.1Schristos	void *handle;
831.1Schristos	int (*testf_dso_pthread_create)(pthread_t *, pthread_attr_t *,
841.1Schristos	    void *(*)(void *), void *);
851.1Schristos
861.1Schristos	handle = dlopen(DSO, RTLD_NOW | RTLD_LOCAL);
871.1Schristos	ATF_REQUIRE_MSG(handle != NULL, "dlopen fails: %s", dlerror());
881.1Schristos
891.1Schristos	testf_dso_pthread_create = dlsym(handle, "testf_dso_pthread_create");
901.1Schristos	ATF_REQUIRE_MSG(testf_dso_pthread_create != NULL,
911.1Schristos	    "dlsym fails: %s", dlerror());
921.1Schristos
931.1Schristos	ret = testf_dso_pthread_create(&thread, NULL, routine, arg);
941.1Schristos	ATF_REQUIRE(ret == 0);
951.1Schristos
961.1Schristos	ATF_REQUIRE(dlclose(handle) == 0);
971.1Schristos
981.1Schristos}
991.1Schristos
1001.1SchristosATF_TP_ADD_TCS(tp)
1011.1Schristos{
1021.1Schristos	ATF_TP_ADD_TC(tp, main_pthread_create_main);
1031.1Schristos	ATF_TP_ADD_TC(tp, main_pthread_create_dso);
1041.1Schristos
1051.1Schristos	return atf_no_error();
1061.1Schristos}
107