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 #ifndef _VPN_MANAGER_SERVICE_MANAGER_H_ |
| 6 #define _VPN_MANAGER_SERVICE_MANAGER_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/file_path.h" |
| 11 #include "gtest/gtest_prod.h" // for FRIEND_TEST |
| 12 |
| 13 class ScopedTempDir; |
| 14 |
| 15 // Generic code to manage setting up and stopping a set of layered |
| 16 // tunnel services. This object contains the code to manage a single |
| 17 // layer. Services are meant to be started from outermost to innermost. |
| 18 // Services are meant to be stopped from the innermost out. To |
| 19 // stop the entire set of services, call Stop on the innermost. |
| 20 // Services go from not-yet-started to started to in_running to |
| 21 // was_stopped. |
| 22 class ServiceManager { |
| 23 public: |
| 24 ServiceManager(const std::string& service_name); |
| 25 virtual ~ServiceManager(); |
| 26 |
| 27 // Initialize directories used by services. |scoped_temp_dir| will |
| 28 // be set to manage an appropriate temp directory. This function |
| 29 // uses a reference to |scoped_temp_dir| and so its lifetime must be |
| 30 // equal to that of all objects derived from ServiceManager. |
| 31 static void InitializeDirectories(ScopedTempDir* scoped_temp_path); |
| 32 |
| 33 // Call to initiate this service. If starting fails immediately this |
| 34 // returns false. If something fails after this returns, OnStopped |
| 35 // will be called. Code outside of the service manager stack |
| 36 // must only call Start on the outermost function. |
| 37 virtual bool Start() = 0; |
| 38 |
| 39 // Callback when this service has successfully started. |
| 40 virtual void OnStarted(); |
| 41 |
| 42 // Call to stop this service. Must not be called on a separate |
| 43 // thread from Start(). Code outside of the service manager stack |
| 44 // must only call Stop on the innermost service. It is ok to |
| 45 // stop an already stopped service. |
| 46 virtual void Stop() = 0; |
| 47 |
| 48 // Returns the maximum amount of time to wait before this call should be |
| 49 // called again in milliseconds. |
| 50 virtual int Poll() = 0; |
| 51 |
| 52 // Process output from child process. |
| 53 virtual void ProcessOutput() = 0; |
| 54 |
| 55 // Returns if |pid| is a child process of this service. |
| 56 virtual bool IsChild(pid_t pid) = 0; |
| 57 |
| 58 // Callback when this service has stopped after having started |
| 59 // successfully. |was_error| indicates if an error occurred. |
| 60 virtual void OnStopped(bool was_error); |
| 61 |
| 62 // Queries if this service is currently running. |
| 63 bool is_running() { |
| 64 return is_running_; |
| 65 } |
| 66 |
| 67 // Queries if this service was once running and is now stopped. |
| 68 bool was_stopped() { |
| 69 return was_stopped_; |
| 70 } |
| 71 |
| 72 // Set up layering between two service managers |outer| and |inner|. |
| 73 // This function may be called multiple times to chain servics together, |
| 74 // for instance: |
| 75 // ServiceManager::SetLayerOrder(&turkey, &duck); |
| 76 // ServiceManager::SetLayerOrder(&duck, &chicken); |
| 77 static void SetLayerOrder(ServiceManager* outer, |
| 78 ServiceManager* inner) { |
| 79 outer->inner_service_ = inner; |
| 80 inner->outer_service_ = outer; |
| 81 } |
| 82 |
| 83 const std::string& service_name() { |
| 84 return service_name_; |
| 85 } |
| 86 |
| 87 // Repeat data from the given |fd| which is assumed to be ready and |
| 88 // send it out to syslog, placing |prefix| before each line of |
| 89 // output. Function will block reading fd so it assumes fd is |
| 90 // ready. It will also only read a fixed size per call. Any |
| 91 // partial line read is stored into |partial_line|. This variable |
| 92 // is used on each call to prefix any newly read data. |
| 93 static void WriteFdToSyslog(int fd, const std::string& prefix, |
| 94 std::string* partial_line); |
| 95 |
| 96 protected: |
| 97 friend class IpsecManagerTest; |
| 98 friend class L2tpManagerTest; |
| 99 friend class ServiceManagerTest; |
| 100 FRIEND_TEST(L2tpManagerTest, PollNothingIfRunning); |
| 101 FRIEND_TEST(IpsecManagerTest, PollNothingIfRunning); |
| 102 FRIEND_TEST(ServiceManagerTest, InitializeDirectories); |
| 103 FRIEND_TEST(ServiceManagerTest, OnStoppedFromFailure); |
| 104 FRIEND_TEST(ServiceManagerTest, OnStoppedFromSuccess); |
| 105 |
| 106 ServiceManager* inner_service() { return inner_service_; } |
| 107 |
| 108 ServiceManager* outer_service() { return outer_service_; } |
| 109 |
| 110 static const FilePath* temp_path() { return temp_path_; } |
| 111 |
| 112 private: |
| 113 // Indicates if this service is currently running. |
| 114 bool is_running_; |
| 115 |
| 116 // Indicates if this service was running and is now stopped. |
| 117 bool was_stopped_; |
| 118 |
| 119 // Pointer to the next layer or NULL if innermost. |
| 120 ServiceManager* inner_service_; |
| 121 |
| 122 // Pointer to the outer layer or NULL if outermost. |
| 123 ServiceManager* outer_service_; |
| 124 |
| 125 // Name of this service. |
| 126 std::string service_name_; |
| 127 |
| 128 // Path to temporary directory on cryptohome. |
| 129 static const FilePath* temp_path_; |
| 130 |
| 131 // Path to base directory of temporary directory on cryptohome. |
| 132 static const char* temp_base_path_; |
| 133 }; |
| 134 |
| 135 #endif // _VPN_MANAGER_SERVICE_MANAGER_H_ |
OLD | NEW |