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 : default:
64 : NaClLog(LOG_INFO,
65 : ("NaClAbiStatHostDescStatXlateCtor:"
66 : " Unusual NaCl descriptor type (not constructible)."
67 : " The NaCl app has a file with st_mode = 0%o."
68 : " (This is normal for std{in,out,err}, or other"
69 : " inherited/injected files.)\n"),
70 0 : src->st_mode);
71 0 : m = NACL_ABI_S_UNSUP;
72 : }
73 1 : if (0 != (src->st_mode & _S_IREAD)) {
74 1 : m |= NACL_ABI_S_IRUSR;
75 : }
76 1 : if (0 != (src->st_mode & _S_IWRITE)) {
77 1 : m |= NACL_ABI_S_IWUSR;
78 : }
79 1 : if (0 != (src->st_mode & _S_IEXEC)) {
80 1 : m |= NACL_ABI_S_IXUSR;
81 : }
82 1 : dst->nacl_abi_st_mode = m;
83 1 : dst->nacl_abi_st_nlink = src->st_nlink;
84 1 : dst->nacl_abi_st_uid = -1; /* not root */
85 1 : dst->nacl_abi_st_gid = -1; /* not wheel */
86 1 : dst->nacl_abi_st_rdev = 0;
87 1 : dst->nacl_abi_st_size = (nacl_abi_off_t) src->st_size;
88 1 : dst->nacl_abi_st_blksize = 0;
89 1 : dst->nacl_abi_st_blocks = 0;
90 1 : dst->nacl_abi_st_atime = (nacl_abi_time_t) src->st_atime;
91 1 : dst->nacl_abi_st_mtime = (nacl_abi_time_t) src->st_mtime;
92 1 : dst->nacl_abi_st_ctime = (nacl_abi_time_t) src->st_ctime;
93 :
94 : /*
95 : * For now, zero these fields. We may want to expose the
96 : * corresponding values if the underlying host OS supports
97 : * nanosecond resolution timestamps later.
98 : */
99 1 : dst->nacl_abi_st_atimensec = 0;
100 1 : dst->nacl_abi_st_mtimensec = 0;
101 1 : dst->nacl_abi_st_ctimensec = 0;
102 :
103 1 : return 0;
104 1 : }
105 :
106 : /* Read/write to a NaClHandle */
107 : ssize_t NaClDescReadFromHandle(NaClHandle handle,
108 : void *buf,
109 0 : size_t length) {
110 0 : size_t count = 0;
111 0 : CHECK(length < kMaxSyncSocketMessageLength);
112 :
113 0 : while (count < length) {
114 : DWORD len;
115 : DWORD chunk = (DWORD) (
116 0 : ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX);
117 : if (ReadFile(handle, (char *) buf + count,
118 0 : chunk, &len, NULL) == FALSE) {
119 0 : return (ssize_t) ((0 < count) ? count : -1);
120 : }
121 0 : count += len;
122 0 : }
123 0 : return (ssize_t) count;
124 0 : }
125 :
126 : ssize_t NaClDescWriteToHandle(NaClHandle handle,
127 : void const *buf,
128 0 : size_t length) {
129 0 : size_t count = 0;
130 0 : CHECK(length < kMaxSyncSocketMessageLength);
131 :
132 0 : while (count < length) {
133 : DWORD len;
134 : DWORD chunk = (DWORD) (
135 0 : ((length - count) <= UINT_MAX) ? (length - count) : UINT_MAX);
136 : if (WriteFile(handle, (const char *) buf + count,
137 0 : chunk, &len, NULL) == FALSE) {
138 0 : return (ssize_t) ((0 < count) ? count : -1);
139 : }
140 0 : count += len;
141 0 : }
142 0 : return (ssize_t) count;
143 0 : }
|