00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <QDir>
00029 #include <QFile>
00030 #include <QFileInfo>
00031 #include <QTextStream>
00032 #include <QApplication>
00033
00034 #include "string.h"
00035 #include "process.h"
00036
00037
00038
00039 qint64
00040 get_pid()
00041 {
00042 #if defined(Q_OS_WIN)
00043 return (qint64)GetCurrentProcessId();
00044 #else
00045 return (qint64)getpid();
00046 #endif
00047 }
00048
00049
00050 bool
00051 is_process_running(qint64 pid)
00052 {
00053 #if defined(Q_OS_WIN)
00054 QHash<qint64, QString> procList = win32_process_list();
00055 if (procList.contains(pid)) {
00056
00057 QString exeFile = procList.value(pid);
00058 QString thisExe = QFileInfo(QApplication::applicationFilePath()).fileName();
00059 return (exeFile.toLower() == thisExe.toLower());
00060 }
00061 return false;
00062 #else
00063
00064 if (kill((pid_t)pid, 0) < 0) {
00065 return (errno != ESRCH);
00066 }
00067 return true;
00068 #endif
00069 }
00070
00071
00072 bool
00073 write_pidfile(QString pidFileName, QString *errmsg)
00074 {
00075
00076 QDir pidFileDir = QFileInfo(pidFileName).absoluteDir();
00077 if (!pidFileDir.exists()) {
00078 pidFileDir.mkpath(QDir::convertSeparators(pidFileDir.absolutePath()));
00079 }
00080
00081
00082 QFile pidfile(pidFileName);
00083 if (!pidfile.open(QIODevice::WriteOnly | QIODevice::Text)) {
00084 return err(errmsg, pidfile.errorString());
00085 }
00086
00087
00088 QTextStream pidstream(&pidfile);
00089 pidstream << get_pid();
00090 return true;
00091 }
00092
00093
00094
00095 qint64
00096 read_pidfile(QString pidFileName, QString *errmsg)
00097 {
00098 qint64 pid;
00099
00100
00101 QFile pidfile(pidFileName);
00102 if (!pidfile.exists()) {
00103 return 0;
00104 }
00105 if (!pidfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
00106 if (errmsg) {
00107 *errmsg = pidfile.errorString();
00108 }
00109 return -1;
00110 }
00111
00112
00113 QTextStream pidstream(&pidfile);
00114 pidstream >> pid;
00115 return pid;
00116 }
00117