Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 129 lines (102 sloc) 2.722 kB
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
3 /* Cherokee
4 *
5 * Authors:
6 * Alvaro Lopez Ortega <alvaro@alobbs.com>
7 *
920e20b @alobbs Happy new year!
alobbs authored
8 * Copyright (C) 2001-2011 Alvaro Lopez Ortega
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 * 02110-1301, USA.
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
23 */
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
24
25 #include "common-internal.h"
26 #include "shm.h"
27 #include "util.h"
28
29 #ifdef HAVE_SYS_MMAN_H
30 # include <sys/mman.h>
31 #endif
32
33 #include <sys/stat.h>
34 #include <unistd.h>
35
13d2ab2 @alobbs Re-implements a few bits of the spawning mechanism.
alobbs authored
36 #define ENTRIES "shm"
37
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
38 ret_t
39 cherokee_shm_init (cherokee_shm_t *shm)
40 {
41 shm->len = 0;
42 shm->mem = NULL;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
43
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
44 cherokee_buffer_init (&shm->name);
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
45
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
46 return ret_ok;
47 }
48
49
50 ret_t
51 cherokee_shm_mrproper (cherokee_shm_t *shm)
52 {
53 if (shm->mem) {
54 munmap (shm->mem, shm->len);
55 }
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
56
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
57 cherokee_buffer_mrproper (&shm->name);
58 return ret_ok;
59 }
60
61
62 ret_t
63 cherokee_shm_create (cherokee_shm_t *shm, char *name, size_t len)
64 {
65 int re;
66 int fd;
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
67
a68f41b @alobbs Merges the Front-Line Cache branch.
alobbs authored
68 fd = cherokee_open (name, O_RDWR | O_EXCL | O_CREAT, 0600);
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
69 if (fd < 0) {
70 return ret_error;
71 }
72
73 re = ftruncate (fd, len);
74 if (re < 0) {
9a6e161 @alobbs Safer fd closing
alobbs authored
75 cherokee_fd_close (fd);
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
76 return ret_error;
77 }
72f6444 @alobbs Clean up: Trims trailing spaces.
alobbs authored
78
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
79 shm->mem = mmap (0, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
80 if (shm->mem == MAP_FAILED) {
81 shm->mem = NULL;
9a6e161 @alobbs Safer fd closing
alobbs authored
82 cherokee_fd_close (fd);
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
83 return ret_error;
84 }
85
9a6e161 @alobbs Safer fd closing
alobbs authored
86 cherokee_fd_close (fd);
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
87
88 shm->len = len;
89 cherokee_buffer_add (&shm->name, name, strlen(name));
90
13d2ab2 @alobbs Re-implements a few bits of the spawning mechanism.
alobbs authored
91 TRACE (ENTRIES, "SHM (mmap: '%s', len: %d) created\n", shm->name.buf, len);
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
92 return ret_ok;
93 }
94
95 ret_t
96 cherokee_shm_map (cherokee_shm_t *shm,
97 cherokee_buffer_t *name)
98 {
99 int re;
100 int fd;
101 struct stat info;
102
a68f41b @alobbs Merges the Front-Line Cache branch.
alobbs authored
103 fd = cherokee_open (name->buf, O_RDWR, 0600);
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
104 if (fd < 0) {
105 return ret_error;
106 }
107
147fcf8 @alobbs {l,f,}stat() related fix. Ensure the EINTR signal is handler
alobbs authored
108 re = cherokee_fstat (fd, &info);
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
109 if (re != 0) {
45e061d @alobbs "Resource acquired to 'fd' at line 103 may be lost here."
alobbs authored
110 cherokee_fd_close (fd);
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
111 return ret_error;
112 }
113
114 shm->mem = mmap (0, info.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
115 if (shm->mem == MAP_FAILED) {
116 shm->mem = NULL;
45e061d @alobbs "Resource acquired to 'fd' at line 103 may be lost here."
alobbs authored
117 cherokee_fd_close (fd);
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
118 return ret_error;
119 }
120
9a6e161 @alobbs Safer fd closing
alobbs authored
121 cherokee_fd_close (fd);
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
122
123 cherokee_buffer_clean (&shm->name);
124 cherokee_buffer_add_buffer (&shm->name, name);
125
13d2ab2 @alobbs Re-implements a few bits of the spawning mechanism.
alobbs authored
126 TRACE (ENTRIES, "SHM (mmap: '%s', size: %d) opened\n", name->buf, info.st_size);
468a464 @alobbs git-svn-id: svn://cherokee-project.com/cherokee/trunk@3168 5dc97367-9…
alobbs authored
127 return ret_ok;
128 }
Something went wrong with that request. Please try again.