mirror of /home/gitosis/repositories/libowfat.git
parent
5959a6d634
commit
ec2f230bb4
5 changed files with 129 additions and 0 deletions
@ -0,0 +1,32 @@ |
||||
#include <sys/types.h> |
||||
#include <sys/socket.h> |
||||
#include <sys/uio.h> |
||||
#include <errno.h> |
||||
#include "io_internal.h" |
||||
|
||||
union fdmsg { |
||||
struct cmsghdr h; |
||||
char buf[CMSG_SPACE(sizeof(int))]; |
||||
}; |
||||
|
||||
int io_passfd(int64 sock,int64 fd) { |
||||
struct msghdr msg = {0}; |
||||
struct cmsghdr *cmsg; |
||||
struct iovec iov; |
||||
char buf[CMSG_SPACE(sizeof(int))]; |
||||
iov.iov_len=1; |
||||
iov.iov_base="x"; |
||||
msg.msg_control = buf; |
||||
msg.msg_controllen = sizeof buf; |
||||
msg.msg_iov=&iov; |
||||
msg.msg_iovlen=1; |
||||
msg.msg_name=0; |
||||
msg.msg_namelen=0; |
||||
cmsg = CMSG_FIRSTHDR(&msg); |
||||
cmsg->cmsg_level = SOL_SOCKET; |
||||
cmsg->cmsg_type = SCM_RIGHTS; |
||||
cmsg->cmsg_len = CMSG_LEN(sizeof(int)); |
||||
*((int*)CMSG_DATA(cmsg))=fd; |
||||
msg.msg_controllen = cmsg->cmsg_len; |
||||
return sendmsg(sock,&msg,0); |
||||
} |
@ -0,0 +1,45 @@ |
||||
#include <sys/types.h> |
||||
#include <sys/socket.h> |
||||
#include <sys/uio.h> |
||||
#include <errno.h> |
||||
#include "io_internal.h" |
||||
|
||||
union fdmsg { |
||||
struct cmsghdr h; |
||||
char buf[CMSG_SPACE(sizeof(int))]; |
||||
}; |
||||
|
||||
int64 io_receivefd(int64 sock) { |
||||
struct iovec iov; |
||||
struct msghdr msg; |
||||
union fdmsg cmsg; |
||||
struct cmsghdr* h; |
||||
char x[100]; |
||||
char name[100]; |
||||
iov.iov_base=x; |
||||
iov.iov_len=100; |
||||
msg.msg_name=name; |
||||
msg.msg_namelen=100; |
||||
msg.msg_control=cmsg.buf; |
||||
msg.msg_controllen=sizeof(union fdmsg); |
||||
msg.msg_iov = &iov; |
||||
msg.msg_iovlen = 1; |
||||
msg.msg_flags=0; |
||||
h=CMSG_FIRSTHDR(&msg); |
||||
h->cmsg_len=CMSG_LEN(sizeof(int)); |
||||
h->cmsg_level=SOL_SOCKET; |
||||
h->cmsg_type=SCM_RIGHTS; |
||||
*((int*)CMSG_DATA(h))=-1; |
||||
if (recvmsg(sock,&msg,0)==-1) |
||||
return -1; |
||||
h=CMSG_FIRSTHDR(&msg); |
||||
if (!h || h->cmsg_len!=CMSG_LEN(sizeof(int)) || h->cmsg_level!=SOL_SOCKET || h->cmsg_type!=SCM_RIGHTS) { |
||||
#ifdef EPROTO |
||||
errno=EPROTO; |
||||
#else |
||||
errno=EINVAL; |
||||
#endif |
||||
return -1; |
||||
} |
||||
return *((int*)CMSG_DATA(h)); |
||||
} |
@ -0,0 +1,39 @@ |
||||
#include <unistd.h> |
||||
#include "io.h" |
||||
#include "buffer.h" |
||||
|
||||
void child(int64 fd) { |
||||
int64 x=io_receivefd(fd); |
||||
char buf[8192]; |
||||
int i; |
||||
if (x==-1) { |
||||
buffer_putsflush(buffer_2,"fd passing failed!\n"); |
||||
exit(1); |
||||
} |
||||
i=read(x,buf,sizeof(buf)); |
||||
write(1,buf,i); |
||||
io_close(x); |
||||
} |
||||
|
||||
void father(int64 fd) { |
||||
int64 x; |
||||
if (io_readfile(&x,"/etc/resolv.conf")) |
||||
io_passfd(fd,x); |
||||
} |
||||
|
||||
main() { |
||||
int64 sp[2]; |
||||
if (io_socketpair(sp)==-1) return 1; |
||||
switch (fork()) { |
||||
case -1: return 1; |
||||
case 0: /* child */ |
||||
io_close(sp[0]); |
||||
child(sp[1]); |
||||
break; |
||||
default: |
||||
io_close(sp[1]); |
||||
father(sp[0]); |
||||
break; |
||||
} |
||||
return 0; |
||||
} |
Loading…
Reference in new issue