QList<ProcessInfo> runningProcesses() { QList<ProcessInfo> processes; QDir procDir(QLatin1String("/proc")); const QFileInfoList procCont = procDir.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot | QDir::Readable); QRegExp validator(QLatin1String("[0-9]+")); Q_FOREACH (const QFileInfo &info, procCont) { if (validator.exactMatch(info.fileName())) { const QString linkPath = QDir(info.absoluteFilePath()).absoluteFilePath(QLatin1String("exe")); const QFileInfo linkInfo(linkPath); if (linkInfo.exists()) { ProcessInfo processInfo; processInfo.name = linkInfo.symLinkTarget(); processInfo.id = info.fileName().toInt(); processes.append(processInfo); } } } return processes; }
bool Brewtarget::initialize() { // Need these for changed(QMetaProperty,QVariant) to be emitted across threads. qRegisterMetaType<QMetaProperty>(); qRegisterMetaType<Equipment*>(); qRegisterMetaType<Mash*>(); qRegisterMetaType<Style*>(); qRegisterMetaType<Brewtarget::DBTable>(); qRegisterMetaType< QList<BrewNote*> >(); qRegisterMetaType< QList<Hop*> >(); qRegisterMetaType< QList<Instruction*> >(); qRegisterMetaType< QList<Fermentable*> >(); qRegisterMetaType< QList<Misc*> >(); qRegisterMetaType< QList<Yeast*> >(); qRegisterMetaType< QList<Water*> >(); // In Unix, make sure the user isn't running 2 copies. #if defined(Q_OS_LINUX) pidFile.setFileName(QString("%1.pid").arg(getUserDataDir())); if( pidFile.exists() ) { // Read the pid. qint64 pid; pidFile.open(QIODevice::ReadOnly); { QTextStream pidStream(&pidFile); pidStream >> pid; } pidFile.close(); // If the pid is in the proc filesystem, another instance is running. // Have to check /proc, because perhaps the last instance crashed without // cleaning up after itself. QDir procDir(QString("/proc/%1").arg(pid)); if( procDir.exists() ) { std::cerr << "Brewtarget is already running. PID: " << pid << std::endl; return false; } }