File: ssh1.c

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (87 lines) | stat: -rw-r--r-- 1,714 bytes parent folder | download
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
#include<stdio.h>
#include<libgen.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<sys/types.h>
#include<sys/wait.h>

int main(int argc, char *argv[])
{
  char *hostname = "localhost";

  char buf[4096];
  strcpy(buf, argv[0]);
  dirname(buf);
  strcat(buf, "/dmtcp1");
  char *dmtcp1Path = realpath(buf, NULL);

  int in[2], out[2], err[2];

  if (argc > 1) {
    hostname = argv[1];
  }

  if (pipe(in) == -1) {
    perror("pipe(in) failed");
  }
  if (pipe(out) == -1) {
    perror("pipe(out) failed");
  }
  if (pipe(err) == -1) {
    perror("pipe(err) failed");
  }

  pid_t child = fork();
  if (child == -1) {
    perror("fork failed");
  }

  if (child == 0) {
    close(in[1]);
    close(out[0]);
    close(err[0]);
    if (dup2(in[0], STDIN_FILENO) == -1) {
      perror("dup failed");
    }
    if (dup2(out[1], STDOUT_FILENO) == -1) {
      perror("dup failed");
    }
    if (dup2(err[1], STDERR_FILENO) == -1) {
      perror("dup failed");
    }

    close(in[0]);
    close(out[1]);
    close(err[1]);

    char *argv[] = {
                    "/usr/bin/ssh",
                    "-o",
                    "StrictHostKeyChecking=no",
                    hostname,
                    dmtcp1Path,
                    NULL};
    execv(argv[0], argv);
    perror("execv failed");
  } else {
    close(in[0]);
    close(out[1]);
    close(err[1]);
    char buf[4096];
    while (1) {
      ssize_t rt = read(out[0], buf, 4096);
      ssize_t wrt;
      if (rt > 0) {
        wrt = write(STDOUT_FILENO, buf, rt);
        if (wrt == -1 && errno != EINTR) {
          perror("write failed.");
          exit(0);
        }
      }
    }
  }
  wait(NULL);
  return 0;
}