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. Directory descriptor abstraction.
9 : */
10 :
11 : #include "native_client/src/include/portability.h"
12 :
13 : #include <stdlib.h>
14 : #include <string.h>
15 :
16 : #include "native_client/src/trusted/desc/nacl_desc_base.h"
17 : #include "native_client/src/trusted/desc/nacl_desc_invalid.h"
18 :
19 : #include "native_client/src/shared/platform/nacl_log.h"
20 : #include "native_client/src/shared/platform/nacl_sync_checked.h"
21 :
22 : #include "native_client/src/trusted/service_runtime/internal_errno.h"
23 : #include "native_client/src/trusted/service_runtime/nacl_config.h"
24 :
25 : static int NaClDescInvalidExternalizeSize(struct NaClDesc *vself,
26 : size_t *nbytes,
27 1062 : size_t *nhandles) {
28 : UNREFERENCED_PARAMETER(vself);
29 1062 : *nbytes = 0;
30 1062 : *nhandles = 0;
31 1062 : return 0;
32 : }
33 :
34 : static int NaClDescInvalidExternalize(struct NaClDesc *vself,
35 1062 : struct NaClDescXferState *xfer) {
36 : UNREFERENCED_PARAMETER(vself);
37 : UNREFERENCED_PARAMETER(xfer);
38 1062 : return 0;
39 : }
40 :
41 : /*
42 : * NaClDescInvalid is the subclass for the singleton invalid descriptor.
43 : */
44 :
45 : static struct NaClDescVtbl const kNaClDescInvalidVtbl = {
46 : {
47 : NaClDescDtorNotImplemented,
48 : },
49 : NaClDescMapNotImplemented,
50 : NaClDescUnmapUnsafeNotImplemented,
51 : NaClDescUnmapNotImplemented,
52 : NaClDescReadNotImplemented,
53 : NaClDescWriteNotImplemented,
54 : NaClDescSeekNotImplemented,
55 : NaClDescIoctlNotImplemented,
56 : NaClDescFstatNotImplemented,
57 : NaClDescGetdentsNotImplemented,
58 : NACL_DESC_INVALID,
59 : NaClDescInvalidExternalizeSize,
60 : NaClDescInvalidExternalize,
61 : NaClDescLockNotImplemented,
62 : NaClDescTryLockNotImplemented,
63 : NaClDescUnlockNotImplemented,
64 : NaClDescWaitNotImplemented,
65 : NaClDescTimedWaitAbsNotImplemented,
66 : NaClDescSignalNotImplemented,
67 : NaClDescBroadcastNotImplemented,
68 : NaClDescSendMsgNotImplemented,
69 : NaClDescRecvMsgNotImplemented,
70 : NaClDescConnectAddrNotImplemented,
71 : NaClDescAcceptConnNotImplemented,
72 : NaClDescPostNotImplemented,
73 : NaClDescSemWaitNotImplemented,
74 : NaClDescGetValueNotImplemented,
75 : };
76 :
77 : static struct NaClMutex *mutex = NULL;
78 : static struct NaClDescInvalid *singleton;
79 :
80 38 : void NaClDescInvalidInit() {
81 38 : mutex = (struct NaClMutex *) malloc(sizeof(*mutex));
82 38 : if (NULL == mutex) {
83 0 : NaClLog(LOG_FATAL, "Cannot allocate NaClDescInvalid mutex\n");
84 : }
85 38 : if (!NaClMutexCtor(mutex)) {
86 0 : free(mutex);
87 0 : mutex = NULL;
88 0 : NaClLog(LOG_FATAL, "Cannot construct NaClDescInvalid mutex\n");
89 : }
90 38 : }
91 :
92 26 : void NaClDescInvalidFini() {
93 26 : if (NULL != mutex) {
94 26 : NaClMutexDtor(mutex);
95 26 : free(mutex);
96 26 : mutex = NULL;
97 : }
98 26 : }
99 :
100 1073 : struct NaClDescInvalid const *NaClDescInvalidMake() {
101 1073 : NaClXMutexLock(mutex);
102 1073 : if (NULL == singleton) {
103 : do {
104 : /* Allocate an instance. */
105 11 : singleton = (struct NaClDescInvalid *) malloc(sizeof(*singleton));
106 11 : if (NULL == singleton) {
107 0 : break;
108 : }
109 : /* Do the base class construction. */
110 11 : if (!NaClDescCtor(&(singleton->base))) {
111 0 : free(singleton);
112 0 : singleton = NULL;
113 0 : break;
114 : }
115 : /* Construct the derived class (simply set the vtbl). */
116 11 : singleton->base.base.vtbl =
117 : (struct NaClRefCountVtbl const *) &kNaClDescInvalidVtbl;
118 : } while (0);
119 : }
120 1073 : NaClXMutexUnlock(mutex);
121 : /* If we reached this point and still have NULL == singleton there was an
122 : * error in allocation or construction. Return NULL to indicate error.
123 : */
124 1073 : if (NULL == singleton) {
125 0 : return NULL;
126 : }
127 :
128 1073 : return (struct NaClDescInvalid *) NaClDescRef(&(singleton->base));
129 : }
130 :
131 : int NaClDescInvalidInternalize(struct NaClDesc **baseptr,
132 : struct NaClDescXferState *xfer,
133 1062 : struct NaClDescQuotaInterface *quota_interface) {
134 : UNREFERENCED_PARAMETER(xfer);
135 : UNREFERENCED_PARAMETER(quota_interface);
136 :
137 1062 : *baseptr = (struct NaClDesc *) NaClDescInvalidMake();
138 :
139 1062 : return 0;
140 : }
|