UNIX域协议之描述符传递
一、mycat程序
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h> #define BUFFSIZE 4096 int my_open(const char *, int);
void err_sys(const char *, ...);
void err_quit(const char *, ...); int main(int argc, char **argv)
{
int fd, n;
char buff[BUFFSIZE]; if (argc != ) {
err_quit("usage: mycat <pathname>");
} if ( (fd = my_open(argv[], O_RDONLY)) < ) {
err_sys("cannot open %s", argv[]);
} while ( (n = read(fd, buff, BUFFSIZE)) > ) {
write(STDOUT_FILENO, buff, n);
} exit();
}
#include <errno.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/socket.h> void err_sys(const char *, ...);
void err_quit(const char *, ...);
ssize_t Read_fd(int, void *, size_t, int *); int my_open(const char *pathname, int mode)
{
int fd, sockfd[], status;
pid_t childpid;
char c, argsockfd[], argmode[]; socketpair:
if (socketpair(AF_LOCAL, SOCK_STREAM, , sockfd) == -) {
if (errno == EINTR) {
goto socketpair;
} else {
err_quit("socketpair failed to create stream pipe");
}
} if ( (childpid = fork()) == ) { /* child process */
close(sockfd[]);
snprintf(argsockfd, sizeof(argsockfd), "%d", sockfd[]);
snprintf(argmode, sizeof(argmode), "%d", mode);
execl("../openfile/openfile", "openfile", argsockfd,
pathname, argmode,(char *) NULL);
err_sys("execl error");
} close(sockfd[]); /* close the end we don't use */ waitpid(childpid, &status, ); if (WIFEXITED(status) == ) {
err_quit("child did not terminate");
}
if ( (status = WEXITSTATUS(status)) == ) {
Read_fd(sockfd[], &c, , &fd);
} else {
errno = status; /* set errno value from child's status */
fd = -;
} close(sockfd[]);
return (fd);
}
#include <stddef.h>
#include <sys/socket.h> void err_sys(const char *, ...);
void err_quit(const char *, ...); ssize_t read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
{
struct msghdr msg;
struct iovec iov[];
ssize_t n; union {
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof(int))];
} control_un;
struct cmsghdr *cmptr; msg.msg_control = control_un.control;
msg.msg_controllen = sizeof(control_un.control); msg.msg_name = NULL;
msg.msg_namelen = ; iov[].iov_base = ptr;
iov[].iov_len = nbytes;
msg.msg_iov = iov;
msg.msg_iovlen = ; if ( (n = recvmsg(fd, &msg, )) <= ) {
return (n);
} if ( (cmptr = CMSG_FIRSTHDR(&msg)) != NULL &&
cmptr->cmsg_len == CMSG_LEN(sizeof(int))) {
if (cmptr->cmsg_level != SOL_SOCKET) {
err_quit("control level != SOL_SOCKET");
}
if (cmptr->cmsg_type != SCM_RIGHTS) {
err_quit("control type != SCM_RIGHTS");
}
*recvfd = *((int *) CMSG_DATA(cmptr));
} else {
*recvfd = -; /* descriptor was not passed */
} return(n);
} ssize_t Read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
{
ssize_t n; if ( (n = read_fd(fd, ptr, nbytes, recvfd)) < ) {
err_sys("read_fd error");
} return (n);
}
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h> /* ANSI C header file */
#include <syslog.h> /* for syslog() */ #define MAXLINE 4096 int daemon_proc; /* set nonzero by daemon_init() */ static void err_doit(int, int, const char *, va_list); /* Nonfatal error related to system call
* Print message and return */ void err_ret(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_INFO, fmt, ap);
va_end(ap);
return;
} /* Fatal error related to system call
* Print message and terminate */ void err_sys(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_ERR, fmt, ap);
va_end(ap);
exit();
} /* Fatal error related to system call
* Print message, dump core, and terminate */ void err_dump(const char *fmt, ...) {
va_list ap; va_start(ap, fmt);
err_doit(, LOG_ERR, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(); /* shouldn't get here */
} /* Nonfatal error unrelated to system call
* Print message and return */ void err_msg(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_INFO, fmt, ap);
va_end(ap);
return;
} /* Fatal error unrelated to system call
* Print message and terminate */ void err_quit(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_ERR, fmt, ap);
va_end(ap);
exit();
} /* Print message and return to caller
* Caller specifies "errnoflag" and "level" */ static void err_doit(int errnoflag, int level, const char *fmt, va_list ap) { int errno_save, n;
char buf[MAXLINE + ]; errno_save = errno; /* value caller might want printed */
#ifdef HAVE_VSNPRINTF
vsnprintf(buf, MAXLINE, fmt, ap); /* safe */
#else
vsprintf(buf, fmt, ap); /* not safe */
#endif
n = strlen(buf);
if (errnoflag)
snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
strcat(buf, "\n"); if (daemon_proc) {
syslog(level, buf);
} else {
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(stderr);
}
return;
}
二、openfile程序
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h> void err_quit(const char *, ...);
ssize_t Write_fd(int, void *, size_t, int); int main(int argc, char **argv)
{
int fd; if (argc != ) {
err_quit("openfile <sockfd> <filename> <mode>");
} if ( (fd = open(argv[], atoi(argv[]))) < ) {
exit( (errno > ) ? errno : );
} if (Write_fd(atoi(argv[]), "", , fd) < ) {
exit( (errno > ) ? errno : );
} exit();
}
#include <stddef.h>
#include <sys/socket.h> void err_sys(const char *, ...); ssize_t write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
{
struct msghdr msg;
struct iovec iov[]; union {
struct cmsghdr cm;
char control[CMSG_SPACE(sizeof(int))];
} control_un;
struct cmsghdr *cmptr; msg.msg_control = control_un.control;
msg.msg_controllen = sizeof(control_un.control); cmptr = CMSG_FIRSTHDR(&msg);
cmptr->cmsg_len = CMSG_LEN(sizeof(int));
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS;
*((int *) CMSG_DATA(cmptr)) = sendfd; msg.msg_name = NULL;
msg.msg_namelen = ; iov[].iov_base = ptr;
iov[].iov_len = nbytes;
msg.msg_iov = iov;
msg.msg_iovlen = ; return (sendmsg(fd, &msg, ));
} ssize_t Write_fd(int fd, void *ptr, size_t nbytes, int sendfd)
{
ssize_t n; if ( (n = write_fd(fd, ptr, nbytes, sendfd)) < ) {
err_sys("write_fd error");
} return (n);
}
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h> /* ANSI C header file */
#include <syslog.h> /* for syslog() */ #define MAXLINE 4096 int daemon_proc; /* set nonzero by daemon_init() */ static void err_doit(int, int, const char *, va_list); /* Nonfatal error related to system call
* Print message and return */ void err_ret(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_INFO, fmt, ap);
va_end(ap);
return;
} /* Fatal error related to system call
* Print message and terminate */ void err_sys(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_ERR, fmt, ap);
va_end(ap);
exit();
} /* Fatal error related to system call
* Print message, dump core, and terminate */ void err_dump(const char *fmt, ...) {
va_list ap; va_start(ap, fmt);
err_doit(, LOG_ERR, fmt, ap);
va_end(ap);
abort(); /* dump core and terminate */
exit(); /* shouldn't get here */
} /* Nonfatal error unrelated to system call
* Print message and return */ void err_msg(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_INFO, fmt, ap);
va_end(ap);
return;
} /* Fatal error unrelated to system call
* Print message and terminate */ void err_quit(const char *fmt, ...) { va_list ap; va_start(ap, fmt);
err_doit(, LOG_ERR, fmt, ap);
va_end(ap);
exit();
} /* Print message and return to caller
* Caller specifies "errnoflag" and "level" */ static void err_doit(int errnoflag, int level, const char *fmt, va_list ap) { int errno_save, n;
char buf[MAXLINE + ]; errno_save = errno; /* value caller might want printed */
#ifdef HAVE_VSNPRINTF
vsnprintf(buf, MAXLINE, fmt, ap); /* safe */
#else
vsprintf(buf, fmt, ap); /* not safe */
#endif
n = strlen(buf);
if (errnoflag)
snprintf(buf + n, MAXLINE - n, ": %s", strerror(errno_save));
strcat(buf, "\n"); if (daemon_proc) {
syslog(level, buf);
} else {
fflush(stdout); /* in case stdout and stderr are the same */
fputs(buf, stderr);
fflush(stderr);
}
return;
}
UNIX域协议之描述符传递的更多相关文章
- 《TCP-IP详解卷3:TCP 事务协议、HTTP、NNTP和UNIX域协议》【PDF】下载
TCP-IP详解卷3:TCP 事务协议.HTTP.NNTP和UNIX域协议>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062539 ...
- UNIX域协议(命名套接字)
这里主要介绍命名UNIX域套接字 1.什么是UNIX域套接字Unix域协议并不是一个实际的协议族,而是在单个主机上执行客户/服务通信的一种方式.是进程间通信(IPC)的一种方式.它提供了两类套接字:字 ...
- Socket编程实践(13) --UNIX域协议
UNIX域协议 UNIX域套接字与TCP相比, 在同一台主机上, UNIX域套接字更有效率, 几乎是TCP的两倍(由于UNIX域套接字不需要经过网络协议栈,不需要打包/拆包,计算校验和,维护序号和应答 ...
- UNP学习笔记(第十五章 UNIX域协议)
UNIX域协议是在单个主机上执行客户/服务器通信的一种方法 使用UNIX域套接字有以下3个理由: 1.UNIX域套接字往往比通信两端位于同一个主机的TCP套接字快出一倍 2.UNIX域套接字可用于在同 ...
- UNP学习 Unix域协议
Unix域协议并不是一个实际的协议族,它只是在同一台主机上进行客户-服务器通信时,使用与在不同主机上的客户和服务器间通信时相同的API的一种方法. 当客户和服务器在同一台主机上时,Unix域协议是这套 ...
- 《Unix 网络编程》15:Unix 域协议
Unix 域协议 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ 本 ...
- UNIX网络编程读书笔记:UNIX域协议
概述 UNIX域协议并不是一个实际的协议族,而是在单个主机上执行客户/服务器通信的一种方法,所用API与在不同主机上执行客户/服务器通信所用的API(套接口API)相同.UNIX域协议可视为进程间通信 ...
- NDK开发之获得域和方法描述符
在NDK开发之调用方法和NDK开发之访问域两篇博客中,我们在获得域ID和方法ID时都需要一个叫做描述符的参数,那么在实际开发中我们怎么知道我们要调用的域或者方法的描述符呢? 一个简单的方法就是使用Ja ...
- UNIX域协议(无名套接字)
关于什么是UNIX域套接字可以参考:http://www.cnblogs.com/xcywt/p/8185597.html这里主要介绍非命名的UNIX域套接字的用法.1.socketpair函数先看m ...
随机推荐
- [20190416]exclusive latch测试脚本.txt
[20190416]exclusive latch测试脚本.txt --//昨天做了shared latch的测试脚本,今天完善exclusive latch测试脚本,上个星期的测试我是手工执行的.- ...
- Python Learning: 02
OK, let's continue. Conditional Judgments and Loop if if-else if-elif-else while for break continue ...
- [idea] SpringBoot整合swagger2实现CRUD
一:创建SpringBoot ,在pom.xml文件中加入jar包 <dependency> <groupId>io.springfox</groupId> < ...
- 老白关于rac性能调优的建议(10gRAC)
RAC应用设计方面需要在底层做很有设计.虽然ORACLE的售前人员总是说RAC的扩展性是透明的,只要把应用分到不同的节点,就可以平滑的扩展系统能力了.而事实上,RAC的CACHE FUSION机制决定 ...
- 关于 insufficient memory case 4 的解决记录
用户在上传图片的时候,系统会报异常 insufficient memory case 4,追踪代码发生在jdk中 image.io 的 read() 方法.这是一个耽搁了很久的bug,客户反馈了好几次 ...
- 三分钟明白 Activiti工作流 -- java运用
原文地址:https://blog.csdn.net/jiangyu1013/article/details/73250902 一. 什么是工作流 以请假为例,现在大多数公司的请假流程是这样的 员工打 ...
- 正则表达式和re模块
目录 re的元字符 字符集[ ] 转义符 分组 ( ) |符号 re下的常用方法 分组 re的元字符 import re ret = re.findall("e..a", &quo ...
- Python------Mongodb操作
Python3要操作Mongodb需要下载pymongo,Linux下获取pymongo的方法也比较简单,控制台输入命令:sudo pip3 install pymongo 即可. Pymongo的文 ...
- Linux(Ubuntu)使用日记------Mongodb的安装与使用
1.安装 Linux下安装mongodb还是比较容易的 直接使用apt-get安装即可,命令如下: sudo apt-get install mongodb 安装完成之后进行检验, “mongo sh ...
- 其他综合-内网下Yum仓库搭建配置
内网下Yum仓库搭建配置 1.实验环境 虚拟机环境: VMware 12 版本虚拟机 网络环境: 内网 IP 段:172.16.1.0 外网 iP 段(模拟):10.0.0.0 实验基础:(能够上网, ...