shm implemented as memfd syscall
cheliequan
2022-12-27 54951dfb930bea890aef14be02236f81b3a19f2e
src/memfd.c
@@ -12,6 +12,7 @@
#include <time.h>
#include <stdarg.h>
#include "memfd.h"
#include <assert.h>
#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
} while (0)
@@ -70,12 +71,16 @@
  struct stat st;
  /* Create an anonymous file in tmpfs; */
  if(0 >= len)
  {
      return -1;
  }
  fd = sys_memfd_create(name, MFD_CLOEXEC);
  if (fd == -1)
  {
    errExit("memfd_create");
      errExit("memfd_create");
  }
  /* Size the file as specified on the command line */
@@ -83,7 +88,7 @@
  mydebug("length: %zu\n", len);
  if (ftruncate(fd, len) == -1)
  {
      errExit("truncate");
      errExit("ftruncate");
  }
  if (fstat (fd, &st))
@@ -174,12 +179,12 @@
    }
    len = st.st_size;
    ret = munmap(*ppaddr, len);
    ret = munmap((void *)*ppaddr, len);
    if (ret == -1)
    {
        errExit("munmap()");
    }
    *ppaddr = NULL;
    mydebug("length: %zu, atime: %lu.%lu\n", len, st.st_atim.tv_sec, st.st_atim.tv_nsec);
    return len;
}