forked from ravinet/mahimahi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkshell.cc
89 lines (72 loc) · 2.63 KB
/
linkshell.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* -*-mode:c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
#include <getopt.h>
#include "link_queue.hh"
#include "packetshell.cc"
using namespace std;
void usage_error( const string & program_name )
{
throw Exception( "Usage", program_name + " [--uplink-log=FILENAME] [--downlink-log=FILENAME] [--once] UPLINK DOWNLINK [COMMAND...]" );
}
int main( int argc, char *argv[] )
{
try {
/* clear environment while running as root */
char ** const user_environment = environ;
environ = nullptr;
check_requirements( argc, argv );
if ( argc < 3 ) {
usage_error( argv[ 0 ] );
}
const option command_line_options[] = {
{ "uplink-log", required_argument, nullptr, 'u' },
{ "downlink-log", required_argument, nullptr, 'd' },
{ "once", no_argument, nullptr, 'o' },
{ 0, 0, nullptr, 0 }
};
string uplink_logfile, downlink_logfile;
bool repeat = true;
while ( true ) {
const int opt = getopt_long( argc, argv, "u:d:", command_line_options, nullptr );
if ( opt == -1 ) { /* end of options */
break;
}
switch ( opt ) {
case 'u':
uplink_logfile = optarg;
break;
case 'd':
downlink_logfile = optarg;
break;
case 'o':
repeat = false;
break;
case '?':
usage_error( argv[ 0 ] );
break;
default:
throw Exception( "getopt_log", "unexpected return value " + to_string( opt ) );
}
}
if ( optind + 1 >= argc ) {
usage_error( argv[ 0 ] );
}
const std::string uplink_filename = argv[ optind ];
const std::string downlink_filename = argv[ optind + 1 ];
vector< string > command;
if ( optind + 2 == argc ) {
command.push_back( shell_path() );
} else {
for ( int i = optind + 2; i < argc; i++ ) {
command.push_back( argv[ i ] );
}
}
PacketShell<LinkQueue> link_shell_app( "link" );
link_shell_app.start_uplink( "[link] ", user_environment, command,
uplink_filename, uplink_logfile, repeat );
link_shell_app.start_downlink( downlink_filename, downlink_logfile, repeat );
return link_shell_app.wait_for_exit();
} catch ( const Exception & e ) {
e.perror();
return EXIT_FAILURE;
}
}