shm implemented as memfd syscall
zhangmeng
2023-07-27 157b3411dd123694ca29dd80fe9ecc683958ccab
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
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include "ipc_msg.h"
 
 
int basic_create_ipc_client(char * unix_domain_path, char * send_buf, int send_len, void ** pptr_recv_buf, int *ptr_recv_len)
{
  int connect_fd;
  struct sockaddr_un srv_addr;
  int ret;
  char buf[MAX_LEN] = {0};
  int recv_len = 0;
 
  if((NULL == pptr_recv_buf)||(NULL == ptr_recv_len))
  {
    return -1;
  }
 
  if (strlen(unix_domain_path) > sizeof(srv_addr.sun_path))
  {
    perror("too long filename!\n");
    return -1;
  }
 
  connect_fd = socket(AF_UNIX, SOCK_STREAM, 0);
 
  if(connect_fd < 0)
  {
    perror("client create socket failed");
    return -1;
  }
  srv_addr.sun_family = AF_UNIX;
 
  if(unix_domain_path == NULL)
  {
    unix_domain_path  = UNIX_DOMAIN;
  }
 
  strcpy(srv_addr.sun_path, unix_domain_path);
  ret = connect(connect_fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr));
  if(ret == -1)
  {
    perror("connect to server failed!");
    close(connect_fd);
    //unlink(unix_domain_path);
    return -1;
  }
 
  printf("connect to server path:%s success!", srv_addr.sun_path);
 
  //memset(rcv_buf, 0, sizeof(rcv_buf));
  //int rcv_num = read(connect_fd, rcv_buf, sizeof(rcv_buf));
  //memcpy(&memfd_data, rcv_buf, sizeof(memfd_data_st));
 
  printf("send data to server... ...\n");
  write(connect_fd, send_buf, send_len);
  printf("send end!\n");
 
  recv_len = read(connect_fd, buf, sizeof(buf));
  if(recv_len == 0)
  {
    close(connect_fd);    
    return -1;
  }
 
  memcpy(*pptr_recv_buf, buf, recv_len);
  *ptr_recv_len = recv_len;
  close(connect_fd);
 
  return 0;
}