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, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
28 #include <sys/statfs.h>
29 #include <bits/libc-lock.h>
30 #include "linux_fsinfo.h"
32 #include <kernel-features.h>
35 /* Mount point of the shared memory filesystem. */
42 /* This is the default directory. */
43 static const char defaultdir
[] = "/dev/shm/";
45 /* Protect the `mountpoint' variable above. */
46 __libc_once_define (static, once
);
49 #if defined O_CLOEXEC && !defined __ASSUME_O_CLOEXEC
50 static bool have_o_cloexec
;
54 /* Determine where the shmfs is mounted (if at all). */
64 /* The canonical place is /dev/shm. This is at least what the
65 documentation tells everybody to do. */
66 if (__statfs (defaultdir
, &f
) == 0 && f
.f_type
== SHMFS_SUPER_MAGIC
)
68 /* It is in the normal place. */
69 mountpoint
.dir
= (char *) defaultdir
;
70 mountpoint
.dirlen
= sizeof (defaultdir
) - 1;
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))
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. */
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
90 if (strcmp (mp
->mnt_type
, "tmpfs") == 0
91 #ifndef __ASSUME_TMPFS_NAME
92 || strcmp (mp
->mnt_type
, "shm") == 0
96 /* Found it. There might be more than one place where the
97 filesystem is mounted but one is enough for us. */
100 /* First make sure this really is the correct entry. At least
101 some versions of the kernel give wrong information because
102 of the implicit mount of the shmfs for SysV IPC. */
103 if (__statfs (mp
->mnt_dir
, &f
) != 0 || f
.f_type
!= SHMFS_SUPER_MAGIC
)
106 namelen
= strlen (mp
->mnt_dir
);
109 /* Hum, maybe some crippled entry. Keep on searching. */
112 mountpoint
.dir
= (char *) malloc (namelen
+ 2);
113 if (mountpoint
.dir
!= NULL
)
115 char *cp
= __mempcpy (mountpoint
.dir
, mp
->mnt_dir
, namelen
);
119 mountpoint
.dirlen
= cp
- mountpoint
.dir
;
125 /* Close the stream. */
130 /* Open shared memory object. This implementation assumes the shmfs
131 implementation introduced in the late 2.3.x kernel series to be
132 available. Normally the filesystem will be mounted at /dev/shm but
133 we fall back on searching for the actual mount point should opening
136 shm_open (const char *name
, int oflag
, mode_t mode
)
142 /* Determine where the shmfs is mounted. */
143 __libc_once (once
, where_is_shmfs
);
145 /* If we don't know the mount points there is nothing we can do. Ever. */
146 if (mountpoint
.dir
== NULL
)
148 __set_errno (ENOSYS
);
152 /* Construct the filename. */
153 while (name
[0] == '/')
158 /* The name "/" is not supported. */
159 __set_errno (EINVAL
);
163 namelen
= strlen (name
);
164 fname
= (char *) alloca (mountpoint
.dirlen
+ namelen
+ 1);
165 __mempcpy (__mempcpy (fname
, mountpoint
.dir
, mountpoint
.dirlen
),
172 /* And get the file descriptor.
173 XXX Maybe we should test each descriptor whether it really is for a
174 file on the shmfs. If this is what should be done the whole function
175 should be revamped since we can determine whether shmfs is available
176 while trying to open the file, all in one turn. */
177 fd
= open (fname
, oflag
| O_NOFOLLOW
, mode
);
180 #if !defined O_CLOEXEC || !defined __ASSUME_O_CLOEXEC
182 if (have_o_cloexec
<= 0)
185 /* We got a descriptor. Now set the FD_CLOEXEC bit. */
186 int flags
= fcntl (fd
, F_GETFD
, 0);
188 if (__builtin_expect (flags
, 0) >= 0)
191 if (have_o_cloexec
== 0)
192 have_o_cloexec
= (flags
& FD_CLOEXEC
) == 0 ? -1 : 1;
193 if (have_o_cloexec
< 0)
197 flags
= fcntl (fd
, F_SETFD
, flags
);
203 /* Something went wrong. We cannot return the descriptor. */
204 int save_errno
= errno
;
207 __set_errno (save_errno
);
212 else if (__builtin_expect (errno
== EISDIR
, 0))
213 /* It might be better to fold this error with EINVAL since
214 directory names are just another example for unsuitable shared
215 object names and the standard does not mention EISDIR. */
216 __set_errno (EINVAL
);
222 /* Unlink a shared memory object. */
224 shm_unlink (const char *name
)
229 /* Determine where the shmfs is mounted. */
230 __libc_once (once
, where_is_shmfs
);
232 if (mountpoint
.dir
== NULL
)
234 /* We cannot find the shmfs. If `name' is really a shared
235 memory object it must have been created by another process
236 and we have no idea where that process found the mountpoint. */
237 __set_errno (ENOENT
);
241 /* Construct the filename. */
242 while (name
[0] == '/')
247 /* The name "/" is not supported. */
248 __set_errno (ENOENT
);
252 namelen
= strlen (name
);
253 fname
= (char *) alloca (mountpoint
.dirlen
+ namelen
+ 1);
254 __mempcpy (__mempcpy (fname
, mountpoint
.dir
, mountpoint
.dirlen
),
257 /* And remove the file. */
258 int ret
= unlink (fname
);
259 if (ret
< 0 && errno
== EPERM
)
260 __set_errno (EACCES
);
265 /* Make sure the table is freed if we want to free everything before
267 libc_freeres_fn (freeit
)
269 if (mountpoint
.dir
!= defaultdir
)
270 free (mountpoint
.dir
);