Delete ClassInfoHook
[hiphop-php.git] / hphp / util / light-process.h
blob1d3c10d15186d7d1804f9742ab9a21aaa2ec2fce
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-2014 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 #ifndef incl_HPHP_LIGHT_PROCESS_H_
18 #define incl_HPHP_LIGHT_PROCESS_H_
20 #include <string>
21 #include <vector>
22 #include <cstdio>
23 #include <cstdlib>
24 #include <map>
26 #include <signal.h>
27 #include <unistd.h>
29 #include "hphp/util/process.h"
30 #include "hphp/util/lock.h"
32 namespace HPHP {
33 ///////////////////////////////////////////////////////////////////////////////
34 // light-weight process
36 class LightProcess {
37 public:
38 LightProcess();
39 ~LightProcess();
41 static void Close();
42 static bool Available();
43 static void Initialize(const std::string &prefix, int count,
44 const std::vector<int> &inherited_fds);
45 static void ChangeUser(const std::string &username);
47 typedef std::function<void(pid_t)> LostChildHandler;
48 static void SetLostChildHandler(const LostChildHandler& handler);
50 static FILE *popen(const char *cmd, const char *type,
51 const char *cwd = nullptr);
52 static int pclose(FILE *f);
54 /**
55 * Opens a process with given cwd and environment variables.
57 * The parameters "created" and "desired" describe the pipes that need to
58 * be setup for the child process: "created" contains created fd for child,
59 * and "desired" contains desired fd in child.
61 * The parameter env contains strings of the form <name>=<content>.
63 static pid_t proc_open(const char *cmd, const std::vector<int> &created,
64 const std::vector<int> &desired,
65 const char *cwd, const std::vector<std::string> &env);
67 /**
68 * The main process is not the (direct) parent of the worker process,
69 * and therefore it has to delegate to the shadow process to do waitpid.
70 * There can be a timeout (in seconds), after which SIGKILL is sent to
71 * the child process.
73 static pid_t waitpid(pid_t pid, int *stat_loc, int options, int timeout = 0);
75 static pid_t pcntl_waitpid(pid_t pid, int *stat_loc, int options);
77 private:
78 static int GetId();
79 static void SigChldHandler(int sig, siginfo_t* info, void* ctx);
81 bool initShadow(const std::string &prefix, int id,
82 const std::vector<int> &inherited_fds);
83 void runShadow(int fdin, int fdout);
84 void closeShadow();
86 /**
87 * For later light processes to close their pipes to previous ones.
89 void closeFiles();
91 static FILE *LightPopenImpl(const char *cmd, const char *type,
92 const char *cwd);
93 static FILE *HeavyPopenImpl(const char *cmd, const char *type,
94 const char *cwd);
96 static Mutex s_mutex;
97 pid_t m_shadowProcess;
98 FILE *m_fin; // the pipe to read from the child
99 FILE *m_fout; // the pipe to write to the child
100 Mutex m_procMutex;
101 std::string m_afdtFilename;
102 int m_afdt_fd;
103 int m_afdt_lfd;
104 std::map<int64_t, int64_t> m_popenMap;
107 ///////////////////////////////////////////////////////////////////////////////
110 #endif // incl_HPHP_LIGHT_PROCESS_H_