Add a HHIR-level peephole optimization to reorder CheckTypes
[hiphop-php.git] / hphp / util / light-process.h
blobbeff68d8d0fdd2d636ee8cb3073f7a804dc50915
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
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 +----------------------------------------------------------------------+
17 #pragma once
19 #ifdef _MSC_VER
20 # error LightProcess is not supported under MSVC!
21 #endif
23 #include <string>
24 #include <vector>
25 #include <cstdio>
26 #include <cstdlib>
27 #include <map>
29 #include <signal.h>
30 #include <unistd.h>
32 #include "hphp/util/process.h"
33 #include "hphp/util/lock.h"
35 namespace HPHP {
36 ///////////////////////////////////////////////////////////////////////////////
37 // light-weight process
39 struct LightProcess {
40 LightProcess();
41 ~LightProcess();
43 Mutex& mutex() { return m_procMutex; }
45 static void Close();
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,
72 pid_t pgid = 0
75 /**
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);
88 /**
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
92 * the child process.
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);
98 /**
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
109 private:
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);
116 void closeShadow();
119 * For later light processes to close their pipes to previous ones.
121 void closeFiles();
123 static FILE *LightPopenImpl(const char *cmd, const char *type,
124 const char *cwd);
125 static FILE *HeavyPopenImpl(const char *cmd, const char *type,
126 const char *cwd);
128 pid_t m_shadowProcess;
129 Mutex m_procMutex;
130 int m_afdt_fd;
131 std::map<FILE*, pid_t> m_popenMap;
133 public:
134 // whether to abort when user change fails.
135 static bool g_strictUser;
138 ///////////////////////////////////////////////////////////////////////////////