blob: 10da3efc2b344517693e7c3c9a7a527ace39d1b5 [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>
35#include "sys/system_properties.h"
36
37static bool get_property_value(const char* property_name, char* dest, size_t dest_size) {
38 assert(property_name && dest && dest_size != 0);
39 const prop_info* prop = __system_property_find(property_name);
40 if (!prop) return false;
41
42 struct PropCbCookie {
43 char* dest;
44 size_t size;
45 };
46 *dest = '\0';
47 PropCbCookie cb_cookie = {dest, dest_size};
48
49 __system_property_read_callback(
50 prop,
51 [](void* cookie, const char* /* name */, const char* value, uint32_t /* serial */) {
52 auto* cb_cookie = reinterpret_cast<PropCbCookie*>(cookie);
53 strncpy(cb_cookie->dest, value, cb_cookie->size);
54 },
55 &cb_cookie);
Mitch Phillips9634c362022-06-23 11:07:00 -070056 if (*dest != '\0') return true;
Mitch Phillipse6997d52020-11-30 15:04:14 -080057
58 return false;
59}
60
61bool get_config_from_env_or_sysprops(const char* env_var_name, const char* const* sys_prop_names,
62 size_t sys_prop_names_size, char* options,
63 size_t options_size) {
64 const char* env = getenv(env_var_name);
65 if (env && *env != '\0') {
66 strncpy(options, env, options_size);
67 options[options_size - 1] = '\0'; // Ensure null-termination.
68 return true;
69 }
70
71 for (size_t i = 0; i < sys_prop_names_size; ++i) {
72 if (sys_prop_names[i] == nullptr) continue;
73 if (get_property_value(sys_prop_names[i], options, options_size)) return true;
74 }
75 return false;
76}