OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <poll.h> | |
6 #include <signal.h> | |
7 #include <stddef.h> | |
8 #include <sys/wait.h> | |
9 | |
10 #include <string> | |
11 | |
12 #include "base/command_line.h" | |
13 #include "base/logging.h" | |
14 #include "base/scoped_temp_dir.h" | |
15 #include "chromeos/process.h" | |
16 #include "chromeos/syslog_logging.h" | |
17 #include "gflags/gflags.h" | |
18 #include "vpn-manager/ipsec_manager.h" | |
petkov
2011/03/11 19:12:53
blank line before
| |
19 #include "vpn-manager/l2tp_manager.h" | |
20 | |
21 #pragma GCC diagnostic ignored "-Wstrict-aliasing" | |
22 DEFINE_string(client_cert_file, "", "File with IPsec client certificate"); | |
23 DEFINE_string(client_key_file, "", "File with IPsec client private key"); | |
24 DEFINE_string(psk_file, "", "File with IPsec pre-shared key"); | |
25 DEFINE_string(remote_address, "", "VPN server address"); | |
26 DEFINE_string(server_ca_file, "", "File with IPsec server CA"); | |
27 #pragma GCC diagnostic error "-Wstrict-aliasing" | |
28 | |
29 // True if a signal has requested termination. | |
30 static bool s_terminate_request = false; | |
31 | |
32 void HandleSignal(int sig_num) { | |
33 LOG(INFO) << "Caught signal " << sig_num; | |
34 switch(sig_num) { | |
35 case SIGTERM: | |
36 case SIGINT: | |
37 s_terminate_request = true; | |
38 break; | |
39 case SIGALRM: | |
40 break; | |
41 } | |
42 } | |
43 | |
44 static void InstallSignalHandlers() { | |
45 struct sigaction sa; | |
46 memset(&sa, 0, sizeof(sa)); | |
47 sa.sa_handler = HandleSignal; | |
48 sigaction(SIGTERM, &sa, NULL); | |
49 sigaction(SIGINT, &sa, NULL); | |
50 sigaction(SIGALRM, &sa, NULL); | |
51 } | |
52 | |
53 static void LockDownUmask() { | |
54 // Only user and group may access configuration files we create. | |
55 umask(S_IWGRP | S_IROTH | S_IWOTH); | |
56 } | |
57 | |
58 // Run the main event loop. The events to handle are: | |
59 // 1) timeout from poll | |
60 // 2) caught signal | |
61 // 3) stdout/err of child process ready | |
62 // 4) child process dies | |
63 static void RunEventLoop(IpsecManager* ipsec, L2tpManager* l2tp) { | |
64 do { | |
65 int status; | |
66 int ipsec_poll_timeout = ipsec->Poll(); | |
67 int l2tp_poll_timeout = l2tp->Poll(); | |
68 int poll_timeout = (ipsec_poll_timeout > l2tp_poll_timeout) ? | |
69 ipsec_poll_timeout : l2tp_poll_timeout; | |
70 | |
71 const int poll_input_count = 2; | |
72 struct pollfd poll_inputs[poll_input_count] = { | |
73 { ipsec->output_fd(), POLLIN }, // ipsec output | |
74 { l2tp->output_fd(), POLLIN } // l2tp output | |
75 }; | |
76 int poll_result = poll(poll_inputs, poll_input_count, poll_timeout); | |
77 if (poll_result < 0 && errno != EINTR) { | |
78 int saved_errno = errno; | |
79 LOG(ERROR) << "Unexpected poll error: " << saved_errno; | |
80 return; | |
81 } | |
82 | |
83 // Check if there are any child processes to be reaped without | |
84 // blocking. | |
85 pid_t pid = waitpid(-1, &status, WNOHANG); | |
86 if (pid > 0 && (ipsec->IsChild(pid) || l2tp->IsChild(pid))) { | |
87 LOG(WARNING) << "Child process " << pid << " stopped early"; | |
88 s_terminate_request = true; | |
89 } | |
90 | |
91 if (poll_inputs[0].revents & POLLIN) | |
92 ipsec->ProcessOutput(); | |
93 if (poll_inputs[1].revents & POLLIN) | |
94 l2tp->ProcessOutput(); | |
95 } while(!ipsec->was_stopped() && !s_terminate_request); | |
96 } | |
97 | |
98 int main(int argc, char* argv[]) { | |
99 ScopedTempDir temp_dir; | |
100 CommandLine::Init(argc, argv); | |
101 google::ParseCommandLineFlags(&argc, &argv, true); | |
102 int log_flags = chromeos::kLogToSyslog; | |
103 if (isatty(STDOUT_FILENO)) log_flags |= chromeos::kLogToStderr; | |
104 chromeos::InitLog(log_flags); | |
105 chromeos::OpenLog("l2tpipsec_vpn", true); | |
106 IpsecManager ipsec; | |
107 L2tpManager l2tp; | |
108 | |
109 LockDownUmask(); | |
110 | |
111 ServiceManager::InitializeDirectories(&temp_dir); | |
112 | |
113 if (!ipsec.Initialize(1, | |
114 FLAGS_remote_address, | |
115 FLAGS_psk_file, | |
116 FLAGS_server_ca_file, | |
117 FLAGS_client_key_file, | |
118 FLAGS_client_cert_file)) { | |
119 return 1; | |
120 } | |
121 if (!l2tp.Initialize(FLAGS_remote_address)) { | |
122 return 1; | |
123 } | |
124 ServiceManager::SetLayerOrder(&ipsec, &l2tp); | |
125 | |
126 InstallSignalHandlers(); | |
127 CHECK(ipsec.Start()) << "Unable to start IPsec layer"; | |
128 | |
129 RunEventLoop(&ipsec, &l2tp); | |
130 | |
131 LOG(INFO) << "Shutting down..."; | |
132 l2tp.Stop(); | |
133 return 0; | |
134 } | |
OLD | NEW |