forked from ioi/isolate
-
Notifications
You must be signed in to change notification settings - Fork 7
/
util.c
182 lines (155 loc) · 3.22 KB
/
util.c
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
/*
* Process Isolator -- Utility Functions
*
* (c) 2012-2017 Martin Mares <[email protected]>
* (c) 2012-2014 Bernard Blackham <[email protected]>
*/
#include "isolate.h"
#include <dirent.h>
#include <errno.h>
#include <ftw.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/fsuid.h>
#include <sys/stat.h>
#include <unistd.h>
void *
xmalloc(size_t size)
{
void *p = malloc(size);
if (!p)
die("Out of memory");
return p;
}
char *
xstrdup(char *str)
{
char *p = strdup(str);
if (!p)
die("Out of memory");
return p;
}
int
dir_exists(char *path)
{
struct stat st;
return (stat(path, &st) >= 0 && S_ISDIR(st.st_mode));
}
void
make_dir(char *path)
{
char *sep = (path[0] == '/' ? path+1 : path);
for (;;)
{
sep = strchr(sep, '/');
if (sep)
*sep = 0;
if (mkdir(path, 0777) < 0 && errno != EEXIST)
die("Cannot create directory %s: %m", path);
if (!sep)
break;
*sep++ = '/';
}
// mkdir() above may have returned EEXIST even if the path was not
// a directory. Ensure that it is.
struct stat st;
if (stat(path, &st) < 0)
die("Cannot stat %s: %m", path);
if (!S_ISDIR(st.st_mode))
die("Cannot create %s: already exists, but not a directory", path);
}
static int
rmtree_helper(const char *fpath, const struct stat *sb, int typeflag UNUSED, struct FTW *ftwbuf UNUSED)
{
if (S_ISDIR(sb->st_mode))
{
if (rmdir(fpath) < 0)
die("Cannot rmdir %s: %m", fpath);
}
else
{
if (unlink(fpath) < 0)
die("Cannot unlink %s: %m", fpath);
}
return 0;
}
void
rmtree(char *path)
{
nftw(path, rmtree_helper, 32, FTW_MOUNT | FTW_PHYS | FTW_DEPTH);
}
static uid_t chown_uid;
static gid_t chown_gid;
static int
chowntree_helper(const char *fpath, const struct stat *sb UNUSED, int typeflag UNUSED, struct FTW *ftwbuf UNUSED)
{
if (lchown(fpath, chown_uid, chown_gid) < 0)
die("Cannot chown %s: %m", fpath);
else
return 0;
}
void
chowntree(char *path, uid_t uid, gid_t gid)
{
chown_uid = uid;
chown_gid = gid;
nftw(path, chowntree_helper, 32, FTW_MOUNT | FTW_PHYS);
}
static int fd_to_keep = -1;
void
close_all_fds(void)
{
/* Close all file descriptors except 0, 1, 2 */
DIR *dir = opendir("/proc/self/fd");
if (!dir)
die("Cannot open /proc/self/fd: %m");
int dir_fd = dirfd(dir);
struct dirent *e;
while (e = readdir(dir))
{
char *end;
long int fd = strtol(e->d_name, &end, 10);
if (*end)
continue;
if (fd >= 0 && fd <= 2 || fd == dir_fd || fd == fd_to_keep)
continue;
close(fd);
}
closedir(dir);
}
/*** Meta-files ***/
static FILE *metafile;
void
meta_open(const char *name)
{
if (!strcmp(name, "-"))
{
metafile = stdout;
return;
}
if (setfsuid(getuid()) < 0)
die("Failed to switch FS UID: %m");
metafile = fopen(name, "w");
if (setfsuid(geteuid()) < 0)
die("Failed to switch FS UID back: %m");
if (!metafile)
die("Failed to open metafile '%s'",name);
fd_to_keep = fileno(metafile);
}
void
meta_close(void)
{
if (metafile && metafile != stdout)
fclose(metafile);
}
void
meta_printf(const char *fmt, ...)
{
if (!metafile)
return;
va_list args;
va_start(args, fmt);
vfprintf(metafile, fmt, args);
va_end(args);
}