1 /* map_nommap.c -- dummy 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 <unistd.h>
47 #include <sys/stat.h>
48 #include <sysexits.h>
49 #include <syslog.h>
50
51 #include "xmalloc.h"
52 #include "map.h"
53
54 #define SLOP (4*1024)
55
56 EXPORTED const char map_method_desc[] = "nommap";
57
58 /*
59 * Create/refresh mapping of file
60 */
61 void
map_refresh(int fd,int onceonly,const char ** base,size_t * len,size_t newlen,const char * name,const char * mboxname)62 EXPORTED map_refresh(int fd, int onceonly, const char **base,
63 size_t *len, size_t newlen, const char *name,
64 const char *mboxname)
65 {
66 char *p;
67 int n, left;
68 struct stat sbuf;
69 char buf[80];
70
71 if (newlen == MAP_UNKNOWN_LEN) {
72 if (fstat(fd, &sbuf) == -1) {
73 syslog(LOG_ERR, "IOERROR: fstating %s file%s%s: %m", name,
74 mboxname ? " for " : "", mboxname ? mboxname : "");
75 snprintf(buf, sizeof(buf), "failed to fstat %s file", name);
76 fatal(buf, EX_IOERR);
77 }
78 newlen = sbuf.st_size;
79 }
80
81 /* Need a larger buffer */
82 if (*len < newlen) {
83 if (*len) free((char *)*base);
84 *len = newlen + (onceonly ? 0 : SLOP);
85 *base = xmalloc(*len);
86 }
87
88 lseek(fd, 0L, 0);
89 left = newlen;
90 p = (char*) *base;
91
92 while (left) {
93 n = read(fd, p, left);
94 if (n <= 0) {
95 if (n == 0) {
96 syslog(LOG_ERR, "IOERROR: reading %s file%s%s: end of file",
97 name,
98 mboxname ? " for " : "", mboxname ? mboxname : "");
99 }
100 else {
101 syslog(LOG_ERR, "IOERROR: reading %s file%s%s: %m",
102 name,
103 mboxname ? " for " : "", mboxname ? mboxname : "");
104 }
105 snprintf(buf, sizeof(buf), "failed to read %s file", name);
106 fatal(buf, EX_IOERR);
107 }
108 p += n;
109 left -= n;
110 }
111 }
112
113 /*
114 * Destroy mapping of file
115 */
116 void
map_free(const char ** base,size_t * len)117 EXPORTED map_free(const char **base, size_t *len)
118 {
119 if (*len) free((char *)*base);
120 *base = 0;
121 *len = 0;
122 }
123