Coverage Report

Created: 2023-03-26 06:28

/src/httpd/srclib/apr/mmap/unix/mmap.c
Line
Count
Source (jump to first uncovered line)
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 <assert.h>
18
19
#include "apr.h"
20
#include "apr_private.h"
21
#include "apr_general.h"
22
#include "apr_strings.h"
23
#include "apr_mmap.h"
24
#include "apr_errno.h"
25
#include "apr_arch_file_io.h"
26
#include "apr_portable.h"
27
28
/* System headers required for the mmap library */
29
#ifdef BEOS
30
#include <kernel/OS.h>
31
#endif
32
#if APR_HAVE_STRING_H
33
#include <string.h>
34
#endif
35
#if APR_HAVE_STDIO_H
36
#include <stdio.h>
37
#endif
38
#if APR_HAVE_UNISTD_H
39
#include <unistd.h>  /* for sysconf() */
40
#endif
41
#ifdef HAVE_SYS_STAT_H
42
#include <sys/stat.h>
43
#endif
44
#ifdef HAVE_SYS_MMAN_H
45
#include <sys/mman.h>
46
#endif
47
48
#if APR_HAS_MMAP || defined(BEOS)
49
50
static apr_status_t mmap_cleanup(void *themmap)
51
0
{
52
0
    apr_mmap_t *mm = themmap;
53
0
    apr_mmap_t *next = APR_RING_NEXT(mm,link);
54
0
    int rv = 0;
55
56
    /* we no longer refer to the mmaped region */
57
0
    APR_RING_REMOVE(mm,link);
58
0
    APR_RING_NEXT(mm,link) = NULL;
59
0
    APR_RING_PREV(mm,link) = NULL;
60
61
0
    if (next != mm) {
62
        /* more references exist, so we're done */
63
0
        return APR_SUCCESS;
64
0
    }
65
66
#ifdef BEOS
67
    rv = delete_area(mm->area);
68
#else
69
0
    rv = munmap((char *)mm->mm - mm->poffset, mm->size + mm->poffset);
70
0
#endif
71
0
    mm->mm = (void *)-1;
72
73
0
    if (rv == 0) {
74
0
        return APR_SUCCESS;
75
0
    }
76
0
    return errno;
77
0
}
78
79
APR_DECLARE(apr_status_t) apr_mmap_create(apr_mmap_t **new,
80
                                          apr_file_t *file, apr_off_t offset,
81
                                          apr_size_t size, apr_int32_t flag,
82
                                          apr_pool_t *cont)
83
0
{
84
0
    void *mm;
85
#ifdef BEOS
86
    area_id aid = -1;
87
    uint32 pages = 0;
88
#else
89
0
    static long psize;
90
0
    apr_off_t poffset = 0;
91
0
    apr_int32_t native_flags = 0;
92
0
#endif
93
94
#if APR_HAS_LARGE_FILES && defined(HAVE_MMAP64)
95
#define mmap mmap64
96
#elif APR_HAS_LARGE_FILES && SIZEOF_OFF_T == 4
97
    /* LFS but no mmap64: check for overflow */
98
    if ((apr_int64_t)offset + size > INT_MAX)
99
        return APR_EINVAL;
100
#endif
101
102
0
    if (size == 0)
103
0
        return APR_EINVAL;
104
105
0
    if (file == NULL || file->filedes == -1 || file->buffered)
106
0
        return APR_EBADF;
107
0
    (*new) = (apr_mmap_t *)apr_pcalloc(cont, sizeof(apr_mmap_t));
108
109
#ifdef BEOS
110
    /* XXX: mmap shouldn't really change the seek offset */
111
    apr_file_seek(file, APR_SET, &offset);
112
113
    /* There seems to be some strange interactions that mean our area must
114
     * be set as READ & WRITE or writev will fail!  Go figure...
115
     * So we ignore the value in flags and always ask for both READ and WRITE
116
     */
117
    pages = (size + B_PAGE_SIZE -1) / B_PAGE_SIZE;
118
    aid = create_area("apr_mmap", &mm , B_ANY_ADDRESS, pages * B_PAGE_SIZE,
119
        B_NO_LOCK, B_WRITE_AREA|B_READ_AREA);
120
121
    if (aid < B_NO_ERROR) {
122
        /* we failed to get an area we can use... */
123
        *new = NULL;
124
        return APR_ENOMEM;
125
    }
126
127
    if (aid >= B_NO_ERROR)
128
        read(file->filedes, mm, size);
129
130
    (*new)->area = aid;
131
#else
132
133
0
    if (flag & APR_MMAP_WRITE) {
134
0
        native_flags |= PROT_WRITE;
135
0
    }
136
0
    if (flag & APR_MMAP_READ) {
137
0
        native_flags |= PROT_READ;
138
0
    }
139
140
0
#if defined(_SC_PAGESIZE)
141
0
    if (psize == 0) {
142
0
        psize = sysconf(_SC_PAGESIZE);
143
        /* the page size should be a power of two */
144
0
        assert(psize > 0 && (psize & (psize - 1)) == 0);
145
0
    }
146
0
    poffset = offset & (apr_off_t)(psize - 1);
147
0
    (*new)->poffset = poffset;
148
0
#endif
149
150
0
    mm = mmap(NULL, size + poffset,
151
0
              native_flags, MAP_SHARED,
152
0
              file->filedes, offset - poffset);
153
154
0
    if (mm == (void *)-1) {
155
        /* we failed to get an mmap'd file... */
156
0
        *new = NULL;
157
0
        return errno;
158
0
    }
159
160
0
    mm = (char *)mm + poffset;
161
0
#endif
162
163
0
    (*new)->mm = mm;
164
0
    (*new)->size = size;
165
0
    (*new)->cntxt = cont;
166
0
    APR_RING_ELEM_INIT(*new, link);
167
168
    /* register the cleanup... */
169
0
    apr_pool_cleanup_register((*new)->cntxt, (void*)(*new), mmap_cleanup,
170
0
             apr_pool_cleanup_null);
171
0
    return APR_SUCCESS;
172
0
}
173
174
APR_DECLARE(apr_status_t) apr_mmap_dup(apr_mmap_t **new_mmap,
175
                                       apr_mmap_t *old_mmap,
176
                                       apr_pool_t *p)
177
0
{
178
0
    *new_mmap = (apr_mmap_t *)apr_pmemdup(p, old_mmap, sizeof(apr_mmap_t));
179
0
    (*new_mmap)->cntxt = p;
180
181
0
    APR_RING_INSERT_AFTER(old_mmap, *new_mmap, link);
182
183
0
    apr_pool_cleanup_register(p, *new_mmap, mmap_cleanup,
184
0
                              apr_pool_cleanup_null);
185
0
    return APR_SUCCESS;
186
0
}
187
188
APR_DECLARE(apr_status_t) apr_mmap_delete(apr_mmap_t *mm)
189
0
{
190
0
    return apr_pool_cleanup_run(mm->cntxt, mm, mmap_cleanup);
191
0
}
192
193
#endif