1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/ext/base/android_utils.h"
18
19 #include "perfetto/base/build_config.h"
20
21 #include <string>
22
23 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
24 #include <sys/system_properties.h>
25 #endif
26
27 #include "perfetto/base/compiler.h"
28 #include "perfetto/base/logging.h"
29
30 namespace perfetto {
31 namespace base {
32
33 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
34
GetAndroidProp(const char * name)35 std::string GetAndroidProp(const char* name) {
36 std::string ret;
37 #if __ANDROID_API__ >= 26
38 const prop_info* pi = __system_property_find(name);
39 if (!pi) {
40 return ret;
41 }
42 __system_property_read_callback(
43 pi,
44 [](void* dst_void, const char*, const char* value, uint32_t) {
45 std::string& dst = *static_cast<std::string*>(dst_void);
46 dst = value;
47 },
48 &ret);
49 #else // __ANDROID_API__ < 26
50 char value_buf[PROP_VALUE_MAX];
51 int len = __system_property_get(name, value_buf);
52 if (len > 0 && static_cast<size_t>(len) < sizeof(value_buf)) {
53 ret = std::string(value_buf, static_cast<size_t>(len));
54 }
55 #endif
56 return ret;
57 }
58
59 #endif // PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
60
61 } // namespace base
62 } // namespace perfetto
63