]>
git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/unix/sysv/linux/shm_open.c
1 /* Copyright (C) 2000-2004,2006,2007 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
27 #include <sys/statfs.h>
28 #include <bits/libc-lock.h>
29 #include "linux_fsinfo.h"
31 #include <kernel-features.h>
34 /* Mount point of the shared memory filesystem. */
41 /* This is the default directory. */
42 static const char defaultdir
[] = "/dev/shm/";
44 /* Protect the `mountpoint' variable above. */
45 __libc_once_define (static, once
);
48 #if defined O_CLOEXEC && !defined __ASSUME_O_CLOEXEC
49 static bool have_o_cloexec
;
53 /* Determine where the shmfs is mounted (if at all). */
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
)
67 /* It is in the normal place. */
68 mountpoint
.dir
= (char *) defaultdir
;
69 mountpoint
.dirlen
= sizeof (defaultdir
) - 1;
74 /* OK, do it the hard way. Look through the /proc/mounts file and if
75 this does not exist through /etc/fstab to find the mount point. */
76 fp
= __setmntent ("/proc/mounts", "r");
77 if (__builtin_expect (fp
== NULL
, 0))
79 fp
= __setmntent (_PATH_MNTTAB
, "r");
80 if (__builtin_expect (fp
== NULL
, 0))
81 /* There is nothing we can do. Blind guesses are not helpful. */
85 /* Now read the entries. */
86 while ((mp
= __getmntent_r (fp
, &resmem
, buf
, sizeof buf
)) != NULL
)
87 /* The original name is "shm" but this got changed in early Linux
89 if (strcmp (mp
->mnt_type
, "tmpfs") == 0
90 #ifndef __ASSUME_TMPFS_NAME
91 || strcmp (mp
->mnt_type
, "shm") == 0
95 /* Found it. There might be more than one place where the
96 filesystem is mounted but one is enough for us. */
99 /* First make sure this really is the correct entry. At least
100 some versions of the kernel give wrong information because
101 of the implicit mount of the shmfs for SysV IPC. */
102 if (__statfs (mp
->mnt_dir
, &f
) != 0 || f
.f_type
!= SHMFS_SUPER_MAGIC
)
105 namelen
= strlen (mp
->mnt_dir
);
108 /* Hum, maybe some crippled entry. Keep on searching. */
111 mountpoint
.dir
= (char *) malloc (namelen
+ 2);
112 if (mountpoint
.dir
!= NULL
)
114 char *cp
= __mempcpy (mountpoint
.dir
, mp
->mnt_dir
, namelen
);
118 mountpoint
.dirlen
= cp
- mountpoint
.dir
;
124 /* Close the stream. */
129 /* Open shared memory object. This implementation assumes the shmfs
130 implementation introduced in the late 2.3.x kernel series to be
131 available. Normally the filesystem will be mounted at /dev/shm but
132 we fall back on searching for the actual mount point should opening
135 shm_open (const char *name
, int oflag
, mode_t mode
)
141 /* Determine where the shmfs is mounted. */
142 __libc_once (once
, where_is_shmfs
);
144 /* If we don't know the mount points there is nothing we can do. Ever. */
145 if (mountpoint
.dir
== NULL
)
147 __set_errno (ENOSYS
);
151 /* Construct the filename. */
152 while (name
[0] == '/')
157 /* The name "/" is not supported. */
158 __set_errno (EINVAL
);
162 namelen
= strlen (name
);
163 fname
= (char *) alloca (mountpoint
.dirlen
+ namelen
+ 1);
164 __mempcpy (__mempcpy (fname
, mountpoint
.dir
, mountpoint
.dirlen
),
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
);
179 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
181 if (have_o_cloexec
<= 0)
184 /* We got a descriptor. Now set the FD_CLOEXEC bit. */
185 int flags
= fcntl (fd
, F_GETFD
, 0);
187 if (__builtin_expect (flags
, 0) >= 0)
190 if (have_o_cloexec
== 0)
191 have_o_cloexec
= (flags
& FD_CLOEXEC
) == 0 ? -1 : 1;
192 if (have_o_cloexec
< 0)
196 flags
= fcntl (fd
, F_SETFD
, flags
);
202 /* Something went wrong. We cannot return the descriptor. */
203 int save_errno
= errno
;
206 __set_errno (save_errno
);
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
);
221 /* Unlink a shared memory object. */
223 shm_unlink (const char *name
)
228 /* Determine where the shmfs is mounted. */
229 __libc_once (once
, where_is_shmfs
);
231 if (mountpoint
.dir
== NULL
)
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
);
240 /* Construct the filename. */
241 while (name
[0] == '/')
246 /* The name "/" is not supported. */
247 __set_errno (ENOENT
);
251 namelen
= strlen (name
);
252 fname
= (char *) alloca (mountpoint
.dirlen
+ namelen
+ 1);
253 __mempcpy (__mempcpy (fname
, mountpoint
.dir
, mountpoint
.dirlen
),
256 /* And remove the file. */
257 int ret
= unlink (fname
);
258 if (ret
< 0 && errno
== EPERM
)
259 __set_errno (EACCES
);
264 /* Make sure the table is freed if we want to free everything before
266 libc_freeres_fn (freeit
)
268 if (mountpoint
.dir
!= defaultdir
)
269 free (mountpoint
.dir
);