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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "memfd.h"
#include "ipc_msg.h"
 
 
int memfd_read(int fd)
{
  unsigned char* addr;
  ssize_t  i, len;
  len = basic_shm_mmap(fd, &addr);
  if(len < 0)
  {
    return -1;
  }
  printf ("start: ");
  for (i = 0; i < 32; ++i)
    printf ("%i ", addr[i]);
  printf ("\nend: ");
  for (i = 0; i < 32; ++i)
    printf ("%i ", addr[len-32+i]);
  printf ("\ndone\n");
  return len;
}
 
 
int main(int argc, char **argv)
{
  int fd = 0;
  int ret;
  int recv_len = 0;
  memfd_data_st  memfd_data = {0};
  char snd_buf[MAX_LEN] = {0};
  char * unix_domain_path = NULL;
 
  char *recv_buf = (char *)malloc(MAX_LEN);
 
  /*client get memfd from server*/
  memset(snd_buf, 0, sizeof(snd_buf));
  strncpy(snd_buf, GET_MEMFD_MSG, strlen(GET_MEMFD_MSG) + 1);
  printf("snd_buf: %s\n", snd_buf);  
  memset(recv_buf, 0, MAX_LEN);
 
  unix_domain_path = UNIX_DOMAIN;
  ret = basic_create_ipc_client(unix_domain_path, snd_buf, strlen(snd_buf), (void **)&recv_buf, &recv_len);
  if(ret < 0)
  {
    return ret;
  }
 
  memcpy(&memfd_data, recv_buf, sizeof(memfd_data));
 
  printf("receive message from server,pid:%d, memfd:%d\n", memfd_data.pid, memfd_data.memfd);
  if ((memfd_data.pid != 0) && (memfd_data.memfd != 0))
  {
    char fd_path[256] = {0};
    snprintf(fd_path, sizeof(fd_path), "/proc/%d/fd/%d", memfd_data.pid, memfd_data.memfd);
    fprintf(stderr,"fd_path:%s\n", fd_path);
    fd = basic_shm_open(memfd_data.memfd, memfd_data.pid, 1);
    ret = memfd_read(fd);
  }
 
  basic_shm_close(fd);
 
  /*client get memfd and custom data from server*/
#if 1
 
  memset(snd_buf, 0, sizeof(snd_buf));
  strncpy(snd_buf, GET_CUSTOM_MSG, strlen(GET_CUSTOM_MSG) + 1);
  printf("\nsnd_buf: %s\n", snd_buf);  
  memset(recv_buf, 0, MAX_LEN);
 
  unix_domain_path = UNIX_DOMAIN;
  ret = basic_create_ipc_client(unix_domain_path, snd_buf, strlen(snd_buf), (void **)&recv_buf, &recv_len);
  if(ret < 0)
  {
    return ret;
  }
 
  memcpy(&memfd_data, recv_buf, sizeof(memfd_data));
  if(memfd_data.len > 0)
  {
      memfd_data.data = malloc(memfd_data.len);
      memcpy(memfd_data.data, recv_buf+sizeof(memfd_data), memfd_data.len);
  }
 
  printf("receive message from server,pid:%d, memfd:%d\n", memfd_data.pid, memfd_data.memfd);
  if ((memfd_data.pid != 0) && (memfd_data.memfd != 0))
  {
    char fd_path[256] = {0};
    snprintf(fd_path, sizeof(fd_path), "/proc/%d/fd/%d", memfd_data.pid, memfd_data.memfd);
    fprintf(stderr,"fd_path:%s\n", fd_path);
    fd = basic_shm_open(memfd_data.memfd, memfd_data.pid, 1);
    ret = memfd_read(fd);
  }
 
  basic_shm_close(fd);
  fprintf(stderr,"custom data:%s\n", (char *)memfd_data.data);
  if(memfd_data.len > 0)
  {
     free(memfd_data.data);
  }
#endif
 
#if 1
  /*client get usedata from server*/
  memset(snd_buf, 0, sizeof(snd_buf));
  strncpy(snd_buf, GET_DATA_MSG, strlen(GET_DATA_MSG) + 1);
  printf("\nsnd_buf: %s\n", snd_buf);  
  memset(recv_buf, 0, MAX_LEN);  
  recv_len = 0;
 
  //unix_domain_path="/tmp/unix_domain_usedata";
  ret = basic_create_ipc_client(unix_domain_path, snd_buf, strlen(snd_buf), (void **)&recv_buf, &recv_len);
  if(ret < 0)
  {
    return ret;
  }
  printf("receive message from server msg:%s\n", recv_buf);
  /*deal the recv_buf*/
#endif
 
  free(recv_buf);
  return 0;
 
}