/***************************************************************** * sthp_serv.c the execute module is is implementation * the server side of String Type Handle Protocol * * Copyright (C) 2009 Andrey Y. Rjavskov(aka rjaan) * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software Foundation, * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * *****************************************************************/ #include #include #include #include #include #include #include #include #include #include #include "sthp.h" static int _sockfd; static void nothing_exec_sighandler ( int signum ); static void child_handler ( int sockfd ); static void sig_handler ( int sig_num ); int main ( char** argv, int argc ) __attribute__ ((__noreturn__)); int main ( char** argv, int argc ) { int status; int sockfd, optval = 1; struct sigaction sa, osa; struct sockaddr_un servaddr; print_stderr ( STDERRMSG_HAVE_BYTE_DEFAULT, "sthp server init \n"); sa.sa_handler = nothing_exec_sighandler; sigemptyset ( &sa.sa_mask ); sa.sa_flags = SA_RESTART; if ( sigaction ( SIGALRM, &sa, &osa ) < 0 ) { print_stderr ( STDERRMSG_HAVE_BYTE_DEFAULT, "sthp server error: can\'t change the action taken by %d process.Terminated!\n", getpid()); exit ( EXIT_FAILURE ); } sockfd = socket( AF_LOCAL, SOCK_STREAM, 0 ); if( sockfd == -1 ) { print_stderr ( STDERRMSG_HAVE_BYTE_DEFAULT, "sthp server error: can\'t create socket!\n" ); exit ( EXIT_FAILURE ); } sthp_set_signal ( &sockfd ); _sockfd = sockfd; if ( setsockopt ( sockfd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(int) ) ) { print_stderr ( STDERRMSG_HAVE_BYTE_DEFAULT, "sthp server error: can\'t set option to socket!\n" ); close( sockfd ); exit ( EXIT_FAILURE ); } unlink( STHP_UNIXSERVER_PATH ); bzero( &servaddr, sizeof ( struct sockaddr_un ) ); servaddr.sun_family = AF_LOCAL; strcpy ( servaddr.sun_path, STHP_UNIXSERVER_PATH ); if ( bind ( sockfd, (struct sockaddr*)&servaddr, sizeof ( struct sockaddr_un ) ) == -1 ) { print_stderr ( STDERRMSG_HAVE_BYTE_DEFAULT, "sthp server error: can\'t bind socket with unix path!\n" ); close( sockfd ); exit ( EXIT_FAILURE); } if ( listen ( sockfd, 1 ) == -1 ) { print_stderr ( STDERRMSG_HAVE_BYTE_DEFAULT, "sthp server error: cannot transform an active socket into a passive socket!\n" ); close( sockfd ); exit ( EXIT_FAILURE ); } print_stderr ( STDERRMSG_HAVE_BYTE_DEFAULT, "sthp server wait connections!\n"); signal ( SIGCLD, SIG_IGN ); waitpid ( -1, &status, WNOHANG ); while ( 1 ) { int newsockfd; struct sockaddr_un cliaddr; int cliaddr_len = sizeof ( struct sockaddr_un ); if ( ( newsockfd = accept ( sockfd, (struct sockaddr*)&cliaddr, &cliaddr_len ) ) == -1 ) { print_stderr ( STDERRMSG_HAVE_BYTE_DEFAULT, "sthp server error: cannot transform an active socket into a passive socket!\n" ); close( sockfd ); exit ( EXIT_FAILURE ); } if ( !fork( ) ) { close(sockfd); child_handler ( newsockfd ); exit(0); } close(newsockfd); } close ( sockfd ); exit ( EXIT_SUCCESS ); } static void nothing_exec_sighandler ( int signum ) { if ( signum != SIGALRM ) { return; } print_stderr ( STDERRMSG_HAVE_BYTE_DEFAULT, "%d process has accepted SIGALARM at time system call executing", getpid() ); } static void child_handler ( int sockfd ) { int bytes = 0; char *ask = NULL; char *reply; if ( ( bytes = sthp_recv ( sockfd, &ask ) ) == -EXIT_FAILURE ) { goto child_end; } print_stderr ( strlen(ask), "ACCEPTED STRING: \"%s\"\n", ask); reply = STRING_TYPE_NAME ( sthp_is_string_type_from ( ask ) ); print_stderr ( strlen(ask), "IT HAVE LENGTH: %d BYTES\n", bytes ); print_stderr ( strlen(ask), "IT HAVE TYPE: %s\n", reply ); if( sthp_send ( sockfd, reply ) == -EXIT_FAILURE ) { goto child_end; } print_stderr ( strlen(ask), "SEND REPLY\n" ); child_end: if( ask ) { free( ask ); } close(sockfd); } /*eof*/