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 : * A simple service for "kernel services". The socket address will be
9 : * available to the NaCl module.
10 : */
11 :
12 : #include "native_client/src/trusted/service_runtime/include/sys/nacl_kernel_service.h"
13 : #include "native_client/src/trusted/service_runtime/nacl_kernel_service.h"
14 :
15 : #include "native_client/src/shared/platform/nacl_log.h"
16 : #include "native_client/src/shared/platform/nacl_sync.h"
17 : #include "native_client/src/shared/platform/nacl_sync_checked.h"
18 : #include "native_client/src/shared/srpc/nacl_srpc.h"
19 :
20 : #include "native_client/src/trusted/desc/nacl_desc_invalid.h"
21 : #include "native_client/src/trusted/reverse_service/reverse_control_rpc.h"
22 : #include "native_client/src/trusted/service_runtime/include/sys/errno.h"
23 : #include "native_client/src/trusted/service_runtime/nacl_runtime_host_interface.h"
24 : #include "native_client/src/trusted/simple_service/nacl_simple_service.h"
25 :
26 :
27 : struct NaClSrpcHandlerDesc const kNaClKernelServiceHandlers[];
28 :
29 : int NaClKernelServiceCtor(
30 255 : struct NaClKernelService *self,
31 255 : NaClThreadIfFactoryFunction thread_factory_fn,
32 255 : void *thread_factory_data,
33 255 : struct NaClRuntimeHostInterface *runtime_host) {
34 255 : NaClLog(4,
35 : ("NaClKernelServiceCtor: self 0x%"NACL_PRIxPTR","
36 : " runtime_host 0x%"NACL_PRIxPTR"\n"),
37 : (uintptr_t) self,
38 : (uintptr_t) runtime_host);
39 255 : if (!NaClSimpleServiceCtor(&self->base,
40 : kNaClKernelServiceHandlers,
41 : thread_factory_fn,
42 : thread_factory_data)) {
43 0 : return 0;
44 : }
45 : self->runtime_host = (struct NaClRuntimeHostInterface *)
46 255 : NaClRefCountRef((struct NaClRefCount *) runtime_host);
47 255 : NACL_VTBL(NaClRefCount, self) =
48 : (struct NaClRefCountVtbl *) &kNaClKernelServiceVtbl;
49 255 : return 1;
50 255 : }
51 :
52 0 : void NaClKernelServiceDtor(struct NaClRefCount *vself) {
53 0 : struct NaClKernelService *self = (struct NaClKernelService *) vself;
54 :
55 0 : NaClRefCountUnref((struct NaClRefCount *) self->runtime_host);
56 :
57 0 : NACL_VTBL(NaClRefCount, self) =
58 : (struct NaClRefCountVtbl *) &kNaClSimpleServiceVtbl;
59 0 : (*NACL_VTBL(NaClRefCount, self)->Dtor)(vself);
60 0 : }
61 :
62 : void NaClKernelServiceInitializationComplete(
63 0 : struct NaClKernelService *self) {
64 0 : NaClLog(4,
65 : "NaClKernelServiceInitializationComplete(0x%08"NACL_PRIxPTR")\n",
66 : (uintptr_t) self);
67 :
68 0 : (*NACL_VTBL(NaClRuntimeHostInterface, self->runtime_host)->
69 : StartupInitializationComplete)(self->runtime_host);
70 0 : }
71 :
72 : static void NaClKernelServiceInitializationCompleteRpc(
73 0 : struct NaClSrpcRpc *rpc,
74 0 : struct NaClSrpcArg **in_args,
75 0 : struct NaClSrpcArg **out_args,
76 0 : struct NaClSrpcClosure *done_cls) {
77 0 : struct NaClKernelService *nksp =
78 : (struct NaClKernelService *) rpc->channel->server_instance_data;
79 0 : UNREFERENCED_PARAMETER(in_args);
80 0 : UNREFERENCED_PARAMETER(out_args);
81 :
82 0 : rpc->result = NACL_SRPC_RESULT_OK;
83 0 : (*done_cls->Run)(done_cls);
84 :
85 0 : (*NACL_VTBL(NaClKernelService, nksp)->InitializationComplete)(nksp);
86 0 : }
87 :
88 : int NaClKernelServiceCreateProcess(
89 1 : struct NaClKernelService *self,
90 1 : struct NaClDesc **out_sock_addr,
91 1 : struct NaClDesc **out_app_addr) {
92 1 : NaClLog(4,
93 : ("NaClKernelServiceCreateProcess(0x%08"NACL_PRIxPTR
94 : ", 0x%08"NACL_PRIxPTR", 0x%08"NACL_PRIxPTR"\n"),
95 : (uintptr_t) self,
96 : (uintptr_t) out_sock_addr,
97 : (uintptr_t) out_app_addr);
98 :
99 1 : return (*NACL_VTBL(NaClRuntimeHostInterface, self->runtime_host)->
100 : CreateProcess)(self->runtime_host,
101 : out_sock_addr,
102 : out_app_addr);
103 : }
104 :
105 : static void NaClKernelServiceCreateProcessRpc(
106 1 : struct NaClSrpcRpc *rpc,
107 1 : struct NaClSrpcArg **in_args,
108 1 : struct NaClSrpcArg **out_args,
109 1 : struct NaClSrpcClosure *done_cls) {
110 1 : struct NaClKernelService *nksp =
111 : (struct NaClKernelService *) rpc->channel->server_instance_data;
112 1 : int status;
113 1 : struct NaClDesc *sock_addr = NULL;
114 1 : struct NaClDesc *app_addr = NULL;
115 2 : UNREFERENCED_PARAMETER(in_args);
116 :
117 1 : NaClLog(4, "NaClKernelServiceCreateProcessRpc: creating process\n");
118 1 : status = (*NACL_VTBL(NaClKernelService, nksp)->CreateProcess)(
119 : nksp, &sock_addr, &app_addr);
120 1 : out_args[0]->u.ival = status;
121 2 : out_args[1]->u.hval = (0 == status)
122 : ? sock_addr
123 1 : : (struct NaClDesc *) NaClDescInvalidMake();
124 2 : out_args[2]->u.hval = (0 == status)
125 : ? app_addr
126 1 : : (struct NaClDesc *) NaClDescInvalidMake();
127 1 : NaClLog(4,
128 : ("NaClKernelServiceCreateProcessRpc: status %d, sock_addr"
129 : " 0x08%"NACL_PRIxPTR", app_addr 0x%08"NACL_PRIxPTR"\n"),
130 : status, (uintptr_t) sock_addr, (uintptr_t) app_addr);
131 1 : rpc->result = NACL_SRPC_RESULT_OK;
132 1 : (*done_cls->Run)(done_cls);
133 1 : if (0 == status) {
134 1 : NaClDescUnref(sock_addr);
135 1 : NaClDescUnref(app_addr);
136 1 : }
137 1 : }
138 :
139 : struct NaClSrpcHandlerDesc const kNaClKernelServiceHandlers[] = {
140 : { NACL_KERNEL_SERVICE_INITIALIZATION_COMPLETE,
141 : NaClKernelServiceInitializationCompleteRpc, },
142 : { NACL_KERNEL_SERVICE_CREATE_PROCESS,
143 : NaClKernelServiceCreateProcessRpc, },
144 : { (char const *) NULL, (NaClSrpcMethod) NULL, },
145 : };
146 :
147 : struct NaClKernelServiceVtbl const kNaClKernelServiceVtbl = {
148 : {
149 : {
150 : NaClKernelServiceDtor,
151 : },
152 : NaClSimpleServiceConnectionFactory,
153 : NaClSimpleServiceAcceptConnection,
154 : NaClSimpleServiceAcceptAndSpawnHandler,
155 : NaClSimpleServiceRpcHandler,
156 : },
157 : NaClKernelServiceInitializationComplete,
158 : NaClKernelServiceCreateProcess,
159 : };
|