Added -D option. Fixed segmentation fault when -d is used. Fixed memory leak.

This commit is contained in:
Tatsuhiro Tsujikawa 2012-02-08 02:57:19 +09:00
parent 0e56709a24
commit 6ac7433a5e
1 changed files with 31 additions and 5 deletions

View File

@ -62,10 +62,11 @@ namespace spdylay {
struct Config { struct Config {
std::string htdocs; std::string htdocs;
bool verbose; bool verbose;
bool daemon;
uint16_t port; uint16_t port;
std::string private_key_file; std::string private_key_file;
std::string cert_file; std::string cert_file;
Config(): verbose(false), port(0) {} Config(): verbose(false), daemon(false), port(0) {}
}; };
extern bool ssl_debug; extern bool ssl_debug;
@ -861,7 +862,7 @@ int reactor()
namespace { namespace {
void print_usage(std::ostream& out) void print_usage(std::ostream& out)
{ {
out << "Usage: spdyd [-hv] PORT PRIVATE_KEY CERT" << std::endl; out << "Usage: spdyd [-Ddhv] PORT PRIVATE_KEY CERT" << std::endl;
} }
} // namespace } // namespace
@ -871,32 +872,42 @@ void print_help(std::ostream& out)
print_usage(out); print_usage(out);
out << "\n" out << "\n"
<< "OPTIONS:\n" << "OPTIONS:\n"
<< " -D, --daemon Run in a background. If -D is used, the\n"
<< " current working directory is changed to '/'.\n"
<< " Therefore if this option is used, -d option\n"
<< " must be specified.\n"
<< "\n"
<< " -d, --htdocs=PATH Specify document root. If this option is not\n" << " -d, --htdocs=PATH Specify document root. If this option is not\n"
<< " specified, the document root is the current\n" << " specified, the document root is the current\n"
<< " working directory.\n" << " working directory.\n"
<< "\n" << "\n"
<< " -v, --verbose Print debug information such as reception/\n" << " -v, --verbose Print debug information such as reception/\n"
<< " transmission of frames and name/value pairs.\n" << " transmission of frames and name/value pairs.\n"
<< "\n"
<< " -h, --help Print this help.\n"
<< std::endl; << std::endl;
} }
} // namespace } // namespace
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
config.htdocs = "./";
while(1) { while(1) {
static option long_options[] = { static option long_options[] = {
{"verbose", no_argument, 0, 'v' }, {"daemon", no_argument, 0, 'D' },
{"htdocs", required_argument, 0, 'd' }, {"htdocs", required_argument, 0, 'd' },
{"help", no_argument, 0, 'h' }, {"help", no_argument, 0, 'h' },
{"verbose", no_argument, 0, 'v' },
{0, 0, 0, 0 } {0, 0, 0, 0 }
}; };
int option_index = 0; int option_index = 0;
int c = getopt_long(argc, argv, "dhv", long_options, &option_index); int c = getopt_long(argc, argv, "Dd:hv", long_options, &option_index);
if(c == -1) { if(c == -1) {
break; break;
} }
switch(c) { switch(c) {
case 'D':
config.daemon = true;
break;
case 'd': case 'd':
config.htdocs = optarg; config.htdocs = optarg;
break; break;
@ -917,6 +928,20 @@ int main(int argc, char **argv)
std::cerr << "Too few arguments" << std::endl; std::cerr << "Too few arguments" << std::endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if(config.daemon) {
if(config.htdocs.empty()) {
print_usage(std::cerr);
std::cerr << "-d option must be specified when -D is used." << std::endl;
exit(EXIT_FAILURE);
}
if(daemon(0, 0) == -1) {
perror("daemon");
exit(EXIT_FAILURE);
}
}
if(config.htdocs.empty()) {
config.htdocs = "./";
}
struct sigaction act; struct sigaction act;
memset(&act, 0, sizeof(struct sigaction)); memset(&act, 0, sizeof(struct sigaction));
act.sa_handler = SIG_IGN; act.sa_handler = SIG_IGN;
@ -935,6 +960,7 @@ int main(int argc, char **argv)
proto_list[0] = 6; proto_list[0] = 6;
memcpy(&proto_list[1], "spdy/2", 6); memcpy(&proto_list[1], "spdy/2", 6);
reactor(); reactor();
delete [] proto_list;
return 0; return 0;
} }