xf.li | 6c8fc1e | 2023-08-12 00:11:09 -0700 | [diff] [blame] | 1 | #include <errno.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | #include <unistd.h> |
| 6 | #include <signal.h> |
| 7 | #include <time.h> |
| 8 | #include <pthread.h> |
| 9 | #include <sys/prctl.h> |
| 10 | #include <sys/ipc.h> |
| 11 | #include <sys/shm.h> |
| 12 | #include <sys/types.h> |
| 13 | #include <unistd.h> |
| 14 | #include <assert.h> |
| 15 | #include <sys/mman.h> |
| 16 | #include <sys/stat.h> |
| 17 | #include <fcntl.h> |
| 18 | |
| 19 | /** |
| 20 | * ºê¶¨Òå |
| 21 | */ |
| 22 | #define SHM_NAME_SIZE (128) |
| 23 | #define SHM_TEXT_SIZE (8192) |
| 24 | #define SHM_PAGE_SIZE (4096) |
| 25 | #define SHM_ALLOC_ONE_PAGE_SIZE (1024) |
| 26 | #define SHM_ALLOC_TWO_PAGE_SIZE (8100) |
| 27 | #define SHM_ALLOC_FOUR_PAGE_SIZE (16000) |
| 28 | #define SHM_NAME_CROSS_PREFIX ("remote-") |
| 29 | |
| 30 | /** |
| 31 | * Êý¾ÝÀàÐͶ¨Òå |
| 32 | */ |
| 33 | typedef int bool; |
| 34 | enum |
| 35 | { |
| 36 | FALSE, |
| 37 | TRUE |
| 38 | }; |
| 39 | |
| 40 | typedef struct |
| 41 | { |
| 42 | unsigned int shm_size; |
| 43 | char name[SHM_NAME_SIZE]; |
| 44 | }shm_node_t; |
| 45 | |
| 46 | typedef struct |
| 47 | { |
| 48 | int write; |
| 49 | char data[SHM_TEXT_SIZE]; |
| 50 | }shm_use_remote; |
| 51 | |
| 52 | shm_node_t shm_node; |
| 53 | |
| 54 | bool shm_is_remote(const char *shm_name) |
| 55 | { |
| 56 | if (!shm_name) |
| 57 | return FALSE; |
| 58 | if (strncmp(shm_name, SHM_NAME_CROSS_PREFIX, strlen(SHM_NAME_CROSS_PREFIX)) == 0) |
| 59 | return TRUE; |
| 60 | else |
| 61 | return FALSE; |
| 62 | } |
| 63 | |
| 64 | int main (int argc, char *argv[]) |
| 65 | { |
| 66 | int opt; |
| 67 | int shmfd = 0; |
| 68 | int ret = 0; |
| 69 | unsigned int shm_size = 0; |
| 70 | char *name = NULL; |
| 71 | char *pshm = NULL; |
| 72 | char *read_data = NULL; |
| 73 | extern char *optarg; |
| 74 | |
| 75 | shm_use_remote *shared; |
| 76 | |
| 77 | while ((opt = getopt(argc,argv,"n:d:s::")) != EOF) |
| 78 | { |
| 79 | switch (opt) |
| 80 | { |
| 81 | case 'n': |
| 82 | name = optarg; |
| 83 | break; |
| 84 | case 'd': |
| 85 | read_data = optarg; |
| 86 | break; |
| 87 | case 's': |
| 88 | shm_size = atoi(optarg); |
| 89 | break; |
| 90 | default: |
| 91 | break; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | if (!name || !read_data) |
| 96 | { |
| 97 | printf("param error: you can input \"shm_posix_read -n \"remote-shm_test\" -d \"123456\" -s1024 \n"); |
| 98 | printf("or \"shm_posix_read -n \"remote-shm_test\" -d \"123456\" \n"); |
| 99 | printf("-n \"remote-shm_test\": create a shm using name remote-shm_test\n"); |
| 100 | printf("-d \"123456\": if we write 123456 to shm, then can read 123456 form shm, the test case is pass! \n"); |
| 101 | printf("-s1024: set the size of shm, then the default size is 1024 \n"); |
| 102 | return -1; |
| 103 | } |
| 104 | |
| 105 | if (shm_size == 0) |
| 106 | shm_size = SHM_ALLOC_ONE_PAGE_SIZE; |
| 107 | |
| 108 | printf("AP read test: name is %s, shm_size is %d\n", name, shm_size); |
| 109 | |
| 110 | shmfd = shm_open(name, O_CREAT | O_RDWR, 0666); |
| 111 | if(shmfd < 0) |
| 112 | { |
| 113 | perror("shm_open fail\n"); |
| 114 | exit(1); |
| 115 | } |
| 116 | printf("shm_open return ok\n"); |
| 117 | |
| 118 | if(ftruncate(shmfd, shm_size) == -1) |
| 119 | { |
| 120 | perror("ftruncate fail\n"); |
| 121 | exit(1); |
| 122 | } |
| 123 | |
| 124 | pshm = (char *)mmap(NULL, shm_size, PROT_READ|PROT_WRITE, MAP_SHARED, shmfd, 0); |
| 125 | if (pshm == MAP_FAILED) |
| 126 | { |
| 127 | perror("mmap fail\n"); |
| 128 | exit(1); |
| 129 | } |
| 130 | printf("mmap return ok\n"); |
| 131 | shm_node.shm_size = shm_size; |
| 132 | strncpy(shm_node.name, name, SHM_NAME_SIZE-1); |
| 133 | |
| 134 | shared = (shm_use_remote *)pshm; |
| 135 | shared->write = 1; |
| 136 | printf("read_data %s ok\n", read_data); |
| 137 | |
| 138 | while(1) |
| 139 | { |
| 140 | if(strncmp(shared->data, read_data, sizeof(read_data)) == 0) |
| 141 | { |
| 142 | printf("Success, AP read %s from shm\n", read_data); |
| 143 | break; |
| 144 | } |
| 145 | else |
| 146 | sleep(2); |
| 147 | } |
| 148 | |
| 149 | ret = munmap(pshm, shm_size); |
| 150 | if(ret < 0) |
| 151 | { |
| 152 | printf("munmap return errno %d: %s\n", errno, strerror(errno)); |
| 153 | exit(1); |
| 154 | } |
| 155 | |
| 156 | if(shm_is_remote(name)) |
| 157 | { |
| 158 | ret = shm_unlink(name); |
| 159 | if(ret < 0) |
| 160 | { |
| 161 | printf("shm_unlink errno %d: %s\n", errno, strerror(errno)); |
| 162 | exit(1); |
| 163 | } |
| 164 | } |
| 165 | close(shmfd); |
| 166 | |
| 167 | printf("shm posix read end\n"); |
| 168 | return 0; |
| 169 | } |
| 170 | |