xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/rpcchannel.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2019 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef SANDBOXED_API_RPCCHANNEL_H_
16 #define SANDBOXED_API_RPCCHANNEL_H_
17 
18 #include <cstddef>
19 #include <cstdint>
20 
21 #include "absl/status/status.h"
22 #include "absl/status/statusor.h"
23 #include "absl/synchronization/mutex.h"
24 #include "sandboxed_api/call.h"
25 #include "sandboxed_api/sandbox2/comms.h"
26 #include "sandboxed_api/var_type.h"
27 
28 namespace sapi {
29 
30 // This class exposes functions which provide primitives operating over the
31 // Comms channel.
32 class RPCChannel {
33  public:
RPCChannel(sandbox2::Comms * comms)34   explicit RPCChannel(sandbox2::Comms* comms) : comms_(comms) {}
35 
36   // Calls a function.
37   absl::Status Call(const FuncCall& call, uint32_t tag, FuncRet* ret,
38                     v::Type exp_type);
39 
40   // Allocates memory.
41   absl::Status Allocate(size_t size, void** addr);
42 
43   // Reallocates memory.
44   absl::Status Reallocate(void* old_addr, size_t size, void** new_addr);
45 
46   // Frees memory.
47   absl::Status Free(void* addr);
48 
49   // Returns address of a symbol.
50   absl::Status Symbol(const char* symname, void** addr);
51 
52   // Makes the remote part exit.
53   absl::Status Exit();
54 
55   // Transfers fd to sandboxee.
56   absl::Status SendFD(int local_fd, int* remote_fd);
57 
58   // Retrieves fd from sandboxee.
59   absl::Status RecvFD(int remote_fd, int* local_fd);
60 
61   // Closes fd in sandboxee.
62   absl::Status Close(int remote_fd);
63 
64   // Returns length of a null-terminated c-style string (invokes strlen).
65   absl::StatusOr<size_t> Strlen(void* str);
66 
comms()67   sandbox2::Comms* comms() const { return comms_; }
68 
69  private:
70   // Receives the result after a call.
71   absl::StatusOr<FuncRet> Return(v::Type exp_type);
72 
73   sandbox2::Comms* comms_;  // Owned by sandbox2;
74   absl::Mutex mutex_;
75 };
76 
77 }  // namespace sapi
78 
79 #endif  // SANDBOXED_API_RPCCHANNEL_H_
80