23#include <QApplication>
36 return (qint64)GetCurrentProcessId();
38 return (qint64)getpid();
48 if (procList.contains(pid)) {
50 QString exeFile = procList.value(pid);
51 QString thisExe = QFileInfo(QApplication::applicationFilePath()).fileName();
52 return (exeFile.toLower() == thisExe.toLower());
57 if (kill((pid_t)pid, 0) < 0) {
58 return (errno != ESRCH);
69 QDir pidFileDir = QFileInfo(pidFileName).absoluteDir();
70 if (!pidFileDir.exists()) {
71 pidFileDir.mkpath(QDir::convertSeparators(pidFileDir.absolutePath()));
75 QFile pidfile(pidFileName);
76 if (!pidfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
77 return err(errmsg, pidfile.errorString());
81 QTextStream pidstream(&pidfile);
94 QFile pidfile(pidFileName);
95 if (!pidfile.exists()) {
98 if (!pidfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
100 *errmsg = pidfile.errorString();
106 QTextStream pidstream(&pidfile);
111QHash<qint64, QString>
120#if defined(Q_OS_WIN32)
121 HANDLE hProcess = OpenProcess(PROCESS_TERMINATE, FALSE,
122 static_cast<DWORD
>(pid));
123 if (hProcess == NULL)
126 BOOL ret = TerminateProcess(hProcess, 0);
127 CloseHandle(hProcess);
129 return (ret != FALSE);
137QHash<qint64, QString>
140 QHash<qint64, QString> pl;
144#if defined(Q_OS_LINUX)
150 ps.start(
"netstat", args, QIODevice::ReadOnly);
151 while(!ps.waitForFinished());
153 QString flt = QString(
"127.0.0.1:%1").arg(port);
154 QStringList lines = QString(ps.readAllStandardOutput()).split(
"\n");
155 QStringList filtered = lines.filter(flt);
158 filtered = filtered.filter(
"LISTENING");
161 if(filtered.length() == 0)
162 return QHash<qint64, QString>();
166#if defined(Q_OS_LINUX)
167 foreach(QString line, lines) {
168 QStringList items = line.trimmed().split(
" ");
169 if(items.length() < 1)
171 items = items.last().trimmed().split(
"/");
172 if(items.length() < 2)
175 pid = items[0].toLong();
178 pl.insert(pid, proc);
181 pid = filtered[0].split(
" ").last().trimmed().toLong();
183 pl.insert(pid, proc);
QHash< qint64, QString > process_list(quint16 port)
qint64 read_pidfile(const QString &pidFileName, QString *errmsg)
bool write_pidfile(const QString &pidFileName, QString *errmsg)
QHash< qint64, QString > universal_process_list(quint16 port)
bool process_kill(qint64 pid)
bool is_process_running(qint64 pid)
bool err(QString *str, const QString &errmsg)
QHash< qint64, QString > win32_process_list()