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 "base/eintr_wrapper.h" |
| 8 #include "base/file_util.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/scoped_temp_dir.h" |
| 11 #include "base/string_util.h" |
| 12 |
| 13 const FilePath* ServiceManager::temp_path_ = NULL; |
| 14 const char* ServiceManager::temp_base_path_ = "/home/chronos/user/tmp"; |
| 15 |
| 16 ServiceManager::ServiceManager(const std::string& service_name) |
| 17 : is_running_(false), |
| 18 was_stopped_(false), |
| 19 inner_service_(NULL), |
| 20 outer_service_(NULL), |
| 21 service_name_(service_name) { |
| 22 } |
| 23 |
| 24 ServiceManager::~ServiceManager() { |
| 25 } |
| 26 |
| 27 void ServiceManager::OnStarted() { |
| 28 CHECK(!is_running_ && !was_stopped_); |
| 29 CHECK(outer_service_ == NULL || outer_service_->is_running_); |
| 30 is_running_ = true; |
| 31 if (inner_service_ == NULL) |
| 32 return; |
| 33 |
| 34 DLOG(INFO) << "Starting inner " << inner_service_->service_name(); |
| 35 if (!inner_service_->Start()) { |
| 36 // Inner service could not be started, stop this layer. |
| 37 LOG(ERROR) << "Inner service " << inner_service_->service_name() |
| 38 << " failed. Stopping " << service_name(); |
| 39 Stop(); |
| 40 } |
| 41 } |
| 42 |
| 43 void ServiceManager::OnStopped(bool was_error) { |
| 44 CHECK(inner_service_ == NULL || !inner_service_->is_running_); |
| 45 is_running_ = false; |
| 46 was_stopped_ = true; |
| 47 if (outer_service_ != NULL) { |
| 48 outer_service_->Stop(); |
| 49 } |
| 50 } |
| 51 |
| 52 void ServiceManager::InitializeDirectories(ScopedTempDir* scoped_temp_path) { |
| 53 scoped_temp_path->CreateUniqueTempDirUnderPath(FilePath(temp_base_path_)); |
| 54 temp_path_ = &scoped_temp_path->path(); |
| 55 LOG(INFO) << "Using temporary directory " << temp_path_->value(); |
| 56 } |
| 57 |
| 58 void ServiceManager::WriteFdToSyslog(int fd, |
| 59 const std::string& prefix, |
| 60 std::string* partial_line) { |
| 61 char buffer[256]; |
| 62 ssize_t written = HANDLE_EINTR(read(fd, &buffer, sizeof(buffer) - 1)); |
| 63 if (written < 0) { |
| 64 LOG(WARNING) << "Error condition on " << prefix << " pipe"; |
| 65 return; |
| 66 } |
| 67 buffer[written] = '\0'; |
| 68 partial_line->append(buffer); |
| 69 std::vector<std::string> lines; |
| 70 SplitString(*partial_line, '\n', &lines); |
| 71 if (lines.empty()) { |
| 72 partial_line->clear(); |
| 73 } else { |
| 74 *partial_line = lines.back(); |
| 75 lines.pop_back(); |
| 76 } |
| 77 for (size_t i = 0; i < lines.size(); ++i) { |
| 78 LOG(INFO) << prefix << lines[i]; |
| 79 } |
| 80 } |
OLD | NEW |