18#if QT_VERSION >= QT_VERSION_CHECK(6,0,0)
19#include <QtProcessorDetection>
20#include <QtSystemDetection>
24#include <sys/sysinfo.h>
32#include <sys/sysctl.h>
36#include <QReadWriteLock>
37#include <QNetworkProxy>
45#include <QHostAddress>
46#include <QRegularExpression>
47#include <QRegularExpressionMatchIterator>
65 struct sysinfo sinfo {};
66 if (sysinfo(&sinfo) == -1)
68 LOG(VB_GENERAL, LOG_ERR,
"sysinfo() error");
71 uptime = std::chrono::seconds(sinfo.uptime);
73#elif defined(Q_OS_BSD4)
74 std::array<int,2> mib { CTL_KERN, KERN_BOOTTIME };
75 struct timeval bootTime;
80 len =
sizeof(bootTime);
81 if (sysctl(mib.data(), 2, &bootTime, &len,
nullptr, 0) == -1)
83 LOG(VB_GENERAL, LOG_ERR,
"sysctl() error");
86 uptime = std::chrono::seconds(time(
nullptr) - bootTime.tv_sec);
87#elif defined(Q_OS_WINDOWS)
88 uptime = std::chrono::seconds(::GetTickCount() / 1000);
91 LOG(VB_GENERAL, LOG_NOTICE,
"Unknown platform. How do I get the uptime?");
106 [[maybe_unused]]
int &freeMB,
107 [[maybe_unused]]
int &totalVM,
108 [[maybe_unused]]
int &freeVM)
111 static constexpr size_t MB { 1024LL * 1024 };
112 struct sysinfo sinfo {};
113 if (sysinfo(&sinfo) == -1)
115 LOG(VB_GENERAL, LOG_ERR,
116 "getMemStats(): Error, sysinfo() call failed.");
120 totalMB = (int)((sinfo.totalram * sinfo.mem_unit)/MB);
121 freeMB = (int)((sinfo.freeram * sinfo.mem_unit)/MB);
122 totalVM = (int)((sinfo.totalswap * sinfo.mem_unit)/MB);
123 freeVM = (int)((sinfo.freeswap * sinfo.mem_unit)/MB);
125#elif defined(Q_OS_DARWIN)
127 mach_msg_type_number_t count;
129 vm_statistics_data_t s;
131 mp = mach_host_self();
134 if (host_page_size(mp, &pageSize) != KERN_SUCCESS)
137 count = HOST_VM_INFO_COUNT;
138 if (host_statistics(mp, HOST_VM_INFO,
139 (host_info_t)&s, &count) != KERN_SUCCESS)
141 LOG(VB_GENERAL, LOG_ERR,
"getMemStats(): Error, "
142 "failed to get virtual memory statistics.");
147 totalMB = (s.active_count + s.inactive_count +
148 s.wire_count + s.free_count) * pageSize / 1024;
149 freeMB = s.free_count * pageSize / 1024;
157 totalVM = (int)(fsInfo.getTotalSpace() >> 10);
158 freeVM = (int)(fsInfo.getFreeSpace() >> 10);
174#if !defined(Q_OS_WINDOWS) && !defined(Q_OS_ANDROID)
176 if (
getloadavg(loads.data(), loads.size()) != -1)
184 QStringList strlist(QString(
"QUERY_LOAD"));
188 load[0] = strlist[0].toDouble();
189 load[1] = strlist[1].toDouble();
190 load[2] = strlist[2].toDouble();
199 QStringList strlist(QString(
"QUERY_UPTIME"));
204 if (strlist[0].isEmpty() || !strlist[0].at(0).isNumber())
207 if (
sizeof(std::chrono::seconds::rep) ==
sizeof(
long long))
208 uptime = std::chrono::seconds(strlist[0].toLongLong());
209 else if (
sizeof(std::chrono::seconds::rep) ==
sizeof(
long))
210 uptime = std::chrono::seconds(strlist[0].toLong());
211 else if (
sizeof(std::chrono::seconds::rep) ==
sizeof(
int))
212 uptime = std::chrono::seconds(strlist[0].toInt());
219 QStringList strlist(QString(
"QUERY_MEMSTATS"));
223 totalMB = strlist[0].toInt();
224 freeMB = strlist[1].toInt();
225 totalVM = strlist[2].toInt();
226 freeVM = strlist[3].toInt();
250 QString cmd = QString(
"%systemroot%\\system32\\ping.exe -w %1 -n 1 %2>NUL")
251 .arg(
timeout.count()) .arg(host);
258 QHostAddress addr = QHostAddress(addrstr);
259#if defined(Q_OS_FREEBSD) || defined(Q_OS_DARWIN)
260 QString timeoutparam(
"-t");
263 QString timeoutparam(
"-w");
266 addr.protocol() == QAbstractSocket::IPv6Protocol ?
"ping6" :
"ping";
267 QString cmd = QString(
"%1 %2 %3 -c 1 %4 >/dev/null 2>&1")
268 .arg(pingcmd, timeoutparam,
269 QString::number(duration_cast<std::chrono::seconds>(
timeout).count()),
280bool telnet(
const QString &host,
int port)
284 bool connected = s->ConnectToHost(host, port);
314 char *buf =
new char[buflen];
321 if (!dst.isWritable() && !dst.isOpen())
323 odst = dst.open(QIODevice::Unbuffered |
324 QIODevice::WriteOnly |
325 QIODevice::Truncate);
328 if (!src.isReadable() && !src.isOpen())
329 osrc = src.open(QIODevice::Unbuffered|QIODevice::ReadOnly);
331 bool ok = dst.isWritable() && src.isReadable();
332 long long total_bytes = 0LL;
336 long long rlen = src.read(buf, buflen);
339 LOG(VB_GENERAL, LOG_ERR,
"read error");
348 while ((rlen-off>0) && ok)
350 long long wlen = dst.write(buf + off, rlen - off);
355 LOG(VB_GENERAL, LOG_ERR,
"write error");
368 return (ok) ? total_bytes : -1LL;
377 char tempfilename[MAX_PATH] =
"";
380 if (GetTempFileNameA(
temppath,
"mth", 0, tempfilename))
385 unlink(tempfilename);
386 ret = mkdir(tempfilename);
389 ret = open(tempfilename, O_CREAT | O_RDWR, S_IREAD | S_IWRITE);
391 QString tmpFileName(tempfilename);
393 QByteArray safe_name_template = name_template.toLatin1();
394 const char *
tmp = safe_name_template.constData();
395 char *ctemplate = strdup(
tmp);
399 ret = (mkdtemp(ctemplate)) ? 0 : -1;
403 mode_t cur_umask = umask(S_IRWXO | S_IRWXG);
404 ret = mkstemp(ctemplate);
408 QString tmpFileName(ctemplate);
414 LOG(VB_GENERAL, LOG_ERR, QString(
"createTempFile(%1), Error ")
415 .arg(name_template) +
ENO);
416 return name_template;
419 if (!dir && (ret >= 0))
445 QByteArray fname =
filename.toLatin1();
446 int ret = chmod(fname.constData(), 0666);
449 LOG(VB_GENERAL, LOG_ERR, QString(
"Unable to change permissions on file. (%1)").arg(
filename));
460 QByteArray
tmp = query.toLocal8Bit();
461 std::cout <<
tmp.constData();
463 tmp = def.toLocal8Bit();
465 std::cout <<
" [" <<
tmp.constData() <<
"] ";
469 if (!isatty(fileno(stdin)) || !isatty(fileno(
stdout)))
471 std::cout << std::endl <<
"[console is not interactive, using default '"
472 <<
tmp.constData() <<
"']" << std::endl;
476 QTextStream stream(stdin);
477 QString qresponse = stream.readLine();
479 if (qresponse.isEmpty())
490 QString str_resp =
getResponse(query, QString(
"%1").arg(def));
491 if (str_resp.isEmpty())
494 int resp = str_resp.toInt(&ok);
495 return (ok ? resp : def);
500 QStringList *intermediaries,
504 LOG(VB_GENERAL, LOG_DEBUG,
505 QString(
"getSymlinkTarget('%1', 0x%2, %3)")
506 .arg(start_file).arg((uint64_t)intermediaries,0,16)
511 QString cur_file = start_file;
512 QFileInfo fi(cur_file);
516 intermediaries->clear();
517 intermediaries->push_back(start_file);
520 for (
uint i = 0; (i <= maxLinks) && fi.isSymLink() &&
521 !(link = fi.symLinkTarget()).isEmpty(); i++)
523 cur_file = (link[0] ==
'/') ?
525 fi.absoluteDir().absolutePath() +
"/" + link;
527 if (intermediaries && !intermediaries->contains(cur_file))
528 intermediaries->push_back(cur_file);
530 fi = QFileInfo(cur_file);
536 for (
uint i = 0; i < intermediaries->size(); i++)
538 LOG(VB_GENERAL, LOG_DEBUG, QString(
" inter%1: %2")
539 .arg(i).arg((*intermediaries)[i]));
543 LOG(VB_GENERAL, LOG_DEBUG,
544 QString(
"getSymlinkTarget() -> '%1'")
545 .arg((!fi.isSymLink()) ? cur_file : QString()));
548 return (!fi.isSymLink()) ? cur_file : QString();
553 QStringList tokens = MAC.split(
':');
554 if (tokens.size() != 6)
556 LOG(VB_NETWORK, LOG_ERR,
557 QString(
"IsMACAddress(%1) = false, doesn't have 6 parts").arg(MAC));
561 for (
int y = 0; y < 6; y++)
563 if (tokens[y].isEmpty())
565 LOG(VB_NETWORK, LOG_ERR,
566 QString(
"IsMACAddress(%1) = false, part #%2 is empty.")
572 int value = tokens[y].toInt(&ok, 16);
575 LOG(VB_NETWORK, LOG_ERR,
576 QString(
"IsMACAddress(%1) = false, unable to "
577 "convert part '%2' to integer.")
578 .arg(MAC, tokens[y]));
584 LOG(VB_NETWORK, LOG_ERR,
585 QString(
"IsMACAddress(%1) = false, part #%2 "
586 "evaluates to %3 which is higher than 255.")
587 .arg(MAC).arg(y).arg(value));
592 LOG(VB_NETWORK, LOG_DEBUG, QString(
"IsMACAddress(%1) = true").arg(MAC));
599 QFileInfo fileinfo(
file);
600 qint64 initialsize = fileinfo.size();
603 if (initialsize == 0)
606 if (
file.open(QIODevice::ReadOnly))
610 LOG(VB_GENERAL, LOG_ERR,
611 "Error: Unable to open selected file, missing read permissions?");
616 QDataStream stream(&
file);
617 stream.setByteOrder(QDataStream::LittleEndian);
618 for (quint64
tmp = 0, i = 0; i < 65536/
sizeof(
tmp); i++)
624 file.seek(initialsize - 65536);
625 for (quint64
tmp = 0, i = 0; i < 65536/
sizeof(
tmp); i++)
633 QString
output = QString(
"%1").arg(hash, 0, 16);
639 std::vector<char> msg(6,
static_cast<char>(0xFF));
640 std::array<char,6> macaddr {};
641 QStringList tokens = MAC.split(
':');
643 if (tokens.size() != 6)
645 LOG(VB_GENERAL, LOG_ERR,
646 QString(
"WakeOnLan(%1): Incorrect MAC length").arg(MAC));
650 for (
int y = 0; y < 6; y++)
653 macaddr[y] = tokens[y].toInt(&ok, 16);
657 LOG(VB_GENERAL, LOG_ERR,
658 QString(
"WakeOnLan(%1): Invalid MAC address").arg(MAC));
664 for (
int x = 0; x < 16; x++)
665 msg.insert(msg.end(), macaddr.cbegin(), macaddr.cend());
667 LOG(VB_NETWORK, LOG_INFO,
668 QString(
"WakeOnLan(): Sending WOL packet to %1").arg(MAC));
670 QUdpSocket udp_socket;
671 qlonglong msglen = msg.size();
672 return udp_socket.writeDatagram(
673 msg.data(), msglen, QHostAddress::Broadcast, 32767) == msglen;
693 const char *command =
"ps -ax | grep -i pulseaudio | grep -v grep > /dev/null";
695 const char *command =
"ps ch -C pulseaudio -o pid > /dev/null";
709 if ((-1 == ret) && (0 != errno) && (val >= 0))
711 LOG(VB_GENERAL, LOG_ERR,
"Failed to nice process" +
ENO);
720#ifdef _POSIX_PRIORITY_SCHEDULING
745#if defined(Q_OS_LINUX) && ( defined(Q_PROCESSOR_X86) || defined(Q_PROCESSOR_POWER) || \
746 defined(Q_PROCESSOR_IA64) )
750#include <sys/ptrace.h>
751#include <sys/syscall.h>
752#if __has_include(<linux/ioprio.h>)
757#include <linux/ioprio.h>
760static constexpr int8_t IOPRIO_BITS { 16 };
761static constexpr int8_t IOPRIO_CLASS_SHIFT { 13 };
762static constexpr int IOPRIO_PRIO_MASK { (1UL << IOPRIO_CLASS_SHIFT) - 1 };
763static constexpr int IOPRIO_PRIO_CLASS(
int mask)
764 {
return mask >> IOPRIO_CLASS_SHIFT; };
765static constexpr int IOPRIO_PRIO_DATA(
int mask)
766 {
return mask & IOPRIO_PRIO_MASK; };
767static constexpr int IOPRIO_PRIO_VALUE(
int pclass,
int data)
768 {
return (pclass << IOPRIO_CLASS_SHIFT) | data; };
770enum { IOPRIO_CLASS_NONE,IOPRIO_CLASS_RT,IOPRIO_CLASS_BE,IOPRIO_CLASS_IDLE, };
771enum { IOPRIO_WHO_PROCESS = 1, IOPRIO_WHO_PGRP, IOPRIO_WHO_USER, };
776 int new_ioclass {IOPRIO_CLASS_BE};
778 new_ioclass = IOPRIO_CLASS_RT;
780 new_ioclass = IOPRIO_CLASS_IDLE;
781 int new_iodata = (new_ioclass == IOPRIO_CLASS_BE) ? val : 0;
782 int new_ioprio = IOPRIO_PRIO_VALUE(new_ioclass, new_iodata);
785 int old_ioprio = syscall(SYS_ioprio_get, IOPRIO_WHO_PROCESS, pid);
786 if (old_ioprio == new_ioprio)
789 int ret = syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, pid, new_ioprio);
791 if (-1 == ret && EPERM == errno && IOPRIO_CLASS_BE != new_ioclass)
793 new_iodata = (new_ioclass == IOPRIO_CLASS_RT) ? 0 : 7;
794 new_ioprio = IOPRIO_PRIO_VALUE(IOPRIO_CLASS_BE, new_iodata);
795 ret = syscall(SYS_ioprio_set, IOPRIO_WHO_PROCESS, pid, new_ioprio);
812 QFileInfoList entries = aDir.entryInfoList(QDir::NoDotAndDotDot |
813 QDir::Dirs | QDir::Files);
814 int count = entries.size();
815 bool has_err =
false;
817 for (
int idx = 0; idx < count && !has_err; idx++)
819 const QFileInfo& entryInfo = entries[idx];
820 QString path = entryInfo.absoluteFilePath();
821 if (entryInfo.isDir())
834 if (!has_err && !aDir.rmdir(aDir.absolutePath()))
853 QString
LOC =
"setHttpProxy() - ";
856 QString var(qEnvironmentVariable(
"http_proxy"));
858 var = qEnvironmentVariable(
"HTTP_PROXY");
861 if (!var.startsWith(
"http://"))
862 var.prepend(
"http://");
864 QUrl url = QUrl(var, QUrl::TolerantMode);
865 QString host = url.host();
866 int port = url.port();
879 LOG(VB_NETWORK, LOG_INFO,
LOC +
880 QString(
"assuming port %1 on host %2") .arg(port).arg(host));
883 else if (!
ping(host, 1s))
885 LOG(VB_GENERAL, LOG_ERR,
LOC +
886 QString(
"cannot locate host %1").arg(host) +
887 "\n\t\t\tPlease check HTTP_PROXY environment variable!");
889 else if (!
telnet(host,port))
891 LOG(VB_GENERAL, LOG_ERR,
LOC +
892 QString(
"%1:%2 - cannot connect!").arg(host).arg(port) +
893 "\n\t\t\tPlease check HTTP_PROXY environment variable!");
897 LOG(VB_NETWORK, LOG_DEBUG,
LOC + QString(
"using http://%1:%2@%3:%4")
898 .arg(url.userName()).arg(url.password())
899 .arg(host).arg(port));
902 QNetworkProxy(QNetworkProxy::HttpCachingProxy,
903 host, port, url.userName(), url.password());
904 QNetworkProxy::setApplicationProxy(
p);
908 LOG(VB_NETWORK, LOG_DEBUG,
LOC +
"no HTTP_PROXY environment var.");
913 QNetworkProxyQuery query(QUrl(
"http://www.mythtv.org"));
915 proxies = QNetworkProxyFactory::systemProxyForQuery(query);
917 for (
const auto&
p : std::as_const(
proxies))
919 QString host =
p.hostName();
922 if (
p.type() == QNetworkProxy::NoProxy)
927 LOG(VB_NETWORK, LOG_ERR,
LOC +
928 "failed to contact proxy host " + host);
932 LOG(VB_NETWORK, LOG_INFO,
LOC + QString(
"using proxy host %1:%2")
933 .arg(host).arg(port));
934 QNetworkProxy::setApplicationProxy(
p);
940 if (!
p.user().isEmpty())
942 url =
"http://%1:%2@%3:%4",
943 url = url.arg(
p.user(),
p.password());
947 url =
"http://%1:%2";
950 url = url.arg(
p.hostName()).arg(
p.port());
951 setenv(
"HTTP_PROXY", url.toLatin1(), 1);
952 setenv(
"http_proxy", url.toLatin1(), 0);
957 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.