Merge remote branch 'google-nacl/master'
[mono.git] / mono / io-layer / posix.c
1 /*
2  * posix.c:  Posix-specific support.
3  *
4  * Author:
5  *      Dick Porter (dick@ximian.com)
6  *
7  * (C) 2002 Ximian, Inc.
8  * Copyright (c) 2002-2009 Novell, Inc.
9  */
10
11 #include <config.h>
12 #include <glib.h>
13 #include <fcntl.h>
14 #include <unistd.h>
15 #include <errno.h>
16 #include <string.h>
17 #include <sys/stat.h>
18 #include <sys/types.h>
19 #include <stdio.h>
20
21 #include <mono/io-layer/wapi.h>
22 #include <mono/io-layer/wapi-private.h>
23 #include <mono/io-layer/handles-private.h>
24 #include <mono/io-layer/io-private.h>
25
26 static guint32
27 convert_from_flags(int flags)
28 {
29         guint32 fileaccess=0;
30         
31 #ifndef O_ACCMODE
32 #define O_ACCMODE (O_RDONLY|O_WRONLY|O_RDWR)
33 #endif
34
35         if((flags & O_ACCMODE) == O_RDONLY) {
36                 fileaccess=GENERIC_READ;
37         } else if ((flags & O_ACCMODE) == O_WRONLY) {
38                 fileaccess=GENERIC_WRITE;
39         } else if ((flags & O_ACCMODE) == O_RDWR) {
40                 fileaccess=GENERIC_READ|GENERIC_WRITE;
41         } else {
42 #ifdef DEBUG
43                 g_message("%s: Can't figure out flags 0x%x", __func__, flags);
44 #endif
45         }
46
47         /* Maybe sort out create mode too */
48
49         return(fileaccess);
50 }
51
52
53 gpointer _wapi_stdhandle_create (int fd, const gchar *name)
54 {
55         struct _WapiHandle_file file_handle = {0};
56         gpointer handle;
57         int flags;
58         
59 #ifdef DEBUG
60         g_message("%s: creating standard handle type %s, fd %d", __func__,
61                   name, fd);
62 #endif
63
64 #if !defined(__native_client__) 
65         /* Check if fd is valid */
66         do {
67                 flags=fcntl(fd, F_GETFL);
68         } while (flags == -1 && errno == EINTR);
69
70         if(flags==-1) {
71                 /* Invalid fd.  Not really much point checking for EBADF
72                  * specifically
73                  */
74 #ifdef DEBUG
75                 g_message("%s: fcntl error on fd %d: %s", __func__, fd,
76                           strerror(errno));
77 #endif
78
79                 SetLastError (_wapi_get_win32_file_error (errno));
80                 return(INVALID_HANDLE_VALUE);
81         }
82         file_handle.fileaccess=convert_from_flags(flags);
83 #else
84         /* 
85          * fcntl will return -1 in nacl, as there is no real file system API. 
86          * Yet, standard streams are available.
87          */
88         file_handle.fileaccess = (fd == STDIN_FILENO) ? GENERIC_READ : GENERIC_WRITE;
89 #endif
90
91         file_handle.filename = g_strdup(name);
92         /* some default security attributes might be needed */
93         file_handle.security_attributes=0;
94
95         /* Apparently input handles can't be written to.  (I don't
96          * know if output or error handles can't be read from.)
97          */
98         if (fd == 0) {
99                 file_handle.fileaccess &= ~GENERIC_WRITE;
100         }
101         
102         file_handle.sharemode=0;
103         file_handle.attrs=0;
104
105         handle = _wapi_handle_new_fd (WAPI_HANDLE_CONSOLE, fd, &file_handle);
106         if (handle == _WAPI_HANDLE_INVALID) {
107                 g_warning ("%s: error creating file handle", __func__);
108                 SetLastError (ERROR_GEN_FAILURE);
109                 return(INVALID_HANDLE_VALUE);
110         }
111         
112 #ifdef DEBUG
113         g_message("%s: returning handle %p", __func__, handle);
114 #endif
115
116         return(handle);
117 }
118