| Index: service_manager.cc
|
| diff --git a/service_manager.cc b/service_manager.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..3112631847d6b792e2ea5c28ec80ba261af6d29a
|
| --- /dev/null
|
| +++ b/service_manager.cc
|
| @@ -0,0 +1,91 @@
|
| +// 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);
|
| + CHECK(file_util::CreateDirectory(*temp_path_))
|
| + << "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,
|
| + std::string* partial_line) {
|
| + char buffer[256];
|
| + int written = HANDLE_EINTR(read(fd, &buffer, sizeof(buffer) - 1));
|
| + if (written < 0) {
|
| + LOG(WARNING) << "Error condition on " << prefix << " pipe";
|
| + return;
|
| + }
|
| + buffer[written] = '\0';
|
| + partial_line->append(buffer);
|
| + std::vector<std::string> lines;
|
| + SplitString(*partial_line, '\n', &lines);
|
| + if (lines.empty()) {
|
| + partial_line->clear();
|
| + } else {
|
| + *partial_line = lines.back();
|
| + lines.pop_back();
|
| + }
|
| + for (size_t i = 0; i < lines.size(); ++i)
|
| + LOG(INFO) << prefix << lines[i];
|
| +}
|
|
|