/src/trafficserver/include/proxy/http/remap/PluginDso.h
Line | Count | Source |
1 | | /** @file |
2 | | |
3 | | Header file for a class that deals with plugin Dynamic Shared Objects (DSO) |
4 | | |
5 | | @par License |
6 | | |
7 | | Licensed to the Apache Software Foundation (ASF) under one |
8 | | or more contributor license agreements. See the NOTICE file |
9 | | distributed with this work for additional information |
10 | | regarding copyright ownership. The ASF licenses this file |
11 | | to you under the Apache License, Version 2.0 (the |
12 | | "License"); you may not use this file except in compliance |
13 | | with the License. You may obtain a copy of the License at |
14 | | |
15 | | http://www.apache.org/licenses/LICENSE-2.0 |
16 | | |
17 | | Unless required by applicable law or agreed to in writing, software |
18 | | distributed under the License is distributed on an "AS IS" BASIS, |
19 | | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | See the License for the specific language governing permissions and |
21 | | limitations under the License. |
22 | | |
23 | | @par Details |
24 | | |
25 | | Implements code necessary for Reverse Proxy which mostly consists of |
26 | | general purpose hostname substitution in URLs. |
27 | | |
28 | | */ |
29 | | |
30 | | #pragma once |
31 | | |
32 | | #include "swoc/swoc_file.h" |
33 | | |
34 | | #include <unordered_map> |
35 | | #include <dlfcn.h> |
36 | | #include <vector> |
37 | | #include <forward_list> |
38 | | #include <ctime> |
39 | | #include <atomic> |
40 | | |
41 | | #include "swoc/IntrusiveDList.h" |
42 | | |
43 | | #include "ts/apidefs.h" |
44 | | #include "ts/remap.h" |
45 | | |
46 | | namespace fs = swoc::file; |
47 | | |
48 | | #include "tscore/Ptr.h" |
49 | | #include "tsutil/Metrics.h" |
50 | | #include "iocore/eventsystem/EventSystem.h" |
51 | | |
52 | | #include "proxy/Plugin.h" |
53 | | #include "proxy/PluginThreadContext.h" |
54 | | |
55 | | #include <string> |
56 | | #include <string_view> |
57 | | |
58 | | class PluginDso : public PluginThreadContext |
59 | | { |
60 | | friend class PluginFactory; |
61 | | |
62 | | public: |
63 | | PluginDso(const fs::path &configPath, const fs::path &effectivePath, const fs::path &runtimePath); |
64 | | virtual ~PluginDso(); |
65 | | |
66 | | /* DSO Load, unload, get symbols from DSO */ |
67 | | virtual bool load(std::string &error, const fs::path &compilerPath); |
68 | | virtual bool unload(std::string &error); |
69 | | bool isLoaded(); |
70 | | bool getSymbol(const char *symbol, void *&address, std::string &error) const; |
71 | | |
72 | | /* Accessors for effective and runtime paths */ |
73 | | const fs::path &effectivePath() const; |
74 | | const fs::path &runtimePath() const; |
75 | | swoc::file::file_time_type modTime() const; |
76 | | void *dlOpenHandle() const; |
77 | | void * |
78 | | dlh() const |
79 | 0 | { |
80 | 0 | return _dlh; |
81 | 0 | } |
82 | | |
83 | | /* List used by the plugin factory */ |
84 | | using self_type = PluginDso; ///< Self reference type. |
85 | | self_type *_next = nullptr; |
86 | | self_type *_prev = nullptr; |
87 | | using Linkage = swoc::IntrusiveLinkage<self_type>; |
88 | | using PluginList = swoc::IntrusiveDList<PluginDso::Linkage>; |
89 | | |
90 | | /* Methods to be called when processing a list of plugins, to be overloaded by the remap or the global plugins correspondingly */ |
91 | | virtual void indicatePreReload() = 0; |
92 | | virtual void indicatePostReload(TSRemapReloadStatus reloadStatus) = 0; |
93 | | virtual bool init(std::string &error) = 0; |
94 | | virtual void done() = 0; |
95 | | |
96 | | void acquire() override; |
97 | | void release() override; |
98 | | |
99 | | void incInstanceCount(); |
100 | | void decInstanceCount(); |
101 | | int instanceCount() const; |
102 | | bool isDynamicReloadEnabled() const; |
103 | | |
104 | | class LoadedPlugins : public RefCountObjInHeap |
105 | | { |
106 | | public: |
107 | 0 | LoadedPlugins() : _mutex(new_ProxyMutex()) {} |
108 | | void add(PluginDso *plugin); |
109 | | void remove(PluginDso *plugin); |
110 | | PluginDso *findByEffectivePath(const fs::path &path, bool dynamicReloadEnabled); |
111 | | void indicatePreReload(const char *factoryId); |
112 | | void indicatePostReload(bool reloadSuccessful, const std::unordered_map<PluginDso *, int> &pluginUsed, const char *factoryId); |
113 | | |
114 | | /** |
115 | | * @brief Check if the opt out table contains the passed plugin's effective path. |
116 | | * @return true If the plugin was marked to not participate in the dynamic reload by |
117 | | * TSPluginDSOReloadEnable() API. |
118 | | * @param effectivePath canonical value of the plugin's path. |
119 | | */ |
120 | | bool isPluginInDsoOptOutTable(const fs::path &effectivePath); |
121 | | /** |
122 | | * @brief Add the plugin's path to the opt out table in order to let the Plugin Factory |
123 | | * that this plugin is not interested in taking part of the dynamic reloading. |
124 | | * This function will store the plugin's canonical path. |
125 | | * @param[in] pluginPath Plugin's path. |
126 | | * @return false if any errors converting the plugin's path to a canonical path, true otherwise. |
127 | | */ |
128 | | bool addPluginPathToDsoOptOutTable(std::string_view pluginPath); |
129 | | /** |
130 | | * @brief Removes the passed plugin's effective path from the opt out list. |
131 | | * @note This is mostly used by unit test than needs to remove the plugin's effectivePath |
132 | | * from the opt out list. |
133 | | * @param pluginPath plugin's path. |
134 | | * @note This function is mainly for unit tests purposes. |
135 | | */ |
136 | | void removePluginPathFromDsoOptOutTable(std::string_view pluginPath); |
137 | | |
138 | | private: |
139 | | PluginList _list; /** @brief plugin list */ |
140 | | Ptr<ProxyMutex> _mutex; /** @brief mutex used when updating the plugin list from multiple threads */ |
141 | | |
142 | | struct DisableDSOReloadPluginInfo { |
143 | | fs::path dsoEffectivePath; |
144 | | }; |
145 | | |
146 | | std::forward_list<DisableDSOReloadPluginInfo> _optoutDsoReloadPlugins; |
147 | | }; |
148 | | |
149 | | static const Ptr<LoadedPlugins> & |
150 | | loadedPlugins() |
151 | 0 | { |
152 | 0 | if (!_plugins) { |
153 | 0 | _plugins = new LoadedPlugins(); |
154 | 0 | } |
155 | 0 | return _plugins; |
156 | 0 | } |
157 | | |
158 | | protected: |
159 | | void clean(std::string &error); |
160 | | |
161 | | fs::path _configPath; /** @brief the name specified in the config file */ |
162 | | fs::path _effectivePath; /** @brief the plugin installation path which was used to load DSO */ |
163 | | fs::path _runtimePath; /** @brief the plugin runtime path where the plugin was copied to be loaded */ |
164 | | |
165 | | void *_dlh = nullptr; /** @brief dlopen handler used internally in this class, used as flag for loaded vs unloaded (nullptr) */ |
166 | | |
167 | | static constexpr const char *const _tag = "plugin_dso"; /** @brief log tag used by this class */ |
168 | | static const DbgCtl &_dbg_ctl(); |
169 | | swoc::file::file_time_type _mtime{fs::file_time_type::min()}; /* @brief modification time of the DSO's file, used for checking */ |
170 | | static Ptr<LoadedPlugins> _plugins; /** @brief a global list of plugins */ |
171 | | std::atomic<int> _instanceCount{0}; /** @brief used for properly calling "done" and "indicate config reload" */ |
172 | | }; |
173 | | |
174 | | inline const DbgCtl & |
175 | | PluginDso::_dbg_ctl() |
176 | 0 | { |
177 | 0 | static DbgCtl dc{_tag}; |
178 | |
|
179 | 0 | return dc; |
180 | 0 | } |