GCC Code Coverage Report


Directory: ./
File: Plugins.cpp
Date: 2026-12-01 12:27:28
Exec Total Coverage
Lines: 778 850 91.5%
Functions: 82 83 98.8%
Branches: 927 1700 54.5%

Line Branch Exec Source
1 /*
2 * This file is part of the Falcon Player (FPP) and is Copyright (C)
3 * 2013-2022 by the Falcon Player Developers.
4 *
5 * The Falcon Player (FPP) is free software, and is covered under
6 * multiple Open Source licenses. Please see the included 'LICENSES'
7 * file for descriptions of what files are covered by each license.
8 *
9 * This source file is covered under the LGPL v2.1 as described in the
10 * included LICENSE.LGPL file.
11 */
12
13 #include "fpp-pch.h"
14
15 #include "fpp-json.h"
16
17 #include <sys/stat.h>
18 #include <sys/wait.h>
19 #include <poll.h>
20 #include <signal.h>
21 #include <cstring>
22 #include <dirent.h>
23 #include <dlfcn.h>
24 #include <errno.h>
25 #include <algorithm>
26 #include <chrono>
27 #include <thread>
28 #include <list>
29 #include <map>
30 #include <memory>
31 #include <condition_variable>
32 #include <mutex>
33 #include <optional>
34 #include <set>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string>
38 #include <unistd.h>
39 #include <vector>
40
41 #include "FileMonitor.h"
42 #include "Plugin.h"
43 #include "Warnings.h"
44 #include "common.h"
45 #include "config.h"
46 #include "log.h"
47 #include "mediadetails.h"
48 #include "settings.h"
49 #include "commands/Commands.h"
50
51 #include "CurlManager.h"
52 #include "EPollManager.h"
53 #include "Timers.h"
54 #include "Plugins.h"
55
56 PluginManager PluginManager::INSTANCE;
57
58 // mChannelDataPlugins is the one plugin registry walked off the main loop: the
59 // channel output thread calls modifySequenceData()/modifyChannelData() every
60 // frame, while a runtime load or unload edits the list on the main loop and an
61 // unload then deletes the plugin. The output thread holds this across its whole
62 // walk, so once detachPlugin() has taken it and dropped a plugin, no frame is
63 // still inside that plugin's code and it is safe to destroy. File-static rather
64 // than a member so PluginManager's layout is unchanged for plugins.
65 static std::mutex channelDataPluginsLock;
66
67 // mPlaylistPlugins is walked from whichever thread made the playlist or media
68 // transition - the main loop, an HTTP or MQTT thread, a scheduler command -
69 // while a runtime load or unload edits it on the main loop and an unload then
70 // deletes the plugin. A walk copies the list under this lock, then, just
71 // before each call, checks under it again that the plugin is still listed and
72 // counts it as in flight for that one call; finishUnload() waits for a
73 // plugin's count to drain before deleting it. Checking per call matters: an
74 // unload that lands mid-walk must keep the plugins later in the copy from
75 // being entered after their shutdown(), and must wait only on a call that is
76 // really running, not on a slow callback into some other plugin. Nothing is
77 // held across the calls themselves: script plugins block in waitpid() for
78 // seconds, and a callback can start another transition, which walks the list
79 // again on the same thread. File-static, like channelDataPluginsLock, so
80 // PluginManager's layout is unchanged for plugins.
81 static std::mutex playlistPluginsLock;
82 static std::condition_variable playlistPluginCallDone;
83 static std::map<FPPPlugins::Plugin*, int> playlistPluginCallsInFlight;
84
85 // Plugins whose settings-file watch detachPlugin() has already removed. The
86 // watch is registered under the plugin's name, and a runtime reload constructs
87 // the new instance -- registering its watch under that same name -- while the
88 // old one is still waiting to be deleted. The old one's destructor removing the
89 // watch again would take the new instance's with it, and the reloaded plugin
90 // would never hear of a settings change until fppd restarted. File-static, like
91 // the locks above, so Plugin's layout is unchanged for plugins.
92 static std::mutex settingsUnwatchedLock;
93 static std::set<const FPPPlugins::Plugin*> settingsUnwatched;
94
95 template<class F>
96 90664 static void ForEachPlaylistPlugin(const std::vector<FPPPlugins::PlaylistEventPlugin*>& plugins, F&& f) {
97 90664 std::vector<FPPPlugins::PlaylistEventPlugin*> snapshot;
98 {
99
1/2
✓ Branch 0 taken 45332 times.
✗ Branch 1 not taken.
90664 std::lock_guard<std::mutex> lock(playlistPluginsLock);
100
1/2
✓ Branch 0 taken 45332 times.
✗ Branch 1 not taken.
90664 snapshot = plugins;
101 90664 }
102
2/2
✓ Branch 0 taken 197 times.
✓ Branch 1 taken 45330 times.
91436 for (auto a : snapshot) {
103 394 FPPPlugins::Plugin* key = nullptr;
104 {
105
1/2
✓ Branch 0 taken 197 times.
✗ Branch 1 not taken.
394 std::lock_guard<std::mutex> lock(playlistPluginsLock);
106
3/4
✓ Branch 0 taken 197 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 193 times.
394 if (std::find(plugins.begin(), plugins.end(), a) == plugins.end()) {
107 8 continue; // unloaded since the copy was taken, maybe freed
108 }
109 // Only now: the cast reads the object, which is live only while
110 // it is still listed.
111
1/2
✓ Branch 0 taken 193 times.
✗ Branch 1 not taken.
386 key = dynamic_cast<FPPPlugins::Plugin*>(a);
112
1/2
✓ Branch 0 taken 193 times.
✗ Branch 1 not taken.
386 playlistPluginCallsInFlight[key]++;
113
2/2
✓ Branch 0 taken 193 times.
✓ Branch 1 taken 4 times.
394 }
114 struct Done {
115 FPPPlugins::Plugin* key;
116 191 ~Done() {
117 191 std::lock_guard<std::mutex> lock(playlistPluginsLock);
118 191 auto it = playlistPluginCallsInFlight.find(key);
119 191 if (it != playlistPluginCallsInFlight.end() && --it->second <= 0) {
120 135 playlistPluginCallsInFlight.erase(it);
121 135 playlistPluginCallDone.notify_all();
122 }
123 191 }
124 386 } done{ key };
125
1/2
✓ Branch 0 taken 191 times.
✗ Branch 1 not taken.
386 f(a);
126 }
127 90660 }
128
129 namespace FPPPlugins
130 {
131 1953 static std::map<std::string, std::string> parseSettingsFile(const std::string& dname) {
132 1953 std::map<std::string, std::string> settings;
133
3/4
✓ Branch 0 taken 1953 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 1947 times.
1953 if (FileExists(dname)) {
134
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 FILE* file = fopen(dname.c_str(), "r");
135
136
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (file != NULL) {
137 6 char* line = NULL;
138 6 size_t len = 0;
139 ssize_t read;
140 6 int sIndex = 0;
141
3/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 6 times.
14 while ((read = getline(&line, &len, file)) != -1) {
142
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 8 times.
8 if ((!line) || (!read) || (read == 1)) {
143 ✗ continue;
144 }
145
146 // Split on the first '=' only and drop exactly one pair of
147 // surrounding quotes, as LoadSettings() reads FPP's own
148 // settings file: splitting on every '=' cut values such as
149 // base64 secrets short, and every quote was trimmed.
150
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 std::string l(line, read);
151 8 size_t eq = l.find('=');
152
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 char* key = trimwhitespace(l.substr(0, eq).c_str());
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!strlen(key)) {
154 ✗ free(key);
155 ✗ continue;
156 }
157
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (eq == std::string::npos) {
158 ✗ fprintf(stderr, "Error tokenizing value for %s setting\n", key);
159 ✗ free(key);
160 ✗ continue;
161 }
162
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 std::string value = l.substr(eq + 1);
163
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 TrimWhiteSpace(value);
164
4/8
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 8 times.
✗ Branch 7 not taken.
8 if (value.size() >= 2 && value.front() == '"' && value.back() == '"') {
165
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 value = value.substr(1, value.size() - 2);
166 }
167
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 settings[key] = value;
168 8 free(key);
169
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 }
170
171
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 if (line) {
172 6 free(line);
173 }
174
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 fclose(file);
175 }
176 }
177 1953 return settings;
178 ✗ }
179
180 1949 Plugin::Plugin(const std::string& n) :
181 1949 name(n) {
182
4/8
✓ Branch 0 taken 1949 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1949 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1949 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1949 times.
✗ Branch 7 not taken.
3617 std::string dname = FPP_DIR_CONFIG("/plugin." + name);
183
1/2
✓ Branch 0 taken 1949 times.
✗ Branch 1 not taken.
1949 settings = parseSettingsFile(dname);
184 1949 }
185 2 Plugin::Plugin(const std::string& n, bool monitorSettings) :
186 2 name(n) {
187
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (monitorSettings) {
188
4/8
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
4 std::string dname = FPP_DIR_CONFIG("/plugin." + name);
189
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 settings = parseSettingsFile(dname);
190
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 FileMonitor::INSTANCE.AddFile(name, dname, [this, dname]() {
191
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 std::map<std::string, std::string> newSettings = parseSettingsFile(dname);
192
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (auto& s : newSettings) {
193
6/10
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
2 if (settings.find(s.first) == settings.end() || settings[s.first] != s.second) {
194
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogInfo(VB_PLUGIN, "Plugin %s setting %s changed to %s\n", name.c_str(), s.first.c_str(), s.second.c_str());
195
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 settings[s.first] = s.second;
196
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 settingChanged(s.first, s.second);
197 }
198 }
199 2 });
200 2 }
201 2 }
202
203 3894 Plugin::~Plugin() {
204 {
205 3894 std::lock_guard<std::mutex> lock(settingsUnwatchedLock);
206
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 1907 times.
3894 if (settingsUnwatched.erase(this)) {
207 80 return;
208 }
209
2/2
✓ Branch 0 taken 1907 times.
✓ Branch 1 taken 40 times.
3894 }
210 7628 FileMonitor::INSTANCE.RemoveFile(name, FPP_DIR_CONFIG("/plugin." + name));
211
4/4
✓ Branch 0 taken 1907 times.
✓ Branch 1 taken 40 times.
✓ Branch 2 taken 1907 times.
✓ Branch 3 taken 40 times.
3974 }
212
213 ✗ void Plugin::reloadSettings() {
214 ✗ std::string dname = FPP_DIR_CONFIG("/plugin." + name);
215 ✗ settings = parseSettingsFile(dname);
216 ✗ }
217 }
218 class LifecycleCallback {
219 public:
220 51 LifecycleCallback(const std::string& name, const std::string& filename) :
221 51 mName(name),
222
1/2
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
51 mFilename(filename) {
223 51 }
224 200 virtual ~LifecycleCallback() {}
225
226 void run(const std::string& lifecycle);
227
228 private:
229 std::string mName;
230 std::string mFilename;
231 };
232
233 class MediaCallback {
234 public:
235 6 MediaCallback(const std::string& name, const std::string& filename) :
236 6 mName(name),
237
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 mFilename(filename) {
238 6 }
239 24 virtual ~MediaCallback() {}
240
241 void run(const Json::Value& playlist, const MediaDetails& mediaDetails);
242
243 private:
244 std::string mName;
245 std::string mFilename;
246 };
247
248 class PlaylistCallback {
249 public:
250 55 PlaylistCallback(const std::string& name, const std::string& filename) :
251 55 mName(name),
252
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 mFilename(filename) {
253 55 }
254 212 virtual ~PlaylistCallback() {}
255
256 void run(const Json::Value& playlist, const std::string& action, const std::string& section, int idx);
257
258 private:
259 std::string mName;
260 std::string mFilename;
261 };
262
263 extern MediaDetails mediaDetails;
264
265 const char* type_to_string[] = {
266 "both",
267 "media",
268 "sequence",
269 "pause",
270 "video",
271 };
272
273 class ScriptFPPPlugin : public FPPPlugins::Plugin, public FPPPlugins::PlaylistEventPlugin {
274 public:
275 220 ScriptFPPPlugin(const std::string& n, const std::string& filename, const std::string& lst) :
276
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 FPPPlugins::Plugin(n), FPPPlugins::PlaylistEventPlugin(), fileName(filename) {
277
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 std::vector<std::string> types = split(lst, ',');
278
2/2
✓ Branch 0 taken 269 times.
✓ Branch 1 taken 220 times.
489 for (int i = 0; i < types.size(); i++) {
279
3/4
✓ Branch 0 taken 269 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 263 times.
269 if (types[i] == "media") {
280
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 LogDebug(VB_PLUGIN, "Plugin %s supports media callback.\n", name.c_str());
281
3/8
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
6 m_mediaCallback = new MediaCallback(name, filename);
282
3/4
✓ Branch 0 taken 263 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✓ Branch 3 taken 208 times.
263 } else if (types[i] == "playlist") {
283
1/2
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
55 LogDebug(VB_PLUGIN, "Plugin %s supports playlist callback.\n", name.c_str());
284
3/8
✓ Branch 0 taken 55 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 55 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 55 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
55 m_playlistCallback = new PlaylistCallback(name, filename);
285
3/4
✓ Branch 0 taken 208 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 157 times.
208 } else if (types[i] == "lifecycle") {
286
3/8
✓ Branch 0 taken 51 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 51 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
51 m_lifecycleCallback = new LifecycleCallback(name, filename);
287 } else {
288
1/2
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
157 otherTypes.push_back(types[i]);
289 }
290 }
291
2/4
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 220 times.
✗ Branch 3 not taken.
220 lifecycleCallback("startup");
292 220 }
293 872 virtual ~ScriptFPPPlugin() {
294 436 lifecycleCallback("shutdown");
295
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 212 times.
436 if (m_mediaCallback)
296
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 delete m_mediaCallback;
297
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 165 times.
436 if (m_playlistCallback)
298
1/2
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
106 delete m_playlistCallback;
299
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 168 times.
436 if (m_lifecycleCallback)
300
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
100 delete m_lifecycleCallback;
301 872 }
302
303 220 bool hasCallback() const {
304
6/6
✓ Branch 0 taken 214 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 160 times.
✓ Branch 3 taken 54 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 157 times.
220 return m_mediaCallback != nullptr || m_playlistCallback != nullptr || m_lifecycleCallback != nullptr;
305 }
306
307 157 const std::list<std::string>& getOtherTypes() const {
308 157 return otherTypes;
309 }
310
311 15 virtual void mediaCallback(const Json::Value& playlist, const MediaDetails& mediaDetails) override {
312
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 4 times.
15 if (m_mediaCallback) {
313 11 m_mediaCallback->run(playlist, mediaDetails);
314 }
315 15 }
316 170 virtual void playlistCallback(const Json::Value& playlist, const std::string& action, const std::string& section, int item) override {
317
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 17 times.
170 if (m_playlistCallback) {
318 153 m_playlistCallback->run(playlist, action, section, item);
319 }
320 168 }
321 438 virtual void lifecycleCallback(const std::string& lifecycle) {
322
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 337 times.
438 if (m_lifecycleCallback) {
323 101 m_lifecycleCallback->run(lifecycle);
324 }
325 438 }
326
327 private:
328 const std::string fileName;
329
330 std::list<std::string> otherTypes;
331 MediaCallback* m_mediaCallback = nullptr;
332 PlaylistCallback* m_playlistCallback = nullptr;
333 LifecycleCallback* m_lifecycleCallback = nullptr;
334 };
335
336 3252 PluginManager::PluginManager() {
337 3252 }
338
339 class ScriptCommand : public Command {
340 public:
341 7 ScriptCommand(const std::string& dir, Json::Value& json) :
342
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
14 Command(json["name"].asString()),
343 7 directory(dir),
344
4/8
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 7 times.
✗ Branch 7 not taken.
21 description(json) {
345
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 script = json["script"].asString();
346
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 description.removeMember("script");
347 7 }
348 7 bool IsOk() {
349
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 return FileExists(directory + "/" + script);
350 }
351 2 virtual Json::Value getDescription() override {
352 2 return description;
353 }
354
355 8 virtual std::unique_ptr<Result> run(const std::vector<std::string>& args) override {
356 // Everything the child needs is built before the fork: fppd is
357 // threaded, and a child that allocates or logs can block forever on a
358 // lock another thread held at the fork (see PlaylistCallback::run()).
359
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 std::string eventScript = directory + "/" + script;
360
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
16 std::string mediaDir = "MEDIADIR=" + getFPPMediaDir();
361
3/6
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 8 times.
✗ Branch 5 not taken.
8 std::string fppDir = "FPPDIR=" + getFPPDDir();
362
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 std::string pluginDir = "SCRIPTDIR=" + directory;
363
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
16 std::vector<const char*> envs = { mediaDir.c_str(), fppDir.c_str(), pluginDir.c_str(), nullptr };
364 8 std::vector<const char*> sargs;
365
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 sargs.push_back(eventScript.c_str());
366
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
10 for (auto& a : args) {
367
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 sargs.push_back(a.c_str());
368 }
369
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 sargs.push_back(nullptr);
370
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 LogDebug(VB_PLUGIN, "Calling %s for command: %s\n", script.c_str(), name.c_str());
371
372 8 int pid = fork();
373
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 14 times.
15 if (pid == -1) {
374 // Usually out of memory. This runs on whatever thread ran the
375 // command, so exiting took all of fppd down for one plugin call.
376
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 LogErr(VB_PLUGIN, "Failed to fork for plugin command %s: %s\n", name.c_str(), FPPstrerror(errno));
377
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 return std::make_unique<Command::ErrorResult>(name + " failed: could not start the script");
378 }
379
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 if (pid == 0) {
380 7 execve(eventScript.c_str(), (char* const*)&sargs[0], (char* const*)&envs[0]);
381 7 _exit(EXIT_FAILURE);
382 }
383
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 LogExcess(VB_PLUGIN, "Command parent process, resuming work.\n");
384
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 waitpid(pid, nullptr, 0);
385
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 return std::make_unique<Command::Result>(name + " complete");
386 8 }
387
388 std::string directory;
389 std::string script;
390 Json::Value description;
391 };
392
393 namespace {
394 // Attributes to a plugin every command that appears in the registry while this
395 // object is alive. See mPluginCommands in Plugins.h for why ownership is
396 // deduced this way rather than reported. Scoped rather than open-coded because
397 // the load path it wraps has several early returns.
398 class CommandRecorder {
399 public:
400 276 CommandRecorder(std::map<std::string, std::vector<std::pair<std::string, Command*>>>& into,
401 276 const std::string& plugin) :
402 276 mInto(into),
403 276 mPlugin(plugin) {
404
3/4
✓ Branch 0 taken 276 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16519 times.
✓ Branch 3 taken 276 times.
16795 for (const auto& c : CommandManager::INSTANCE.getRegisteredCommands()) {
405
1/2
✓ Branch 0 taken 16519 times.
✗ Branch 1 not taken.
16519 mBefore.insert(c.first);
406 276 }
407 276 }
408 276 ~CommandRecorder() {
409 276 std::vector<std::pair<std::string, Command*>> added;
410
2/2
✓ Branch 0 taken 16665 times.
✓ Branch 1 taken 276 times.
16941 for (const auto& c : CommandManager::INSTANCE.getRegisteredCommands()) {
411
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 16519 times.
16665 if (mBefore.find(c.first) == mBefore.end()) {
412 146 added.push_back(c);
413 }
414 276 }
415
2/2
✓ Branch 0 taken 73 times.
✓ Branch 1 taken 203 times.
276 if (!added.empty()) {
416 73 auto& slot = mInto[mPlugin];
417 73 slot.insert(slot.end(), added.begin(), added.end());
418 73 LogDebug(VB_PLUGIN, "Plugin %s registered %d command(s)\n", mPlugin.c_str(), (int)added.size());
419 }
420 276 }
421 CommandRecorder(const CommandRecorder&) = delete;
422 CommandRecorder& operator=(const CommandRecorder&) = delete;
423
424 private:
425 std::map<std::string, std::vector<std::pair<std::string, Command*>>>& mInto;
426 std::string mPlugin;
427 std::set<std::string> mBefore;
428 };
429 } // namespace
430
431 // Registers the commands a plugin declares in commands/descriptions.json. These
432 // are FPP-owned objects describing scripts in the plugin's directory, and an
433 // uninstalled plugin should not leave its commands listed - but withdrawing
434 // them again is not this function's job. They go into the registry through
435 // addCommand() like any other, so the CommandRecorder wrapping the load
436 // attributes them to the plugin and unloadPlugin() takes them back from there.
437 223 static void LoadPluginCommands(const std::string& dir) {
438
5/10
✓ Branch 0 taken 223 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 223 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 223 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 223 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 223 times.
✗ Branch 9 not taken.
223 std::string commandDir = FPP_DIR_PLUGIN("/" + dir + "/commands/");
439
1/2
✓ Branch 0 taken 223 times.
✗ Branch 1 not taken.
223 std::string descriptions = commandDir + "/descriptions.json";
440
3/4
✓ Branch 0 taken 223 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✓ Branch 3 taken 216 times.
223 if (FileExists(descriptions)) {
441
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 Json::Value json = LoadJsonFromFile(descriptions, JsonRoot::Array);
442
3/4
✓ Branch 0 taken 21 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✓ Branch 3 taken 7 times.
21 for (Json::ArrayIndex x = 0; x < json.size(); x++) {
443
2/4
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
✗ Branch 3 not taken.
14 Json::Value jscmd = json[x];
444 // Read at fppd startup: an entry ScriptCommand can't read (not an
445 // object, or a name or script that isn't a string) is skipped, not
446 // thrown on -- one plugin's typo would otherwise stop fppd starting.
447
14/22
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 9 times.
✓ Branch 9 taken 2 times.
✓ Branch 10 taken 9 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 9 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 9 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 9 times.
✓ Branch 17 taken 5 times.
✓ Branch 18 taken 7 times.
✓ Branch 19 taken 7 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
23 if (!jscmd.isObject() || !jscmd["name"].isString() || jscmd["name"].asString().empty() ||
448
4/6
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 7 times.
9 !jscmd["script"].isString()) {
449
3/6
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 7 times.
✗ Branch 5 not taken.
7 WarningHolder::InvalidConfigEntry("plugins/" + dir + "/commands/descriptions.json",
450
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
14 "command " + std::to_string(x + 1) + " needs a name and a script");
451 7 continue;
452 }
453
3/8
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 7 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
7 ScriptCommand* cmd = new ScriptCommand(commandDir, jscmd);
454
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 if (cmd->IsOk()) {
455
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 CommandManager::INSTANCE.addCommand(cmd);
456 } else {
457 ✗ delete cmd;
458 }
459
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 }
460 7 }
461 223 }
462
463 2651 void PluginManager::init() {
464 2651 }
465 2651 void PluginManager::loadUserPlugins() {
466 DIR* dp;
467 struct dirent* ep;
468
469
4/8
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2651 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2651 times.
✗ Branch 7 not taken.
2651 dp = opendir(FPP_DIR_PLUGIN("").c_str());
470
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 if (dp != NULL) {
471
2/2
✓ Branch 0 taken 5490 times.
✓ Branch 1 taken 2651 times.
8141 while ((ep = readdir(dp))) {
472 // Skip ".", "..", and hidden entries. The test is on the LEADING
473 // character: a plugin directory is normally named "fpp-Something"
474 // and contains no dot at all, so keying off "has a dot anywhere"
475 // skips every real plugin instead of the three entries meant to be
476 // excluded.
477
2/2
✓ Branch 0 taken 5302 times.
✓ Branch 1 taken 188 times.
5490 if (ep->d_name[0] == '.') {
478 5305 continue;
479 }
480 struct stat statbuf;
481
4/8
✓ Branch 0 taken 188 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 188 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 188 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 188 times.
✗ Branch 7 not taken.
374 std::string dname = FPP_DIR_PLUGIN("/" + ep->d_name);
482 188 lstat(dname.c_str(), &statbuf);
483
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 185 times.
188 if (!S_ISDIR(statbuf.st_mode)) {
484
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 dname += "/.linkOK"; // Allow developers to use symlinks if desired
485
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 if (!FileExists(dname))
486 3 continue;
487 }
488
4/6
✓ Branch 0 taken 185 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 185 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 141 times.
✓ Branch 5 taken 44 times.
555 if (mLoadedUserPlugins.find(ep->d_name) == mLoadedUserPlugins.end()) {
489
2/4
✓ Branch 0 taken 141 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 141 times.
✗ Branch 3 not taken.
284 loadUserPlugin(ep->d_name);
490 }
491
2/2
✓ Branch 0 taken 185 times.
✓ Branch 1 taken 3 times.
188 }
492 2651 closedir(dp);
493 } else {
494 ✗ LogWarn(VB_PLUGIN, "Couldn't open the directory %s: (%d): %s\n", FPP_DIR_PLUGIN("").c_str(), errno, FPPstrerror(errno));
495 }
496
497 // After an FPPOS reflash the boot code sets pluginReinstallNeededAfterOS to
498 // "1" if plugins were present. The Plugin Manager turns that into the list
499 // of plugins still to reinstall and clears it once every one has been
500 // reinstalled or uninstalled. Surface the reinstall prompt, and keep it in
501 // sync while fppd is running. Native plugins still on the list were not
502 // loaded above - see loadUserPlugin().
503 static bool reinstallListenerRegistered = false;
504
1/2
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
2651 if (!reinstallListenerRegistered) {
505 2651 reinstallListenerRegistered = true;
506
3/6
✓ Branch 0 taken 2651 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2651 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2651 times.
✗ Branch 5 not taken.
10899 registerSettingsListener("PluginManager", "pluginReinstallNeededAfterOS",
507 5303 [this](const std::string&) { checkPluginReinstallWarning(); });
508 }
509 2651 checkPluginReinstallWarning();
510
511 2651 return;
512 }
513
514 // Warning id shared with the www/warnings-definitions.json "Plugin_Warnings"
515 // entry (drives the icon/grouping); the Fix button itself is rendered from the
516 // fixUrl/fixText carried in the warning data.
517 static constexpr int PLUGIN_REINSTALL_WARNING_ID = 57;
518 // Not only after an FPPOS upgrade: a File Copy restore of plugins sets it too.
519 static const std::string PLUGIN_REINSTALL_WARNING_MSG = "Plugins must be reinstalled";
520
521 2652 void PluginManager::checkPluginReinstallWarning() {
522
4/6
✓ Branch 0 taken 2652 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2652 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 2650 times.
2652 if (getSetting("pluginReinstallNeededAfterOS") != "") {
523
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
8 WarningHolder::AddWarning(PLUGIN_REINSTALL_WARNING_ID, PLUGIN_REINSTALL_WARNING_MSG,
524 "plugins.php?action=reinstallAll", "Reinstall All Plugins");
525 } else {
526
2/4
✓ Branch 0 taken 2650 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2650 times.
✗ Branch 3 not taken.
5300 WarningHolder::RemoveWarning(PLUGIN_REINSTALL_WARNING_ID, PLUGIN_REINSTALL_WARNING_MSG);
527 }
528 2652 }
529
530 600 PluginManager::~PluginManager() {
531 600 Cleanup();
532 600 }
533 3220 void PluginManager::Cleanup() {
534 // Give API-providing plugins their unregister callback before destroying
535 // them: that is where they remove (and may delete) the Command objects
536 // they added to CommandManager. Without this, those commands are still
537 // registered when CommandManager::Cleanup() bulk-deletes everything it
538 // holds, and a plugin destructor that also deletes them double-frees.
539
1/2
✓ Branch 0 taken 3220 times.
✗ Branch 1 not taken.
3220 unregisterApis();
540 3220 mAPIProviderPlugins.clear();
541 // Anything still settling from an unload will never get another timer tick
542 // now, so finish it here rather than leaking the object (and, for an opted-in
543 // plugin, the mapping) on the way out.
544
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3220 times.
3220 while (!mPendingUnloads.empty()) {
545 ✗ PendingUnload p = std::move(mPendingUnloads.back());
546 ✗ mPendingUnloads.pop_back();
547 ✗ finishUnload(p);
548 ✗ }
549 // A playlist or media callback can still be inside a plugin on another
550 // thread: the MQTT thread handling a playlist command, or a command thread.
551 // Nothing waited for those, so the deletes below could free a plugin (a
552 // script plugin blocks in waitpid() for as long as its script runs) while
553 // it was still running. Detach them all, as an unload does, and wait for
554 // the calls in flight -- briefly: shutdown has a budget. A plugin still
555 // busy past that is left alive, and so is every plugin library, since its
556 // code may still be running: each path from here ends in exec or _exit.
557 3220 std::set<FPPPlugins::Plugin*> busy;
558 {
559
1/2
✓ Branch 0 taken 3220 times.
✗ Branch 1 not taken.
3220 std::unique_lock<std::mutex> lock(playlistPluginsLock);
560 3220 mPlaylistPlugins.clear();
561
3/4
✓ Branch 0 taken 3220 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3218 times.
3220 if (!playlistPluginCallDone.wait_for(lock, std::chrono::seconds(2), []() {
562 3224 return playlistPluginCallsInFlight.empty();
563 })) {
564
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (auto& [plugin, n] : playlistPluginCallsInFlight) {
565
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 busy.insert(plugin);
566 }
567 }
568 3220 }
569
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3220 times.
3222 for (auto* a : busy) {
570
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogWarn(VB_PLUGIN, "Plugin %s is still in a playlist callback at shutdown; leaving it\n",
571 a->getName().c_str());
572 }
573 // Inbound HTTP is disarmed above and the routes have drained, so no request
574 // is inside plugin code. Now let each plugin stop its own activity - threads,
575 // timers, connections - while it is still a whole object. Destructors are too
576 // late for that: a thread calling in during destruction reads a plugin that
577 // is half gone.
578
2/2
✓ Branch 0 taken 1752 times.
✓ Branch 1 taken 3220 times.
4972 for (auto& a : mPlugins) {
579
3/4
✓ Branch 0 taken 1752 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1750 times.
✓ Branch 3 taken 2 times.
1752 if (!busy.count(a)) {
580
1/2
✓ Branch 0 taken 1750 times.
✗ Branch 1 not taken.
1750 a->shutdown();
581 }
582 }
583 // A request still in flight holds a callback whose code - the
584 // std::function manager that destroys it - lives in the plugin library
585 // that issued it, and the libraries are dlclose()d below. Destroying such a
586 // callback afterwards (fppd's own cancelAll() later in shutdown) jumps into
587 // unmapped memory. Drop them all now, while every library is still mapped,
588 // as unloadPlugin() does for one plugin. Every request, not just the ones
589 // tagged with a plugin's name: an untagged one is just as much the
590 // plugin's, and nothing of FPP's own still needs a reply this late.
591
1/2
✓ Branch 0 taken 3220 times.
✗ Branch 1 not taken.
3220 CurlManager::INSTANCE.cancelAll();
592
2/2
✓ Branch 0 taken 1752 times.
✓ Branch 1 taken 3220 times.
4972 while (!mPlugins.empty()) {
593
3/4
✓ Branch 0 taken 1752 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1750 times.
✓ Branch 3 taken 2 times.
1752 if (!busy.count(mPlugins.back())) {
594
1/2
✓ Branch 0 taken 1750 times.
✗ Branch 1 not taken.
1750 delete mPlugins.back();
595 }
596 1752 mPlugins.pop_back();
597 }
598 3220 mPluginDirNames.clear();
599 // Delete any commands that plugins registered but did not remove in
600 // unregisterApis(). Those commands have vtables in the plugin library.
601 // CommandManager::Cleanup() must run before dlclose() so that the virtual
602 // destructor dispatch doesn't hit unmapped memory. The call is idempotent
603 // (guarded by an atomic flag), so the subsequent call in main() is a no-op.
604
1/2
✓ Branch 0 taken 3220 times.
✗ Branch 1 not taken.
3220 CommandManager::INSTANCE.Cleanup();
605
2/2
✓ Branch 0 taken 3218 times.
✓ Branch 1 taken 2 times.
3220 if (busy.empty()) {
606
2/2
✓ Branch 0 taken 1724 times.
✓ Branch 1 taken 3218 times.
4942 for (auto& a : mShlibHandles) {
607 1724 dlclose(a);
608 }
609 }
610 3220 mShlibHandles.clear();
611 3220 }
612
613 1723 FPPPlugins::Plugin* PluginManager::findPlugin(const std::string& name, const std::string& shlibName) {
614
2/2
✓ Branch 0 taken 1116 times.
✓ Branch 1 taken 1637 times.
2753 for (auto& a : mPlugins) {
615
2/2
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 1030 times.
1116 if (a->getName() == name) {
616 86 return a;
617 }
618 }
619
5/8
✓ Branch 0 taken 1637 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1635 times.
✓ Branch 4 taken 1637 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1637 times.
✗ Branch 7 not taken.
1637 std::string libName = "lib" + (shlibName == "" ? name : shlibName) + SHLIB_EXT;
620
1/2
✓ Branch 0 taken 1637 times.
✗ Branch 1 not taken.
1637 FPPPlugins::Plugin* p = loadSHLIBPlugin(libName, name);
621
2/2
✓ Branch 0 taken 50 times.
✓ Branch 1 taken 1587 times.
1637 if (!p) {
622
16/38
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 50 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 50 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 50 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 50 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 44 times.
✓ Branch 11 taken 6 times.
✓ Branch 12 taken 44 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 44 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 50 times.
✗ Branch 17 not taken.
✓ Branch 18 taken 50 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 50 times.
✗ Branch 21 not taken.
✓ Branch 22 taken 50 times.
✗ Branch 23 not taken.
✓ Branch 24 taken 50 times.
✗ Branch 25 not taken.
✓ Branch 26 taken 44 times.
✓ Branch 27 taken 6 times.
✗ Branch 28 not taken.
✗ Branch 29 not taken.
✗ Branch 30 not taken.
✗ Branch 31 not taken.
✗ Branch 32 not taken.
✗ Branch 33 not taken.
✗ Branch 34 not taken.
✗ Branch 35 not taken.
✗ Branch 36 not taken.
✗ Branch 37 not taken.
150 if (DirectoryExists(FPP_DIR_PLUGIN("/" + name)) && (mLoadedUserPlugins.find(name) == mLoadedUserPlugins.end())) {
623
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
44 loadUserPlugin(name);
624 }
625
2/2
✓ Branch 0 taken 49 times.
✓ Branch 1 taken 6 times.
55 for (auto& a : mPlugins) {
626
2/2
✓ Branch 0 taken 44 times.
✓ Branch 1 taken 5 times.
49 if (a->getName() == name) {
627 44 return a;
628 }
629 }
630 }
631
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1587 times.
1593 if (!p) {
632
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 LogErr(VB_PLUGIN, "Failed to load plugin: %s Error: %s\n", libName.c_str(), dlerror());
633 }
634 1593 return p;
635 1637 }
636 147 bool PluginManager::awaitingReinstallAfterOS(const std::string& dirName) {
637
1/2
✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
147 std::string v = getSetting("pluginReinstallNeededAfterOS");
638
1/2
✓ Branch 0 taken 147 times.
✗ Branch 1 not taken.
147 TrimWhiteSpace(v);
639
2/2
✓ Branch 0 taken 146 times.
✓ Branch 1 taken 1 times.
147 if (v.empty()) {
640 146 return false;
641 }
642 // Boot writes "1" (every installed plugin); the Plugin Manager turns that
643 // into the comma-separated list of plugins still to reinstall.
644
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 if (v == "1") {
645 1 return true;
646 }
647 ✗ for (auto& p : split(v, ',')) {
648 ✗ TrimWhiteSpace(p);
649 ✗ if (p == dirName) {
650 ✗ return true;
651 }
652 ✗ }
653 ✗ return false;
654 147 }
655
656 223 FPPPlugins::Plugin* PluginManager::loadUserPlugin(const std::string& name, bool explicitLoad) {
657
1/2
✓ Branch 0 taken 223 times.
✗ Branch 1 not taken.
223 LogDebug(VB_PLUGIN, "Found Plugin: (%s)\n", name.c_str());
658 // Covers the whole load, so it catches both the commands FPP registers from
659 // the plugin's descriptions.json below and the ones a C++ plugin registers
660 // from its own constructor, which runs inside loadSHLIBPlugin().
661
1/2
✓ Branch 0 taken 223 times.
✗ Branch 1 not taken.
223 CommandRecorder recordCommands(mPluginCommands, name);
662
5/10
✓ Branch 0 taken 223 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 223 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 223 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 223 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 223 times.
✗ Branch 9 not taken.
223 std::string filename = FPP_DIR_PLUGIN("/" + name + "/callbacks");
663 223 bool found = false;
664
665
3/4
✓ Branch 0 taken 223 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 66 times.
✓ Branch 3 taken 157 times.
223 if (FileExists(filename)) {
666
1/2
✓ Branch 0 taken 66 times.
✗ Branch 1 not taken.
66 printf("Found callback with no extension");
667 66 found = true;
668 } else {
669 157 std::vector<std::string> extensions;
670
2/4
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 157 times.
✗ Branch 3 not taken.
314 extensions.push_back(std::string(".sh"));
671
2/4
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 157 times.
✗ Branch 3 not taken.
314 extensions.push_back(std::string(".pl"));
672
2/4
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 157 times.
✗ Branch 3 not taken.
314 extensions.push_back(std::string(".php"));
673
2/4
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 157 times.
✗ Branch 3 not taken.
157 extensions.push_back(std::string(".py"));
674
675
2/2
✓ Branch 0 taken 628 times.
✓ Branch 1 taken 157 times.
785 for (std::vector<std::string>::iterator i = extensions.begin(); i != extensions.end(); ++i) {
676
1/2
✓ Branch 0 taken 628 times.
✗ Branch 1 not taken.
628 std::string tmpFilename = filename + *i;
677
3/4
✓ Branch 0 taken 628 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 155 times.
✓ Branch 3 taken 473 times.
628 if (FileExists(tmpFilename.c_str())) {
678
1/2
✓ Branch 0 taken 155 times.
✗ Branch 1 not taken.
155 filename += *i;
679 155 found = true;
680 }
681 628 }
682 157 }
683 // Registered with CommandManager, so the CommandRecorder above attributes
684 // these to the plugin along with everything else it adds.
685
1/2
✓ Branch 0 taken 223 times.
✗ Branch 1 not taken.
223 LoadPluginCommands(name);
686
687
3/6
✓ Branch 0 taken 223 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 223 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 223 times.
✗ Branch 5 not taken.
223 std::string eventScript = FPP_DIR + "/scripts/eventScript";
688
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 221 times.
223 if (!found) {
689
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogExcess(VB_PLUGIN, "No callbacks supported by plugin: '%s'\n", name.c_str());
690 2 return nullptr;
691 }
692
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 LogDebug(VB_PLUGIN, "Processing Callbacks (%s) for plugin: '%s'\n", filename.c_str(), name.c_str());
693
694 int output_pipe[2], pid, bytes_read;
695 char readbuffer[128];
696
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 std::string callback_list = "";
697
698 // A failure here (out of memory or fds) skips this one plugin rather
699 // than exiting fppd over it.
700
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 221 times.
221 if (pipe(output_pipe) == -1) {
701 ✗ LogErr(VB_PLUGIN, "Plugin %s: failed to make a pipe for %s --list: %s; not loading it\n",
702 name.c_str(), filename.c_str(), FPPstrerror(errno));
703 ✗ return nullptr;
704 }
705
706
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 443 times.
221 if ((pid = fork()) == -1) {
707 ✗ LogErr(VB_PLUGIN, "Plugin %s: failed to fork for %s --list: %s; not loading it\n",
708 name.c_str(), filename.c_str(), FPPstrerror(errno));
709 ✗ close(output_pipe[0]);
710 ✗ close(output_pipe[1]);
711 ✗ return nullptr;
712 }
713
714
2/2
✓ Branch 0 taken 222 times.
✓ Branch 1 taken 221 times.
443 if (pid == 0) {
715 // Its own process group, so a timeout below can end the script and
716 // anything it started.
717 222 setpgid(0, 0);
718 222 dup2(output_pipe[1], STDOUT_FILENO);
719
1/2
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
222 close(output_pipe[1]);
720
1/2
✓ Branch 0 taken 222 times.
✗ Branch 1 not taken.
222 close(output_pipe[0]);
721 222 execl(eventScript.c_str(), "eventScript", filename.c_str(), "--list", NULL);
722 222 _exit(EXIT_FAILURE);
723 } else {
724
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 close(output_pipe[1]);
725
726 // This runs at startup, so a plugin whose script never answers (stuck
727 // on a network, say) would keep fppd from ever starting. Give it a
728 // while, then give up on the plugin.
729 221 constexpr int LIST_TIMEOUT_MS = 10000;
730
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 long long deadline = GetMonotonicTimeMS() + LIST_TIMEOUT_MS;
731 221 bool timedOut = false;
732 while (true) {
733
1/2
✓ Branch 0 taken 441 times.
✗ Branch 1 not taken.
441 long long left = deadline - GetMonotonicTimeMS();
734 441 struct pollfd pfd = { output_pipe[0], POLLIN, 0 };
735
2/4
✓ Branch 0 taken 441 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 441 times.
✗ Branch 3 not taken.
441 int pr = left > 0 ? poll(&pfd, 1, (int)left) : 0;
736
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 441 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
441 if (pr < 0 && errno == EINTR) {
737 ✗ continue;
738 }
739
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 440 times.
441 if (pr <= 0) {
740 1 timedOut = (pr == 0);
741 1 break;
742 }
743
1/2
✓ Branch 0 taken 440 times.
✗ Branch 1 not taken.
440 bytes_read = read(output_pipe[0], readbuffer, sizeof(readbuffer) - 1);
744
745
2/2
✓ Branch 0 taken 220 times.
✓ Branch 1 taken 220 times.
440 if (bytes_read <= 0)
746 220 break;
747
748 220 readbuffer[bytes_read] = '\0';
749
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 callback_list += readbuffer;
750 220 }
751
1/2
✓ Branch 0 taken 221 times.
✗ Branch 1 not taken.
221 close(output_pipe[0]);
752
753
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 220 times.
221 if (timedOut) {
754 1 kill(-pid, SIGKILL);
755
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 waitpid(pid, NULL, 0);
756
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_PLUGIN, "Plugin %s: %s --list did not answer within %d seconds; not loading it\n",
757 name.c_str(), filename.c_str(), LIST_TIMEOUT_MS / 1000);
758
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 WarningHolder::AddWarning(5, "Could not load plugin " + name);
759 1 return nullptr;
760 }
761
762
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 TrimWhiteSpace(callback_list);
763
764
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 LogExcess(VB_PLUGIN, "Callback output: (%s)\n", callback_list.c_str());
765
1/2
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
220 waitpid(pid, NULL, 0);
766 }
767
768
3/8
✓ Branch 0 taken 220 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 220 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 220 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
220 ScriptFPPPlugin* spl = new ScriptFPPPlugin(name, filename, callback_list);
769
2/2
✓ Branch 0 taken 63 times.
✓ Branch 1 taken 157 times.
220 if (spl->hasCallback()) {
770
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 mLoadedUserPlugins.emplace(name);
771
2/4
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 63 times.
✗ Branch 3 not taken.
63 mPluginDirNames[spl] = name;
772
1/2
✓ Branch 0 taken 63 times.
✗ Branch 1 not taken.
63 addPlugin(spl);
773 63 return spl;
774 } else {
775
1/2
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
157 for (auto& a : spl->getOtherTypes()) {
776
3/6
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 157 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 157 times.
✗ Branch 5 not taken.
314 if (startsWith(a, "c++")) {
777
7/14
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 157 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 157 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 157 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 157 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 157 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 157 times.
✗ Branch 13 not taken.
157 std::string shlibName = FPP_DIR_PLUGIN("/" + name + "/lib" + name + SHLIB_EXT);
778
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 151 times.
157 if (a[3] == ':') {
779
7/14
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 6 times.
✗ Branch 13 not taken.
18 shlibName = FPP_DIR_PLUGIN("/" + name + "/" + a.substr(4));
780 }
781
1/2
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
157 delete spl;
782
1/2
✓ Branch 0 taken 157 times.
✗ Branch 1 not taken.
157 mLoadedUserPlugins.emplace(name);
783 // An FPPOS reflash replaces FPP but keeps the plugin clones, so
784 // this library was built against the previous FPP's headers.
785 // The API version check cannot be relied on to refuse it - it
786 // only catches what someone remembered to bump for, and 10.2
787 // shipped layout changes without a bump - and a stale library
788 // that does load can crash fppd in its constructor (a boot
789 // loop) or in shutdown() when the Plugin Manager unloads it to
790 // reinstall. So do not load it at all until it is rebuilt; the
791 // reinstall warning is already up, and the Plugin Manager's
792 // post-rebuild load request comes through with explicitLoad.
793
7/8
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 147 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 146 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 156 times.
157 if (!explicitLoad && awaitingReinstallAfterOS(name)) {
794
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_PLUGIN, "Not loading plugin %s: FPPOS was upgraded and it has not been reinstalled, "
795 "so %s was built against the previous FPP\n",
796 name.c_str(), shlibName.c_str());
797 1 return nullptr;
798 }
799
2/2
✓ Branch 0 taken 155 times.
✓ Branch 1 taken 1 times.
156 auto* p = loadSHLIBPlugin(shlibName, name);
800
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 144 times.
155 if (p == nullptr) {
801
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
11 WarningHolder::AddWarning(5, "Could not load plugin " + name);
802 }
803 155 return p;
804 157 }
805 }
806 ✗ delete spl;
807 }
808 ✗ return nullptr;
809 226 }
810
811 1793 FPPPlugins::Plugin* PluginManager::loadSHLIBPlugin(const std::string& shlibName, const std::string& dirName) {
812 // If this path was loaded before and the file behind it has since been
813 // replaced - an upgrade between an unloadPlugin() and a loadPlugin() - it
814 // cannot simply be reopened. dlopen() matches an already-loaded object by
815 // NAME before it ever compares the file, so the same path hands back the
816 // copy still mapped and the new build is ignored: the upgrade appears to
817 // succeed while the old code keeps running. Give the new file a name of its
818 // own to open, then unlink that name - the mapping keeps the inode alive, so
819 // nothing is left behind on disk.
820 //
821 // An UNCHANGED file has the opposite problem once that has happened: the
822 // plain path is still bound to the first build ever loaded from it, so
823 // opening it by that name brings the original build back - a second reload
824 // after an upgrade silently reverted to the pre-upgrade code. Reopen it
825 // under the name it was last opened as, which the loader matches to the
826 // build that is actually current.
827
1/2
✓ Branch 0 taken 1793 times.
✗ Branch 1 not taken.
1793 std::string openPath = shlibName;
828 struct stat sb;
829 1793 bool haveStat = (stat(shlibName.c_str(), &sb) == 0);
830
1/2
✓ Branch 0 taken 1793 times.
✗ Branch 1 not taken.
1793 auto known = mLoadedShlibInodes.find(shlibName);
831
8/8
✓ Branch 0 taken 1742 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 173 times.
✓ Branch 3 taken 1569 times.
✓ Branch 4 taken 172 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 172 times.
✓ Branch 7 taken 1621 times.
1793 if (haveStat && known != mLoadedShlibInodes.end() && known->second.inode == sb.st_ino) {
832
1/2
✓ Branch 0 taken 172 times.
✗ Branch 1 not taken.
172 openPath = known->second.openedAs;
833
6/6
✓ Branch 0 taken 1570 times.
✓ Branch 1 taken 51 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1569 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1620 times.
1621 } else if (haveStat && known != mLoadedShlibInodes.end()) {
834 static int generation = 0;
835
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 std::string genPath = shlibName + ".gen" + std::to_string(++generation);
836 1 unlink(genPath.c_str());
837
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (link(shlibName.c_str(), genPath.c_str()) == 0) {
838
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 openPath = genPath;
839 } else {
840 ✗ LogWarn(VB_PLUGIN, "%s was replaced on disk but a private name could not be made (%s); "
841 "the previously loaded build stays in use until fppd restarts\n",
842 shlibName.c_str(), FPPstrerror(errno));
843 }
844 1 }
845
846 1793 void* handle = dlopen(openPath.c_str(), RTLD_NOW);
847
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1791 times.
1793 if (openPath != shlibName) {
848 2 unlink(openPath.c_str());
849 }
850
3/4
✓ Branch 0 taken 1741 times.
✓ Branch 1 taken 52 times.
✓ Branch 2 taken 1741 times.
✗ Branch 3 not taken.
1793 if (handle != nullptr && haveStat) {
851
1/2
✓ Branch 0 taken 1741 times.
✗ Branch 1 not taken.
1741 mLoadedShlibInodes[shlibName] = { sb.st_ino, openPath };
852 }
853
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 1741 times.
1793 if (handle == nullptr) {
854
14/22
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 51 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 51 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 51 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 51 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 50 times.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 51 times.
✓ Branch 13 taken 1 times.
✓ Branch 14 taken 51 times.
✓ Branch 15 taken 1 times.
✓ Branch 16 taken 50 times.
✓ Branch 17 taken 2 times.
✗ Branch 18 not taken.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
52 if (!FileExists(shlibName) && !FileExists(getFPPDDir("/" + shlibName))) {
855
1/2
✓ Branch 0 taken 50 times.
✗ Branch 1 not taken.
50 LogErr(VB_PLUGIN, "Failed to find shlib %s\n", shlibName.c_str());
856 }
857 52 char* er = dlerror();
858
1/2
✓ Branch 0 taken 52 times.
✗ Branch 1 not taken.
52 LogErr(VB_PLUGIN, "Failed to load shlib: %s\n", er);
859 52 return nullptr;
860 }
861 FPPPlugins::Plugin* (*fptr)();
862 1741 *(void**)(&fptr) = dlsym(handle, "createPlugin");
863
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1739 times.
1741 if (fptr == nullptr) {
864
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 LogErr(VB_PLUGIN, "Failed to find createPlugin() function in shlib %s\n", shlibName.c_str());
865
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 WarningHolder::AddWarning(5, "Could not load plugin " + shlibName + " (missing createPlugin entry point)");
866 2 dlclose(handle);
867 2 return nullptr;
868 }
869
870 // Check plugin API version to prevent crashes from ABI-incompatible plugins.
871 //
872 // dlsym() on a handle searches the object AND its DT_NEEDED dependencies, and
873 // every plugin links -lfpp. libfpp.so also defines this weak symbol (Plugin.h
874 // emits it into every TU that includes it), so a plugin predating the version
875 // mechanism resolves straight through to FPP's own copy and reports a version
876 // that trivially matches. Verified on Debian 13/aarch64: without the
877 // provenance check below, a .so defining no version symbol is accepted.
878 // So confirm the symbol actually lives in the plugin by comparing the shared
879 // object it was found in against the one createPlugin() came from - that entry
880 // point is never defined by libfpp, so it identifies the plugin itself.
881 int (*vfptr)();
882 1739 *(void**)(&vfptr) = dlsym(handle, "fpp_plugin_api_version");
883 Dl_info versionInfo;
884 Dl_info pluginInfo;
885
1/2
✓ Branch 0 taken 1739 times.
✗ Branch 1 not taken.
1739 bool ownVersionSymbol = vfptr != nullptr &&
886
1/2
✓ Branch 0 taken 1739 times.
✗ Branch 1 not taken.
3478 dladdr((void*)vfptr, &versionInfo) != 0 &&
887
1/2
✓ Branch 0 taken 1739 times.
✗ Branch 1 not taken.
5217 dladdr((void*)fptr, &pluginInfo) != 0 &&
888
2/2
✓ Branch 0 taken 1738 times.
✓ Branch 1 taken 1 times.
1739 versionInfo.dli_fbase == pluginInfo.dli_fbase;
889
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1738 times.
1739 if (!ownVersionSymbol) {
890
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogErr(VB_PLUGIN, "Plugin %s was compiled against an older FPP API and is not compatible. Please update and rebuild the plugin.\n", shlibName.c_str());
891
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 WarningHolder::AddWarning(5, "Could not load plugin " + shlibName + " (built against an older FPP API - rebuild required)");
892 1 dlclose(handle);
893 1 return nullptr;
894 }
895
1/2
✓ Branch 0 taken 1738 times.
✗ Branch 1 not taken.
1738 int pluginVersion = vfptr();
896
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1738 times.
1738 if (pluginVersion != FPP_PLUGIN_API_VERSION) {
897 ✗ LogErr(VB_PLUGIN, "Plugin %s API version %d does not match FPP API version %d. Please update and rebuild the plugin.\n", shlibName.c_str(), pluginVersion, FPP_PLUGIN_API_VERSION);
898 ✗ WarningHolder::AddWarning(5, "Could not load plugin " + shlibName + " (API version mismatch - rebuild required)");
899 ✗ dlclose(handle);
900 ✗ return nullptr;
901 }
902
903 // Second, independent gate: compare the plugin's compiled-in sizeof() for
904 // the two types it shares layout with against ours. The version number
905 // above only helps if somebody remembers to bump it, and twice now a member
906 // was added to CommandArg without one - a plugin then push_backs onto
907 // Command::args with its own smaller node size while Command::~Command
908 // destroys those nodes with the larger one, running ~string past the end of
909 // the plugin's allocation. This catches that even when the versions agree.
910 // Both types are pimpl'd as of version 5 so these should now never move,
911 // which makes this cheap to keep and loud if the freeze ever slips.
912 //
913 // Same provenance trick as above, but inverted: here a symbol resolving to
914 // libfpp's own copy is the expected case for a plugin that never includes
915 // Commands.h. It registers no commands, so there is no layout to disagree
916 // about and nothing to check.
917 6948 auto abiSizeMatches = [&](const char* symbol, unsigned int coreSize, const char* what) {
918 unsigned int (*sfptr)();
919 6948 *(void**)(&sfptr) = dlsym(handle, symbol);
920 Dl_info sizeInfo;
921
5/6
✓ Branch 0 taken 6936 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 6936 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3306 times.
✓ Branch 5 taken 3642 times.
13884 if (sfptr == nullptr || dladdr((void*)sfptr, &sizeInfo) == 0 ||
922
2/2
✓ Branch 0 taken 3294 times.
✓ Branch 1 taken 3642 times.
6936 sizeInfo.dli_fbase != pluginInfo.dli_fbase) {
923 3306 return true; // not the plugin's own symbol - nothing to compare
924 }
925
1/2
✓ Branch 0 taken 3642 times.
✗ Branch 1 not taken.
3642 unsigned int pluginSize = sfptr();
926
2/2
✓ Branch 0 taken 3639 times.
✓ Branch 1 taken 3 times.
3642 if (pluginSize == coreSize) {
927 3639 return true;
928 }
929
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogErr(VB_PLUGIN, "Plugin %s was built with a %u byte %s but FPP uses %u bytes. Please update and rebuild the plugin.\n",
930 shlibName.c_str(), pluginSize, what, coreSize);
931
5/10
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
✗ Branch 9 not taken.
3 WarningHolder::AddWarning(5, "Could not load plugin " + shlibName + " (" + what + " ABI mismatch - rebuild required)");
932 3 return false;
933 1738 };
934
1/2
✓ Branch 0 taken 1738 times.
✗ Branch 1 not taken.
1738 if (!abiSizeMatches("fpp_command_arg_abi_size", (unsigned int)sizeof(Command::CommandArg), "Command::CommandArg") ||
935
2/4
✓ Branch 0 taken 1737 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1737 times.
✗ Branch 3 not taken.
1737 !abiSizeMatches("fpp_command_abi_size", (unsigned int)sizeof(Command), "Command") ||
936 // The VB_* macros resolve to a member offset within FPPLogger::INSTANCE,
937 // so a plugin that disagrees about this layout passes _LogWrite() a
938 // reference into the wrong facility - see the LAYOUT RULE in log.h.
939
7/8
✓ Branch 0 taken 1737 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1737 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1736 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 1735 times.
5211 !abiSizeMatches("fpp_logger_instance_abi_size", (unsigned int)sizeof(FPPLoggerInstance), "FPPLoggerInstance") ||
940
4/6
✓ Branch 0 taken 1736 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1736 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1735 times.
1736 !abiSizeMatches("fpp_logger_abi_span", fpp_logger_abi_span(), "FPPLogger facility layout")) {
941 3 dlclose(handle);
942 3 return nullptr;
943 }
944
945
2/2
✓ Branch 0 taken 1734 times.
✓ Branch 1 taken 1 times.
1735 FPPPlugins::Plugin* p = fptr();
946
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1731 times.
1734 if (p == nullptr) {
947
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 LogErr(VB_PLUGIN, "Failed to create plugin from shlib %s\n", shlibName.c_str());
948
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 WarningHolder::AddWarning(5, "Could not load plugin " + shlibName + " (createPlugin returned no plugin)");
949 3 dlclose(handle);
950 3 return nullptr;
951 }
952
1/2
✓ Branch 0 taken 1731 times.
✗ Branch 1 not taken.
1731 mShlibHandles.push_back(handle);
953
1/2
✓ Branch 0 taken 1731 times.
✗ Branch 1 not taken.
1731 addPlugin(p);
954
955 // Opt-in: only a plugin that declares itself safe gets dlclose()d on unload.
956 // Same provenance check as the ABI gates above - the symbol has to be the
957 // plugin's own, not one picked up from a dependency.
958 1731 bool supportsUnload = false;
959 int (*unloadFptr)();
960 1731 *(void**)(&unloadFptr) = dlsym(handle, "fpp_plugin_supports_unload");
961 Dl_info unloadInfo;
962
5/6
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 1688 times.
✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 43 times.
✓ Branch 5 taken 1688 times.
1774 if (unloadFptr != nullptr && dladdr((void*)unloadFptr, &unloadInfo) != 0 &&
963
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 unloadInfo.dli_fbase == pluginInfo.dli_fbase) {
964
1/2
✓ Branch 0 taken 43 times.
✗ Branch 1 not taken.
43 supportsUnload = (unloadFptr() == 1);
965 }
966
2/4
✓ Branch 0 taken 1731 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1731 times.
✗ Branch 3 not taken.
1731 mPluginDirNames[p] = dirName;
967
1/2
✓ Branch 0 taken 1731 times.
✗ Branch 1 not taken.
1731 mPluginLibraries[dirName] = { handle, supportsUnload };
968
3/4
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 1688 times.
✓ Branch 2 taken 1731 times.
✗ Branch 3 not taken.
1731 LogDebug(VB_PLUGIN, "Plugin %s %s unloading\n", p->getName().c_str(),
969 supportsUnload ? "supports" : "does not support");
970 1731 return p;
971
1/2
✓ Branch 0 taken 1741 times.
✗ Branch 1 not taken.
3534 }
972 63 bool PluginManager::hasPlugins() {
973 63 return !mPlugins.empty();
974 }
975 1794 void PluginManager::addPlugin(FPPPlugins::Plugin* p) {
976
1/2
✓ Branch 0 taken 1794 times.
✗ Branch 1 not taken.
1794 mPlugins.push_back(p);
977
978
1/2
✓ Branch 0 taken 1794 times.
✗ Branch 1 not taken.
1794 FPPPlugin::PlaylistEventPlugin* pep = dynamic_cast<FPPPlugin::PlaylistEventPlugin*>(p);
979
2/2
✓ Branch 0 taken 89 times.
✓ Branch 1 taken 1705 times.
1794 if (pep) {
980
1/2
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
89 std::lock_guard<std::mutex> lock(playlistPluginsLock);
981
1/2
✓ Branch 0 taken 89 times.
✗ Branch 1 not taken.
89 mPlaylistPlugins.push_back(pep);
982 89 }
983
1/2
✓ Branch 0 taken 1794 times.
✗ Branch 1 not taken.
1794 FPPPlugin::ChannelOutputPlugin* coep = dynamic_cast<FPPPlugin::ChannelOutputPlugin*>(p);
984
2/2
✓ Branch 0 taken 1656 times.
✓ Branch 1 taken 138 times.
1794 if (coep) {
985
1/2
✓ Branch 0 taken 1656 times.
✗ Branch 1 not taken.
1656 mChannelOutputPlugins.push_back(coep);
986 }
987
1/2
✓ Branch 0 taken 1794 times.
✗ Branch 1 not taken.
1794 FPPPlugin::ChannelDataPlugin* cdp = dynamic_cast<FPPPlugin::ChannelDataPlugin*>(p);
988
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 1769 times.
1794 if (cdp) {
989
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 std::lock_guard<std::mutex> lock(channelDataPluginsLock);
990
1/2
✓ Branch 0 taken 25 times.
✗ Branch 1 not taken.
25 mChannelDataPlugins.push_back(cdp);
991 25 }
992
1/2
✓ Branch 0 taken 1794 times.
✗ Branch 1 not taken.
1794 FPPPlugin::APIProviderPlugin* app = dynamic_cast<FPPPlugin::APIProviderPlugin*>(p);
993
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1765 times.
1794 if (app) {
994
1/2
✓ Branch 0 taken 29 times.
✗ Branch 1 not taken.
29 mAPIProviderPlugins.push_back(app);
995 }
996 1794 }
997 template<class P, class F>
998 static void CallIntoPlugin(P* p, const char* what, F&& f);
999
1000 325 void PluginManager::mediaCallback(const Json::Value& playlist, const MediaDetails& mediaDetails) {
1001
1/2
✓ Branch 0 taken 325 times.
✗ Branch 1 not taken.
325 ForEachPlaylistPlugin(mPlaylistPlugins, [&](FPPPlugins::PlaylistEventPlugin* a) {
1002
1/2
✓ Branch 0 taken 15 times.
✗ Branch 1 not taken.
30 CallIntoPlugin(a, "mediaCallback", [&]() { a->mediaCallback(playlist, mediaDetails); });
1003 15 });
1004 325 }
1005 2649 void PluginManager::registerApis() {
1006
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2649 times.
2673 for (auto a : mAPIProviderPlugins) {
1007 // Some plugins register their commands here rather than in their
1008 // constructor, so this needs recording too - see mPluginCommands.
1009
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 auto* plugin = dynamic_cast<FPPPlugins::Plugin*>(a);
1010 24 std::optional<CommandRecorder> recordCommands;
1011
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (plugin) {
1012
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 recordCommands.emplace(mPluginCommands, dirNameFor(plugin));
1013 }
1014
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 a->registerApis();
1015 24 }
1016 2649 }
1017 3220 void PluginManager::unregisterApis() {
1018
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 3220 times.
3246 for (auto a : mAPIProviderPlugins) {
1019
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 a->unregisterApis();
1020 }
1021 3220 }
1022 196890 void PluginManager::modifySequenceData(int ms, uint8_t* seqData) {
1023
1/2
✓ Branch 0 taken 196890 times.
✗ Branch 1 not taken.
196890 std::lock_guard<std::mutex> lock(channelDataPluginsLock);
1024
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 196890 times.
197012 for (auto a : mChannelDataPlugins) {
1025
1/2
✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
122 a->modifySequenceData(ms, seqData);
1026 }
1027 196890 }
1028 196890 void PluginManager::modifyChannelData(int ms, uint8_t* seqData) {
1029
1/2
✓ Branch 0 taken 196890 times.
✗ Branch 1 not taken.
196890 std::lock_guard<std::mutex> lock(channelDataPluginsLock);
1030
2/2
✓ Branch 0 taken 122 times.
✓ Branch 1 taken 196890 times.
197012 for (auto a : mChannelDataPlugins) {
1031
1/2
✓ Branch 0 taken 122 times.
✗ Branch 1 not taken.
122 a->modifyChannelData(ms, seqData);
1032 }
1033 196890 }
1034 2649 void PluginManager::addControlCallbacks(std::map<int, std::function<bool(int)>>& callbacks) {
1035
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 2649 times.
2673 for (auto a : mAPIProviderPlugins) {
1036 // Record which descriptors this plugin added, so unloadPlugin() can take
1037 // them back out of the epoll loop. The callbacks it installs are
1038 // std::functions whose code lives in the plugin, so leaving one
1039 // registered after the plugin is gone is a call into nothing.
1040 24 std::set<int> before;
1041
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 24 times.
72 for (const auto& c : callbacks) {
1042
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 before.insert(c.first);
1043 }
1044
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 auto* plugin = dynamic_cast<FPPPlugins::Plugin*>(a);
1045 {
1046 // Several plugins register their commands from here - see
1047 // mPluginCommands. Scoped so it records before the fds are.
1048 24 std::optional<CommandRecorder> recordCommands;
1049
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if (plugin) {
1050
2/4
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 24 times.
✗ Branch 3 not taken.
24 recordCommands.emplace(mPluginCommands, dirNameFor(plugin));
1051 }
1052
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 a->addControlCallbacks(callbacks);
1053 24 }
1054
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
24 if (!plugin) {
1055 ✗ continue;
1056 }
1057
2/2
✓ Branch 0 taken 60 times.
✓ Branch 1 taken 24 times.
84 for (const auto& c : callbacks) {
1058
3/4
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 48 times.
60 if (before.find(c.first) == before.end()) {
1059
3/6
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 12 times.
✗ Branch 5 not taken.
12 mPluginControlFds[dirNameFor(plugin)].push_back(c.first);
1060 }
1061 }
1062
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 }
1063 2649 }
1064
1065 // ---- Runtime load/unload ---------------------------------------------------
1066
1067 // The directory a plugin was installed from. Falls back to the plugin's own
1068 // name for anything not loaded through loadUserPlugin() - a script plugin, or a
1069 // shlib reached through findPlugin() - where the two are the same by
1070 // construction. See mPluginDirNames in Plugins.h for why this exists.
1071 1921 std::string PluginManager::dirNameFor(FPPPlugins::Plugin* plugin) const {
1072
1/2
✓ Branch 0 taken 1921 times.
✗ Branch 1 not taken.
1921 auto it = mPluginDirNames.find(plugin);
1073
2/4
✓ Branch 0 taken 1921 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1921 times.
✗ Branch 3 not taken.
1921 return it != mPluginDirNames.end() ? it->second : plugin->getName();
1074 }
1075
1076 // Resolves the name the Plugin Manager and the REST endpoints use - the
1077 // directory - to the loaded plugin, whatever the plugin calls itself.
1078 187 FPPPlugins::Plugin* PluginManager::findPluginByDir(const std::string& dirName) const {
1079
2/2
✓ Branch 0 taken 139 times.
✓ Branch 1 taken 81 times.
220 for (auto& a : mPlugins) {
1080
3/4
✓ Branch 0 taken 139 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 106 times.
✓ Branch 3 taken 33 times.
139 if (dirNameFor(a) == dirName) {
1081 106 return a;
1082 }
1083 }
1084 81 return nullptr;
1085 }
1086
1087 1717 void PluginManager::noteChannelOutputCreated(FPPPlugins::ChannelOutputPlugin* plugin) {
1088
2/4
✓ Branch 0 taken 1717 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1717 times.
✗ Branch 3 not taken.
1717 if (auto* p = dynamic_cast<FPPPlugins::Plugin*>(plugin)) {
1089
2/4
✓ Branch 0 taken 1717 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1717 times.
✗ Branch 3 not taken.
1717 mPluginsWithOutputs.insert(dirNameFor(p));
1090 }
1091 1717 }
1092
1093 147 bool PluginManager::isPluginLoaded(const std::string& name) {
1094 147 return findPluginByDir(name) != nullptr;
1095 }
1096
1097 34 void PluginManager::startPlugin(FPPPlugins::Plugin* plugin) {
1098
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 auto* api = dynamic_cast<FPPPlugins::APIProviderPlugin*>(plugin);
1099
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 5 times.
34 if (!api) {
1100 29 return;
1101 }
1102 // The boot path records these in registerApis()/addControlCallbacks(); a
1103 // runtime load comes through here instead and has to do the same.
1104
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 const std::string dirName = dirNameFor(plugin);
1105
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 CommandRecorder recordCommands(mPluginCommands, dirName);
1106
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 api->registerApis();
1107
1108 // At boot these land in a map fppd hands to EPollManager once everything has
1109 // registered. That map is long gone by the time a plugin loads at runtime,
1110 // so collect into a local one and register the descriptors directly.
1111 5 std::map<int, std::function<bool(int)>> callbacks;
1112
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 api->addControlCallbacks(callbacks);
1113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
5 for (auto& c : callbacks) {
1114 ✗ EPollManager::INSTANCE.addFileDescriptor(c.first, c.second);
1115 ✗ mPluginControlFds[dirName].push_back(c.first);
1116 }
1117 5 }
1118
1119 40 void PluginManager::detachPlugin(FPPPlugins::Plugin* plugin) {
1120 320 auto drop = [plugin](auto& vec) {
1121
2/2
✓ Branch 0 taken 53 times.
✓ Branch 1 taken 160 times.
426 for (auto it = vec.begin(); it != vec.end();) {
1122
3/4
✓ Branch 0 taken 53 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 44 times.
✓ Branch 3 taken 9 times.
106 if (dynamic_cast<FPPPlugins::Plugin*>(*it) == plugin) {
1123
1/2
✓ Branch 0 taken 44 times.
✗ Branch 1 not taken.
88 it = vec.erase(it);
1124 } else {
1125 18 ++it;
1126 }
1127 }
1128 360 };
1129 {
1130
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 std::lock_guard<std::mutex> lock(playlistPluginsLock);
1131
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 drop(mPlaylistPlugins);
1132 40 }
1133
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 drop(mChannelOutputPlugins);
1134 {
1135
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 std::lock_guard<std::mutex> lock(channelDataPluginsLock);
1136
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 drop(mChannelDataPlugins);
1137 40 }
1138
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 drop(mAPIProviderPlugins);
1139
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 for (auto it = mPlugins.begin(); it != mPlugins.end(); ++it) {
1140
2/2
✓ Branch 0 taken 40 times.
✓ Branch 1 taken 8 times.
48 if (*it == plugin) {
1141
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 mPlugins.erase(it);
1142 40 break;
1143 }
1144 }
1145 // The settings watch calls settingChanged() on the plugin, so it goes too --
1146 // and it has to go now, while nothing else holds this name: a reload
1147 // registers the new instance's watch under it before the old one is
1148 // deleted. See settingsUnwatched.
1149
5/10
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 40 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 40 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 40 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 40 times.
✗ Branch 9 not taken.
80 FileMonitor::INSTANCE.RemoveFile(plugin->getName(), FPP_DIR_CONFIG("/plugin." + plugin->getName()));
1150 {
1151
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 std::lock_guard<std::mutex> lock(settingsUnwatchedLock);
1152
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 settingsUnwatched.insert(plugin);
1153 40 }
1154 40 }
1155
1156 54 bool PluginManager::loadPlugin(const std::string& name, std::string& error) {
1157
3/4
✓ Branch 0 taken 54 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 16 times.
✓ Branch 3 taken 38 times.
54 if (isPluginLoaded(name)) {
1158 16 return true; // already running; nothing to do
1159 }
1160
6/12
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 38 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 38 times.
76 if (!DirectoryExists(FPP_DIR_PLUGIN("/" + name))) {
1161 ✗ error = "no plugin directory for " + name;
1162 ✗ return false;
1163 }
1164 // Plenty of plugins are web-UI only - no callbacks script, nothing for fppd
1165 // to run. That is a successful no-op, not a failure: reporting it as one
1166 // told the user to restart FPPD to enable something that needs neither.
1167
5/10
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 38 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 38 times.
✗ Branch 9 not taken.
38 std::string callbacks = FPP_DIR_PLUGIN("/" + name + "/callbacks");
1168
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 bool haveCallbacks = FileExists(callbacks);
1169
2/2
✓ Branch 0 taken 152 times.
✓ Branch 1 taken 38 times.
190 for (const char* ext : { ".sh", ".pl", ".php", ".py" }) {
1170
7/12
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 119 times.
✓ Branch 2 taken 33 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 33 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 33 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 33 times.
✓ Branch 9 taken 119 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
152 haveCallbacks = haveCallbacks || FileExists(callbacks + ext);
1171 }
1172
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (!haveCallbacks) {
1173 ✗ LogDebug(VB_PLUGIN, "Plugin %s has no callbacks; nothing for fppd to load\n", name.c_str());
1174 ✗ return true;
1175 }
1176
1177 // A previous unloadPlugin() left the name in this set (the library stays
1178 // mapped), so clear it or loadUserPlugin() will not look at the plugin again.
1179
1/2
✓ Branch 0 taken 38 times.
✗ Branch 1 not taken.
38 mLoadedUserPlugins.erase(name);
1180
1181
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 1 times.
38 FPPPlugins::Plugin* p = loadUserPlugin(name, true);
1182
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 34 times.
37 if (!p) {
1183 // loadUserPlugin() logs the specific reason - a missing callbacks script,
1184 // an ABI-version refusal from loadSHLIBPlugin(), a createPlugin() that
1185 // returned nothing.
1186
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 error = "plugin " + name + " did not load - see the log";
1187 3 return false;
1188 }
1189
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 startPlugin(p);
1190
1/2
✓ Branch 0 taken 34 times.
✗ Branch 1 not taken.
34 LogInfo(VB_PLUGIN, "Loaded plugin %s\n", name.c_str());
1191 34 return true;
1192 38 }
1193
1194 40 bool PluginManager::unloadPlugin(const std::string& name, std::string& error) {
1195 // By directory name, which is what the Plugin Manager and the REST
1196 // endpoints pass - a plugin is free to call itself something else.
1197
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 FPPPlugins::Plugin* plugin = findPluginByDir(name);
1198
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (!plugin) {
1199 ✗ return true; // not loaded; the caller's goal already holds
1200 }
1201 // A channel output the plugin created is owned by the output system, not by
1202 // the plugin, and may be mid-show. Nothing here can take it back, so refuse
1203 // rather than leave an output running against a destroyed plugin. Keyed on
1204 // having actually produced one - merely implementing ChannelOutputPlugin
1205 // says nothing, since the FPPPlugin convenience base inherits all four
1206 // plugin interfaces.
1207
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
40 if (mPluginsWithOutputs.find(name) != mPluginsWithOutputs.end()) {
1208 ✗ error = name + " provides a channel output in use and can only be removed by restarting";
1209 ✗ return false;
1210 }
1211
1212 // Order matters: stop inbound HTTP first (unregisterApis does not return
1213 // until no request is inside the plugin's handlers and they are destroyed),
1214 // then let the plugin stop its own threads and timers while it is still a
1215 // whole object, then stop calling it, then destroy it.
1216
3/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 37 times.
40 if (auto* api = dynamic_cast<FPPPlugins::APIProviderPlugin*>(plugin)) {
1217
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 api->unregisterApis();
1218 }
1219 // Withdraw the plugin's epoll descriptors BEFORE shutdown(), while they are
1220 // still open: a plugin that closes them itself in shutdown() would make the
1221 // EPOLL_CTL_DEL fail, and the callback - which lives in the plugin - has to
1222 // come out of the map either way.
1223
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 auto fds = mPluginControlFds.find(name);
1224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 40 times.
40 if (fds != mPluginControlFds.end()) {
1225 ✗ for (int fd : fds->second) {
1226 ✗ EPollManager::INSTANCE.removeFileDescriptor(fd);
1227 }
1228 ✗ mPluginControlFds.erase(fds);
1229 }
1230
1231
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 std::function<bool()> ready = plugin->shutdown();
1232
1233 // Anything the plugin still has in flight holds a callback that lives in its
1234 // library. shutdown() cannot cancel those - they are owned by CurlManager -
1235 // so drop them here. Requests the plugin did not tag with its name are
1236 // invisible to this, which is exactly why unmapping is opt-in below.
1237
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 CurlManager::INSTANCE.cancelRequests(name);
1238
1239 // Backstop for commands the plugin did not take back itself. A C++ plugin
1240 // owns what it registers and is expected to withdraw and delete it in
1241 // shutdown() (see Plugin.h) - this is the safety net for one that forgets,
1242 // plus the only owner of the commands FPP itself registered from a script
1243 // plugin's descriptions.json. A Command subclass declared in a plugin has
1244 // its vtable in that plugin's .so, so anything left has to go before the
1245 // library does.
1246
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 auto cmds = mPluginCommands.find(name);
1247
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 33 times.
40 if (cmds != mPluginCommands.end()) {
1248 // What is registered under each name NOW, so a command the plugin
1249 // already withdrew itself (in shutdown(), which has just run) is not
1250 // deleted twice, and one that something else has since re-registered
1251 // under the same name is left to its new owner.
1252 7 std::map<std::string, Command*> current;
1253
3/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 453 times.
✓ Branch 3 taken 7 times.
460 for (const auto& c : CommandManager::INSTANCE.getRegisteredCommands()) {
1254
1/2
✓ Branch 0 taken 453 times.
✗ Branch 1 not taken.
453 current.emplace(c.first, c.second);
1255 7 }
1256 7 std::vector<std::string> leftBehind;
1257
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 7 times.
14 for (const auto& c : cmds->second) {
1258
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 auto it = current.find(c.first);
1259
5/6
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 3 times.
7 if (it == current.end() || it->second != c.second) {
1260 4 continue;
1261 }
1262
3/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
3 if (dynamic_cast<ScriptCommand*>(c.second)) {
1263 // FPP's own class, so it may outlive this call: a script can
1264 // run for any length of time, and its run must finish on a
1265 // live object.
1266
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 CommandManager::INSTANCE.retireCommand(c.second);
1267 } else {
1268
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 CommandManager::INSTANCE.removeCommand(c.first); // unregisters only
1269
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 delete c.second;
1270 }
1271
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 leftBehind.push_back(c.first);
1272 }
1273 // Say so rather than quietly covering for it: a C++ plugin reaching this
1274 // has a shutdown() that does not withdraw what it registered, and the
1275 // net only reaches the commands added while it was loading. (Script
1276 // plugins are expected here - FPP registered those from their
1277 // descriptions.json and owns them.)
1278
7/8
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 2 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 6 times.
7 if (!leftBehind.empty() && mPluginLibraries.find(name) != mPluginLibraries.end()) {
1279 1 std::string list;
1280
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 for (const auto& n : leftBehind) {
1281
3/6
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 list += (list.empty() ? "" : ", ") + n;
1282 }
1283
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_PLUGIN, "Plugin %s left %d command(s) registered at unload (%s); it should withdraw and delete them in shutdown()\n",
1284 name.c_str(), (int)leftBehind.size(), list.c_str());
1285 1 }
1286
1/2
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
7 mPluginCommands.erase(cmds);
1287 7 }
1288
1289 // Detach now: from here nothing calls into the plugin, so as far as the rest
1290 // of FPP is concerned it is unloaded, whatever the settling time says.
1291
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 detachPlugin(plugin);
1292
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 mLoadedUserPlugins.erase(name);
1293
1294 // Unmapping is opt-in. A plugin that declared FPP_PLUGIN_SUPPORTS_UNLOAD has
1295 // asserted it leaves nothing behind pointing into its library; without that
1296 // the mapping stays, which costs address space and nothing else.
1297 40 void* handle = nullptr;
1298 40 bool unmap = false;
1299
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 auto lib = mPluginLibraries.find(name);
1300 40 bool hadLibrary = (lib != mPluginLibraries.end());
1301
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 32 times.
40 if (lib != mPluginLibraries.end()) {
1302
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
8 if (lib->second.supportsUnload) {
1303 5 handle = lib->second.handle;
1304 5 unmap = true;
1305
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
6 for (auto it = mShlibHandles.begin(); it != mShlibHandles.end(); ++it) {
1306
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 1 times.
6 if (*it == handle) {
1307
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 mShlibHandles.erase(it); // or Cleanup() would dlclose it a second time
1308 5 break;
1309 }
1310 }
1311
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 mPluginLibraries.erase(lib);
1312 // The library leaves the link map when it is closed, so a later load
1313 // of the same path opens whatever is on disk - no generation link
1314 // needed for it.
1315
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
11 for (auto it = mLoadedShlibInodes.begin(); it != mLoadedShlibInodes.end();) {
1316
5/8
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 5 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 5 times.
✗ Branch 7 not taken.
6 it = (it->first.find("/" + name + "/") != std::string::npos) ? mLoadedShlibInodes.erase(it) : ++it;
1317 }
1318 }
1319 }
1320
1321 40 PendingUnload pending;
1322 40 pending.plugin = plugin;
1323 40 pending.handle = handle;
1324 40 pending.unmap = unmap;
1325 40 pending.hadLibrary = hadLibrary;
1326
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 pending.name = name;
1327 40 pending.ready = std::move(ready);
1328 // Cap the wait so a predicate that never returns true delays teardown
1329 // rather than holding the plugin forever.
1330
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 pending.deadlineMS = GetMonotonicTimeMS() + 60000;
1331
1332
2/2
✓ Branch 0 taken 39 times.
✓ Branch 1 taken 1 times.
40 if (!pending.ready) {
1333
1/2
✓ Branch 0 taken 39 times.
✗ Branch 1 not taken.
39 finishUnload(pending);
1334 39 return true;
1335 }
1336 // Still finishing. A load arriving in the meantime is fine: it constructs a
1337 // fresh object, and the dlopen it does raises the library's reference count
1338 // above what this later drops.
1339
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 mPendingUnloads.push_back(std::move(pending));
1340
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogInfo(VB_PLUGIN, "Unloaded plugin %s (waiting for it to finish)\n", name.c_str());
1341
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 pollPendingUnload(name);
1342 1 return true;
1343 40 }
1344
1345 // Re-arms itself once a second until the plugin says it is done. Safe to
1346 // reschedule from inside the callback: a one-shot timer is removed from the list
1347 // before it fires, so addTimer() here adds a fresh entry rather than mutating
1348 // the one being run.
1349 4 void PluginManager::pollPendingUnload(const std::string& name) {
1350
5/10
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 4 times.
✗ Branch 9 not taken.
4 Timers::INSTANCE.addTimer("PluginUnload-" + name, GetTimeMS() + 1000, [this, name]() {
1351
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 for (auto it = mPendingUnloads.begin(); it != mPendingUnloads.end(); ++it) {
1352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (it->name != name) {
1353 ✗ continue;
1354 }
1355 4 bool done = false;
1356
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 long long now = GetMonotonicTimeMS();
1357
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (now >= it->deadlineMS) {
1358 ✗ LogWarn(VB_PLUGIN, "Plugin %s never reported finished; tearing down anyway\n", name.c_str());
1359 ✗ done = true;
1360 } else {
1361 // The predicate is the plugin's code, which is why this runs
1362 // while the library is still mapped.
1363 try {
1364
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 done = it->ready();
1365 ✗ } catch (...) {
1366 ✗ LogWarn(VB_PLUGIN, "Plugin %s threw while reporting readiness; tearing down\n", name.c_str());
1367 ✗ done = true;
1368 ✗ }
1369 }
1370
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
4 if (done) {
1371 1 PendingUnload p = std::move(*it);
1372
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 mPendingUnloads.erase(it);
1373
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 finishUnload(p);
1374 1 } else {
1375
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 pollPendingUnload(name);
1376 }
1377 4 return;
1378 }
1379 });
1380 4 }
1381
1382 40 void PluginManager::finishUnload(PendingUnload& p) {
1383 // Drop the predicate before the library goes: it is the plugin's code too.
1384 40 p.ready = nullptr;
1385 // Detached already, but a playlist or media callback that took its copy of
1386 // the list before then may still be inside the plugin on another thread.
1387 {
1388
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 std::unique_lock<std::mutex> lock(playlistPluginsLock);
1389
2/4
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 40 times.
40 if (!playlistPluginCallDone.wait_for(lock, std::chrono::seconds(30), [&p]() {
1390
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 return playlistPluginCallsInFlight.find(p.plugin) == playlistPluginCallsInFlight.end();
1391 })) {
1392 ✗ LogWarn(VB_PLUGIN, "Plugin %s still had a playlist callback running after 30 s; deleting it anyway\n", p.name.c_str());
1393 }
1394 40 }
1395 40 mPluginDirNames.erase(p.plugin); // so the map never holds a freed pointer
1396
1/2
✓ Branch 0 taken 40 times.
✗ Branch 1 not taken.
40 delete p.plugin; // the plugin's own full teardown
1397
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
40 if (p.unmap && p.handle) {
1398 5 dlclose(p.handle);
1399 5 LogInfo(VB_PLUGIN, "Unloaded plugin %s (library unmapped)\n", p.name.c_str());
1400
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 32 times.
35 } else if (p.hadLibrary) {
1401 3 LogInfo(VB_PLUGIN, "Unloaded plugin %s (library kept mapped)\n", p.name.c_str());
1402 } else {
1403 // A script plugin has no library of its own to keep or drop.
1404 32 LogInfo(VB_PLUGIN, "Unloaded plugin %s\n", p.name.c_str());
1405 }
1406 40 }
1407 // Plugin callbacks run on the main loop (MultiSync plugin packets arrive from
1408 // any LAN sender) and on API threads. Nothing above them catches, so a plugin
1409 // that throws would terminate fppd; contain it here and log which plugin it was.
1410 template<class P, class F>
1411 394 static void CallIntoPlugin(P* p, const char* what, F&& f) {
1412 try {
1413
2/2
✓ Branch 0 taken 192 times.
✓ Branch 1 taken 3 times.
394 f();
1414 12 } catch (...) {
1415
0/2
✗ Branch 0 not taken.
✗ Branch 1 not taken.
6 FPPPlugins::Plugin* named = dynamic_cast<FPPPlugins::Plugin*>(p);
1416
3/10
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
6 std::string name = named ? named->getName() : "(unnamed)";
1417 try {
1418 6 throw;
1419
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
12 } catch (const std::exception& ex) {
1420
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
6 LogErr(VB_PLUGIN, "Plugin %s threw from %s: %s\n", name.c_str(), what, ex.what());
1421 ✗ } catch (...) {
1422 ✗ LogErr(VB_PLUGIN, "Plugin %s threw a non-standard exception from %s\n", name.c_str(), what);
1423 }
1424 6 }
1425 390 }
1426
1427 5 void PluginManager::multiSyncData(const std::string& pn, uint8_t* data, int len) {
1428
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 for (auto a : mPlugins) {
1429
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (a->getName() == pn) {
1430
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
8 CallIntoPlugin(a, "multiSyncData", [&]() { a->multiSyncData(data, len); });
1431 }
1432 }
1433 5 }
1434 44309 void PluginManager::playlistCallback(const Json::Value& playlist, const std::string& action, const std::string& section, int item) {
1435
1/2
✓ Branch 0 taken 44307 times.
✗ Branch 1 not taken.
44309 ForEachPlaylistPlugin(mPlaylistPlugins, [&](FPPPlugins::PlaylistEventPlugin* a) {
1436
1/2
✓ Branch 0 taken 170 times.
✗ Branch 1 not taken.
344 CallIntoPlugin(a, "playlistCallback", [&]() { a->playlistCallback(playlist, action, section, item); });
1437 170 });
1438 44307 }
1439 698 void PluginManager::playlistInserted(const std::string& playlist, const int position, int endPosition, bool immediate) {
1440
1/2
✓ Branch 0 taken 698 times.
✗ Branch 1 not taken.
698 ForEachPlaylistPlugin(mPlaylistPlugins, [&](FPPPlugins::PlaylistEventPlugin* a) {
1441
1/2
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
12 CallIntoPlugin(a, "playlistInserted", [&]() { a->playlistInserted(playlist, position, endPosition, immediate); });
1442 6 });
1443 698 }
1444
1445 // blocking
1446 11 void MediaCallback::run(const Json::Value& playlist, const MediaDetails& mediaDetails) {
1447 // Built before the fork, and the child only execs: see PlaylistCallback::run().
1448
3/6
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
11 std::string eventScript = FPP_DIR + "/scripts/eventScript";
1449
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 Json::Value root;
1450
1451
4/8
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
11 root["type"] = playlist["currentEntry"]["type"];
1452
1453
8/26
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 11 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
✓ Branch 18 taken 11 times.
✗ Branch 19 not taken.
✓ Branch 20 taken 11 times.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 11 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
11 root["Sequence"] = playlist["currentEntry"]["type"].asString() == "both" ? playlist["currentEntry"]["sequence"]["sequenceName"].asString().c_str() : "";
1454
5/10
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 11 times.
✗ Branch 9 not taken.
22 root["Media"] = playlist["currentEntry"]["type"].asString() == "both"
1455
0/10
✗ Branch 0 not taken.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
11 ? playlist["currentEntry"]["media"]["mediaFilename"].asString().c_str()
1456
7/16
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 11 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 11 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 11 times.
✗ Branch 11 not taken.
✗ Branch 12 not taken.
✓ Branch 13 taken 11 times.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
22 : playlist["currentEntry"]["mediaFilename"].asString().c_str();
1457
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 if (!mediaDetails.title.empty()) {
1458
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 root["title"] = mediaDetails.title;
1459 }
1460
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 10 times.
11 if (!mediaDetails.artist.empty()) {
1461
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 root["artist"] = mediaDetails.artist;
1462 }
1463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!mediaDetails.album.empty()) {
1464 ✗ root["album"] = mediaDetails.album;
1465 }
1466
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (mediaDetails.year) {
1467 ✗ root["year"] = std::to_string(mediaDetails.year);
1468 }
1469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!mediaDetails.comment.empty()) {
1470 ✗ root["comment"] = mediaDetails.comment;
1471 }
1472
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (mediaDetails.track) {
1473 ✗ root["track"] = std::to_string(mediaDetails.track);
1474 }
1475
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (!mediaDetails.genre.empty()) {
1476 ✗ root["genre"] = mediaDetails.genre;
1477 }
1478
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
11 if (mediaDetails.length) {
1479
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 root["length"] = std::to_string(mediaDetails.length);
1480 }
1481
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
11 if (mediaDetails.seconds) {
1482
2/4
✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 root["seconds"] = std::to_string(mediaDetails.seconds);
1483 }
1484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (mediaDetails.minutes) {
1485 ✗ root["minutes"] = std::to_string(mediaDetails.minutes);
1486 }
1487
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
11 if (mediaDetails.bitrate) {
1488
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 root["bitrate"] = std::to_string(mediaDetails.bitrate);
1489 }
1490
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
11 if (mediaDetails.sampleRate) {
1491
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 root["sampleRate"] = std::to_string(mediaDetails.sampleRate);
1492 }
1493
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 5 times.
11 if (mediaDetails.channels) {
1494
2/4
✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
6 root["channels"] = std::to_string(mediaDetails.channels);
1495 }
1496
1497
2/4
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
11 std::string pluginData = SaveJsonToString(root);
1498
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 LogDebug(VB_PLUGIN, "Media plugin data: %s\n", pluginData.c_str());
1499 int pid;
1500
1501
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 22 times.
11 if ((pid = fork()) == -1) {
1502 // Skip this callback, as LifecycleCallback does: exiting from here
1503 // took fppd down mid-show for one plugin's notification.
1504 ✗ LogErr(VB_PLUGIN, "Failed to fork for %s's media callback: %s\n", mName.c_str(), FPPstrerror(errno));
1505 ✗ return;
1506 }
1507
1508
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
22 if (pid == 0) {
1509 11 execl(eventScript.c_str(), "eventScript", mFilename.c_str(), "--type", "media", "--data", pluginData.c_str(), NULL);
1510 11 _exit(EXIT_FAILURE);
1511 }
1512
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 LogExcess(VB_PLUGIN, "Media parent process, resuming work.\n");
1513
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 waitpid(pid, NULL, 0);
1514
3/6
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
11 }
1515
1516 153 void PlaylistCallback::run(const Json::Value& playlist, const std::string& action, const std::string& section, int idx) {
1517 // Everything the child needs is built here, before the fork, and the
1518 // child only execs. fppd is threaded and these run on any thread (an
1519 // MQTT stop runs them on mosquitto's): a child that allocated or logged
1520 // could block forever on a lock another thread held at the moment of the
1521 // fork -- the logger's, an allocator's -- and this thread then waited for
1522 // it in waitpid() for good, the main loop with it.
1523
3/6
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 153 times.
✗ Branch 5 not taken.
153 std::string eventScript = FPP_DIR + "/scripts/eventScript";
1524
1/2
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
153 Json::Value root = playlist;
1525
2/4
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
153 root["Action"] = action;
1526
2/4
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
153 root["Section"] = section;
1527
2/4
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
153 root["Item"] = idx;
1528
2/4
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 153 times.
✗ Branch 3 not taken.
153 std::string pluginData = SaveJsonToString(root);
1529
1/2
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
153 LogDebug(VB_PLUGIN, "Calling %s callback for playlist: %s\n", mName.c_str(), mFilename.c_str());
1530
1/2
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
153 LogDebug(VB_PLUGIN, "Playlist plugin data: %s\n", pluginData.c_str());
1531
1532 int pid;
1533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 306 times.
153 if ((pid = fork()) == -1) {
1534 ✗ LogErr(VB_PLUGIN, "Failed to fork for %s's playlist callback: %s\n", mName.c_str(), FPPstrerror(errno));
1535 ✗ return;
1536 }
1537
1538
2/2
✓ Branch 0 taken 153 times.
✓ Branch 1 taken 153 times.
306 if (pid == 0) {
1539 153 execl(eventScript.c_str(), "eventScript", mFilename.c_str(), "--type", "playlist", "--data", pluginData.c_str(), NULL);
1540 153 _exit(EXIT_FAILURE);
1541 }
1542
1/2
✓ Branch 0 taken 153 times.
✗ Branch 1 not taken.
153 LogExcess(VB_PLUGIN, "Playlist parent process, resuming work.\n");
1543
1/2
✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
153 waitpid(pid, NULL, 0);
1544
3/6
✓ Branch 0 taken 151 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 151 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 151 times.
✗ Branch 5 not taken.
151 }
1545 101 void LifecycleCallback::run(const std::string& lifecycle) {
1546 // Built before the fork, and the child only execs: see PlaylistCallback::run().
1547
3/6
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 101 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 101 times.
✗ Branch 5 not taken.
101 std::string eventScript = FPP_DIR + "/scripts/eventScript";
1548
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 LogDebug(VB_PLUGIN, "Calling %s callback for lifecycle %s: %s\n", mName.c_str(), lifecycle.c_str(), mFilename.c_str());
1549 int pid;
1550
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 202 times.
101 if ((pid = fork()) == -1) {
1551 ✗ LogErr(VB_PLUGIN, "Failed to fork for %s's %s callback\n", mName.c_str(), lifecycle.c_str());
1552 ✗ return;
1553 }
1554
1555
2/2
✓ Branch 0 taken 101 times.
✓ Branch 1 taken 101 times.
202 if (pid == 0) {
1556 101 execl(eventScript.c_str(), "eventScript", mFilename.c_str(), "--type", "lifecycle", lifecycle.c_str(), NULL);
1557 101 _exit(EXIT_FAILURE);
1558 } else {
1559
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 LogExcess(VB_PLUGIN, "Lifecycle parent process, resuming work.\n");
1560 // A plugin load or unload at runtime runs this on the main loop, so
1561 // a script that takes its time (starting or stopping a daemon) held
1562 // output for as long as it ran. Wait a bounded time, then let it
1563 // finish on its own; it isn't killed, since whatever it is starting
1564 // may be meant to outlive it.
1565 101 constexpr int LIFECYCLE_WAIT_MS = 5000;
1566
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 long long deadline = GetMonotonicTimeMS() + LIFECYCLE_WAIT_MS;
1567 while (true) {
1568
1/2
✓ Branch 0 taken 1103 times.
✗ Branch 1 not taken.
1103 int r = waitpid(pid, NULL, WNOHANG);
1569
4/6
✓ Branch 0 taken 1103 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 100 times.
✓ Branch 3 taken 1003 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 100 times.
1103 if (r == pid || (r < 0 && errno != EINTR)) {
1570 break;
1571 }
1572
3/4
✓ Branch 0 taken 1003 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1002 times.
1003 if (GetMonotonicTimeMS() >= deadline) {
1573
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 LogWarn(VB_PLUGIN, "Plugin %s: %s callback still running after %d seconds; continuing without it\n",
1574 mName.c_str(), lifecycle.c_str(), LIFECYCLE_WAIT_MS / 1000);
1575 1 break;
1576 }
1577
1/2
✓ Branch 0 taken 1002 times.
✗ Branch 1 not taken.
1002 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1578 1002 }
1579 }
1580
1/2
✓ Branch 0 taken 101 times.
✗ Branch 1 not taken.
101 }
1581