shm implemented as memfd syscall
cheliequan
2022-12-28 ca312c023b11d7885227addc7564494998d8898c
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
#include <stdio.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 i;
  char *fd_path;
  memfd_data_st  memfd_data = {0};
  int rcv_num = 0;
 
  ret = basic_create_ipc_client(UNIX_DOMAIN, &memfd_data);
  if(ret < 0)
  {
    return ret;
  }
 
  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);
  return 0;
 
}