forked from grondo/slurm-spank-plugins
-
Notifications
You must be signed in to change notification settings - Fork 3
/
pty.c
565 lines (446 loc) · 10.7 KB
/
pty.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*****************************************************************************
*
* Copyright (C) 2007-2008 Lawrence Livermore National Security, LLC.
* Produced at Lawrence Livermore National Laboratory.
* Written by Mark Grondona <mgrondona@llnl.gov>.
*
* 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/>.
****************************************************************************/
/*
* Hack to run task 0 under a pty for a slurm job.
*/
#include <stdlib.h>
#include <pty.h>
#include <utmp.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/poll.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <signal.h>
#include <slurm/spank.h>
SPANK_PLUGIN (pty, 1)
/*
* Globals:
*/
static int do_pty = 0;
static int master = -1;
static int listenfd = -1;
static pid_t pid;
static struct termios termdefaults;
static int pty_opt_process (int val, const char *optarg, int remote);
struct spank_option spank_options[] =
{
{ "pty", NULL,
"Allocate a pty for rank 0. Must also specify -u."
" (Use of --pty implies --output=0)",
0, 0, (spank_opt_cb_f) pty_opt_process
},
SPANK_OPTIONS_TABLE_END
};
struct pty_winsz {
unsigned rows;
unsigned cols;
};
static void pty_winsz_pack (struct pty_winsz *w)
{
w->rows = htonl (w->rows);
w->cols = htonl (w->cols);
}
static void pty_winsz_unpack (struct pty_winsz *w)
{
w->rows = ntohl (w->rows);
w->cols = ntohl (w->cols);
}
static int pty_opt_process (int val, const char *optarg, int remote)
{
do_pty = 1;
return (0);
}
void process_pty ()
{
unsigned char buf [4096];
int len;
if ((len = read (master, buf, sizeof (buf))) < 0) {
if (errno == EAGAIN)
return;
if (errno == EIO) /* Why do we get this sometimes */
return;
slurm_error ("read (pty master): %m\n");
exit (1);
}
else if (len == 0) {
close (STDOUT_FILENO);
close (master);
master = -1;
return;
}
write (STDOUT_FILENO, buf, len);
}
void process_stdin ()
{
unsigned char buf [4096];
int len;
if ((len = read (STDIN_FILENO, buf, sizeof (buf))) < 0) {
slurm_error ("stdin read: %m\n");
exit (1);
}
else if (len == 0) {
close (STDOUT_FILENO);
master = -1;
return;
}
write (master, buf, len);
}
void check_for_slave_exit ()
{
int status = 0;
if (waitpid (pid, &status, WNOHANG) <= 0)
return;
if (WIFEXITED (status))
exit (status);
}
static int fd_set_nonblocking (int fd)
{
int fval;
assert (fd >= 0);
if ((fval = fcntl (fd, F_GETFL, 0)) < 0)
return (-1);
if (fcntl (fd, F_SETFL, fval | O_NONBLOCK) < 0)
return (-1);
return (0);
}
static int get_winsize (spank_t sp, struct winsize *wsp)
{
char val [64];
memset (wsp, 0, sizeof (*wsp));
if (spank_getenv (sp, "SLURM_PTY_WIN_ROW", val, 64) == ESPANK_SUCCESS) {
spank_unsetenv (sp, "SLURM_PTY_WIN_ROW");
wsp->ws_row = atoi (val);
}
if (spank_getenv (sp, "SLURM_PTY_WIN_COL", val, 64) == ESPANK_SUCCESS) {
spank_unsetenv (sp, "SLURM_PTY_WIN_COL");
wsp->ws_col = atoi (val);
}
if (!wsp->ws_row && !wsp->ws_col)
return (0);
return (1);
}
int pty_connect_back (spank_t sp)
{
char ip [64], port [16];
struct sockaddr_in addr;
int s;
int rc = spank_getenv (sp, "SLURM_LAUNCH_NODE_IPADDR", ip, 64);
if (rc != ESPANK_SUCCESS) {
slurm_error ("failed to read SLURM_NODE_IPADDR in env!");
return (-1);
}
if (spank_getenv (sp, "SLURM_PTY_PORT", port, 16) != ESPANK_SUCCESS) {
slurm_error ("failed to read SLURM_PTY_PORT in env!");
return (-1);
}
addr.sin_family = AF_INET;
inet_aton (ip, &addr.sin_addr);
addr.sin_port = htons (atoi (port));
if ((s = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
slurm_error ("pty: socket: %m");
return (-1);
}
if (connect (s, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
slurm_error ("pty: connect: %m");
close (s);
return (-1);
}
return (s);
}
static int write_pty_winsize (int fd, struct winsize *ws)
{
int len;
struct pty_winsz winsz;
winsz.rows = ws->ws_row;
winsz.cols = ws->ws_col;
pty_winsz_pack (&winsz);
if ((len = write (fd, &winsz, sizeof (winsz))) < 0) {
slurm_error ("write_pty_winsz: %m");
return (-1);
}
return (len);
}
static int read_pty_winsize (int fd, struct winsize *ws)
{
struct pty_winsz winsz;
int len;
if ((len = read (fd, &winsz, sizeof (winsz))) < 0) {
slurm_error ("read_pty_winsz: %m");
return (-1);
}
if (len == 0) {
slurm_error ("read_pty_winsz: Remote closed connection.");
return (-1);
}
pty_winsz_unpack (&winsz);
memset (ws, 0, sizeof (*ws));
ws->ws_col = winsz.cols;
ws->ws_row = winsz.rows;
return (0);
}
static void process_winsz_event (int fd, int master)
{
struct winsize ws;
if (read_pty_winsize (fd, &ws) < 0)
return;
ioctl (master, TIOCSWINSZ, &ws);
kill (0, SIGWINCH);
}
static int no_close_stdio (spank_t sp)
{
char val [64];
const char var[] = "SLURM_PTY_NO_CLOSE_STDIO";
if (spank_getenv (sp, var, val, 64) == ESPANK_SUCCESS)
return (1);
return 0;
}
static void close_stdio (void)
{
int devnull;
if ((devnull = open ("/dev/null", O_RDWR)) < 0) {
slurm_error ("Failed to open /dev/null: %m");
}
else {
dup2 (devnull, STDOUT_FILENO);
dup2 (devnull, STDIN_FILENO);
dup2 (devnull, STDERR_FILENO);
close (devnull);
}
}
int slurm_spank_task_init (spank_t sp, int ac, char **av)
{
int taskid;
int rfd;
struct winsize ws;
struct winsize *wsp = NULL;
if (!do_pty)
return (0);
spank_get_item (sp, S_TASK_GLOBAL_ID, &taskid);
if (taskid != 0) {
if (!no_close_stdio (sp))
close_stdio ();
return (0);
}
if ((rfd = pty_connect_back (sp)) < 0) {
slurm_error ("Failed to connect back to pty server");
}
if (get_winsize (sp, &ws))
wsp = &ws;
if ((pid = forkpty (&master, NULL, NULL, wsp)) < 0) {
slurm_error ("Failed to allocate a pty for rank 0: %m\n");
return (0);
}
else if (pid == 0) {
/* Child. Continue with SLURM code */
return (0);
}
/* Parent: process data from client */
while (1) {
struct pollfd fds[3];
int rc;
int nfds = 2;
fd_set_nonblocking (master);
fd_set_nonblocking (STDIN_FILENO);
fds[0].fd = master;
fds[1].fd = STDIN_FILENO;
fds[0].events = POLLIN | POLLERR;
fds[1].events = POLLIN | POLLERR;
if (rfd >= 0) {
fd_set_nonblocking (rfd);
fds[2].fd = rfd;
fds[2].events = POLLIN | POLLERR;
nfds++;
}
if ((rc = poll (fds, 3, -1)) < 0) {
slurm_error ("poll: %m\n");
exit (1);
}
if (fds[0].revents & POLLERR) {
check_for_slave_exit ();
continue;
}
if (fds[0].revents & POLLIN)
process_pty ();
if (fds[1].revents & POLLIN)
process_stdin ();
if (fds[2].revents & POLLIN)
process_winsz_event (rfd, master);
check_for_slave_exit ();
}
return (0);
}
static void pty_restore (void)
{
/* STDIN is probably closed by now */
if (tcsetattr (STDOUT_FILENO, TCSANOW, &termdefaults) < 0)
fprintf (stderr, "tcsetattr: %s\n", strerror (errno));
}
static int set_winsize (spank_t sp)
{
struct winsize ws;
char buf[64];
ioctl (STDIN_FILENO, TIOCGWINSZ, &ws);
snprintf (buf, sizeof (buf), "%d", ws.ws_row);
setenv ("SLURM_PTY_WIN_ROW", buf, 1);
snprintf (buf, sizeof (buf), "%d", ws.ws_col);
setenv ("SLURM_PTY_WIN_COL", buf, 1);
return (0);
}
static void sigset_sigwinch (sigset_t *pset)
{
sigemptyset (pset);
sigaddset (pset, SIGWINCH);
}
static int notify_winsize_change (int fd)
{
struct winsize ws;
ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws);
write_pty_winsize (fd, &ws);
return (0);
}
/*
* Detect when a window size change event occurs.
*/
static int winch = 0;
static void handle_sigwinch (int sig)
{
winch = 1;
signal (SIGWINCH, handle_sigwinch);
}
static void * pty_thread (void *arg)
{
int fd;
sigset_t set;
sigset_sigwinch (&set);
pthread_sigmask (SIG_UNBLOCK, &set, NULL);
signal (SIGWINCH, handle_sigwinch);
if ((fd = accept (listenfd, NULL, NULL)) < 0) {
slurm_error ("pty: accept: %m");
return NULL;
}
for (;;) {
poll (NULL, 0, -1);
if (winch && notify_winsize_change (fd) < 0)
return NULL;
winch = 0;
}
return (NULL);
}
static int bind_wild (int sockfd)
{
socklen_t len;
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(0); /* bind ephemeral port */
if (bind (sockfd, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
slurm_error ("bind: %m\n");
return (-1);
}
len = sizeof(sin);
if (getsockname(sockfd, (struct sockaddr *) &sin, &len) < 0)
return (-1);
return ntohs(sin.sin_port);
}
static int do_listen (int *fd, short *port)
{
int rc, val;
if ((*fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
return -1;
val = 1;
rc = setsockopt(*fd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int));
if (rc > 0) {
goto cleanup;
}
*port = bind_wild (*fd);
if ((rc = listen(*fd, 16)) < 0) {
slurm_error ("listen: %m");
goto cleanup;
}
return (0);
cleanup:
close (*fd);
return (-1);
}
static void set_pty_env (short port)
{
char buf [64];
snprintf (buf, sizeof (buf), "%hu", port);
setenv ("SLURM_PTY_PORT", buf, 1);
}
static int pty_thread_create (spank_t sp)
{
short port;
int err;
pthread_attr_t attr;
pthread_t tid;
if (do_listen (&listenfd, &port) < 0) {
slurm_error ("Unable to create pty listen port: %m");
return (-1);
}
set_pty_env (port);
pthread_attr_init (&attr);
pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
err = pthread_create (&tid, &attr, &pty_thread, NULL);
pthread_attr_destroy (&attr);
if (err)
return (-1);
return (0);
}
static void block_sigwinch (void)
{
sigset_t set;
sigset_sigwinch (&set);
pthread_sigmask (SIG_BLOCK, &set, NULL);
}
int slurm_spank_local_user_init (spank_t sp, int ac, char **av)
{
struct termios term;
int fd = STDIN_FILENO;
if (!do_pty)
return (0);
/* Save terminal settings for restore */
tcgetattr (fd, &termdefaults);
tcgetattr (fd, &term);
/* Set raw mode on local tty */
cfmakeraw (&term);
tcsetattr (fd, TCSANOW, &term);
atexit (&pty_restore);
set_winsize (sp);
block_sigwinch ();
pty_thread_create (sp);
return (0);
}