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 : #include "native_client/src/trusted/service_runtime/nacl_reverse_quota_interface.h"
8 : #include "native_client/src/include/portability.h"
9 : #include "native_client/src/include/nacl_macros.h"
10 :
11 : #include "native_client/src/shared/platform/nacl_log.h"
12 : #include "native_client/src/shared/platform/nacl_sync.h"
13 : #include "native_client/src/shared/platform/nacl_sync_checked.h"
14 : #include "native_client/src/shared/srpc/nacl_srpc.h"
15 :
16 : #include "native_client/src/trusted/desc/nacl_desc_base.h"
17 : #include "native_client/src/trusted/desc/nacl_desc_quota.h"
18 : #include "native_client/src/trusted/desc/nacl_desc_quota_interface.h"
19 :
20 : #include "native_client/src/trusted/reverse_service/reverse_control_rpc.h"
21 : #include "native_client/src/trusted/service_runtime/sel_ldr.h"
22 :
23 :
24 : /*
25 : * A descriptor quota interface that works over the reverse channel to Chrome.
26 : */
27 :
28 : struct NaClDescQuotaInterfaceVtbl const kNaClReverseQuotaInterfaceVtbl;
29 :
30 : int NaClReverseQuotaInterfaceCtor(
31 3 : struct NaClReverseQuotaInterface *self,
32 3 : struct NaClSecureService *server) {
33 3 : NaClLog(4, "NaClReverseQuotaInterfaceCtor:"
34 : " self 0x%"NACL_PRIxPTR", server 0x%"NACL_PRIxPTR"\n",
35 : (uintptr_t) self, (uintptr_t) server);
36 3 : if (!NaClDescQuotaInterfaceCtor(&self->base)) {
37 0 : return 0;
38 : }
39 : self->server = (struct NaClSecureService *)
40 3 : NaClRefCountRef((struct NaClRefCount *) server);
41 3 : NACL_VTBL(NaClRefCount, self) =
42 : (struct NaClRefCountVtbl const *) &kNaClReverseQuotaInterfaceVtbl;
43 3 : return 1;
44 3 : }
45 :
46 0 : static void NaClReverseQuotaInterfaceDtor(struct NaClRefCount *vself) {
47 0 : struct NaClReverseQuotaInterface *self =
48 : (struct NaClReverseQuotaInterface *) vself;
49 0 : NaClLog(4, "NaClReverseQuotaInterfaceDtor\n");
50 :
51 0 : NaClRefCountUnref((struct NaClRefCount *) self->server);
52 :
53 0 : NACL_VTBL(NaClRefCount, self) =
54 : (struct NaClRefCountVtbl *) &kNaClDescQuotaInterfaceVtbl;
55 0 : (*NACL_VTBL(NaClRefCount, self)->Dtor)(vself);
56 0 : }
57 :
58 : static int64_t NaClReverseQuotaInterfaceWriteRequest(
59 0 : struct NaClDescQuotaInterface *vself,
60 0 : uint8_t const *file_id,
61 0 : int64_t offset,
62 0 : int64_t length) {
63 0 : struct NaClReverseQuotaInterface *self =
64 : (struct NaClReverseQuotaInterface *) vself;
65 0 : NaClSrpcError rpc_result;
66 0 : int64_t allowed;
67 0 : int64_t rv;
68 :
69 0 : NaClLog(4, "Entered NaClReverseQuotaWriteRequest\n");
70 0 : NaClXMutexLock(&self->server->mu);
71 0 : if (NACL_REVERSE_CHANNEL_INITIALIZED !=
72 : self->server->reverse_channel_initialization_state) {
73 0 : NaClLog(LOG_FATAL,
74 : "NaClReverseQuotaWriteRequest: Reverse channel not initialized\n");
75 0 : }
76 0 : rpc_result = NaClSrpcInvokeBySignature(&self->server->reverse_channel,
77 : NACL_REVERSE_REQUEST_QUOTA_FOR_WRITE,
78 : 16,
79 : file_id,
80 : offset,
81 : length,
82 : &allowed);
83 0 : if (NACL_SRPC_RESULT_OK != rpc_result) {
84 0 : rv = 0;
85 0 : } else {
86 0 : rv = allowed;
87 : }
88 0 : NaClXMutexUnlock(&self->server->mu);
89 0 : NaClLog(4, "Leaving NaClReverseQuotaWriteRequest\n");
90 0 : return rv;
91 : }
92 :
93 : static int64_t NaClReverseQuotaInterfaceFtruncateRequest(
94 0 : struct NaClDescQuotaInterface *self,
95 0 : uint8_t const *file_id,
96 0 : int64_t length) {
97 0 : UNREFERENCED_PARAMETER(self);
98 0 : UNREFERENCED_PARAMETER(file_id);
99 :
100 0 : NaClLog(LOG_FATAL, "FtruncateRequest invoked!?!\n");
101 0 : return length;
102 : }
103 :
104 : struct NaClDescQuotaInterfaceVtbl const kNaClReverseQuotaInterfaceVtbl = {
105 : {
106 : NaClReverseQuotaInterfaceDtor,
107 : },
108 : NaClReverseQuotaInterfaceWriteRequest,
109 : NaClReverseQuotaInterfaceFtruncateRequest,
110 : };
|