2009-09-02 86 views
1

我現在已經將這個減少到最小的測試用例。到目前爲止,我已經能夠確定這是與ssh管道產生的僞終端有關的問題。將'-t -t'添加到ssh調用中改善了一些事情,因此現在需要第二次調用fgets()才能解決問題。我懷疑ssh命令的stderr輸出在某種程度上可以解決問題,因爲現在我已經將stderr重定向到stdout中執行的ssh代碼中。我不知道「tcgetattr:Invalid argument」錯誤是否是問題的一部分,但我不知道如何擺脫它。它似乎來自-t -t在場。我相信-t -t正在朝着正確的方向前進,但我必須以某種方式爲stderr設置僞終端,也許測試會正常工作?在'ssh'的popen()上調用fgets()會刷新調用進程的stdin的開始(ptty問題)

的Makefile文件:

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 ???? 

回答

1

好的,我終於得到了這個工作。祕訣是要提供的/ dev/null作爲輸入到我的ssh命令從上面的測試用例如下:

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

然而,當代碼工作正常,我得到一個討厭的消息,這顯然我可以忽略我的目的(雖然我想使消息消失):

tcgetattr: Inappropriate ioctl for device 
+0

嘗試將輸出TO管道輸出到/ dev/null而不是FROM它。 – GaZ 2012-12-04 14:50:44