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