Chromium Code Reviews| Index: service_manager.cc |
| diff --git a/service_manager.cc b/service_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6de112a6ae18373ac6b1c25c8109147338080341 |
| --- /dev/null |
| +++ b/service_manager.cc |
| @@ -0,0 +1,87 @@ |
| +// Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "vpn-manager/service_manager.h" |
| + |
| +#include <stdlib.h> |
| + |
| +#include "base/eintr_wrapper.h" |
| +#include "base/file_util.h" |
| +#include "base/logging.h" |
| +#include "base/string_util.h" |
| + |
| +FilePath* ServiceManager::temp_path_ = NULL; |
| + |
| +const char kTempDirectory[] = "/home/chronos/user/tmp/l2tpipsec"; |
| + |
| +ServiceManager::ServiceManager(const std::string& service_name) |
| + : is_running_(false), |
| + was_stopped_(false), |
| + inner_service_(NULL), |
| + outer_service_(NULL), |
| + service_name_(service_name) { |
| +} |
| + |
| +ServiceManager::~ServiceManager() { |
| +} |
| + |
| +void ServiceManager::OnStarted() { |
| + CHECK(!is_running_ && !was_stopped_); |
| + CHECK(outer_service_ == NULL || outer_service_->is_running_); |
| + is_running_ = true; |
| + if (inner_service_ == NULL) |
| + return; |
| + |
| + DLOG(INFO) << "Starting inner " << inner_service_->service_name(); |
| + if (!inner_service_->Start()) { |
| + // Inner service could not be started, stop this layer. |
| + LOG(ERROR) << "Inner service " << inner_service_->service_name() |
| + << " failed. Stopping " << service_name(); |
| + Stop(); |
| + } |
| +} |
| + |
| +void ServiceManager::OnStopped(bool was_error) { |
| + CHECK(inner_service_ == NULL || !inner_service_->is_running_); |
| + is_running_ = false; |
| + was_stopped_ = true; |
| + if (outer_service_ != NULL) { |
| + outer_service_->Stop(); |
| + } |
| +} |
| + |
| +void ServiceManager::InitializeDirectories() { |
| + if (!temp_path_) |
| + 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.
|
| + 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
|
| + << "Unable to create " << temp_path_->value(); |
| + atexit(ServiceManager::DeleteTemp); |
| +} |
| + |
| +void ServiceManager::DeleteTemp() { |
| + if (!temp_path_) |
| + return; |
| + file_util::Delete(*temp_path_, true); |
| + delete temp_path_; |
| + temp_path_ = NULL; |
| +} |
| + |
| +void ServiceManager::WriteFdToSyslog(int fd, const std::string& prefix) { |
| + char buffer[256]; |
| + // Note that short reads and reads of over sizeof(buffer) amount of |
| + // data are going to split into different syslog lines. |
| + // TODO: buffer up incomplete lines before submitting. |
| + 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!
|
| + if (written < 0) { |
| + LOG(WARNING) << "Error condition on " << prefix << " pipe"; |
| + return; |
| + } |
| + buffer[written] = '\0'; |
| + std::string trimmed_contents; |
| + TrimWhitespaceASCII(buffer, TRIM_TRAILING, &trimmed_contents); |
| + std::vector<std::string> lines; |
| + SplitString(trimmed_contents, '\n', &lines); |
| + 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
|
| + LOG(INFO) << prefix << lines[i]; |
| +} |