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 "errno.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include "testutil.h"
28 
29 #if APR_HAS_FORK
30 
31 #define MAX_ITER 200
32 #define CHILDREN 6
33 #define MAX_COUNTER (MAX_ITER * CHILDREN)
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,apr_proc_t ** proc,apr_pool_t * p)45 static void make_child(abts_case *tc, 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 (apr_proc_mutex_lock(proc_lock))
72                 exit(1);
73             i++;
74             *x = increment(*x);
75             if (apr_proc_mutex_unlock(proc_lock))
76                 exit(1);
77         } while (i < MAX_ITER);
78         exit(0);
79     }
80 
81     ABTS_ASSERT(tc, "fork failed", rv == APR_INPARENT);
82 }
83 
84 /* Wait for a child process and check it terminated with success. */
await_child(abts_case * tc,apr_proc_t * proc)85 static void await_child(abts_case *tc, apr_proc_t *proc)
86 {
87     int code;
88     apr_exit_why_e why;
89     apr_status_t rv;
90 
91     rv = apr_proc_wait(proc, &code, &why, APR_WAIT);
92     ABTS_ASSERT(tc, "child did not terminate with success",
93              rv == APR_CHILD_DONE && why == APR_PROC_EXIT && code == 0);
94 }
95 
test_exclusive(abts_case * tc,const char * lockname,apr_lockmech_e mech)96 static void test_exclusive(abts_case *tc, const char *lockname,
97                            apr_lockmech_e mech)
98 {
99     apr_proc_t *child[CHILDREN];
100     apr_status_t rv;
101     int n;
102 
103     rv = apr_proc_mutex_create(&proc_lock, lockname, mech, p);
104     APR_ASSERT_SUCCESS(tc, "create the mutex", rv);
105     if (rv != APR_SUCCESS)
106         return;
107 
108     for (n = 0; n < CHILDREN; n++)
109         make_child(tc, &child[n], p);
110 
111     for (n = 0; n < CHILDREN; n++)
112         await_child(tc, child[n]);
113 
114     ABTS_ASSERT(tc, "Locks don't appear to work", *x == MAX_COUNTER);
115 }
116 #endif
117 
proc_mutex(abts_case * tc,void * data)118 static void proc_mutex(abts_case *tc, void *data)
119 {
120 #if APR_HAS_FORK
121     apr_status_t rv;
122     const char *shmname = "tpm.shm";
123     apr_shm_t *shm;
124     apr_lockmech_e *mech = data;
125 
126     /* Use anonymous shm if available. */
127     rv = apr_shm_create(&shm, sizeof(int), NULL, p);
128     if (rv == APR_ENOTIMPL) {
129         apr_file_remove(shmname, p);
130         rv = apr_shm_create(&shm, sizeof(int), shmname, p);
131     }
132 
133     APR_ASSERT_SUCCESS(tc, "create shm segment", rv);
134     if (rv != APR_SUCCESS)
135         return;
136 
137     x = apr_shm_baseaddr_get(shm);
138     test_exclusive(tc, NULL, *mech);
139     rv = apr_shm_destroy(shm);
140     APR_ASSERT_SUCCESS(tc, "Error destroying shared memory block", rv);
141 #else
142     ABTS_NOT_IMPL(tc, "APR lacks fork() support");
143 #endif
144 }
145 
146 
testprocmutex(abts_suite * suite)147 abts_suite *testprocmutex(abts_suite *suite)
148 {
149     apr_lockmech_e mech = APR_LOCK_DEFAULT;
150 
151     suite = ADD_SUITE(suite)
152     abts_run_test(suite, proc_mutex, &mech);
153 #if APR_HAS_POSIXSEM_SERIALIZE
154     mech = APR_LOCK_POSIXSEM;
155     abts_run_test(suite, proc_mutex, &mech);
156 #endif
157 #if APR_HAS_SYSVSEM_SERIALIZE
158     mech = APR_LOCK_SYSVSEM;
159     abts_run_test(suite, proc_mutex, &mech);
160 #endif
161 #if APR_HAS_PROC_PTHREAD_SERIALIZE
162     mech = APR_LOCK_PROC_PTHREAD;
163     abts_run_test(suite, proc_mutex, &mech);
164 #endif
165 #if APR_HAS_FCNTL_SERIALIZE
166     mech = APR_LOCK_FCNTL;
167     abts_run_test(suite, proc_mutex, &mech);
168 #endif
169 #if APR_HAS_FLOCK_SERIALIZE
170     mech = APR_LOCK_FLOCK;
171     abts_run_test(suite, proc_mutex, &mech);
172 #endif
173 
174     return suite;
175 }
176