close fds on error and add CLOEXEC to FDs which stay open for a longer time

git-svn-id: svn://svn.lighttpd.net/lighttpd/branches/lighttpd-merge-1.4.x@794 152afb58-edef-0310-8abb-c4023f1b3aa9
This commit is contained in:
Jan Kneschke 2005-10-22 08:10:07 +00:00
parent 396a4de69b
commit ff29203dba
5 changed files with 17 additions and 5 deletions

View File

@ -439,6 +439,9 @@ static int deflate_file_to_file(server *srv, connection *con, plugin_data *p, bu
}
if (-1 == (r = write(ofd, p->b->ptr, p->b->used))) {
munmap(start, sce->st.st_size);
close(ofd);
close(ifd);
return -1;
}

View File

@ -146,11 +146,15 @@ int network_write_chunkqueue_linuxsendfile(server *srv, connection *con, int fd,
}
/* open file if not already opened */
if (-1 == c->file.fd &&
-1 == (c->file.fd = open(c->file.name->ptr, O_RDONLY))) {
log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
if (-1 == c->file.fd) {
if (-1 == (c->file.fd = open(c->file.name->ptr, O_RDONLY))) {
log_error_write(srv, __FILE__, __LINE__, "ss", "open failed: ", strerror(errno));
return -1;
return -1;
}
#ifdef FD_CLOEXEC
fcntl(c->file.fd, F_SETFD, FD_CLOEXEC);
#endif
}
/* Linux sendfile() */

View File

@ -171,6 +171,7 @@ int network_write_chunkqueue_openssl(server *srv, connection *con, SSL *ssl, chu
lseek(ifd, offset, SEEK_SET);
if (-1 == (toSend = read(ifd, local_send_buffer, toSend))) {
close(ifd);
log_error_write(srv, __FILE__, __LINE__, "ss", "read failed: ", strerror(errno));
return -1;
}

View File

@ -110,7 +110,7 @@ int network_write_chunkqueue_write(server *srv, connection *con, int fd, chunkqu
if ((r = write(fd, p + offset, toSend)) <= 0) {
log_error_write(srv, __FILE__, __LINE__, "ss", "write failed: ", strerror(errno));
munmap(p, sce->st.st_size);
return -1;
}

View File

@ -196,7 +196,11 @@ int network_write_chunkqueue_writev(server *srv, connection *con, int fd, chunkq
* 3. use non-blocking IO for file-transfers
* */
if (MAP_FAILED == (c->file.mmap.start = mmap(0, sce->st.st_size, PROT_READ, MAP_SHARED, c->file.fd, 0))) {
/* close it here, otherwise we'd have to set FD_CLOEXEC */
close(c->file.fd);
c->file.fd = -1;
log_error_write(srv, __FILE__, __LINE__, "ssbd", "mmap failed: ",
strerror(errno), c->file.name, c->file.fd);