| 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 CHROMEOS_PROCESS_H_ | |
| 6 #define CHROMEOS_PROCESS_H_ | |
| 7 | |
| 8 #include <sys/types.h> | |
| 9 | |
| 10 #include <string> | |
| 11 #include <vector> | |
| 12 | |
| 13 #include <base/string_util.h> | |
| 14 | |
| 15 namespace chromeos { | |
| 16 // Manages a process. Can create the process, attach to an existing | |
| 17 // process by pid or pid file, and kill the process. Upon destruction | |
| 18 // any managed process is killed with SIGKILL. Use Release() to | |
| 19 // release the process from management. A given system process may | |
| 20 // only be managed by one Process at a time. | |
| 21 class Process { | |
| 22 public: | |
| 23 Process(); | |
| 24 virtual ~Process(); | |
| 25 | |
| 26 // Adds |arg| to the executable command-line to be run. The | |
| 27 // executable name itself is the first argument. | |
| 28 virtual void AddArg(const std::string& arg) = 0; | |
| 29 | |
| 30 // Adds |option| and |value| as an option with a string value to the | |
| 31 // command line to be run. | |
| 32 inline void AddStringOption(const std::string& option, | |
| 33 const std::string& value) { | |
| 34 AddArg(option); | |
| 35 AddArg(value); | |
| 36 } | |
| 37 | |
| 38 // Adds |option| and |value| as an option which takes an integer | |
| 39 // value to the command line to be run. | |
| 40 inline void AddIntOption(const std::string& option, int value) { | |
| 41 AddArg(option); | |
| 42 AddArg(StringPrintf("%d", value)); | |
| 43 } | |
| 44 | |
| 45 // Redirects stderr and stdout to |output_file|. | |
| 46 virtual void RedirectOutput(const std::string& output_file) = 0; | |
| 47 | |
| 48 // Starts this process, returning true if successful. | |
| 49 virtual bool Start() = 0; | |
| 50 | |
| 51 // Waits for this process to finish. Returns the process's exit | |
| 52 // status if it exited normally, or otherwise returns -1. | |
| 53 virtual int Wait() = 0; | |
| 54 | |
| 55 // Start and wait for this process to finish. Returns same value as | |
| 56 // Wait(). | |
| 57 virtual int Run() = 0; | |
| 58 | |
| 59 // Returns the pid of this process or else returns 0 if there is no | |
| 60 // corresponding process (either because it has not yet been started | |
| 61 // or has since exited). | |
| 62 virtual pid_t pid() = 0; | |
| 63 | |
| 64 // Kills this process with |signal|. If process is not a child, | |
| 65 // returns immediately with a value based on whether kill was | |
| 66 // successful. If the process is a child and |timeout| is non-zero, | |
| 67 // returns true if the process is able to be reaped within the given | |
| 68 // |timeout| in seconds. | |
| 69 virtual bool Kill(int signal, int timeout) = 0; | |
| 70 | |
| 71 // Resets this Process object to refer to the process with |pid|. | |
| 72 // If |pid| is zero, this object no longer refers to a process. | |
| 73 virtual void Reset(pid_t new_pid) = 0; | |
| 74 | |
| 75 // Same as Reset but reads the pid from |pid_file|. Returns false | |
| 76 // only when the file cannot be read/parsed. | |
| 77 virtual bool ResetPidByFile(const std::string& pid_file) = 0; | |
| 78 | |
| 79 // Releases the process so that on destruction, the process is not killed. | |
| 80 virtual pid_t Release() = 0; | |
| 81 | |
| 82 // Returns if |pid| is a currently running process. | |
| 83 static bool ProcessExists(pid_t pid); | |
| 84 }; | |
| 85 | |
| 86 class ProcessImpl : public Process { | |
| 87 public: | |
| 88 ProcessImpl(); | |
| 89 virtual ~ProcessImpl(); | |
| 90 | |
| 91 virtual void AddArg(const std::string& arg); | |
| 92 virtual void RedirectOutput(const std::string& output_file); | |
| 93 virtual bool Start(); | |
| 94 virtual int Wait(); | |
| 95 virtual int Run(); | |
| 96 virtual pid_t pid(); | |
| 97 virtual bool Kill(int signal, int timeout); | |
| 98 virtual void Reset(pid_t pid); | |
| 99 virtual bool ResetPidByFile(const std::string& pid_file); | |
| 100 virtual pid_t Release(); | |
| 101 | |
| 102 protected: | |
| 103 std::string output_file_; | |
| 104 std::vector<std::string> arguments_; | |
| 105 | |
| 106 void UpdatePid(pid_t new_pid); | |
| 107 | |
| 108 private: | |
| 109 // Pid of currently managed process or 0 if no currently managed | |
| 110 // process. pid must not be modified except by calling | |
| 111 // UpdatePid(new_pid). | |
| 112 pid_t pid_; | |
| 113 }; | |
| 114 | |
| 115 } // namespace chromeos | |
| 116 | |
| 117 #endif // CHROMEOS_PROCESS_H | |
| OLD | NEW |