hurd: Add open.

* include/fcntl.h (O_RDONLY, O_WRONLY, O_RDWR, O_CREAT, O_APPEND,
O_TRUNC)[__GNU__]: Specialize for GNU.
* lib/gnu/dir-lookup.c: New file.
* lib/stdio/fopen.c (fopen): Use O_RDONLY.
* lib/tests/posix/50-open-read.c (main): Likewise.
* lib/tests/stdio/90-fseek.c (main): Likewise.
* include/gnu/syscall.h (SYS__dir_lookup): New value.
* lib/gnu/_open3.c: New file.
* build-aux/configure-lib.sh (libc_SOURCES): Build them.
This commit is contained in:
Jan Nieuwenhuizen 2019-03-14 00:08:35 +01:00
parent 67d82a6de5
commit 89945751d9
No known key found for this signature in database
GPG key ID: F3C1A0D9C1D65273
9 changed files with 149 additions and 7 deletions

View file

@ -157,7 +157,9 @@ lib/string/strncmp.c
if test $mes_kernel = gnu; then if test $mes_kernel = gnu; then
libc_SOURCES="$libc_SOURCES libc_SOURCES="$libc_SOURCES
lib/gnu/_open3.c
lib/gnu/_read.c lib/gnu/_read.c
lib/gnu/dir-lookup.c
lib/gnu/fd-read.c lib/gnu/fd-read.c
lib/gnu/io-read.c lib/gnu/io-read.c
lib/stub/access.c lib/stub/access.c
@ -171,7 +173,6 @@ lib/stub/fork.c
lib/stub/_getcwd.c lib/stub/_getcwd.c
lib/stub/gettimeofday.c lib/stub/gettimeofday.c
lib/stub/ioctl.c lib/stub/ioctl.c
lib/stub/_open3.c
lib/stub/time.c lib/stub/time.c
lib/stub/unlink.c lib/stub/unlink.c
lib/stub/waitpid.c lib/stub/waitpid.c

View file

@ -30,6 +30,7 @@
#else // ! SYSTEM_LIBC #else // ! SYSTEM_LIBC
// *INDENT-OFF* // *INDENT-OFF*
#if __linux__
#define O_RDONLY 0 #define O_RDONLY 0
#define O_WRONLY 1 #define O_WRONLY 1
#define O_RDWR 2 #define O_RDWR 2
@ -38,6 +39,16 @@
#define O_TRUNC 0x200 #define O_TRUNC 0x200
#define O_APPEND 0x400 #define O_APPEND 0x400
#define O_DIRECTORY 0x10000 #define O_DIRECTORY 0x10000
#elif __GNU__
#define O_RDONLY 1
#define O_WRONLY 2
#define O_RDWR 3
#define O_CREAT 0x10
#define O_APPEND 0x100
#define O_TRUNC 0x10000
#else
#error platform not supported
#endif
// *INDENT-ON* // *INDENT-ON*
#define FD_CLOEXEC 1 #define FD_CLOEXEC 1

View file

