Improve extfs incoming message reliability.

This commit is contained in:
Jonas 'Sortie' Termansen 2015-09-27 22:51:42 +02:00
parent ec990882b0
commit 01afa43fb0
1 changed files with 16 additions and 14 deletions

View File

@ -22,6 +22,7 @@
#if defined(__sortix__) #if defined(__sortix__)
#include <sys/mman.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/types.h> #include <sys/types.h>
@ -680,23 +681,24 @@ void HandleIncomingMessage(int chl, struct fsm_msg_header* hdr, Filesystem* fs)
RespondError(chl, ENOTSUP); RespondError(chl, ENOTSUP);
return; return;
} }
uint8_t* body = (uint8_t*) malloc(hdr->msgsize); uint8_t body_buffer[65536];
if ( !body ) uint8_t* body = body_buffer;
if ( sizeof(body_buffer) < hdr->msgsize )
{ {
fprintf(stderr, "extfs: message of type %zu too large: %zu bytes\n", hdr->msgtype, hdr->msgsize); body = (uint8_t*) mmap(NULL, hdr->msgsize, PROT_READ | PROT_WRITE,
RespondError(chl, errno); MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
return; if ( (void*) body == MAP_FAILED )
{
RespondError(chl, errno);
return;
}
} }
size_t amount = readall(chl, body, hdr->msgsize); if ( readall(chl, body, hdr->msgsize) == hdr->msgsize )
if ( amount < hdr->msgsize ) handlers[hdr->msgtype](chl, body, fs);
{ else
fprintf(stderr, "extfs: incomplete message of type %zu: got %zi of %zu bytes\n", hdr->msgtype, amount, hdr->msgsize);
RespondError(chl, errno); RespondError(chl, errno);
free(body); if ( sizeof(body_buffer) < hdr->msgsize )
return; munmap(body, hdr->msgsize);
}
handlers[hdr->msgtype](chl, body, fs);
free(body);
} }
static volatile bool should_terminate = false; static volatile bool should_terminate = false;