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 #ifndef NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_NACL_RESOURCE_H_
8 #define NATIVE_CLIENT_SRC_TRUSTED_SERVICE_RUNTIME_NACL_RESOURCE_H_
9 
10 #include "native_client/src/include/nacl_base.h"
11 
12 EXTERN_C_BEGIN
13 
14 /*
15  * Pseudo device name for NACL_EXE_STD{OUT,ERR}.
16  */
17 #define NACL_RESOURCE_DEBUG_WARNING   "DEBUG_ONLY:"
18 #define NACL_RESOURCE_FILE_PREFIX     "file:"
19 #define NACL_RESOURCE_FILE_DEV_NULL   "/dev/null"
20 
21 struct NaClResource;
22 
23 struct NaClResourceSchemes {
24   char const      *scheme_prefix;
25   int             default_scheme;
26   /*
27    * |default_scheme| is a bool.  If no scheme prefixes match, try
28    * Open with this.  There should be only one default scheme per
29    * scheme_table.
30    */
31 
32   /*
33    * The reason to separate out these functions is to make resource
34    * namespace separation clearer.  Files, which require --no-sandbox
35    * to disable the outer sandbox, allow arbitrary paths for logging
36    * untrusted code output; pseudo-devices (for postmessage) is
37    * (currently) a namespace of one entry.
38    *
39    * |nacl_flags| should be NACL_ABI_ versions of |flags| and should
40    * be consistent.  This is typically determined at compile time, but
41    * the utility NaClHostDescMapOpenFlags can be used to convert
42    * nacl_flags values to flags values.
43    *
44    * |mode| should be file access mode (if file, if O_CREAT, if appropriate).
45    */
46   struct NaClDesc *(*Open)(struct NaClResource  *resource,
47                            char const           *resource_specifier_rest,
48                            int                  nacl_flags,
49                            int                  mode /* 0777 etc */,
50                            int                  allow_debug /* bool */);
51 };
52 
53 
54 struct NaClResource {
55   /*
56    * no vtbl with virtual dtor, since (for now) only object creator
57    * should dtor/delete, and there are no other virtual functions
58    * needed.
59    */
60   struct NaClResourceSchemes const  *schemes;
61   size_t                            num_schemes;
62 };
63 
64 /*
65  * NaClResourceOpen handles NACL_RESOURCE_DEBUG_WARNING_PREFIX checks
66  * (and stripping), NACL_RESOURCE_{FILE,DEV}_PREFIX dispatch.
67  *
68  * This function does not take a descriptor number to directly modify
69  * the descriptor array and require the caller to invoke
70  * NaClAppSetDesc(), since the API allows other uses of the returned
71  * NaClDesc object than just for redirection.
72  */
73 struct NaClDesc *NaClResourceOpen(struct NaClResource *self,
74                                   char const          *resource_locator,
75                                   int                 nacl_flags,
76                                   int                 mode);
77 
78 /*
79  * Subclasses can expand on the NaClResource base class, e.g., add
80  * startup phase information so that the Open functions can get the
81  * NaClApp pointer, etc.  The sole base class member function,
82  * NaClResourceOpen, is unaware of startup phases and relies on the
83  * scheme table's Open function to do the right thing.
84  */
85 struct NaClResourceNaClApp {
86   struct NaClResource base;
87   struct NaClApp      *nap;
88 };
89 
90 int NaClResourceNaClAppCtor(struct NaClResourceNaClApp        *self,
91                             struct NaClResourceSchemes const  *scheme_tbl,
92                             size_t                            num_schemes,
93                             struct NaClApp                    *nap);
94 
95 /*
96  * Invoke Ctor with standard resource schemes.
97  */
98 int NaClResourceNaClAppInit(struct NaClResourceNaClApp        *self,
99                             struct NaClApp                    *nap);
100 
101 EXTERN_C_END
102 
103 #endif
104