1 /*
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 
get_property_value(const char * property_name,char * dest,size_t dest_size)37 static 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);
56   return *dest != '\0';
57 }
58 
get_config_from_env_or_sysprops(const char * env_var_name,const char * const * sys_prop_names,size_t sys_prop_names_size,char * options,size_t options_size)59 bool get_config_from_env_or_sysprops(const char* env_var_name, const char* const* sys_prop_names,
60                                      size_t sys_prop_names_size, char* options,
61                                      size_t options_size) {
62   const char* env = getenv(env_var_name);
63   if (env && *env != '\0') {
64     strncpy(options, env, options_size);
65     options[options_size - 1] = '\0';  // Ensure null-termination.
66     return true;
67   }
68 
69   for (size_t i = 0; i < sys_prop_names_size; ++i) {
70     if (sys_prop_names[i] == nullptr) continue;
71     if (get_property_value(sys_prop_names[i], options, options_size)) return true;
72   }
73   return false;
74 }
75