#include "unp.h"
int my_open(const char *,int);
int
main(int argc, char **argv)
{
int fd,n;
char buff[BUFFSIZE];
if(argc!=2)
err_quit("usage:mycat ");
if((fd=open(argv[1],O_RDONLY))<0)
err_sys("cannot open %s",argv[1]);
while((n=Read(fd,buff,BUFFSIZE))>0){
printf("%d\n",n);
Write(STDOUT_FILENO,buff,n);
}
exit(0);
}
int
my_open(const char *pathname, int mode)
{
int fd,sockfd[2],status;
pid_t childpid;
char c,argsockfd[10],argmode[10];
Socketpair(AF_LOCAL,SOCK_STREAM,0,sockfd);
if((childpid=Fork())==0){
Close(sockfd[0]);
snprintf(argsockfd,sizeof(argsockfd),"%d",sockfd[1]);
snprintf(argmode,sizeof(argmode),"%d",mode);
execl("./openfile","openfile",argsockfd,pathname,argmode,(char*)NULL);
err_sys("execl error");
}
Close(sockfd[1]);
Waitpid(childpid,&status,0);//这个status是openfile主函数的返回值。
if(WIFEXITED(status)==0)
err_quit("child did not terminate");
if((status=WEXITSTATUS(status))==0)
Read_fd(sockfd[0],&c,1,&fd);
else{
errno=status;
fd=-1;
}
Close(sockfd[0]);
return(fd);
}
#include "unp.h"
int
main(int argc,char **argv)
{
int fd;
if(argc!=4)
err_quit("openfile ");
if((fd=open(argv[2],atoi(argv[3])))<0)
exit((errno>0)?errno:255);
if(write_fd(atoi(argv[1]),"",1,fd)<0)
exit((errno>0)?errno:255);
exit(0);
}
//read_fd()
#include "unp.h"
ssize_t
read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
{
struct msghdr msg;
struct iovec iov[1];
ssize_t n;
#ifdef HAVE_MSGHDR_MSG_CONTROL
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);
#else
int newfd;
msg.msg_accrights = (caddr_t) &newfd;
msg.msg_accrightslen = sizeof(int);
#endif
msg.msg_name = NULL;
msg.msg_namelen = 0;
iov[0].iov_base = ptr;
iov[0].iov_len = nbytes;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
if ( (n = recvmsg(fd, &msg, 0)) <= 0)
return(n);
#ifdef HAVE_MSGHDR_MSG_CONTROL
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");
//cmsg_level和cmsg_type见unp图14-11
if (cmptr->cmsg_type != SCM_RIGHTS)
err_quit("control type != SCM_RIGHTS");
*recvfd = *((int *) CMSG_DATA(cmptr));
} else
*recvfd = -1; /* descriptor was not passed */
#else
/* *INDENT-OFF* */
if (msg.msg_accrightslen == sizeof(int))
*recvfd = newfd;
else
*recvfd = -1; /* descriptor was not passed */
/* *INDENT-ON* */
#endif
return(n);
}
/* end read_fd */
ssize_t
Read_fd(int fd, void *ptr, size_t nbytes, int *recvfd)
{
ssize_t n;
if ( (n = read_fd(fd, ptr, nbytes, recvfd)) < 0)
err_sys("read_fd error");
return(n);
}
一、snprintf()
int snprintf ( char * s, size_t n, const char * format, ... );
/* snprintf example */
#include
int main ()
{
char buffer [100];
int cx;
cx = snprintf ( buffer, 100, "The half of %d is %d", 60, 60/2 );
if (cx>=0 && cx<100) // check returned value
snprintf ( buffer+cx, 100-cx, ", and the half of that is %d.", 60/2/2 );
puts (buffer);
return 0;
}
Output:
The half of 60 is 30, and the half of that is 15.
二、几个宏
子进程的结束状态返回后存于status,底下有几个宏可判别结束情况
WIFEXITED(status)如果子进程正常结束则为非0值。
WEXITSTATUS(status)取得子进程exit()返回的结束代码,一般会先用WIFEXITED 来判断是否正常结束才能使用此宏。
WIFSIGNALED(status)如果子进程是因为信号而结束则此宏值为真
WTERMSIG(status)取得子进程因信号而中止的信号代码,一般会先用WIFSIGNALED 来判断后才使用此宏。
WIFSTOPPED(status)如果子进程处于暂停执行情况则此宏值为真。一般只有使用WUNTRACED 时才会有此情况。
WSTOPSIG(status)取得引发子进程暂停的信号代码,一般会先用WIFSTOPPED 来判断后才使用此宏。
三、socketpair函数
#include
int socketpair(int family, int type, int protocol, int sockfd[2]);
//成功返回非0,出错为-1;
本函数会返回两个彼此连接的描述符。