Chromium Code Reviews| 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_; | |
| 15 | |
| 16 const char kTempDirectory[] = "/home/chronos/user/tmp/l2tpipsec"; | |
| 17 | |
| 18 ServiceManager::ServiceManager(const std::string& service_name) | |
| 19 : is_running_(false), was_stopped_(false), inner_service_(NULL), | |
|
petkov
2011/03/04 18:42:56
each initializer should be on a separate line, no?
kmixter1
2011/03/05 02:48:59
Done.
| |
| 20 outer_service_(NULL), service_name_(service_name) { | |
| 21 } | |
| 22 | |
| 23 ServiceManager::~ServiceManager() { | |
| 24 } | |
| 25 | |
| 26 void ServiceManager::OnStarted() { | |
| 27 is_running_ = true; | |
|
petkov
2011/03/04 18:42:56
CHECK(!is_running_ && !was_stopped)?
kmixter1
2011/03/05 02:48:59
Done.
| |
| 28 if (inner_service_ == NULL) | |
| 29 return; | |
| 30 | |
| 31 LOG(INFO) << "Starting inner " << inner_service_->service_name(); | |
| 32 if (!inner_service_->Start()) { | |
|
petkov
2011/03/04 18:42:56
Hmm, so you always start the chain from the outerm
kmixter1
2011/03/05 02:48:59
Yes - start from the outermost and stop from the i
| |
| 33 // Inner service could not be started, stop this layer. | |
| 34 LOG(ERROR) << "Inner service " << inner_service_->service_name() | |
| 35 << " failed. Stopping " << service_name(); | |
| 36 Stop(); | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 void ServiceManager::OnStopped(bool was_error) { | |
| 41 is_running_ = false; | |
|
petkov
2011/03/04 18:42:56
CHECK(is_running && !was_stopped)?
kmixter1
2011/03/05 02:48:59
It's ok to stop an already stopped service, see ne
| |
| 42 was_stopped_ = true; | |
| 43 if (outer_service_ != NULL) { | |
| 44 outer_service_->Stop(); | |
| 45 } | |
| 46 } | |
| 47 | |
| 48 void ServiceManager::Initialize() { | |
| 49 if (temp_path_.value().empty()) { | |
| 50 temp_path_ = FilePath(kTempDirectory); | |
| 51 } | |
| 52 FilePath base = temp_path_.DirName(); | |
|
petkov
2011/03/04 18:42:56
why do you split it into base and the rest? Create
kmixter1
2011/03/05 02:48:59
No kidding? I didn't know it was so kind. See si
| |
| 53 if (!file_util::DirectoryExists(base)) { | |
| 54 CHECK(file_util::CreateDirectory(base)) | |
| 55 << "Unable to create " << base.value(); | |
| 56 } | |
| 57 if (!file_util::DirectoryExists(temp_path_)) { | |
| 58 CHECK(file_util::CreateDirectory(temp_path_)) | |
| 59 << "Unable to create " << temp_path_.value(); | |
| 60 } | |
| 61 atexit(ServiceManager::DeleteTemp); | |
| 62 } | |
| 63 | |
| 64 void ServiceManager::DeleteTemp() { | |
| 65 file_util::Delete(temp_path_, true); | |
| 66 } | |
| 67 | |
| 68 void ServiceManager::WriteFdToSyslog(int fd, const char* prefix) { | |
| 69 char buffer[256]; | |
| 70 // Note that short reads and reads of over sizeof(buffer) amount of | |
| 71 // data are going to split into different syslog lines. Seems ok | |
|
petkov
2011/03/04 18:42:56
how is this ensured, by multiple calls to this met
kmixter1
2011/03/05 02:48:59
Right now the code does arbitrarily split lines.
| |
| 72 // for now. | |
| 73 int written = HANDLE_EINTR(read(fd, &buffer, sizeof(buffer) - 1)); | |
| 74 if (written < 0) { | |
| 75 LOG(WARNING) << "Error condition on " << prefix << " pipe"; | |
| 76 return; | |
| 77 } | |
| 78 buffer[written] = '\0'; | |
| 79 std::string trimmed_contents; | |
| 80 TrimWhitespaceASCII(buffer, TRIM_TRAILING, &trimmed_contents); | |
| 81 std::vector<std::string> lines; | |
| 82 SplitString(trimmed_contents, '\n', &lines); | |
| 83 for (size_t i = 0; i < lines.size(); ++i) | |
| 84 LOG(INFO) << prefix << lines[i]; | |
| 85 } | |
| OLD | NEW |