1 /* map_stupidshared.c -- memory-mapping routines working around DEC stupidity.
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 EXPORTED const char map_method_desc[] = "stupidshared";
55
56 #ifndef MAP_FAILED
57 #define MAP_FAILED ((void *)-1)
58 #endif
59
60 /*
61 * Create/refresh mapping of file
62 */
63 void
map_refresh(int fd,int onceonly,const char ** base,size_t * len,size_t newlen,const char * name,const char * mboxname)64 EXPORTED map_refresh(int fd, int onceonly, const char **base,
65 size_t *len, size_t newlen, const char *name,
66 const char *mboxname)
67 {
68 (void)onceonly;
69 struct stat sbuf;
70 int flags;
71 char buf[256];
72
73 if (newlen == MAP_UNKNOWN_LEN) {
74 if (fstat(fd, &sbuf) == -1) {
75 syslog(LOG_ERR, "IOERROR: fstating %s file%s%s: %m", name,
76 mboxname ? " for " : "", mboxname ? mboxname : "");
77 snprintf(buf, sizeof(buf), "failed to fstat %s file", name);
78 fatal(buf, EX_IOERR);
79 }
80 newlen = sbuf.st_size;
81 }
82
83 /* Already mapped in */
84 if (*len >= newlen) return;
85
86 if (*len) munmap((char *)*base, *len);
87
88 flags = MAP_SHARED;
89 #ifdef MAP_FILE
90 flags |= MAP_FILE;
91 #endif
92 #ifdef MAP_VARIABLE
93 flags |= MAP_VARIABLE;
94 #endif
95
96 *base = (char *)mmap((caddr_t)0, newlen, PROT_READ, flags, fd, 0L);
97 if (*base == (char *)MAP_FAILED) {
98 syslog(LOG_ERR, "IOERROR: mapping %s file%s%s: %m", name,
99 mboxname ? " for " : "", mboxname ? mboxname : "");
100 snprintf(buf, sizeof(buf), "failed to mmap %s file", name);
101 fatal(buf, EX_IOERR);
102 }
103 *len = newlen;
104 }
105
106 /*
107 * Destroy mapping of file
108 */
109 void
map_free(const char ** base,size_t * len)110 EXPORTED map_free(const char **base, size_t *len)
111 {
112 if (*len) munmap((char *)*base, *len);
113 *base = 0;
114 *len = 0;
115 }
116