Unix Network Programming Episode 97

Unix Domain Datagram Client/Server
#include "unp.h"

int main(int argc, char **argv)
{
    int sockfd;
    struct sockaddr_un servaddr;

    sockfd=Socket(AF_LOCAL, SOCK_STREAM,0);

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sun_family=AF_LOCAL;
    strcpy(servaddr.sun_path, UNIXSTR_PATH);

    Connect(sockfd, (SA *)&servaddr, sizeof(servaddr));
    str_cli(sdin, sockfd);

    return 0;
}

Unix domain stream protocol echo client

#include "unp.h"

int main(int argc, char **argv)
{
    int sockfd;
    struct sockaddr_un servaddr, clientaddr;

    sockfd=Socket(AF_LOCAL, SOCK_STREAM,0);

    unlink(UNIXDG_PATH);
    bzero(&servaddr, sizeof(servaddr));
    servaddr.sun_family=AF_LOCAL;
    strcpy(servaddr.sun_path, UNIXSTR_PATH);

    Bind(sockfd, (SA *)&servaddr, sizeof(servaddr));

    dg_echo(sockfd, (SA *)&clientaddr, sizeof(clientaddr));

    return 0;
}

Unix domain datagram protocol echo server

#include "unp.h"

int main(int argc, char **argv)
{
    int sockfd;
    struct sockaddr_un servaddr, clientaddr;

    sockfd=Socket(AF_LOCAL, SOCK_DGRAM,0);

    bzero(&clientaddr, sizeof(clientaddr));
    clientaddr.sun_family=AF_LOCAL;
    strcpy(clientaddr.sun_path, tmpnam(NULL));

    Bind(sockfd, (SA *)&clientaddr, sizeof(clientaddr));

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sun_family=AF_LOCAL;
    strcpy(servaddr.sun_path, UNIXDG_PATH);
    dg_echo(stdin,sockfd, (SA *)&servaddr, sizeof(servaddr));

    return 0;
}

Unix domain datagram protocol echo client

Passing Descriptors

When we think of passing an open descriptor from one process to another, we normally think of either

  • A child sharing all the open descriptors with the parent after a call to fork
  • All descriptors normally remaining open when exec is called

The 4.4BSD technique allows multiple descriptors to be passed with a single sendmsg, whereas the SVR4 technique passes only a single descriptor at a time. All our examples pass one descriptor at a time.

The steps involved in passing a descriptor between two processes are then as follows:

1.Create a Unix domain socket, either a stream socket or a datagram socket.
2.One process opens a descriptor by calling any of the Unix functions that returns a descriptor: open, pipe, mkfifo, socket, or accept, for example. Any type of descriptor can be passed from one process to another, which is why we call the technique “descriptor passing” and not “file descriptor passing.”
3.The sending process builds a msghdr structure (Section 14.5(See 9.3.5)) containing the descriptor to be passed. POSIX specifies that the descriptor be sent as ancillary data (the msg_control member of the msghdr structure, Section 14.6(See 9.3.6)), but older implementations use the msg_accrights member. The sending process calls sendmsg to send the descriptor across the Unix domain socket from Step 1. At this point, we say that the descriptor is “in flight.” Even if the sending process closes the descriptor after calling sendmsg, but before the receiving process calls recvmsg (in the next step), the descriptor remains open for the receiving process. Sending a descriptor increments the descriptor’s reference count by one.
4.The receiving process calls recvmsg to receive the descriptor on the Unix domain socket from Step 1. It is normal for the descriptor number in the receiving process to differ from the descriptor number in the sending process. Passing a descriptor is not passing a descriptor number, but involves creating a new descriptor in the receiving process that refers to the same file table entry within the kernel as the descriptor that was sent by the sending process.

Descriptor Passing Example

We now provide an example of descriptor passing. We will write a program named mycat that takes a pathname as a command-line argument, opens the file, and copies it to standard output. But instead of calling the normal Unix open function, we call our own function named my_open. This function creates a stream pipe and calls fork and exec to initiate another program that opens the desired file. This program must then pass the open descriptor back to the parent across the stream pipe.

  • 18
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值