-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.c
44 lines (38 loc) · 1.06 KB
/
context.c
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
#include <fcntl.h>
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdbool.h>
#include "spawn.h"
#include "context.h"
#define PACK_SIZE 5*1024*1024
ipc_buffer
get_shared_mem(char* path)
{
int fd = shm_open(path,O_RDWR,0);
void* mem = mmap(NULL,
PACK_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED,
fd,
0);
close(fd);
return (ipc_buffer) {.header=(ipc_header*)mem,.buffer=mem+sizeof(ipc_header)};
}
ipc_context
create_context(ipc_pipe microeng,char* name, int prefix)
{
char buff[128];
char write_name[50];
char read_name[50];
snprintf(write_name,sizeof(write_name),"/%smicroengwrites",name);
snprintf(read_name,sizeof(read_name),"/%smicroengreads",name);
snprintf(buff,sizeof(buff),"strt_ctx\nname_ctx %s\npfix_ctx %d\nlnch_ctx\n",name,prefix);
write(microeng.write,buff,sizeof(buff));
read(microeng.read,buff,sizeof("done\n"));
ipc_context out = {.read=get_shared_mem(read_name),.write=get_shared_mem(write_name),.name=malloc(strlen(name)),.prefix=prefix};
strcpy(out.name,name);
return out;
}