| | |
| | | #include <stdlib.h> |
| | | #include <stdio.h> |
| | | #include <errno.h> |
| | | |
| | | #include <string.h> |
| | | #include <pthread.h> |
| | | #include "memfd.h" |
| | | #include "ipc_msg.h" |
| | | |
| | | int g_memfd = 0; |
| | | |
| | | //typedef int (*readfunc)(struct user_data*); |
| | | |
| | | void handler(){ |
| | | |
| | |
| | | |
| | | |
| | | |
| | | int proc_memfd(struct user_data* rdata) |
| | | int proc_msg(struct user_data* rdata, void * arg, int len) |
| | | { |
| | | int fd = rdata->fd; |
| | | unsigned int n_size; |
| | | char line[MAX_LEN]= {0}; |
| | | int n = 0; |
| | | int nwrite = 0; |
| | |
| | | return -1; |
| | | } |
| | | |
| | | if(0 == strncmp(rdata->line, HELLO_MSG, strlen(HELLO_MSG))) |
| | | |
| | | if(0 == strncmp(rdata->line, GET_MEMFD_MSG, strlen(GET_MEMFD_MSG))) |
| | | { |
| | | memfd_data.memfd = g_memfd; |
| | | st_custom_data * pst_data = (st_custom_data *)(arg); |
| | | memfd_data.pid = getpid(); |
| | | memfd_data.memfd = pst_data->memfd; |
| | | memfd_data.data = NULL; |
| | | memfd_data.len = 0; |
| | | data_size = sizeof(memfd_data_st) + memfd_data.len; |
| | | |
| | | data_size = sizeof(memfd_data_st); |
| | | memcpy(line, &memfd_data, data_size); |
| | | n = data_size; |
| | | } |
| | | else |
| | | else if(0 == strncmp(rdata->line, GET_DATA_MSG, strlen(GET_DATA_MSG))) |
| | | { |
| | | return -1; |
| | | if((NULL == arg) || (0 == len)) |
| | | { |
| | | return -1; |
| | | } |
| | | |
| | | st_custom_data * pst_data = (st_custom_data *)(arg); |
| | | memfd_data.pid = 0; |
| | | memfd_data.memfd = 0; |
| | | memfd_data.data = pst_data->data; |
| | | memfd_data.len = pst_data->len; |
| | | data_size = pst_data->len; |
| | | /*copy the arg to line,so it can send to client*/ |
| | | memcpy(line, pst_data->data, data_size); |
| | | n = data_size; |
| | | } |
| | | |
| | | n = data_size; |
| | | else if(0 == strncmp(rdata->line, GET_CUSTOM_MSG, strlen(GET_CUSTOM_MSG))) |
| | | { |
| | | if((NULL == arg) || (0 == len)) |
| | | { |
| | | return -1; |
| | | } |
| | | st_custom_data * pst_data = (st_custom_data *)(arg); |
| | | memfd_data.pid = getpid(); |
| | | memfd_data.memfd = pst_data->memfd; |
| | | memfd_data.data = pst_data->data; |
| | | memfd_data.len = pst_data->len; |
| | | data_size = sizeof(memfd_data_st); |
| | | memcpy(line, &memfd_data, data_size); |
| | | |
| | | memcpy(line+data_size, memfd_data.data, memfd_data.len); |
| | | data_size += memfd_data.len; |
| | | n = data_size; |
| | | } |
| | | |
| | | mydebug("[SERVER] writetask %u sending %d\n", (uint32_t)pthread_self(), rdata->fd); |
| | | while(n>0) |
| | | { |
| | | nwrite = write(rdata->fd, line+data_size-n,n);////ET |
| | |
| | | if (errno == ECONNRESET) |
| | | { |
| | | close(rdata->fd); |
| | | printf("[SERVER] Error: send responce failed: %s\n", strerror(errno)); |
| | | mydebug("[SERVER] Error: send responce failed: %s\n", strerror(errno)); |
| | | } |
| | | else if (nwrite == 0) |
| | | { |
| | | close(rdata->fd); |
| | | printf("[SERVER] Error: client closed connection."); |
| | | mydebug("[SERVER] Error: client closed connection."); |
| | | } |
| | | break; |
| | | } |
| | |
| | | |
| | | } |
| | | |
| | | /*define you our proc_memfd funtion to send your message to other processes,or just put it in shm, |
| | | /*define you our proc_msg funtion to send your message to other processes, |
| | | notice the file description fd and the pid of creator to other*/ |
| | | int main(int argc, char **argv) |
| | | { |
| | | char *name; |
| | | ssize_t len; |
| | | char * unixdomain = NULL; |
| | | char * unix_domain_path = NULL; |
| | | int ret = 0; |
| | | args_data_st args_data_st={0}; |
| | | int g_memfd = 0; |
| | | |
| | | if (argc < 3) { |
| | | fprintf(stderr, "%s name size [unix domain path]\n", argv[0]); |
| | |
| | | |
| | | name = argv[1]; |
| | | len = atoi(argv[2]) * 1024LU * 1024LU * 1024LU; |
| | | unixdomain = argv[3]; |
| | | |
| | | signal(SIGTERM,handler); |
| | | signal(SIGABRT,handler); |
| | | signal(SIGSEGV,handler); |
| | | |
| | | |
| | | #if 1 |
| | | g_memfd = basic_shm_create(name, len); |
| | | ret = shm_write(g_memfd); |
| | | |
| | | ret = basic_create_ipc_server(unixdomain); |
| | | char data[256] = "test custom usedata\n"; |
| | | |
| | | st_custom_data st_data; |
| | | st_data.memfd = g_memfd; |
| | | st_data.data = data; |
| | | st_data.len = strlen(data)+1; |
| | | args_data_st.wfunc = proc_msg; |
| | | args_data_st.args = &st_data; |
| | | args_data_st.len = sizeof(st_data)+st_data.len; |
| | | |
| | | unix_domain_path=UNIX_DOMAIN; |
| | | ret = basic_create_ipc_server(unix_domain_path,&args_data_st); |
| | | if(ret < 0) |
| | | { |
| | | printf("failed to create ipc_server %s\n", unix_domain_path); |
| | | } |
| | | |
| | | #endif |
| | | |
| | | #if 0 |
| | | char send_buf[256] = "test custom usedata\n"; |
| | | args_data_st.wfunc = proc_msg; |
| | | args_data_st.args = &send_buf; |
| | | args_data_st.len = strlen(send_buf); |
| | | |
| | | unix_domain_path="/tmp/unix_domain_usedata"; |
| | | ret = basic_create_ipc_server(unix_domain_path,&args_data_st); |
| | | if(ret < 0) |
| | | { |
| | | printf("failed to create ipc_server\n"); |
| | | } |
| | | #endif |
| | | |
| | | // basic_shm_close(g_memfd); |
| | | return 0; |