@ -38,6 +38,12 @@ enum
SYS__task_get_special_port = 2058, SYS__task_get_special_port = 2058,
}; };
// hurd/fsys.defs
enum
{
SYS__dir_lookup = 20018,
};
// hurd/io.defs // hurd/io.defs
enum enum
{ {

48
lib/gnu/_open3.c Normal file
View file

@ -0,0 +1,48 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* GNU Mes is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
/** Commentary:
Inspired by implementation in GNU C Library:
__hurd_file_name_lookup, __hurd_file_name_lookup_retry
Copyright (C) 1992-2016 Free Software Foundation, Inc.
*/
#include <gnu/hurd.h>
#include <gnu/hurd-types.h>
#include <gnu/syscall.h>
#include <mach/mach-init.h>
int
_open3 (char const *file_name, int flags, int mode)
{
mach_port_t port;
int do_retry;
char retry_name[1024];
int start_dir = (file_name[0] == '/') ? INIT_PORT_CRDIR : INIT_PORT_CWDIR;
mach_port_t start_port = _hurd_startup_data.portarray[start_dir];
while (file_name[0] == '/')
file_name++;
error_t e = __dir_lookup (start_port, file_name, flags, mode, &do_retry, retry_name, &port);
if (e)
return -1;
int fd = _hurd_dtable_count++;
_hurd_dtable[fd] = port;
return fd;
}

76
lib/gnu/dir-lookup.c Normal file
View file

@ -0,0 +1,76 @@
/* -*-comment-start: "//";comment-end:""-*-
* GNU Mes --- Maxwell Equations of Software
* Copyright © 2019 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
*
* This file is part of GNU Mes.
*
* GNU Mes is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* GNU Mes is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
*/
#include <gnu/syscall.h>
#include <string.h>
#include <sys/stat.h>
struct mach_msg_string_int_int
{
mach_msg_header_t header;
mach_msg_type_t type_one; string_t one;
mach_msg_type_t type_two; int two;
mach_msg_type_t type_three; int three;
};
struct mach_msg_int_int_string_int
{
mach_msg_header_t header;
mach_msg_type_t type_one; int one;
mach_msg_type_t type_two; int two;
mach_msg_type_t type_three; string_t three;
mach_msg_type_t type_four; int four;
};
mach_msg_type_t mach_msg_type_file_name =
{
/* msgt_name = */ (unsigned char) MACH_MSG_TYPE_STRING_C,
/* msgt_size = */ 8,
/* msgt_number = */ 1024,
/* msgt_inline = */ 1,
/* msgt_longform = */ 0,
/* msgt_deallocate = */ 0,
/* msgt_unused = */ 0
};
kern_return_t __dir_lookup (file_t start_dir, string_t file_name, int flags, mode_t mode, retry_type *do_retry, string_t retry_name, mach_port_t *port)
{
union message
{
struct mach_msg_string_int_int request;
struct mach_msg_int_int_string_int reply;
};
union message message = {0};
message.request.header.msgh_size = sizeof (message.request);
message.request.type_one = mach_msg_type_file_name;
strcpy (message.request.one, file_name);
message.request.type_two = mach_msg_type_int32;
message.request.two = flags;
message.request.type_three = mach_msg_type_int32;
message.request.three = mode;
kern_return_t result = __syscall_get (start_dir, SYS__dir_lookup,
&message.request.header,
sizeof (message.reply));
if (message.reply.one != KERN_SUCCESS)
return message.reply.one;
*port = message.reply.four;
return result;
}

View file

@ -20,7 +20,7 @@
#include <gnu/syscall.h> #include <gnu/syscall.h>
struct mach_msg_pointer_int struct mach_msg_pointer_loff
{ {
mach_msg_header_t header; mach_msg_header_t header;
mach_msg_type_long_t type_one; char *one; mach_msg_type_long_t type_one; char *one;
@ -30,8 +30,8 @@ struct mach_msg_pointer_int
kern_return_t kern_return_t
__io_write (io_t io, data_t data, mach_msg_type_number_t size, loff_t offset, vm_size_t *wrote) __io_write (io_t io, data_t data, mach_msg_type_number_t size, loff_t offset, vm_size_t *wrote)
{ {
struct mach_msg_pointer_int message = {0}; struct mach_msg_pointer_loff message = {0};
message.header.msgh_size = sizeof (struct mach_msg_pointer_int); message.header.msgh_size = sizeof (struct mach_msg_pointer_loff);
message.type_one = mach_msg_type_pointer; message.type_one = mach_msg_type_pointer;
message.one = data; message.one = data;
message.type_two = mach_msg_type_int64; message.type_two = mach_msg_type_int64;

View file

@ -54,7 +54,7 @@ fopen (char const *file_name, char const *opentype)
fd = _open3 (file_name, flags, mode); fd = _open3 (file_name, flags, mode);
} }
else else
fd = _open3 (file_name, 0, 0); fd = _open3 (file_name, O_RDONLY, 0);
if (__mes_debug ()) if (__mes_debug ())
{ {

View file

@ -41,7 +41,7 @@ main (int argc, char const *argv[])
eputs ("test open:"); eputs ("test open:");
eputs (file_name); eputs (file_name);
eputs ("\n"); eputs ("\n");
int filedes = open (file_name, 0, 0); int filedes = open (file_name, O_RDONLY, 0);
if (filedes <= 2) if (filedes <= 2)
return 1; return 1;
char buf[20]; char buf[20];

View file

@ -27,7 +27,7 @@
int int
main () main ()
{ {
int fd = open ("../COPYING", 0); int fd = open ("../COPYING", O_RDONLY);
if (fd <= 0) if (fd <= 0)
return 1; return 1;
FILE *f = fdopen (fd, "r"); FILE *f = fdopen (fd, "r");