OSDN Git Service

Make sure global timeout is *not* triggered, right after tryContactHost() succeeded.
[mutilities/MUtilities.git] / src / UpdateChecker.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2018 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 #include <MUtils/Global.h>
23 #include <MUtils/UpdateChecker.h>
24 #include <MUtils/OSSupport.h>
25 #include <MUtils/Exception.h>
26
27 #include <QStringList>
28 #include <QFile>
29 #include <QFileInfo>
30 #include <QDir>
31 #include <QProcess>
32 #include <QUrl>
33 #include <QEventLoop>
34 #include <QTimer>
35 #include <QElapsedTimer>
36 #include <QSet>
37 #include <QHash>
38 #include <QQueue>
39
40 #include "Mirrors.h"
41
42 ///////////////////////////////////////////////////////////////////////////////
43 // CONSTANTS
44 ///////////////////////////////////////////////////////////////////////////////
45
46 static const char *GLOBALHEADER_ID = "!Update";
47
48 static const char *MIRROR_URL_POSTFIX[] = 
49 {
50         "update.ver",
51         "update_beta.ver",
52         NULL
53 };
54
55 static const int MIN_CONNSCORE = 5;
56 static const int QUICK_MIRRORS = 3;
57 static const int MAX_CONN_TIMEOUT = 16000;
58 static const int DOWNLOAD_TIMEOUT = 30000;
59
60 static const int VERSION_INFO_EXPIRES_MONTHS = 6;
61 static char *USER_AGENT_STR = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0"; /*use something innocuous*/
62
63 ////////////////////////////////////////////////////////////
64 // Utility Macros
65 ////////////////////////////////////////////////////////////
66
67 #define CHECK_CANCELLED() do \
68 { \
69         if(MUTILS_BOOLIFY(m_cancelled)) \
70         { \
71                 m_success.fetchAndStoreOrdered(0); \
72                 log("", "Update check has been cancelled by user!"); \
73                 setProgress(m_maxProgress); \
74                 setStatus(UpdateStatus_CancelledByUser); \
75                 return; \
76         } \
77 } \
78 while(0)
79
80 #define LOG_MESSAGE_HELPER(X) do \
81 { \
82         if (!(X).isNull()) \
83         { \
84                 emit messageLogged((X)); \
85         } \
86 } \
87 while(0)
88
89 #define STRICMP(X,Y) ((X).compare((Y), Qt::CaseInsensitive) == 0)
90
91 ////////////////////////////////////////////////////////////
92 // Helper Functions
93 ////////////////////////////////////////////////////////////
94
95 static QQueue<QString> buildRandomList(const char *const *values)
96 {
97         QQueue<QString> list;
98         while(*values)
99         {
100                 list.insert(MUtils::next_rand_u32(list.size() + 1), QString::fromLatin1(*(values++)));
101         }
102         return list;
103 }
104
105 static const QHash<QString, QString> *initEnvVars(void)
106 {
107         QHash<QString, QString> *const environment = new QHash<QString, QString>();
108         environment->insert(QLatin1String("CURL_HOME"), QDir::toNativeSeparators(MUtils::temp_folder()));
109         return environment;
110 }
111
112 ////////////////////////////////////////////////////////////
113 // Update Info Class
114 ////////////////////////////////////////////////////////////
115
116 MUtils::UpdateCheckerInfo::UpdateCheckerInfo(void)
117 {
118         resetInfo();
119 }
120         
121 void MUtils::UpdateCheckerInfo::resetInfo(void)
122 {
123         m_buildNo = 0;
124         m_buildDate.setDate(1900, 1, 1);
125         m_downloadSite.clear();
126         m_downloadAddress.clear();
127         m_downloadFilename.clear();
128         m_downloadFilecode.clear();
129         m_downloadChecksum.clear();
130 }
131
132 bool MUtils::UpdateCheckerInfo::isComplete(void)
133 {
134         return (this->m_buildNo > 0) &&
135                 (this->m_buildDate.year() >= 2010) &&
136                 (!this->m_downloadSite.isEmpty()) &&
137                 (!this->m_downloadAddress.isEmpty()) &&
138                 (!this->m_downloadFilename.isEmpty()) &&
139                 (!this->m_downloadFilecode.isEmpty()) &&
140                 (!this->m_downloadChecksum.isEmpty());
141 }
142
143 ////////////////////////////////////////////////////////////
144 // Constructor & Destructor
145 ////////////////////////////////////////////////////////////
146
147 MUtils::UpdateChecker::UpdateChecker(const QString &binCurl, const QString &binGnuPG, const QString &binKeys, const QString &applicationId, const quint32 &installedBuildNo, const bool betaUpdates, const bool testMode)
148 :
149         m_updateInfo(new UpdateCheckerInfo()),
150         m_binaryCurl(binCurl),
151         m_binaryGnuPG(binGnuPG),
152         m_binaryKeys(binKeys),
153         m_applicationId(applicationId),
154         m_installedBuildNo(installedBuildNo),
155         m_betaUpdates(betaUpdates),
156         m_testMode(testMode),
157         m_maxProgress(MIN_CONNSCORE + 5),
158         m_environment(initEnvVars())
159 {
160         m_status = UpdateStatus_NotStartedYet;
161         m_progress = 0;
162
163         if(m_binaryCurl.isEmpty() || m_binaryGnuPG.isEmpty() || m_binaryKeys.isEmpty())
164         {
165                 MUTILS_THROW("Tools not initialized correctly!");
166         }
167 }
168
169 MUtils::UpdateChecker::~UpdateChecker(void)
170 {
171 }
172
173 ////////////////////////////////////////////////////////////
174 // Public slots
175 ////////////////////////////////////////////////////////////
176
177 void MUtils::UpdateChecker::start(Priority priority)
178 {
179         m_success.fetchAndStoreOrdered(0);
180         m_cancelled.fetchAndStoreOrdered(0);
181         QThread::start(priority);
182 }
183
184 ////////////////////////////////////////////////////////////
185 // Protected functions
186 ////////////////////////////////////////////////////////////
187
188 void MUtils::UpdateChecker::run(void)
189 {
190         qDebug("Update checker thread started!");
191         MUTILS_EXCEPTION_HANDLER(m_testMode ? testMirrorsList() : checkForUpdates());
192         qDebug("Update checker thread completed.");
193 }
194
195 void MUtils::UpdateChecker::checkForUpdates(void)
196 {
197         // ----- Initialization ----- //
198
199         m_updateInfo->resetInfo();
200         setProgress(0);
201
202         // ----- Test Internet Connection ----- //
203
204         log("Checking your Internet connection...", "");
205         setStatus(UpdateStatus_CheckingConnection);
206
207         const int networkStatus = OS::network_status();
208         if(networkStatus == OS::NETWORK_TYPE_NON)
209         {
210                 if (!MUtils::OS::arguments().contains("ignore-network-status"))
211                 {
212                         log("Operating system reports that the computer is currently offline !!!");
213                         setProgress(m_maxProgress);
214                         setStatus(UpdateStatus_ErrorNoConnection);
215                         return;
216                 }
217         }
218         
219         msleep(333);
220         setProgress(1);
221
222         // ----- Test Known Hosts Connectivity ----- //
223
224         int connectionScore = 0;
225         QQueue<QString> mirrorList = buildRandomList(known_hosts);
226
227         for(int connectionTimout = 1000; connectionTimout <= MAX_CONN_TIMEOUT; connectionTimout *= 2)
228         {
229                 QElapsedTimer elapsedTimer;
230                 elapsedTimer.start();
231                 const qint64 globalTimeout = 2 * MIN_CONNSCORE * connectionTimout;
232                 forever
233                 {
234                         if (mirrorList.isEmpty())
235                         {
236                                 goto endLoop; /*depleted!*/
237                         }
238                         const QString hostName = mirrorList.dequeue();
239                         if (tryContactHost(hostName, connectionTimout))
240                         {
241                                 setProgress(1 + (++connectionScore));
242                                 if (connectionScore >= MIN_CONNSCORE)
243                                 {
244                                         goto endLoop; /*success*/
245                                 }
246                         }
247                         else
248                         {
249                                 mirrorList.enqueue(hostName);
250                                 if(elapsedTimer.hasExpired(globalTimeout))
251                                 {
252                                         break; /*timer expired*/
253                                 }
254                         }
255                         CHECK_CANCELLED();
256                 }
257         }
258
259 endLoop:
260         if(connectionScore < MIN_CONNSCORE)
261         {
262                 log("", "Connectivity test has failed: Internet connection appears to be broken!");
263                 setProgress(m_maxProgress);
264                 setStatus(UpdateStatus_ErrorConnectionTestFailed);
265                 return;
266         }
267
268         // ----- Fetch Update Info From Server ----- //
269
270         log("----", "", "Internet connection is operational, checking for updates online...");
271         setStatus(UpdateStatus_FetchingUpdates);
272
273         int mirrorCount = 0;
274         mirrorList = buildRandomList(update_mirrors);
275
276         while(!mirrorList.isEmpty())
277         {
278                 const QString currentMirror = mirrorList.takeFirst();
279                 const bool isQuick = (mirrorCount++ < QUICK_MIRRORS);
280                 if(tryUpdateMirror(m_updateInfo.data(), currentMirror, isQuick))
281                 {
282                         m_success.ref(); /*success*/
283                         break;
284                 }
285                 if (isQuick)
286                 {
287                         mirrorList.append(currentMirror); /*re-schedule*/
288                 }
289                 CHECK_CANCELLED();
290                 msleep(1);
291         }
292
293         msleep(333);
294         setProgress(MIN_CONNSCORE + 5);
295
296         // ----- Generate final result ----- //
297
298         if(MUTILS_BOOLIFY(m_success))
299         {
300                 if(m_updateInfo->m_buildNo > m_installedBuildNo)
301                 {
302                         setStatus(UpdateStatus_CompletedUpdateAvailable);
303                 }
304                 else if(m_updateInfo->m_buildNo == m_installedBuildNo)
305                 {
306                         setStatus(UpdateStatus_CompletedNoUpdates);
307                 }
308                 else
309                 {
310                         setStatus(UpdateStatus_CompletedNewVersionOlder);
311                 }
312         }
313         else
314         {
315                 setStatus(UpdateStatus_ErrorFetchUpdateInfo);
316         }
317 }
318
319 void MUtils::UpdateChecker::testMirrorsList(void)
320 {
321         QStringList mirrorList;
322         for(int i = 0; update_mirrors[i]; i++)
323         {
324                 mirrorList << QString::fromLatin1(update_mirrors[i]);
325         }
326
327         // ----- Test update mirrors ----- //
328
329         qDebug("\n[Mirror Sites]");
330         log("Testing all known mirror sites...", "", "---");
331
332         UpdateCheckerInfo updateInfo;
333         while (!mirrorList.isEmpty())
334         {
335                 const QString currentMirror = mirrorList.takeFirst();
336                 bool success = false;
337                 qDebug("Testing: %s", MUTILS_L1STR(currentMirror));
338                 log("", "Testing:", currentMirror, "");
339                 for (quint8 attempt = 0; attempt < 3; ++attempt)
340                 {
341                         updateInfo.resetInfo();
342                         if (tryUpdateMirror(&updateInfo, currentMirror, (!attempt)))
343                         {
344                                 success = true;
345                                 break;
346                         }
347                 }
348                 if (!success)
349                 {
350                         qWarning("\nUpdate mirror seems to be unavailable:\n%s\n", MUTILS_L1STR(currentMirror));
351                 }
352                 log("", "---");
353         }
354
355         // ----- Test known hosts ----- //
356
357         QStringList knownHostList;
358         for (int i = 0; known_hosts[i]; i++)
359         {
360                 knownHostList << QString::fromLatin1(known_hosts[i]);
361         }
362
363         qDebug("\n[Known Hosts]");
364         log("Testing all known hosts...", "", "---");
365
366         while(!knownHostList.isEmpty())
367         {
368                 const QString currentHost = knownHostList.takeFirst();
369                 qDebug("Testing: %s", MUTILS_L1STR(currentHost));
370                 log(QLatin1String(""), "Testing:", currentHost, "");
371                 if (!tryContactHost(currentHost, DOWNLOAD_TIMEOUT))
372                 {
373                         qWarning("\nConnectivity test FAILED on the following host:\n%s\n", MUTILS_L1STR(currentHost));
374                 }
375                 log("---");
376         }
377 }
378
379 ////////////////////////////////////////////////////////////
380 // PRIVATE FUNCTIONS
381 ////////////////////////////////////////////////////////////
382
383 void MUtils::UpdateChecker::setStatus(const int status)
384 {
385         if(m_status != status)
386         {
387                 m_status = status;
388                 emit statusChanged(status);
389         }
390 }
391
392 void MUtils::UpdateChecker::setProgress(const int progress)
393 {
394         const int value = qBound(0, progress, m_maxProgress);
395         if(m_progress != value)
396         {
397                 emit progressChanged(m_progress = value);
398         }
399 }
400
401 void MUtils::UpdateChecker::log(const QString &str1, const QString &str2, const QString &str3, const QString &str4)
402 {
403         LOG_MESSAGE_HELPER(str1);
404         LOG_MESSAGE_HELPER(str2);
405         LOG_MESSAGE_HELPER(str3);
406         LOG_MESSAGE_HELPER(str4);
407 }
408
409 bool MUtils::UpdateChecker::tryUpdateMirror(UpdateCheckerInfo *updateInfo, const QString &url, const bool &quick)
410 {
411         bool success = false;
412         log("", "Trying update mirror:", url, "");
413
414         if (quick)
415         {
416                 setProgress(MIN_CONNSCORE + 1);
417                 if (!tryContactHost(QUrl(url).host(), (MAX_CONN_TIMEOUT / 8)))
418                 {
419                         log("", "Mirror is too slow, skipping!");
420                         return false;
421                 }
422         }
423
424         const QString randPart = next_rand_str();
425         const QString outFileVers = QString("%1/%2.ver").arg(temp_folder(), randPart);
426         const QString outFileSign = QString("%1/%2.sig").arg(temp_folder(), randPart);
427
428         if (!getUpdateInfo(url, outFileVers, outFileSign))
429         {
430                 log("", "Oops: Download of update information has failed!");
431                 goto cleanUp;
432         }
433
434         log("Download completed, verifying signature:", "");
435         setProgress(MIN_CONNSCORE + 4);
436         if (!checkSignature(outFileVers, outFileSign))
437         {
438                 log("", "Bad signature detected, take care !!!");
439                 goto cleanUp;
440         }
441
442         log("", "Signature is valid, parsing update information:", "");
443         success = parseVersionInfo(outFileVers, updateInfo);
444
445 cleanUp:
446         QFile::remove(outFileVers);
447         QFile::remove(outFileSign);
448         return success;
449 }
450
451 bool MUtils::UpdateChecker::getUpdateInfo(const QString &url, const QString &outFileVers, const QString &outFileSign)
452 {
453         log("Downloading update information:", "");
454         setProgress(MIN_CONNSCORE + 2);
455         if(getFile(QUrl(QString("%1%2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileVers))
456         {
457                 if (!m_cancelled)
458                 {
459                         log( "Downloading signature file:", "");
460                         setProgress(MIN_CONNSCORE + 3);
461                         if (getFile(QUrl(QString("%1%2.sig2").arg(url, MIRROR_URL_POSTFIX[m_betaUpdates ? 1 : 0])), outFileSign))
462                         {
463                                 return true; /*completed*/
464                         }
465                 }
466         }
467         return false;
468 }
469
470 //----------------------------------------------------------
471 // PARSE UPDATE INFO
472 //----------------------------------------------------------
473
474 #define _CHECK_HEADER(ID,NAME) \
475         if (STRICMP(name, (NAME))) \
476         { \
477                 sectionId = (ID); \
478                 continue; \
479         }
480
481 #define _PARSE_TEXT(OUT,KEY) \
482         if (STRICMP(key, (KEY))) \
483         { \
484                 (OUT) = val; \
485                 break; \
486         }
487
488 #define _PARSE_UINT(OUT,KEY) \
489         if (STRICMP(key, (KEY))) \
490         { \
491                 bool _ok = false; \
492                 const unsigned int _tmp = val.toUInt(&_ok); \
493                 if (_ok) \
494                 { \
495                         (OUT) = _tmp; \
496                         break; \
497                 } \
498         }
499
500 #define _PARSE_DATE(OUT,KEY) \
501         if (STRICMP(key, (KEY))) \
502         { \
503                 const QDate _tmp = QDate::fromString(val, Qt::ISODate); \
504                 if (_tmp.isValid()) \
505                 { \
506                         (OUT) = _tmp; \
507                         break; \
508                 } \
509         }
510
511 bool MUtils::UpdateChecker::parseVersionInfo(const QString &file, UpdateCheckerInfo *const updateInfo)
512 {
513         updateInfo->resetInfo();
514
515         QFile data(file);
516         if(!data.open(QIODevice::ReadOnly))
517         {
518                 qWarning("Cannot open update info file for reading!");
519                 return false;
520         }
521
522         QDate updateInfoDate;
523         int sectionId = 0;
524         QRegExp regex_sec("^\\[(.+)\\]$"), regex_val("^([^=]+)=(.+)$");
525
526         while(!data.atEnd())
527         {
528                 QString line = QString::fromLatin1(data.readLine()).trimmed();
529                 if (regex_sec.indexIn(line) >= 0)
530                 {
531                         sectionId = 0; /*unknown section*/
532                         const QString name = regex_sec.cap(1).trimmed();
533                         log(QString("Sec: [%1]").arg(name));
534                         _CHECK_HEADER(1, GLOBALHEADER_ID)
535                         _CHECK_HEADER(2, m_applicationId)
536                         continue;
537                 }
538                 if (regex_val.indexIn(line) >= 0)
539                 {
540                         const QString key = regex_val.cap(1).trimmed();
541                         const QString val = regex_val.cap(2).trimmed();
542                         log(QString("Val: \"%1\" = \"%2\"").arg(key, val));
543                         switch (sectionId)
544                         {
545                         case 1:
546                                 _PARSE_DATE(updateInfoDate, "TimestampCreated")
547                                 break;
548                         case 2:
549                                 _PARSE_UINT(updateInfo->m_buildNo,          "BuildNo")
550                                 _PARSE_DATE(updateInfo->m_buildDate,        "BuildDate")
551                                 _PARSE_TEXT(updateInfo->m_downloadSite,     "DownloadSite")
552                                 _PARSE_TEXT(updateInfo->m_downloadAddress,  "DownloadAddress")
553                                 _PARSE_TEXT(updateInfo->m_downloadFilename, "DownloadFilename")
554                                 _PARSE_TEXT(updateInfo->m_downloadFilecode, "DownloadFilecode")
555                                 _PARSE_TEXT(updateInfo->m_downloadChecksum, "DownloadChecksum")
556                                 break;
557                         }
558                 }
559         }
560
561         if (!updateInfo->isComplete())
562         {
563                 log("", "WARNING: Update information is incomplete!");
564                 goto failure;
565         }
566
567         if(updateInfoDate.isValid())
568         {
569                 const QDate expiredDate = updateInfoDate.addMonths(VERSION_INFO_EXPIRES_MONTHS);
570                 if (expiredDate < OS::current_date())
571                 {
572                         log("", QString("WARNING: Update information has expired at %1!").arg(expiredDate.toString(Qt::ISODate)));
573                         goto failure;
574                 }
575         }
576         else
577         {
578                 log("", "WARNING: Timestamp is missing from update information header!");
579                 goto failure;
580         }
581
582         log("", "Success: Update information is complete.");
583         return true; /*success*/
584
585 failure:
586         updateInfo->resetInfo();
587         return false;
588 }
589
590 //----------------------------------------------------------
591 // EXTERNAL TOOLS
592 //----------------------------------------------------------
593
594 bool MUtils::UpdateChecker::getFile(const QUrl &url, const QString &outFile, const unsigned int maxRedir)
595 {
596         QFileInfo output(outFile);
597         output.setCaching(false);
598
599         if (output.exists())
600         {
601                 QFile::remove(output.canonicalFilePath());
602                 if (output.exists())
603                 {
604                         qWarning("Existing output file could not be found!");
605                         return false;
606                 }
607         }
608         
609         QStringList args(QLatin1String("-vsSNqkfL"));
610         args << "-m" << QString::number(DOWNLOAD_TIMEOUT / 1000);
611         args << "--max-redirs" << QString::number(maxRedir);
612         args << "-A" << USER_AGENT_STR;
613         args << "-e" << QString("%1://%2/;auto").arg(url.scheme(), url.host());
614         args << "-o" << output.fileName() << url.toString();
615
616         return execCurl(args, output.absolutePath(), DOWNLOAD_TIMEOUT);
617 }
618
619 bool MUtils::UpdateChecker::tryContactHost(const QString &hostname, const int &timeoutMsec)
620 {
621         log(QString("Connecting to host: %1").arg(hostname), "");
622
623         QStringList args(QLatin1String("-vsSNqkI"));
624         args << "-m" << QString::number(qMax(1, timeoutMsec / 1000));
625         args << "-A" << USER_AGENT_STR;
626         args << "-o" << OS::null_device() << QString("http://%1/").arg(hostname);
627         
628         return execCurl(args, temp_folder(), timeoutMsec);
629 }
630
631 bool MUtils::UpdateChecker::checkSignature(const QString &file, const QString &signature)
632 {
633         if (QFileInfo(file).absolutePath().compare(QFileInfo(signature).absolutePath(), Qt::CaseInsensitive) != 0)
634         {
635                 qWarning("CheckSignature: File and signature should be in same folder!");
636                 return false;
637         }
638
639         QString keyRingPath(m_binaryKeys);
640         bool removeKeyring = false;
641         if (QFileInfo(file).absolutePath().compare(QFileInfo(m_binaryKeys).absolutePath(), Qt::CaseInsensitive) != 0)
642         {
643                 keyRingPath = make_temp_file(QFileInfo(file).absolutePath(), "gpg");
644                 removeKeyring = true;
645                 if (!QFile::copy(m_binaryKeys, keyRingPath))
646                 {
647                         qWarning("CheckSignature: Failed to copy the key-ring file!");
648                         return false;
649                 }
650         }
651
652         QStringList args;
653         args << QStringList() << "--homedir" << ".";
654         args << "--keyring" << QFileInfo(keyRingPath).fileName();
655         args << QFileInfo(signature).fileName();
656         args << QFileInfo(file).fileName();
657
658         const int exitCode = execProcess(m_binaryGnuPG, args, QFileInfo(file).absolutePath(), DOWNLOAD_TIMEOUT);
659         if (exitCode != INT_MAX)
660         {
661                 log(QString().sprintf("Exited with code %d", exitCode));
662         }
663
664         if (removeKeyring)
665         {
666                 remove_file(keyRingPath);
667         }
668
669         return (exitCode == 0); /*completed*/
670 }
671
672 bool MUtils::UpdateChecker::execCurl(const QStringList &args, const QString &workingDir, const int timeout)
673 {
674         const int exitCode = execProcess(m_binaryCurl, args, workingDir, timeout + (timeout / 2));
675         if (exitCode != INT_MAX)
676         {
677                 switch (exitCode)
678                 {
679                         case  0: log(QLatin1String("DONE: Transfer completed successfully."), "");                     break;
680                         case  6: log(QLatin1String("ERROR: Remote host could not be resolved!"), "");                  break;
681                         case  7: log(QLatin1String("ERROR: Connection to remote host could not be established!"), ""); break;
682                         case 22: log(QLatin1String("ERROR: Requested URL was not found or returned an error!"), "");   break;
683                         case 28: log(QLatin1String("ERROR: Operation timed out !!!"), "");                             break;
684                         default: log(QString().sprintf("ERROR: Terminated with unknown code %d", exitCode), "");       break;
685                 }
686         }
687
688         return (exitCode == 0); /*completed*/
689 }
690
691 int MUtils::UpdateChecker::execProcess(const QString &programFile, const QStringList &args, const QString &workingDir, const int timeout)
692 {
693         QProcess process;
694         init_process(process, workingDir, true, NULL, m_environment.data());
695
696         QEventLoop loop;
697         connect(&process, SIGNAL(error(QProcess::ProcessError)), &loop, SLOT(quit()));
698         connect(&process, SIGNAL(finished(int, QProcess::ExitStatus)), &loop, SLOT(quit()));
699         connect(&process, SIGNAL(readyRead()), &loop, SLOT(quit()));
700
701         QTimer timer;
702         timer.setSingleShot(true);
703         connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
704
705         process.start(programFile, args);
706         if (!process.waitForStarted())
707         {
708                 log("PROCESS FAILED TO START !!!", "");
709                 qWarning("WARNING: %s process could not be created!", MUTILS_UTF8(QFileInfo(programFile).fileName()));
710                 return INT_MAX; /*failed to start*/
711         }
712
713         bool bAborted = false;
714         timer.start(qMax(timeout, 1500));
715
716         while (process.state() != QProcess::NotRunning)
717         {
718                 loop.exec();
719                 while (process.canReadLine())
720                 {
721                         const QString line = QString::fromLatin1(process.readLine()).simplified();
722                         if (line.length() > 1)
723                         {
724                                 log(line);
725                         }
726                 }
727                 const bool bCancelled = MUTILS_BOOLIFY(m_cancelled);
728                 if (bAborted = (bCancelled || ((!timer.isActive()) && (!process.waitForFinished(125)))))
729                 {
730                         log(bCancelled ? "CANCELLED BY USER !!!" : "PROCESS TIMEOUT !!!", "");
731                         qWarning("WARNING: %s process %s!", MUTILS_UTF8(QFileInfo(programFile).fileName()), bCancelled ? "cancelled" : "timed out");
732                         break; /*abort process*/
733                 }
734         }
735
736         timer.stop();
737         timer.disconnect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
738
739         if (bAborted)
740         {
741                 process.kill();
742                 process.waitForFinished(-1);
743         }
744
745         while (process.canReadLine())
746         {
747                 const QString line = QString::fromLatin1(process.readLine()).simplified();
748                 if (line.length() > 1)
749                 {
750                         log(line);
751                 }
752         }
753
754         return bAborted ? INT_MAX : process.exitCode();
755 }
756
757 ////////////////////////////////////////////////////////////
758 // SLOTS
759 ////////////////////////////////////////////////////////////
760
761 /*NONE*/
762
763 ////////////////////////////////////////////////////////////
764 // EVENTS
765 ////////////////////////////////////////////////////////////
766
767 /*NONE*/