Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | #ifndef __LINKER_NAMESPACES_H |
| 30 | #define __LINKER_NAMESPACES_H |
| 31 | |
| 32 | #include "linker_common_types.h" |
| 33 | |
Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 34 | #include <string> |
Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 35 | #include <vector> |
Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 36 | #include <unordered_set> |
| 37 | |
| 38 | struct android_namespace_t; |
| 39 | |
| 40 | struct android_namespace_link_t { |
| 41 | public: |
| 42 | android_namespace_link_t(android_namespace_t* linked_namespace, |
| 43 | const std::unordered_set<std::string>& shared_lib_sonames) |
| 44 | : linked_namespace_(linked_namespace), shared_lib_sonames_(shared_lib_sonames) |
| 45 | {} |
| 46 | |
| 47 | android_namespace_t* linked_namespace() const { |
| 48 | return linked_namespace_; |
| 49 | } |
| 50 | |
| 51 | bool is_accessible(const char* soname) const { |
| 52 | return shared_lib_sonames_.find(soname) != shared_lib_sonames_.end(); |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | android_namespace_t* const linked_namespace_; |
| 57 | const std::unordered_set<std::string> shared_lib_sonames_; |
| 58 | }; |
Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 59 | |
| 60 | struct android_namespace_t { |
| 61 | public: |
| 62 | android_namespace_t() : name_(nullptr), is_isolated_(false) {} |
| 63 | |
| 64 | const char* get_name() const { return name_; } |
| 65 | void set_name(const char* name) { name_ = name; } |
| 66 | |
| 67 | bool is_isolated() const { return is_isolated_; } |
| 68 | void set_isolated(bool isolated) { is_isolated_ = isolated; } |
| 69 | |
| 70 | const std::vector<std::string>& get_ld_library_paths() const { |
| 71 | return ld_library_paths_; |
| 72 | } |
| 73 | void set_ld_library_paths(std::vector<std::string>&& library_paths) { |
| 74 | ld_library_paths_ = library_paths; |
| 75 | } |
| 76 | |
| 77 | const std::vector<std::string>& get_default_library_paths() const { |
| 78 | return default_library_paths_; |
| 79 | } |
| 80 | void set_default_library_paths(std::vector<std::string>&& library_paths) { |
| 81 | default_library_paths_ = library_paths; |
| 82 | } |
| 83 | |
| 84 | const std::vector<std::string>& get_permitted_paths() const { |
| 85 | return permitted_paths_; |
| 86 | } |
| 87 | void set_permitted_paths(std::vector<std::string>&& permitted_paths) { |
| 88 | permitted_paths_ = permitted_paths; |
| 89 | } |
| 90 | |
Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 91 | const std::vector<android_namespace_link_t>& linked_namespaces() const { |
| 92 | return linked_namespaces_; |
| 93 | } |
| 94 | void add_linked_namespace(android_namespace_t* linked_namespace, |
| 95 | const std::unordered_set<std::string>& shared_lib_sonames) { |
| 96 | linked_namespaces_.push_back(android_namespace_link_t(linked_namespace, shared_lib_sonames)); |
| 97 | } |
| 98 | |
Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 99 | void add_soinfo(soinfo* si) { |
| 100 | soinfo_list_.push_back(si); |
| 101 | } |
| 102 | |
| 103 | void add_soinfos(const soinfo_list_t& soinfos) { |
| 104 | for (auto si : soinfos) { |
| 105 | add_soinfo(si); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void remove_soinfo(soinfo* si) { |
| 110 | soinfo_list_.remove_if([&](soinfo* candidate) { |
| 111 | return si == candidate; |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | const soinfo_list_t& soinfo_list() const { return soinfo_list_; } |
| 116 | |
| 117 | // For isolated namespaces - checks if the file is on the search path; |
| 118 | // always returns true for not isolated namespace. |
| 119 | bool is_accessible(const std::string& path); |
| 120 | |
Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame^] | 121 | // Returns true if si is accessible from this namespace. A soinfo |
| 122 | // is considered accessible when it belongs to this namespace |
| 123 | // or one of it's parent soinfos belongs to this namespace. |
| 124 | bool is_accessible(soinfo* si); |
| 125 | |
Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 126 | private: |
| 127 | const char* name_; |
| 128 | bool is_isolated_; |
| 129 | std::vector<std::string> ld_library_paths_; |
| 130 | std::vector<std::string> default_library_paths_; |
| 131 | std::vector<std::string> permitted_paths_; |
Dimitry Ivanov | 7d429d3 | 2017-02-01 15:28:52 -0800 | [diff] [blame] | 132 | // Loader looks into linked namespace if it was not able |
| 133 | // to find a library in this namespace. Note that library |
| 134 | // lookup in linked namespaces are limited by the list of |
| 135 | // shared sonames. |
| 136 | std::vector<android_namespace_link_t> linked_namespaces_; |
Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 137 | soinfo_list_t soinfo_list_; |
| 138 | |
| 139 | DISALLOW_COPY_AND_ASSIGN(android_namespace_t); |
| 140 | }; |
| 141 | |
| 142 | #endif /* __LINKER_NAMESPACES_H */ |