1
/* Copyright 2000-2005 The Apache Software Foundation or its licensors, as
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
8
* http://www.apache.org/licenses/LICENSE-2.0
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
17
#include "testglobalmutex.h"
18
#include "apr_thread_proc.h"
19
#include "apr_global_mutex.h"
20
#include "apr_strings.h"
21
#include "apr_errno.h"
24
static void launch_child(abts_case *tc, apr_lockmech_e mech,
25
apr_proc_t *proc, apr_pool_t *p)
27
apr_procattr_t *procattr;
31
rv = apr_procattr_create(&procattr, p);
32
APR_ASSERT_SUCCESS(tc, "Couldn't create procattr", rv);
34
rv = apr_procattr_io_set(procattr, APR_NO_PIPE, APR_NO_PIPE,
36
APR_ASSERT_SUCCESS(tc, "Couldn't set io in procattr", rv);
38
rv = apr_procattr_error_check_set(procattr, 1);
39
APR_ASSERT_SUCCESS(tc, "Couldn't set error check in procattr", rv);
41
args[0] = "globalmutexchild" EXTENSION;
42
args[1] = (const char*)apr_itoa(p, (int)mech);
44
rv = apr_proc_create(proc, "./globalmutexchild" EXTENSION, args, NULL,
46
APR_ASSERT_SUCCESS(tc, "Couldn't launch program", rv);
49
static int wait_child(abts_case *tc, apr_proc_t *proc)
54
ABTS_ASSERT(tc, "Error waiting for child process",
55
apr_proc_wait(proc, &exitcode, &why, APR_WAIT) == APR_CHILD_DONE);
57
ABTS_ASSERT(tc, "child didn't terminate normally", why == APR_PROC_EXIT);
61
/* return symbolic name for a locking meechanism */
62
static const char *mutexname(apr_lockmech_e mech)
65
case APR_LOCK_FCNTL: return "fcntl";
66
case APR_LOCK_FLOCK: return "flock";
67
case APR_LOCK_SYSVSEM: return "sysvsem";
68
case APR_LOCK_PROC_PTHREAD: return "proc_pthread";
69
case APR_LOCK_POSIXSEM: return "posixsem";
70
case APR_LOCK_DEFAULT: return "default";
71
default: return "unknown";
75
static void test_exclusive(abts_case *tc, void *data)
77
apr_lockmech_e mech = *(apr_lockmech_e *)data;
78
apr_proc_t p1, p2, p3, p4;
80
apr_global_mutex_t *global_lock;
82
abts_log_message("lock mechanism is: ");
83
abts_log_message(mutexname(mech));
85
rv = apr_global_mutex_create(&global_lock, LOCKNAME, mech, p);
86
APR_ASSERT_SUCCESS(tc, "Error creating mutex", rv);
88
launch_child(tc, mech, &p1, p);
89
launch_child(tc, mech, &p2, p);
90
launch_child(tc, mech, &p3, p);
91
launch_child(tc, mech, &p4, p);
93
x += wait_child(tc, &p1);
94
x += wait_child(tc, &p2);
95
x += wait_child(tc, &p3);
96
x += wait_child(tc, &p4);
98
if (x != MAX_COUNTER) {
100
sprintf(buf, "global mutex '%s' failed: %d not %d",
101
mutexname(mech), x, MAX_COUNTER);
102
abts_fail(tc, buf, __LINE__);
106
abts_suite *testglobalmutex(abts_suite *suite)
108
apr_lockmech_e mech = APR_LOCK_DEFAULT;
110
suite = ADD_SUITE(suite)
111
abts_run_test(suite, test_exclusive, &mech);
112
#if APR_HAS_POSIXSEM_SERIALIZE
113
mech = APR_LOCK_POSIXSEM;
114
abts_run_test(suite, test_exclusive, &mech);
116
#if APR_HAS_SYSVSEM_SERIALIZE
117
mech = APR_LOCK_SYSVSEM;
118
abts_run_test(suite, test_exclusive, &mech);
120
#if APR_HAS_PROC_PTHREAD_SERIALIZE
121
mech = APR_LOCK_PROC_PTHREAD;
122
abts_run_test(suite, test_exclusive, &mech);
124
#if APR_HAS_FCNTL_SERIALIZE
125
mech = APR_LOCK_FCNTL;
126
abts_run_test(suite, test_exclusive, &mech);
128
#if APR_HAS_FLOCK_SERIALIZE
129
mech = APR_LOCK_FLOCK;
130
abts_run_test(suite, test_exclusive, &mech);