Peter Qiu | c0beca5 | 2015-09-03 11:25:46 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2013 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef SHILL_EXTERNAL_TASK_H_ |
| 18 | #define SHILL_EXTERNAL_TASK_H_ |
| 19 | |
| 20 | #include <sys/types.h> |
| 21 | |
| 22 | #include <map> |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 23 | #include <memory> |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 24 | #include <string> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <base/callback.h> |
Ben Chan | a0ddf46 | 2014-02-06 11:32:42 -0800 | [diff] [blame] | 28 | #include <base/files/file_path.h> |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 29 | #include <base/memory/weak_ptr.h> |
| 30 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
| 31 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 32 | #include "shill/rpc_task.h" |
| 33 | |
| 34 | namespace shill { |
| 35 | |
| 36 | class ControlInterface; |
| 37 | class Error; |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 38 | class EventDispatcher; |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 39 | class ProcessManager; |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 40 | |
| 41 | class ExternalTask : public RPCTaskDelegate { |
| 42 | public: |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 43 | ExternalTask(ControlInterface* control, |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 44 | ProcessManager* process_manager, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 45 | const base::WeakPtr<RPCTaskDelegate>& task_delegate, |
| 46 | const base::Callback<void(pid_t, int)>& death_callback); |
Ben Chan | 5ea763b | 2014-08-13 11:07:54 -0700 | [diff] [blame] | 47 | ~ExternalTask() override; // But consider DestroyLater... |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 48 | |
| 49 | // Schedule later deletion of the ExternalTask. Useful when in the |
| 50 | // middle of an ExternalTask callback. Note that the caller _must_ |
| 51 | // release ownership of |this|. For example: |
| 52 | // |
| 53 | // class Foo : public SupportsWeakPtr<Foo>, public RPCTaskDelegate { |
| 54 | // public: |
| 55 | // Foo() { |
| 56 | // task_.reset(new ExternalTask(...)); |
| 57 | // } |
| 58 | // |
| 59 | // void Notify(...) { |
| 60 | // task_.release()->DestroyLater(...); // Passes ownership. |
| 61 | // } |
| 62 | // |
| 63 | // private: |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 64 | // std::unique_ptr<ExternalTask> task_; |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 65 | // } |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 66 | void DestroyLater(EventDispatcher* dispatcher); |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 67 | |
| 68 | // Forks off a process to run |program|, with the command-line |
| 69 | // arguments |arguments|, and the environment variables specified in |
| 70 | // |environment|. |
| 71 | // |
mukesh agrawal | c4f9aa0 | 2013-08-15 19:23:13 -0700 | [diff] [blame] | 72 | // If |terminate_with_parent| is true, the child process will be |
| 73 | // configured to terminate itself if this process dies. Otherwise, |
| 74 | // the child process will retain its default behavior. |
| 75 | // |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 76 | // On success, returns true, and leaves |error| unmodified. |
| 77 | // On failure, returns false, and sets |error|. |
| 78 | // |
| 79 | // |environment| SHOULD NOT contain kRPCTaskServiceVariable or |
| 80 | // kRPCTaskPathVariable, as that may prevent the child process |
| 81 | // from communicating back to the ExternalTask. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 82 | virtual bool Start(const base::FilePath& program, |
| 83 | const std::vector<std::string>& arguments, |
| 84 | const std::map<std::string, std::string>& environment, |
mukesh agrawal | c4f9aa0 | 2013-08-15 19:23:13 -0700 | [diff] [blame] | 85 | bool terminate_with_parent, |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 86 | Error* error); |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 87 | virtual void Stop(); |
| 88 | |
| 89 | private: |
| 90 | friend class ExternalTaskTest; |
| 91 | FRIEND_TEST(ExternalTaskTest, Destructor); |
| 92 | FRIEND_TEST(ExternalTaskTest, GetLogin); |
| 93 | FRIEND_TEST(ExternalTaskTest, Notify); |
| 94 | FRIEND_TEST(ExternalTaskTest, OnTaskDied); |
| 95 | FRIEND_TEST(ExternalTaskTest, Start); |
| 96 | FRIEND_TEST(ExternalTaskTest, Stop); |
| 97 | FRIEND_TEST(ExternalTaskTest, StopNotStarted); |
| 98 | |
| 99 | // Implements RPCTaskDelegate. |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 100 | void GetLogin(std::string* user, std::string* password) override; |
Alex Vakulenko | 016fa0e | 2014-08-11 15:59:58 -0700 | [diff] [blame] | 101 | void Notify( |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 102 | const std::string& event, |
| 103 | const std::map<std::string, std::string>& details) override; |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 104 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 105 | // Called when the external process exits. |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 106 | void OnTaskDied(int exit_status); |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 107 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 108 | static void Destroy(ExternalTask* task); |
mukesh agrawal | 9da0777 | 2013-05-15 14:15:17 -0700 | [diff] [blame] | 109 | |
Paul Stewart | a794cd6 | 2015-06-16 13:13:10 -0700 | [diff] [blame] | 110 | ControlInterface* control_; |
Peter Qiu | a24480a | 2015-08-11 23:09:54 -0700 | [diff] [blame] | 111 | ProcessManager* process_manager_; |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 112 | |
Ben Chan | cd47732 | 2014-10-17 14:19:30 -0700 | [diff] [blame] | 113 | std::unique_ptr<RPCTask> rpc_task_; |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 114 | base::WeakPtr<RPCTaskDelegate> task_delegate_; |
| 115 | base::Callback<void(pid_t, int)> death_callback_; |
| 116 | |
| 117 | // The PID of the spawned process. May be 0 if no process has been |
| 118 | // spawned yet or the process has died. |
| 119 | pid_t pid_; |
| 120 | |
mukesh agrawal | ae30e9e | 2013-05-28 14:09:16 -0700 | [diff] [blame] | 121 | DISALLOW_COPY_AND_ASSIGN(ExternalTask); |
| 122 | }; |
| 123 | |
| 124 | } // namespace shill |
| 125 | |
| 126 | #endif // SHILL_EXTERNAL_TASK_H_ |