Browse Source

[multiple] better packing of struct chunk

remove c->file.name
previous commit modified c->file.name to share buffer with c->mem
personal/stbuehler/fix-fdevent
Glenn Strauss 3 years ago
parent
commit
8c0792a58c
  1. 1
      src/chunk.c
  2. 21
      src/chunk.h
  3. 8
      src/mod_cgi.c
  4. 12
      src/mod_deflate.c
  5. 8
      src/mod_webdav.c
  6. 5
      src/network_write.c

1
src/chunk.c

@ -59,7 +59,6 @@ static chunk *chunk_init(void) {
c->type = MEM_CHUNK;
c->mem = buffer_init();
c->file.name = c->mem;
c->file.start = c->file.length = c->file.mmap.offset = 0;
c->file.fd = -1;
c->file.mmap.start = MAP_FAILED;

21
src/chunk.h

@ -10,33 +10,30 @@
#include "array.h"
typedef struct chunk {
struct chunk *next;
enum { MEM_CHUNK, FILE_CHUNK } type;
buffer *mem; /* either the storage of the mem-chunk or the read-ahead buffer */
buffer *mem; /* either the storage of the mem-chunk or the name of the file */
/* the size of the chunk is either:
* - mem-chunk: buffer_string_length(chunk::mem)
* - file-chunk: chunk::file.length
*/
off_t offset; /* octets sent from this chunk */
struct {
/* filechunk */
buffer *name; /* name of the file */
off_t start; /* starting offset in the file */
off_t length; /* octets to send from the starting offset */
int fd;
int is_temp; /* file is temporary and will be deleted if on cleanup */
struct {
char *start; /* the start pointer of the mmap'ed area */
size_t length; /* size of the mmap'ed area */
off_t offset; /* start is <n> octet away from the start of the file */
} mmap;
int is_temp; /* file is temporary and will be deleted if on cleanup */
} file;
/* the size of the chunk is either:
* - mem-chunk: buffer_string_length(chunk::mem)
* - file-chunk: chunk::file.length
*/
off_t offset; /* octets sent from this chunk */
struct chunk *next;
} chunk;
typedef struct {

8
src/mod_cgi.c

@ -524,8 +524,8 @@ static ssize_t cgi_write_file_chunk_mmap(server *srv, connection *con, int fd, c
/*(simplified from chunk.c:chunkqueue_open_file_chunk())*/
UNUSED(con);
if (-1 == c->file.fd) {
if (-1 == (c->file.fd = fdevent_open_cloexec(c->file.name->ptr, O_RDONLY, 0))) {
log_error_write(srv, __FILE__, __LINE__, "ssb", "open failed:", strerror(errno), c->file.name);
if (-1 == (c->file.fd = fdevent_open_cloexec(c->mem->ptr, O_RDONLY, 0))) {
log_error_write(srv, __FILE__, __LINE__, "ssb", "open failed:", strerror(errno), c->mem);
return -1;
}
}
@ -551,10 +551,10 @@ static ssize_t cgi_write_file_chunk_mmap(server *srv, connection *con, int fd, c
|| 0 >= (toSend = read(c->file.fd, data, toSend))) {
if (-1 == toSend) {
log_error_write(srv, __FILE__, __LINE__, "ssbdo", "lseek/read failed:",
strerror(errno), c->file.name, c->file.fd, offset);
strerror(errno), c->mem, c->file.fd, offset);
} else { /*(0 == toSend)*/
log_error_write(srv, __FILE__, __LINE__, "sbdo", "unexpected EOF (input truncated?):",
c->file.name, c->file.fd, offset);
c->mem, c->file.fd, offset);
}
free(data);
return -1;

12
src/mod_deflate.c

@ -732,8 +732,8 @@ static int mod_deflate_file_chunk(server *srv, connection *con, handler_ctx *hct
#endif
if (-1 == c->file.fd) { /* open the file if not already open */
if (-1 == (c->file.fd = fdevent_open_cloexec(c->file.name->ptr, O_RDONLY, 0))) {
log_error_write(srv, __FILE__, __LINE__, "sbs", "open failed for:", c->file.name, strerror(errno));
if (-1 == (c->file.fd = fdevent_open_cloexec(c->mem->ptr, O_RDONLY, 0))) {
log_error_write(srv, __FILE__, __LINE__, "sbs", "open failed for:", c->mem, strerror(errno));
return -1;
}
@ -794,7 +794,7 @@ static int mod_deflate_file_chunk(server *srv, connection *con, handler_ctx *hct
if (MAP_FAILED == (c->file.mmap.start = mmap(0, (size_t)to_mmap, PROT_READ, MAP_SHARED, c->file.fd, c->file.mmap.offset))
&& (errno != EINVAL || MAP_FAILED == (c->file.mmap.start = mmap(0, (size_t)to_mmap, PROT_READ, MAP_PRIVATE, c->file.fd, c->file.mmap.offset)))) {
log_error_write(srv, __FILE__, __LINE__, "ssbd", "mmap failed:",
strerror(errno), c->file.name, c->file.fd);
strerror(errno), c->mem, c->file.fd);
return -1;
}
@ -805,7 +805,7 @@ static int mod_deflate_file_chunk(server *srv, connection *con, handler_ctx *hct
if (c->file.mmap.length > (64 KByte) &&
0 != madvise(c->file.mmap.start, c->file.mmap.length, MADV_WILLNEED)) {
log_error_write(srv, __FILE__, __LINE__, "ssbd", "madvise failed:",
strerror(errno), c->file.name, c->file.fd);
strerror(errno), c->mem, c->file.fd);
}
#endif
@ -834,7 +834,7 @@ static int mod_deflate_file_chunk(server *srv, connection *con, handler_ctx *hct
toSend = st_size;
if (toSend > 2 MByte) toSend = 2 MByte;
if (NULL == (start = malloc((size_t)toSend)) || -1 == lseek(c->file.fd, abs_offset, SEEK_SET) || toSend != read(c->file.fd, start, (size_t)toSend)) {
log_error_write(srv, __FILE__, __LINE__, "sbss", "reading", c->file.name, "failed:", strerror(errno));
log_error_write(srv, __FILE__, __LINE__, "sbss", "reading", c->mem, "failed:", strerror(errno));
free(start);
return -1;
@ -850,7 +850,7 @@ static int mod_deflate_file_chunk(server *srv, connection *con, handler_ctx *hct
sigbus_jmp_valid = 0;
log_error_write(srv, __FILE__, __LINE__, "sbd", "SIGBUS in mmap:",
c->file.name, c->file.fd);
c->mem, c->file.fd);
return -1;
}
}

8
src/mod_webdav.c

@ -1132,7 +1132,7 @@ static int webdav_parse_chunkqueue(server *srv, connection *con, handler_ctx *hc
data = c->file.mmap.start + c->offset;
} else {
if (-1 == c->file.fd && /* open the file if not already open */
-1 == (c->file.fd = fdevent_open_cloexec(c->file.name->ptr, O_RDONLY, 0))) {
-1 == (c->file.fd = fdevent_open_cloexec(c->mem->ptr, O_RDONLY, 0))) {
log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
return -1;
@ -1151,7 +1151,7 @@ static int webdav_parse_chunkqueue(server *srv, connection *con, handler_ctx *hc
if (-1 == lseek(c->file.fd, c->file.start + c->offset, SEEK_SET)
|| 0 > (rd = read(c->file.fd, data, weHave))) {
log_error_write(srv, __FILE__, __LINE__, "ssbd", "lseek/read failed: ",
strerror(errno), c->file.name, c->file.fd);
strerror(errno), c->mem, c->file.fd);
free(data);
return -1;
}
@ -1847,7 +1847,7 @@ SUBREQUEST_FUNC(mod_webdav_subrequest_handler_huge) {
data = c->file.mmap.start + c->offset;
} else {
if (-1 == c->file.fd && /* open the file if not already open */
-1 == (c->file.fd = fdevent_open_cloexec(c->file.name->ptr, O_RDONLY, 0))) {
-1 == (c->file.fd = fdevent_open_cloexec(c->mem->ptr, O_RDONLY, 0))) {
log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
close(fd);
return HANDLER_ERROR;
@ -1866,7 +1866,7 @@ SUBREQUEST_FUNC(mod_webdav_subrequest_handler_huge) {
if (-1 == lseek(c->file.fd, c->file.start + c->offset, SEEK_SET)
|| 0 > (rd = read(c->file.fd, data, dlen))) {
log_error_write(srv, __FILE__, __LINE__, "ssbd", "lseek/read failed: ",
strerror(errno), c->file.name, c->file.fd);
strerror(errno), c->mem, c->file.fd);
free(data);
close(fd);
return HANDLER_ERROR;

5
src/network_write.c

@ -281,7 +281,8 @@ static int network_write_file_chunk_mmap(server *srv, int fd, chunkqueue *cq, of
MAP_SHARED, c->file.fd, c->file.mmap.offset);
if (MAP_FAILED == c->file.mmap.start) {
log_error_write(srv, __FILE__, __LINE__, "ssbdoo", "mmap failed:",
strerror(errno), c->file.name, c->file.fd, c->file.mmap.offset, (off_t) c->file.mmap.length);
strerror(errno), c->mem, c->file.fd,
c->file.mmap.offset, (off_t) c->file.mmap.length);
return -1;
}
@ -316,7 +317,7 @@ static int network_write_file_chunk_mmap(server *srv, int fd, chunkqueue *cq, of
sigbus_jmp_valid = 0;
log_error_write(srv, __FILE__, __LINE__, "sbd", "SIGBUS in mmap:",
c->file.name, c->file.fd);
c->mem, c->file.fd);
munmap(c->file.mmap.start, c->file.mmap.length);
c->file.mmap.start = MAP_FAILED;

Loading…
Cancel
Save