1 /* map_shared.c -- memory-mapping routines.
2 *
3 * Copyright (c) 1994-2008 Carnegie Mellon University. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
15 * distribution.
16 *
17 * 3. The name "Carnegie Mellon University" must not be used to
18 * endorse or promote products derived from this software without
19 * prior written permission. For permission or any legal
20 * details, please contact
21 * Carnegie Mellon University
22 * Center for Technology Transfer and Enterprise Creation
23 * 4615 Forbes Avenue
24 * Suite 302
25 * Pittsburgh, PA 15213
26 * (412) 268-7393, fax: (412) 268-7395
27 * innovation@andrew.cmu.edu
28 *
29 * 4. Redistributions of any form whatsoever must retain the following
30 * acknowledgment:
31 * "This product includes software developed by Computing Services
32 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
33 *
34 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
41 */
42
43 #include <config.h>
44 #include <stdio.h>
45 #include <sys/types.h>
46 #include <sys/mman.h>
47 #include <sys/stat.h>
48 #include <sysexits.h>
49 #include <syslog.h>
50
51 #include "map.h"
52 #include "xmalloc.h"
53
54 #define SLOP (8*1024)
55
56 EXPORTED const char map_method_desc[] = "shared";
57
58 /*
59 * Create/refresh mapping of file
60 */
map_refresh(int fd,int onceonly,const char ** base,size_t * len,size_t newlen,const char * name,const char * mboxname)61 EXPORTED void map_refresh(int fd, int onceonly, const char **base,
62 size_t *len, size_t newlen,
63 const char *name, const char *mboxname)
64 {
65 struct stat sbuf;
66 char buf[256];
67
68 if (newlen == MAP_UNKNOWN_LEN) {
69 if (fstat(fd, &sbuf) == -1) {
70 syslog(LOG_ERR, "IOERROR: fstating %s file%s%s: %m", name,
71 mboxname ? " for " : "", mboxname ? mboxname : "");
72 snprintf(buf, sizeof(buf), "failed to fstat %s file", name);
73 fatal(buf, EX_IOERR);
74 }
75 newlen = sbuf.st_size;
76 }
77
78 /* Already mapped in */
79 if (*len >= newlen) return;
80
81 if (*len) munmap((char *)*base, *len);
82
83 if (!onceonly) {
84 newlen = (newlen + 2*SLOP - 1) & ~(SLOP-1);
85 }
86
87 *base = (char *)mmap((caddr_t)0, newlen, PROT_READ, MAP_SHARED
88 #ifdef MAP_FILE
89 | MAP_FILE
90 #endif
91 #ifdef MAP_VARIABLE
92 | MAP_VARIABLE
93 #endif
94 , fd, 0L);
95 if (*base == (char *)-1) {
96 syslog(LOG_ERR, "IOERROR: mapping %s file%s%s: %m", name,
97 mboxname ? " for " : "", mboxname ? mboxname : "");
98 snprintf(buf, sizeof(buf), "failed to mmap %s file", name);
99 fatal(buf, EX_IOERR);
100 }
101 *len = newlen;
102 }
103
104 /*
105 * Destroy mapping of file
106 */
map_free(const char ** base,size_t * len)107 EXPORTED void map_free(const char **base, size_t *len)
108 {
109 if (*len) munmap((char *)*base, *len);
110 *base = 0;
111 *len = 0;
112 }
113