#define _POSIX_C_SOURCE 200809L #include /* assert */ #include /* err */ #include /* errno */ #include /* uint32_t, int64_t */ #include /* exit */ #include /* strlen, memcmp */ #include /* read, write, close, unlink, … */ #include /* socket, AF_* */ #include /* struct sockaddr_un */ #include /* ntohl */ /* Napište podprogram ‹count_server›, který přijme 3 parametry: * * 1. ‹sock_fd› je popisovač proudového socketu, který je svázán * s adresou a je nastaven do režimu poslouchání, * 2. ‹count› je maximální počet připojení (po jeho dosažení se * podprogram vrátí), * 3. ‹initial› je počáteční hodnota počítadla. * * Server si pamatuje počítadlo v rozsahu 0–63 a implementuje * následující protokol (každá zpráva je ukončena znakem nového * řádku): * * • klient může odeslat příkazy ‹inc›, ‹dec›, ‹get›, * • je-li zpráva od klienta ‹inc› nebo ‹dec›, server zvýší (sníží) * počítadlo o 1 a odpoví ‹ok›, * • je-li zpráva ‹get›, server odpoví dekadickým zápisem hodnoty * počítadla (je-li toto menší než 10, odpověď doplní * levostrannou nulou). * * Dorazí-li zpráva ‹inc› a počítadlo je 63, nová hodnota bude 0, * dorazí-li ‹dec› a počítadlo je 0, nová hodnota bude 63. * * Návratová hodnota 0 znamená, že bylo úspěšně obslouženo ‹count› * klientů, -1 znamená systémovou chybu. */ int count_server( int sock_fd, int count, int initial ); /* ┄┄┄┄┄┄┄ %< ┄┄┄┄┄┄┄┄┄┄ následují testy ┄┄┄┄┄┄┄┄┄┄ %< ┄┄┄┄┄┄┄ */ #include /* waitpid */ #include /* signal, SIG_IGN, SIGPIPE */ static void close_or_warn( int fd, const char *name ) { if ( close( fd ) == -1 ) warn( "closing %s", name ); } static void unlink_if_exists( const char *file ) { if ( unlink( file ) == -1 && errno != ENOENT ) err( 2, "unlink" ); } static int reap( pid_t pid ) { int status; if ( waitpid( pid, &status, 0 ) == -1 ) err( 2, "wait" ); if ( WIFEXITED( status ) ) return WEXITSTATUS( status ); else return -1; } static pid_t fork_server( int sock_fd, int clients, int init ) { pid_t pid = fork(); if ( pid == -1 ) err( 2, "fork" ); if ( pid == 0 ) { alarm( 3 ); exit( count_server( sock_fd, clients, init ) ? 1 : 0 ); } close_or_warn( sock_fd, "server socket in client" ); return pid; } static const struct sockaddr_un test_addr = { .sun_family = AF_UNIX, .sun_path = "zt.a_socket" }; static int client_connect() { int fd = socket( AF_UNIX, SOCK_STREAM, 0 ); if ( fd == -1 ) err( 2, "socket" ); if ( connect( fd, ( const struct sockaddr * ) &test_addr, sizeof test_addr ) == -1 ) return -1; return fd; } static int client_cmd( int fd, const char *cmd, const char *expect ) { int r_len = strlen( expect ); char reply[ r_len ]; if ( send( fd, cmd, strlen( cmd ), 0 ) == -1 ) return -1; if ( recv( fd, reply, r_len, MSG_WAITALL ) != r_len ) return -1; return 0; } int main( void ) { if ( signal( SIGPIPE, SIG_IGN ) == SIG_ERR ) err( 2, "signal" ); char buffer[ 4 ]; unlink_if_exists( test_addr.sun_path ); int sock_fd = socket( AF_UNIX, SOCK_STREAM, 0 ); if ( sock_fd == -1 ) err( 2, "socket" ); if ( bind( sock_fd, ( const struct sockaddr * ) &test_addr, sizeof test_addr ) == -1 ) err( 2, "bind" ); if ( listen( sock_fd, 3 ) == -1 ) err( 2, "listen" ); pid_t pid = fork_server( sock_fd, 3, 15 ); int c1 = client_connect(); int c2 = client_connect(); assert( client_cmd( c1, "get\n", "15\n" ) == 0 ); assert( client_cmd( c1, "inc\n", "ok\n" ) == 0 ); assert( client_cmd( c2, "get\n", "16\n" ) == 0 ); int c3 = client_connect(); assert( send( c3, "g", 1, 0 ) == 1 ); assert( client_cmd( c2, "dec\n", "ok\n" ) == 0 ); assert( client_cmd( c1, "inc\n", "ok\n" ) == 0 ); assert( send( c3, "e", 1, 0 ) == 1 ); assert( client_cmd( c2, "inc\n", "ok\n" ) == 0 ); assert( send( c3, "t", 1, 0 ) == 1 ); assert( client_cmd( c2, "inc\n", "ok\n" ) == 0 ); assert( client_cmd( c1, "dec\n", "ok\n" ) == 0 ); close_or_warn( c1, "c1" ); assert( send( c3, "\n", 1, 0 ) == 1 ); assert( recv( c3, buffer, 3, MSG_WAITALL ) == 3 ); assert( memcmp( buffer, "17", 2 ) == 0 ); close_or_warn( c2, "c2" ); close_or_warn( c3, "c3" ); assert( reap( pid ) == 0 ); unlink_if_exists( test_addr.sun_path ); return 0; }