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