shm implemented as memfd syscall
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#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;
}