17
mbd.c
- #include "mbd.h"
- #include "mbd_kbd.h"
- #include "mb_sio.h"
- #include "mbdef.h"
- #include "ctk/shell.h"
- #include <sys/types.h>
- #include <sys/socket.h>
- #if defined( LINUX ) || defined (HPUX)
- #include <sys/time.h>
- #else
- #include <sys/select.h>
- #endif
- #include <sys/file.h>
- #include <sys/ioctl.h>
- #include <sys/param.h>
- #include <stdlib.h>
- #include <string.h>
- #if defined( SUN ) || defined( LINUX )
- #include <netinet/in.h>
- #include <unistd.h>
- #endif
- #include <netdb.h>
- #include <signal.h>
- #include <time.h>
- #include <errno.h>
- #include "ctk/debug.h"
- #if defined( DEBUG )
- #include "ctk/dump.h"
- int debug = 0;
- #endif
- #ifndef MIN
- #define MIN(x, y) ((x) < (y) ? (x) : (y))
- #endif
- #ifndef MAX
- #define MAX(x, y) ((x) < (y) ? (y) : (x))
- #endif
- struct magicbus mb; /* application object */
- static int mb_write( int s, u_char *buf, int len );
- static void mb_watch_socket( int socket ) {
- struct mb_socket *s_in;
- ONDEBUG2( fprintf( stdout, "\n[accept=%i]", socket ) );
- s_in = (struct mb_socket *)malloc( sizeof ( struct mb_socket ));
- s_in->n_sent = s_in->n_recv = 0;
- s_in->s_socket = socket;
- s_in->s_name = 0;
- list_put( mb.in_sockets, -1, s_in );
- /* accounting */
- mb.n_conn++;
- }
- static void mb_drop_socket( int socket ) {
- struct mb_socket *s_in;
- struct mb_toreq *to_r;
- int i, len;
- ONDEBUG2( fprintf( stdout, "\n[drop=%i]", socket ) );
- for ( i = 0, len = list_length( mb.in_sockets ); i < len; i++ ) {
- s_in = (struct mb_socket *)list_get( mb.in_sockets, i );
- if ( s_in->s_socket == socket ) {
- list_delete( mb.in_sockets, i );
- if ( s_in->s_name )
- free( s_in->s_name );
- free( s_in );
- break;
- }
- }
- for ( i = 0, len = list_length( mb.to_reqs ); i < len; i++ ) {
- to_r = (struct mb_toreq *)list_get( mb.to_reqs, i );
- /* lost a requester? */
- if ( to_r->r_sin == socket ) {
- list_delete( mb.to_reqs, i );
- free( to_r );
- break;
- }
- /* lost a responder? */
- if ( to_r->r_sout == socket ) {
- list_delete( mb.to_reqs, i );
- if ( to_r->r_sin ) {
- char rsp[ 64 ];
- int n;
- /* notify requester */
- n = sprintf( rsp, "%c%c%u%c", MB_STAMP, MB_TIMEOUT, to_r->r_msg_id, 0 );
- mb_write( to_r->r_sin, (u_char *)rsp, n );
- ONDEBUG1( fprintf( stdout, "reply_type=%c msg_id=%u\n", MB_TIMEOUT, to_r->r_msg_id );
- dump( (u_char *)rsp, MIN(n, mb.dump_size), stdout ) );
- }
- free( to_r );
- break;
- }
- }
- }
- static struct mb_socket *mb_find_socket( int socket ) {
- int i, len = list_length( mb.in_sockets );
- struct mb_socket *s_in;
- for ( i = 0; i < len; i++ ) {
- s_in = (struct mb_socket *)list_get( mb.in_sockets, i );
- if ( s_in->s_socket == socket )
- return s_in;
- }
- return 0;
- }
- static int mb_count_socket_name( char *s_name ) {
- int i, len = list_length( mb.in_sockets );
- struct mb_socket *s_in;
- int n = 0;
- for ( i = 0; i < len; i++ ) {
- s_in = (struct mb_socket *)list_get( mb.in_sockets, i );
- if ( s_in->s_name && strcmp( s_in->s_name, s_name ) == 0 )
- n++;
- }
- return n;
- }
- static int mb_read( int s, u_char *buf, int len ) {
- int r = s_read( s, buf, len );
- if ( r < 0 )
- mb_drop_socket( s );
- return r;
- }
- static int mb_write( int s, u_char *buf, int len ) {
- int w = s_write( s, buf, len );
- if ( w < 0 )
- mb_drop_socket( s );
- return w;
- }
- static int mb_getc( int s ) {
- int c = s_getc( s );
- if ( c < 0 )
- mb_drop_socket( s );
- return c;
- }
- static int mb_getuint( int s ) {
- int i = s_getuint( s );
- if ( i < 0 )
- mb_drop_socket( s );
- return i;
- }
- static char *mb_getident( int s ) {
- char *p = s_getident( s );
- if ( !p )
- mb_drop_socket( s );
- return p;
- }
- static int mb_add_toreq( int msg_id, int sin, int sout, int timeout ) {
- struct mb_toreq *to_r;
- int i, len;
- time_t attime = time( 0 ) + timeout;
- static int r_id = 0;
- if ( ++r_id < 0 )
- r_id = 1;
- for ( i = 0, len = list_length( mb.to_reqs ); i < len; i++ ) {
- to_r = (struct mb_toreq *)list_get( mb.to_reqs, i );
- if ( to_r->r_attime > attime )
- break;
- }
- to_r = (struct mb_toreq *)malloc( sizeof (struct mb_toreq) );
- to_r->r_id = r_id;
- to_r->r_msg_id = msg_id;
- to_r->r_sout = sout;
- to_r->r_sin = sin;
- to_r->r_attime = attime;
- list_insert( mb.to_reqs, i, to_r );
- return r_id;
- }
- static time_t mb_check_toreqs( ) {
- struct mb_toreq *to_r;
- time_t now;
- char rsp[ 64 ];
- int n;
- if ( list_length( mb.to_reqs ) == 0 )
- /* be quick */
- return 0;
- now = time( 0 );
- while ( (to_r = (struct mb_toreq *)list_get( mb.to_reqs, 0 )) ) {
- if ( to_r->r_attime > now )
- break;
- if ( to_r->r_sin ) {
- n = sprintf( rsp, "%c%c%u%c", MB_STAMP, MB_TIMEOUT, to_r->r_msg_id, 0 );
- mb_write( to_r->r_sin, (u_char *)rsp, n );
- ONDEBUG1( fprintf( stdout, "reply_type=%c msg_id=%u\n", MB_TIMEOUT, to_r->r_msg_id );
- dump( (u_char *)rsp, MIN(n, mb.dump_size), stdout ) );
- free( to_r );
- }
- list_delete( mb.to_reqs, 0 );
- }
- to_r = list_get( mb.to_reqs, 0 );
- return to_r ? to_r->r_attime : 0;
- }
- int mb_getpagesize( ) {
- return mb.page_size;
- }
- int mb_setpagesize( int pagesize ) {
- if ( pagesize > 0 )
- mb.page_size = pagesize;
- return mb.page_size;
- }
- int mb_gettimeout( ) {
- return mb.timeout;
- }
- int mb_settimeout( int to ) {
- if ( to > 0 )
- mb.timeout = to;
- return mb.timeout;
- }
- void mb_register( int socket ) {
- struct mb_socket *s_in = mb_find_socket( socket );
- int msg_id;
- char *s_name;
- char msg[ 32 ];
- int msg_len;
- char rsp[ 64 ];
- int n;
- if ( (msg_id = mb_getuint( socket )) < 0 )
- return;
- ONDEBUG1( fprintf( stdout, " msg_id=%u", msg_id ) );
- if ( (s_name = mb_getident( socket )) == 0 )
- return;
- ONDEBUG1( fprintf( stdout, " domain=%s\n", s_name ) );
- if ( s_in->s_name ) { /* already named? */
- free( s_in->s_name );
- s_in->s_name = 0;
- }
- /* change socket's name */
- if ( (s_in->s_name = (char *)malloc( strlen( s_name ) + 1 )) == 0 ) {
- mb_drop_socket( socket );
- close( socket ); /* be tough */
- return;
- }
- strcpy( s_in->s_name, s_name );
- /* reply with a '\0' terminated formatted number */
- msg_len = sprintf( msg, "%u", mb_count_socket_name( s_name ) );
- n = sprintf( rsp, "%c%c%u%c%u%c%s%c", MB_STAMP, MB_REGISTER, msg_id, 0, msg_len + 1, 0, msg, 0 );
- mb_write( socket, (u_char *)rsp, n );
- ONDEBUG1( fprintf( stdout, "reply_type=%c msg_id=%u n=%i\n", MB_REGISTER, msg_id, mb_count_socket_name( s_name ) );
- dump( (u_char *)rsp, MIN(n, mb.dump_size), stdout ) );
- }
- void mb_probe( int socket ) {
- int msg_id;
- char *s_name;
- char msg[ 32 ];
- int msg_len;
- char rsp[ 64 ];
- int n;
- if ( (msg_id = mb_getuint( socket )) < 0 )
- return;
- ONDEBUG1( fprintf( stdout, " msg_id=%u", msg_id ) );
- if ( (s_name = mb_getident( socket )) == 0 )
- return;
- ONDEBUG1( fprintf( stdout, " domain=%s\n", s_name ) );
- /* reply with a '\0' terminated formatted number */
- msg_len = sprintf( msg, "%u", mb_count_socket_name( s_name ) );
- n = sprintf( rsp, "%c%c%u%c%u%c%s%c", MB_STAMP, MB_PROBE, msg_id, 0, msg_len + 1, 0, msg, 0 );
- mb_write( socket, (u_char *)rsp, n );
- ONDEBUG1( fprintf( stdout, "reply_type=%c msg_id=%u n=%i\n", MB_REGISTER, msg_id, mb_count_socket_name( s_name ) );
- dump( (u_char *)rsp, MIN(n, mb.dump_size), stdout ) );
- }
- void mb_send( int socket, int how ) {
- struct mb_socket *s_in = mb_find_socket( socket );
- struct mb_socket *s_out;
- char *s_name;
- int msg_id, msg_len;
- char rsp[ 64 ];
- int n;
- list s_list;
- int i, len;
- int r;
- if ( (s_name = mb_getident( socket )) == 0 )
- return;
- ONDEBUG1( fprintf( stdout, " domain=%s", s_name ) );
- if ( (msg_id = mb_getuint( socket )) < 0 )
- return;
- ONDEBUG1( fprintf( stdout, " msg_id=%u", msg_id ) );
- if ( (msg_len = mb_getuint( socket )) < 0 )
- return;
- ONDEBUG1( fprintf( stdout, " msg_len=%u\n", msg_len ) );
- /* find peer */
- for ( s_list = list_new(), i = 0, len = list_length( mb.in_sockets ); i < len; i++ ) {
- s_out = (struct mb_socket *)list_get( mb.in_sockets, i );
- if ( s_out->s_socket != socket && s_out->s_name && strcmp( s_out->s_name, s_name ) == 0 ) {
- list_put( s_list, -1, (void *)s_out->s_socket );
- s_out->n_recv++;
- if ( how != MB_MULTIWAY )
- break; /* only one */
- }
- }
- /* read data */
- if ( msg_len > 0 ) {
- u_char *msg = (u_char *)alloca( mb.page_size );
- int s, r_id;
- /* send header */
- for ( i = 0; i < list_length( s_list ); i++ ) {
- s = (int)list_get( s_list, i );
- r_id = how == MB_TWOWAY ? mb_add_toreq( msg_id, socket, s, mb_gettimeout() ) : 0;
- n = sprintf( rsp, "%c%c%u%c%u%c", MB_STAMP, how, r_id, 0, msg_len, 0 );
- if ( mb_write( s, (u_char *)rsp, n ) < 0 )
- /* lost peer */
- list_delete( s_list, i );
- }
- /* transmit data */
- do {
- n = MIN( msg_len, mb.page_size );
- r = socket != -1 ? mb_read( socket, msg, n ) : n;
- if ( r < 0 ) {
- /* lost sender */
- memset( msg, 0, r = n );
- socket = -1;
- }
- for ( i = 0; i < list_length( s_list ); i++ ) {
- s = (int)list_get( s_list, i );
- if ( mb_write( s, msg, r ) < 0 )
- /* lost peer */
- list_delete( s_list, i );
- }
- ONDEBUG1( dump( msg, MIN(r, mb.dump_size), stdout );
- fprintf( stdout, "to_send=%i\n", msg_len - r ) );
- }
- while ( msg_len -= r );
- }
- /* acknowledge */
- if ( socket != -1 ) {
- if ( list_length( s_list ) > 0 ) {
- if ( how != MB_TWOWAY ) {
- n = sprintf( rsp, "%c%c%u%c%c", MB_STAMP, MB_REPLY, msg_id, 0, 0 );
- mb_write( socket, (u_char *)rsp, n );
- ONDEBUG1( fprintf( stdout, "reply_type=%c msg_id=%u\n", MB_REPLY, msg_id );
- dump( (u_char *)rsp, MIN(n, mb.dump_size), stdout ) );
- }
- }
- else {
- n = sprintf( rsp, "%c%c%u%c", MB_STAMP, MB_NOBODY, msg_id, 0 );
- mb_write( socket, (u_char *)rsp, n );
- ONDEBUG1( fprintf( stdout, "reply_type=%c msg_id=%u\n", MB_NOBODY, msg_id );
- dump( (u_char *)rsp, MIN(n, mb.dump_size), stdout ) );
- }
- }
- /* accouting */
- s_in->n_sent++;
- mb.n_msgs++;
- /* recycle memory */
- list_free( s_list );
- }
- void mb_recv( int socket ) {
- int msg_id, msg_len;
- char rsp[ 64 ];
- int n;
- int s;
- struct mb_toreq *to_r;
- int i, len;
- int r;
- if ( (msg_id = mb_getuint( socket )) < 0 )
- return;
- if ( (msg_len = mb_getuint( socket )) < 0 )
- return;
- ONDEBUG1( fprintf( stdout, " msg_len=%u\n", msg_len ) );
- /* find request */
- for ( i = 0, len = list_length( mb.to_reqs ); i < len; i++ ) {
- to_r = (struct mb_toreq *)list_get( mb.to_reqs, i );
- if ( to_r->r_id == msg_id ) {
- list_delete( mb.to_reqs, i );
- break;
- }
- }
- if ( i < len ) {
- s = to_r->r_sin;
- /* send header */
- if ( s != -1 ) {
- msg_id = to_r->r_msg_id;
- n = sprintf( rsp, "%c%c%u%c%u%c", MB_STAMP, MB_REPLY, msg_id, 0, msg_len, 0 );
- if ( mb_write( s, (u_char *)rsp, n ) < 0 )
- /* lost requester */
- s = -1;
- ONDEBUG1( fprintf( stdout, "reply_type=%c msg_id=%u msg_len=%u\n", MB_REPLY, msg_id, msg_len );
- dump( (u_char *)rsp, MIN(n, mb.dump_size), stdout ) );
- }
- /* recycle memory */
- free( to_r );
- }
- else
- s = -1; /* too late */
- /* read data */
- if ( msg_len > 0 ) {
- u_char *msg = (u_char *)alloca( mb.page_size );
- /* transmit data */
- do {
- n = MIN( msg_len, mb.page_size );
- r = socket != -1 ? mb_read( socket, msg, n ) : n;
- if ( r < 0 ) {
- /* lost responder */
- memset( msg, 0, r = n );
- socket = -1;
- }
- if ( s != -1 ) {
- if ( mb_write( s, msg, n ) < 0 )
- /* lost requester */
- s = -1;
- }
- ONDEBUG1( dump( msg, MIN(n, mb.dump_size) , stdout );
- fprintf( stdout, "to_send=%i\n", msg_len - r ) );
- }
- while ( msg_len -= r );
- }
- }
- static void mb_input( int socket ) {
- int msg_type;
- int c;
- if ( (c = mb_getc( socket )) == MB_STAMP ) {
- ONDEBUG1( fprintf( stdout, "\n%c", c ) );
- msg_type = mb_getc( socket );
- ONDEBUG1( fprintf( stdout, " msg_type=%c", msg_type ) );
- switch ( msg_type ) {
- case MB_ONEWAY:
- case MB_TWOWAY:
- case MB_MULTIWAY:
- mb_send( socket, msg_type );
- return;
- case MB_REPLY:
- mb_recv( socket );
- return;
- case MB_REGISTER:
- mb_register( socket );
- return;
- case MB_PROBE:
- mb_probe( socket );
- return;
- default:
- /* error */
- break;
- }
- }
- /* protocol error */
- mb_drop_socket( socket );
- close( socket );
- }
- int mb_accept( int socket ) {
- int sd;
- union {
- struct sockaddr unix_addr;
- struct sockaddr_in in_addr;
- } in_address;
- socklen_t addrlen = sizeof ( in_address );
- sd = accept( socket, (struct sockaddr *)&in_address, &addrlen );
- if ( sd == -1 ) {
- fprintf( stderr, "%s\n", strerror( errno ) );
- return -1;
- }
- mb_watch_socket( sd );
- return 0;
- }
- int mb_open_unix( ) {
- int sd;
- struct sockaddr sd_address;
- char pathname[ MAXPATHLEN ];
- if ( (sd = socket( PF_UNIX, SOCK_STREAM, 0 )) == -1 ) {
- fprintf( stderr, "%s\n", strerror( errno ) );
- return -1;
- }
- sd_address.sa_family = AF_UNIX;
- sprintf( pathname, "%s/%s", MB_UNIX, mb.port_name );
- (void)strcpy( sd_address.sa_data, pathname );
- if ( bind( sd, &sd_address, sizeof ( sd_address )) != 0 ){
- fprintf( stderr, "%s\n", strerror( errno ) );
- close( sd );
- return -1;
- }
- if ( listen( sd, 5 ) != 0) {
- fprintf( stderr, "%s\n", strerror( errno ) );
- close( sd );
- return -1;
- }
- mb.socket_unix = sd;
- return 0;
- }
- int mb_open_inet( ) {
- int sd;
- struct sockaddr_in sd_address;
- struct servent *serv;
- if ( (serv = getservbyname( mb.port_name, "tcp" )) == 0 ) {
- fprintf( stderr, "Service not found\n" );
- return -1;
- }
- endservent();
- if ( (sd = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) == -1 ) {
- fprintf( stderr, "%s\n", strerror( errno ) );
- return -1;
- }
- sd_address.sin_family = AF_INET;
- sd_address.sin_addr.s_addr = htonl( INADDR_ANY );
- sd_address.sin_port = serv->s_port;
- if ( bind( sd, (struct sockaddr *)&sd_address, sizeof ( sd_address )) != 0 ){
- fprintf( stderr, "%s\n", strerror( errno ) );
- close( sd );
- return -1;
- }
- if ( listen( sd, 5 ) != 0) {
- fprintf( stderr, "%s\n", strerror( errno ) );
- close( sd );
- return -1;
- }
- mb.socket_inet = sd;
- return 0;
- }
- void mb_open( void ) {
- mb_open_unix( );
- mb_open_inet( );
- }
- void mb_close_unix( void ) {
- char pathname[ MAXPATHLEN ];
- if ( mb.socket_unix != -1 ) {
- close( mb.socket_unix );
- mb.socket_unix = -1;
- /* free local address */
- sprintf( pathname, "%s/%s", MB_UNIX, mb.port_name );
- unlink( pathname );
- }
- }
- void mb_close_inet( void ) {
- if ( mb.socket_inet != -1 ) {
- close( mb.socket_inet );
- mb.socket_inet = -1;
- }
- }
- void mb_close( void ) {
- struct mb_socket *s_in;
- while ( (s_in = (struct mb_socket *)list_delete( mb.in_sockets, -1 )) ) {
- close( s_in->s_socket );
- if ( s_in->s_name )
- free( s_in->s_name );
- free( s_in );
- }
- mb_close_inet( );
- mb_close_unix( );
- }
- void mb_intr( int sig ) {
- mb_exit( 1 );
- }
- void mb_exit( int status ) {
- mb_close( );
- exit( status );
- }
- static void startup( ) {
- if ( mb.interactive ) {
- mb.kbd_shell = shell_new( );
- shell_addcmd( mb.kbd_shell, "open", "open [unix | inet]", kbd_open );
- shell_addcmd( mb.kbd_shell, "close", "close [unix | inet]", kbd_close);
- shell_addcmd( mb.kbd_shell, "pagesize", "pagesize [nbytes]", kbd_pagesize);
- shell_addcmd( mb.kbd_shell, "timeout", "timeout [nsecs]", kbd_timeout);
- #if defined( DEBUG )
- shell_addcmd( mb.kbd_shell, "dumpsize", "dumpsize [n_octets]", kbd_dumpsize);
- shell_addcmd( mb.kbd_shell, "debug", "debug [level]", kbd_debug);
- #endif
- shell_addcmd( mb.kbd_shell, "info", "info", kbd_info);
- shell_addcmd( mb.kbd_shell, "exit", "exit | quit", kbd_exit);
- shell_addcmd( mb.kbd_shell, "quit", "quit | exit", kbd_exit);
- #ifdef SIGTTOU
- signal( SIGTTOU, SIG_IGN );
- #endif
- #ifdef SIGTTIN
- signal( SIGTTIN, SIG_IGN );
- #endif
- #ifdef SIGTSTP
- signal( SIGTSTP, SIG_IGN );
- #endif
- signal( SIGPIPE, SIG_IGN ); /* raised if broken stream on write */
- signal( SIGHUP, mb_intr );
- signal( SIGINT, mb_intr );
- signal( SIGTERM, mb_intr );
- fprintf( stdout, "MagicBus %s(%s)\n", MB_VERSION, MB_REVISION);
- fprintf( stdout, "(C)2010 frasq.org.\n" );
- fprintf( stdout, "%s", mb.kbd_prompt );
- fflush( stdout );
- }
- else {
- int pid;
- pid = fork ();
- if ( pid == -1 ) {
- fprintf( stderr, "%s\n", strerror( errno ) );
- exit( 1 );
- }
- if ( pid == 0 )
- /* parent */
- exit( 0 );
- pid = fork ();
- if ( pid == -1 ) {
- fprintf( stderr, "%s\n", strerror( errno ) );
- exit( 1 );
- }
- if ( pid > 0 )
- /* child */
- exit( 0 );
- #ifdef SIGTTOU
- signal( SIGTTOU, SIG_IGN );
- #endif
- #ifdef SIGTTIN
- signal( SIGTTIN, SIG_IGN );
- #endif
- #ifdef SIGTSTP
- signal( SIGTSTP, SIG_IGN );
- #endif
- signal( SIGPIPE, SIG_IGN ); /* raised if broken stream on write */
- signal( SIGHUP, mb_intr );
- signal( SIGINT, mb_intr );
- signal( SIGTERM, mb_intr );
- if ( mb_open_unix( ) == -1 )
- exit( 1 );
- if ( !mb.local )
- if ( mb_open_inet( ) == -1 )
- exit( 1 );
- }
- }
- static void main_loop( ) {
- fd_set read_fds;
- int n_fds;
- int i;
- struct timeval time_out, *tp = 0;
- time_t next_time;
- long delay = 0L;
- struct mb_socket *s_in;
- int nb_in_sockets;
- for ( ;; ) { /* forever */
- FD_ZERO( &read_fds );
- n_fds = FD_SETSIZE;
- nb_in_sockets = list_length( mb.in_sockets );
- if ( mb.interactive )
- FD_SET( 0, &read_fds );
- if ( mb.socket_inet != -1 )
- FD_SET( mb.socket_inet, &read_fds );
- if ( mb.socket_unix != -1 )
- FD_SET( mb.socket_unix, &read_fds );
- for ( i = 0; i < nb_in_sockets; i++ ) {
- s_in = (struct mb_socket *)list_get( mb.in_sockets, i );
- FD_SET( s_in->s_socket, &read_fds );
- }
- switch ( select( n_fds, &read_fds, 0, 0, tp )) {
- case -1: /* trouble */
- if ( errno != EINTR )
- mb_exit( 1 );
- break;
- case 0: /* time out */
- break;
- default: /* event */
- if ( mb.interactive && FD_ISSET( 0, &read_fds ))
- /* from operator */
- shell_input( mb.kbd_shell, mb.kbd_prompt );
- if ( mb.socket_unix != -1 && FD_ISSET( mb.socket_unix, &read_fds ))
- /* from here */
- mb_accept( mb.socket_unix );
- if ( mb.socket_inet != -1 && FD_ISSET( mb.socket_inet, &read_fds ))
- /* from afar */
- mb_accept( mb.socket_inet );
- for ( i = 0; i < list_length( mb.in_sockets ); i++ ) {
- s_in = (struct mb_socket *)list_get( mb.in_sockets, i );
- if ( FD_ISSET( s_in->s_socket, &read_fds ))
- mb_input( s_in->s_socket );
- }
- break;
- }
- /* check timers */
- if( (next_time = mb_check_toreqs( )) ) {
- /* wake up for next timer */
- if( (delay = next_time - time( (time_t *)0 )) < 0 )
- delay = 0; /* just to be sure */
- ONDEBUG2( fprintf( stdout, "\n[delay=%ld]", delay ));
- time_out.tv_sec = delay;
- time_out.tv_usec = 0L;
- tp = &time_out;
- }
- else
- tp = 0;
- }
- }
- int main( int argc, char **argv ) {
- extern int opterr;
- int c;
- mb.local = 0;
- mb.interactive = 0;
- mb.kbd_prompt = MBD_PROMPT;
- mb.port_name = MB_INET;
- mb.socket_inet = -1;
- mb.socket_unix = -1;
- mb.in_sockets = list_new();
- mb.to_reqs = list_new();
- mb.page_size = MB_PAGESIZE;
- mb.timeout = 60;
- mb.dump_size = 16;
- opterr = 0;
- #if defined( DEBUG )
- while ( (c = getopt( argc, argv, "D:d:P:ip:lt:" )) != -1 )
- #else
- while ( (c = getopt( argc, argv, "P:ip:lt:" )) != -1 )
- #endif
- switch ( c ) {
- #if defined( DEBUG )
- case 'D':
- debug = atoi( optarg );
- break;
- case 'd': {
- int dump_size = atoi( optarg );
- if ( dump_size >= 16 )
- mb.dump_size = dump_size;
- break;
- }
- #endif
- case 'P':
- mb.kbd_prompt = optarg;
- break;
- case 'i':
- mb.interactive = 1;
- break;
- case 'p': {
- int page_size = atoi( optarg );
- if ( page_size > 0 )
- mb.page_size = page_size;
- break;
- }
- case 'l':
- mb.local = 1;
- break;
- case 't': {
- int to = atoi( optarg );
- if ( to > 0 )
- mb.timeout = to;
- break;
- }
- case '?':
- default:
- error:
- #if defined( DEBUG )
- fprintf( stderr, "%s [-D level] [-d dump_size] [-P prompt] [-i] [-l] [-p page_size] [-t time_out] [port_name]\n", argv[ 0 ] );
- #else
- fprintf( stderr, "%s [-P prompt] [-i] [-l] [-p page_size] [-t time_out] [port_name]\n", argv[ 0 ] );
- #endif
- exit( 1 );
- }
- switch ( argc - optind ) {
- case 0:
- break;
- case 1:
- mb.port_name = argv[ optind ];
- break;
- default:
- goto error;
- }
- /* check options */
- if ( mb.interactive )
- mb.local = 0;
- #if defined( DEBUG )
- setbuf( stdout, 0 );
- #endif
- startup( );
- main_loop( );
- }
Comments