/
shm.c
128 lines (102 loc) · 2.66 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2011 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "common-internal.h"
#include "shm.h"
#include "util.h"
#ifdef HAVE_SYS_MMAN_H
# include <sys/mman.h>
#endif
#include <sys/stat.h>
#include <unistd.h>
#define ENTRIES "shm"
ret_t
cherokee_shm_init (cherokee_shm_t *shm)
{
shm->len = 0;
shm->mem = NULL;
cherokee_buffer_init (&shm->name);
return ret_ok;
}
ret_t
cherokee_shm_mrproper (cherokee_shm_t *shm)
{
if (shm->mem) {
munmap (shm->mem, shm->len);
}
cherokee_buffer_mrproper (&shm->name);
return ret_ok;
}
ret_t
cherokee_shm_create (cherokee_shm_t *shm, char *name, size_t len)
{
int re;
int fd;
fd = cherokee_open (name, O_RDWR | O_EXCL | O_CREAT, 0600);
if (fd < 0) {
return ret_error;
}
re = ftruncate (fd, len);
if (re < 0) {
cherokee_fd_close (fd);
return ret_error;
}
shm->mem = mmap (0, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (shm->mem == MAP_FAILED) {
shm->mem = NULL;
cherokee_fd_close (fd);
return ret_error;
}
cherokee_fd_close (fd);
shm->len = len;
cherokee_buffer_add (&shm->name, name, strlen(name));
TRACE (ENTRIES, "SHM (mmap: '%s', len: %d) created\n", shm->name.buf, len);
return ret_ok;
}
ret_t
cherokee_shm_map (cherokee_shm_t *shm,
cherokee_buffer_t *name)
{
int re;
int fd;
struct stat info;
fd = cherokee_open (name->buf, O_RDWR, 0600);
if (fd < 0) {
return ret_error;
}
re = cherokee_fstat (fd, &info);
if (re != 0) {
cherokee_fd_close (fd);
return ret_error;
}
shm->mem = mmap (0, info.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (shm->mem == MAP_FAILED) {
shm->mem = NULL;
cherokee_fd_close (fd);
return ret_error;
}
cherokee_fd_close (fd);
cherokee_buffer_clean (&shm->name);
cherokee_buffer_add_buffer (&shm->name, name);
TRACE (ENTRIES, "SHM (mmap: '%s', size: %d) opened\n", name->buf, info.st_size);
return ret_ok;
}