1 : /*
2 : * Copyright (c) 2011 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 <limits.h>
13 : #include <stdint.h>
14 : #include <string.h>
15 : #include <sys/types.h>
16 : #include <sys/stat.h>
17 :
18 : #include "native_client/src/include/portability.h"
19 : #include "native_client/src/shared/platform/nacl_check.h"
20 : #include "native_client/src/shared/platform/nacl_host_desc.h"
21 : #include "native_client/src/shared/platform/nacl_log.h"
22 : #include "native_client/src/trusted/desc/nacl_desc_base.h"
23 : #include "native_client/src/trusted/desc/nacl_desc_sync_socket.h"
24 : #include "native_client/src/trusted/service_runtime/nacl_config.h"
25 : #include "native_client/src/trusted/service_runtime/include/sys/errno.h"
26 : #include "native_client/src/trusted/service_runtime/include/sys/stat.h"
27 :
28 : void NaClDeallocAddrRange(uintptr_t addr,
29 0 : size_t len) {
30 : UNREFERENCED_PARAMETER(addr);
31 : UNREFERENCED_PARAMETER(len);
32 : return;
33 : }
34 :
35 : /*
36 : * Not quite a copy ctor. Call it a translating ctor, since the
37 : * struct nacl_abi_stat POD object is constructed from the
38 : * nacl_host_stat_t POD object by element-wise copying.
39 : */
40 : int32_t NaClAbiStatHostDescStatXlateCtor(struct nacl_abi_stat *dst,
41 15 : nacl_host_stat_t const *src) {
42 : nacl_abi_mode_t m;
43 :
44 15 : memset(dst, 0, sizeof *dst);
45 :
46 15 : dst->nacl_abi_st_dev = 0;
47 : #if defined(NACL_MASK_INODES)
48 15 : dst->nacl_abi_st_ino = NACL_FAKE_INODE_NUM;
49 : #else
50 : dst->nacl_abi_st_ino = src->st_ino;
51 : #endif
52 :
53 15 : switch (src->st_mode & S_IFMT) {
54 : case S_IFREG:
55 10 : m = NACL_ABI_S_IFREG;
56 10 : break;
57 : case S_IFDIR:
58 2 : m = NACL_ABI_S_IFDIR;
59 2 : break;
60 : #if defined(S_IFCHR)
61 : case S_IFCHR:
62 : /* stdin/out/err can be inherited, so this is okay */
63 0 : m = NACL_ABI_S_IFCHR;
64 0 : break;
65 : #endif
66 : default:
67 3 : NaClLog(LOG_INFO,
68 : ("NaClAbiStatHostDescStatXlateCtor:"
69 : " Unusual NaCl descriptor type (not constructible)."
70 : " The NaCl app has a file with st_mode = 0%o."
71 : " (This is normal for std{in,out,err}, or other"
72 : " inherited/injected files.)\n"),
73 : src->st_mode);
74 3 : m = NACL_ABI_S_UNSUP;
75 : }
76 15 : if (0 != (src->st_mode & S_IRUSR)) {
77 15 : m |= NACL_ABI_S_IRUSR;
78 : }
79 15 : if (0 != (src->st_mode & S_IWUSR)) {
80 15 : m |= NACL_ABI_S_IWUSR;
81 : }
82 15 : if (0 != (src->st_mode & S_IXUSR)) {
83 9 : m |= NACL_ABI_S_IXUSR;
84 : }
85 15 : dst->nacl_abi_st_mode = m;
86 15 : dst->nacl_abi_st_nlink = src->st_nlink;
87 15 : dst->nacl_abi_st_uid = -1; /* not root */
88 15 : dst->nacl_abi_st_gid = -1; /* not wheel */
89 15 : dst->nacl_abi_st_rdev = 0;
90 15 : dst->nacl_abi_st_size = (nacl_abi_off_t) src->st_size;
91 15 : dst->nacl_abi_st_blksize = 0;
92 15 : dst->nacl_abi_st_blocks = 0;
93 15 : dst->nacl_abi_st_atime = src->st_atime;
94 15 : dst->nacl_abi_st_mtime = src->st_mtime;
95 15 : dst->nacl_abi_st_ctime = src->st_ctime;
96 :
97 : /*
98 : * For now, zero these fields. We may want to expose the
99 : * corresponding values if the underlying host OS supports
100 : * nanosecond resolution timestamps later.
101 : */
102 15 : dst->nacl_abi_st_atimensec = 0;
103 15 : dst->nacl_abi_st_mtimensec = 0;
104 15 : dst->nacl_abi_st_ctimensec = 0;
105 :
106 15 : return 0;
107 : }
108 :
109 : /* Read/write to a NaClHandle */
110 : ssize_t NaClDescReadFromHandle(NaClHandle handle,
111 : void *buf,
112 0 : size_t length) {
113 0 : CHECK(length < kMaxSyncSocketMessageLength);
114 :
115 0 : return read(handle, buf, length);
116 : }
117 :
118 : ssize_t NaClDescWriteToHandle(NaClHandle handle,
119 : void const *buf,
120 0 : size_t length) {
121 0 : CHECK(length < kMaxSyncSocketMessageLength);
122 :
123 0 : return write(handle, buf, length);
124 : }
|