Chris@2: /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */ Chris@5: /* Chris@5: Copyright (c) 2016 Queen Mary, University of London Chris@5: Chris@5: Permission is hereby granted, free of charge, to any person Chris@5: obtaining a copy of this software and associated documentation Chris@5: files (the "Software"), to deal in the Software without Chris@5: restriction, including without limitation the rights to use, copy, Chris@5: modify, merge, publish, distribute, sublicense, and/or sell copies Chris@5: of the Software, and to permit persons to whom the Software is Chris@5: furnished to do so, subject to the following conditions: Chris@5: Chris@5: The above copyright notice and this permission notice shall be Chris@5: included in all copies or substantial portions of the Software. Chris@5: Chris@5: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, Chris@5: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF Chris@5: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND Chris@5: NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY Chris@5: CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF Chris@5: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION Chris@5: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Chris@5: Chris@5: Except as contained in this notice, the names of the Centre for Chris@5: Digital Music and Queen Mary, University of London shall not be Chris@5: used in advertising or otherwise to promote the sale, use or other Chris@5: dealings in this Software without prior written authorization. Chris@5: */ Chris@2: Chris@2: #include "plugincandidates.h" Chris@2: Chris@2: #include Chris@2: #include Chris@2: #include Chris@2: Chris@2: #include Chris@2: #include Chris@6: #include Chris@2: Chris@4: #if defined(_WIN32) Chris@2: #define PLUGIN_GLOB "*.dll" Chris@4: #elif defined(__APPLE__) Chris@2: #define PLUGIN_GLOB "*.dylib *.so" Chris@2: #else Chris@2: #define PLUGIN_GLOB "*.so" Chris@2: #endif Chris@2: Chris@2: using namespace std; Chris@2: Chris@2: PluginCandidates::PluginCandidates(string helperExecutableName) : Chris@6: m_helper(helperExecutableName), Chris@6: m_logCallback(0) Chris@2: { Chris@2: } Chris@2: Chris@6: void Chris@6: PluginCandidates::setLogCallback(LogCallback *cb) Chris@6: { Chris@6: m_logCallback = cb; Chris@6: } Chris@6: Chris@2: vector Chris@4: PluginCandidates::getCandidateLibrariesFor(string tag) const Chris@2: { Chris@4: if (m_candidates.find(tag) == m_candidates.end()) return {}; Chris@4: else return m_candidates.at(tag); Chris@2: } Chris@2: Chris@2: vector Chris@4: PluginCandidates::getFailedLibrariesFor(string tag) const Chris@2: { Chris@4: if (m_failures.find(tag) == m_failures.end()) return {}; Chris@4: else return m_failures.at(tag); Chris@2: } Chris@2: Chris@6: void Chris@6: PluginCandidates::log(string message) Chris@6: { Chris@6: if (m_logCallback) m_logCallback->log("PluginCandidates: " + message); Chris@6: } Chris@6: Chris@2: vector Chris@2: PluginCandidates::getLibrariesInPath(vector path) Chris@2: { Chris@2: vector candidates; Chris@2: Chris@2: for (string dirname: path) { Chris@2: Chris@6: log("scanning directory " + dirname + "\n"); Chris@2: Chris@2: QDir dir(dirname.c_str(), PLUGIN_GLOB, Chris@2: QDir::Name | QDir::IgnoreCase, Chris@2: QDir::Files | QDir::Readable); Chris@2: Chris@2: for (unsigned int i = 0; i < dir.count(); ++i) { Chris@2: QString soname = dir.filePath(dir[i]); Chris@2: candidates.push_back(soname.toStdString()); Chris@2: } Chris@2: } Chris@2: Chris@2: return candidates; Chris@2: } Chris@2: Chris@2: void Chris@2: PluginCandidates::scan(string tag, Chris@2: vector pluginPath, Chris@4: string descriptorSymbolName) Chris@2: { Chris@2: vector libraries = getLibrariesInPath(pluginPath); Chris@2: vector remaining = libraries; Chris@2: Chris@2: int runlimit = 20; Chris@2: int runcount = 0; Chris@2: Chris@2: vector result; Chris@2: Chris@2: while (result.size() < libraries.size() && runcount < runlimit) { Chris@4: vector output = runHelper(remaining, descriptorSymbolName); Chris@2: result.insert(result.end(), output.begin(), output.end()); Chris@2: int shortfall = int(remaining.size()) - int(output.size()); Chris@2: if (shortfall > 0) { Chris@2: // Helper bailed out for some reason presumably associated Chris@2: // with the plugin following the last one it reported Chris@4: // on. Add a failure entry for that one and continue with Chris@4: // the following ones. Chris@6: string failed = *(remaining.rbegin() + shortfall - 1); Chris@6: log("helper output ended before result for plugin " + failed + "\n"); Chris@6: result.push_back("FAILURE|" + failed + "|Plugin load check failed or timed out"); Chris@4: remaining = vector Chris@4: (remaining.rbegin(), remaining.rbegin() + shortfall - 1); Chris@2: } Chris@2: ++runcount; Chris@2: } Chris@2: Chris@2: recordResult(tag, result); Chris@2: } Chris@2: Chris@2: vector Chris@2: PluginCandidates::runHelper(vector libraries, string descriptor) Chris@2: { Chris@2: vector output; Chris@6: Chris@6: log("running helper with following library list:\n"); Chris@6: for (auto &lib: libraries) log(lib + "\n"); Chris@2: Chris@2: QProcess process; Chris@2: process.setReadChannel(QProcess::StandardOutput); Chris@4: process.setProcessChannelMode(QProcess::ForwardedErrorChannel); Chris@2: process.start(m_helper.c_str(), { descriptor.c_str() }); Chris@2: if (!process.waitForStarted()) { Chris@2: cerr << "helper failed to start" << endl; Chris@2: throw runtime_error("plugin load helper failed to start"); Chris@2: } Chris@2: for (auto &lib: libraries) { Chris@2: process.write(lib.c_str(), lib.size()); Chris@2: process.write("\n", 1); Chris@2: } Chris@2: Chris@6: QTime t; Chris@6: t.start(); Chris@6: int timeout = 3000; // ms Chris@6: Chris@2: int buflen = 4096; Chris@4: bool done = false; Chris@4: Chris@4: while (!done) { Chris@2: char buf[buflen]; Chris@2: qint64 linelen = process.readLine(buf, buflen); Chris@4: if (linelen > 0) { Chris@4: output.push_back(buf); Chris@4: done = (output.size() == libraries.size()); Chris@4: } else if (linelen < 0) { Chris@4: // error case Chris@6: log("received error code while reading from helper\n"); Chris@4: done = true; Chris@4: } else { Chris@4: // no error, but no line read (could just be between Chris@4: // lines, or could be eof) Chris@4: done = (process.state() == QProcess::NotRunning); Chris@6: if (!done) { Chris@6: if (t.elapsed() > timeout) { Chris@6: // this is purely an emergency measure Chris@6: log("timeout: helper took too long, killing it\n"); Chris@6: process.kill(); Chris@6: done = true; Chris@6: } else { Chris@6: process.waitForReadyRead(200); Chris@6: } Chris@6: } Chris@2: } Chris@4: } Chris@4: Chris@4: if (process.state() != QProcess::NotRunning) { Chris@4: process.close(); Chris@4: process.waitForFinished(); Chris@4: } Chris@2: Chris@2: return output; Chris@2: } Chris@2: Chris@2: void Chris@2: PluginCandidates::recordResult(string tag, vector result) Chris@2: { Chris@4: for (auto &r: result) { Chris@4: Chris@4: QString s(r.c_str()); Chris@4: QStringList bits = s.split("|"); Chris@6: Chris@6: log("read output line from helper: " + r); Chris@6: Chris@4: if (bits.size() < 2 || bits.size() > 3) { Chris@6: log("invalid output line (wrong number of |-separated fields)\n"); Chris@4: continue; Chris@4: } Chris@4: Chris@4: string status = bits[0].toStdString(); Chris@4: Chris@4: string library = bits[1].toStdString(); Chris@4: if (bits.size() == 2) library = bits[1].trimmed().toStdString(); Chris@4: Chris@4: string message = ""; Chris@4: if (bits.size() > 2) message = bits[2].trimmed().toStdString(); Chris@4: Chris@4: if (status == "SUCCESS") { Chris@4: m_candidates[tag].push_back(library); Chris@4: Chris@4: } else if (status == "FAILURE") { Chris@4: m_failures[tag].push_back({ library, message }); Chris@4: Chris@4: } else { Chris@6: log("unexpected status \"" + status + "\" in output line\n"); Chris@4: } Chris@4: } Chris@2: } Chris@2: