xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/testcases/custom_fork.cc (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2023 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 #include "sandboxed_api/sandbox2/comms.h"
16 #include "sandboxed_api/sandbox2/forkingclient.h"
17 #include "sandboxed_api/util/raw_logging.h"
18 
main(int argc,char * argv[])19 int main(int argc, char* argv[]) {
20   sandbox2::Comms comms(sandbox2::Comms::kDefaultConnection);
21   sandbox2::ForkingClient s2client(&comms);
22 
23   for (;;) {
24     pid_t pid = s2client.WaitAndFork();
25     if (pid == -1) {
26       SAPI_RAW_LOG(FATAL, "Could not spawn a new sandboxee");
27     }
28     if (pid == 0) {
29       // Start sandboxing here
30       s2client.SandboxMeHere();
31       return 0;
32     }
33   }
34 }
35