#include <signal.h>
|
#include <sys/types.h>
|
#include <sys/stat.h>
|
#include <unistd.h>
|
#include <stdlib.h>
|
#include <stdio.h>
|
#include <errno.h>
|
#include <string.h>
|
#include <pthread.h>
|
#include "memfd.h"
|
#include "ipc_msg.h"
|
|
|
//typedef int (*readfunc)(struct user_data*);
|
|
void handler(){
|
|
printf("clean program start\n");
|
//unlink(UNIX_DOMAIN);
|
remove(UNIX_DOMAIN);
|
printf("clean end.\n");
|
}
|
|
int shm_write(int fd)
|
{
|
struct stat st;
|
ssize_t len;
|
int ret = 0;
|
unsigned char* paddr;
|
|
if (fstat (fd, &st))
|
errExit ("fstat");
|
len = st.st_size;
|
|
char *content = "hello world!\n";
|
ret = basic_shm_mmap (fd, &paddr);
|
if (ret < 0)
|
errExit("basic_shm_mmap");
|
|
memcpy(paddr, content, strlen(content));
|
|
printf("length: %zu, atime: %lu.%lu\n", len, st.st_atim.tv_sec, st.st_atim.tv_nsec);
|
return len;
|
}
|
|
|
|
int proc_msg(struct user_data* rdata, void * arg, int len)
|
{
|
int fd = rdata->fd;
|
char line[MAX_LEN]= {0};
|
int n = 0;
|
int nwrite = 0;
|
int data_size = 0;
|
memfd_data_st memfd_data = {0};
|
|
if(rdata->n_size <= 0 || fd < 0)
|
{
|
return -1;
|
}
|
|
|
if(0 == strncmp(rdata->line, GET_MEMFD_MSG, strlen(GET_MEMFD_MSG)))
|
{
|
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);
|
memcpy(line, &memfd_data, data_size);
|
n = data_size;
|
}
|
else if(0 == strncmp(rdata->line, GET_DATA_MSG, strlen(GET_DATA_MSG)))
|
{
|
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;
|
}
|
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( nwrite < n )
|
{
|
if((nwrite==-1) && (errno != EAGAIN))
|
{
|
perror("write error");
|
}
|
if (errno == ECONNRESET)
|
{
|
close(rdata->fd);
|
mydebug("[SERVER] Error: send responce failed: %s\n", strerror(errno));
|
}
|
else if (nwrite == 0)
|
{
|
close(rdata->fd);
|
mydebug("[SERVER] Error: client closed connection.");
|
}
|
break;
|
}
|
n -= nwrite;
|
}
|
|
return nwrite;
|
|
}
|
|
/*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 * 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]);
|
exit(EXIT_FAILURE);
|
}
|
|
name = argv[1];
|
len = atoi(argv[2]) * 1024LU * 1024LU * 1024LU;
|
|
signal(SIGTERM,handler);
|
signal(SIGABRT,handler);
|
signal(SIGSEGV,handler);
|
|
|
#if 1
|
g_memfd = basic_shm_create(name, len);
|
ret = shm_write(g_memfd);
|
|
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;
|
}
|