1*f222e3ccSkamil /* $NetBSD: h_nsd_recurse.c,v 1.3 2020/06/01 01:03:21 kamil Exp $ */
2374d5969Spgoyette
3374d5969Spgoyette /*-
4374d5969Spgoyette * Copyright (c) 2008 The NetBSD Foundation, Inc.
5374d5969Spgoyette * All rights reserved.
6374d5969Spgoyette *
7374d5969Spgoyette * This code is derived from software contributed to The NetBSD Foundation
8374d5969Spgoyette * by Jason R. Thorpe.
9374d5969Spgoyette *
10374d5969Spgoyette * Redistribution and use in source and binary forms, with or without
11374d5969Spgoyette * modification, are permitted provided that the following conditions
12374d5969Spgoyette * are met:
13374d5969Spgoyette * 1. Redistributions of source code must retain the above copyright
14374d5969Spgoyette * notice, this list of conditions and the following disclaimer.
15374d5969Spgoyette * 2. Redistributions in binary form must reproduce the above copyright
16374d5969Spgoyette * notice, this list of conditions and the following disclaimer in the
17374d5969Spgoyette * documentation and/or other materials provided with the distribution.
18374d5969Spgoyette *
19374d5969Spgoyette * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20374d5969Spgoyette * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21374d5969Spgoyette * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22374d5969Spgoyette * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23374d5969Spgoyette * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24374d5969Spgoyette * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25374d5969Spgoyette * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26374d5969Spgoyette * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27374d5969Spgoyette * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28374d5969Spgoyette * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29374d5969Spgoyette * POSSIBILITY OF SUCH DAMAGE.
30374d5969Spgoyette */
31374d5969Spgoyette
32374d5969Spgoyette #include <sys/cdefs.h>
33374d5969Spgoyette __COPYRIGHT("@(#) Copyright (c) 2008\
34374d5969Spgoyette The NetBSD Foundation, inc. All rights reserved.");
35*f222e3ccSkamil __RCSID("$NetBSD: h_nsd_recurse.c,v 1.3 2020/06/01 01:03:21 kamil Exp $");
36374d5969Spgoyette
37*f222e3ccSkamil #ifndef _REENTRANT
38374d5969Spgoyette #define _REENTRANT
39*f222e3ccSkamil #endif
40374d5969Spgoyette
41374d5969Spgoyette #include <assert.h>
42374d5969Spgoyette #include <nsswitch.h>
43374d5969Spgoyette #include <pthread.h>
44374d5969Spgoyette #include <stdarg.h>
45374d5969Spgoyette #include <stdio.h>
46374d5969Spgoyette #include <stdlib.h>
47374d5969Spgoyette
48374d5969Spgoyette static const ns_src testsrc[] = {
49374d5969Spgoyette { "test", NS_SUCCESS },
50ec5cad1dSpgoyette { NULL, 0 }
51374d5969Spgoyette };
52374d5969Spgoyette
53374d5969Spgoyette static int
func3(void * rv,void * cb_data,va_list ap)54374d5969Spgoyette func3(void *rv, void *cb_data, va_list ap)
55374d5969Spgoyette {
56374d5969Spgoyette (void)printf("func3: enter\n");
57374d5969Spgoyette (void)printf("func3: exit\n");
58374d5969Spgoyette
59374d5969Spgoyette return NS_SUCCESS;
60374d5969Spgoyette }
61374d5969Spgoyette
62374d5969Spgoyette static int
func2(void * rv,void * cb_data,va_list ap)63374d5969Spgoyette func2(void *rv, void *cb_data, va_list ap)
64374d5969Spgoyette {
65374d5969Spgoyette static const ns_dtab dtab[] = {
66374d5969Spgoyette { "test", func3, NULL },
67ec5cad1dSpgoyette { NULL, NULL, NULL }
68374d5969Spgoyette };
69374d5969Spgoyette int r;
70374d5969Spgoyette
71374d5969Spgoyette (void)printf("func2: enter\n");
72374d5969Spgoyette r = nsdispatch(NULL, dtab, "test", "test", testsrc);
73374d5969Spgoyette (void)printf("func2: exit\n");
74374d5969Spgoyette
75374d5969Spgoyette return r;
76374d5969Spgoyette }
77374d5969Spgoyette
78374d5969Spgoyette static int
func1(void)79374d5969Spgoyette func1(void)
80374d5969Spgoyette {
81374d5969Spgoyette static const ns_dtab dtab[] = {
82374d5969Spgoyette { "test", func2, NULL },
83ec5cad1dSpgoyette { NULL, NULL, NULL }
84374d5969Spgoyette };
85374d5969Spgoyette int r;
86374d5969Spgoyette
87374d5969Spgoyette (void)printf("func1: enter\n");
88374d5969Spgoyette r = nsdispatch(NULL, dtab, "test", "test", testsrc);
89374d5969Spgoyette (void)printf("func1: exit\n");
90374d5969Spgoyette
91374d5969Spgoyette return r;
92374d5969Spgoyette }
93374d5969Spgoyette
94374d5969Spgoyette static void *
thrfunc(void * arg)95374d5969Spgoyette thrfunc(void *arg)
96374d5969Spgoyette {
97374d5969Spgoyette pthread_exit(NULL);
98374d5969Spgoyette }
99374d5969Spgoyette
100374d5969Spgoyette int
main(int argc,char * argv[])101374d5969Spgoyette main(int argc, char *argv[])
102374d5969Spgoyette {
103374d5969Spgoyette pthread_t thr;
104374d5969Spgoyette void *threval;
105374d5969Spgoyette
106374d5969Spgoyette assert(pthread_create(&thr, NULL, thrfunc, NULL) == 0);
107374d5969Spgoyette assert(func1() == NS_SUCCESS);
108374d5969Spgoyette assert(pthread_join(thr, &threval) == 0);
109374d5969Spgoyette }
110