1*11be35a1SLionel Sambuc /* $NetBSD: t_getenv_thread.c,v 1.2 2012/03/15 02:02:23 joerg Exp $ */
2*11be35a1SLionel Sambuc
3*11be35a1SLionel Sambuc /*-
4*11be35a1SLionel Sambuc * Copyright (c) 2010 The NetBSD Foundation, Inc.
5*11be35a1SLionel Sambuc * All rights reserved.
6*11be35a1SLionel Sambuc *
7*11be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
8*11be35a1SLionel Sambuc * by Matthias Scheler.
9*11be35a1SLionel Sambuc *
10*11be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*11be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
12*11be35a1SLionel Sambuc * are met:
13*11be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*11be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*11be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*11be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*11be35a1SLionel Sambuc *
19*11be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20*11be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21*11be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22*11be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23*11be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24*11be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25*11be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26*11be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27*11be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28*11be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29*11be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
30*11be35a1SLionel Sambuc */
31*11be35a1SLionel Sambuc #include <sys/cdefs.h>
32*11be35a1SLionel Sambuc __RCSID("$NetBSD: t_getenv_thread.c,v 1.2 2012/03/15 02:02:23 joerg Exp $");
33*11be35a1SLionel Sambuc
34*11be35a1SLionel Sambuc #include <atf-c.h>
35*11be35a1SLionel Sambuc #include <errno.h>
36*11be35a1SLionel Sambuc #include <pthread.h>
37*11be35a1SLionel Sambuc #include <stdio.h>
38*11be35a1SLionel Sambuc #include <stdlib.h>
39*11be35a1SLionel Sambuc #include <string.h>
40*11be35a1SLionel Sambuc #include <time.h>
41*11be35a1SLionel Sambuc
42*11be35a1SLionel Sambuc #define THREADED_NUM_THREADS 8
43*11be35a1SLionel Sambuc #define THREADED_NUM_VARS 16
44*11be35a1SLionel Sambuc #define THREADED_VAR_NAME "THREADED%zu"
45*11be35a1SLionel Sambuc #define THREADED_RUN_TIME 10
46*11be35a1SLionel Sambuc
47*11be35a1SLionel Sambuc static void *thread_getenv_r(void *);
48*11be35a1SLionel Sambuc static void *thread_putenv(void *);
49*11be35a1SLionel Sambuc static void *thread_setenv(void *);
50*11be35a1SLionel Sambuc static void *thread_unsetenv(void *);
51*11be35a1SLionel Sambuc
52*11be35a1SLionel Sambuc static void *
thread_getenv_r(void * arg)53*11be35a1SLionel Sambuc thread_getenv_r(void *arg)
54*11be35a1SLionel Sambuc {
55*11be35a1SLionel Sambuc time_t endtime;
56*11be35a1SLionel Sambuc
57*11be35a1SLionel Sambuc endtime = *(time_t *)arg;
58*11be35a1SLionel Sambuc do {
59*11be35a1SLionel Sambuc size_t i;
60*11be35a1SLionel Sambuc char name[32], value[128];
61*11be35a1SLionel Sambuc
62*11be35a1SLionel Sambuc i = lrand48() % THREADED_NUM_VARS;
63*11be35a1SLionel Sambuc (void)snprintf(name, sizeof(name), THREADED_VAR_NAME, i);
64*11be35a1SLionel Sambuc
65*11be35a1SLionel Sambuc if (getenv_r(name, value, sizeof(value)) == -1) {
66*11be35a1SLionel Sambuc ATF_CHECK(errno == ENOENT);
67*11be35a1SLionel Sambuc }
68*11be35a1SLionel Sambuc } while (time(NULL) < endtime);
69*11be35a1SLionel Sambuc
70*11be35a1SLionel Sambuc return NULL;
71*11be35a1SLionel Sambuc }
72*11be35a1SLionel Sambuc
73*11be35a1SLionel Sambuc
74*11be35a1SLionel Sambuc static void *
thread_putenv(void * arg)75*11be35a1SLionel Sambuc thread_putenv(void *arg)
76*11be35a1SLionel Sambuc {
77*11be35a1SLionel Sambuc time_t endtime;
78*11be35a1SLionel Sambuc size_t i;
79*11be35a1SLionel Sambuc static char vars[THREADED_NUM_VARS][128];
80*11be35a1SLionel Sambuc
81*11be35a1SLionel Sambuc for (i = 0; i < THREADED_NUM_VARS; i++) {
82*11be35a1SLionel Sambuc (void)snprintf(vars[i], sizeof(vars[i]),
83*11be35a1SLionel Sambuc THREADED_VAR_NAME "=putenv %ld", i, lrand48());
84*11be35a1SLionel Sambuc }
85*11be35a1SLionel Sambuc
86*11be35a1SLionel Sambuc endtime = *(time_t *)arg;
87*11be35a1SLionel Sambuc do {
88*11be35a1SLionel Sambuc char name[128];
89*11be35a1SLionel Sambuc
90*11be35a1SLionel Sambuc i = lrand48() % THREADED_NUM_VARS;
91*11be35a1SLionel Sambuc (void)strlcpy(name, vars[i], sizeof(name));
92*11be35a1SLionel Sambuc *strchr(name, '=') = '\0';
93*11be35a1SLionel Sambuc
94*11be35a1SLionel Sambuc ATF_CHECK(unsetenv(name) != -1);
95*11be35a1SLionel Sambuc ATF_CHECK(putenv(vars[i]) != -1);
96*11be35a1SLionel Sambuc } while (time(NULL) < endtime);
97*11be35a1SLionel Sambuc
98*11be35a1SLionel Sambuc return NULL;
99*11be35a1SLionel Sambuc }
100*11be35a1SLionel Sambuc
101*11be35a1SLionel Sambuc static void *
thread_setenv(void * arg)102*11be35a1SLionel Sambuc thread_setenv(void *arg)
103*11be35a1SLionel Sambuc {
104*11be35a1SLionel Sambuc time_t endtime;
105*11be35a1SLionel Sambuc
106*11be35a1SLionel Sambuc endtime = *(time_t *)arg;
107*11be35a1SLionel Sambuc do {
108*11be35a1SLionel Sambuc size_t i;
109*11be35a1SLionel Sambuc char name[32], value[64];
110*11be35a1SLionel Sambuc
111*11be35a1SLionel Sambuc i = lrand48() % THREADED_NUM_VARS;
112*11be35a1SLionel Sambuc (void)snprintf(name, sizeof(name), THREADED_VAR_NAME, i);
113*11be35a1SLionel Sambuc (void)snprintf(value, sizeof(value), "setenv %ld", lrand48());
114*11be35a1SLionel Sambuc
115*11be35a1SLionel Sambuc ATF_CHECK(setenv(name, value, 1) != -1);
116*11be35a1SLionel Sambuc } while (time(NULL) < endtime);
117*11be35a1SLionel Sambuc
118*11be35a1SLionel Sambuc return NULL;
119*11be35a1SLionel Sambuc }
120*11be35a1SLionel Sambuc
121*11be35a1SLionel Sambuc static void *
thread_unsetenv(void * arg)122*11be35a1SLionel Sambuc thread_unsetenv(void *arg)
123*11be35a1SLionel Sambuc {
124*11be35a1SLionel Sambuc time_t endtime;
125*11be35a1SLionel Sambuc
126*11be35a1SLionel Sambuc endtime = *(time_t *)arg;
127*11be35a1SLionel Sambuc do {
128*11be35a1SLionel Sambuc size_t i;
129*11be35a1SLionel Sambuc char name[32];
130*11be35a1SLionel Sambuc
131*11be35a1SLionel Sambuc i = lrand48() % THREADED_NUM_VARS;
132*11be35a1SLionel Sambuc (void)snprintf(name, sizeof(name), THREADED_VAR_NAME, i);
133*11be35a1SLionel Sambuc
134*11be35a1SLionel Sambuc ATF_CHECK(unsetenv(name) != -1);
135*11be35a1SLionel Sambuc } while (time(NULL) < endtime);
136*11be35a1SLionel Sambuc
137*11be35a1SLionel Sambuc return NULL;
138*11be35a1SLionel Sambuc }
139*11be35a1SLionel Sambuc
140*11be35a1SLionel Sambuc ATF_TC(getenv_r_thread);
ATF_TC_HEAD(getenv_r_thread,tc)141*11be35a1SLionel Sambuc ATF_TC_HEAD(getenv_r_thread, tc)
142*11be35a1SLionel Sambuc {
143*11be35a1SLionel Sambuc
144*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test getenv_r(3) with threads");
145*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "%d", THREADED_RUN_TIME + 5);
146*11be35a1SLionel Sambuc }
147*11be35a1SLionel Sambuc
ATF_TC_BODY(getenv_r_thread,tc)148*11be35a1SLionel Sambuc ATF_TC_BODY(getenv_r_thread, tc)
149*11be35a1SLionel Sambuc {
150*11be35a1SLionel Sambuc pthread_t threads[THREADED_NUM_THREADS];
151*11be35a1SLionel Sambuc time_t endtime;
152*11be35a1SLionel Sambuc size_t i, j;
153*11be35a1SLionel Sambuc
154*11be35a1SLionel Sambuc endtime = time(NULL) + THREADED_RUN_TIME;
155*11be35a1SLionel Sambuc
156*11be35a1SLionel Sambuc for (i = j = 0; j < 2; j++) {
157*11be35a1SLionel Sambuc
158*11be35a1SLionel Sambuc ATF_CHECK(pthread_create(&threads[i++], NULL, thread_getenv_r,
159*11be35a1SLionel Sambuc &endtime) == 0);
160*11be35a1SLionel Sambuc }
161*11be35a1SLionel Sambuc
162*11be35a1SLionel Sambuc for (j = 0; j < i; j++)
163*11be35a1SLionel Sambuc ATF_CHECK(pthread_join(threads[j], NULL) == 0);
164*11be35a1SLionel Sambuc }
165*11be35a1SLionel Sambuc
166*11be35a1SLionel Sambuc ATF_TC(putenv_thread);
ATF_TC_HEAD(putenv_thread,tc)167*11be35a1SLionel Sambuc ATF_TC_HEAD(putenv_thread, tc)
168*11be35a1SLionel Sambuc {
169*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test concurrent access by putenv(3)");
170*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "%d", THREADED_RUN_TIME + 5);
171*11be35a1SLionel Sambuc }
172*11be35a1SLionel Sambuc
ATF_TC_BODY(putenv_thread,tc)173*11be35a1SLionel Sambuc ATF_TC_BODY(putenv_thread, tc)
174*11be35a1SLionel Sambuc {
175*11be35a1SLionel Sambuc pthread_t threads[THREADED_NUM_THREADS];
176*11be35a1SLionel Sambuc time_t endtime;
177*11be35a1SLionel Sambuc size_t i, j;
178*11be35a1SLionel Sambuc
179*11be35a1SLionel Sambuc endtime = time(NULL) + THREADED_RUN_TIME;
180*11be35a1SLionel Sambuc
181*11be35a1SLionel Sambuc for (i = j = 0; j < 2; j++) {
182*11be35a1SLionel Sambuc
183*11be35a1SLionel Sambuc ATF_CHECK(pthread_create(&threads[i++], NULL, thread_putenv,
184*11be35a1SLionel Sambuc &endtime) == 0);
185*11be35a1SLionel Sambuc }
186*11be35a1SLionel Sambuc
187*11be35a1SLionel Sambuc for (j = 0; j < i; j++)
188*11be35a1SLionel Sambuc ATF_CHECK(pthread_join(threads[j], NULL) == 0);
189*11be35a1SLionel Sambuc }
190*11be35a1SLionel Sambuc
191*11be35a1SLionel Sambuc ATF_TC(setenv_thread);
ATF_TC_HEAD(setenv_thread,tc)192*11be35a1SLionel Sambuc ATF_TC_HEAD(setenv_thread, tc)
193*11be35a1SLionel Sambuc {
194*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test concurrent access by setenv(3)");
195*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "%d", THREADED_RUN_TIME + 5);
196*11be35a1SLionel Sambuc }
197*11be35a1SLionel Sambuc
ATF_TC_BODY(setenv_thread,tc)198*11be35a1SLionel Sambuc ATF_TC_BODY(setenv_thread, tc)
199*11be35a1SLionel Sambuc {
200*11be35a1SLionel Sambuc pthread_t threads[THREADED_NUM_THREADS];
201*11be35a1SLionel Sambuc time_t endtime;
202*11be35a1SLionel Sambuc size_t i, j;
203*11be35a1SLionel Sambuc
204*11be35a1SLionel Sambuc endtime = time(NULL) + THREADED_RUN_TIME;
205*11be35a1SLionel Sambuc
206*11be35a1SLionel Sambuc for (i = j = 0; j < 2; j++) {
207*11be35a1SLionel Sambuc
208*11be35a1SLionel Sambuc ATF_CHECK(pthread_create(&threads[i++], NULL, thread_setenv,
209*11be35a1SLionel Sambuc &endtime) == 0);
210*11be35a1SLionel Sambuc }
211*11be35a1SLionel Sambuc
212*11be35a1SLionel Sambuc for (j = 0; j < i; j++)
213*11be35a1SLionel Sambuc ATF_CHECK(pthread_join(threads[j], NULL) == 0);
214*11be35a1SLionel Sambuc }
215*11be35a1SLionel Sambuc
216*11be35a1SLionel Sambuc ATF_TC(unsetenv_thread);
ATF_TC_HEAD(unsetenv_thread,tc)217*11be35a1SLionel Sambuc ATF_TC_HEAD(unsetenv_thread, tc)
218*11be35a1SLionel Sambuc {
219*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Test unsetenv(3) with threads");
220*11be35a1SLionel Sambuc atf_tc_set_md_var(tc, "timeout", "%d", THREADED_RUN_TIME + 5);
221*11be35a1SLionel Sambuc }
222*11be35a1SLionel Sambuc
ATF_TC_BODY(unsetenv_thread,tc)223*11be35a1SLionel Sambuc ATF_TC_BODY(unsetenv_thread, tc)
224*11be35a1SLionel Sambuc {
225*11be35a1SLionel Sambuc pthread_t threads[THREADED_NUM_THREADS];
226*11be35a1SLionel Sambuc time_t endtime;
227*11be35a1SLionel Sambuc size_t i, j;
228*11be35a1SLionel Sambuc
229*11be35a1SLionel Sambuc endtime = time(NULL) + THREADED_RUN_TIME;
230*11be35a1SLionel Sambuc
231*11be35a1SLionel Sambuc for (i = j = 0; j < 2; j++) {
232*11be35a1SLionel Sambuc
233*11be35a1SLionel Sambuc ATF_CHECK(pthread_create(&threads[i++], NULL, thread_unsetenv,
234*11be35a1SLionel Sambuc &endtime) == 0);
235*11be35a1SLionel Sambuc }
236*11be35a1SLionel Sambuc
237*11be35a1SLionel Sambuc for (j = 0; j < i; j++)
238*11be35a1SLionel Sambuc ATF_CHECK(pthread_join(threads[j], NULL) == 0);
239*11be35a1SLionel Sambuc }
240*11be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)241*11be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
242*11be35a1SLionel Sambuc {
243*11be35a1SLionel Sambuc
244*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, getenv_r_thread);
245*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, putenv_thread);
246*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, setenv_thread);
247*11be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, unsetenv_thread);
248*11be35a1SLionel Sambuc
249*11be35a1SLionel Sambuc return atf_no_error();
250*11be35a1SLionel Sambuc }
251