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 = 0;
129 vm_size_t pageSize = 0;
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))
611 LOG(VB_GENERAL, LOG_ERR,
612 "Error: Unable to open selected file, missing read permissions?");
617 QDataStream stream(&
file);
618 stream.setByteOrder(QDataStream::LittleEndian);
619 for (quint64 tmp = 0, i = 0; i < 65536/
sizeof(tmp); i++)
625 file.seek(initialsize - 65536);
626 for (quint64 tmp = 0, i = 0; i < 65536/
sizeof(tmp); i++)
634 QString
output = QString(
"%1").arg(hash, 0, 16);
640 std::vector<char> msg(6,
static_cast<char>(0xFF));
641 std::array<char,6> macaddr {};
642 QStringList tokens = MAC.split(
':');
644 if (tokens.size() != 6)
646 LOG(VB_GENERAL, LOG_ERR,
647 QString(
"WakeOnLan(%1): Incorrect MAC length").arg(MAC));
651 for (
int y = 0; y < 6; y++)
654 macaddr[y] = tokens[y].toInt(&ok, 16);
658 LOG(VB_GENERAL, LOG_ERR,
659 QString(
"WakeOnLan(%1): Invalid MAC address").arg(MAC));
665 for (
int x = 0; x < 16; x++)
666 msg.insert(msg.end(), macaddr.cbegin(), macaddr.cend());
668 LOG(VB_NETWORK, LOG_INFO,
669 QString(
"WakeOnLan(): Sending WOL packet to %1").arg(MAC));
671 QUdpSocket udp_socket;
672 qlonglong msglen = msg.size();
673 return udp_socket.writeDatagram(
674 msg.data(), msglen, QHostAddress::Broadcast, 32767) == msglen;
694 const char *command =
"ps -ax | grep -i pulseaudio | grep -v grep > /dev/null";
696 const char *command =
"ps ch -C pulseaudio -o pid > /dev/null";
710 if ((-1 == ret) && (0 != errno) && (val >= 0))
712 LOG(VB_GENERAL, LOG_ERR,
"Failed to nice process" +
ENO);
721#ifdef _POSIX_PRIORITY_SCHEDULING
723 std::this_thread::sleep_for(5ms);
725 std::this_thread::sleep_for(5ms);
746#if defined(Q_OS_LINUX) && ( defined(Q_PROCESSOR_X86) || defined(Q_PROCESSOR_POWER) || \
747 defined(Q_PROCESSOR_IA64) )
751#include <sys/ptrace.h>
752#include <sys/syscall.h>
753#if __has_include(<linux/ioprio.h>)
758#include <linux/ioprio.h>
761static constexpr int8_t IOPRIO_BITS { 16 };
762static constexpr int8_t IOPRIO_CLASS_SHIFT { 13 };
763static constexpr int IOPRIO_PRIO_MASK { (1UL << IOPRIO_CLASS_SHIFT) - 1 };
764static constexpr int IOPRIO_PRIO_CLASS(
int mask)
765 {
return mask >> IOPRIO_CLASS_SHIFT; };
766static constexpr int IOPRIO_PRIO_DATA(
int mask)
767 {
return mask & IOPRIO_PRIO_MASK; };
768static constexpr int IOPRIO_PRIO_VALUE(
int pclass,
int data)
769 {
return (pclass << IOPRIO_CLASS_SHIFT) | data; };
771enum { IOPRIO_CLASS_NONE,IOPRIO_CLASS_RT,IOPRIO_CLASS_BE,IOPRIO_CLASS_IDLE, };
772enum { IOPRIO_WHO_PROCESS = 1, IOPRIO_WHO_PGRP, IOPRIO_WHO_USER, };
777 int new_ioclass {IOPRIO_CLASS_BE};
779 new_ioclass = IOPRIO_CLASS_RT;
781 new_ioclass = IOPRIO_CLASS_IDLE;
782 int new_iodata = (new_ioclass == IOPRIO_CLASS_BE) ? val : 0;
783 int new_ioprio = IOPRIO_PRIO_VALUE(new_ioclass, new_iodata);
786 int old_ioprio = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, pid);
787 if (old_ioprio == new_ioprio)
790 int ret = syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, pid, new_ioprio);
792 if (-1 == ret && EPERM == errno && IOPRIO_CLASS_BE != new_ioclass)
794 new_iodata = (new_ioclass == IOPRIO_CLASS_RT) ? 0 : 7;
795 new_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, new_iodata);
796 ret = syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, pid, new_ioprio);
813 QFileInfoList entries = aDir.entryInfoList(QDir::NoDotAndDotDot |
814 QDir::Dirs | QDir::Files);
815 int count = entries.size();
816 bool has_err =
false;
818 for (
int idx = 0; idx < count && !has_err; idx++)
820 const QFileInfo& entryInfo = entries[idx];
821 QString path = entryInfo.absoluteFilePath();
822 if (entryInfo.isDir())
835 if (!has_err && !aDir.rmdir(aDir.absolutePath()))
854 QString
LOC =
"setHttpProxy() - ";
857 QString var(qEnvironmentVariable(
"http_proxy"));
859 var = qEnvironmentVariable(
"HTTP_PROXY");
862 if (!var.startsWith(
"http://"))
863 var.prepend(
"http://");
865 QUrl url = QUrl(var, QUrl::TolerantMode);
866 QString host = url.host();
867 int port = url.port();
880 LOG(VB_NETWORK, LOG_INFO,
LOC +
881 QString(
"assuming port %1 on host %2") .arg(port).arg(host));
884 else if (!
ping(host, 1s))
886 LOG(VB_GENERAL, LOG_ERR,
LOC +
887 QString(
"cannot locate host %1").arg(host) +
888 "\n\t\t\tPlease check HTTP_PROXY environment variable!");
890 else if (!
telnet(host,port))
892 LOG(VB_GENERAL, LOG_ERR,
LOC +
893 QString(
"%1:%2 - cannot connect!").arg(host).arg(port) +
894 "\n\t\t\tPlease check HTTP_PROXY environment variable!");
898 LOG(VB_NETWORK, LOG_DEBUG,
LOC + QString(
"using http://%1:%2@%3:%4")
899 .arg(url.userName()).arg(url.password())
900 .arg(host).arg(port));
903 QNetworkProxy(QNetworkProxy::HttpCachingProxy,
904 host, port, url.userName(), url.password());
905 QNetworkProxy::setApplicationProxy(
p);
909 LOG(VB_NETWORK, LOG_DEBUG,
LOC +
"no HTTP_PROXY environment var.");
914 QNetworkProxyQuery query(QUrl(
"http://www.mythtv.org"));
916 proxies = QNetworkProxyFactory::systemProxyForQuery(query);
918 for (
const auto&
p : std::as_const(
proxies))
920 QString host =
p.hostName();
923 if (
p.type() == QNetworkProxy::NoProxy)
928 LOG(VB_NETWORK, LOG_ERR,
LOC +
929 "failed to contact proxy host " + host);
933 LOG(VB_NETWORK, LOG_INFO,
LOC + QString(
"using proxy host %1:%2")
934 .arg(host).arg(port));
935 QNetworkProxy::setApplicationProxy(
p);
941 if (!
p.user().isEmpty())
943 url =
"http://%1:%2@%3:%4",
944 url = url.arg(
p.user(),
p.password());
948 url =
"http://%1:%2";
951 url = url.arg(
p.hostName()).arg(
p.port());
952 setenv(
"HTTP_PROXY", url.toLatin1().constData(), 1);
953 setenv(
"http_proxy", url.toLatin1().constData(), 0);
958 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.