-
Notifications
You must be signed in to change notification settings - Fork 1
/
dsh.c
593 lines (528 loc) · 18 KB
/
dsh.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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
* DSH / dancer's shell or the distributed shell
* Copyright (C) 2001-2007 Junichi Uekawa
*
* This program 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 program 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, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Main program.
*/
#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include "dsh.h"
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#include "linkedlist.h"
#include "parameter.h"
#include "gettext.h"
#define _(A) gettext(A)
char *remoteshell_command = "rsh";
int verbose_flag = 0; /* verbosity flag */
int wait_shell = 1; /* waiting for shell to execute (concurrence) */
int pipe_option = 0; /* show machine names -- piping option. */
int num_topology = 1; /* number of topology to use as a block to execute rsh.
1 = for-loop
2 = binary-tree
4 = quad-tree.
*/
linkedlist *remoteshell_command_opt_r =
NULL; /* reverse-ordered list of rsh options. */
int buffer_size = 1024; /* buffer size. */
int forklimit = 0; /* do not limit number of forks */
/** current fork count; required for forklimit processing */
static int currentforkcount = 0;
/* function defining "getline" */
#ifndef HAVE_GETLINE
/* an imcomplete, and wrong implementation of getline */
ssize_t getline(char **LINEPTR, size_t *N, FILE *STREAM) {
const int GETLINESIZE = 256;
int fgl;
if (!*LINEPTR) *LINEPTR = malloc(GETLINESIZE);
if (*N != GETLINESIZE) *LINEPTR = realloc(*LINEPTR, GETLINESIZE);
if (!*LINEPTR) {
return -1;
}
if (!fgets(*LINEPTR, GETLINESIZE - 1, STREAM)) return -1;
*N = strlen(*LINEPTR);
return GETLINESIZE;
}
#endif
/* define asprintf if it doesn't exist */
#ifndef HAVE_ASPRINTF
#include <stdarg.h>
int asprintf(char **strp, const char *fmt, ...) {
ssize_t buflen = 50 * strlen(fmt); /* pick a number, any number */
*strp = malloc(buflen);
if (*strp) {
va_list ap;
va_start(ap, fmt);
vsnprintf(*strp, buflen, fmt, ap);
va_end(ap);
return buflen;
}
return -1;
}
#endif /* HAVE_ASPRINTF */
#if !defined(WAIT_ANY)
#define WAIT_ANY ((pid_t)-1)
#endif /* WAIT_ANY */
/**
* A process that just echoes back, reading fd_in using getline,
* and writing to fd_out
*
* @returns -1 on error, 0 on success.
*/
static int do_echoing_back_process(int fd_in, int fd_out, const char *prompt) {
char *buf = NULL;
size_t bufsize = 0;
FILE *f = fdopen(fd_in, "r");
FILE *standard_output = fdopen(fd_out, "w");
setlinebuf(standard_output);
if (!f || !standard_output) {
fprintf(stderr, _("%s: Could not open descriptor [%i] or [%i]\n"), PACKAGE,
fd_in, fd_out);
return -1;
}
while (0 < getline(&buf, &bufsize, f)) {
fprintf(standard_output, "%s: %s", prompt, buf);
}
if (buf) free(buf);
return 0;
}
/**
* Forks the output pipe routine for fd[x]
* this is the main process, forking the child process.
*
* @returns -1 on error, 0 on success
*/
static int fork_and_pipe_echoing_routine(
/** File descriptor ID */ int fdid, const char *machinename) {
int childid;
int capture_fd[2];
if (-1 == pipe(capture_fd)) {
fprintf(stderr, _("%s: cannot create pipe\n"), PACKAGE);
return -1;
}
if (0 != (childid = fork())) {
int status;
/* The parent process */
close(capture_fd[1]);
if (do_echoing_back_process(capture_fd[0], fdid, machinename))
exit(EXIT_FAILURE);
waitpid(WAIT_ANY, &status, 0); /* need to error check this ? */
assert(WIFEXITED(status));
exit(WEXITSTATUS(status));
} else if (childid == -1) { /* do some error processing */
fprintf(stderr, _("%s: Cannot spawn process\n"), PACKAGE);
return -1;
} else {
/* The child process -- this is the main trunc
I don't want dsh to wait for the termination of this process.
*/
if (-1 == dup2(capture_fd[1], fdid)) {
fprintf(stderr, PACKAGE ": %s\n", _("Failed playing with pipe"));
return -1;
}
close(capture_fd[0]);
close(capture_fd[1]);
}
return 0;
}
/**
* Actually execute the program, with maybe a pipe process being forked.
* the actual program execution is done by llexec.
* This part does the optional pipe construction or direct llexec
* depending on the flags.
*
* @returns -1 on failure
*/
static int do_execute_with_optional_pipe(const char *remoteshell_command,
const linkedlist *commandline,
int pipe_option, /** 1=pipe output and
put pretty flags*/
const char *machinename) {
if (pipe_option & PIPE_OPTION_OUTPUT) /* pipe the outputs */
{
if (fork_and_pipe_echoing_routine(1, machinename) ||
fork_and_pipe_echoing_routine(2, machinename)) {
fprintf(stderr, _("%s: Failed on constructing a pipe and forking\n"),
PACKAGE);
return -1;
}
}
llexec(remoteshell_command, commandline);
fprintf(stderr, _("%s: Failed executing %s with llexec call\n"), PACKAGE,
remoteshell_command);
exit(EXIT_FAILURE);
}
/**
The exit code to give after successful termination of the dsh program.
*/
int dsh_exit_code = 0;
/**
How to update the dsh exit code.
dsh may return exit code generated from all of its child processes,
this is the core engine.
*/
void dsh_update_exit_code(
int exit_code_of_child /** The exit code of the child process*/) {
/* default behavior was to always ignore. */
if ((exit_code_of_child != EXIT_SUCCESS) && (dsh_exit_code == EXIT_SUCCESS)) {
dsh_exit_code = exit_code_of_child;
}
}
static int *fd_output_array = NULL; /** array of fd to duplicate input to */
static int fd_output_array_len = 0;
/**
* adds an fd to the list of fds to be processed by the input-duplication
* daemon.
*/
static void add_fd_to_output_array(int fd) {
fd_output_array_len++;
fd_output_array = realloc(fd_output_array, fd_output_array_len * sizeof(int));
fd_output_array[fd_output_array_len - 1] = fd;
}
/**
* spawns rsh/ssh session on single machine
* This is still the main process forking the child process.
*
* @returns -1 on failure, 0 on success
*/
static int execute_rsh_single(const char *remoteshell_command,
const linkedlist *remoteshell_command_opt_r,
const char *param_machinename,
const linkedlist *rshcommandline_r,
int pipe_option /** The pipe option */) {
int childpid;
int childstatus = 0;
int input_pipe[2];
if (pipe_option & PIPE_OPTION_INPUT) {
pipe(input_pipe);
}
/* Execute the rsh process */
if (0 == (childpid = fork())) { /* child process */
/* Must NOT fork again from this point until I say otherwise */
linkedlist *tmp = NULL;
char *machinename =
strdup(param_machinename); /* we will always exit() from here, so
no need to free this myself. */
char *username = machinename;
linkedlist *local_remoteshell_command_opt_r =
lldup(remoteshell_command_opt_r);
/* input piping */
if (pipe_option & PIPE_OPTION_INPUT) {
dup2(input_pipe[0], 0);
close(input_pipe[1]);
close(input_pipe[0]);
/* Must NOT fork again before this point. */
}
/* process to handle username@hostname */
if (NULL !=
(machinename = strchr(machinename, '@'))) { /* username was specified */
*(machinename++) = 0; /* cut the string at @ and point to following
string (machine name) */
local_remoteshell_command_opt_r =
lladd(local_remoteshell_command_opt_r, "-l");
local_remoteshell_command_opt_r =
lladd(local_remoteshell_command_opt_r, username);
} else { /* no username was specified */
machinename = username;
username = NULL;
}
tmp = llcat(tmp, local_remoteshell_command_opt_r);
tmp = lladd(tmp, machinename);
tmp = llcat(tmp, lldup(rshcommandline_r));
tmp = llreverse(tmp);
if (verbose_flag) /* debugging */
{
printf(_("DUMPing parameters passed to llexec\n"));
lldump(tmp);
}
if (-1 == (do_execute_with_optional_pipe(remoteshell_command, tmp,
pipe_option, param_machinename))) {
fprintf(stderr, _("%s: Failed to execute remote shell command %s\n"),
PACKAGE, remoteshell_command);
exit(EXIT_FAILURE);
}
/* what follows should never occur */
fprintf(stderr,
_("%s: Unexpected error occurred, do_execute_with_optional_pipe "
"failed, and returned an error code that is not -1\n"),
PACKAGE);
exit(EXIT_FAILURE);
} else if (childpid == -1) {
/* fork failed */
fprintf(stderr, _("%s: fork failed, in execute_rsh_single\n"), PACKAGE);
return -1;
} else /* parent process */
{
if (pipe_option & PIPE_OPTION_INPUT) {
close(input_pipe[0]);
/* add input_pipe[1] to the array of outputs one
must handle here... */
add_fd_to_output_array(input_pipe[1]);
}
currentforkcount++;
if ((wait_shell) || /* if wait_shell is specified */
((forklimit > 0) &&
(currentforkcount > forklimit)) /* or fork limit is exceeded */
) {
/* wait for termination,
if it was required */
if (verbose_flag)
printf("%s\n", _("... Waiting for process to end with waitpid"));
if (-1 !=
waitpid(
wait_shell
? /* wait for childpid; or ANY if forklimit.
Setting this to ANY means it will go ahead when
any process ends, while waiting for childpid
means to wait for last process, which is less efficient.
*/
childpid
: /* wait_shell */
WAIT_ANY /* forklimit */
,
&childstatus, 0)) {
assert(WIFEXITED(childstatus));
dsh_update_exit_code(WEXITSTATUS(childstatus));
} else {
assert(0); /* weird error condition. */
}
}
return 0;
}
/* nobody reaches here. */
}
/**
* spawns dsh session on other machines
*
* TODO: quote properly.
* @return -1 on failure.
*/
static int execute_rsh_multiple(const char *remoteshell_command,
const linkedlist *remoteshell_command_opt_r,
const linkedlist *machinelist, int nummachines,
const linkedlist *rshcommandline_r) {
linkedlist *extraparam = NULL;
linkedlist *tmp;
linkedlist *tmp_start;
char *buffer;
extraparam = lladd(extraparam, DSH_COMMANDLINE);
tmp = tmp_start = lldup(machinelist);
while (tmp && (nummachines-- > 0)) {
extraparam = lladd(extraparam, "-m");
extraparam = lladd(extraparam, tmp->string);
tmp = tmp->next;
}
llfree(tmp_start);
tmp = tmp_start = llreverse(lldup(remoteshell_command_opt_r));
while (tmp) {
extraparam = lladd(extraparam, "-o");
extraparam = lladd(extraparam, tmp->string);
tmp = tmp->next;
}
llfree(tmp_start);
/* hand over any extra options here. */
if (verbose_flag) extraparam = lladd(extraparam, "-v");
if (wait_shell)
extraparam = lladd(extraparam, "-w");
else
extraparam = lladd(extraparam, "-c");
if (pipe_option) extraparam = lladd(extraparam, "-M");
if (asprintf(&buffer, "-n%i", num_topology) < 0) {
fprintf(stderr, _("%s: asprintf failed\n"), PACKAGE);
return -1;
}
extraparam = lladd(extraparam, buffer);
free(buffer);
/* add remoteshell command */
if (asprintf(&buffer, "-r%s", remoteshell_command) < 0) {
fprintf(stderr, _("%s: asprintf failed\n"), PACKAGE);
return -1;
}
extraparam = lladd(extraparam, buffer);
free(buffer);
/* end of extra options. */
extraparam = lladd(extraparam, "--");
rshcommandline_r = llcat(extraparam, lldup(rshcommandline_r));
return execute_rsh_single(remoteshell_command, remoteshell_command_opt_r,
machinelist->string, rshcommandline_r, 0);
}
/**
execute remote shell
executes dsh on other machines, or
executes rsh to execute command.
depending on the parameter and
status.
@return 0 on success, -1 on failure.
*/
static int execute_rsh(
const char *remoteshell_command,
const linkedlist *remoteshell_command_opt_r, const linkedlist *machinelist,
int nummachines /** The number of machines to invoke rsh at the same time */
,
const linkedlist *rshcommandline_r) {
if (nummachines == 1)
return execute_rsh_single(remoteshell_command, remoteshell_command_opt_r,
machinelist->string, rshcommandline_r,
pipe_option);
else
return execute_rsh_multiple(remoteshell_command, remoteshell_command_opt_r,
machinelist, nummachines, rshcommandline_r);
}
/**
* Forks off to do read from input, and duplicate it to
* output the same thing to all of individual remote processes
*
* Used for processing the -i option
*
* By the time this routine is called, the child rsh processes are
* up and running.
*/
void run_input_forking_child_processes_process() {
char *buf;
int count;
int i;
switch (fork()) {
case 0:
/* the child process */
buf = malloc_with_error(buffer_size);
signal(SIGPIPE, SIG_IGN); /* I'll handle SIGPIPE with EPIPE */
/* This routine blocks even if all processes associated with
fd_output_array is terminated */
while ((count = read(0, buf, buffer_size)) != -1) {
if (count == 0) /* if there is zero-byte read, it is an end-of-file */
break;
for (i = 0; i < fd_output_array_len; ++i) {
if (write(fd_output_array[i], buf, count) == -1) {
/* handle errors */
if (errno == EPIPE) {
fprintf(stderr, _("%s: Process terminated (before write).\n"),
PACKAGE);
/* pipe ended */
goto out_of_while;
/* This is going to terminate ALL processes
even if only one server terminated early. */
} else
perror("dsh: write");
}
}
}
out_of_while:
for (i = 0; i < fd_output_array_len; ++i) {
close(fd_output_array[i]);
}
exit(EXIT_SUCCESS);
case -1:
/* fork failed */
fprintf(stderr, _("%s: fork failed trying to dupilcate input\n"),
PACKAGE);
break;
default:
/* parent process closes the output array. */
for (i = 0; i < fd_output_array_len; ++i) {
close(fd_output_array[i]);
}
if (verbose_flag)
printf(_("%s: forked off input forking process\n"), PACKAGE);
/* do not read from stdin. */
open_devnull();
}
}
/**
Called after the command-line parsing.
do the shell execution without caring
about what actually would happen
@return -1 on failure
@return dsh_exit_code on success
return value is used as dsh exit code.
*/
int do_shell(linkedlist *machinelist, linkedlist *rshcommandline_r) {
int nummachines = llcount(machinelist) / num_topology;
int i;
/* topology 1 is special. */
if (nummachines == 0 || (num_topology == 1)) {
nummachines = 1;
}
while (machinelist) {
if (wait_shell && verbose_flag) {
fprintf(stderr, _("--- Executing on %s \n"), machinelist->string);
}
if (execute_rsh(remoteshell_command, remoteshell_command_opt_r, machinelist,
nummachines, rshcommandline_r)) {
fprintf(stderr, _("%s: execute_rsh failed, rsh invocation failure.\n"),
PACKAGE);
return -1;
}
/* skip the machines that program was executed on, and repeat */
for (i = 0; (i < nummachines) && machinelist; ++i)
machinelist = machinelist->next;
}
/* I have executed on all processes, I can now start cleaning up process... */
/* Try forking the input using fork, for -i option. */
if (pipe_option &= PIPE_OPTION_INPUT)
run_input_forking_child_processes_process();
if (!wait_shell) {
int childstatus = 0;
int childpid;
/* waiting for all. */
while (-1 != (childpid = waitpid(WAIT_ANY, &childstatus, 0))) {
/* Is child dead of signal ? i.e. segv, etc. */
if (WIFSIGNALED(childstatus)) {
fprintf(stderr, _("%s: Child process %i exited with signal %i\n"),
PACKAGE, childpid, WTERMSIG(childstatus));
fflush(stderr);
dsh_update_exit_code(2);
} else /* get exit code and update */
{
assert(WIFEXITED(childstatus));
dsh_update_exit_code(WEXITSTATUS(childstatus));
}
}
}
if (verbose_flag) fprintf(stderr, _("--- Terminated running\n"));
return dsh_exit_code;
}
/**
* The main code. Sets up internationalization.
*/
int main(int ac, char **av) {
char *buf = NULL;
setlocale(LC_ALL, "");
if (!textdomain(PACKAGE_NAME)) {
if (!bindtextdomain(PACKAGE_NAME, LOCALEDIR))
fprintf(stderr, "%s: failed to call bindtextdomain\n", PACKAGE);
}
/* load configuration files. */
load_configfile(DSH_CONF);
if (asprintf(&buf, "%s/.dsh/dsh.conf", getenv("HOME")) < 0) {
fprintf(stderr, _("%s: asprintf failed\n"), PACKAGE);
exit(1);
}
load_configfile(buf);
free(buf);
return parse_options(ac, av);
}