【APUE】Chapter17 Advanced IPC sign extension 结构体内存对齐
17.1 Introduction
這一章主要講了UNIX Domain Sockets這樣的進(jìn)程間通訊方式,并列舉了具體的幾個例子。
?
17.2 UNIX Domain Sockets
這是一種特殊socket類型,主要用于高效的IPC,特點主要在于高效(因為省去了很多與數(shù)據(jù)無關(guān)的格式的要求)。
int socketpair(int?domain, int?type, int?protocol, int?sockfd[2]) 這個函數(shù)用于構(gòu)建一對unix domain sockets;并且與之前的pipe函數(shù)不同,這里構(gòu)建fd都是full-duplex的。
下面列舉一個poll + message queue + 多線程 的例子。
為什么要舉上面的例子?因為沒辦法直接用poll去管理多個message queue。
message queue在unix系統(tǒng)中有兩種標(biāo)示方法:1. 全局用一個key 2. 進(jìn)程內(nèi)部用一個identifier
而poll關(guān)注的對象只能是file descriptor;所以,用unix domain sockets作為二者的橋梁。
例子包含兩個部分,reciver端和sender端。
reciver掛起來幾個message queue,每個queue單獨開一個線程去處理;主線程跟每個queue線程的關(guān)聯(lián)方式就是unix domain sockets。代碼如下:
1 #include "../apue.3e/include/apue.h" 2 #include <sys/poll.h> 3 #include <pthread.h> 4 #include <sys/msg.h> 5 #include <sys/socket.h> 6 7 #define NQ 3 8 #define MAXMSZ 512 9 #define KEY 0x123 10 11 struct threadinfo{ 12 int qid; 13 int fd; 14 }; 15 16 struct mymesg{ 17 long mtype; 18 char mtext[MAXMSZ]; 19 }; 20 21 void * helper(void *arg) 22 { 23 int n; 24 struct mymesg m; 25 struct threadinfo *tip = arg; 26 27 for(; ;) 28 { 29 memset(&m, 0, sizeof(m)); 30 if ((n = msgrcv(tip->qid, &m, MAXMSZ, 0, MSG_NOERROR))<0) { 31 err_sys("msgrcv error"); 32 } 33 /*來自一個消息隊列的內(nèi)容 就特定的file desrciptor中*/ 34 if (write(tip->fd, m.mtext, n)<0) { 35 err_sys("write error"); 36 } 37 } 38 } 39 40 int main(int argc, char *argv[]) 41 { 42 int i, n, err; 43 int fd[2]; 44 int qid[NQ]; /*message queue在process內(nèi)部的identifier*/ 45 struct pollfd pfd[NQ]; 46 struct threadinfo ti[NQ]; 47 pthread_t tid[NQ]; 48 char buf[MAXMSZ]; 49 50 /*給每個消息隊列設(shè)定處理線程*/ 51 for (i=0; i<NQ; i++) { 52 /*返回消息隊列的identifier 類似file descriptor*/ 53 if ((qid[i] = msgget((KEY+i), IPC_CREAT|0666))<0) { 54 err_sys("msgget error"); 55 } 56 printf("queue ID %d is %d\n", i, qid[i]); 57 /*構(gòu)建unix domain sockets*/ 58 if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fd)<0) { 59 err_sys("socketpair error"); 60 } 61 pfd[i].fd = fd[0]; /*main線程把住fd[0]這頭*/ 62 pfd[i].events = POLLIN; /*有data要去讀*/ 63 /* qid[i]在同一個process都可以用來表示同一個message queue */ 64 ti[i].qid = qid[i]; /*在每個線程中記錄要處理的消息隊列的id*/ 65 ti[i].fd = fd[1]; /*每個隊列的線程把住fd[1]這頭*/ 66 /*為每個消息隊列創(chuàng)建一個處理線程 并將對應(yīng)的threadinfo參數(shù)傳入線程*/ 67 if ((err = pthread_create(&tid[i], NULL, helper, &ti[i]))!=0) { 68 err_exit(err, "pthread_create error"); 69 } 70 } 71 72 for (;;) { 73 /*一直輪詢著 直到有隊列可以等待了 再執(zhí)行*/ 74 if (poll(pfd, NQ, -1)<0) { 75 err_sys("poll error"); 76 } 77 /*由于能進(jìn)行到這里 則一定是有隊列ready了 找到所有ready的隊列*/ 78 for (i=0; i<NQ; i++) { 79 if (pfd[i].revents & POLLIN) { /*挑出來所有滿足POLLIN條件的*/ 80 if ((n=read(pfd[i].fd, buf, sizeof(buf)))<0) { 81 err_sys("read error"); 82 } 83 buf[n] = 0; /* 這個末尾賦'\0'是必要的 因為接下來要執(zhí)行printf*/ 84 printf("queue id %d, message %s\n",qid[i],buf); 85 } 86 } 87 } 88 exit(0); 89 }sender端,用command-line argument的方式讀入message的外部key,以及寫入message queue的數(shù)據(jù),具體代碼如下:
#include "../apue.3e/include/apue.h" #include <sys/msg.h>#define MAXMSZ 512struct mymesg{long mtype;char mtext[MAXMSZ]; };int main(int argc, char *argv[]) {key_t key;long qid;size_t nbytes;struct mymesg m;if (argc != 3) {fprintf(stderr, "usage: sendmsg KEY message\n");exit(1);}key = strtol(argv[1], NULL, 0);if ((qid = msgget(key,0))<0) {err_sys("can't open queue key %s", argv[1]);}memset(&m, 0, sizeof(m));strncpy(m.mtext, argv[2], MAXMSZ-1);nbytes = strlen(m.mtext);m.mtype = 1;if (msgsnd(qid, &m, nbytes, 0)<0) {err_sys("can't send message");}exit(0); }執(zhí)行結(jié)果如下:
分析:
(1)unix socket domain在上述代碼中的好處主要是方便了多個message queue的管理
(2)引入unix socket domain雖然帶來了方便,但也在reciver中引入了兩次額外的cost:一個是line34的write,向unix domain socket多寫了一次;一個是line80的read,從unix domain socket多讀了一次。如果這種cost在可接受范圍內(nèi),那么unix socket domain就可以應(yīng)用。
?
17.2.1 Naming UNIX Domain Sockets
上面介紹的這種socketpair的方式構(gòu)造unix domain sockets,輸出是幾個fd,因此只能用于有親屬關(guān)系的process中。
如果要unrelated process之間用unix domain sockets通信,得從外面process能找到這個unix domain socket。
struct sockaddr_un{
sa_family_t sun_family; ?/*AF_UNIX*/
char sun_path[108]; ?/*pathname*/
}
這個結(jié)構(gòu)體可以用來被構(gòu)造成一個“可以被外面process找到的”的unix domain socket的地址,類似于“ip+port”的作用。
具體需要如下三個步驟的操作:
(1)fd = socket(AF_UNIX, SOCK_STREAM, 0) // 產(chǎn)生unix domain socket
(2)un.sun_family = AF_UNIX?strcpy(un.sun_path, pathname)
(3)bind(fd, (struct sockaddr *)&un, size) ?// 將unix domain socket與fd綁定
另,這里的pathname需要是一個獨一無二的文件名。后面的一系列內(nèi)容,都把sockaddr_un按照ip+port進(jìn)行理解就順暢了。
有了結(jié)構(gòu)體中sun_path這個文件名,這個unix domain socket就有了自己獨一無二的標(biāo)識,其他進(jìn)程就可以通過這個標(biāo)識找到它。
1 #include "../apue.3e/include/apue.h" 2 #include <sys/socket.h> 3 #include <sys/un.h> 4 #include <string.h> 5 6 int main(int argc, char *argv[]) 7 { 8 int fd, size; 9 struct sockaddr_un un; 10 11 un.sun_family = AF_UNIX; 12 memset(un.sun_path, 0, sizeof(un.sun_path)); 13 strcpy(un.sun_path, "foo.socket"); 14 15 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0))<0) { 16 err_sys("socket fail"); 17 } 18 size = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path); 19 if (bind(fd, (struct sockaddr *)&un, size)<0) { 20 err_sys("bind failed"); 21 } 22 printf("UNIX domain socket bound\n"); 23 exit(0); 24 }這里“foo.socket"不需要事先真的存在,它只需要是一個獨特的名稱就可以了。
執(zhí)行結(jié)果如下:
程序執(zhí)行的當(dāng)前文件夾下是沒有foo.socket這個文件的
執(zhí)行如上程序:
可以看到執(zhí)行完程序后:
(1)foo.socket這個文件自動生成了,而且文件類型是socket(srwxrwxr-x中的s)
(2)如果foo.socket已經(jīng)被占用了是沒辦法再綁定其他的unix domain socket的
?
17.3 Unique Connections
基于17.2.1的naming unix domain socket技術(shù),就可以針對unix domain socket展開listen, accept, connect等一些列用于network socket的操作;用這樣的方式來實現(xiàn)同一個host內(nèi)部的IPC。
具體的示意圖,如下所示:
apue中分別給出了listen accept connect三個函數(shù)的unix domain socket版。
int serv_listen(const char *name);
int serv_accpet(int listenfd, uid_t *uidptr);
int cli_conn(const char *name);
具體實現(xiàn)如下:
serv_listen函數(shù)(返回一個unix domain socket專門用于監(jiān)聽client發(fā)送來的請求)
1 #include "../apue.3e/include/apue.h" 2 #include <sys/socket.h> 3 #include <sys/un.h> 4 #include <errno.h> 5 6 #define QLEN 10 7 8 /*只要傳入一個well known name 就可返回fd*/ 9 int serv_listen(const char *name) 10 { 11 int fd; 12 int len; 13 int err; 14 int rval; 15 struct sockaddr_un un; 16 17 /*對name的長度上限有要求*/ 18 if (strlen(name) >= sizeof(un.sun_path)) { 19 errno = ENAMETOOLONG; 20 return -1; 21 } 22 /*這里創(chuàng)建的方式是SOCK_STREAM*/ 23 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0))<0) { 24 return -2; 25 } 26 /*防止name已經(jīng)被占用了 這是一種排他的做法*/ 27 unlink(name); 28 /*初始化socket address structure*/ 29 memset(&un, 0, sizeof(un.sun_path)); 30 un.sun_family = AF_UNIX; 31 strcpy(un.sun_path, name); 32 len = offsetof(struct sockaddr_un, sun_path) + strlen(name); 33 /*執(zhí)行bind操作 因為有name所以可以綁定*/ 34 if (bind(fd, (struct sockaddr *)&un, len)<0) { 35 rval = -3; 36 goto errout; 37 } 38 /*執(zhí)行l(wèi)isten的操作 并設(shè)置等待隊列的長度*/ 39 if (listen(fd, QLEN)<0) { 40 rval = -4; 41 goto errout; 42 } 43 return fd; 44 errout: 45 err = errno; 46 close(fd); 47 errno = err; 48 return rval; 49 }serv_accpet函數(shù)(這里有一點沒看懂 為什么client's name有30s的限制)
1 #include "../apue.3e/include/apue.h" 2 #include <sys/socket.h> 3 #include <sys/un.h> 4 #include <time.h> 5 #include <errno.h> 6 7 #define STALE 30 /*client's name can't be older than this sec*/ 8 9 int serv_accept(int listenfd, uid_t *uidptr) 10 { 11 int clifd; 12 int err; 13 int rval; 14 socklen_t len; 15 time_t staletime; 16 struct sockaddr_un un; 17 struct stat statbuf; 18 char *name; /*name中存放的是發(fā)起請求的client的地址信息*/ 19 20 /*因為sizeof不計算結(jié)尾的\0 所以在計算分配內(nèi)存的時候要考慮進(jìn)來*/ 21 if ((name = malloc(sizeof(un.sun_path+1)))==NULL) { 22 return -1; 23 } 24 len = sizeof(un); 25 /*就在這里阻塞著 等著client端發(fā)送來請求*/ 26 if ((clifd = accept(listenfd, (struct sockaddr *)&un, &len))<0) { 27 free(name); 28 return -2; 29 } 30 /*再讓len為path的實際長度 并存到name中*/ 31 len -= offsetof(struct sockaddr_un, sun_path); 32 memcpy(name, un.sun_path, len); 33 name[len] = 0; /*最后補(bǔ)上\0*/ 34 if (stat(name, &statbuf)<0) { /*讓statbuf獲得client關(guān)聯(lián)的文件的status*/ 35 rval = -3; 36 goto errout; 37 } 38 39 /*1. 驗證與client端關(guān)聯(lián)的文件類型是不是socket file*/ 40 #ifdef S_ISSOCK 41 if (S_ISSOCK(statbuf.st_mode)==0) { 42 rval = -4; 43 goto errout; 44 } 45 #endif 46 /*2. 驗證與clinet端關(guān)聯(lián)的文件的權(quán)限*/ 47 /*G for group O for owner U for user */ 48 /*驗證permission只有user-read user-write user-execute*/ 49 /*注意 ||運算符的優(yōu)先級 要高于 !=運算符的優(yōu)先級*/ 50 if ((statbuf.st_mode & (S_IRWXG | S_IRWXO)) || 51 (statbuf.st_mode & S_IRWXU) != S_IRWXU) { 52 rval = -5; 53 goto errout; 54 } 55 /*3. 驗證與client端關(guān)聯(lián)的文件被創(chuàng)建的時間*/ 56 staletime = time(NULL) - STALE; /**/ 57 if (statbuf.st_atim < staletime || 58 statbuf.st_ctim < staletime || 59 statbuf.st_mtim < staletime) { 60 rval = -6; 61 goto errout; 62 } 63 if (uidptr != NULL) { 64 *uidptr = statbuf.st_uid; 65 } 66 unlink(name); 67 free(name); 68 return clifd; 69 70 errout: 71 err = errno; 72 close(clifd); 73 free(name); 74 errno = err; 75 return rval; 76 }cli_conn
1 #include "../apue.3e/include/apue.h" 2 #include <sys/socket.h> 3 #include <sys/un.h> 4 #include <errno.h> 5 6 #define CLI_PATH "/var/tmp" /*客戶端標(biāo)示*/ 7 #define CLI_PERM S_IRWXU /*權(quán)限設(shè)置*/ 8 9 int cli_conn(const char *name) 10 { 11 int fd; 12 int len; 13 int err; 14 int rval; 15 struct sockaddr_un un, sun;// un代表client端 sun代表server端 16 int do_unlink = 0; 17 /*1. 驗證傳入的name是否合理 18 * 這個name是server的name 先校驗server name的長度 */ 19 if (strlen(name) >= sizeof(un.sun_path)) { 20 errno = ENAMETOOLONG; 21 return -1; 22 } 23 /*2. 構(gòu)建client端的fd 24 * 這個fd是client的專門發(fā)送請求的fd*/ 25 if ((fd = socket(AF_UNIX, SOCK_STREAM, 0))<0) { 26 return -1; 27 } 28 /*3. 構(gòu)建client端的地址*/ 29 /* 將文件名+進(jìn)程號共寫進(jìn)un.sun_path 并記錄長度 這里約定了path的格式*/ 30 memset(&un, 0, sizeof(un)); 31 un.sun_family = AF_UNIX; 32 sprintf(un.sun_path, "%s%05ld", CLI_PATH, (long)getpid()); 33 printf("file is %s\n", un.sun_path); 34 len = offsetof(struct sockaddr_un, sun_path) + strlen(un.sun_path); 35 /*4. 將構(gòu)建的fd與構(gòu)建的client端地址綁定*/ 36 unlink(un.sun_path); /*防止CLI_PATH+pid這個特殊的文件名已經(jīng)被占用了*/ 37 if (bind(fd, (struct sockaddr *)&un, len)<0) { 38 rval = -2; 39 goto errout; 40 } 41 /* 為什么要先綁定再設(shè)定權(quán)限?因為如果不能綁定 修改權(quán)限就是無用功*/ 42 if (chmod(un.sun_path, CLI_PERM)<0) { 43 rval = -3; 44 do_unlink = 1; 45 goto errout; 46 } 47 /*5. 告訴client通過name去找server*/ 48 /* 通過這個name這個key與'server'的process建立連接*/ 49 memset(&sun, 0 ,sizeof(sun)); 50 sun.sun_family = AF_UNIX; 51 strcpy(sun.sun_path, name); 52 len = offsetof(struct sockaddr_un, sun_path) + strlen(name); 53 if (connect(fd, (struct sockaddr *)&sun, len)<0) { 54 rval = -4; 55 do_unlink = 1; 56 goto errout; 57 } 58 return fd; 59 errout: 60 err = errno; 61 close(fd); 62 if (do_unlink) { 63 unlink(un.sun_path); 64 } 65 errno = err; 66 return raval; 67 }?
?
17.4 Passing File Descriptors
在進(jìn)程間傳遞file descriptor是也是unix domain socket的一種強(qiáng)大的功能。文件打開的各種細(xì)節(jié),都隱藏在server端了。
至今在apue上已經(jīng)有三種進(jìn)程間的file descriptor的傳遞方式:
(1)figure3.8的情況,不同的process分別打開同一個file,每個process中的fd有各自的file table,這兩個fd基本沒有什么關(guān)系:
(2)figure8.2的情況,parent通過fork產(chǎn)生child,整個parent的memory layout都copy到child中,這兩個fd屬于不同的地址空間,但是值是相同的,并且共享同一個file table:
(3)17.4節(jié)的情況,通過unix domain socket的方式傳遞fd,這兩個fd屬于不同的地址空間,除了共享同一個file table沒有其他的不同:
這一部分還講了其他一些相關(guān)的結(jié)構(gòu)體內(nèi)容,這些細(xì)節(jié)為了看懂代碼而用,關(guān)鍵記住上面的三種fd方式就可以了。
apue這部分自己設(shè)定了一個protocol,設(shè)定通過unix domain socket傳遞fd的協(xié)議,這個協(xié)議的細(xì)節(jié)不用關(guān)注太多;重點看如何告訴系統(tǒng),發(fā)送的是一個fd。
利用unix domain socket發(fā)送和接收fd的代碼如下:
send_fd的代碼(如何告訴系統(tǒng)發(fā)送的是一個fd?先把struct cmsghdr cmptr設(shè)定好line43~45,將cmptr賦值給struct msghdr msg中的msg.msg_control,這樣系統(tǒng)就知道發(fā)送的是一個fd)
1 #include "../apue.3e/include/apue.h" 2 #include <bits/socket.h> 3 #include <sys/socket.h> 4 5 /* 由于不同系統(tǒng)對于cmsghdr的實現(xiàn)不同 CMSG_LEN這個宏就是計算cmsghdr+int 6 * 所需要的memory大小是多少 這樣動態(tài)分配內(nèi)存的時候才知道分配多少大小*/ 7 #define CONTROLLEN CMSG_LEN(sizeof(int)) 8 9 static struct cmsghdr *cmptr = NULL; 10 11 int send_fd(int fd, int fd_to_send) 12 { 13 struct iovec iov[1]; 14 struct msghdr msg; 15 char buf[2]; /*這是真正的協(xié)議頭的兩個特征bytes*/ 16 /*scatter read or gather write 具體參考14.6 17 * 具體到這里的情景比較簡單 因為iovec的長度只有1 相當(dāng)于就調(diào)用了一個write 18 * 但是Unix domain socket的格式要去必須是struct iovec這種數(shù)據(jù)格式*/ 19 iov[0].iov_base = buf; 20 iov[0].iov_len = 2; 21 msg.msg_iov = iov; 22 msg.msg_iovlen = 1; 23 msg.msg_name = NULL; 24 msg.msg_namelen = 0; 25 /*調(diào)用send_fd分兩種情況: 26 * 1. 正常調(diào)用傳遞fd, 則fd_to_send是大于零的 27 * 2. 在send_err中調(diào)用send_fd, 則fd_to_send表示的是errorcode*/ 28 if (fd_to_send<0) { 29 msg.msg_control = NULL; 30 msg.msg_controllen = 0; 31 buf[1] = -fd_to_send; /*出錯的fd_to_send都是負(fù)數(shù)*/ 32 if (buf[1] == 0) { /*這個protocol并不是完美的 如果fd_to_send 33 是-256 則沒有正數(shù)與其對應(yīng) 協(xié)議在這里特殊處理-1與-256都代表 errorcode 1*/ 34 buf[1] = 1; 35 } 36 } 37 else { 38 /*這里cmptr獲得的memory大小是由CMSG_LEN算出來的*/ 39 if (cmptr == NULL && (cmptr = malloc(CONTROLLEN)) == NULL ) { 40 return -1; 41 } 42 /*通過Unix domain socket發(fā)送fd 就如下設(shè)置*/ 43 cmptr->cmsg_level = SOL_SOCKET; 44 cmptr->cmsg_type = SCM_RIGHTS; 45 cmptr->cmsg_len = CONTROLLEN; 46 /*將cmptr融進(jìn)要發(fā)送的msg*/ 47 msg.msg_control = cmptr; 48 msg.msg_controllen = CONTROLLEN; 49 /*得搞清楚strut cmsghdr的結(jié)構(gòu) 50 * struct cmsghdr{ 51 * socklen_t cmsg_len; 52 * int cmsg_level; 53 * int cmsg_type; 54 * } 55 * // followed by the actual control message data 56 * CMSG_DATA做的事情就是在cmsghdr緊屁股后面放上'fd_to_send'這個內(nèi)容 57 * ubuntu系統(tǒng)上查看<sys/socket.h>文件中的這個宏的具體實現(xiàn) 58 * 這個宏的具體實現(xiàn)就是struct cmsghdr結(jié)構(gòu)體的指針+1, 然后將這個位置*/ 59 *(int *)CMSG_DATA(cmptr) = fd_to_send; 60 buf[1] = 0; 61 } 62 buf[0] = 0; /*這就是給recv_fd設(shè)定的null byte flag recv_fd()函數(shù)中就是靠這個位來判斷的*/ 63 /*這里校驗的sendmsg返回值是不是2 就是char buf[2]中的內(nèi)容 64 * struct msghdr msg中 只有msg_iov中的數(shù)據(jù)算是被校驗的內(nèi)容 65 * 而msg_control這樣的數(shù)據(jù) 都叫ancillary data 即輔助數(shù)據(jù) 66 * 輔助數(shù)據(jù)雖然也跟著發(fā)送出去了 但是不在sendmsg返回值的校驗標(biāo)準(zhǔn)中*/ 67 if (sendmsg(fd, &msg, 0)!=2) { 68 return -1; 69 } 70 return 0 71 }接收端的代碼recv_fd如下(代碼不難理解,有個坑是line56是apue勘誤表中才修改過來,否則有問題;勘誤表的鏈接:http://www.apuebook.com/errata3e.html)
1 #include "open_fd.h" 2 #include <sys/socket.h> /* struct msghdr */ 3 4 /* size of control buffer to send/recv one file descriptor */ 5 #define CONTROLLEN CMSG_LEN(sizeof(int)) 6 7 static struct cmsghdr *cmptr = NULL; /* malloc'ed first time */ 8 9 /* 10 * Receive a file descriptor from a server process. Also, any data 11 * received is passed to (*userfunc)(STDERR_FILENO, buf, nbytes). 12 * We have a 2-byte protocol for receiving the fd from send_fd(). 13 */ 14 int 15 recv_fd(int fd, ssize_t (*userfunc)(int, const void *, size_t)) 16 { 17 int newfd, nr, status; 18 char *ptr; 19 char buf[MAXLINE]; 20 struct iovec iov[1]; 21 struct msghdr msg; 22 23 status = -1; 24 for ( ; ; ) { 25 iov[0].iov_base = buf; 26 iov[0].iov_len = sizeof(buf); 27 msg.msg_iov = iov; 28 msg.msg_iovlen = 1; 29 msg.msg_name = NULL; 30 msg.msg_namelen = 0; 31 if (cmptr == NULL && (cmptr = malloc(CONTROLLEN)) == NULL) 32 return(-1); 33 msg.msg_control = cmptr; 34 msg.msg_controllen = CONTROLLEN; 35 if ((nr = recvmsg(fd, &msg, 0)) < 0) { 36 err_ret("recvmsg error"); 37 return(-1); 38 } else if (nr == 0) { 39 err_ret("connection closed by server"); 40 return(-1); 41 } 42 43 /* 44 * See if this is the final data with null & status. Null 45 * is next to last byte of buffer; status byte is last byte. 46 * Zero status means there is a file descriptor to receive. 47 */ 48 for (ptr = buf; ptr < &buf[nr]; ) { 49 if (*ptr++ == 0) { 50 if (ptr != &buf[nr-1]) 51 err_dump("message format error"); 52 status = *ptr & 0xFF; /* prevent sign extension */ 53 if (status == 0) { 54 printf("msg.msg_controllen:%zu\n", msg.msg_controllen); 55 printf("CONTROLLEN:%zu\n", CONTROLLEN); 56 if (msg.msg_controllen < CONTROLLEN) 57 err_dump("status = 0 but no fd"); 58 newfd = *(int *)CMSG_DATA(cmptr); 59 } else { 60 newfd = -status; 61 } 62 nr -= 2; 63 } 64 } 65 if (nr > 0 && (*userfunc)(STDERR_FILENO, buf, nr) != nr) 66 return(-1); 67 if (status >= 0) /* final data has arrived */ 68 return(newfd); /* descriptor, or -status */ 69 } 70 }?
?
17.5 An Open Server, Version 1
這一節(jié)正是利用17.4中的passing file descriptor的技術(shù)來構(gòu)建一個"open" server:
這個server專門用來接收client發(fā)送的請求(即打開哪個文件,怎么打開),然后在server端把文件打開,再利用unix domain socket的技術(shù)把file descriptor給傳遞過去。
具體用到的技術(shù)就是client運行起來,通過fork+execl的方式調(diào)用opend(相當(dāng)于server端的程序),并且通過socketpair的方式建立進(jìn)程間的通信。
將書上的代碼整理了一下(main.c表示client端,maind.c表示server端,lib文件夾中包含用到的一些函數(shù),include文件夾中的.h文件包括各種公用的lib)
main.c代碼如下:
1 #include "open_fd.h" 2 #include <fcntl.h> 3 #include <sys/uio.h> 4 5 #define BUFFSIZE 8192 6 #define CL_OPEN "open" // client's request for server 7 8 int csopen(char *name, int oflag) 9 { 10 pid_t pid; 11 int len; 12 char buf[10]; 13 struct iovec iov[3]; 14 static int fd[2] = {-1, -1}; 15 /*首次需要建立child parent的鏈接*/ 16 if (fd[0] < 0) { 17 printf("frist time build up fd_pipe\n"); 18 /*構(gòu)建一個全雙工的pipe*/ 19 if (fd_pipe(fd) < 0) { 20 err_ret("fd_pipe error"); 21 return -1; 22 } 23 printf("fd[0]:%d,fd[1]:%d\n",fd[0],fd[1]); 24 if((pid = fork())<0){ 25 err_ret("fork error"); 26 return -1; 27 } 28 else if (pid ==0) { /*child*/ 29 close(fd[0]); 30 /*這個地方需要注意 這種full-duplex的fd 可以把in和out都掛到這個fd上面 之前只掛了stdin沒有掛out所以有問題*/ 31 /*將child的stdin 銜接到fd[1]上面*/ 32 if (fd[1] != STDIN_FILENO && dup2(fd[1],STDIN_FILENO)!=STDIN_FILENO) { 33 err_sys("dup2 error to stdin"); 34 } 35 /*將child的stdout 銜接到fd[1]上面*/ 36 if (fd[1] != STDOUT_FILENO && dup2(fd[1],STDOUT_FILENO)!=STDOUT_FILENO) { 37 err_sys("dup2 error to stdout"); 38 } 39 /*執(zhí)行opend這個程序 這時opend這個程序的stdin就指向fd[1] child和parent通過pipe連接了起來*/ 40 if (execl("./opend", "opend", (char *)0)<0) { 41 err_sys("execl error"); 42 } 43 } 44 close(fd[1]); /*parent*/ 45 } 46 47 /*iov三個char array合成一個char array 每個array以空格分開*/ 48 sprintf(buf, " %d", oflag); 49 iov[0].iov_base = CL_OPEN " "; /* string concatenation */ 50 iov[0].iov_len = strlen(CL_OPEN) + 1; 51 iov[1].iov_base = name; /*傳入的filename在中間的io*/ 52 iov[1].iov_len = strlen(name); 53 iov[2].iov_base = buf; 54 iov[2].iov_len = strlen(buf) + 1; /* +1 for null at end of buf */ 55 len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len; 56 /*通過fd[0] fd[1]這個通道 由client向server發(fā)送數(shù)據(jù)*/ 57 /*writev在會把緩沖區(qū)的輸出數(shù)據(jù)按順序集合到一起 再發(fā)送出去*/ 58 if (writev(fd[0], &iov[0], 3) != len) { 59 err_ret("writev error"); 60 return(-1); 61 } 62 /* read descriptor, returned errors handled by write() */ 63 return recv_fd(fd[0], write); 64 } 65 66 /*這是client端調(diào)用的程序*/ 67 int main(int argc, char *argv[]) 68 { 69 int n, fd; 70 char buf[BUFFSIZE], line[MAXLINE]; 71 /*每次從stdin cat進(jìn)來filename*/ 72 while (fgets(line, MAXLINE, stdin)!=NULL) { 73 /*替換把回車替換掉*/ 74 if (line[strlen(line)-1] == '\n') { 75 line[strlen(line)-1] = 0; 76 } 77 /*打開文件*/ 78 if ((fd = csopen(line, O_RDONLY))<0) { 79 continue; 80 } 81 /*把fd這個文件讀寫完成*/ 82 printf("fd obtained from other process : %d\n",fd); 83 while ((n = read(fd, buf, BUFFSIZE))>0) { 84 if (write(STDOUT_FILENO, buf, n)!= n) { 85 err_sys("write error"); 86 } 87 } 88 if (n<0) { 89 err_sys("read error"); 90 } 91 close(fd); 92 } 93 exit(0); 94 }maind.c的代碼如下:
1 #include <errno.h> 2 #include <fcntl.h> 3 #include "open_fd.h" 4 5 #define CL_OPEN "open" 6 #define MAXARGC 50 7 #define WHITE " \t\n" 8 9 char errmsg[MAXLINE]; 10 int oflag; 11 char *pathname; 12 13 /* cli_args和buf_args兩個函數(shù)起到把讀進(jìn)來的buf解析的功能 14 * 了解大體功能即可 不用細(xì)看*/ 15 16 int cli_args(int argc, char **argv) 17 { 18 if (argc != 3 || strcmp(argv[0], CL_OPEN) != 0) { 19 strcpy(errmsg, "usage: <pathname> <oflag>\n"); 20 return(-1); 21 } 22 pathname = argv[1]; /* save ptr to pathname to open */ 23 oflag = atoi(argv[2]); 24 return(0); 25 } 26 27 int buf_args(char *buf, int (*optfunc)(int, char **)) 28 { 29 char *ptr, *argv[MAXARGC]; 30 int argc; 31 32 if (strtok(buf, WHITE) == NULL) /* an argv[0] is required */ 33 return(-1); 34 argv[argc = 0] = buf; 35 while ((ptr = strtok(NULL, WHITE)) != NULL) { 36 if (++argc >= MAXARGC-1) /* -1 for room for NULL at end */ 37 return(-1); 38 argv[argc] = ptr; 39 } 40 argv[++argc] = NULL; 41 42 /* 43 * Since argv[] pointers point into the user's buf[], 44 * user's function can just copy the pointers, even 45 * though argv[] array will disappear on return. 46 */ 47 return((*optfunc)(argc, argv)); 48 } 49 50 void handle_request(char *buf, int nread, int fd) 51 { 52 int newfd; 53 if (buf[nread-1] != 0) { 54 send_err(fd, -1, errmsg); 55 return; 56 } 57 if (buf_args(buf, cli_args) < 0) { /* parse args & set options */ 58 send_err(fd, -1, errmsg); 59 return; 60 } 61 if ((newfd = open(pathname, oflag)) < 0) { 62 send_err(fd, -1, errmsg); 63 return; 64 } 65 if (send_fd(fd, newfd) < 0) /* send the descriptor */ 66 err_sys("send_fd error"); 67 close(newfd); /* we're done with descriptor */ 68 } 69 70 /*server端*/ 71 int main(void) 72 { 73 int nread; 74 char buf[MAXLINE]; 75 for (; ; ){ 76 /*一直阻塞著 等著stdin讀數(shù)據(jù)*/ 77 if ((nread = read(STDIN_FILENO, buf, MAXLINE))<0) { 78 err_sys("read error on stream pipe"); 79 } 80 else if (nread == 0) { 81 break; 82 } 83 handle_request(buf, nread, STDOUT_FILENO); 84 } 85 exit(0); 86 }其余l(xiāng)ib和include中的代碼有的是apue書上這個章節(jié)的,有的是apue源代碼提供的lib,這些不再贅述了。
直接看運行結(jié)果(在當(dāng)前文件夾下面設(shè)定了一個xbf的文本文件,流程是讓client發(fā)送以只讀方式打開這個文件的請求,由server打開這個文件,然后再將fd返回)
先得注意msg.msg_controllen與CONTROLLEN是不等的,這是原書勘誤表中的一個bug。
server中打開的xbf文件的fd就是存在了msg這個結(jié)構(gòu)體的最后的位置發(fā)送過來的。
如果將main.c中的line91注釋掉,結(jié)果如下:
可以看到,真正client接收到的fd的值,與server端發(fā)送時候的fd的值是沒有關(guān)系的,只是client端哪個最小的fd的值可用,就會用這個fd的值對應(yīng)上server打開的xbf這個文件。
總結(jié)一下,流程是這樣的:
(1)server打開xbf文件 →
(2)server將與xbf文件對應(yīng)的fd掛到cmsghdr的最后 →
(3)server通過fd_pipe產(chǎn)生的unix domain socket將msghdr發(fā)送到client端 →
(4)在發(fā)送的過程中kernel記錄的應(yīng)該是這個fd對應(yīng)的file table信息 →
(5)在client接收到這個file table時候,kernel分配一個client端可用的最小fd →
(6)client端獲得了一個fd并且這個fd已經(jīng)指向開打的xbf文件
其余的具體protocol不用細(xì)看,但是一些技術(shù)細(xì)節(jié)后面再單獨記錄。
?
17.6 An Open Server Version 2
這里主要用到的是naming unix domain socket的技術(shù),為的是可以在unrelated process之間傳遞file descriptor。
理解這個部分的重點是書上17.29和17.30兩個loop函數(shù)的實現(xiàn):一個用的是select函數(shù),一個用的是poll函數(shù)。(還需要熟悉守護(hù)進(jìn)程的知識以及command-line argument的解析的套路)
要想迅速串起來這部分的代碼,還得回顧一下select和poll函數(shù),這二者的輸入?yún)?shù)中都有value-on return類型的,先理解好輸入?yún)?shù)。
loop.select.c代碼如下:
1 #include "opend.h" 2 #include <sys/select.h> 3 4 void 5 loop(void) 6 { 7 int i, n, maxfd, maxi, listenfd, clifd, nread; 8 char buf[MAXLINE]; 9 uid_t uid; 10 fd_set rset, allset; 11 12 /* 與poll的用法不同 這里喂給select的fd_set是不預(yù)先設(shè)定大小的 13 * 而是靠maxfd來標(biāo)定大小*/ 14 FD_ZERO(&allset); 15 /* obtain fd to listen for client requests on */ 16 if ((listenfd = serv_listen(CS_OPEN)) < 0) 17 log_sys("serv_listen error"); 18 /* 將server這個用于監(jiān)聽的fd加入集合*/ 19 FD_SET(listenfd, &allset); 20 /* 需要監(jiān)聽的最大的fd就是剛剛分配的listenfd*/ 21 maxfd = listenfd; 22 maxi = -1; 23 24 for ( ; ; ) { 25 rset = allset; /* rset gets modified each time around */ 26 /* select中的&rset這個參數(shù) 返回的時候只保留ready的fd*/ 27 if ((n = select(maxfd + 1, &rset, NULL, NULL, NULL)) < 0) 28 log_sys("select error"); 29 /* 處理有client發(fā)送請求的case*/ 30 if (FD_ISSET(listenfd, &rset)) { 31 /* accept new client request */ 32 if ((clifd = serv_accept(listenfd, &uid)) < 0) 33 log_sys("serv_accept error: %d", clifd); 34 i = client_add(clifd, uid); 35 FD_SET(clifd, &allset); /*A 向allset中增加需要監(jiān)聽的內(nèi)容*/ 36 if (clifd > maxfd) /* 更新select監(jiān)控的最大的fd大小*/ 37 maxfd = clifd; /* max fd for select() */ 38 if (i > maxi) /* 更新Client array的大小*/ 39 maxi = i; /* max index in client[] array */ 40 log_msg("new connection: uid %d, fd %d", uid, clifd); 41 continue; 42 } 43 /* 沒有新的client 處理Client array中ready的client */ 44 for (i = 0; i <= maxi; i++) { /* go through client[] array */ 45 if ((clifd = client[i].fd) < 0) /*沒被占用的*/ 46 continue; 47 if (FD_ISSET(clifd, &rset)) { /*在監(jiān)聽的set中*/ 48 /* read argument buffer from client */ 49 if ((nread = read(clifd, buf, MAXLINE)) < 0) { 50 log_sys("read error on fd %d", clifd); 51 } else if (nread == 0) { /* nread=0表明client已經(jīng)關(guān)閉了*/ 52 log_msg("closed: uid %d, fd %d", 53 client[i].uid, clifd); 54 client_del(clifd); /* client has closed cxn */ 55 FD_CLR(clifd, &allset); /* B 從allset中刪除需要監(jiān)聽的內(nèi)容*/ 56 close(clifd); 57 } else { /* process client's request */ 58 handle_request(buf, nread, clifd, client[i].uid); 59 } 60 } 61 } 62 } 63 }loop.pool.c的代碼如下:
#include "opend.h" #include <poll.h>#define NALLOC 10 /* # pollfd structs to alloc/realloc */static struct pollfd * grow_pollfd(struct pollfd *pfd, int *maxfd) {int i;int oldmax = *maxfd;int newmax = oldmax + NALLOC;if ((pfd = realloc(pfd, newmax * sizeof(struct pollfd))) == NULL)err_sys("realloc error");for (i = oldmax; i < newmax; i++) {pfd[i].fd = -1;pfd[i].events = POLLIN;pfd[i].revents = 0;}*maxfd = newmax;return(pfd); }void loop(void) {int i, listenfd, clifd, nread;char buf[MAXLINE];uid_t uid;struct pollfd *pollfd;int numfd = 1;int maxfd = NALLOC;/* 先分配10個fd槽 */if ((pollfd = malloc(NALLOC * sizeof(struct pollfd))) == NULL)err_sys("malloc error");for (i = 0; i < NALLOC; i++) {pollfd[i].fd = -1;pollfd[i].events = POLLIN; /*read*/pollfd[i].revents = 0;}/* obtain fd to listen for client requests on */if ((listenfd = serv_listen(CS_OPEN)) < 0)log_sys("serv_listen error");client_add(listenfd, 0); /* we use [0] for listenfd */pollfd[0].fd = listenfd;for ( ; ; ) {/* 這里控制的是numfd而不是maxfd*/if (poll(pollfd, numfd, -1) < 0)log_sys("poll error");/* 1. 先判斷是否有新的client請求 */if (pollfd[0].revents & POLLIN) {/* accept new client request */if ((clifd = serv_accept(listenfd, &uid)) < 0)log_sys("serv_accept error: %d", clifd);client_add(clifd, uid);/* possibly increase the size of the pollfd array *//* 如果Client array數(shù)量超過了pollfd的數(shù)量 就realloc*/if (numfd == maxfd)pollfd = grow_pollfd(pollfd, &maxfd);pollfd[numfd].fd = clifd;pollfd[numfd].events = POLLIN;pollfd[numfd].revents = 0;numfd++;log_msg("new connection: uid %d, fd %d", uid, clifd);/* 與select不同 這里沒有continue 而是可以直接向下進(jìn)行* 為什么可以直接向下進(jìn)行 而select就不可以* 因為poll使用pollfd來標(biāo)定需要等著的fd的* 每個struct pollfd中* a. 既有關(guān)心ready的事件* b. 又有真正ready的事件* 處理一個fd并不會影響其他fd的狀態(tài)*/}/* 2. 再判斷有哪些ready的client*/for (i = 1; i < numfd; i++) {if (pollfd[i].revents & POLLHUP) {goto hungup;} else if (pollfd[i].revents & POLLIN) {/* read argument buffer from client */if ((nread = read(pollfd[i].fd, buf, MAXLINE)) < 0) {log_sys("read error on fd %d", pollfd[i].fd);} else if (nread == 0) { hungup:/* the client closed the connection */log_msg("closed: uid %d, fd %d",client[i].uid, pollfd[i].fd);client_del(pollfd[i].fd);close(pollfd[i].fd);if (i < (numfd-1)) { /* 這個應(yīng)該是corner case的判斷*//* 這么做是為了節(jié)約空間* 把末端的fd及相關(guān)信息頂?shù)絠這個位置上 *//* pack the array */pollfd[i].fd = pollfd[numfd-1].fd;pollfd[i].events = pollfd[numfd-1].events;pollfd[i].revents = pollfd[numfd-1].revents;/* 由于把末位的頂?shù)絠這個位置上* 所以要再check一遍這個位置 */i--; /* recheck this entry */}numfd--;} else { /* process client's request */handle_request(buf, nread, pollfd[i].fd,client[i].uid);}}}} }?
===================================分割線===================================
記錄幾個遇到的技術(shù)細(xì)節(jié)問題
1. sign extension的問題
上面recv_fd中的line54有一個不是很直觀的做法
int status;
char *ptr;
status = *ptr & 0xFF;
ptr是char類型,可以代表0~255的值,代表不同的返回狀態(tài)。比如*ptr為128的值用二進(jìn)制表示為1000000。
由于status是int類型占4bytes 32bits,如果直接status = *ptr,就涉及到位擴(kuò)展的問題,最高位到底是當(dāng)成符號位還是取值位呢?
(1)首先,char到底是有符號還是無符號的,取決于編譯器,見這篇文章(http://descent-incoming.blogspot.jp/2013/02/c-char-signed-unsigned.html)
(2)0xFF默認(rèn)是無符號int型,高位8都為0
因此,無論char是不是有符號的,一旦與0xFF做了與運算,則相當(dāng)于把char類型的最高位自動當(dāng)成了取值位了。就避免了上面提到的符號位擴(kuò)展的問題。
為了方便記憶,寫了一個小例子記錄這種sign extension帶來的影響:
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(int argc, char *argv[]) 5 { 6 /*驗證int的byte數(shù)目*/ 7 int status = -1; 8 char c1 = 254; /*默認(rèn)254是int類型占4bytes 轉(zhuǎn)換成char類型占1bytes 直接截取低8位*/ 9 unsigned char c2 = 254; 10 /*gcc編譯器 默認(rèn)的char是有符號的 因為直接從char轉(zhuǎn)換到int是用char的符號位補(bǔ)齊高位*/ 11 status = c1; 12 printf("status converted from c1 : %d\n", status); 13 /*如果是unsigned char是沒有符號位的 因此從unsigned char轉(zhuǎn)換到int是高位直接補(bǔ)0*/ 14 status = c2; 15 printf("status converted from c2 : %d\n", status); 16 /*驗證默認(rèn)的0xFF是4 bytes 32 bits的*/ 17 printf("size of defalut int : %ld\n", sizeof(0xFF)); 18 status = c1 & 0xFF; 19 printf("status converted from c1 & 0xFF : %d\n", status); 20 /*如果是1 byte 8 bits的int類型*/ 21 int8_t i8 = 0xFF; 22 status = c1 & i8; 23 printf("status converted from c1 & int8_t i8 : %d\n", status); 24 }執(zhí)行結(jié)果如下:
上面的例子應(yīng)該可以包含絕大多數(shù)情況了。
這是當(dāng)時看過的一個不錯的資料:http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/signExt.html
?
2. sizeof與strelen的問題:
http://www.cnblogs.com/carekee/articles/1630789.html
?
3. 結(jié)構(gòu)體內(nèi)存對齊問題:
send_fd和recv_fd代碼中都用到了一個宏定義CMSG_LEN:查看這個宏在socket.h中的定義,引申出CMSG_ALIGN這個內(nèi)存對齊的宏定義。
(1)要想回顧C(jī)MSG_ALIGN怎么做到內(nèi)存對齊的,可以參考下面的blog:http://blog.csdn.net/duanlove/article/details/9948947
(2)要想理解為什么要進(jìn)行內(nèi)存對齊,可以參考下面的blog:http://www.cppblog.com/snailcong/archive/2009/03/16/76705.html
(3)從實操層面,學(xué)習(xí)如何計算結(jié)構(gòu)體的內(nèi)存對齊方法,可以參考下面的blog:http://blog.csdn.net/hairetz/article/details/4084088
把上面的內(nèi)容總結(jié)起來,可得結(jié)構(gòu)體內(nèi)存對齊如下的結(jié)論:
1 A元素是結(jié)構(gòu)體前面的元素 B元素是結(jié)構(gòu)體后面的元素,一般結(jié)構(gòu)體開始的偏移量是0,則:A元素必須讓B元素滿足 B元素的尋址偏移量是B元素size的整數(shù)倍大小 2 整個結(jié)構(gòu)的大小必須是其中最大字段大小的整數(shù)倍。 按照上面兩個原則 就大概能算出來常規(guī)套路下結(jié)構(gòu)體需要內(nèi)存對齊后的大小 最后還是自己寫一個例子,通過實操記憶一下: 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 struct str1{ 5 char a; 6 char b; 7 short c; 8 long d; 9 }; 10 11 struct str2{ 12 char a; 13 }; 14 15 int main(int argc, char *argv[]) 16 { 17 struct str2 s2; 18 struct str1 s1; 19 char *p; 20 char c; 21 short s; 22 long l; 23 24 printf("size of str2 : %ld\n", sizeof(struct str2)); 25 printf("addr of str2.a : %p\n", &s2.a); 26 printf("size of str1 : %ld\n", sizeof(struct str1)); 27 printf("addr of str1.a : %p\n", &s1.a); 28 printf("addr of str1.b : %p\n", &s1.b); 29 printf("addr of str1.c : %p\n", &s1.c); 30 printf("addr of str1.d : %p\n", &s1.d); 31 printf("addr of char pointer p : %p\n", &p); 32 printf("addr of char c : %p\n", &c); 33 printf("addr of long l : %p\n", &l); 34 printf("addr of short s : %p\n", &s); 35 }運行結(jié)果如下:
分析:
(1)結(jié)構(gòu)體內(nèi)存對齊按照上面說的規(guī)律
(2)其余的變量內(nèi)存分配,并不是完全按照變量定義的順序,我的理解是按照變量的所占字節(jié)的大小,字節(jié)大的分配在高地址(stack地址分配由高向低生長),這樣有助于節(jié)約內(nèi)存空間,降低內(nèi)存對齊帶來的memory的浪費。
另,深入看了一下malloc函數(shù),果然malloc也是考慮了內(nèi)存對齊的問題的。
(1)man malloc可以看到如下的信息:
(2)這個blog專門講malloc考慮內(nèi)存對齊的內(nèi)存分配機(jī)制的:http://blog.csdn.net/elpmis/article/details/4500917
?
4. 對于char c = 0 和 char c = '\0'問題的解釋
二者本質(zhì)是一樣的,只是表述上有所區(qū)別,ascii碼'\0'的值就是0.
http://stackoverflow.com/questions/16955936/string-termination-char-c-0-vs-char-c-0
?
===================================分割線===================================
?
?
APUE這本書刷到這里也差不多了,后面兩章內(nèi)容不是很新暫時不刷了。
這本書看過一遍,感覺還是遠(yuǎn)遠(yuǎn)不夠,以后應(yīng)該常放在手邊翻閱。
轉(zhuǎn)載于:https://www.cnblogs.com/xbf9xbf/p/5121748.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的【APUE】Chapter17 Advanced IPC sign extension 结构体内存对齐的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一起来学SpringBoot | 第四篇
- 下一篇: 基于 HanLP 的 ES 中文分词插件