/** * If no windows manager is set, * probe the environment to automatically guess one. **/ static void checkDesktop() { // if no desktop is given, look if one is passed via command line const char* const pcDesktopSession(::getenv(DESKTOP_SESSION)); if (!pcDesktopSession) { // no desktop is set and no desktop is given on the command line. // most likely we are called via sudo // try to test for at least very well known running desktop managers const uint uiUid(effectiveUid()); QFileInfoList procList(QDir(PROCDIR).entryInfoList(QDir::AllDirs|QDir::NoDotAndDotDot)); bool fDone(false); for (QFileInfoList::const_iterator procIt(procList.constBegin()); !fDone && procIt != procList.constEnd(); procIt++) { bool fOk(false); QString strPid((*procIt).fileName()); strPid.toUInt(&fOk); if (fOk) // pid must be numeric, ignore every thing else { // is this process owned by the user if (uiUid == (*procIt).ownerId()) { // we have a valid pid // open the cmdline file to determine what's the name of the process running QFile cmdLineFile(PROCDIR + strPid + "/comm"); if (cmdLineFile.open(QFile::ReadOnly)) { const QString strCli(cmdLineFile.readAll()); if (strCli.startsWith("gnome-session")) { fDone = true; ::setenv(DESKTOP_SESSION, "gnome", 0); } else if (strCli.startsWith("ksmserver")) { fDone = true; ::setenv(DESKTOP_SESSION, "kde", 0); } else if (strCli.startsWith("xfce4-session")) { fDone = true; ::setenv(DESKTOP_SESSION, "xfce", 0); } } else qWarning() << "Failed to open proc command line file" << cmdLineFile.fileName(); } } } } }
bool CheckProcessFile(const std::string& name, const std::string& processName) { // Try to cast the filename to an integer number (=PID) try { unsigned long pid = utils::toUint( name ); // Was the PID read correctly? if (pid == 0) { return false; } const std::string cmdLineFileName = systemProcFolder + name + "/cmdline"; std::ifstream cmdLineFile(cmdLineFileName.c_str()); if (cmdLineFile.is_open()) { // Read the command line from the process file std::string cmdLine; getline(cmdLineFile, cmdLine); if (cmdLine.find(processName) != std::string::npos) { // Process found, return success return true; } } // Close the file cmdLineFile.close(); } catch( ... ) { // Cast to int failed, no PID } return false; }