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