annotate src/plugincandidates.cpp @ 44:0f7df035192d errorcode

Fix for older Qt 5 compatibility
author Chris Cannam
date Fri, 31 Aug 2018 12:11:26 +0100
parents 46e45b4a4a03
children 0d2d3c89fdf6
rev   line source
Chris@2 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@5 2 /*
Chris@40 3 Copyright (c) 2016-2018 Queen Mary, University of London
Chris@5 4
Chris@19 5 Permission is hereby granted, free of charge, to any person
Chris@19 6 obtaining a copy of this software and associated documentation
Chris@19 7 files (the "Software"), to deal in the Software without
Chris@19 8 restriction, including without limitation the rights to use, copy,
Chris@19 9 modify, merge, publish, distribute, sublicense, and/or sell copies
Chris@19 10 of the Software, and to permit persons to whom the Software is
Chris@19 11 furnished to do so, subject to the following conditions:
Chris@5 12
Chris@19 13 The above copyright notice and this permission notice shall be
Chris@19 14 included in all copies or substantial portions of the Software.
Chris@5 15
Chris@19 16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
Chris@19 17 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
Chris@19 18 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Chris@19 19 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
Chris@19 20 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
Chris@19 21 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
Chris@19 22 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Chris@5 23
Chris@19 24 Except as contained in this notice, the names of the Centre for
Chris@19 25 Digital Music and Queen Mary, University of London shall not be
Chris@19 26 used in advertising or otherwise to promote the sale, use or other
Chris@19 27 dealings in this Software without prior written authorization.
Chris@5 28 */
Chris@2 29
Chris@2 30 #include "plugincandidates.h"
Chris@2 31
Chris@40 32 #include "../version.h"
Chris@40 33
Chris@2 34 #include <set>
Chris@2 35 #include <stdexcept>
Chris@2 36 #include <iostream>
Chris@2 37
Chris@2 38 #include <QProcess>
Chris@2 39 #include <QDir>
Chris@6 40 #include <QTime>
Chris@2 41
Chris@4 42 #if defined(_WIN32)
Chris@2 43 #define PLUGIN_GLOB "*.dll"
Chris@4 44 #elif defined(__APPLE__)
Chris@2 45 #define PLUGIN_GLOB "*.dylib *.so"
Chris@2 46 #else
Chris@2 47 #define PLUGIN_GLOB "*.so"
Chris@2 48 #endif
Chris@2 49
Chris@2 50 using namespace std;
Chris@2 51
Chris@2 52 PluginCandidates::PluginCandidates(string helperExecutableName) :
Chris@6 53 m_helper(helperExecutableName),
Chris@6 54 m_logCallback(0)
Chris@2 55 {
Chris@2 56 }
Chris@2 57
Chris@6 58 void
Chris@6 59 PluginCandidates::setLogCallback(LogCallback *cb)
Chris@6 60 {
Chris@6 61 m_logCallback = cb;
Chris@6 62 }
Chris@6 63
Chris@2 64 vector<string>
Chris@4 65 PluginCandidates::getCandidateLibrariesFor(string tag) const
Chris@2 66 {
Chris@4 67 if (m_candidates.find(tag) == m_candidates.end()) return {};
Chris@4 68 else return m_candidates.at(tag);
Chris@2 69 }
Chris@2 70
Chris@2 71 vector<PluginCandidates::FailureRec>
Chris@4 72 PluginCandidates::getFailedLibrariesFor(string tag) const
Chris@2 73 {
Chris@4 74 if (m_failures.find(tag) == m_failures.end()) return {};
Chris@4 75 else return m_failures.at(tag);
Chris@2 76 }
Chris@2 77
Chris@6 78 void
Chris@6 79 PluginCandidates::log(string message)
Chris@6 80 {
Chris@24 81 if (m_logCallback) {
Chris@24 82 m_logCallback->log("PluginCandidates: " + message);
Chris@24 83 } else {
Chris@24 84 cerr << "PluginCandidates: " << message << endl;
Chris@24 85 }
Chris@6 86 }
Chris@6 87
Chris@2 88 vector<string>
Chris@2 89 PluginCandidates::getLibrariesInPath(vector<string> path)
Chris@2 90 {
Chris@2 91 vector<string> candidates;
Chris@2 92
Chris@2 93 for (string dirname: path) {
Chris@2 94
Chris@24 95 log("scanning directory " + dirname);
Chris@2 96
Chris@2 97 QDir dir(dirname.c_str(), PLUGIN_GLOB,
Chris@19 98 QDir::Name | QDir::IgnoreCase,
Chris@19 99 QDir::Files | QDir::Readable);
Chris@2 100
Chris@19 101 for (unsigned int i = 0; i < dir.count(); ++i) {
Chris@2 102 QString soname = dir.filePath(dir[i]);
Chris@11 103 // NB this means the library names passed to the helper
Chris@11 104 // are UTF-8 encoded
Chris@2 105 candidates.push_back(soname.toStdString());
Chris@2 106 }
Chris@2 107 }
Chris@2 108
Chris@2 109 return candidates;
Chris@2 110 }
Chris@2 111
Chris@2 112 void
Chris@2 113 PluginCandidates::scan(string tag,
Chris@19 114 vector<string> pluginPath,
Chris@19 115 string descriptorSymbolName)
Chris@2 116 {
Chris@40 117 string helperVersion = getHelperCompatibilityVersion();
Chris@40 118 if (helperVersion != CHECKER_COMPATIBILITY_VERSION) {
Chris@40 119 log("wrong plugin checker helper version found: expected v" +
Chris@40 120 string(CHECKER_COMPATIBILITY_VERSION) + ", found v" +
Chris@40 121 helperVersion);
Chris@43 122 throw runtime_error("wrong version of plugin load helper found");
Chris@40 123 }
Chris@40 124
Chris@2 125 vector<string> libraries = getLibrariesInPath(pluginPath);
Chris@2 126 vector<string> remaining = libraries;
Chris@2 127
Chris@2 128 int runlimit = 20;
Chris@2 129 int runcount = 0;
Chris@2 130
Chris@2 131 vector<string> result;
Chris@2 132
Chris@2 133 while (result.size() < libraries.size() && runcount < runlimit) {
Chris@19 134 vector<string> output = runHelper(remaining, descriptorSymbolName);
Chris@19 135 result.insert(result.end(), output.begin(), output.end());
Chris@19 136 int shortfall = int(remaining.size()) - int(output.size());
Chris@19 137 if (shortfall > 0) {
Chris@19 138 // Helper bailed out for some reason presumably associated
Chris@19 139 // with the plugin following the last one it reported
Chris@19 140 // on. Add a failure entry for that one and continue with
Chris@19 141 // the following ones.
Chris@6 142 string failed = *(remaining.rbegin() + shortfall - 1);
Chris@24 143 log("helper output ended before result for plugin " + failed);
Chris@19 144 result.push_back("FAILURE|" + failed + "|Plugin load check failed or timed out");
Chris@4 145 remaining = vector<string>
Chris@4 146 (remaining.rbegin(), remaining.rbegin() + shortfall - 1);
Chris@19 147 }
Chris@19 148 ++runcount;
Chris@2 149 }
Chris@2 150
Chris@2 151 recordResult(tag, result);
Chris@2 152 }
Chris@2 153
Chris@40 154 string
Chris@40 155 PluginCandidates::getHelperCompatibilityVersion()
Chris@2 156 {
Chris@2 157 QProcess process;
Chris@2 158 process.setReadChannel(QProcess::StandardOutput);
Chris@4 159 process.setProcessChannelMode(QProcess::ForwardedErrorChannel);
Chris@40 160 process.start(m_helper.c_str(), { "--version" });
Chris@40 161
Chris@2 162 if (!process.waitForStarted()) {
cannam@13 163 QProcess::ProcessError err = process.error();
cannam@13 164 if (err == QProcess::FailedToStart) {
cannam@13 165 std::cerr << "Unable to start helper process " << m_helper
cannam@13 166 << std::endl;
cannam@13 167 } else if (err == QProcess::Crashed) {
cannam@13 168 std::cerr << "Helper process " << m_helper
cannam@13 169 << " crashed on startup" << std::endl;
cannam@13 170 } else {
cannam@13 171 std::cerr << "Helper process " << m_helper
cannam@13 172 << " failed on startup with error code "
cannam@13 173 << err << std::endl;
cannam@13 174 }
Chris@19 175 throw runtime_error("plugin load helper failed to start");
Chris@2 176 }
Chris@40 177 process.waitForFinished();
Chris@40 178
Chris@40 179 QByteArray output = process.readAllStandardOutput();
Chris@40 180 while (output.endsWith('\n') || output.endsWith('\r')) {
Chris@40 181 output.chop(1);
Chris@40 182 }
Chris@40 183
Chris@44 184 string versionString = QString(output).toStdString();
Chris@40 185 log("read version string from helper: " + versionString);
Chris@40 186 return versionString;
Chris@40 187 }
Chris@40 188
Chris@40 189 vector<string>
Chris@40 190 PluginCandidates::runHelper(vector<string> libraries, string descriptor)
Chris@40 191 {
Chris@40 192 vector<string> output;
Chris@40 193
Chris@40 194 log("running helper " + m_helper + " with following library list:");
Chris@40 195 for (auto &lib: libraries) log(lib);
Chris@40 196
Chris@40 197 QProcess process;
Chris@40 198 process.setReadChannel(QProcess::StandardOutput);
Chris@40 199 process.setProcessChannelMode(QProcess::ForwardedErrorChannel);
Chris@40 200 process.start(m_helper.c_str(), { descriptor.c_str() });
Chris@40 201
Chris@40 202 if (!process.waitForStarted()) {
Chris@40 203 QProcess::ProcessError err = process.error();
Chris@40 204 if (err == QProcess::FailedToStart) {
Chris@40 205 std::cerr << "Unable to start helper process " << m_helper
Chris@40 206 << std::endl;
Chris@40 207 } else if (err == QProcess::Crashed) {
Chris@40 208 std::cerr << "Helper process " << m_helper
Chris@40 209 << " crashed on startup" << std::endl;
Chris@40 210 } else {
Chris@40 211 std::cerr << "Helper process " << m_helper
Chris@40 212 << " failed on startup with error code "
Chris@40 213 << err << std::endl;
Chris@40 214 }
Chris@40 215 throw runtime_error("plugin load helper failed to start");
Chris@40 216 }
Chris@40 217
Chris@2 218 for (auto &lib: libraries) {
Chris@19 219 process.write(lib.c_str(), lib.size());
Chris@19 220 process.write("\n", 1);
Chris@2 221 }
Chris@2 222
Chris@6 223 QTime t;
Chris@6 224 t.start();
Chris@33 225 int timeout = 15000; // ms
Chris@6 226
Chris@12 227 const int buflen = 4096;
Chris@4 228 bool done = false;
Chris@4 229
Chris@4 230 while (!done) {
Chris@19 231 char buf[buflen];
Chris@19 232 qint64 linelen = process.readLine(buf, buflen);
Chris@4 233 if (linelen > 0) {
Chris@4 234 output.push_back(buf);
Chris@4 235 done = (output.size() == libraries.size());
Chris@4 236 } else if (linelen < 0) {
Chris@4 237 // error case
Chris@24 238 log("received error code while reading from helper");
Chris@4 239 done = true;
Chris@19 240 } else {
Chris@4 241 // no error, but no line read (could just be between
Chris@4 242 // lines, or could be eof)
Chris@4 243 done = (process.state() == QProcess::NotRunning);
Chris@6 244 if (!done) {
Chris@6 245 if (t.elapsed() > timeout) {
Chris@6 246 // this is purely an emergency measure
Chris@24 247 log("timeout: helper took too long, killing it");
Chris@6 248 process.kill();
Chris@6 249 done = true;
Chris@6 250 } else {
Chris@6 251 process.waitForReadyRead(200);
Chris@6 252 }
Chris@6 253 }
Chris@2 254 }
Chris@4 255 }
Chris@4 256
Chris@4 257 if (process.state() != QProcess::NotRunning) {
Chris@4 258 process.close();
Chris@4 259 process.waitForFinished();
Chris@4 260 }
cannam@13 261
Chris@24 262 log("helper completed");
cannam@13 263
Chris@2 264 return output;
Chris@2 265 }
Chris@2 266
Chris@2 267 void
Chris@2 268 PluginCandidates::recordResult(string tag, vector<string> result)
Chris@2 269 {
Chris@4 270 for (auto &r: result) {
Chris@4 271
Chris@4 272 QString s(r.c_str());
Chris@4 273 QStringList bits = s.split("|");
Chris@6 274
Chris@24 275 log(("read output line from helper: " + s.trimmed()).toStdString());
Chris@6 276
Chris@4 277 if (bits.size() < 2 || bits.size() > 3) {
Chris@24 278 log("invalid output line (wrong number of |-separated fields)");
Chris@4 279 continue;
Chris@4 280 }
Chris@4 281
Chris@4 282 string status = bits[0].toStdString();
Chris@4 283
Chris@4 284 string library = bits[1].toStdString();
Chris@40 285 if (bits.size() == 2) {
Chris@40 286 library = bits[1].trimmed().toStdString();
Chris@40 287 }
Chris@4 288
Chris@4 289 if (status == "SUCCESS") {
Chris@4 290 m_candidates[tag].push_back(library);
Chris@4 291
Chris@4 292 } else if (status == "FAILURE") {
Chris@40 293
Chris@40 294 QString messageAndCode = "";
Chris@40 295 if (bits.size() > 2) {
Chris@40 296 messageAndCode = bits[2].trimmed();
Chris@40 297 }
Chris@40 298
Chris@40 299 PluginCheckCode code = PluginCheckCode::FAIL_OTHER;
Chris@40 300 string message = "";
Chris@40 301
Chris@40 302 QRegExp codeRE("^(.*) *\\[([0-9]+)\\]$");
Chris@40 303 if (codeRE.exactMatch(messageAndCode)) {
Chris@40 304 QStringList caps(codeRE.capturedTexts());
Chris@40 305 if (caps.length() == 3) {
Chris@40 306 message = caps[1].toStdString();
Chris@40 307 code = PluginCheckCode(caps[2].toInt());
Chris@40 308 log("split failure report into message and failure code "
Chris@40 309 + caps[2].toStdString());
Chris@40 310 } else {
Chris@40 311 log("unable to split out failure code from report");
Chris@40 312 }
Chris@40 313 } else {
Chris@40 314 log("failure message does not give a failure code");
Chris@40 315 }
Chris@40 316
Chris@40 317 if (message == "") {
Chris@40 318 message = messageAndCode.toStdString();
Chris@40 319 }
Chris@40 320
Chris@40 321 m_failures[tag].push_back({ library, code, message });
Chris@4 322
Chris@4 323 } else {
Chris@24 324 log("unexpected status \"" + status + "\" in output line");
Chris@4 325 }
Chris@4 326 }
Chris@2 327 }
Chris@2 328