1 : /*
2 : * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 :
7 : /*
8 : * NaCl Service Runtime. I/O Descriptor / Handle abstraction. Memory
9 : * mapping using descriptors.
10 : */
11 :
12 : #include "native_client/src/include/portability.h"
13 : #include <windows.h>
14 : #include <sys/types.h>
15 : #include <sys/stat.h>
16 :
17 : #include "native_client/src/shared/platform/nacl_check.h"
18 : #include "native_client/src/shared/platform/nacl_host_desc.h"
19 : #include "native_client/src/shared/platform/nacl_log.h"
20 : #include "native_client/src/trusted/desc/nacl_desc_base.h"
21 : #include "native_client/src/trusted/desc/nacl_desc_sync_socket.h"
22 : #include "native_client/src/trusted/service_runtime/nacl_config.h"
23 : #include "native_client/src/trusted/service_runtime/include/sys/errno.h"
24 : #include "native_client/src/trusted/service_runtime/include/sys/stat.h"
25 :
26 0 : void NaClDescUnmapUnsafe(struct NaClDesc *desc, void *addr, size_t length) {
27 0 : int rc = (*NACL_VTBL(NaClDesc, desc)->UnmapUnsafe)(desc, addr, length);
28 0 : if (rc != 0) {
29 : NaClLog(LOG_FATAL,
30 : "NaClDescUnmapUnsafe: UnmapUnsafe() failed, rc %d, error %d\n",
31 0 : rc, GetLastError());
32 : }
33 0 : }
34 :
35 : int32_t NaClAbiStatHostDescStatXlateCtor(struct nacl_abi_stat *dst,
36 1 : nacl_host_stat_t const *src) {
37 : nacl_abi_mode_t m;
38 :
39 1 : memset(dst, 0, sizeof *dst);
40 :
41 1 : if (src->st_size > INT32_MAX) {
42 0 : return -NACL_ABI_EOVERFLOW;
43 : }
44 :
45 1 : dst->nacl_abi_st_dev = 0;
46 : #if defined(NACL_MASK_INODES)
47 1 : dst->nacl_abi_st_ino = NACL_FAKE_INODE_NUM;
48 : #else
49 : dst->nacl_abi_st_ino = src->st_ino;
50 : /*
51 : * MSDN says: st_ino has no meaning on most windows file systems,
52 : * but in case windows mounts a Unix filesystem, we copy it anyway.
53 : */
54 : #endif
55 :
56 1 : switch (src->st_mode & S_IFMT) {
57 : case S_IFREG:
58 1 : m = NACL_ABI_S_IFREG;
59 1 : break;
60 : case S_IFDIR:
61 1 : m = NACL_ABI_S_IFDIR;
62 1 : break;
63 : case S_IFCHR:
64 : /* stdin/out/err can be inherited, so this is okay */
65 0 : m = NACL_ABI_S_IFCHR;
66 0 : break;
67 : default:
68 : NaClLog(LOG_INFO,
69 : ("NaClAbiStatHostDescStatXlateCtor:"
70 : " Unusual NaCl descriptor type (not constructible)."
71 : " The NaCl app has a file with st_mode = 0%o.\n"),
72 0 : src->st_mode);
73 0 : m = NACL_ABI_S_UNSUP;
74 : }
75 1 : if (0 != (src->st_mode & _S_IREAD)) {
76 1 : m |= NACL_ABI_S_IRUSR;
77 : }
78 1 : if (0 != (src->st_mode & _S_IWRITE)) {
79 1 : m |= NACL_ABI_S_IWUSR;
80 : }
81 1 : if (0 != (src->st_mode & _S_IEXEC)) {
82 1 : m |= NACL_ABI_S_IXUSR;
83 : }
84 1 : dst->nacl_abi_st_mode = m;
85 1 : dst->nacl_abi_st_nlink = src->st_nlink;
86 1 : dst->nacl_abi_st_uid = -1; /* not root */
87 1 : dst->nacl_abi_st_gid = -1; /* not wheel */
88 1 : dst->nacl_abi_st_rdev = 0;
89 1 : dst->nacl_abi_st_size = (nacl_abi_off_t) src->st_size;
90 1 : dst->nacl_abi_st_blksize = 0;
91 1 : dst->nacl_abi_st_blocks = 0;
92 1 : dst->nacl_abi_st_atime = (nacl_abi_time_t) src->st_atime;
93 1 : dst->nacl_abi_st_mtime = (nacl_abi_time_t) src->st_mtime;
94 1 : dst->nacl_abi_st_ctime = (nacl_abi_time_t) src->st_ctime;
95 :
96 : /*
97 : * For now, zero these fields. We may want to expose the
98 : * corresponding values if the underlying host OS supports
99 : * nanosecond resolution timestamps later.
100 : */
101 1 : dst->nacl_abi_st_atimensec = 0;
102 1 : dst->nacl_abi_st_mtimensec = 0;
103 1 : dst->nacl_abi_st_ctimensec = 0;
104 :
105 1 : return 0;
106 1 : }
107 :
108 : /* Read/write to a NaClHandle */
109 : ssize_t NaClDescReadFromHandle(NaClHandle handle,
110 : void *buf,
111 0 : size_t length) {
112 0 : size_t count = 0;
113 0 : CHECK(length < kMaxSyncSocketMessageLength);
114 :
115 0 : while (count < length) {
116 : DWORD len;
117 : DWORD chunk = (DWORD) (
118 0 : ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX);
119 : if (ReadFile(handle, (char *) buf + count,
120 0 : chunk, &len, NULL) == FALSE) {
121 0 : return (ssize_t) ((0 < count) ? count : -1);
122 : }
123 0 : count += len;
124 0 : }
125 0 : return (ssize_t) count;
126 0 : }
127 :
128 : ssize_t NaClDescWriteToHandle(NaClHandle handle,
129 : void const *buf,
130 0 : size_t length) {
131 0 : size_t count = 0;
132 0 : CHECK(length < kMaxSyncSocketMessageLength);
133 :
134 0 : while (count < length) {
135 : DWORD len;
136 : DWORD chunk = (DWORD) (
137 0 : ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX);
138 : if (WriteFile(handle, (const char *) buf + count,
139 0 : chunk, &len, NULL) == FALSE) {
140 0 : return (ssize_t) ((0 < count) ? count : -1);
141 : }
142 0 : count += len;
143 0 : }
144 0 : return (ssize_t) count;
145 0 : }
|