19#if QT_VERSION >= QT_VERSION_CHECK(6,5,0)
20#include <QtProcessorDetection>
21#include <QtSystemDetection>
25#include <sys/sysinfo.h>
33#include <sys/sysctl.h>
37#include <QReadWriteLock>
38#include <QNetworkProxy>
46#include <QHostAddress>
47#include <QRegularExpression>
48#include <QRegularExpressionMatchIterator>
66 struct sysinfo sinfo {};
67 if (sysinfo(&sinfo) == -1)
69 LOG(VB_GENERAL, LOG_ERR,
"sysinfo() error");
72 uptime = std::chrono::seconds(sinfo.uptime);
74#elif defined(Q_OS_BSD4)
75 std::array<int,2> mib { CTL_KERN, KERN_BOOTTIME };
76 struct timeval bootTime;
81 len =
sizeof(bootTime);
82 if (sysctl(mib.data(), 2, &bootTime, &len,
nullptr, 0) == -1)
84 LOG(VB_GENERAL, LOG_ERR,
"sysctl() error");
87 uptime = std::chrono::seconds(time(
nullptr) - bootTime.tv_sec);
88#elif defined(Q_OS_WINDOWS)
89 uptime = std::chrono::seconds(::GetTickCount() / 1000);
92 LOG(VB_GENERAL, LOG_NOTICE,
"Unknown platform. How do I get the uptime?");
107 [[maybe_unused]]
int &freeMB,
108 [[maybe_unused]]
int &totalVM,
109 [[maybe_unused]]
int &freeVM)
112 static constexpr size_t MB { 1024LL * 1024 };
113 struct sysinfo sinfo {};
114 if (sysinfo(&sinfo) == -1)
116 LOG(VB_GENERAL, LOG_ERR,
117 "getMemStats(): Error, sysinfo() call failed.");
121 totalMB = (int)((sinfo.totalram * sinfo.mem_unit)/MB);
122 freeMB = (int)((sinfo.freeram * sinfo.mem_unit)/MB);
123 totalVM = (int)((sinfo.totalswap * sinfo.mem_unit)/MB);
124 freeVM = (int)((sinfo.freeswap * sinfo.mem_unit)/MB);
126#elif defined(Q_OS_DARWIN)
128 mach_msg_type_number_t count;
130 vm_statistics_data_t s;
132 mp = mach_host_self();
135 if (host_page_size(mp, &pageSize) != KERN_SUCCESS)
138 count = HOST_VM_INFO_COUNT;
139 if (host_statistics(mp, HOST_VM_INFO,
140 (host_info_t)&s, &count) != KERN_SUCCESS)
142 LOG(VB_GENERAL, LOG_ERR,
"getMemStats(): Error, "
143 "failed to get virtual memory statistics.");
148 totalMB = (s.active_count + s.inactive_count +
149 s.wire_count + s.free_count) * pageSize / 1024;
150 freeMB = s.free_count * pageSize / 1024;
158 totalVM = (int)(fsInfo.getTotalSpace() >> 10);
159 freeVM = (int)(fsInfo.getFreeSpace() >> 10);
175#if !defined(Q_OS_WINDOWS) && !defined(Q_OS_ANDROID)
177 if (
getloadavg(loads.data(), loads.size()) != -1)
185 QStringList strlist(QString(
"QUERY_LOAD"));
189 load[0] = strlist[0].toDouble();
190 load[1] = strlist[1].toDouble();
191 load[2] = strlist[2].toDouble();
200 QStringList strlist(QString(
"QUERY_UPTIME"));
205 if (strlist[0].isEmpty() || !strlist[0].at(0).isNumber())
208 if (
sizeof(std::chrono::seconds::rep) ==
sizeof(
long long))
209 uptime = std::chrono::seconds(strlist[0].toLongLong());
210 else if (
sizeof(std::chrono::seconds::rep) ==
sizeof(
long))
211 uptime = std::chrono::seconds(strlist[0].toLong());
212 else if (
sizeof(std::chrono::seconds::rep) ==
sizeof(
int))
213 uptime = std::chrono::seconds(strlist[0].toInt());
220 QStringList strlist(QString(
"QUERY_MEMSTATS"));
224 totalMB = strlist[0].toInt();
225 freeMB = strlist[1].toInt();
226 totalVM = strlist[2].toInt();
227 freeVM = strlist[3].toInt();
251 QString cmd = QString(
"%systemroot%\\system32\\ping.exe -w %1 -n 1 %2>NUL")
252 .arg(
timeout.count()) .arg(host);
259 QHostAddress addr = QHostAddress(addrstr);
260#if defined(Q_OS_FREEBSD) || defined(Q_OS_DARWIN)
261 QString timeoutparam(
"-t");
264 QString timeoutparam(
"-w");
267 addr.protocol() == QAbstractSocket::IPv6Protocol ?
"ping6" :
"ping";
268 QString cmd = QString(
"%1 %2 %3 -c 1 %4 >/dev/null 2>&1")
269 .arg(pingcmd, timeoutparam,
270 QString::number(duration_cast<std::chrono::seconds>(
timeout).count()),
281bool telnet(
const QString &host,
int port)
285 bool connected = s->ConnectToHost(host, port);
315 char *buf =
new char[buflen];
322 if (!dst.isWritable() && !dst.isOpen())
324 odst = dst.open(QIODevice::Unbuffered |
325 QIODevice::WriteOnly |
326 QIODevice::Truncate);
329 if (!src.isReadable() && !src.isOpen())
330 osrc = src.open(QIODevice::Unbuffered|QIODevice::ReadOnly);
332 bool ok = dst.isWritable() && src.isReadable();
333 long long total_bytes = 0LL;
337 long long rlen = src.read(buf, buflen);
340 LOG(VB_GENERAL, LOG_ERR,
"read error");
349 while ((rlen-off>0) && ok)
351 long long wlen = dst.write(buf + off, rlen - off);
356 LOG(VB_GENERAL, LOG_ERR,
"write error");
369 return (ok) ? total_bytes : -1LL;
378 char tempfilename[MAX_PATH] =
"";
381 if (GetTempFileNameA(
temppath,
"mth", 0, tempfilename))
386 unlink(tempfilename);
387 ret = mkdir(tempfilename);
390 ret = open(tempfilename, O_CREAT | O_RDWR, S_IREAD | S_IWRITE);
392 QString tmpFileName(tempfilename);
394 QByteArray safe_name_template = name_template.toLatin1();
395 std::string ctemplate = safe_name_template.constData();
399 ret = (mkdtemp(ctemplate.data())) ? 0 : -1;
403 mode_t cur_umask = umask(S_IRWXO | S_IRWXG);
404 ret = mkstemp(ctemplate.data());
408 QString tmpFileName = QString::fromStdString(ctemplate);
413 LOG(VB_GENERAL, LOG_ERR, QString(
"createTempFile(%1), Error ")
414 .arg(name_template) +
ENO);
415 return name_template;
418 if (!dir && (ret >= 0))
444 QByteArray fname =
filename.toLatin1();
445 int ret = chmod(fname.constData(), 0666);
448 LOG(VB_GENERAL, LOG_ERR, QString(
"Unable to change permissions on file. (%1)").arg(
filename));
459 QByteArray tmp = query.toLocal8Bit();
460 std::cout << tmp.constData();
462 tmp = def.toLocal8Bit();
464 std::cout <<
" [" << tmp.constData() <<
"] ";
468 if (!isatty(fileno(stdin)) || !isatty(fileno(
stdout)))
470 std::cout <<
"\n[console is not interactive, using default '"
471 << tmp.constData() <<
"']\n";
475 QTextStream stream(stdin);
476 QString qresponse = stream.readLine();
478 if (qresponse.isEmpty())
489 QString str_resp =
getResponse(query, QString(
"%1").arg(def));
490 if (str_resp.isEmpty())
493 int resp = str_resp.toInt(&ok);
494 return (ok ? resp : def);
499 QStringList *intermediaries,
503 LOG(VB_GENERAL, LOG_DEBUG,
504 QString(
"getSymlinkTarget('%1', 0x%2, %3)")
505 .arg(start_file).arg((uint64_t)intermediaries,0,16)
510 QString cur_file = start_file;
511 QFileInfo fi(cur_file);
515 intermediaries->clear();
516 intermediaries->push_back(start_file);
519 for (
uint i = 0; (i <= maxLinks) && fi.isSymLink() &&
520 !(link = fi.symLinkTarget()).isEmpty(); i++)
522 cur_file = (link[0] ==
'/') ?
524 fi.absoluteDir().absolutePath() +
"/" + link;
526 if (intermediaries && !intermediaries->contains(cur_file))
527 intermediaries->push_back(cur_file);
529 fi = QFileInfo(cur_file);
535 for (
uint i = 0; i < intermediaries->size(); i++)
537 LOG(VB_GENERAL, LOG_DEBUG, QString(
" inter%1: %2")
538 .arg(i).arg((*intermediaries)[i]));
542 LOG(VB_GENERAL, LOG_DEBUG,
543 QString(
"getSymlinkTarget() -> '%1'")
544 .arg((!fi.isSymLink()) ? cur_file : QString()));
547 return (!fi.isSymLink()) ? cur_file : QString();
552 QStringList tokens = MAC.split(
':');
553 if (tokens.size() != 6)
555 LOG(VB_NETWORK, LOG_ERR,
556 QString(
"IsMACAddress(%1) = false, doesn't have 6 parts").arg(MAC));
560 for (
int y = 0; y < 6; y++)
562 if (tokens[y].isEmpty())
564 LOG(VB_NETWORK, LOG_ERR,
565 QString(
"IsMACAddress(%1) = false, part #%2 is empty.")
571 int value = tokens[y].toInt(&ok, 16);
574 LOG(VB_NETWORK, LOG_ERR,
575 QString(
"IsMACAddress(%1) = false, unable to "
576 "convert part '%2' to integer.")
577 .arg(MAC, tokens[y]));
583 LOG(VB_NETWORK, LOG_ERR,
584 QString(
"IsMACAddress(%1) = false, part #%2 "
585 "evaluates to %3 which is higher than 255.")
586 .arg(MAC).arg(y).arg(value));
591 LOG(VB_NETWORK, LOG_DEBUG, QString(
"IsMACAddress(%1) = true").arg(MAC));
598 QFileInfo fileinfo(
file);
599 qint64 initialsize = fileinfo.size();
602 if (initialsize == 0)
605 if (
file.open(QIODevice::ReadOnly))
609 LOG(VB_GENERAL, LOG_ERR,
610 "Error: Unable to open selected file, missing read permissions?");
615 QDataStream stream(&
file);
616 stream.setByteOrder(QDataStream::LittleEndian);
617 for (quint64 tmp = 0, i = 0; i < 65536/
sizeof(tmp); i++)
623 file.seek(initialsize - 65536);
624 for (quint64 tmp = 0, i = 0; i < 65536/
sizeof(tmp); i++)
632 QString
output = QString(
"%1").arg(hash, 0, 16);
638 std::vector<char> msg(6,
static_cast<char>(0xFF));
639 std::array<char,6> macaddr {};
640 QStringList tokens = MAC.split(
':');
642 if (tokens.size() != 6)
644 LOG(VB_GENERAL, LOG_ERR,
645 QString(
"WakeOnLan(%1): Incorrect MAC length").arg(MAC));
649 for (
int y = 0; y < 6; y++)
652 macaddr[y] = tokens[y].toInt(&ok, 16);
656 LOG(VB_GENERAL, LOG_ERR,
657 QString(
"WakeOnLan(%1): Invalid MAC address").arg(MAC));
663 for (
int x = 0; x < 16; x++)
664 msg.insert(msg.end(), macaddr.cbegin(), macaddr.cend());
666 LOG(VB_NETWORK, LOG_INFO,
667 QString(
"WakeOnLan(): Sending WOL packet to %1").arg(MAC));
669 QUdpSocket udp_socket;
670 qlonglong msglen = msg.size();
671 return udp_socket.writeDatagram(
672 msg.data(), msglen, QHostAddress::Broadcast, 32767) == msglen;
692 const char *command =
"ps -ax | grep -i pulseaudio | grep -v grep > /dev/null";
694 const char *command =
"ps ch -C pulseaudio -o pid > /dev/null";
708 if ((-1 == ret) && (0 != errno) && (val >= 0))
710 LOG(VB_GENERAL, LOG_ERR,
"Failed to nice process" +
ENO);
719#ifdef _POSIX_PRIORITY_SCHEDULING
721 std::this_thread::sleep_for(5ms);
723 std::this_thread::sleep_for(5ms);
744#if defined(Q_OS_LINUX) && ( defined(Q_PROCESSOR_X86) || defined(Q_PROCESSOR_POWER) || \
745 defined(Q_PROCESSOR_IA64) )
749#include <sys/ptrace.h>
750#include <sys/syscall.h>
751#if __has_include(<linux/ioprio.h>)
756#include <linux/ioprio.h>
759static constexpr int8_t IOPRIO_BITS { 16 };
760static constexpr int8_t IOPRIO_CLASS_SHIFT { 13 };
761static constexpr int IOPRIO_PRIO_MASK { (1UL << IOPRIO_CLASS_SHIFT) - 1 };
762static constexpr int IOPRIO_PRIO_CLASS(
int mask)
763 {
return mask >> IOPRIO_CLASS_SHIFT; };
764static constexpr int IOPRIO_PRIO_DATA(
int mask)
765 {
return mask & IOPRIO_PRIO_MASK; };
766static constexpr int IOPRIO_PRIO_VALUE(
int pclass,
int data)
767 {
return (pclass << IOPRIO_CLASS_SHIFT) | data; };
769enum { IOPRIO_CLASS_NONE,IOPRIO_CLASS_RT,IOPRIO_CLASS_BE,IOPRIO_CLASS_IDLE, };
770enum { IOPRIO_WHO_PROCESS = 1, IOPRIO_WHO_PGRP, IOPRIO_WHO_USER, };
775 int new_ioclass {IOPRIO_CLASS_BE};
777 new_ioclass = IOPRIO_CLASS_RT;
779 new_ioclass = IOPRIO_CLASS_IDLE;
780 int new_iodata = (new_ioclass == IOPRIO_CLASS_BE) ? val : 0;
781 int new_ioprio = IOPRIO_PRIO_VALUE(new_ioclass, new_iodata);
784 int old_ioprio = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, pid);
785 if (old_ioprio == new_ioprio)
788 int ret = syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, pid, new_ioprio);
790 if (-1 == ret && EPERM == errno && IOPRIO_CLASS_BE != new_ioclass)
792 new_iodata = (new_ioclass == IOPRIO_CLASS_RT) ? 0 : 7;
793 new_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, new_iodata);
794 ret = syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, pid, new_ioprio);
811 QFileInfoList entries = aDir.entryInfoList(QDir::NoDotAndDotDot |
812 QDir::Dirs | QDir::Files);
813 int count = entries.size();
814 bool has_err =
false;
816 for (
int idx = 0; idx < count && !has_err; idx++)
818 const QFileInfo& entryInfo = entries[idx];
819 QString path = entryInfo.absoluteFilePath();
820 if (entryInfo.isDir())
833 if (!has_err && !aDir.rmdir(aDir.absolutePath()))
852 QString
LOC =
"setHttpProxy() - ";
855 QString var(qEnvironmentVariable(
"http_proxy"));
857 var = qEnvironmentVariable(
"HTTP_PROXY");
860 if (!var.startsWith(
"http://"))
861 var.prepend(
"http://");
863 QUrl url = QUrl(var, QUrl::TolerantMode);
864 QString host = url.host();
865 int port = url.port();
878 LOG(VB_NETWORK, LOG_INFO,
LOC +
879 QString(
"assuming port %1 on host %2") .arg(port).arg(host));
882 else if (!
ping(host, 1s))
884 LOG(VB_GENERAL, LOG_ERR,
LOC +
885 QString(
"cannot locate host %1").arg(host) +
886 "\n\t\t\tPlease check HTTP_PROXY environment variable!");
888 else if (!
telnet(host,port))
890 LOG(VB_GENERAL, LOG_ERR,
LOC +
891 QString(
"%1:%2 - cannot connect!").arg(host).arg(port) +
892 "\n\t\t\tPlease check HTTP_PROXY environment variable!");
896 LOG(VB_NETWORK, LOG_DEBUG,
LOC + QString(
"using http://%1:%2@%3:%4")
897 .arg(url.userName()).arg(url.password())
898 .arg(host).arg(port));
901 QNetworkProxy(QNetworkProxy::HttpCachingProxy,
902 host, port, url.userName(), url.password());
903 QNetworkProxy::setApplicationProxy(
p);
907 LOG(VB_NETWORK, LOG_DEBUG,
LOC +
"no HTTP_PROXY environment var.");
912 QNetworkProxyQuery query(QUrl(
"http://www.mythtv.org"));
914 proxies = QNetworkProxyFactory::systemProxyForQuery(query);
916 for (
const auto&
p : std::as_const(
proxies))
918 QString host =
p.hostName();
921 if (
p.type() == QNetworkProxy::NoProxy)
926 LOG(VB_NETWORK, LOG_ERR,
LOC +
927 "failed to contact proxy host " + host);
931 LOG(VB_NETWORK, LOG_INFO,
LOC + QString(
"using proxy host %1:%2")
932 .arg(host).arg(port));
933 QNetworkProxy::setApplicationProxy(
p);
939 if (!
p.user().isEmpty())
941 url =
"http://%1:%2@%3:%4",
942 url = url.arg(
p.user(),
p.password());
946 url =
"http://%1:%2";
949 url = url.arg(
p.hostName()).arg(
p.port());
950 setenv(
"HTTP_PROXY", url.toLatin1().constData(), 1);
951 setenv(
"http_proxy", url.toLatin1().constData(), 0);
956 LOG(VB_NETWORK, LOG_ERR,
LOC +
"failed to find a network proxy");
static QString resolveAddress(const QString &host, ResolveType type=ResolveAny, bool keepscope=false)
if host is an IP address, it will be returned or resolved otherwise.
bool SendReceiveStringList(QStringList &strlist, bool quickTimeout=false, bool block=true)
Send a message to the backend and wait for a response.
Class for communcating between myth backends and frontends.
@ GENERIC_EXIT_OK
Exited with no error.
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
#define ENO
This can be appended to the LOG args with "+".
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
bool RemoteGetUptime(std::chrono::seconds &uptime)
bool getMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
Returns memory statistics in megabytes.
loadArray getLoadAvgs(void)
Returns the system load averages.
bool RemoteGetLoad(loadArray &load)
bool MythWakeup(const QString &wakeUpCommand, uint flags, std::chrono::seconds timeout)
bool MythRemoveDirectory(QDir &aDir)
bool IsMACAddress(const QString &MAC)
int intResponse(const QString &query, int def)
In an interactive shell, prompt the user to input a number.
bool getUptime(std::chrono::seconds &uptime)
Returns uptime statistics.
QString getResponse(const QString &query, const QString &def)
In an interactive shell, prompt the user to input a string.
bool IsPulseAudioRunning(void)
Is A/V Sync destruction daemon is running on this host?
QString getSymlinkTarget(const QString &start_file, QStringList *intermediaries, unsigned maxLinks)
void setHttpProxy(void)
Get network proxy settings from OS, and use for [Q]Http[Comms].
bool WakeOnLAN(const QString &MAC)
QString FileHash(const QString &filename)
bool telnet(const QString &host, int port)
Can we talk to port on host?
bool RemoteGetMemStats(int &totalMB, int &freeMB, int &totalVM, int &freeVM)
bool ping(const QString &host, std::chrono::milliseconds timeout)
Can we ping host within timeout seconds?
bool myth_ioprio(int)
Allows setting the I/O priority of the current process/thread.
bool makeFileAccessible(const QString &filename)
QString createTempFile(QString name_template, bool dir)
std::array< double, 3 > loadArray
@ kMSDontBlockInputDevs
avoid blocking LIRC & Joystick Menu
@ kMSProcessEvents
process events while waiting
@ kMSDontDisableDrawing
avoid disabling UI drawing
uint myth_system(const QString &command, uint flags, std::chrono::seconds timeout)
MBASE_PUBLIC long long copy(QFile &dst, QFile &src, uint block_size=0)
Copies src file to dst file.