]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/shm_open.c
fef8fd531accd4f473e87306c9ed8da5ee1bacb9
[thirdparty/glibc.git] / sysdeps / unix / sysv / linux / shm_open.c
1 /* Copyright (C) 2000-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <http://www.gnu.org/licenses/>. */
17
18 #include <errno.h>
19 #include <fcntl.h>
20 #include <mntent.h>
21 #include <paths.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/mman.h>
27 #include <sys/statfs.h>
28 #include <bits/libc-lock.h>
29 #include "linux_fsinfo.h"
30
31 #include <kernel-features.h>
32
33
34 /* Mount point of the shared memory filesystem. */
35 static struct
36 {
37 char *dir;
38 size_t dirlen;
39 } mountpoint;
40
41 /* This is the default directory. */
42 static const char defaultdir[] = "/dev/shm/";
43
44 /* Protect the `mountpoint' variable above. */
45 __libc_once_define (static, once);
46
47
48 #if defined O_CLOEXEC && !defined __ASSUME_O_CLOEXEC
49 static bool have_o_cloexec;
50 #endif
51
52
53 /* Determine where the shmfs is mounted (if at all). */
54 static void
55 where_is_shmfs (void)
56 {
57 char buf[512];
58 struct statfs f;
59 struct mntent resmem;
60 struct mntent *mp;
61 FILE *fp;
62
63 /* The canonical place is /dev/shm. This is at least what the
64 documentation tells everybody to do. */
65 if (__statfs (defaultdir, &f) == 0 && (f.f_type == SHMFS_SUPER_MAGIC
66 || f.f_type == RAMFS_MAGIC))
67 {
68 /* It is in the normal place. */
69 mountpoint.dir = (char *) defaultdir;
70 mountpoint.dirlen = sizeof (defaultdir) - 1;
71
72 return;
73 }
74
75 /* OK, do it the hard way. Look through the /proc/mounts file and if
76 this does not exist through /etc/fstab to find the mount point. */
77 fp = __setmntent ("/proc/mounts", "r");
78 if (__builtin_expect (fp == NULL, 0))
79 {
80 fp = __setmntent (_PATH_MNTTAB, "r");
81 if (__builtin_expect (fp == NULL, 0))
82 /* There is nothing we can do. Blind guesses are not helpful. */
83 return;
84 }
85
86 /* Now read the entries. */
87 while ((mp = __getmntent_r (fp, &resmem, buf, sizeof buf)) != NULL)
88 /* The original name is "shm" but this got changed in early Linux
89 2.4.x to "tmpfs". */
90 if (strcmp (mp->mnt_type, "tmpfs") == 0
91 || strcmp (mp->mnt_type, "shm") == 0)
92 {
93 /* Found it. There might be more than one place where the
94 filesystem is mounted but one is enough for us. */
95 size_t namelen;
96
97 /* First make sure this really is the correct entry. At least
98 some versions of the kernel give wrong information because
99 of the implicit mount of the shmfs for SysV IPC. */
100 if (__statfs (mp->mnt_dir, &f) != 0 || (f.f_type != SHMFS_SUPER_MAGIC
101 && f.f_type != RAMFS_MAGIC))
102 continue;
103
104 namelen = strlen (mp->mnt_dir);
105
106 if (namelen == 0)
107 /* Hum, maybe some crippled entry. Keep on searching. */
108 continue;
109
110 mountpoint.dir = (char *) malloc (namelen + 2);
111 if (mountpoint.dir != NULL)
112 {
113 char *cp = __mempcpy (mountpoint.dir, mp->mnt_dir, namelen);
114 if (cp[-1] != '/')
115 *cp++ = '/';
116 *cp = '\0';
117 mountpoint.dirlen = cp - mountpoint.dir;
118 }
119
120 break;
121 }
122
123 /* Close the stream. */
124 __endmntent (fp);
125 }
126
127
128 /* Open shared memory object. This implementation assumes the shmfs
129 implementation introduced in the late 2.3.x kernel series to be
130 available. Normally the filesystem will be mounted at /dev/shm but
131 we fall back on searching for the actual mount point should opening
132 such a file fail. */
133 int
134 shm_open (const char *name, int oflag, mode_t mode)
135 {
136 size_t namelen;
137 char *fname;
138 int fd;
139
140 /* Determine where the shmfs is mounted. */
141 __libc_once (once, where_is_shmfs);
142
143 /* If we don't know the mount points there is nothing we can do. Ever. */
144 if (mountpoint.dir == NULL)
145 {
146 __set_errno (ENOSYS);
147 return -1;
148 }
149
150 /* Construct the filename. */
151 while (name[0] == '/')
152 ++name;
153
154 namelen = strlen (name);
155
156 /* Validate the filename. */
157 if (name[0] == '\0' || namelen > NAME_MAX || strchr (name, '/') != NULL)
158 {
159 __set_errno (EINVAL);
160 return -1;
161 }
162
163 fname = (char *) alloca (mountpoint.dirlen + namelen + 1);
164 __mempcpy (__mempcpy (fname, mountpoint.dir, mountpoint.dirlen),
165 name, namelen + 1);
166
167 #ifdef O_CLOEXEC
168 oflag |= O_CLOEXEC;
169 #endif
170
171 /* And get the file descriptor.
172 XXX Maybe we should test each descriptor whether it really is for a
173 file on the shmfs. If this is what should be done the whole function
174 should be revamped since we can determine whether shmfs is available
175 while trying to open the file, all in one turn. */
176 fd = open (fname, oflag | O_NOFOLLOW, mode);
177 if (fd != -1)
178 {
179 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
180 # ifdef O_CLOEXEC
181 if (have_o_cloexec <= 0)
182 # endif
183 {
184 /* We got a descriptor. Now set the FD_CLOEXEC bit. */
185 int flags = fcntl (fd, F_GETFD, 0);
186
187 if (__builtin_expect (flags, 0) >= 0)
188 {
189 # ifdef O_CLOEXEC
190 if (have_o_cloexec == 0)
191 have_o_cloexec = (flags & FD_CLOEXEC) == 0 ? -1 : 1;
192 if (have_o_cloexec < 0)
193 # endif
194 {
195 flags |= FD_CLOEXEC;
196 flags = fcntl (fd, F_SETFD, flags);
197 }
198 }
199
200 if (flags == -1)
201 {
202 /* Something went wrong. We cannot return the descriptor. */
203 int save_errno = errno;
204 close (fd);
205 fd = -1;
206 __set_errno (save_errno);
207 }
208 }
209 #endif
210 }
211 else if (__builtin_expect (errno == EISDIR, 0))
212 /* It might be better to fold this error with EINVAL since
213 directory names are just another example for unsuitable shared
214 object names and the standard does not mention EISDIR. */
215 __set_errno (EINVAL);
216
217 return fd;
218 }
219
220
221 /* Unlink a shared memory object. */
222 int
223 shm_unlink (const char *name)
224 {
225 size_t namelen;
226 char *fname;
227
228 /* Determine where the shmfs is mounted. */
229 __libc_once (once, where_is_shmfs);
230
231 if (mountpoint.dir == NULL)
232 {
233 /* We cannot find the shmfs. If `name' is really a shared
234 memory object it must have been created by another process
235 and we have no idea where that process found the mountpoint. */
236 __set_errno (ENOENT);
237 return -1;
238 }
239
240 /* Construct the filename. */
241 while (name[0] == '/')
242 ++name;
243
244 namelen = strlen (name);
245
246 /* Validate the filename. */
247 if (name[0] == '\0' || namelen > NAME_MAX || strchr (name, '/') != NULL)
248 {
249 __set_errno (ENOENT);
250 return -1;
251 }
252
253 fname = (char *) alloca (mountpoint.dirlen + namelen + 1);
254 __mempcpy (__mempcpy (fname, mountpoint.dir, mountpoint.dirlen),
255 name, namelen + 1);
256
257 /* And remove the file. */
258 int ret = unlink (fname);
259 if (ret < 0 && errno == EPERM)
260 __set_errno (EACCES);
261 return ret;
262 }
263
264
265 /* Make sure the table is freed if we want to free everything before
266 exiting. */
267 libc_freeres_fn (freeit)
268 {
269 if (mountpoint.dir != defaultdir)
270 free (mountpoint.dir);
271 }