1. 程式人生 > >記一次傳遞檔案控制代碼引發的血案

記一次傳遞檔案控制代碼引發的血案

apue 上講 Solaris 系統是可以在程序間通過 STREAMS 管道傳遞檔案控制代碼的。

 

 

書上講道:“在技術上,傳送程序實際上向接收程序傳送一個指向一開啟檔案表項的指標,該指標被分配存放在接收程序的第一個可用描述符項中。”

 

個人非常感興趣,就寫下了下面的兩個程式來驗證 STREAMS 管道是否支援傳送接收檔案描述符,且傳送方與接收方的描述符是否可能不相同。

spipe_server.c

  1 #define MAXLINE 128
  2 
  3 int get_temp_fd ()
  4 {
  5     char fname[128] = "/tmp/outXXXXXX"; 
  6     int fd = mkstemp (fname); 
  7     printf ("create temp file %s with fd %d\n", fname, fd); 
  8     return fd; 
  9 }
 10 
 11 int main (int argc, char *argv[])
 12 {
 13     if (argc < 2) { 
 14         printf ("usage: spipe_server <spipe_client>\n"); 
 15         return 0; 
 16     }
 17 
 18     int n;
 19     int fd[2], fd_to_send, fd_to_recv; 
 20     if (pipe (fd) < 0) {
 21         printf ("pipe error\n"); 
 22         return 0; 
 23     }
 24     printf ("create pipe %d.%d\n", fd[0], fd[1]); 
 25 
 26     char line[MAXLINE]; 
 27     pid_t pid = fork (); 
 28     if (pid < 0) {
 29         printf ("fork error\n"); 
 30         return 0; 
 31     }
 32     else if (pid > 0)
 33     {
 34         close (fd[1]); 
 35         while (fgets (line, MAXLINE, stdin) != NULL) { 
 36             n = strlen (line); 
 37             // create temp file and write requet into it !
 38             fd_to_send = get_temp_fd (); 
 39             if (fd_to_send < 0) {
 40                 printf ("get temp fd failed\n"); 
 41                 return 0; 
 42             }
 43             
 44             if (write (fd_to_send, line, n) != n){
 45                 printf ("write error to file\n"); 
 46                 return 0; 
 47             }
 48 
 49             if (ioctl (fd[0], I_SENDFD, fd_to_send) < 0) {
 50                 printf ("send fd to peer failed, error %d\n", errno); 
 51                 return -1; 
 52             }
 53             else 
 54                 printf ("send fd %d to peer\n", fd_to_send); 
 55 
 56             // after send, fd_to_send is close automatically 
 57             struct strrecvfd recvfd;
 58             if (ioctl (fd[0], I_RECVFD, &recvfd) < 0) {
 59                 printf ("recv fd from peer failed, error %d\n", errno); 
 60                 return -1; 
 61             }
 62             else 
 63             {
 64                 fd_to_recv = recvfd.fd; 
 65                 printf ("recv fd %d from peer\n", fd_to_recv);   
 66             }
 67 
 68             // read response by receving the new fd!
 69             if ((n = read (fd_to_recv, line, MAXLINE)) < 0) {
 70                 printf ("read error from file\n"); 
 71                 return 0; 
 72             }
 73 
 74             close (fd_to_recv); 
 75             if (n == 0) { 
 76                 printf ("child closed pipe\n"); 
 77                 break;
 78             }
 79 
 80             line[n] = 0; 
 81             if (fputs (line, stdout) == EOF) {
 82                 printf ("fputs error\n"); 
 83                 return 0; 
 84             }
 85         }
 86 
 87         if (ferror (stdin)) {
 88             printf ("fputs error\n"); 
 89             return 0; 
 90         }
 91 
 92         return 0; 
 93     }
 94     else { 
 95         close (fd[0]); 
 96         if (fd[1] != STDIN_FILENO) { 
 97             if (dup2 (fd[1], STDIN_FILENO) != STDIN_FILENO) {
 98                 printf ("dup2 error to stdin\n"); 
 99                 return 0; 
100             }
101             //close (fd[0]); 
102         }
103 
104         if (fd[1] != STDOUT_FILENO) { 
105             if (dup2 (fd[1], STDOUT_FILENO) != STDOUT_FILENO) {
106                 printf ("dup2 error to stdout\n"); 
107                 return 0; 
108             }
109             close (fd[1]); 
110         }
111 
112         if (execl (argv[1], argv[1], (char *)0) < 0) {
113             printf ("execl error\n"); 
114             return 0; 
115         }
116     }
117 
118     return 0; 
119 }

server端開啟一個 STREAMS 管道(通過pipe),此管道將作為傳遞檔案描述符的通道。

它關閉管道的另一端,然後在fork出的子程序中將另一端重定向到子程序的標準輸入、輸出。

之後不斷從console讀入使用者輸入的兩個整數,建立一個臨時檔案(get_temp_fd)並將使用者輸入寫入檔案,

之後通過管道將此臨時檔案傳遞給子程序,然後在管道上等待子程序返回的另一個臨時檔案控制代碼,

該控制代碼中包含了兩數相加的結果,將其讀出並展示給console使用者。

 

spipe_client.c

 1 #define MAXLINE 128
 2 
 3 int get_temp_fd ()
 4 {
 5     char fname[128] = "/tmp/inXXXXXX";
 6     int fd = mkstemp (fname);
 7     fprintf (stderr, "create temp file %s with fd %d\n", fname, fd);
 8     return fd;
 9 }
