xref: /aosp_15_r20/external/cronet/third_party/apache-portable-runtime/src/test/testshmproducer.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_errno.h"
19 #include "apr_general.h"
20 #include "apr_lib.h"
21 #include "apr_strings.h"
22 #include "apr_time.h"
23 #include "testshm.h"
24 #include "apr.h"
25 
26 #if APR_HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 
30 
31 #if APR_HAS_SHARED_MEMORY
msgput(int boxnum,char * msg)32 static void msgput(int boxnum, char *msg)
33 {
34     apr_cpystrn(boxes[boxnum].msg, msg, strlen(msg) + 1);
35     boxes[boxnum].msgavail = 1;
36 }
37 
main(void)38 int main(void)
39 {
40     apr_status_t rv;
41     apr_pool_t *pool;
42     apr_shm_t *shm;
43     int i;
44     int sent = 0;
45 
46     apr_initialize();
47 
48     if (apr_pool_create(&pool, NULL) != APR_SUCCESS) {
49         exit(-1);
50     }
51 
52     rv = apr_shm_attach(&shm, SHARED_FILENAME, pool);
53     if (rv != APR_SUCCESS) {
54         exit(-2);
55     }
56 
57     boxes = apr_shm_baseaddr_get(shm);
58 
59     /* produce messages on all of the boxes, in descending order,
60      * Yes, we could just return N_BOXES, but I want to have a double-check
61      * in this code.  The original code actually sent N_BOXES - 1 messages,
62      * so rather than rely on possibly buggy code, this way we know that we
63      * are returning the right number.
64      */
65     for (i = N_BOXES - 1, sent = 0; i >= 0; i--, sent++) {
66         msgput(i, MSG);
67         apr_sleep(apr_time_from_sec(1));
68     }
69 
70     rv = apr_shm_detach(shm);
71     if (rv != APR_SUCCESS) {
72         exit(-3);
73     }
74 
75     return sent;
76 }
77 
78 #else /* APR_HAS_SHARED_MEMORY */
79 
main(void)80 int main(void)
81 {
82     /* Just return, this program will never be launched, so there is no
83      * reason to print a message.
84      */
85     return 0;
86 }
87 
88 #endif /* APR_HAS_SHARED_MEMORY */
89 
90