-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsend_fd_server.c
106 lines (80 loc) · 2.08 KB
/
send_fd_server.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/un.h>
#include <stddef.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int send_fd(int sock, int fd)
{
iovec iov[1];
char c = 0;
iov[0].iov_base = &c;
iov[0].iov_len = 1;
int cmsgsize = CMSG_LEN(sizeof(int));
cmsghdr* cmptr = (cmsghdr*)malloc(cmsgsize);
if(cmptr == NULL){
printf("[send_fd] init cmptr error\n");
exit(1);
}
cmptr->cmsg_level = SOL_SOCKET;
cmptr->cmsg_type = SCM_RIGHTS; // we are sending fd.
cmptr->cmsg_len = cmsgsize;
msghdr msg;
msg.msg_iov = iov;
msg.msg_iovlen = 1;
msg.msg_name = NULL;
msg.msg_namelen = 0;
msg.msg_control = cmptr;
msg.msg_controllen = cmsgsize;
*(int *)CMSG_DATA(cmptr) = fd;
int ret = sendmsg(sock, &msg, 0);
free(cmptr);
if (ret == -1){
printf("[send_fd] sendmsg error\n");
exit(1);
}
}
int main()
{
int fd;
int ret;
struct sockaddr_un addr, addr1;
socklen_t len;
int i;
char str[1024];
fd = socket(AF_UNIX, SOCK_STREAM, 0);
if ( fd == -1)
perror("socket error");
addr.sun_family = AF_UNIX;
sprintf(addr.sun_path, "xliuwei");
addr.sun_path[0]=0;
len = offsetof(struct sockaddr_un, sun_path) + 7;
printf("len=%d\n", (long long)(len) );
ret = bind(fd, (struct sockaddr*)(&addr), len);
if ( ret == -1)
perror("bind fail\n");
ret = listen(fd, 4);
if ( ret == -1)
perror("listen fail\n");
len = sizeof(addr1);
ret = accept(fd, (struct sockaddr*)(&addr1), &len);
if ( ret == -1 )
perror("accept fail\n");
int ff = open("aaaaaa", O_CREAT|O_TRUNC|O_RDWR, 00777);
if ( ff <0 )
perror(" file open error\n");
send_fd(ret, ff);
return 0;
i=0;
while(1)
{
printf("ok\n");
sprintf(str, "i=%d", i);
// write(ret , str, strlen(str));
//
sleep(1);
i++;
}
}