Apache HTTPD
testglobalmutex.c
Go to the documentation of this file.
1/* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
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"
22#include "testutil.h"
23
26{
27 apr_procattr_t *procattr;
28 const char *args[3];
29 apr_status_t rv;
30
31 rv = apr_procattr_create(&procattr, p);
32 APR_ASSERT_SUCCESS(tc, "Couldn't create procattr", rv);
33
36 APR_ASSERT_SUCCESS(tc, "Couldn't set io in procattr", rv);
37
38 rv = apr_procattr_error_check_set(procattr, 1);
39 APR_ASSERT_SUCCESS(tc, "Couldn't set error check in procattr", rv);
40
41 args[0] = "globalmutexchild" EXTENSION;
42 args[1] = (const char*)apr_itoa(p, (int)mech);
43 args[2] = NULL;
44 rv = apr_proc_create(proc, TESTBINPATH "globalmutexchild" EXTENSION, args, NULL,
45 procattr, p);
46 APR_ASSERT_SUCCESS(tc, "Couldn't launch program", rv);
47}
48
50{
51 int exitcode;
53
54 ABTS_ASSERT(tc, "Error waiting for child process",
56
57 ABTS_ASSERT(tc, "child didn't terminate normally", why == APR_PROC_EXIT);
58 return exitcode;
59}
60
61/* return symbolic name for a locking meechanism */
62static const char *mutexname(apr_lockmech_e mech)
63{
64 switch (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 case APR_LOCK_DEFAULT_TIMED: return "default_timed";
72 default: return "unknown";
73 }
74}
75
76static void test_exclusive(abts_case *tc, void *data)
77{
79 apr_proc_t p1, p2, p3, p4;
80 apr_status_t rv;
81 apr_global_mutex_t *global_lock;
82 int x = 0;
83 abts_log_message("lock mechanism is: ");
85
86 rv = apr_global_mutex_create(&global_lock, LOCKNAME, mech, p);
87 if (rv == APR_ENOTIMPL) {
88 /* MacOS lacks TIMED implementation, so don't fail for ENOTIMPL */
89 ABTS_NOT_IMPL(tc, "global mutex TIMED not implemented");
90 return;
91 }
92 APR_ASSERT_SUCCESS(tc, "Error creating mutex", rv);
93
94 launch_child(tc, mech, &p1, p);
95 launch_child(tc, mech, &p2, p);
96 launch_child(tc, mech, &p3, p);
97 launch_child(tc, mech, &p4, p);
98
99 x += wait_child(tc, &p1);
100 x += wait_child(tc, &p2);
101 x += wait_child(tc, &p3);
102 x += wait_child(tc, &p4);
103
104 if (x != MAX_COUNTER) {
105 char buf[200];
106 sprintf(buf, "global mutex '%s' failed: %d not %d",
108 abts_fail(tc, buf, __LINE__);
109 }
110}
111
113{
115
116 suite = ADD_SUITE(suite)
118#if APR_HAS_POSIXSEM_SERIALIZE
121#endif
122#if APR_HAS_SYSVSEM_SERIALIZE
125#endif
126#if APR_HAS_PROC_PTHREAD_SERIALIZE
129#endif
130#if APR_HAS_FCNTL_SERIALIZE
133#endif
134#if APR_HAS_FLOCK_SERIALIZE
137#endif
140
141 return suite;
142}
143
void abts_run_test(abts_suite *ts, test_func f, void *value)
Definition abts.c:175
void abts_fail(abts_case *tc, const char *message, int lineno)
Definition abts.c:391
void abts_log_message(const char *fmt,...)
Definition abts.c:270
#define ADD_SUITE(suite)
Definition abts.h:67
#define ABTS_NOT_IMPL(a, b)
Definition abts.h:129
#define ABTS_ASSERT(a, b, c)
Definition abts.h:130
#define EXTENSION
Definition testutil.h:43
#define TESTBINPATH
Definition testutil.h:35
#define APR_ASSERT_SUCCESS(tc, ctxt, rv)
Definition testutil.h:58
APR Error Codes.
APR Global Locking Routines.
APR Strings library.
APR Thread and Process Library.
const unsigned char * buf
Definition util_md5.h:50
#define APR_CHILD_DONE
Definition apr_errno.h:446
#define APR_ENOTIMPL
Definition apr_errno.h:476
const char apr_lockmech_e mech
apr_size_t size
int apr_status_t
Definition apr_errno.h:44
void * data
apr_lockmech_e
@ APR_LOCK_FLOCK
@ APR_LOCK_SYSVSEM
@ APR_LOCK_POSIXSEM
@ APR_LOCK_DEFAULT_TIMED
@ APR_LOCK_PROC_PTHREAD
@ APR_LOCK_FCNTL
@ APR_LOCK_DEFAULT
apr_proc_t * proc
const char const char *const * args
#define APR_NO_PIPE
apr_exit_why_e
int * exitcode
@ APR_WAIT
@ APR_PROC_EXIT
apr_pool_t * p
Definition md_event.c:32
return NULL
Definition mod_so.c:359
abts_suite * testglobalmutex(abts_suite *suite)
static const char * mutexname(apr_lockmech_e mech)
static void launch_child(abts_case *tc, apr_lockmech_e mech, apr_proc_t *proc, apr_pool_t *p)
static int wait_child(abts_case *tc, apr_proc_t *proc)
static void test_exclusive(abts_case *tc, void *data)
#define LOCKNAME
#define MAX_COUNTER