1 /* $NetBSD: t_getenv_thread.c,v 1.2 2012/03/15 02:02:23 joerg Exp $ */
4 * Copyright (c) 2010 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_getenv_thread.c,v 1.2 2012/03/15 02:02:23 joerg Exp $");
42 #define THREADED_NUM_THREADS 8
43 #define THREADED_NUM_VARS 16
44 #define THREADED_VAR_NAME "THREADED%zu"
45 #define THREADED_RUN_TIME 10
47 static void *thread_getenv_r(void *);
48 static void *thread_putenv(void *);
49 static void *thread_setenv(void *);
50 static void *thread_unsetenv(void *);
53 thread_getenv_r(void *arg
)
57 endtime
= *(time_t *)arg
;
60 char name
[32], value
[128];
62 i
= lrand48() % THREADED_NUM_VARS
;
63 (void)snprintf(name
, sizeof(name
), THREADED_VAR_NAME
, i
);
65 if (getenv_r(name
, value
, sizeof(value
)) == -1) {
66 ATF_CHECK(errno
== ENOENT
);
68 } while (time(NULL
) < endtime
);
75 thread_putenv(void *arg
)
79 static char vars
[THREADED_NUM_VARS
][128];
81 for (i
= 0; i
< THREADED_NUM_VARS
; i
++) {
82 (void)snprintf(vars
[i
], sizeof(vars
[i
]),
83 THREADED_VAR_NAME
"=putenv %ld", i
, lrand48());
86 endtime
= *(time_t *)arg
;
90 i
= lrand48() % THREADED_NUM_VARS
;
91 (void)strlcpy(name
, vars
[i
], sizeof(name
));
92 *strchr(name
, '=') = '\0';
94 ATF_CHECK(unsetenv(name
) != -1);
95 ATF_CHECK(putenv(vars
[i
]) != -1);
96 } while (time(NULL
) < endtime
);
102 thread_setenv(void *arg
)
106 endtime
= *(time_t *)arg
;
109 char name
[32], value
[64];
111 i
= lrand48() % THREADED_NUM_VARS
;
112 (void)snprintf(name
, sizeof(name
), THREADED_VAR_NAME
, i
);
113 (void)snprintf(value
, sizeof(value
), "setenv %ld", lrand48());
115 ATF_CHECK(setenv(name
, value
, 1) != -1);
116 } while (time(NULL
) < endtime
);
122 thread_unsetenv(void *arg
)
126 endtime
= *(time_t *)arg
;
131 i
= lrand48() % THREADED_NUM_VARS
;
132 (void)snprintf(name
, sizeof(name
), THREADED_VAR_NAME
, i
);
134 ATF_CHECK(unsetenv(name
) != -1);
135 } while (time(NULL
) < endtime
);
140 ATF_TC(getenv_r_thread
);
141 ATF_TC_HEAD(getenv_r_thread
, tc
)
144 atf_tc_set_md_var(tc
, "descr", "Test getenv_r(3) with threads");
145 atf_tc_set_md_var(tc
, "timeout", "%d", THREADED_RUN_TIME
+ 5);
148 ATF_TC_BODY(getenv_r_thread
, tc
)
150 pthread_t threads
[THREADED_NUM_THREADS
];
154 endtime
= time(NULL
) + THREADED_RUN_TIME
;
156 for (i
= j
= 0; j
< 2; j
++) {
158 ATF_CHECK(pthread_create(&threads
[i
++], NULL
, thread_getenv_r
,
162 for (j
= 0; j
< i
; j
++)
163 ATF_CHECK(pthread_join(threads
[j
], NULL
) == 0);
166 ATF_TC(putenv_thread
);
167 ATF_TC_HEAD(putenv_thread
, tc
)
169 atf_tc_set_md_var(tc
, "descr", "Test concurrent access by putenv(3)");
170 atf_tc_set_md_var(tc
, "timeout", "%d", THREADED_RUN_TIME
+ 5);
173 ATF_TC_BODY(putenv_thread
, tc
)
175 pthread_t threads
[THREADED_NUM_THREADS
];
179 endtime
= time(NULL
) + THREADED_RUN_TIME
;
181 for (i
= j
= 0; j
< 2; j
++) {
183 ATF_CHECK(pthread_create(&threads
[i
++], NULL
, thread_putenv
,
187 for (j
= 0; j
< i
; j
++)
188 ATF_CHECK(pthread_join(threads
[j
], NULL
) == 0);
191 ATF_TC(setenv_thread
);
192 ATF_TC_HEAD(setenv_thread
, tc
)
194 atf_tc_set_md_var(tc
, "descr", "Test concurrent access by setenv(3)");
195 atf_tc_set_md_var(tc
, "timeout", "%d", THREADED_RUN_TIME
+ 5);
198 ATF_TC_BODY(setenv_thread
, tc
)
200 pthread_t threads
[THREADED_NUM_THREADS
];
204 endtime
= time(NULL
) + THREADED_RUN_TIME
;
206 for (i
= j
= 0; j
< 2; j
++) {
208 ATF_CHECK(pthread_create(&threads
[i
++], NULL
, thread_setenv
,
212 for (j
= 0; j
< i
; j
++)
213 ATF_CHECK(pthread_join(threads
[j
], NULL
) == 0);
216 ATF_TC(unsetenv_thread
);
217 ATF_TC_HEAD(unsetenv_thread
, tc
)
219 atf_tc_set_md_var(tc
, "descr", "Test unsetenv(3) with threads");
220 atf_tc_set_md_var(tc
, "timeout", "%d", THREADED_RUN_TIME
+ 5);
223 ATF_TC_BODY(unsetenv_thread
, tc
)
225 pthread_t threads
[THREADED_NUM_THREADS
];
229 endtime
= time(NULL
) + THREADED_RUN_TIME
;
231 for (i
= j
= 0; j
< 2; j
++) {
233 ATF_CHECK(pthread_create(&threads
[i
++], NULL
, thread_unsetenv
,
237 for (j
= 0; j
< i
; j
++)
238 ATF_CHECK(pthread_join(threads
[j
], NULL
) == 0);
244 ATF_TP_ADD_TC(tp
, getenv_r_thread
);
245 ATF_TP_ADD_TC(tp
, putenv_thread
);
246 ATF_TP_ADD_TC(tp
, setenv_thread
);
247 ATF_TP_ADD_TC(tp
, unsetenv_thread
);
249 return atf_no_error();