shm implemented as memfd syscall
cheliequan
2022-12-02 ddc93e90e36d78d110b4168768ce49c4d06e80f8
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
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include "memfd.h"
#include <errno.h>
#include <stdio.h>
 
#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
 
int main(int argc, char *argv[])
{
  ssize_t i, len;
  unsigned char* paddr;
  struct stat st;
  pid_t pid = 0;
  int fd = 0;
  int ret = 0;
 
  if (argc != 3) {
    fprintf(stderr, "%s PID FD\n", argv[0]);
    exit(EXIT_FAILURE);
  }
 
  pid = atol(argv[1]);
  fd = atol(argv[2]);
 
  fd = basic_shm_open(fd, pid, 1);
  if (fd <= 0)
  {
    errExit("basic_shm_open");
  }
 
 
  if (fstat (fd, &st))
    errExit ("fstat");
 
 
  len = st.st_size;
 
  printf("length: %zu, atime: %lu.%lu\n", len, st.st_atim.tv_sec, st.st_atim.tv_nsec);
 
  ret =  basic_shm_mmap(fd, &paddr);
  if (ret < 0)
    errExit("basic_shm_mmap");
 
  printf ("start: ");
  for (i = 0; i < 32; ++i)
    printf ("%i ", paddr[i]);
  printf ("\nend: ");
  for (i = 0; i < 32; ++i)
    printf ("%i ", paddr[len-32+i]);
  printf ("\ndone\n");
 
  ret =  basic_shm_unmmap(fd, &paddr);
  if (ret < 0)
    errExit("basic_shm_unmmap");
    
  basic_shm_close(fd);
 
  pause();
 
  exit(EXIT_SUCCESS);
}