-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshm.c
107 lines (88 loc) · 2.44 KB
/
shm.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
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
/******************************************************************************
* Copyright (C) Dean Zou 2016-2017
* file: shm.c
* author: Dean Zou <[email protected]>
* created: 2017-07-31 16:29:41
* updated: 2017-07-31 16:29:41
*****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/errno.h>
#include <sys/mman.h>
#include <fcntl.h>
#include "shm.h"
#include "log.h"
struct shm_ctx *shm_init(const char *name, long size,int unique)
{
int fd;
int ret = -1;
struct shm_ctx *shm_ctx = NULL;
void *addr = NULL;
if(unique == SHM_ID_UNIQUE){
fd = shm_open(name, O_CREAT|O_EXCL|O_RDWR, 00777);
if(fd == -1){
log_error("shm_init->shm_open %s failed. %s unique = %d\n",name,strerror(errno),unique);
goto error2;
}
}else{
fd = shm_open(name, O_CREAT|O_RDWR, 00777);
if(fd == -1){
log_error("shm_init->shm_open %s failed. %s unique = %d\n",name,strerror(errno),unique);
goto error2;
}
}
ret = ftruncate(fd, size);
if(ret == -1){
log_error("shm_init->ftruncate failed. %s\n",strerror(errno));
goto error1;
}
addr = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, SEEK_SET);
if(addr == NULL){
log_error("shm_init->mmap failed. %s\n",strerror(errno));
goto error1;
}
shm_ctx = (struct shm_ctx *)calloc(1,sizeof(struct shm_ctx));
if(shm_ctx == NULL)
goto error0;
shm_ctx->addr = addr;
shm_ctx->fd = fd;
shm_ctx->size = size;
strcpy(shm_ctx->name,name);
return shm_ctx;
error0:
munmap(shm_ctx->addr, shm_ctx->size);
error1:
close(fd);
shm_unlink(name);
error2:
return shm_ctx;
}
int shm_close(struct shm_ctx *shm_ctx)
{
int ret = -1;
ret = munmap(shm_ctx->addr, shm_ctx->size);
close(shm_ctx->fd);
free(shm_ctx);
return ret;
}
int shm_deinit(struct shm_ctx *shm_ctx)
{
int ret = -1;
ret = munmap(shm_ctx->addr, shm_ctx->size);
close(shm_ctx->fd);
ret = shm_unlink(shm_ctx->name);
free(shm_ctx);
return ret;
}
int shm_write(struct shm_ctx *shm_ctx, const void *buf, long len,long pos)
{
memcpy(shm_ctx->addr + pos,buf, len);
return len;
}
int shm_read(struct shm_ctx *shm_ctx, void *buf, long len,long pos)
{
memcpy(buf,shm_ctx->addr + pos, len);
return len;
}