xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/test/testprocmutex.c (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 "apr_shm.h"
18 #include "apr_thread_proc.h"
19 #include "apr_file_io.h"
20 #include "apr_proc_mutex.h"
21 #include "apr_errno.h"
22 #include "apr_general.h"
23 #include "apr_getopt.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include "testutil.h"
27 
28 #if APR_HAS_FORK
29 
30 #define MAX_ITER 200
31 #define CHILDREN 6
32 #define MAX_COUNTER (MAX_ITER * CHILDREN)
33 #define MAX_WAIT_USEC (1000*1000)
34 
35 static apr_proc_mutex_t *proc_lock;
36 static volatile int *x;
37 
38 /* a slower more racy way to implement (*x)++ */
increment(int n)39 static int increment(int n)
40 {
41     apr_sleep(1);
42     return n+1;
43 }
44 
make_child(abts_case * tc,int trylock,apr_proc_t ** proc,apr_pool_t * p)45 static void make_child(abts_case *tc, int trylock, apr_proc_t **proc, apr_pool_t *p)
46 {
47     apr_status_t rv;
48 
49     *proc = apr_pcalloc(p, sizeof(**proc));
50 
51     /* slight delay to allow things to settle */
52     apr_sleep (1);
53 
54     rv = apr_proc_fork(*proc, p);
55     if (rv == APR_INCHILD) {
56         int i = 0;
57         /* The parent process has setup all processes to call apr_terminate
58          * at exit.  But, that means that all processes must also call
59          * apr_initialize at startup.  You cannot have an unequal number
60          * of apr_terminate and apr_initialize calls.  If you do, bad things
61          * will happen.  In this case, the bad thing is that if the mutex
62          * is a semaphore, it will be destroyed before all of the processes
63          * die.  That means that the test will most likely fail.
64          */
65         apr_initialize();
66 
67         if (apr_proc_mutex_child_init(&proc_lock, NULL, p))
68             exit(1);
69 
70         do {
71             if (trylock) {
72                 int wait_usec = 0;
73 
74                 while ((rv = apr_proc_mutex_trylock(proc_lock))) {
75                     if (!APR_STATUS_IS_EBUSY(rv))
76                         exit(1);
77                     if (++wait_usec >= MAX_WAIT_USEC)
78                         exit(1);
79                     apr_sleep(1);
80                 }
81             }
82             else {
83                 if (apr_proc_mutex_lock(proc_lock))
84                     exit(1);
85             }
86 
87             i++;
88             *x = increment(*x);
89             if (apr_proc_mutex_unlock(proc_lock))
90                 exit(1);
91         } while (i < MAX_ITER);
92         exit(0);
93     }
94 
95     ABTS_ASSERT(tc, "fork failed", rv == APR_INPARENT);
96 }
97 
98 /* Wait for a child process and check it terminated with success. */
await_child(abts_case * tc,apr_proc_t * proc)99 static void await_child(abts_case *tc, apr_proc_t *proc)
100 {
101     int code;
102     apr_exit_why_e why;
103     apr_status_t rv;
104 
105     rv = apr_proc_wait(proc, &code, &why, APR_WAIT);
106     ABTS_ASSERT(tc, "child did not terminate with success",
107              rv == APR_CHILD_DONE && why == APR_PROC_EXIT && code == 0);
108 }
109 
test_exclusive(abts_case * tc,const char * lockname,apr_lockmech_e mech)110 static void test_exclusive(abts_case *tc, const char *lockname,
111                            apr_lockmech_e mech)
112 {
113     apr_proc_t *child[CHILDREN];
114     apr_status_t rv;
115     int n;
116 
117     rv = apr_proc_mutex_create(&proc_lock, lockname, mech, p);
118     APR_ASSERT_SUCCESS(tc, "create the mutex", rv);
119     if (rv != APR_SUCCESS)
120         return;
121 
122     for (n = 0; n < CHILDREN; n++)
123         make_child(tc, 0, &child[n], p);
124 
125     for (n = 0; n < CHILDREN; n++)
126         await_child(tc, child[n]);
127 
128     ABTS_ASSERT(tc, "Locks don't appear to work", *x == MAX_COUNTER);
129 
130     rv = apr_proc_mutex_trylock(proc_lock);
131     if (rv == APR_ENOTIMPL) {
132         ABTS_NOT_IMPL(tc, "apr_proc_mutex_trylock not implemented");
133         return;
134     }
135     APR_ASSERT_SUCCESS(tc, "check for trylock", rv);
136 
137     rv = apr_proc_mutex_unlock(proc_lock);
138     APR_ASSERT_SUCCESS(tc, "unlock after trylock check", rv);
139 
140     *x = 0;
141 
142     for (n = 0; n < CHILDREN; n++)
143         make_child(tc, 1, &child[n], p);
144 
145     for (n = 0; n < CHILDREN; n++)
146         await_child(tc, child[n]);
147 
148     ABTS_ASSERT(tc, "Locks don't appear to work with trylock",
149                 *x == MAX_COUNTER);
150 }
151 #endif
152 
proc_mutex(abts_case * tc,void * data)153 static void proc_mutex(abts_case *tc, void *data)
154 {
155 #if APR_HAS_FORK
156     apr_status_t rv;
157     const char *shmname = "tpm.shm";
158     apr_shm_t *shm;
159     apr_lockmech_e *mech = data;
160 
161     /* Use anonymous shm if available. */
162     rv = apr_shm_create(&shm, sizeof(int), NULL, p);
163     if (rv == APR_ENOTIMPL) {
164         apr_file_remove(shmname, p);
165         rv = apr_shm_create(&shm, sizeof(int), shmname, p);
166     }
167 
168     APR_ASSERT_SUCCESS(tc, "create shm segment", rv);
169     if (rv != APR_SUCCESS)
170         return;
171 
172     x = apr_shm_baseaddr_get(shm);
173     test_exclusive(tc, NULL, *mech);
174     rv = apr_shm_destroy(shm);
175     APR_ASSERT_SUCCESS(tc, "Error destroying shared memory block", rv);
176 #else
177     ABTS_NOT_IMPL(tc, "APR lacks fork() support");
178 #endif
179 }
180 
181 
testprocmutex(abts_suite * suite)182 abts_suite *testprocmutex(abts_suite *suite)
183 {
184     apr_lockmech_e mech = APR_LOCK_DEFAULT;
185 
186     suite = ADD_SUITE(suite)
187     abts_run_test(suite, proc_mutex, &mech);
188 #if APR_HAS_POSIXSEM_SERIALIZE
189     mech = APR_LOCK_POSIXSEM;
190     abts_run_test(suite, proc_mutex, &mech);
191 #endif
192 #if APR_HAS_SYSVSEM_SERIALIZE
193     mech = APR_LOCK_SYSVSEM;
194     abts_run_test(suite, proc_mutex, &mech);
195 #endif
196 #if APR_HAS_PROC_PTHREAD_SERIALIZE
197     mech = APR_LOCK_PROC_PTHREAD;
198     abts_run_test(suite, proc_mutex, &mech);
199 #endif
200 #if APR_HAS_FCNTL_SERIALIZE
201     mech = APR_LOCK_FCNTL;
202     abts_run_test(suite, proc_mutex, &mech);
203 #endif
204 #if APR_HAS_FLOCK_SERIALIZE
205     mech = APR_LOCK_FLOCK;
206     abts_run_test(suite, proc_mutex, &mech);
207 #endif
208 
209     return suite;
210 }
211