1 /* Determine directory for shm/sem files. Linux version.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 #include "shm-directory.h"
26 #include <sys/statfs.h>
27 #include <libc-lock.h>
28 #include "linux_fsinfo.h"
31 /* Mount point of the shared memory filesystem. */
38 /* This is the default directory. */
39 static const char defaultdir
[] = "/dev/shm/";
41 /* Protect the `mountpoint' variable above. */
42 __libc_once_define (static, once
);
45 /* Determine where the shmfs is mounted (if at all). */
55 /* The canonical place is /dev/shm. This is at least what the
56 documentation tells everybody to do. */
57 if (__statfs (defaultdir
, &f
) == 0 && (f
.f_type
== SHMFS_SUPER_MAGIC
58 || f
.f_type
== RAMFS_MAGIC
))
60 /* It is in the normal place. */
61 mountpoint
.dir
= (char *) defaultdir
;
62 mountpoint
.dirlen
= sizeof (defaultdir
) - 1;
67 /* OK, do it the hard way. Look through the /proc/mounts file and if
68 this does not exist through /etc/fstab to find the mount point. */
69 fp
= __setmntent ("/proc/mounts", "r");
70 if (__glibc_unlikely (fp
== NULL
))
72 fp
= __setmntent (_PATH_MNTTAB
, "r");
73 if (__glibc_unlikely (fp
== NULL
))
74 /* There is nothing we can do. Blind guesses are not helpful. */
78 /* Now read the entries. */
79 while ((mp
= __getmntent_r (fp
, &resmem
, buf
, sizeof buf
)) != NULL
)
80 /* The original name is "shm" but this got changed in early Linux
82 if (strcmp (mp
->mnt_type
, "tmpfs") == 0
83 || strcmp (mp
->mnt_type
, "shm") == 0)
85 /* Found it. There might be more than one place where the
86 filesystem is mounted but one is enough for us. */
89 /* First make sure this really is the correct entry. At least
90 some versions of the kernel give wrong information because
91 of the implicit mount of the shmfs for SysV IPC. */
92 if (__statfs (mp
->mnt_dir
, &f
) != 0 || (f
.f_type
!= SHMFS_SUPER_MAGIC
93 && f
.f_type
!= RAMFS_MAGIC
))
96 namelen
= strlen (mp
->mnt_dir
);
99 /* Hum, maybe some crippled entry. Keep on searching. */
102 mountpoint
.dir
= (char *) malloc (namelen
+ 2);
103 if (mountpoint
.dir
!= NULL
)
105 char *cp
= __mempcpy (mountpoint
.dir
, mp
->mnt_dir
, namelen
);
109 mountpoint
.dirlen
= cp
- mountpoint
.dir
;
115 /* Close the stream. */
121 __shm_directory (size_t *len
)
123 /* Determine where the shmfs is mounted. */
124 __libc_once (once
, where_is_shmfs
);
126 /* If we don't know the mount points there is nothing we can do. Ever. */
127 if (__glibc_unlikely (mountpoint
.dir
== NULL
))
129 __set_errno (ENOSYS
);
133 *len
= mountpoint
.dirlen
;
134 return mountpoint
.dir
;
136 #if IS_IN (libpthread)
137 hidden_def (__shm_directory
)
141 /* Make sure the table is freed if we want to free everything before
143 libc_freeres_fn (freeit
)
145 if (mountpoint
.dir
!= defaultdir
)
146 free (mountpoint
.dir
);