/
ProcessPosix.cpp
166 lines (141 loc) · 2.89 KB
/
ProcessPosix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include "PlatformPch.h"
#include "Platform/Process.h"
#include "Platform/Assert.h"
#include "Platform/Error.h"
#include "Platform/Encoding.h"
#include "Platform/Utility.h"
#include "Platform/File.h"
#include <sstream>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <unistd.h>
#include <signal.h>
#include <spawn.h>
#if HELIUM_OS_MAC
# include <mach-o/dyld.h>
#endif
using namespace Helium;
int Helium::Execute( const std::string& command )
{
return system( command.c_str() );
}
int Helium::Execute( const std::string& command, std::string& output )
{
FILE* f = popen( command.c_str(), "r" );
char line[200];
line[199] = '\0';
while ( fgets(line, 199, f) )
{
output += line;
}
return pclose( f );
}
ProcessHandle Helium::Spawn( const std::string& cmd, bool autoKill )
{
ProcessHandle pid;
if ( autoKill )
{
// NYI: http://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits
return 0;
}
else
{
const char* spawnedArgs[] = { "/bin/bash", "-c", cmd.c_str(), NULL };
if ( posix_spawn( &pid, spawnedArgs[0], NULL, NULL, const_cast< char* const* >( spawnedArgs ), NULL ) == 0 )
{
return pid;
}
}
return 0;
}
bool Helium::SpawnRunning( ProcessHandle handle )
{
int status = 0;
HELIUM_VERIFY( handle == waitpid( handle, &status, WNOHANG | WUNTRACED ) );
if ( WIFEXITED( status ) )
{
return false;
}
else
{
return true;
}
}
int Helium::SpawnResult( ProcessHandle handle )
{
int status = 0;
HELIUM_VERIFY( handle == waitpid( handle, &status, WUNTRACED ) );
if ( WIFEXITED( status ) )
{
return WEXITSTATUS( status );
}
return -1;
}
void Helium::SpawnKill( ProcessHandle handle )
{
kill( handle, SIGTERM );
}
int Helium::GetProcessId( ProcessHandle handle )
{
return handle;
}
std::string Helium::GetProcessString()
{
std::ostringstream result;
result << GetProcessName() << "_" << getpid() << "_" << syscall(SYS_gettid);
return result.str();
}
std::string Helium::GetProcessPath()
{
char buf[ PATH_MAX ];
#if HELIUM_OS_MAC
uint32_t size = sizeof(buf);
HELIUM_ASSERT( _NSGetExecutablePath(buf, &size) == 0 );
#elif HELIUM_OS_LINUX
size_t len = readlink("/proc/self/exe", buf, sizeof(buf));
buf[len] = '\0';
#endif
return buf;
}
std::string Helium::GetProcessName()
{
std::string path = GetProcessPath();
size_t pos = path.find( Helium::PathSeparator );
if ( pos != std::string::npos )
{
return path.substr( pos );
}
HELIUM_ASSERT( false );
return "";
}
std::string Helium::GetUserName()
{
const char* user = getenv( "USER" );
if ( user )
{
return user;
}
HELIUM_ASSERT( false );
return "";
}
std::string Helium::GetMachineName()
{
const char* user = getenv( "HOSTNAME" );
if ( user )
{
return user;
}
HELIUM_ASSERT( false );
return "";
}
std::string Helium::GetHomeDirectory()
{
const char* user = getenv( "HOME" );
if ( user )
{
return user;
}
HELIUM_ASSERT( false );
return "";
}