⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 recvfd.c

📁 UNIX环境高级编程第二版(英文版)配书的源代码
💻 C
字号:
#include "apue.h"#include <stropts.h>/* * Receive a file descriptor from another process (a server). * In addition, any data received from the server is passed * to (*userfunc)(STDERR_FILENO, buf, nbytes).  We have a * 2-byte protocol for receiving the fd from send_fd(). */intrecv_fd(int fd, ssize_t (*userfunc)(int, const void *, size_t)){	int					newfd, nread, flag, status;	char				*ptr;	char				buf[MAXLINE];	struct strbuf		dat;	struct strrecvfd	recvfd;	status = -1;	for ( ; ; ) {		dat.buf = buf;		dat.maxlen = MAXLINE;		flag = 0;		if (getmsg(fd, NULL, &dat, &flag) < 0)			err_sys("getmsg error");		nread = dat.len;		if (nread == 0) {			err_ret("connection closed by server");			return(-1);		}		/*		 * See if this is the final data with null & status.		 * Null must be next to last byte of buffer, status		 * byte is last byte.  Zero status means there must		 * be a file descriptor to receive.		 */		for (ptr = buf; ptr < &buf[nread]; ) {			if (*ptr++ == 0) {				if (ptr != &buf[nread-1])					err_dump("message format error"); 				status = *ptr & 0xFF;	/* prevent sign extension */ 				if (status == 0) {					if (ioctl(fd, I_RECVFD, &recvfd) < 0)						return(-1);					newfd = recvfd.fd;	/* new descriptor */				} else {					newfd = -status;				}				nread -= 2;			}		}		if (nread > 0)			if ((*userfunc)(STDERR_FILENO, buf, nread) != nread)				return(-1);		if (status >= 0)	/* final data has arrived */			return(newfd);	/* descriptor, or -status */	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -