| | |
| | | #include <stdlib.h> |
| | | #include <time.h> |
| | | #include <stdarg.h> |
| | | #include "memfd.h" |
| | | #include <assert.h> |
| | | |
| | | #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ |
| | | } while (0) |
| | |
| | | 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 */ |
| | |
| | | mydebug("length: %zu\n", len); |
| | | if (ftruncate(fd, len) == -1) |
| | | { |
| | | errExit("truncate"); |
| | | errExit("ftruncate"); |
| | | } |
| | | |
| | | if (fstat (fd, &st)) |
| | |
| | | |
| | | int basic_shm_open(int fd, pid_t pid, int flags) |
| | | { |
| | | if(0 >= flags) |
| | | { |
| | | basic_shm_path_open(fd, getpid()); |
| | | int memfd = -1; |
| | | switch(flags){ |
| | | case local_open_flag: |
| | | memfd = basic_shm_path_open(fd, getpid()); |
| | | break; |
| | | case remote_open_flag: |
| | | memfd = basic_shm_path_open(fd, pid); |
| | | break; |
| | | default: |
| | | mydebug("not support\n"); |
| | | break; |
| | | } |
| | | else |
| | | { |
| | | basic_shm_path_open(fd, pid); |
| | | } |
| | | return memfd; |
| | | } |
| | | |
| | | int basic_shm_mmap(int fd, unsigned char** ppaddr) |
| | |
| | | } |
| | | 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; |
| | | } |
| | | |
| | | int basic_shm_close(int fd) |
| | | { |
| | | int ret = -1; |
| | | if (fd >= 0) |
| | | { |
| | | close(fd); |
| | | ret = close(fd); |
| | | } |
| | | return ret; |
| | | } |
| | | |
| | | static void mfd_assert_size(int fd, size_t size) |