blob: e025edde0d818c61c708906997a852cb10405dfb [file] [log] [blame]
Mitch Phillipse6997d52020-11-30 15:04:14 -08001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include "sysprop_helpers.h"
30
31#include <assert.h>
32#include <stdint.h>
33#include <stdlib.h>
34#include <string.h>
Elliott Hughes209d3be2025-08-06 14:02:59 -070035#include <sys/system_properties.h>
36
37#include <async_safe/CHECK.h>
Mitch Phillipse6997d52020-11-30 15:04:14 -080038
39static bool get_property_value(const char* property_name, char* dest, size_t dest_size) {
Elliott Hughes209d3be2025-08-06 14:02:59 -070040 CHECK(property_name);
41 CHECK(dest);
42 CHECK(dest_size != 0);
43
Mitch Phillipse6997d52020-11-30 15:04:14 -080044 const prop_info* prop = __system_property_find(property_name);
45 if (!prop) return false;
46
47 struct PropCbCookie {
48 char* dest;
49 size_t size;
50 };
51 *dest = '\0';
52 PropCbCookie cb_cookie = {dest, dest_size};
53
54 __system_property_read_callback(
55 prop,
56 [](void* cookie, const char* /* name */, const char* value, uint32_t /* serial */) {
57 auto* cb_cookie = reinterpret_cast<PropCbCookie*>(cookie);
Elliott Hughes209d3be2025-08-06 14:02:59 -070058 strlcpy(cb_cookie->dest, value, cb_cookie->size);
Mitch Phillipse6997d52020-11-30 15:04:14 -080059 },
60 &cb_cookie);
Florian Mayer85eb6fd2022-08-01 19:00:57 +000061 return *dest != '\0';
Mitch Phillipse6997d52020-11-30 15:04:14 -080062}
63
64bool get_config_from_env_or_sysprops(const char* env_var_name, const char* const* sys_prop_names,
Florian Mayerdd443782023-05-17 20:59:14 +000065 size_t sys_prop_names_size, char* options,
66 size_t options_size) {
Mitch Phillipse6997d52020-11-30 15:04:14 -080067 const char* env = getenv(env_var_name);
68 if (env && *env != '\0') {
Elliott Hughes209d3be2025-08-06 14:02:59 -070069 strlcpy(options, env, options_size);
Mitch Phillipse6997d52020-11-30 15:04:14 -080070 return true;
71 }
72
73 for (size_t i = 0; i < sys_prop_names_size; ++i) {
74 if (sys_prop_names[i] == nullptr) continue;
Florian Mayerdd443782023-05-17 20:59:14 +000075 if (get_property_value(sys_prop_names[i], options, options_size)) return true;
Mitch Phillipse6997d52020-11-30 15:04:14 -080076 }
77 return false;
78}