forked from chaos/slurm-spank-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system-safe-preload.c
343 lines (269 loc) · 7.12 KB
/
system-safe-preload.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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*****************************************************************************
*
* Copyright (C) 2007-2008 Lawrence Livermore National Security, LLC.
* Produced at Lawrence Livermore National Laboratory.
* Written by Mark Grondona <[email protected]>.
*
* UCRL-CODE-235358
*
* This file is part of chaos-spankings, a set of spank plugins for SLURM.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
/*
* safe-system.so : Making system(3) safe for MPI jobs everywhere.
*/
#include <stdlib.h>
#include <dlfcn.h>
#include <pthread.h>
#include <sys/types.h>
#include <unistd.h>
#include <errno.h>
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
extern char **environ;
typedef int (*system_f) (const char * cmd);
static void * libc_handle;
static system_f real_system;
static int client_fd = -1;
static int server_fd = -1;
static int write_n (int fd, const void *buf, size_t n)
{
size_t nleft;
ssize_t nwritten;
unsigned const char *p;
p = buf;
nleft = n;
while (nleft > 0) {
if ((nwritten = write (fd, p, nleft)) < 0) {
if (errno == EINTR)
continue;
else
return (-1);
}
nleft -= nwritten;
p += nwritten;
}
return (n);
}
static int read_n (int fd, void *buf, size_t n)
{
size_t nleft;
ssize_t nread;
unsigned char *p;
p = buf;
nleft = n;
while (nleft > 0) {
if ((nread = read (fd, p, nleft)) < 0) {
if (errno == EINTR)
continue;
else
return (-1);
}
else if (nread == 0) { /* EOF */
break;
}
nleft -= nread;
p += nread;
}
return (n - nleft);
}
static int create_socketpair (void)
{
int pfds[2];
if (socketpair (AF_UNIX, SOCK_STREAM, 0, pfds) < 0) {
fprintf (stderr, "systemsafe: socketpair failed: %s\n", strerror (errno));
return (-1);
}
client_fd = pfds[0];
server_fd = pfds[1];
fcntl (client_fd, F_SETFD, FD_CLOEXEC);
fcntl (server_fd, F_SETFD, FD_CLOEXEC);
return (0);
}
static int read_string (int fd, char **bufp)
{
int len = 0;
int rc;
*bufp = NULL;
/*
* Read string length
*/
if ((rc = read_n (fd, &len, sizeof (int))) < 0) {
fprintf (stderr, "systemsafe: read_string: %s\n", strerror (errno));
return (-1);
}
if (rc == 0)
return (0);
if ((*bufp = malloc (len + 1)) == NULL) {
fprintf (stderr, "systemsafe: read_string: malloc (%d): %s\n",
len, strerror (errno));
return (-1);
}
if ((rc = read_n (fd, *bufp, len)) < 0) {
fprintf (stderr, "systemsafe: read_string: %s\n", strerror (errno));
return (-1);
}
if (rc == 0)
return (0);
(*bufp) [len] = '\0';
return (len);
}
static int write_string (int fd, const char *str)
{
int len = strlen (str);
int rc;
if (write_n (fd, &len, sizeof (int)) < 0) {
fprintf (stderr, "systemsafe: write: %s\n", strerror (errno));
return (-1);
}
rc = write_n (fd, str, len);
return (rc);
}
void free_env (char **env)
{
int i = 0;
while (env [i])
free (env [i++]);
free (env);
return;
}
int read_env (int fd, char ***envp)
{
int envc = 0;
int i;
if (read_n (fd, &envc, sizeof (int)) < 0) {
fprintf (stderr, "systemsafe: read_env: %s\n", strerror (errno));
return (-1);
}
if (!(*envp = malloc ((envc + 1) * sizeof (**envp)))) {
fprintf (stderr, "systemsafe: read_env: malloc: %s\n", strerror (errno));
return (-1);
}
for (i = 0; i < envc; i++) {
char *entry;
if (read_string (fd, &entry) < 0) {
fprintf (stderr, "systemsafe: %s\n", strerror (errno));
free_env (*envp);
return (-1);
}
if (strncmp ("LD_PRELOAD=", entry, 10) == 0)
entry [11] = '\0';
(*envp)[i] = entry;
}
(*envp)[envc] = NULL;
return (0);
}
static void handle_system_request (int fd)
{
char *cmd, *path, **env, **oldenv;
int rc;
if ((rc = read_string (fd, &cmd)) < 0) {
fprintf (stderr, "systemsafe: read cmd: %s\n", strerror (errno));
exit (0);
}
if (rc == 0) /* EOF, time to exit */
exit (0);
if (read_string (fd, &path) < 0) {
fprintf (stderr, "systemsafe: read path: %s\n", strerror (errno));
exit (0);
}
if (read_env (fd, &env) < 0) {
fprintf (stderr, "systemsafe: read env: %s\n", strerror (errno));
exit (0);
}
if (chdir (path) < 0)
fprintf (stderr, "systemsafe: Failed to chdir to %s: %s\n",
path, strerror (errno));
oldenv = environ;
environ = env;
rc = (*real_system) (cmd);
write_n (fd, &rc, sizeof (int));
environ = oldenv;
free_env (env);
free (cmd);
free (path);
return;
}
static void system_server (void)
{
char c = 0;
close (client_fd);
write (server_fd, &c, 1);
for (;;)
handle_system_request (server_fd);
return;
}
static int create_system_server (void)
{
pid_t pid;
char c;
create_socketpair ();
if ((pid = fork ()) < 0)
return (-1);
if (pid == 0) {
system_server ();
exit (0);
}
close (server_fd);
/*
* Wait for system_server setup to complete
*/
read (client_fd, &c, 1);
return (0);
}
static int write_env (int fd)
{
int i, envc = 0;
while (environ[envc])
envc++;
write (fd, &envc, sizeof (int));
for (i = 0; i < envc; i++)
write_string (fd, environ [i]);
return (0);
}
int system (const char *cmd)
{
int rc;
char path [4096];
if (cmd == NULL) {
errno = EINVAL;
return (-1);
}
write_string (client_fd, cmd);
write_string (client_fd, getcwd (path, sizeof (path)));
write_env (client_fd);
if (read (client_fd, &rc, sizeof (int)) < 0) {
fprintf (stderr, "system: failed to read status from server: %s\n",
strerror (errno));
return (-1);
}
return (rc);
}
void __attribute__ ((constructor)) fork_safe_init (void)
{
if ((libc_handle = dlopen ("libc.so.6", RTLD_LAZY)) == NULL) {
exit (1);
}
if ((real_system = dlsym (libc_handle, "system")) == NULL)
exit (2);
create_system_server ();
return;
}
/*
* vi: ts=4 sw=4 expandtab
*/