-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathutil.cc
295 lines (274 loc) · 6.98 KB
/
util.cc
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// C headers
extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mount.h>
#include <sys/wait.h>
#include <signal.h>
#include <libgen.h>
}
// C++ headers
#include <iostream>
#include <system_error>
#include "util.h"
using namespace std;
using namespace util;
bool util::PathExists(string path)
{
struct stat s;
int r = lstat(path.c_str(), &s);
if (r < 0)
{
if (errno != ENOENT)
{
throw system_error(errno, system_category(), "PathExists, lstat() failed");
}
return false;
}
return true;
}
bool util::IsRegularFile(string path)
{
struct stat s;
int r = lstat(path.c_str(), &s);
if (r < 0)
{
if (errno != ENOENT)
{
throw system_error(errno, system_category(), "IsRegularFile, lstat() failed");
}
return false; // Does not exist
}
return S_ISREG(s.st_mode);
}
bool util::IsDirectory(string path)
{
struct stat s;
int r = lstat(path.c_str(), &s);
if (r < 0)
{
if (errno != ENOENT)
{
throw system_error(errno, system_category(), "IsDirectory, lstat() failed");
}
return false; // Does not exist
}
return S_ISDIR(s.st_mode);
}
string util::BaseName(string path)
{
char* copy = strdup(path.c_str());
string bn = basename(copy);
free(copy);
return bn;
}
void util::DeleteFile(string path)
{
if (unlink(path.c_str()) < 0)
{
throw system_error(errno, system_category(), "DeleteFile, unlink() failed");
}
}
void util::DeleteFolder(string path)
{
if (rmdir(path.c_str()) < 0)
{
throw system_error(errno, system_category(), "DeleteFolder, rmdir() failed");
}
}
void util::ChangeMode(string path, unsigned short mode)
{
mode_t old_mask = umask(0);
if (chmod(path.c_str(), mode) < 0)
{
umask(old_mask); // umask does not change errno
throw system_error(errno, system_category(), "ChangeMode, chmod() failed");
}
umask(old_mask);
}
string util::CreateTempFolder(string path_prefix)
{
char buffer[256];
const char* X = "XXXXXX";
if ((path_prefix.size() + strlen(X) + 1) > sizeof(buffer))
{
throw runtime_error("path prefix is too long!");
}
snprintf(buffer, sizeof(buffer), "%s%s", path_prefix.c_str(), X);
if (mkdtemp(buffer) == NULL)
{
throw system_error(errno, system_category(), "CreateTempFolder, mkdtemp() failed");
}
return string(buffer);
}
void util::CreateFolder(string path, unsigned short mode)
{
mode_t old_mask = umask(0);
if (mkdir(path.c_str(), mode) < 0)
{
umask(old_mask); // umask does not change errno
throw system_error(errno, system_category(), "CreateFolder, mkdir() failed");
}
umask(old_mask);
}
void util::BindMount(string source, string dest)
{
if (mount(source.c_str(), dest.c_str(), "", MS_BIND | MS_REC, "") < 0)
{
throw system_error(errno, system_category(), "BindMount, 1st mount() failed");
}
if (mount(source.c_str(), dest.c_str(), "", MS_BIND | MS_REMOUNT | MS_RDONLY | MS_PRIVATE, "") < 0)
{
throw system_error(errno, system_category(), "BindMount, 2nd mount() failed");
}
}
void util::Unmount(string dest)
{
if (umount(dest.c_str()) < 0)
{
throw system_error(errno, system_category(), "Unmount, umount() failed");
}
}
void util::MarkMountPointPrivate(string path)
{
if (mount(path.c_str(), path.c_str(), "", MS_REMOUNT | MS_PRIVATE, "") < 0)
{
throw system_error(errno, system_category(), "MarkMountPointPrivate, mount() failed");
}
}
void util::CreatePrivateMount(string path)
{
if (mount(path.c_str(), path.c_str(), "", MS_BIND | MS_REC, "") < 0)
{
throw system_error(errno, system_category(), "CreatePrivateMount, mount() failed");
}
MarkMountPointPrivate(path);
}
void util::MountSpecialFileSystem(string path, string fs)
{
if (mount(fs.c_str(), path.c_str(), fs.c_str(), 0, "") < 0)
{
throw system_error(errno, system_category(), "MountSpecialFileSystem, mount() failed");
}
}
void util::Chroot(string new_root)
{
if (chroot(new_root.c_str()) < 0)
{
throw system_error(errno, system_category(), "Chroot, chroot() failed");
}
}
void util::Chdir(string path)
{
if (chdir(path.c_str()) < 0)
{
throw system_error(errno, system_category(), "Chdir, chdir() failed");
}
}
void util::ForkExecWait(char* args[], Task beforeExec)
{
pid_t pid = fork();
if (pid == 0)
{
// Child
beforeExec();
execv(args[0], args);
cerr << "Error in execv: " << strerror(errno) << endl;
exit(EXIT_FAILURE);
}
else if (pid > 0)
{
// Parent
if (waitpid(pid, NULL, 0) < 0)
{
throw system_error(errno, system_category(), "ForkExecWait, waitpid() failed");
}
}
else
{
// Error, in parent
throw system_error(errno, system_category(), "ForkExecWait, fork() failed");
}
}
void util::ForkExecWaitTimeout(char* args[], Task beforeExec, unsigned int timeout_ms)
{
pid_t timer_pid = fork();
if (timer_pid == 0)
{
struct timespec req;
req.tv_sec = timeout_ms / 1000;
req.tv_nsec = (timeout_ms % 1000) * 1000000;
// Sleep for the amount of timeout
nanosleep(&req, NULL);
_exit(0);
}
else if (timer_pid < 0)
{
throw system_error(errno, system_category(), "ForkExecWaitTimeout, failed to fork() timer process");
}
pid_t child_pid = fork();
if (child_pid == 0)
{
// Child
beforeExec();
execv(args[0], args);
cerr << "Error in execv: " << strerror(errno) << endl;
exit(EXIT_FAILURE);
}
else if (child_pid < 0)
{
// Error, in parent
throw system_error(errno, system_category(), "ForkExecWaitTimeout, failed to fork() child process");
}
// Parent: wait
pid_t x = waitpid(-1, NULL, 0);
if (x == child_pid)
{
kill(timer_pid, SIGKILL);
waitpid(-1, NULL, 0);
}
else if (x == timer_pid)
{
kill(child_pid, SIGKILL);
waitpid(-1, NULL, 0);
}
else
{
throw runtime_error("ForkExecWaitTimeout, Could not determine which child process exitted");
}
}
void util::ForkCallWait(Task task)
{
pid_t pid = fork();
if (pid == 0)
{
// Child
task();
exit(EXIT_SUCCESS);
}
else if (pid > 0)
{
// Parent
if (waitpid(pid, NULL, 0) < 0)
{
throw system_error(errno, system_category(), "ForkCallWait, waitpid() failed");
}
}
else
{
// Error, in parent
throw system_error(errno, system_category(), "ForkCallWait, fork() failed");
}
}
void util::Unshare(int flags)
{
if (unshare(flags) < 0)
{
throw system_error(errno, system_category(), "Unshare, unshare() failed");
}
}