Hurd: Missing critical region locks.
[platform/upstream/glibc.git] / sysdeps / mach / hurd / opendir.c
1 /* Copyright (C) 1993-2012 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 <limits.h>
20 #include <stddef.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <dirent.h>
24 #include <fcntl.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <hurd.h>
30 #include <hurd/fd.h>
31 #include "dirstream.h"
32
33
34 /* Open a directory stream on a file descriptor in Hurd internal form.
35    We do no checking here on the descriptor.  */
36 DIR *
37 _hurd_fd_opendir (struct hurd_fd *d)
38 {
39   DIR *dirp;
40
41   if (d == NULL)
42     {
43       errno = EBADF;
44       return NULL;
45     }
46
47   dirp = (DIR *) malloc (sizeof (DIR));
48   if (dirp == NULL)
49     return NULL;
50
51   /* Set the descriptor to close on exec. */
52   HURD_CRITICAL_BEGIN;
53   __spin_lock (&d->port.lock);
54   d->flags |= FD_CLOEXEC;
55   __spin_unlock (&d->port.lock);
56   HURD_CRITICAL_END;
57
58   dirp->__fd = d;
59   dirp->__data = dirp->__ptr = NULL;
60   dirp->__entry_data = dirp->__entry_ptr = 0;
61   dirp->__allocation = 0;
62   dirp->__size = 0;
63
64   __libc_lock_init (dirp->__lock);
65
66   return dirp;
67 }
68
69
70 /* Open a directory stream on NAME.  */
71 DIR *
72 __opendir (const char *name)
73 {
74   if (name[0] == '\0')
75     {
76       /* POSIX.1-1990 says an empty name gets ENOENT;
77          but `open' might like it fine.  */
78       __set_errno (ENOENT);
79       return NULL;
80     }
81
82   int fd = __open (name, O_RDONLY | O_NONBLOCK | O_DIRECTORY);
83   if (fd < 0)
84     return NULL;
85
86   /* Extract the pointer to the descriptor structure.  */
87   DIR *dirp = _hurd_fd_opendir (_hurd_fd_get (fd));
88   if (dirp == NULL)
89     __close (fd);
90
91   return dirp;
92 }
93 weak_alias (__opendir, opendir)