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