Commit d0bc503b authored by Geoff Simmons's avatar Geoff Simmons

Don't bother writing a terminating null to the mapped page.

Since we mmap length st_size + 1, and since mmap(2) fills the mapped
page past the length of the file with nulls, we ensure that there
is a terminating null after the file contents.

This means that the file can be opened with O_RDONLY and mmap'd
with PROT_READ. So the child process only needs read permissions
on the file.

Pointed out by Nils
parent 4ad8547a
......@@ -142,7 +142,7 @@ check(union sigval val)
rdr->flags &= ~RDR_MAPPED;
errno = 0;
if ((fd = open(info->path, O_RDWR)) < 0) {
if ((fd = open(info->path, O_RDONLY)) < 0) {
VERRMSG(rdr, "%s: cannot open %s: %s", rdr->vcl_name,
info->path, vstrerror(errno));
VSL(SLT_Error, 0, rdr->errbuf);
......@@ -150,9 +150,17 @@ check(union sigval val)
return;
}
/*
* By mapping the length st_size + 1, and due to the fact that
* mmap(2) fills the region of the mapped page past the length of
* the file with 0's, we ensure that there is a terminating null
* byte in the mapping after the file contents. So that the mapped
* address can be used as a VCL_STRING or a C string, without
* having to make copies.
*/
errno = 0;
if ((addr = mmap(NULL, st.st_size + 1, PROT_READ|PROT_WRITE,
MAP_PRIVATE, fd, 0)) == MAP_FAILED) {
if ((addr = mmap(NULL, st.st_size + 1, PROT_READ, MAP_PRIVATE, fd, 0))
== MAP_FAILED) {
VERRMSG(rdr, "%s: could not map %s: %s", rdr->vcl_name,
info->path, vstrerror(errno));
VSL(SLT_Error, 0, rdr->errbuf);
......@@ -164,12 +172,6 @@ check(union sigval val)
AN(addr);
rdr->flags |= RDR_MAPPED;
/*
* Add a terminating null byte, so that the mapped file can be
* used as a VCL_STRING or a C string.
*/
*((char *)(addr + st.st_size)) = '\0';
info->mtime.tv_sec = st.st_mtim.tv_sec;
info->mtime.tv_nsec = st.st_mtim.tv_nsec;
info->dev = st.st_dev;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment