-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathremote.cxx
1007 lines (858 loc) · 28.1 KB
/
remote.cxx
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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// systemtap remote execution
// Copyright (C) 2010-2011 Red Hat Inc.
//
// This file is part of systemtap, and is free software. You can
// redistribute it and/or modify it under the terms of the GNU General
// Public License (GPL); either version 2, or (at your option) any
// later version.
#include "config.h"
extern "C" {
#include <fcntl.h>
#include <poll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
}
#include <cstdio>
#include <iomanip>
#include <memory>
#include <stdexcept>
#include <sstream>
#include <string>
#include <vector>
#include "buildrun.h"
#include "remote.h"
#include "util.h"
using namespace std;
// Decode URIs as per RFC 3986, though not bothering to be strict
class uri_decoder {
public:
const string uri;
string scheme, authority, path, query, fragment;
bool has_authority, has_query, has_fragment;
uri_decoder(const string& uri):
uri(uri), has_authority(false), has_query(false), has_fragment(false)
{
const string re =
"^([^:]+):(//[^/?#]*)?([^?#]*)(\\?[^#]*)?(#.*)?$";
vector<string> matches;
if (regexp_match(uri, re, matches) != 0)
throw runtime_error(_F("string doesn't appear to be a URI: %s", uri.c_str()));
scheme = matches[1];
if (!matches[2].empty())
{
has_authority = true;
authority = matches[2].substr(2);
}
path = matches[3];
if (!matches[4].empty())
{
has_query = true;
query = matches[4].substr(1);
}
if (!matches[5].empty())
{
has_fragment = true;
fragment = matches[5].substr(1);
}
}
};
// loopback target for running locally
class direct : public remote {
private:
pid_t child;
vector<string> args;
direct(systemtap_session& s): remote(s), child(0) {}
int start()
{
args = make_run_command(*s);
if (! staprun_r_arg.empty()) // PR13354
{
args.push_back ("-r");
args.push_back (staprun_r_arg);
}
pid_t pid = stap_spawn (s->verbose, args);
if (pid <= 0)
return 1;
child = pid;
return 0;
}
int finish()
{
if (child <= 0)
return 1;
int ret = stap_waitpid(s->verbose, child);
if (ret > 128)
s->print_warning(_F("%s exited with signal: %d (%s)",
args.front().c_str(), ret - 128,
strsignal(ret - 128)));
else if (ret > 0)
s->print_warning(_F("%s exited with status: %d",
args.front().c_str(), ret));
child = 0;
return ret;
}
public:
friend class remote;
virtual ~direct() { finish(); }
};
class stapsh : public remote {
private:
int interrupts_sent;
int fdin, fdout;
FILE *IN, *OUT;
string remote_version;
virtual void prepare_poll(vector<pollfd>& fds)
{
if (fdout >= 0 && OUT)
{
pollfd p = { fdout, POLLIN, 0 };
fds.push_back(p);
}
// need to send a signal?
if (fdin >= 0 && IN && interrupts_sent < pending_interrupts)
{
pollfd p = { fdin, POLLOUT, 0 };
fds.push_back(p);
}
}
virtual void handle_poll(vector<pollfd>& fds)
{
for (unsigned i=0; i < fds.size(); ++i)
if (fds[i].fd == fdin || fds[i].fd == fdout)
{
bool err = false;
// need to send a signal?
if (fds[i].revents & POLLOUT && IN &&
interrupts_sent < pending_interrupts)
{
if (send_command("quit\n") == 0)
++interrupts_sent;
else
err = true;
}
// have data to read?
if (fds[i].revents & POLLIN && OUT)
{
char buf[4096];
if (!prefix.empty())
{
// If we have a line prefix, then read lines one at a
// time and copy out with the prefix.
errno = 0;
while (fgets(buf, sizeof(buf), OUT))
cout << prefix << buf;
if (errno != EAGAIN)
err = true;
}
else
{
// Otherwise read an entire block of data at once.
size_t rc = fread(buf, 1, sizeof(buf), OUT);
if (rc > 0)
{
// NB: The buf could contain binary data,
// including \0, so write as a block instead of
// the usual <<string.
cout.write(buf, rc);
}
else
err = true;
}
}
// any errors?
if (err || fds[i].revents & ~(POLLIN|POLLOUT))
close();
}
}
string get_reply()
{
// Some schemes like unix may have stdout and stderr mushed together.
// There shouldn't be anything except dbug messages on stderr before we
// actually start running, and there's no get_reply after that. So
// we'll just loop and skip those that start with "stapsh:".
char reply[4096];
while (fgets(reply, sizeof(reply), OUT))
{
if (!startswith(reply, "stapsh:"))
return reply;
// Why not clog here? Well, once things get running we won't be
// able to distinguish stdout/err, so trying to fake it here would
// be less consistent than just keeping it merged.
cout << reply;
}
// Reached EOF, nothing to reply...
return "";
}
int send_command(const string& cmd)
{
if (!IN)
return 2;
if (fputs(cmd.c_str(), IN) < 0 ||
fflush(IN) != 0)
return 1;
return 0;
}
int send_file(const string& filename, const string& dest)
{
int rc = 0;
FILE* f = fopen(filename.c_str(), "r");
if (!f)
return 1;
struct stat fs;
rc = fstat(fileno(f), &fs);
if (!rc)
{
ostringstream cmd;
cmd << "file " << fs.st_size << " " << dest << "\n";
rc = send_command(cmd.str());
}
off_t i = 0;
while (!rc && i < fs.st_size)
{
char buf[4096];
size_t r = sizeof(buf);
if (fs.st_size - i < (off_t)r)
r = fs.st_size - i;
r = fread(buf, 1, r, f);
if (r == 0)
rc = 1;
else
{
size_t w = fwrite(buf, 1, r, IN);
if (w != r)
rc = 1;
else
i += w;
}
}
if (!rc)
rc = fflush(IN);
fclose(f);
if (!rc)
{
string reply = get_reply();
if (reply != "OK\n")
{
rc = 1;
if (s->verbose > 0)
{
if (reply.empty())
clog << _("stapsh file ERROR: no reply") << endl;
else
clog << _F("stapsh file replied %s", reply.c_str());
}
}
}
return rc;
}
static string qpencode(const string& str)
{
ostringstream o;
o << setfill('0') << hex;
for (const char* s = str.c_str(); *s; ++s)
if (*s >= 33 && *s <= 126 && *s != 61)
o << *s;
else
o << '=' << setw(2) << (unsigned)(unsigned char) *s;
return o.str();
}
protected:
stapsh(systemtap_session& s)
: remote(s), interrupts_sent(0),
fdin(-1), fdout(-1), IN(0), OUT(0)
{}
virtual int prepare()
{
int rc = 0;
string localmodule = s->tmpdir + "/" + s->module_name + ".ko";
string remotemodule = s->module_name + ".ko";
if ((rc = send_file(localmodule, remotemodule)))
return rc;
if (file_exists(localmodule + ".sgn") &&
(rc = send_file(localmodule + ".sgn", remotemodule + ".sgn")))
return rc;
if (!s->uprobes_path.empty())
{
string remoteuprobes = basename(s->uprobes_path.c_str());
if ((rc = send_file(s->uprobes_path, remoteuprobes)))
return rc;
if (file_exists(s->uprobes_path + ".sgn") &&
(rc = send_file(s->uprobes_path + ".sgn", remoteuprobes + ".sgn")))
return rc;
}
return rc;
}
virtual int start()
{
// Send the staprun args
// NB: The remote is left to decide its own staprun path
ostringstream run("run", ios::out | ios::ate);
vector<string> cmd = make_run_command(*s, ".", remote_version);
// PR13354: identify our remote index/url
if (strverscmp("1.7", remote_version.c_str()) <= 0 && // -r supported?
! staprun_r_arg.empty())
{
cmd.push_back ("-r");
cmd.push_back (staprun_r_arg);
}
for (unsigned i = 1; i < cmd.size(); ++i)
run << ' ' << qpencode(cmd[i]);
run << '\n';
int rc = send_command(run.str());
if (!rc)
{
string reply = get_reply();
if (reply != "OK\n")
{
rc = 1;
if (s->verbose > 0)
{
if (reply.empty())
clog << _("stapsh run ERROR: no reply") << endl;
else
clog << _F("stapsh run replied %s", reply.c_str());
}
}
}
if (!rc)
{
long flags = fcntl(fdout, F_GETFL) | O_NONBLOCK;
fcntl(fdout, F_SETFL, flags);
}
else
// If run failed for any reason, then this
// connection is effectively dead to us.
close();
return rc;
}
void close()
{
if (IN) fclose(IN);
if (OUT) fclose(OUT);
IN = OUT = NULL;
fdin = fdout = -1;
}
virtual int finish()
{
close();
return 0;
}
void set_child_fds(int in, int out)
{
if (fdin >= 0 || fdout >= 0 || IN || OUT)
throw runtime_error(_("stapsh file descriptors already set"));
fdin = in;
fdout = out;
IN = fdopen(fdin, "w");
OUT = fdopen(fdout, "r");
if (!IN || !OUT)
throw runtime_error(_("invalid file descriptors for stapsh"));
if (send_command("stap " VERSION "\n"))
throw runtime_error(_("error sending hello to stapsh"));
string reply = get_reply();
if (reply.empty())
throw runtime_error(_("error receiving hello from stapsh"));
// stapsh VERSION MACHINE RELEASE
vector<string> uname;
tokenize(reply, uname, " \t\r\n");
if (uname.size() != 4 || uname[0] != "stapsh")
throw runtime_error(_("failed to get uname from stapsh"));
// We assume that later versions will know how to talk to us.
// Looking backward, we use this for make_run_command().
this->remote_version = uname[1];
this->s = s->clone(uname[2], uname[3]);
}
public:
virtual ~stapsh() { close(); }
};
// direct_stapsh is meant only for testing, as a way to exercise the stapsh
// mechanisms without requiring test machines to have actual remote access.
class direct_stapsh : public stapsh {
private:
pid_t child;
direct_stapsh(systemtap_session& s)
: stapsh(s), child(0)
{
int in, out;
vector<string> cmd;
cmd.push_back(BINDIR "/stapsh");
if (s.perpass_verbose[4] > 1)
cmd.push_back("-v");
if (s.perpass_verbose[4] > 2)
cmd.push_back("-v");
// mask signals while we spawn, so we can simulate manual signals to
// the "remote" target, as we must for the real ssh_remote case.
{
stap_sigmasker masked;
child = stap_spawn_piped(s.verbose, cmd, &in, &out);
}
if (child <= 0)
throw runtime_error(_("error launching stapsh"));
try
{
set_child_fds(in, out);
}
catch (runtime_error&)
{
finish();
throw;
}
}
virtual int finish()
{
int rc = stapsh::finish();
if (child <= 0)
return rc;
int rc2 = stap_waitpid(s->verbose, child);
child = 0;
return rc ?: rc2;
}
public:
friend class remote;
virtual ~direct_stapsh() { finish(); }
};
// Connect to an existing stapsh on a unix socket.
class unix_stapsh : public stapsh {
private:
unix_stapsh(systemtap_session& s, const uri_decoder& ud)
: stapsh(s)
{
sockaddr_un server;
server.sun_family = AF_UNIX;
if (ud.path.empty())
throw runtime_error(_("unix target requires a /path"));
if (ud.path.size() > sizeof(server.sun_path) - 1)
throw runtime_error(_("unix target /path is too long"));
strcpy(server.sun_path, ud.path.c_str());
if (ud.has_authority)
throw runtime_error(_("unix target doesn't support a hostname"));
if (ud.has_query)
throw runtime_error(_("unix target URI doesn't support a ?query"));
if (ud.has_fragment)
throw runtime_error(_("unix target URI doesn't support a #fragment"));
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (fd <= 0)
throw runtime_error(_("error opening a socket"));
if (connect(fd, (struct sockaddr *)&server, SUN_LEN(&server)) < 0)
{
const char *msg = strerror(errno);
::close(fd);
throw runtime_error(_F("error connecting to socket: %s", msg));
}
// Try to dup it, so class stapsh can have truly separate fds for its
// fdopen handles. If it fails for some reason, it can still work with
// just one though.
int fd2 = dup(fd);
if (fd2 < 0)
fd2 = fd;
try
{
set_child_fds(fd, fd2);
}
catch (runtime_error&)
{
finish();
::close(fd);
::close(fd2);
throw;
}
}
public:
friend class remote;
virtual ~unix_stapsh() { finish(); }
};
// stapsh-based ssh_remote
class ssh_remote : public stapsh {
private:
pid_t child;
ssh_remote(systemtap_session& s): stapsh(s), child(0) {}
int connect(const string& host, const string& port)
{
int rc = 0;
int in, out;
vector<string> cmd;
cmd.push_back("ssh");
cmd.push_back("-q");
cmd.push_back(host);
if (!port.empty())
{
cmd.push_back("-p");
cmd.push_back(port);
}
// This is crafted so that we get a silent failure with status 127 if
// the command is not found. The combination of -P and $cmd ensures
// that we pull the command out of the PATH, not aliases or such.
string stapsh_cmd = "cmd=`type -P stapsh || exit 127` && exec \"$cmd\"";
if (s->perpass_verbose[4] > 1)
stapsh_cmd.append(" -v");
if (s->perpass_verbose[4] > 2)
stapsh_cmd.append(" -v");
// NB: We need to explicitly choose bash, as $SHELL may be weird...
cmd.push_back("/bin/bash -c '" + stapsh_cmd + "'");
// mask signals while we spawn, so we can manually send even tty
// signals *through* ssh rather than to ssh itself
{
stap_sigmasker masked;
child = stap_spawn_piped(s->verbose, cmd, &in, &out);
}
if (child <= 0)
throw runtime_error(_("error launching stapsh"));
try
{
set_child_fds(in, out);
}
catch (runtime_error&)
{
rc = finish();
// ssh itself signals errors with 255
if (rc == 255)
throw runtime_error(_("error establishing ssh connection"));
// If rc == 127, that's command-not-found, so we let ::create()
// try again in legacy mode. But only do this if there's a single
// remote, as the old code didn't handle ttys well with multiple
// remotes. Otherwise, throw up again. *barf*
if (rc != 127 || s->remote_uris.size() > 1)
throw;
}
return rc;
}
int finish()
{
int rc = stapsh::finish();
if (child <= 0)
return rc;
int rc2 = stap_waitpid(s->verbose, child);
child = 0;
return rc ?: rc2;
}
public:
static remote* create(systemtap_session& s, const string& host);
static remote* create(systemtap_session& s, const uri_decoder& ud);
virtual ~ssh_remote() { finish(); }
};
// ssh connection without stapsh, for legacy stap installations
// NB: ssh commands use a tty (-t) so signals are passed along to the remote.
// It does this by putting the local tty in raw mode, so it only works for tty
// signals, and only for a single remote at a time.
class ssh_legacy_remote : public remote {
private:
vector<string> ssh_args, scp_args;
string ssh_control;
string host, port, tmpdir;
pid_t child;
ssh_legacy_remote(systemtap_session& s, const string& host, const string& port)
: remote(s), host(host), port(port), child(0)
{
open_control_master();
try
{
get_uname();
}
catch (runtime_error&)
{
close_control_master();
throw;
}
}
void open_control_master()
{
static unsigned index = 0;
if (s->tmpdir.empty()) // sanity check, shouldn't happen
throw runtime_error(_("No tmpdir available for ssh control master"));
ssh_control = s->tmpdir + "/ssh_remote_control_" + lex_cast(++index);
scp_args.clear();
scp_args.push_back("scp");
scp_args.push_back("-q");
scp_args.push_back("-o");
scp_args.push_back("ControlPath=" + ssh_control);
ssh_args = scp_args;
ssh_args[0] = "ssh";
ssh_args.push_back(host);
if (!port.empty())
{
scp_args.push_back("-P");
scp_args.push_back(port);
ssh_args.push_back("-p");
ssh_args.push_back(port);
}
// NB: ssh -f will stay in the foreground until authentication is
// complete and the control socket is created, so we know it's ready to
// go when stap_system returns.
vector<string> cmd = ssh_args;
cmd.push_back("-f");
cmd.push_back("-N");
cmd.push_back("-M");
int rc = stap_system(s->verbose, cmd);
if (rc != 0)
throw runtime_error(_F("failed to create an ssh control master for %s : rc= %d",
host.c_str(), rc));
if (s->verbose>1)
clog << _F("Created ssh control master at %s",
lex_cast_qstring(ssh_control).c_str()) << endl;
}
void close_control_master()
{
if (ssh_control.empty())
return;
vector<string> cmd = ssh_args;
cmd.push_back("-O");
cmd.push_back("exit");
int rc = stap_system(s->verbose, cmd, true, true);
if (rc != 0)
cerr << _F("failed to stop the ssh control master for %s : rc=%d",
host.c_str(), rc) << endl;
ssh_control.clear();
scp_args.clear();
ssh_args.clear();
}
void get_uname()
{
ostringstream out;
vector<string> uname;
vector<string> cmd = ssh_args;
cmd.push_back("-t");
cmd.push_back("uname -rm");
int rc = stap_system_read(s->verbose, cmd, out);
if (rc == 0)
tokenize(out.str(), uname, " \t\r\n");
if (uname.size() != 2)
throw runtime_error(_F("failed to get uname from %s : rc= %d", host.c_str(), rc));
const string& release = uname[0];
const string& arch = uname[1];
// XXX need to deal with command-line vs. implied arch/release
this->s = s->clone(arch, release);
}
int start()
{
int rc;
string localmodule = s->tmpdir + "/" + s->module_name + ".ko";
string tmpmodule;
// Make a remote tempdir.
{
ostringstream out;
vector<string> vout;
vector<string> cmd = ssh_args;
cmd.push_back("-t");
cmd.push_back("mktemp -d -t stapXXXXXX");
rc = stap_system_read(s->verbose, cmd, out);
if (rc == 0)
tokenize(out.str(), vout, "\r\n");
if (vout.size() != 1)
{
cerr << _F("failed to make a tempdir on %s : rc=%d",
host.c_str(), rc) << endl;
return -1;
}
tmpdir = vout[0];
tmpmodule = tmpdir + "/" + s->module_name + ".ko";
}
// Transfer the module.
if (rc == 0)
{
vector<string> cmd = scp_args;
cmd.push_back(localmodule);
cmd.push_back(host + ":" + tmpmodule);
rc = stap_system(s->verbose, cmd);
if (rc != 0)
cerr << _F("failed to copy the module to %s : rc=%d",
host.c_str(), rc) << endl;
}
// Transfer the module signature.
if (rc == 0 && file_exists(localmodule + ".sgn"))
{
vector<string> cmd = scp_args;
cmd.push_back(localmodule + ".sgn");
cmd.push_back(host + ":" + tmpmodule + ".sgn");
rc = stap_system(s->verbose, cmd);
if (rc != 0)
cerr << _F("failed to copy the module signature to %s : rc=%d",
host.c_str(), rc) << endl;
}
// What about transfering uprobes.ko? In this ssh "legacy" mode, we
// don't the remote systemtap version, but -uPATH wasn't added until
// 1.4. Rather than risking a getopt error, we'll just assume that
// this isn't supported. The remote will just have to provide its own
// uprobes.ko in SYSTEMTAP_RUNTIME or already loaded.
// Run the module on the remote.
if (rc == 0) {
vector<string> cmd = ssh_args;
cmd.push_back("-t");
// We don't know the actual version, but all <=1.3 are approx equal.
vector<string> staprun_cmd = make_run_command(*s, tmpdir, "1.3");
staprun_cmd[0] = "staprun"; // NB: The remote decides its own path
// NB: PR13354: we assume legacy installations don't have
// staprun -r support, so we ignore staprun_r_arg.
cmd.push_back(cmdstr_join(staprun_cmd));
pid_t pid = stap_spawn(s->verbose, cmd);
if (pid > 0)
child = pid;
else
{
cerr << _F("failed to run the module on %s : ret=%d",
host.c_str(), pid) << endl;
rc = -1;
}
}
return rc;
}
int finish()
{
int rc = 0;
if (child > 0)
{
rc = stap_waitpid(s->verbose, child);
child = 0;
}
if (!tmpdir.empty())
{
// Remove the tempdir.
// XXX need to make sure this runs even with e.g. CTRL-C exits
vector<string> cmd = ssh_args;
cmd.push_back("-t");
cmd.push_back("rm -r " + cmdstr_quoted(tmpdir));
int rc2 = stap_system(s->verbose, cmd);
if (rc2 != 0)
cerr << _F("failed to delete the tempdir on %s : rc=%d",
host.c_str(), rc2) << endl;
if (rc == 0)
rc = rc2;
tmpdir.clear();
}
close_control_master();
return rc;
}
public:
friend class ssh_remote;
virtual ~ssh_legacy_remote()
{
close_control_master();
}
};
// Try to establish a stapsh connection to the remote, but fallback
// to the older mechanism if the command is not found.
remote*
ssh_remote::create(systemtap_session& s, const string& target)
{
string port, host = target;
size_t i = host.find(':');
if (i != string::npos)
{
port = host.substr(i + 1);
host.erase(i);
}
auto_ptr<ssh_remote> p (new ssh_remote(s));
int rc = p->connect(host, port);
if (rc == 0)
return p.release();
else if (rc == 127) // stapsh command not found
return new ssh_legacy_remote(s, host, port); // try legacy instead
return NULL;
}
remote*
ssh_remote::create(systemtap_session& s, const uri_decoder& ud)
{
if (!ud.has_authority || ud.authority.empty())
throw runtime_error(_("ssh target requires a hostname"));
if (!ud.path.empty() && ud.path != "/")
throw runtime_error(_("ssh target URI doesn't support a /path"));
if (ud.has_query)
throw runtime_error(_("ssh target URI doesn't support a ?query"));
if (ud.has_fragment)
throw runtime_error(_("ssh target URI doesn't support a #fragment"));
return create(s, ud.authority);
}
remote*
remote::create(systemtap_session& s, const string& uri, int idx)
{
remote *it = 0;
try
{
if (uri.find(':') != string::npos)
{
const uri_decoder ud(uri);
// An ssh "host:port" is ambiguous with a URI "scheme:path".
// So if it looks like a number, just assume ssh.
if (!ud.has_authority && !ud.has_query &&
!ud.has_fragment && !ud.path.empty() &&
ud.path.find_first_not_of("1234567890") == string::npos)
it = ssh_remote::create(s, uri);
else if (ud.scheme == "direct")
it = new direct(s);
else if (ud.scheme == "stapsh")
it = new direct_stapsh(s);
else if (ud.scheme == "unix")
it = new unix_stapsh(s, ud);
else if (ud.scheme == "ssh")
it = ssh_remote::create(s, ud);
else
throw runtime_error(_F("unrecognized URI scheme '%s' in remote: %s",
ud.scheme.c_str(), uri.c_str()));
}
else
// XXX assuming everything else is ssh for now...
it = ssh_remote::create(s, uri);
}
catch (std::runtime_error& e)
{
cerr << e.what() << " on remote '" << uri << "'" << endl;
it = 0;
}
if (it && idx >= 0) // PR13354: remote metadata for staprun -r IDX:URI
{
stringstream r_arg;
r_arg << idx << ":" << uri;
it->staprun_r_arg = r_arg.str();
}
return it;
}
int
remote::run(const vector<remote*>& remotes)
{
// NB: the first failure "wins"
int ret = 0, rc = 0;
for (unsigned i = 0; i < remotes.size() && !pending_interrupts; ++i)
{
remote *r = remotes[i];
r->s->verbose = r->s->perpass_verbose[4];
if (r->s->use_remote_prefix)
r->prefix = lex_cast(i) + ": ";
rc = r->prepare();
if (rc)
return rc;
}
for (unsigned i = 0; i < remotes.size() && !pending_interrupts; ++i)
{
rc = remotes[i]->start();
if (!ret)
ret = rc;
}
// mask signals while we're preparing to poll
{
stap_sigmasker masked;
// polling loop for remotes that have fds to watch
for (;;)
{
vector<pollfd> fds;
for (unsigned i = 0; i < remotes.size(); ++i)
remotes[i]->prepare_poll (fds);
if (fds.empty())
break;
rc = ppoll (&fds[0], fds.size(), NULL, &masked.old);
if (rc < 0 && errno != EINTR)
break;
for (unsigned i = 0; i < remotes.size(); ++i)
remotes[i]->handle_poll (fds);
}
}
for (unsigned i = 0; i < remotes.size(); ++i)
{
rc = remotes[i]->finish();
if (!ret)
ret = rc;