zoukankan      html  css  js  c++  java
  • 记一次传递文件句柄引发的血案

    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
    ", 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>
    "); 
     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
    "); 
     22         return 0; 
     23     }
     24     printf ("create pipe %d.%d
    ", fd[0], fd[1]); 
     25 
     26     char line[MAXLINE]; 
     27     pid_t pid = fork (); 
     28     if (pid < 0) {
     29         printf ("fork error
    "); 
     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
    "); 
     41                 return 0; 
     42             }
     43             
     44             if (write (fd_to_send, line, n) != n){
     45                 printf ("write error to file
    "); 
     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
    ", errno); 
     51                 return -1; 
     52             }
     53             else 
     54                 printf ("send fd %d to peer
    ", 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
    ", errno); 
     60                 return -1; 
     61             }
     62             else 
     63             {
     64                 fd_to_recv = recvfd.fd; 
     65                 printf ("recv fd %d from peer
    ", 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
    "); 
     71                 return 0; 
     72             }
     73 
     74             close (fd_to_recv); 
     75             if (n == 0) { 
     76                 printf ("child closed pipe
    "); 
     77                 break;
     78             }
     79 
     80             line[n] = 0; 
     81             if (fputs (line, stdout) == EOF) {
     82                 printf ("fputs error
    "); 
     83                 return 0; 
     84             }
     85         }
     86 
     87         if (ferror (stdin)) {
     88             printf ("fputs error
    "); 
     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
    "); 
     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
    "); 
    107                 return 0; 
    108             }
    109             close (fd[1]); 
    110         }
    111 
    112         if (execl (argv[1], argv[1], (char *)0) < 0) {
    113             printf ("execl error
    "); 
    114             return 0; 
    115         }
    116     }
    117 
    118     return 0; 
    119 }

    server端打开一个 STREAMS 管道(通过pipe),此管道将作为传递文件描述符的通道。

    它关闭管道的另一端,然后在fork出的子进程中将另一端重定向到子进程的标准输入、输出。

    之后不断从console读入用户输入的两个整数,创建一个临时文件(get_temp_fd)并将用户输入写入文件,

    之后通过管道将此临时文件传递给子进程,然后在管道上等待子进程返回的另一个临时文件句柄,

    该句柄中包含了两数相加的结果,将其读出并展示给console用户。

    关于Solaris上pipe的特别之处,请参考我之前写过的一篇介绍文章:神奇的 Solaris pipe

    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
    ", 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
    ", errno); 
    19         return -1; 
    20     }
    21 
    22     fdin = recvfd.fd; 
    23     fprintf (stderr, "recv fd %d, position %u
    ", fdin, tell(fdin)); 
    24     fdout = get_temp_fd (); 
    25     if (fdout < 0) { 
    26         fprintf (stderr, "get temp fd failed
    "); 
    27         return -1; 
    28     }
    29 
    30     n = read (fdin, line, MAXLINE);
    31     if (n > 0) {
    32         line[n] = 0; 
    33         fprintf (stderr, "source: %s
    ", line); 
    34         if (sscanf (line, "%d%d", &int1, &int2) == 2) { 
    35             sprintf (line, "%d
    ", int1 + int2); 
    36             n = strlen (line); 
    37             if (write (fdout, line, n) != n) {
    38                 fprintf (stderr, "write error
    "); 
    39                 return 0; 
    40             }
    41         }
    42         else { 
    43             if (write (fdout, "invalid args
    ", 13) != 13) {
    44                 fprintf (stderr, "write msg error
    "); 
    45                 return 0; 
    46             }
    47         }
    48 
    49         if (lseek (fdout, 0, SEEK_SET) < 0)
    50             fprintf (stderr, "seek to begin failed
    "); 
    51         else 
    52             fprintf (stderr, "seek to head
    "); 
    53 
    54         if (ioctl (STDOUT_FILENO, I_SENDFD, fdout) < 0) {
    55             fprintf (stderr, "send fd to peer failed, error %d
    ", errno); 
    56             return -1; 
    57         }
    58 
    59         // fdout will be automatically closed by send_fd
    60         fprintf (stderr, "send fd %d
    ", 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
    "); 

    再运行 demo,果然发现多了一句:

    no more data

    看来确实是因为子进程退出导致管道关闭了。

    那为什么子进程什么数据也没有从临时文件句柄中读到呢?

    一开始怀疑是数据写入后,没有 flush 到磁盘,从而导致另一端没有读到,于是在写入数据之后、发送句柄之前,加了以下代码:

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

    再运行 demo,果然发现多了一句:

    sync data to file

    数据同步成功了。但是结果还是一样,没有改善。

    走到这边真的是有点想不通了,琢磨了一宿,晚上突然想到会不会是文件偏移没有归位导致的。

    第二天回来,立马在接收端打印了一下文件偏移 (offset):

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

     再运行 demo,输出的偏移果然有问题!

    recv fd 3, position 4

    这下原因清楚了,原来是接收进程与发送进程共享了文件句柄的偏移,导致再读取的过程中直接读到了文件尾。

    修改代码,在发送文件句柄之前重置文件偏移:

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

    同理,在 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 的过程中,我理解到书本知识到可运行的代码之间,还是有很多细节需要处理的,

    有时看书就感觉自己会了,但到了实践就可能会遇到这样那样的问题(这些问题甚至和你要测试的东西无关),

    动手解决问题的过程其实也加深了对书本知识的了解,正所谓:”纸上得来终觉浅,绝知此事要躬行“,

    以此小文与各位共勉!

    后记:在 linux 上,虽然没有 STREAMS 系统可用,依然可以借助其它方式来传递文件句柄,

    这就是 Unix 域套接字、和基于其上的 sendmsg/recvmsg、 来收发 SCM_RIGHTS/SCM_CREDENTIALS 类型的控制消息,

    不仅可以发送文件句柄,还可以提供发送进程的 uid 等凭证,用于权限校验。

  • 相关阅读:
    java数据结构之LinkedHashMap
    java数据结构之HashSet和TreeSet以及LinkedHashSet
    Java数据结构之TreeMap
    java数据结构之HashMap
    java数据结构之CopyOnWriteArrayList和CopyOnWriteArraySet
    java数据结构之LinkedList
    全站从http升级到https(WordPress博客)
    Fiddler抓取https请求 & Fiddler抓包工具常用功能详解
    linux下python2升级python3,python2和python3并存
    为什么ios手机安装好fiddler证书/charles证书还是抓不到https请求?
  • 原文地址:https://www.cnblogs.com/goodcitizen/p/murder_caused_by_passing_file_handle.html
Copyright © 2011-2022 走看看