Changeset 7180

Show
Ignore:
Timestamp:
02/27/08 15:05:04 (5 years ago)
Author:
frsyuki
Message:

lang/c/partty: fixed busy loop probrem

Location:
lang/c/partty/trunk
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • lang/c/partty/trunk/host.cc

    r7068 r7180  
    7979        { 
    8080                uint16_t msglen = ntohs(reply.message_length); 
    81                 char* msg = (char*)malloc(msglen); 
     81                char* msg = (char*)malloc(msglen+1); 
    8282                if( msg == NULL ) { 
    8383                        throw initialize_error("failed to receive negotiation reply"); 
     
    8888                } 
    8989                if( reply.code != negotiation_reply::SUCCESS ) { 
     90                        msg[msglen] = '\0'; 
    9091                        initialize_error e(msg); 
    9192                        free(msg); 
     
    160161        } else if( len == 0 ) { throw io_end_error("end of stdinput"); } 
    161162        // ブロックしながらシェルに書き込む 
    162         // XXX 書き込み可能になるまでビジーループ 
    163         if( write_all(sh, shared_buffer, len) != (size_t)len ){ 
     163        if( continued_blocking_write_all(sh, shared_buffer, len) != (size_t)len ){ 
    164164                throw io_error("pty is broken"); 
    165165        } 
     
    191191        if( m_locking ) { return 0; } 
    192192        // ブロックしながらシェルに書き込む 
    193         // XXX 書き込み可能になるまでビジーループ 
    194         if( write_all(sh, shared_buffer, len) != (size_t)len ){ 
     193        if( continued_blocking_write_all(sh, shared_buffer, len) != (size_t)len ){ 
    195194                throw io_error("pty is broken"); 
    196195        } 
     
    208207        } else if( len == 0 ) { throw io_end_error("session ends"); } 
    209208        // ブロックしながら標準出力に書き込む 
    210         // XXX 書き込み可能になるまでビジーループ 
    211         if( write_all(STDOUT_FILENO, shared_buffer, len) != (size_t)len ) { 
     209        if( continued_blocking_write_all(STDOUT_FILENO, shared_buffer, len) != (size_t)len ) { 
    212210                throw io_error("stdoutput is broken"); 
    213211        } 
    214212        // ブロックしながらServerに書き込む 
    215         // XXX 書き込み可能になるまでビジーループ 
    216         if( write_all(server, shared_buffer, len) != (size_t)len ) { 
     213        if( continued_blocking_write_all(server, shared_buffer, len) != (size_t)len ) { 
    217214                throw io_error("server connection is broken"); 
    218215        } 
  • lang/c/partty/trunk/multiplexer.cc

    r7068 r7180  
    7575        for(delimiter = 0x3A; delimiter <= 0x7E; ++delimiter) { 
    7676                // ':' - '~' 
    77                 if( !(exist = strchr(info.session_name, delimiter)) ) { break; } 
     77                if( (exist = strchr(info.session_name, delimiter)) ) { break; } 
    7878        } 
    7979        if( !exist ) { 
    8080                for(delimiter = 0x21; delimiter < 0x3A; ++delimiter) { 
    8181                        // '!' - '9' 
    82                         if( !(exist = strchr(info.session_name, delimiter)) ) { break; } 
     82                        if( (exist = strchr(info.session_name, delimiter)) ) { break; } 
    8383                } 
    8484        } 
     
    195195        ssize_t len = read(fd, shared_buffer, SHARED_BUFFER_SIZE); 
    196196        if( len < 0 ) { 
    197                 if( errno == EAGAIN || errno == EINTR ) { /* do nothing */ } 
     197                if( errno == EAGAIN || errno == EINTR ) { return 0; } 
    198198                else { throw io_error("host socket is broken"); } 
    199199        } else if( len == 0 ) { 
     
    226226                ssize_t len = read(fd, shared_buffer, SHARED_BUFFER_SIZE); 
    227227                if( len < 0 ) { 
    228                         if( errno == EAGAIN || errno == EINTR ) { /* do nothing */ } 
     228                        if( errno == EAGAIN || errno == EINTR ) { /* pass through */ } 
    229229                        else { 
    230230                                perror("read from guest failed"); 
     
    243243                                return 0; 
    244244                        } 
    245                         // FIXME write_all? ブロッキングモードにする? 
    246245                        // FIXME Hostが切断されたのかエラーが発生したのか区別できない 
    247                         if( write_all(host, ibuf.buf, ibuf.len) != ibuf.len ) { 
     246                        if( continued_blocking_write_all(host, ibuf.buf, ibuf.len) != ibuf.len ) { 
    248247                                throw io_error("host socket is broken"); 
    249248                        } 
     
    257256                //if( srv.olength == 0 ) { return 0; }  // olengthは0の可能性がある? 
    258257                if( len < 0 ) { 
    259                         if( errno == EAGAIN || errno == EINTR ) { /* do nothing */ } 
     258                        if( errno == EAGAIN || errno == EINTR ) { return 0; } 
    260259                        else { 
    261260                                perror("write to guest failed"); 
  • lang/c/partty/trunk/partty.h

    r7170 r7180  
    1212 
    1313#ifndef PARTTY_GATE_DIR 
    14 #define PARTTY_GATE_DIR "./var/" 
    15 #endif 
    16  
    17 //#ifndef PARTTY_GATE_SOCKET_NAME 
    18 //#define PARTTY_GATE_SOCKET_NAME "gate" 
    19 //#endif 
     14#define PARTTY_GATE_DIR "./var/socket/" 
     15#endif 
     16 
     17#ifndef PARTTY_ARCHIVE_DIR 
     18#define PARTTY_ARCHIVE_DIR \ 
     19        "./archive/archive/" 
     20#endif 
    2021 
    2122#ifndef PARTTY_GATE_SESSION_BANNER 
     
    6566 
    6667static const char* const GATE_DIR = PARTTY_GATE_DIR; 
    67 //static const char* const GATE_SOCKET_NAME = PARTTY_GATE_SOCKET_NAME; 
     68static const char* const ARCHIVE_DIR = PARTTY_ARCHIVE_DIR; 
    6869static const char* const GATE_PASSWORD_BANNER = PARTTY_GATE_PASSWORD_BANNER; 
    6970static const char* const GATE_SESSION_BANNER = PARTTY_GATE_SESSION_BANNER; 
     
    7374 
    7475 
    75 //static const char PARTTY_VERSION[] = "0.0"; 
    76 //static const char PARTTY_REVISION[] = "0"; 
    7776static const uint8_t PROTOCOL_VERSION = 1; 
    7877 
     
    239238class Host { 
    240239public: 
     240        struct config_t { 
     241                config_t(int _server_socket, 
     242                                const session_info_ref_t& info_) : 
     243                        server_socket(_server_socket), 
     244                        info(info_) {} 
     245        public: 
     246                int lock_code; 
     247        private: 
     248                int server_socket; 
     249                const session_info_ref_t& info; 
     250                friend class HostIMPL; 
     251        }; 
     252public: 
    241253        Host(int server_socket, char lock_code, 
    242254                const session_info_ref_t& info); 
     
    255267class Gate { 
    256268public: 
     269        struct config_t { 
     270                config_t(int listen_socket_) : 
     271                        listen_socket(listen_socket_) {} 
     272        public: 
     273        private: 
     274                int listen_socket; 
     275                friend class GateIMPL; 
     276        }; 
     277public: 
    257278        Gate(int listen_socket); 
    258279        ~Gate(); 
  • lang/c/partty/trunk/uniext.h

    r7068 r7180  
    55#include <unistd.h> 
    66#include <errno.h> 
     7#include <fcntl.h> 
    78 
    89namespace Partty { 
    910 
    1011 
    11 inline size_t read_all(int fd, void *buf, size_t count) { 
     12inline size_t read_all(int fd, void *buf, size_t count) 
     13{ 
    1214        char *p = (char*)buf; 
    1315        char * const endp = p + count; 
    1416 
    1517        do { 
    16                 const int num_bytes = read(fd, p, endp - p); 
     18                const ssize_t num_bytes = read(fd, p, endp - p); 
    1719                if( num_bytes < 0 ) { 
    1820                        if( errno != EINTR && errno != EAGAIN ) { 
     
    2729} 
    2830 
    29 inline size_t write_all(int fd, const void *buf, size_t count) { 
     31inline size_t write_all(int fd, const void *buf, size_t count) 
     32{ 
    3033        const char *p = (char*)buf; 
    3134        const char * const endp = p + count; 
    3235 
    3336        do { 
    34                 const int num_bytes = write(fd, p, endp - p); 
     37                const ssize_t num_bytes = write(fd, p, endp - p); 
    3538                if( num_bytes < 0 ) { 
    3639                        if( errno != EINTR && errno != EAGAIN ) { 
     
    4548} 
    4649 
     50inline size_t continued_blocking_read_all(int fd, void* buf, size_t count) 
     51{ 
     52        ssize_t num_bytes; 
     53 
     54        num_bytes = read(fd, buf, count); 
     55        if( num_bytes >= count ) { 
     56                return num_bytes; 
     57        } else if( num_bytes <= 0 ) { 
     58                if( errno != EINTR && errno != EAGAIN ) { return 0; } 
     59                else { num_bytes = 0; } 
     60        } 
     61 
     62        fcntl(fd, F_SETFL, 0); 
     63 
     64        char *p = (char*)buf + num_bytes; 
     65        char * const endp = (char*)buf + count; 
     66 
     67        do { 
     68                num_bytes = read(fd, p, endp - p); 
     69                if( num_bytes < 0 ) { 
     70                        if( errno != EINTR && errno != EAGAIN ) { 
     71                                break; 
     72                        } 
     73                } else { 
     74                        p += num_bytes; 
     75                } 
     76        } while (p < endp); 
     77 
     78        fcntl(fd, F_SETFL, O_NONBLOCK); 
     79 
     80        return endp - p; 
     81} 
     82 
     83inline size_t continued_blocking_write_all(int fd, const void* buf, size_t count) 
     84{ 
     85        ssize_t num_bytes; 
     86 
     87        num_bytes = write(fd, buf, count); 
     88        if( num_bytes >= count ) { 
     89                return num_bytes; 
     90        } else if( num_bytes <= 0 ) { 
     91                if( errno != EINTR && errno != EAGAIN ) { return 0; } 
     92                else { num_bytes = 0; } 
     93        } 
     94 
     95        fcntl(fd, F_SETFL, 0); 
     96 
     97        const char *p = (char*)buf + num_bytes; 
     98        const char * const endp = (char*)buf + count; 
     99 
     100        do { 
     101                num_bytes = write(fd, p, endp - p); 
     102                if( num_bytes < 0 ) { 
     103                        if( errno != EINTR && errno != EAGAIN ) { 
     104                                break; 
     105                        } 
     106                } else { 
     107                        p += num_bytes; 
     108                } 
     109        } while (p < endp); 
     110 
     111        fcntl(fd, F_SETFL, O_NONBLOCK); 
     112 
     113        return endp - p; 
     114} 
     115 
    47116void initprocname(int argc, char**& argv, char**& envp); 
    48117