2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
20 # error LightProcess is not supported under MSVC!
32 #include "hphp/util/process.h"
33 #include "hphp/util/lock.h"
36 ///////////////////////////////////////////////////////////////////////////////
37 // light-weight process
43 Mutex
& mutex() { return m_procMutex
; }
46 static bool Available();
47 static void Initialize(const std::string
&prefix
, int count
,
48 bool trackProcessTimes
,
49 const std::vector
<int> &inherited_fds
);
50 static void ChangeUser(const std::string
&username
);
51 static void ChangeUser(int afdt
, const std::string
&username
);
53 typedef std::function
<void(pid_t
)> LostChildHandler
;
54 static void SetLostChildHandler(const LostChildHandler
& handler
);
56 static FILE *popen(const char *cmd
, const char *type
,
57 const char *cwd
= nullptr);
58 static int pclose(FILE *f
);
60 /** Opens a process with the given arguments, environment, working directory,
61 * and file descriptors.
63 * See Process::ForkAndExecve for details.
65 static pid_t
ForkAndExecve(
66 const std::string
& path
,
67 const std::vector
<std::string
>& argv
,
68 const std::vector
<std::string
>& envp
,
69 const std::string
& cwd
,
70 const std::map
<int, int>& fds
,
71 int flags
= Process::FORK_AND_EXECVE_FLAG_NONE
,
76 * Opens a process with given cwd and environment variables, using the shell.
78 * The parameters "created" and "desired" describe the pipes that need to
79 * be setup for the child process: "created" contains created fd for child,
80 * and "desired" contains desired fd in child.
82 * The parameter env contains strings of the form <name>=<content>.
84 static pid_t
proc_open(const char *cmd
, const std::vector
<int> &created
,
85 const std::vector
<int> &desired
,
86 const char *cwd
, const std::vector
<std::string
> &env
);
89 * The main process is not the (direct) parent of the worker process,
90 * and therefore it has to delegate to the shadow process to do waitpid.
91 * There can be a timeout (in seconds), after which SIGKILL is sent to
94 static pid_t
waitpid(pid_t pid
, int *stat_loc
, int options
, int timeout
= 0);
96 static pid_t
pcntl_waitpid(pid_t pid
, int *stat_loc
, int options
);
99 * When running a CLI server, the requests executed on behalf of local
100 * processes will delegate to a light process pool run by the client.
102 static int createDelegate();
104 static std::unique_ptr
<LightProcess
> setThreadLocalAfdtOverride(int fd
);
105 static std::unique_ptr
<LightProcess
> setThreadLocalAfdtOverride(
106 std::unique_ptr
<LightProcess
> p
110 static void SigChldHandler(int sig
, siginfo_t
* info
, void* ctx
);
112 bool initShadow(int afdt_listen
,
113 const std::string
& afdt_filename
, int id
,
114 const std::vector
<int> &inherited_fds
);
115 static void runShadow(int afdt_fd
);
119 * For later light processes to close their pipes to previous ones.
123 static FILE *LightPopenImpl(const char *cmd
, const char *type
,
125 static FILE *HeavyPopenImpl(const char *cmd
, const char *type
,
128 pid_t m_shadowProcess
;
131 std::map
<FILE*, pid_t
> m_popenMap
;
134 // whether to abort when user change fails.
135 static bool g_strictUser
;
138 ///////////////////////////////////////////////////////////////////////////////