10 
11 int main (void)
12 {
13     int ret, fdin, fdout, n, int1, int2; 
14     char line[MAXLINE]; 
15 
16     struct strrecvfd recvfd;
17     if (ioctl (STDIN_FILENO, I_RECVFD, &recvfd) < 0) {
18         fprintf (stderr, "recv fd from peer failed, error %d\n", errno); 
19         return -1; 
20     }
21 
22     fdin = recvfd.fd; 
23     fprintf (stderr, "recv fd %d, position %u\n", fdin, tell(fdin)); 
24     fdout = get_temp_fd (); 
25     if (fdout < 0) { 
26         fprintf (stderr, "get temp fd failed\n"); 
27         return -1; 
28     }
29 
30     n = read (fdin, line, MAXLINE);
31     if (n > 0) {
32         line[n] = 0; 
33         fprintf (stderr, "source: %s\n", line); 
34         if (sscanf (line, "%d%d", &int1, &int2) == 2) { 
35             sprintf (line, "%d\n", int1 + int2); 
36             n = strlen (line); 
37             if (write (fdout, line, n) != n) {
38                 fprintf (stderr, "write error\n"); 
39                 return 0; 
40             }
41         }
42         else { 
43             if (write (fdout, "invalid args\n", 13) != 13) {
44                 fprintf (stderr, "write msg error\n"); 
45                 return 0; 
46             }
47         }
48 
49         if (lseek (fdout, 0, SEEK_SET) < 0)
50             fprintf (stderr, "seek to begin failed\n"); 
51         else 
52             fprintf (stderr, "seek to head\n"); 
53 
54         if (ioctl (STDOUT_FILENO, I_SENDFD, fdout) < 0) {
55             fprintf (stderr, "send fd to peer failed, error %d\n", errno); 
56             return -1; 
57         }
58 
59         // fdout will be automatically closed by send_fd
60         fprintf (stderr, "send fd %d\n", fdout); 
61     }
62 
63     close (fdin); 
64     return 0; 

 

client 作為子程序因為已經被父程序重定向了標準輸入、標準輸出,就簡單多了,

從標準輸入接收一個檔案描述符作為輸入,讀取內容並解析後計算相加結果,

再取另一個臨時檔案(get_temp_fd)用來儲存結果,並將該檔案描述符回傳給父程序。

 

簡單的修改了下 Makefile 檔案、編譯、執行,結果卻不是很理想:

-bash-3.2$ ./spipe_server ./spipe_client
create pipe 3.4
2 5
create temp file /tmp/outo3a4Il with fd 4
send fd 4 to peer
recv fd 3
create temp file /tmp/ino3aaJl with fd 4
recv fd from peer failed, error 2

 

可以看到 server 到 client 的檔案控制代碼傳遞成功了,在 server 端控制代碼號為 4,傳遞到 client 端後變為 3.

但是在 server 端等待接收檔案控制代碼時卻發生了錯誤,這是怎麼回事?

查了一下錯誤碼 2,為ENOENT,沒有對應的檔案或目錄。

這就奇怪了,讀取管道返回這個錯誤的唯一原因只能是管道被關閉,難道子程序已經不在了麼?

為此,在 client 最後新增一句日誌輸出:

    if (n > 0)
         ....
    else
        fprintf (stderr, "no more data\n"); 

再執行 demo,果然發現多了一句:

no more data

 

看來確實是因為子程序退出導致管道關閉了。

那為什麼子程序什麼資料也沒有從臨時檔案控制代碼中讀到呢?

一開始懷疑是資料寫入後,沒有 flush 到磁碟,從而導致另一端沒有讀到,於是在寫入資料之後、傳送控制代碼之前,加了以下程式碼:

            if (fsync (fd_to_send) < 0)
                printf ("sync file failed\n"); 
            else 
                printf ("sync data to file\n");

再執行 demo,果然發現多了一句:

sync data to file

 

資料同步成功了。但是結果還是一樣,沒有改善。

走到這邊真的是有點想不通了,琢磨了一宿,晚上突然想到會不會是檔案偏移沒有歸位導致的。

第二天回來,立馬在接收端列印了一下檔案偏移 (offset):

fprintf (stderr, "recv fd %d, position %u\n", fdin, tell(fdin)); 

 

 再執行 demo,輸出的偏移果然有問題!

recv fd 3, position 4

 

這下原因清楚了,原來是接收程序與傳送程序共享了檔案控制代碼的偏移,導致再讀取的過程中直接讀到了檔案尾。

修改程式碼,在傳送檔案控制代碼之前重置檔案偏移:

            if (lseek (fd_to_send, 0, SEEK_SET) < 0)
                printf ("seek to begin failed\n");  
            else
                printf ("seek to head\n");

同理,在 client 端做相同的修改。編譯、執行,這下好了:

-bash-3.2$ ./spipe_server ./spipe_client
create pipe 3.4
2 8
create temp file /tmp/outGGaiLl with fd 4
seek to head
send fd 4 to peer
recv fd 3, position 0
create temp file /tmp/inHGaqLl with fd 4
source: 2 8

seek to head
send fd 4
recv fd 5 from peer, position 0
10

 

可以正確的得到計算結果。

 

從寫這個小 demo 的過程中,我理解到書本知識到可執行的程式碼之間,還是有很多細節需要處理的,

有時看書就感覺自己會了,但到了實踐就可能會遇到這樣那樣的問題(這些問題甚至和你要測試的東西無關),

動手解決問題的過程其實也加深了對書本知識的瞭解,正所謂:”紙上得來終覺淺,絕知此事要躬行“,

以此小文與各位共