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 "vpn-manager/service_manager.h" | |
6 | |
7 #include <stdlib.h> | |
8 | |
9 #include "base/eintr_wrapper.h" | |
10 #include "base/file_util.h" | |
11 #include "base/logging.h" | |
12 #include "base/string_util.h" | |
13 | |
14 FilePath* ServiceManager::temp_path_ = NULL; | |
15 | |
16 const char kTempDirectory[] = "/home/chronos/user/tmp/l2tpipsec"; | |
17 | |
18 ServiceManager::ServiceManager(const std::string& service_name) | |
19 : is_running_(false), | |
20 was_stopped_(false), | |
21 inner_service_(NULL), | |
22 outer_service_(NULL), | |
23 service_name_(service_name) { | |
24 } | |
25 | |
26 ServiceManager::~ServiceManager() { | |
27 } | |
28 | |
29 void ServiceManager::OnStarted() { | |
30 CHECK(!is_running_ && !was_stopped_); | |
31 CHECK(outer_service_ == NULL || outer_service_->is_running_); | |
32 is_running_ = true; | |
33 if (inner_service_ == NULL) | |
34 return; | |
35 | |
36 DLOG(INFO) << "Starting inner " << inner_service_->service_name(); | |
37 if (!inner_service_->Start()) { | |
38 // Inner service could not be started, stop this layer. | |
39 LOG(ERROR) << "Inner service " << inner_service_->service_name() | |
40 << " failed. Stopping " << service_name(); | |
41 Stop(); | |
42 } | |
43 } | |
44 | |
45 void ServiceManager::OnStopped(bool was_error) { | |
46 CHECK(inner_service_ == NULL || !inner_service_->is_running_); | |
47 is_running_ = false; | |
48 was_stopped_ = true; | |
49 if (outer_service_ != NULL) { | |
50 outer_service_->Stop(); | |
51 } | |
52 } | |
53 | |
54 void ServiceManager::InitializeDirectories() { | |
55 if (!temp_path_) | |
56 temp_path_ = new FilePath(kTempDirectory); | |
Will Drewry
2011/03/05 04:06:39
Would it make sense to do the converse:
if (temp_
kmixter1
2011/03/11 01:34:27
Done.
| |
57 CHECK(file_util::CreateDirectory(*temp_path_)) | |
Will Drewry
2011/03/05 04:06:39
Any reason not to use a ScopedTempDir()?
If not,
kmixter1
2011/03/11 01:34:27
This pointer nastiness is related to Darin mention
| |
58 << "Unable to create " << temp_path_->value(); | |
59 atexit(ServiceManager::DeleteTemp); | |
60 } | |
61 | |
62 void ServiceManager::DeleteTemp() { | |
63 if (!temp_path_) | |
64 return; | |
65 file_util::Delete(*temp_path_, true); | |
66 delete temp_path_; | |
67 temp_path_ = NULL; | |
68 } | |
69 | |
70 void ServiceManager::WriteFdToSyslog(int fd, const std::string& prefix) { | |
71 char buffer[256]; | |
72 // Note that short reads and reads of over sizeof(buffer) amount of | |
73 // data are going to split into different syslog lines. | |
74 // TODO: buffer up incomplete lines before submitting. | |
75 int written = HANDLE_EINTR(read(fd, &buffer, sizeof(buffer) - 1)); | |
Will Drewry
2011/03/05 04:06:39
nit: read returns a ssize_t which can cause proble
kmixter1
2011/03/11 01:34:27
Thanks!
| |
76 if (written < 0) { | |
77 LOG(WARNING) << "Error condition on " << prefix << " pipe"; | |
78 return; | |
79 } | |
80 buffer[written] = '\0'; | |
81 std::string trimmed_contents; | |
82 TrimWhitespaceASCII(buffer, TRIM_TRAILING, &trimmed_contents); | |
83 std::vector<std::string> lines; | |
84 SplitString(trimmed_contents, '\n', &lines); | |
85 for (size_t i = 0; i < lines.size(); ++i) | |
Will Drewry
2011/03/05 04:06:39
style side note: seems like a vector iterator is m
kmixter1
2011/03/11 01:34:27
I can see that, but I usually prefer to use C styl
| |
86 LOG(INFO) << prefix << lines[i]; | |
87 } | |
OLD | NEW |