X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=ftpd.c;h=7f73e8951ca177e08e15bbd060c05c9e2b1ae16a;hb=f0c7394d9bdae7e980847003f2da6dcc3d2ad96c;hp=4d65210591c26c260d2c864dd5913c14c7dfdf0e;hpb=4ad0559c71f4ef2f498a8b236a53eda9bcdd5c2c;p=betaftpd diff --git a/ftpd.c b/ftpd.c index 4d65210..7f73e89 100644 --- a/ftpd.c +++ b/ftpd.c @@ -28,10 +28,6 @@ #include #endif -#if HAVE_SYS_TYPES_H -#include -#endif - #if HAVE_ERRNO_H #include #endif @@ -76,10 +72,6 @@ #include #endif -#if HAVE_NETINET_IN_H -#include -#endif - #if HAVE_ARPA_INET_H #include #endif @@ -88,10 +80,6 @@ #include #endif -#if HAVE_SYS_SOCKET_H -#include -#endif - #if HAVE_SYS_IOCTL_H #include #endif @@ -112,6 +100,10 @@ #include #endif +#if HAVE_LINUX_TCP_H +#include +#endif + #if HAVE_MMAP #include #endif @@ -171,6 +163,10 @@ #include #endif +#if WANT_DCACHE +#include +#endif + #ifndef MAP_FAILED #define MAP_FAILED -1 #endif @@ -198,6 +194,10 @@ fd_set master_fds, master_send_fds; FILE *xferlog = NULL; #endif +#if HAVE_LINUX_SENDFILE +int sendfile_supported = 1; +#endif + /* * This variable specifies if it's soon time to check for timed out * clients, and timed out directory listing cache entries. It is @@ -439,30 +439,6 @@ struct ftran *alloc_new_ftran(const int sock, const struct conn * const c) return f; } -#if WANT_DCACHE -/* - * alloc_new_dcache(): - * Allocates a new directory cache entry (type `struct dcache'), - * and adds it to the linked list. - */ -struct dcache *alloc_new_dcache() -{ - struct dcache *d = (struct dcache *)(malloc(sizeof(struct dcache))); - - if (d == NULL) return d; - - d->use_count = 0; - d->last_used = 0; - strcpy(d->dir_name, ""); - d->dir_data = NULL; - - add_to_linked_list((struct list_element *)first_dcache, - (struct list_element *)d); - - return d; -} -#endif - /* * destroy_conn(): * Destroy a control connection, remove it from the linked @@ -534,30 +510,9 @@ void destroy_ftran(struct ftran * const f) remove_from_linked_list((struct list_element *)f); } -#if WANT_DCACHE -/* - * destroy_dcache(): - * Destroy a directory listing cache entry, remove it from the - * linked list, and clean up after it. - * - * If you free a cache entry that is in use (use_count > 0), - * BetaFTPD will most likely crash (later). The thing you're supposed - * to do when you're done with a dcache entry, is to decrement - * its use_count, and let the timeout functions do the destroying - * when it's time to do so. - */ -void destroy_dcache(struct dcache * const d) -{ - if (d == NULL) return; - - if (d->dir_data != NULL) free(d->dir_data); - remove_from_linked_list((struct list_element *)d); -} -#endif - /* * process_all_clients(): - * Processes all the control connections in active_clients + * Processes all the _control_ connections in active_clients * (normally returned from a select(), there are at max * NUM_AC active connections in the set), sending them * through to the command parser if a command has been @@ -579,11 +534,7 @@ int process_all_clients(const fd_set * const active_clients, const int num_ac) c = next; next = c->next_conn; #if HAVE_POLL - if (fds[c->sock].revents & (POLLERR|POLLHUP|POLLNVAL)) { - destroy_conn(c); - continue; - } - if (!fds[c->sock].revents & POLLIN) { + if ((fds[c->sock].revents & (POLLIN|POLLERR|POLLHUP|POLLNVAL)) == 0) { continue; } #else @@ -603,8 +554,10 @@ int process_all_clients(const fd_set * const active_clients, const int num_ac) * client has closed the socket. If we get a return value * of -1 (error), we close the socket ourselves. * - * Just to be safe, we include this code for poll() as - * well. + * We do the same for poll(), even though we actually have + * bits that tell us what is happening (in case of new + * input AND error/hangup at the same time, we do an + * explicit check at the bottom of the loop as well). */ destroy_conn(c); continue; @@ -619,6 +572,10 @@ int process_all_clients(const fd_set * const active_clients, const int num_ac) c->buf_len += bytes_avail; parse_command(c); + + if (fds[c->sock].revents & (POLLERR|POLLHUP|POLLNVAL)) { + destroy_conn(c); + } } return checked_through; } @@ -788,11 +745,7 @@ int do_download(struct ftran *f) * Here we use a rather simplified sending `algorithm', * leaving most of the quirks to the system calls. */ - if (f->dir_listing == 0 -#if WANT_UPLOAD - && f->upload == 0 -#endif - ) { + if (sendfile_supported == 1 && f->dir_listing == 0) { int err; size = f->size - f->pos; @@ -999,6 +952,19 @@ int main(void) alarm(60); signal(SIGALRM, handle_alarm); +#if HAVE_LINUX_SENDFILE + /* check that sendfile() is really implemented (same check as configure does) */ + { + int out_fd = 1, in_fd = 0; + off_t offset = 0; + size_t size = 1024; + + errno = 0; + sendfile(out_fd, in_fd, &offset, size); + if (errno == ENOSYS) sendfile_supported = 0; + } +#endif + for ( ;; ) { int i; #ifndef HAVE_POLL @@ -1176,29 +1142,6 @@ void time_out_sockets() } } -#if WANT_DCACHE -/* - * time_out_dcache(): - * Time out expired directory listing cache entries. - * Uses much of the same code as time_out_sockets(). - */ -void time_out_dcache() -{ - struct dcache *d = NULL, *next = first_dcache->next_dcache; - time_t now = time(NULL); - - /* run through the linked list */ - while (next != NULL) { - d = next; - next = d->next_dcache; - - if (d->use_count == 0 && (now - d->last_used > 900)) { - destroy_dcache(d); - } - } -} -#endif - /* * remove_bytes(): * Remove some bytes from the incoming buffer. This gives @@ -1375,7 +1318,7 @@ void init_file_transfer(struct ftran * const f) #if HAVE_MMAP if (f->dir_listing == 0) { #if HAVE_LINUX_SENDFILE - int do_mmap = 0; + int do_mmap = (sendfile_supported) ? 0 : 1; #else int do_mmap = 1; #endif