/* main() for udpxrec module */ int udpxrec_main( int argc, char* const argv[] ) { int rc = 0, ch = 0, custom_log = 0, no_daemon = 0; static const char OPTMASK[] = "vb:e:M:p:B:n:m:l:c:R:u:T"; time_t now = time(NULL); char now_buf[ 32 ] = {0}, sel_buf[ 32 ] = {0}, app_finfo[80] = {0}; extern int optind, optopt; extern const char IPv4_ALL[]; mk_app_info(g_udpxrec_app, g_app_info, sizeof(g_app_info) - 1); if( argc < 2 ) { usage( argv[0], stderr ); return ERR_PARAM; } rc = init_recopt( &g_recopt ); while( (0 == rc) && (-1 != (ch = getopt( argc, argv, OPTMASK ))) ) { switch(ch) { case 'T': no_daemon = 1; break; case 'v': set_verbose( &g_recopt.is_verbose ); break; case 'b': if( (time_t)0 != g_recopt.end_time ) { (void) fprintf( stderr, "Cannot specify start-recording " "time after end-recording time has been set\n" ); } rc = a2time( optarg, &g_recopt.bg_time, time(NULL) ); if( 0 != rc ) { (void) fprintf( stderr, "Invalid time: [%s]\n", optarg ); rc = ERR_PARAM; } else { if( g_recopt.bg_time < now ) { (void)strncpy( now_buf, Zasctime(localtime( &now )), sizeof(now_buf) ); (void)strncpy( sel_buf, Zasctime(localtime( &g_recopt.bg_time )), sizeof(sel_buf) ); (void) fprintf( stderr, "Selected %s time is in the past, " "now=[%s], selected=[%s]\n", "start", now_buf, sel_buf ); rc = ERR_PARAM; } } break; case 'e': if( (time_t)0 == g_recopt.bg_time ) { g_recopt.bg_time = time(NULL); (void)fprintf( stderr, "Start-recording time defaults to now [%s]\n", Zasctime( localtime( &g_recopt.bg_time ) ) ); } rc = a2time( optarg, &g_recopt.end_time, g_recopt.bg_time ); if( 0 != rc ) { (void) fprintf( stderr, "Invalid time: [%s]\n", optarg ); rc = ERR_PARAM; } else { if( g_recopt.end_time < now ) { (void)strncpy( now_buf, Zasctime(localtime( &now )), sizeof(now_buf) ); (void)strncpy( sel_buf, Zasctime(localtime( &g_recopt.end_time )), sizeof(sel_buf) ); (void) fprintf( stderr, "Selected %s time is in the past, " "now=[%s], selected=[%s]\n", "end", now_buf, sel_buf ); rc = ERR_PARAM; } } break; case 'M': rc = a2int64( optarg, &g_recopt.max_fsize ); if( 0 != rc ) { (void) fprintf( stderr, "Invalid file size: [%s]\n", optarg ); rc = ERR_PARAM; } break; case 'p': g_recopt.pidfile = strdup(optarg); break; case 'B': rc = a2size( optarg, &g_recopt.bufsize ); if( 0 != rc ) { (void) fprintf( stderr, "Invalid buffer size: [%s]\n", optarg ); rc = ERR_PARAM; } else if( (g_recopt.bufsize < MIN_MCACHE_LEN) || (g_recopt.bufsize > MAX_MCACHE_LEN)) { (void) fprintf( stderr, "Buffer size must be in [%ld-%ld] bytes range\n", (long)MIN_MCACHE_LEN, (long)MAX_MCACHE_LEN ); rc = ERR_PARAM; } break; case 'n': g_recopt.nice_incr = atoi( optarg ); if( 0 == g_recopt.nice_incr ) { (void) fprintf( stderr, "Invalid nice-value increment: [%s]\n", optarg ); rc = ERR_PARAM; } break; case 'm': rc = get_ipv4_address( optarg, g_recopt.mcast_addr, sizeof(g_recopt.mcast_addr) ); if( 0 != rc ) { (void) fprintf( stderr, "Invalid multicast address: [%s]\n", optarg ); rc = ERR_PARAM; } break; case 'l': g_flog = fopen( optarg, "a" ); if( NULL == g_flog ) { rc = errno; (void) fprintf( stderr, "Error opening logfile [%s]: %s\n", optarg, strerror(rc) ); rc = ERR_PARAM; break; } Setlinebuf( g_flog ); custom_log = 1; break; case 'c': rc = get_addrport( optarg, g_recopt.rec_channel, sizeof( g_recopt.rec_channel ), &g_recopt.rec_port ); if( 0 != rc ) rc = ERR_PARAM; break; case 'R': g_recopt.rbuf_msgs = atoi( optarg ); if( (g_recopt.rbuf_msgs <= 0) && (-1 != g_recopt.rbuf_msgs) ) { (void) fprintf( stderr, "Invalid rcache size: [%s]\n", optarg ); rc = ERR_PARAM; } break; case 'u': g_recopt.waitupd_sec = atoi(optarg); if( g_recopt.waitupd_sec <= 0 ) { (void) fprintf( stderr, "Invalid wait-update value [%s] " "(must be a number > 0)\n", optarg ); rc = ERR_PARAM; } break; case ':': (void) fprintf( stderr, "Option [-%c] requires an argument\n", optopt ); rc = ERR_PARAM; break; case '?': (void) fprintf( stderr, "Unrecognized option: [-%c]\n", optopt ); rc = ERR_PARAM; break; default: usage( argv[0], stderr ); rc = ERR_PARAM; break; } /* switch */ } /* while getopt */ if( 0 == rc ) { if( optind >= argc ) { (void) fputs( "Missing destination file parameter\n", stderr ); rc = ERR_PARAM; } else { g_recopt.dstfile = strdup( argv[optind] ); } if( !(g_recopt.max_fsize > 0 || g_recopt.end_time) ) { (void) fputs( "Must specify either max file [-M] size " "or end time [-e]\n", stderr ); rc = ERR_PARAM; } if( !g_recopt.rec_channel[0] || !g_recopt.rec_port ) { (void) fputs( "Must specify multicast channel to record from\n", stderr ); rc = ERR_PARAM; } } if( rc ) { free_recopt( &g_recopt ); return rc; } do { if( '\0' == g_recopt.mcast_addr[0] ) { (void) strncpy( g_recopt.mcast_addr, IPv4_ALL, sizeof(g_recopt.mcast_addr) - 1 ); } if( !custom_log ) { /* in debug mode output goes to stderr, otherwise to /dev/null */ g_flog = ((uf_TRUE == g_recopt.is_verbose) ? stderr : fopen( "/dev/null", "a" )); if( NULL == g_flog ) { perror("fopen"); rc = ERR_INTERNAL; break; } } if( 0 == geteuid() ) { if( !no_daemon ) { if( stderr == g_flog ) { (void) fprintf( stderr, "Logfile must be specified to run " "in verbose mode in background\n" ); rc = ERR_PARAM; break; } if( NULL == g_recopt.pidfile ) { (void) fprintf( stderr, "pidfile must be specified " "to run as daemon\n" ); rc = ERR_PARAM; break; } if( 0 != (rc = daemonize(0, g_flog)) ) { rc = ERR_INTERNAL; break; } } } /* 0 == geteuid() */ if( NULL != g_recopt.pidfile ) { rc = make_pidfile( g_recopt.pidfile, getpid(), g_flog ); if( 0 != rc ) break; } (void) set_nice( g_recopt.nice_incr, g_flog ); if( 0 != (rc = setup_signals()) ) break; TRACE( fprint_recopt( g_flog, &g_recopt ) ); TRACE( printcmdln( g_flog, g_app_info, argc, argv ) ); if( g_recopt.bg_time ) { if( 0 != (rc = verify_channel()) || g_quit ) break; rc = wait_till( g_recopt.bg_time, g_recopt.waitupd_sec ); if( rc || g_quit ) break; } rc = record(); if( NULL != g_recopt.pidfile ) { if( -1 == unlink(g_recopt.pidfile) ) { mperror( g_flog, errno, "unlink [%s]", g_recopt.pidfile ); } } } while(0); if( g_flog ) { (void)tmfprintf( g_flog, "%s is exiting with rc=[%d]\n", app_finfo, rc ); } if( g_flog && (stderr != g_flog) ) { (void) fclose(g_flog); } free_recopt( &g_recopt ); return rc; }
/* generate service's (HTML) status page */ int mk_status_page( const struct server_ctx* ctx, char* buf, size_t* len, int options ) { int n = -1; prbuf_t pb = NULL; time_t tm_now = time(NULL); const char* str_now = Zasctime(localtime(&tm_now)); size_t num_clients = 0, i = 0; char* client_text = NULL; const char* page_format = NULL; const unsigned delay_msec = 3000; size_t text_size = 0, HEADER_SIZE = 0; static size_t MIN_PER_CLI = 80 + (3 * (IPADDR_STR_SIZE + 5) ); extern const char UDPXY_COPYRIGHT_NOTICE[]; extern const char COMPILE_MODE[]; extern const char VERSION[]; extern const int BUILDNUM; assert( ctx && buf && len ); do { n = prbuf_open( &pb, buf, *len ); if( -1 == n ) break; if( options & MSO_HTTP_HEADER ) { n = prbuf_printf( pb, "%s", HTML_PAGE_HEADER ); if( n <= 0 ) break; } if( options & MSO_RESTART ) { n = prbuf_printf( pb, REDIRECT_SCRIPT_FMT, delay_msec ); if( n <= 0 ) break; } for( i = 0, n = -1; n && (i < LEN_STAT_PAGE_BASE); ++i ) { n = prbuf_printf( pb, "%s", STAT_PAGE_BASE[i] ); } if( n <= 0 ) break; for(text_size = 0, num_clients = 0, i = 0; i < ctx->clmax; ++i ) { if( ctx->cl[i].pid > 0 ) { ++num_clients; text_size += MIN_PER_CLI + strlen(ctx->cl[i].tail); } } if( ! (options & MSO_SKIP_CLIENTS) ) { if( num_clients > (size_t)0 ) { HEADER_SIZE = strlen(ACLIENT_TABLE[0]); HEADER_SIZE += strlen(ACLIENT_TABLE[1]); text_size += HEADER_SIZE; if (text_size > *len) { (void) tmfprintf(g_flog, "Client portion of memory [%ld bytes] > " "[%ld bytes] of the page buffer.\n", (long)text_size, (long)*len); n = -1; break; } client_text = malloc( text_size ); if( NULL == client_text ) { mperror( g_flog, errno, "%s: calloc", __func__ ); n = -1; break; } n = mk_client_entries( ctx, client_text, &text_size ); if( -1 == n ) break; } } /* MSO_SKIP_CLIENTS */ page_format = ( options & MSO_RESTART ) ? RST_PAGE_FMT1 : STAT_PAGE_FMT1; n = prbuf_printf( pb, page_format, getpid(), ctx->listen_addr, ctx->listen_port, ctx->mcast_ifc_addr, num_clients, (client_text ? client_text : ""), REQUEST_GUIDE, VERSION, BUILDNUM, COMPILE_MODE, str_now, UDPXY_COPYRIGHT_NOTICE ); if( -1 == n ) break; } while(0); if( -1 == n ) { (void)tmfprintf( g_flog, "Error preparing status HTML: " "insufficient memory size=[%lu]\n", (u_long)(*len)); } else { *len = prbuf_len( pb ); } free( client_text ); (void) prbuf_close( pb ); return (n > 0 ? 0 : -1); }