From 48c747e0c8f1a09ddadaca06f4c95c7c652b08ea Mon Sep 17 00:00:00 2001 From: "bernard.xiong" Date: Tue, 3 Nov 2009 23:59:30 +0000 Subject: [PATCH] fix select issue in ftpd and lwip_select. cleanup assert information output in lwip. git-svn-id: https://rt-thread.googlecode.com/svn/trunk@148 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- net/apps/ftpd.c | 50 ++++++++++++++--------- net/lwip/src/api/sockets.c | 79 ++++++++++++++++++------------------ net/lwip/src/arch/sys_arch.c | 23 ++++++----- 3 files changed, 82 insertions(+), 70 deletions(-) diff --git a/net/apps/ftpd.c b/net/apps/ftpd.c index 5c64160396..e0c6597c31 100644 --- a/net/apps/ftpd.c +++ b/net/apps/ftpd.c @@ -1,8 +1,7 @@ -#include - #include #include +#include #include #include @@ -107,7 +106,7 @@ int build_full_path(struct ftp_session* session, char* path, char* new_path, siz strcpy(new_path, path); else { - rt_sprintf(new_path, "%s\\%s", session->currentdir, path); + rt_sprintf(new_path, "%s/%s", session->currentdir, path); } return 0; @@ -115,8 +114,8 @@ int build_full_path(struct ftp_session* session, char* path, char* new_path, siz void ftpd_thread_entry(void* parameter) { - int sockfd; int numbytes; + int sockfd, maxfdp1; struct sockaddr_in local; fd_set readfds, tmpfds; struct ftp_session* session; @@ -143,8 +142,21 @@ void ftpd_thread_entry(void* parameter) FD_SET(sockfd, &readfds); for(;;) { + /* get maximum fd */ + maxfdp1 = sockfd + 1; + session = session_list; + while (session != RT_NULL) + { + if (maxfdp1 < session->sockfd + 1) + maxfdp1 = session->sockfd + 1; + + FD_SET(session->sockfd, &readfds); + session = session->next; + } + tmpfds=readfds; - select(0, &tmpfds, 0, 0, 0); + if (select(maxfdp1, &tmpfds, 0, 0, 0) == 0) continue; + if(FD_ISSET(sockfd, &tmpfds)) { int com_socket; @@ -199,7 +211,7 @@ void ftpd_thread_entry(void* parameter) closesocket(session->sockfd); ftp_close_session(session); } - } + } } session = next; @@ -222,7 +234,7 @@ int do_list(char* directory, int sockfd) #endif dirp = opendir(directory); - if (dirp == NULL) + if (dirp == NULL) { line_length = rt_sprintf(line_buffer, "500 Internal Error\r\n"); send(sockfd, line_buffer, line_length, 0); @@ -234,7 +246,7 @@ int do_list(char* directory, int sockfd) entry = readdir(dirp); if (entry == NULL) break; - rt_sprintf(line_buffer, "%s/%s", directory, entry->d_name); + rt_sprintf(line_buffer, "%s/%s", directory, entry->d_name); #ifdef _WIN32 if (_stat(line_buffer, &s) ==0) #else @@ -258,24 +270,24 @@ int do_simple_list(char* directory, int sockfd) DIR* dirp; struct dirent* entry; char line_buffer[256], line_length; - + dirp = opendir(directory); - if (dirp == NULL) + if (dirp == NULL) { line_length = rt_sprintf(line_buffer, "500 Internal Error\r\n"); send(sockfd, line_buffer, line_length, 0); return -1; } - + while (1) { entry = readdir(dirp); if (entry == NULL) break; - + line_length = rt_sprintf(line_buffer, "%s\r\n", entry->d_name); send(sockfd, line_buffer, line_length, 0); } - + return 0; } @@ -354,7 +366,7 @@ int ftp_process_request(struct ftp_session* session, char *buf) else if(str_begin_with(buf, "PASS")==0) { rt_kprintf("%s sent password \"%s\"\n", inet_ntoa(session->remote.sin_addr), parameter_ptr); - if (strcmp(parameter_ptr, FTP_PASSWORD)==0 || + if (strcmp(parameter_ptr, FTP_PASSWORD)==0 || session->is_anonymous == RT_TRUE) { // password correct @@ -566,7 +578,7 @@ err1: int file_size; build_full_path(session, parameter_ptr, filename, 256); - + file_size = ftp_get_filesize(filename); if( file_size == -1) { @@ -600,7 +612,7 @@ err1: } else if(str_begin_with(buf, "CDUP")==0) { - rt_sprintf(filename, "%s\\%s", session->currentdir, ".."); + rt_sprintf(filename, "%s/%s", session->currentdir, ".."); rt_sprintf(sbuf, "250 Changed to directory \"%s\"\r\n", filename); send(session->sockfd, sbuf, strlen(sbuf), 0); @@ -738,13 +750,13 @@ void ftpd_start() { rt_thread_t tid; - tid = rt_thread_create("ftp", + tid = rt_thread_create("ftpd", ftpd_thread_entry, RT_NULL, - 1024, 30, 5); + 4096, 30, 5); if (tid != RT_NULL) rt_thread_startup(tid); } #ifdef RT_USING_FINSH #include -FINSH_FUNCTION_EXPORT(ftpd_start, start ftp server); +FINSH_FUNCTION_EXPORT(ftpd_start, start ftp server) #endif diff --git a/net/lwip/src/api/sockets.c b/net/lwip/src/api/sockets.c index efa54f494b..c02e56ef16 100644 --- a/net/lwip/src/api/sockets.c +++ b/net/lwip/src/api/sockets.c @@ -494,7 +494,7 @@ lwip_recvfrom(int s, void *mem, size_t len, int flags, buf = sock->lastdata; } else { /* If this is non-blocking call, then check first */ - if (((flags & MSG_DONTWAIT) || (sock->flags & O_NONBLOCK)) && + if (((flags & MSG_DONTWAIT) || (sock->flags & O_NONBLOCK)) && (sock->rcvevent <= 0)) { if (off > 0) { /* already received data, return that */ @@ -546,9 +546,9 @@ lwip_recvfrom(int s, void *mem, size_t len, int flags, if (netconn_type(sock->conn) == NETCONN_TCP) { LWIP_ASSERT("invalid copylen, len would underflow", len >= copylen); len -= copylen; - if ( (len <= 0) || - (buf->p->flags & PBUF_FLAG_PUSH) || - (sock->rcvevent <= 0) || + if ( (len <= 0) || + (buf->p->flags & PBUF_FLAG_PUSH) || + (sock->rcvevent <= 0) || ((flags & MSG_PEEK)!=0)) { done = 1; } @@ -702,16 +702,16 @@ lwip_sendto(int s, const void *data, size_t size, int flags, #if LWIP_TCPIP_CORE_LOCKING /* Should only be consider like a sample or a simple way to experiment this option (no check of "to" field...) */ { struct pbuf* p; - + p = pbuf_alloc(PBUF_TRANSPORT, 0, PBUF_REF); if (p == NULL) { err = ERR_MEM; } else { p->payload = (void*)data; p->len = p->tot_len = short_size; - + remote_addr.addr = ((const struct sockaddr_in *)to)->sin_addr.s_addr; - + LOCK_TCPIP_CORE(); if (sock->conn->type==NETCONN_RAW) { err = sock->conn->err = raw_sendto(sock->conn->pcb.raw, p, &remote_addr); @@ -719,7 +719,7 @@ lwip_sendto(int s, const void *data, size_t size, int flags, err = sock->conn->err = udp_sendto(sock->conn->pcb.udp, p, &remote_addr, ntohs(((const struct sockaddr_in *)to)->sin_port)); } UNLOCK_TCPIP_CORE(); - + pbuf_free(p); } } @@ -845,11 +845,11 @@ lwip_selscan(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset) int i, nready = 0; fd_set lreadset, lwriteset, lexceptset; struct lwip_socket *p_sock; - + FD_ZERO(&lreadset); FD_ZERO(&lwriteset); FD_ZERO(&lexceptset); - + /* Go through each socket in each list to count number of sockets which currently match */ for(i = 0; i < maxfdp1; i++) { @@ -875,7 +875,7 @@ lwip_selscan(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset) *readset = lreadset; *writeset = lwriteset; FD_ZERO(exceptset); - + return nready; } @@ -935,27 +935,27 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, FD_ZERO(writeset); if (exceptset) FD_ZERO(exceptset); - + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: no timeout, returning 0\n")); set_errno(0); - + return 0; } - + /* add our semaphore to list */ /* We don't actually need any dynamic memory. Our entry on the * list is only valid while we are in this function, so it's ok * to use local variables */ - + select_cb.sem = sys_sem_new(0); /* Note that we are still protected */ /* Put this select_cb on top of list */ select_cb.next = select_cb_list; select_cb_list = &select_cb; - + /* Now we can safely unprotect */ sys_sem_signal(selectsem); - + /* Now just wait to be woken */ if (timeout == 0) /* Wait forever */ @@ -965,9 +965,8 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, if(msectimeout == 0) msectimeout = 1; } - - if (msectimeout > 0 && sys_arch_timeouts() == NULL){ + if (sys_arch_timeouts() == NULL){ /* it's not a lwip thread, use os semaphore with timeout to handle it */ i = sys_arch_sem_wait(select_cb.sem, msectimeout); if (i == SYS_ARCH_TIMEOUT) i = 0; @@ -977,7 +976,7 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, /* it's a lwip thread, use os semaphore with timeout to handle it */ i = sys_sem_wait_timeout(select_cb.sem, msectimeout); } - + /* Take us off the list */ sys_sem_wait(selectsem); if (select_cb_list == &select_cb) @@ -989,9 +988,9 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, break; } } - + sys_sem_signal(selectsem); - + sys_sem_free(select_cb.sem); if (i == 0) { /* Timeout */ @@ -1001,13 +1000,13 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, FD_ZERO(writeset); if (exceptset) FD_ZERO(exceptset); - + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: timeout expired\n")); set_errno(0); - + return 0; } - + if (readset) lreadset = *readset; else @@ -1020,22 +1019,22 @@ lwip_select(int maxfdp1, fd_set *readset, fd_set *writeset, fd_set *exceptset, lexceptset = *exceptset; else FD_ZERO(&lexceptset); - + /* See what's set */ nready = lwip_selscan(maxfdp1, &lreadset, &lwriteset, &lexceptset); } else sys_sem_signal(selectsem); - + if (readset) *readset = lreadset; if (writeset) *writeset = lwriteset; if (exceptset) *exceptset = lexceptset; - + LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_select: nready=%d\n", nready)); set_errno(0); - + return nready; } @@ -1206,11 +1205,11 @@ lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) /* Do length and type checks for the various options first, to keep it readable. */ switch (level) { - + /* Level: SOL_SOCKET */ case SOL_SOCKET: switch (optname) { - + case SO_ACCEPTCONN: case SO_BROADCAST: /* UNIMPL case SO_DEBUG: */ @@ -1259,7 +1258,7 @@ lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) err = ENOPROTOOPT; } /* switch (optname) */ break; - + /* Level: IPPROTO_IP */ case IPPROTO_IP: switch (optname) { @@ -1291,7 +1290,7 @@ lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) err = ENOPROTOOPT; } /* switch (optname) */ break; - + #if LWIP_TCP /* Level: IPPROTO_TCP */ case IPPROTO_TCP: @@ -1299,7 +1298,7 @@ lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) err = EINVAL; break; } - + /* If this is no TCP socket, ignore any options. */ if (sock->conn->type != NETCONN_TCP) return 0; @@ -1313,7 +1312,7 @@ lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) case TCP_KEEPCNT: #endif /* LWIP_TCP_KEEPALIVE */ break; - + default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_TCP, UNIMPL: optname=0x%x, ..)\n", s, optname)); @@ -1328,7 +1327,7 @@ lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) err = EINVAL; break; } - + /* If this is no UDP lite socket, ignore any options. */ if (sock->conn->type != NETCONN_UDPLITE) return 0; @@ -1337,7 +1336,7 @@ lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) case UDPLITE_SEND_CSCOV: case UDPLITE_RECV_CSCOV: break; - + default: LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, IPPROTO_UDPLITE, UNIMPL: optname=0x%x, ..)\n", s, optname)); @@ -1352,7 +1351,7 @@ lwip_getsockopt(int s, int level, int optname, void *optval, socklen_t *optlen) err = ENOPROTOOPT; } /* switch */ - + if (err != ERR_OK) { sock_set_errno(sock, err); return -1; @@ -1397,7 +1396,7 @@ lwip_getsockopt_internal(void *arg) optval = data->optval; switch (level) { - + /* Level: SOL_SOCKET */ case SOL_SOCKET: switch (optname) { @@ -1443,7 +1442,7 @@ lwip_getsockopt_internal(void *arg) case SO_ERROR: if (sock->err == 0) { sock_set_errno(sock, err_to_errno(sock->conn->err)); - } + } *(int *)optval = sock->err; sock->err = 0; LWIP_DEBUGF(SOCKETS_DEBUG, ("lwip_getsockopt(%d, SOL_SOCKET, SO_ERROR) = %d\n", diff --git a/net/lwip/src/arch/sys_arch.c b/net/lwip/src/arch/sys_arch.c index caecd51cec..1cd8559424 100644 --- a/net/lwip/src/arch/sys_arch.c +++ b/net/lwip/src/arch/sys_arch.c @@ -34,7 +34,7 @@ sys_sem_t sys_sem_new(u8_t count) #if SYS_DEBUG { struct rt_thread *thread; - + thread = rt_thread_self(); LWIP_DEBUGF(SYS_DEBUG, ("%s, Create sem: %s \n",thread->name, tname)); } @@ -51,7 +51,7 @@ void sys_sem_free(sys_sem_t sem) { struct rt_thread *thread; thread = rt_thread_self(); - + LWIP_DEBUGF(SYS_DEBUG, ("%s, Delete sem: %s \n",thread->name, sem->parent.parent.name)); } @@ -68,7 +68,7 @@ void sys_sem_signal(sys_sem_t sem) { struct rt_thread *thread; thread = rt_thread_self(); - + LWIP_DEBUGF(SYS_DEBUG, ("%s, Release signal: %s , %d\n",thread->name, sem->parent.parent.name, sem->value)); } @@ -91,7 +91,7 @@ u32_t sys_arch_sem_wait(sys_sem_t sem, u32_t timeout) { struct rt_thread *thread; thread = rt_thread_self(); - + LWIP_DEBUGF(SYS_DEBUG, ("%s, Wait sem: %s , %d\n",thread->name, sem->parent.parent.name, sem->value)); } @@ -132,7 +132,7 @@ sys_mbox_t sys_mbox_new(int size) { struct rt_thread *thread; thread = rt_thread_self(); - + LWIP_DEBUGF(SYS_DEBUG, ("%s, Create mbox: %s \n",thread->name, tname)); } #endif @@ -148,7 +148,7 @@ void sys_mbox_free(sys_mbox_t mbox) { struct rt_thread *thread; thread = rt_thread_self(); - + LWIP_DEBUGF(SYS_DEBUG, ("%s, Delete mbox: %s\n",thread->name, mbox->parent.parent.name)); } @@ -165,7 +165,7 @@ void sys_mbox_post(sys_mbox_t mbox, void *msg) { struct rt_thread *thread; thread = rt_thread_self(); - + LWIP_DEBUGF(SYS_DEBUG, ("%s, Post mail: %s ,0x%x\n",thread->name, mbox->parent.parent.name, (rt_uint32_t)msg)); } @@ -182,7 +182,7 @@ err_t sys_mbox_trypost(sys_mbox_t mbox, void *msg) { struct rt_thread *thread; thread = rt_thread_self(); - + LWIP_DEBUGF(SYS_DEBUG, ("%s, Post mail: %s ,0x%x\n",thread->name, mbox->parent.parent.name, (rt_uint32_t)msg)); } @@ -220,7 +220,7 @@ u32_t sys_arch_mbox_fetch(sys_mbox_t mbox, void **msg, u32_t timeout) { struct rt_thread *thread; thread = rt_thread_self(); - + LWIP_DEBUGF(SYS_DEBUG, ("%s, Fetch mail: %s , 0x%x\n",thread->name, mbox->parent.parent.name, *(rt_uint32_t **)msg)); } @@ -249,7 +249,7 @@ u32_t sys_arch_mbox_tryfetch(sys_mbox_t mbox, void **msg) { struct rt_thread *thread; thread = rt_thread_self(); - + LWIP_DEBUGF(SYS_DEBUG, ("%s, Fetch mail: %s , 0x%x\n",thread->name, mbox->parent.parent.name, *(rt_uint32_t **)msg)); } @@ -314,6 +314,7 @@ void sys_arch_unprotect(sys_prot_t pval) void sys_arch_assert(const char* file, int line) { - rt_kprintf("Assertion: %d in %s\n", line, file); + rt_kprintf("\nAssertion: %d in %s, thread %s\n", line, file, + rt_thread_self()->name); RT_ASSERT(0); } -- GitLab