1 /* Low-level file access */
5 * hed - Hexadecimal editor
6 * Copyright (C) 2004 Petr Baudis <pasky@ucw.cz>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 /* The idea here is that an mmapped region may get beyond EOF, e.g. if
23 * the file gets truncated by another process. When accessing such
24 * areas, the kernel generates a SIGBUS. Our signal handler replaces
25 * the faulty page with a new mapping. The page is still tracked by
26 * the cache, so it will be freed when the cache object is freed.
42 unsigned int sys_page_shift
;
43 unsigned long sys_page_size
;
47 #ifdef HED_CONFIG_MMAP
49 LIST_HEAD(fixup_files
);
51 static struct hed_block_data
*
52 find_bdata(void *addr
)
54 struct hed_file
*file
;
56 list_for_each_entry(file
, &fixup_files
, fixup_list
) {
57 struct hed_cache
*cache
= file
->cache
;
59 for (i
= 0; i
< cache
->nelem
; ++i
)
60 if (cache
->revmap
[i
]->data
== addr
)
61 return cache
->revmap
[i
];
67 sigbus_handler(int sig
, struct siginfo
*si
, void *ctx
)
69 void *page
= (void*)(intptr_t)FILE_BLOCK_ROUND((intptr_t)si
->si_addr
);
70 struct hed_block_data
*bdata
= find_bdata(page
);
73 bdata
->data
= mmap(page
, sys_page_size
,
74 PROT_READ
| PROT_WRITE
,
75 MAP_PRIVATE
| MAP_ANONYMOUS
| MAP_FIXED
,
78 /* Restart the instruction and fail */
79 signal(SIGBUS
, SIG_DFL
);
83 #endif /* HED_CONFIG_MMAP */
85 #if defined(HED_CONFIG_MMAP) || defined(HED_CONFIG_SWAP_MMAP)
88 file_access_init(void)
90 sys_page_size
= sysconf(_SC_PAGESIZE
);
91 sys_page_shift
= bitsize(sys_page_size
);
92 /* FILE_BLOCK_* macros only work with page sizes which
93 * are a power of two. Abort if this is not the case. */
94 assert(FILE_BLOCK_SIZE
== FILE_BLOCK_ROUND(FILE_BLOCK_SIZE
));
95 assert(SWAP_BLOCK_SIZE
>= FILE_BLOCK_SIZE
);
97 #ifdef HED_CONFIG_MMAP
100 sa
.sa_flags
= SA_SIGINFO
;
101 sa
.sa_sigaction
= sigbus_handler
;
102 sigemptyset(&sa
.sa_mask
);
104 return sigaction(SIGBUS
, &sa
, NULL
);