| | |
| | | shmht: shmht.o memfd.o |
| | | $(CC) -o libshmht.so $(CFLAGS) -shared $^ |
| | | $(AR) rcs libshmht.a $^ |
| | | shmht.o: shmht.c shmht.h |
| | | shmht.o: shmht.c |
| | | $(CC) $(CFLAGS) $(INCLUDE) -fPIC -c shmht.c |
| | | shmht_mytests: shmht_mytests.o shmht.o memfd.o |
| | | $(CC) $(CFLAGS) $(INCLUDE) -L . -pthread -o $@ $^ |
| | | shmht_mytests.o: shmht.h |
| | | shmht_mytests.o: |
| | | $(CC) $(CFLAGS) $(INCLUDE) -fPIC -c shmht_mytests.c |
| | | memfd.o: |
| | | $(CC) $(CFLAGS) $(INCLUDE) -fPIC -c memfd.c |
| | |
| | | $(CC) $(CFLAGS) $(INCLUDE) -fPIC -pthread -o $@ ipc_server.c -lipc_server -L . |
| | | libipc_server.so: |
| | | $(CC) -shared -fPIC -o $@ ipc_server_lib.c ipc_msg.c memfd.c |
| | | ipc_client: |
| | | $(CC) $(CFLAGS) $(INCLUDE) -fPIC -o $@ ipc_client.c ipc_msg.c memfd.c |
| | | ipc_client:libipc_client.so |
| | | $(CC) $(CFLAGS) $(INCLUDE) -fPIC -o $@ ipc_client.c ipc_msg.c memfd.c -lipc_client -L . |
| | | libipc_client.so: |
| | | $(CC) -shared -fPIC -o $@ ipc_client_lib.c ipc_msg.c memfd.c |
| | | clean: |
| | | rm -rf *.so *.o a.out shmht_mytests *.a ipc_client ipc_server |
| | |
| | | #include <stdio.h> |
| | | #include <stdlib.h> |
| | | #include <sys/socket.h> |
| | | #include <sys/un.h> |
| | | #include <unistd.h> |
| | | #include <sys/syscall.h> |
| | | #include <sys/mman.h> |
| | | #include <sys/stat.h> |
| | | #include <fcntl.h> |
| | | #include <string.h> |
| | | #include <stdio.h> |
| | | #include <sys/inotify.h> |
| | | #include <sys/time.h> |
| | | #include <errno.h> |
| | | #include "memfd.h" |
| | | #include "ipc_msg.h" |
| | | |
| | | |
| | | #define errExit(msg) do { perror(msg); exit(EXIT_FAILURE); \ |
| | | } while (0) |
| | | |
| | | |
| | | int memfd_open(char* fd_path) |
| | | { |
| | | ssize_t len; |
| | | int fd; |
| | | struct stat st; |
| | | fd = open(fd_path, O_RDWR); |
| | | if (fd == -1) |
| | | errExit("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); |
| | | |
| | | return fd; |
| | | } |
| | | |
| | | int memfd_read(int fd) |
| | | { |
| | | unsigned char* addr; |
| | | struct stat st; |
| | | |
| | | ssize_t i, len; |
| | | if (fstat (fd, &st)) |
| | | errExit ("fstat"); |
| | | len = st.st_size; |
| | | |
| | | addr = (unsigned char*) mmap (NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
| | | if (addr == MAP_FAILED) |
| | | errExit("mmap"); |
| | | |
| | | len = basic_shm_mmap(fd, &addr); |
| | | if(len < 0) |
| | | { |
| | | return -1; |
| | | } |
| | | printf ("start: "); |
| | | for (i = 0; i < 32; ++i) |
| | | printf ("%i ", addr[i]); |
| | |
| | | return len; |
| | | } |
| | | |
| | | |
| | | int main(int argc, char **argv) |
| | | { |
| | | int fd; |
| | | int connect_fd; |
| | | struct sockaddr_un srv_addr; |
| | | char snd_buf[MAX_LEN] = {0}; |
| | | char rcv_buf[MAX_LEN] = {0}; |
| | | int fd = 0; |
| | | int ret; |
| | | int i; |
| | | char *fd_path; |
| | | memfd_data_st memfd_data = {0}; |
| | | int rcv_num = 0; |
| | | |
| | | connect_fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| | | |
| | | if(connect_fd < 0) |
| | | ret = basic_create_ipc_client(UNIX_DOMAIN, &memfd_data); |
| | | if(ret < 0) |
| | | { |
| | | perror("client create socket failed"); |
| | | return 1; |
| | | return ret; |
| | | } |
| | | srv_addr.sun_family = AF_UNIX; |
| | | strcpy(srv_addr.sun_path, UNIX_DOMAIN); |
| | | ret = connect(connect_fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr)); |
| | | |
| | | if(ret == -1) |
| | | { |
| | | perror("connect to server failed!"); |
| | | close(connect_fd); |
| | | unlink(UNIX_DOMAIN); |
| | | return 1; |
| | | } |
| | | |
| | | printf("connect to server path:%s success!", srv_addr.sun_path); |
| | | |
| | | //memset(rcv_buf, 0, sizeof(rcv_buf)); |
| | | //int rcv_num = read(connect_fd, rcv_buf, sizeof(rcv_buf)); |
| | | //memcpy(&memfd_data, rcv_buf, sizeof(memfd_data_st)); |
| | | |
| | | memset(snd_buf, 0, 256); |
| | | strcpy(snd_buf, HELLO_MSG); |
| | | printf("sizeof(snd_buf): %ld\n", sizeof(snd_buf)); |
| | | |
| | | printf("send data to server... ...\n"); |
| | | // for(i = 0; i < 4; i++) |
| | | { |
| | | write(connect_fd, snd_buf, sizeof(snd_buf)); |
| | | } |
| | | printf("send end!\n"); |
| | | |
| | | |
| | | memset(rcv_buf, 0, sizeof(rcv_buf)); |
| | | rcv_num = read(connect_fd, rcv_buf, sizeof(rcv_buf)); |
| | | if(rcv_num == 0) |
| | | { |
| | | close(connect_fd); |
| | | return 0; |
| | | } |
| | | memcpy(&memfd_data, rcv_buf, rcv_num); |
| | | |
| | | 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); |
| | | fd = memfd_open(fd_path); |
| | | fprintf(stderr,"fd_path:%s\n", fd_path); |
| | | fd = basic_shm_open(memfd_data.memfd, memfd_data.pid , 1); |
| | | ret = memfd_read(fd); |
| | | } |
| | | |
| | | close(connect_fd); |
| | | basic_shm_close(fd); |
| | | return 0; |
| | | |
| | | } |
New file |
| | |
| | | #include <stdio.h> |
| | | #include <stdlib.h> |
| | | #include <sys/socket.h> |
| | | #include <sys/un.h> |
| | | #include <unistd.h> |
| | | #include <sys/stat.h> |
| | | #include <fcntl.h> |
| | | #include <string.h> |
| | | #include <stdio.h> |
| | | #include <errno.h> |
| | | #include "ipc_msg.h" |
| | | |
| | | int basic_create_ipc_client(char * unix_domain_path, memfd_data_st * ptr_memfd_data) |
| | | { |
| | | int fd; |
| | | int connect_fd; |
| | | struct sockaddr_un srv_addr; |
| | | char snd_buf[MAX_LEN] = {0}; |
| | | char rcv_buf[MAX_LEN] = {0}; |
| | | int ret; |
| | | int i; |
| | | char *fd_path; |
| | | int rcv_num = 0; |
| | | |
| | | connect_fd = socket(AF_UNIX, SOCK_STREAM, 0); |
| | | |
| | | if(connect_fd < 0) |
| | | { |
| | | perror("client create socket failed"); |
| | | return 1; |
| | | } |
| | | srv_addr.sun_family = AF_UNIX; |
| | | |
| | | if(unix_domain_path == NULL) |
| | | { |
| | | unix_domain_path = UNIX_DOMAIN; |
| | | } |
| | | |
| | | strcpy(srv_addr.sun_path, unix_domain_path); |
| | | ret = connect(connect_fd, (struct sockaddr*)&srv_addr, sizeof(srv_addr)); |
| | | if(ret == -1) |
| | | { |
| | | perror("connect to server failed!"); |
| | | close(connect_fd); |
| | | unlink(UNIX_DOMAIN); |
| | | return 1; |
| | | } |
| | | |
| | | printf("connect to server path:%s success!", srv_addr.sun_path); |
| | | |
| | | //memset(rcv_buf, 0, sizeof(rcv_buf)); |
| | | //int rcv_num = read(connect_fd, rcv_buf, sizeof(rcv_buf)); |
| | | //memcpy(&memfd_data, rcv_buf, sizeof(memfd_data_st)); |
| | | |
| | | memset(snd_buf, 0, 256); |
| | | strcpy(snd_buf, HELLO_MSG); |
| | | printf("sizeof(snd_buf): %ld\n", sizeof(snd_buf)); |
| | | |
| | | printf("send data to server... ...\n"); |
| | | write(connect_fd, snd_buf, sizeof(snd_buf)); |
| | | printf("send end!\n"); |
| | | |
| | | memset(rcv_buf, 0, sizeof(rcv_buf)); |
| | | rcv_num = read(connect_fd, rcv_buf, sizeof(rcv_buf)); |
| | | if(rcv_num == 0) |
| | | { |
| | | close(connect_fd); |
| | | return 0; |
| | | } |
| | | memcpy(ptr_memfd_data, rcv_buf, rcv_num); |
| | | |
| | | close(connect_fd); |
| | | } |
| | |
| | | #include <stdio.h> |
| | | #include <stdlib.h> |
| | | #include <string.h> |
| | | #include <unistd.h> |
| | | #include <errno.h> |
| | | #include <signal.h> |
| | | #include <sys/types.h> |
| | | #include <sys/stat.h> |
| | | #include <unistd.h> |
| | | #include <stdlib.h> |
| | | #include <stdio.h> |
| | | #include <errno.h> |
| | | |
| | | #include "memfd.h" |
| | | #include "ipc_msg.h" |
| | |
| | | printf("failed to create ipc_server\n"); |
| | | } |
| | | |
| | | basic_shm_close(g_memfd); |
| | | // basic_shm_close(g_memfd); |
| | | return 0; |
| | | } |