1

私はこれを最小限のテストケースに落とし込みました。これまでのところ、これは ssh のパイプで発生する疑似端末に関連する問題であると判断できました。'-t -t' を ssh 呼び出しに追加すると、問題が発生するために fgets() への 2 回目の呼び出しが必要になるという点で改善されました。sshコマンドのstderr出力が何らかの形で問題に関与していると思われます。今のところ、実行するsshコードでstderrをstdoutにリダイレクトしました。「tcgetattr: Invalid argument」エラーが問題の一部であるかどうかは疑問ですが、それを取り除く方法がわかりません。-t -t が存在することから来ているようです。-t -t は正しい方向に進んでいると思いますが、何らかの方法で stderr の疑似端末をセットアップする必要があり、おそらくテストは適切に機能しますか?

メイクファイル:

test:
    gcc -g -DBUILD_MACHINE='"$(shell hostname)"' -c -o test.o test.c
    gcc -g -o test test.o

.PHONY: clean
clean:
    rm -rf test.o test

test.c ソース ファイル:

#include <unistd.h>
#include <string.h>
#include <stdio.h>

int
main(int argc, char *argv[])
{
  const unsigned int bufSize = 32;
  char buf1[bufSize];
  char buf2[bufSize];
  int ssh = argv[1][0] == 'y';
  const char *cmd = ssh ? "ssh -t -t " BUILD_MACHINE " \"ls\" 2>&1" : "ls";

  FILE *fPtr = popen(cmd, "r");

  if (fPtr == NULL) {
    fprintf(stderr,"Unable to spawn command.\n");
        perror("popen(3)");
        exit(1);
  }
  printf("Command: %s\n", cmd);
  if (feof(fPtr) == 0 && fgets(buf2, bufSize, fPtr) != NULL) {
    printf("First result: %s\n", buf2);
    if (feof(fPtr) == 0 && fgets(buf2, bufSize, fPtr) != NULL) {
      printf("Second result: %s\n", buf2);
      int nRead = read(fileno(stdin), buf1, bufSize);

      if (nRead == 0) {
        printf("???? popen() of ssh consumed the beginning of stdin ????\n");
      } else if (nRead > 0) {
        if (strncmp("The quick brown fox jumped", buf1, 26) != 0) {
          printf("??? Failed ???\n");
        } else {
          printf("!!!!!!!   Without ssh popen() did not consume stdin   !!!!!!!\n");
        }
      }
    }
  }
}

これは、通過する方法を実行していることを示しています。

> echo "The quick brown fox jumped" | ./test n
Command: ls
First result: ARCH.linux_26_i86

Second result: Makefile

!!!!!!!   Without ssh popen() did not consume stdin   !!!!!!!

これは、失敗した方法で実行されていることを示しています。

> echo "The quick brown fox jumped" | ./test y
Command: ssh -t -t hostname "ls" 2>&1
First result: tcgetattr: Invalid argument

Second result: %backup%~              gmon.out

???? popen() of ssh consumed the beginning of stdin ????
4

1 に答える 1

1

さて、私はついにこれを機能させました。秘密は、上記のテスト ケースの次のように、ssh コマンドへの入力として /dev/null を指定することでした。

      const char *cmd
        = ssh ? "ssh -t -t " BUILD_MACHINE " \"ls\" 2>&1 < /dev/null" : "ls";

ただし、コードは正しく機能しますが、厄介なメッセージが表示されますが、目的のために無視できるようです (ただし、メッセージを消したいと思います)。

tcgetattr: Inappropriate ioctl for device
于 2009-09-19T17:35:23.047 に答える