記一次傳遞文件句柄引發的血案

来源:https://www.cnblogs.com/goodcitizen/archive/2019/12/26/12103449.html
-Advertisement-
Play Games

apue 上講 Solaris 系統是可以在進程間通過 STREAMS 管道傳遞文件句柄的。 書上講道:“在技術上,發送進程實際上向接收進程傳送一個指向一打開文件表項的指針,該指針被分配存放在接收進程的第一個可用描述符項中。” 個人非常感興趣,就寫下了下麵的兩個程式來驗證 STREAMS 管道是否支 ...


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 的過程中,我理解到書本知識到可運行的代碼之間,還是有很多細節需要處理的,

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

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

以此小文與各位共勉!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 一、Dll的優點: 1、擴展應用程式的特性 2、簡化項目管理 3、有助於節省記憶體 4、促進資源的共用 5、促進本地化 6、有助於解決平臺間的差異 7、可用於特殊目的 有關於dll及註入相關理論資料,可參考《Windows核心編程5》第四部分(19-22章)。 二、做dll註入時遇到的坑 環境:VS2 ...
  • 場景 怎樣在Winform程式中添加滑鼠右鍵時使子選項顯示圖片。 註: 博客主頁: https://blog.csdn.net/badao_liumang_qizhi 關註公眾號 霸道的程式猿 獲取編程相關電子書、教程推送與免費下載。 實現 新建Winform程式,在Properties下的Reso ...
  • 代碼實現 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>L ...
  • 剛開始遇到這個問題時有點懵,UDP為什麼還有分啟動順序?看一下我遇到的錯誤: 先啟動serevr,再啟動client,雙方不管誰先發消息,正常; 先啟動server,再啟動client,之後關閉client,不關閉server,重新再啟動client,client先發消息異常, 先啟動client, ...
  • 場景 在使用ZedGraph生成多條曲線時為了能區分曲線顏色,要求隨機設置曲線顏色。 首先從System.Drawing.Color中獲取所有顏色的對象的數組,然後將其順序打亂隨機排序,然後在生成曲線時從Color數組中取Color並賦值。 效果 註: 博客主頁: https://blog.csdn ...
  • 場景 在ZedGraph隨機生成顏色時需要從顏色數組中取顏色對象。 Color數組存取的是System.Drawing.Color的顏色。 其順序是相鄰的顏色,顏色差距不大,在取顏色時按順序取顏色時,如果顏色條數比較少,差距會不明顯。 需要將此數組的順序打亂,隨機進行排序。 註: 博客主頁: htt ...
  • 場景 需要在生成一組多條曲線時,隨機從一顏色數組中取顏色,至少一百種顏色以上。 而System.Drawing.Color自帶140多種顏色 那麼怎樣將其自帶的顏色對象取出並存在數組中。 註: 博客主頁: https://blog.csdn.net/badao_liumang_qizhi 關註公眾號 ...
  • 在Windows10系統上搭建完深度學習環境用於無人駕駛中的目標檢測後,想在Linux系統上再嘗試一下。由於VMware虛擬機安裝的Linux系統不支持物理硬體,所以需要一步到位安裝一個雙系統。本文介紹如何安裝雙系統以及裝完系統後的輸入法和英偉達顯卡驅動配置。 1.安裝雙系統 詳細操作參考https ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...