Skip to content

Commit 2a921e2

Browse files
okunzcopybara-github
authored andcommitted
sandbox2/util: Expose C-Wrapper for IsRunningInSandbox2.
Creates a new library which can be used to expose C-Wrappers for the C++ functions defined in sandboxed_api/sandbox2/util.h. Wrappers are implemented on a as-needed-basis, starting with `sandbox2::util::IsRunningInSandbox2`. PiperOrigin-RevId: 732018626 Change-Id: Id44f90b92d053d2a22255c14d10b5c1adae7aedf
1 parent cbc3162 commit 2a921e2

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

sandboxed_api/sandbox2/BUILD

+14
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,20 @@ cc_library(
777777
],
778778
)
779779

780+
# Library for C-wrappers of util.h.
781+
cc_library(
782+
name = "util_c",
783+
srcs = ["util_c.cc"],
784+
hdrs = ["util_c.h"],
785+
copts = sapi_platform_copts(),
786+
visibility = ["//visibility:public"],
787+
deps = [
788+
":util",
789+
"@com_google_absl//absl/log",
790+
"@com_google_absl//absl/status:statusor",
791+
],
792+
)
793+
780794
cc_library(
781795
name = "buffer",
782796
srcs = ["buffer.cc"],

sandboxed_api/sandbox2/CMakeLists.txt

+13
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,19 @@ target_compile_options(sandbox2_util PRIVATE
708708
-Wframe-larger-than=17000
709709
)
710710

711+
# sandboxed_api/sandbox2:util_c
712+
add_library(sandbox2_util_c ${SAPI_LIB_TYPE}
713+
util_c.cc
714+
util_c.h
715+
)
716+
add_library(sandbox2::util_c ALIAS sandbox2_util_c)
717+
target_link_libraries(sandbox2_util_c
718+
PRIVATE absl::statusor
719+
absl::log
720+
sandbox2::util
721+
sapi::base
722+
)
723+
711724
# sandboxed_api/sandbox2:buffer
712725
add_library(sandbox2_buffer ${SAPI_LIB_TYPE}
713726
buffer.cc

sandboxed_api/sandbox2/util_c.cc

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright 2025 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/util_c.h"
16+
17+
#include "absl/log/log.h"
18+
#include "absl/status/statusor.h"
19+
#include "sandboxed_api/sandbox2/util.h"
20+
21+
// Returns true if the current process is running inside Sandbox2.
22+
bool IsRunningInSandbox2() {
23+
absl::StatusOr<bool> result = sandbox2::util::IsRunningInSandbox2();
24+
if (!result.ok()) {
25+
LOG(ERROR) << result.status();
26+
return false;
27+
}
28+
return *result;
29+
}

sandboxed_api/sandbox2/util_c.h

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright 2025 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+
// This header provides a C-wrapper for sandbox2::util functions that may be
16+
// useful in C hostcode.
17+
18+
#ifndef SANDBOXED_API_SANDBOX2_UTIL_C_H_
19+
#define SANDBOXED_API_SANDBOX2_UTIL_C_H_
20+
21+
#include <stdbool.h>
22+
23+
#if defined(__cplusplus)
24+
extern "C" {
25+
#endif
26+
27+
// Returns true if the current process is running inside Sandbox2.
28+
bool IsRunningInSandbox2();
29+
30+
#if defined(__cplusplus)
31+
} // extern "C"
32+
#endif
33+
34+
#endif // SANDBOXED_API_SANDBOX2_UTIL_C_H_

0 commit comments

Comments
 (0)