int main(int argc, char *argv[]) { QApplication app(argc, argv); QFile cmdlineFile("/home/pwp/dt8_cmdline"); cmdlineFile.open( QIODevice::ReadOnly | QIODevice::Text ); QTextStream cmdlineStream( &cmdlineFile ); QString line = cmdlineStream.readLine(); cmdlineFile.close(); //ro root=livedisk:/dev/sda3 livedisk_iso=/dt8.iso liveimg if ( line.contains("livedisk") ) { int start = line.indexOf(QString("/dev/"), 0); if (start == -1) { return 3; } int end = line.indexOf(QString(" "), start); if (end == -1) { return 5; } QString devpath; for (int i = start; i < end; ++i) { devpath += line[i]; } } return 0; }
int main() { for (int i = 0; i < 1000; ++ i) { std::string path("foo bar baz"); std::ifstream cmdlineStream(path.c_str(),std::ios_base::in); if (cmdlineStream.is_open()){ std::string name; std::getline(cmdlineStream, name,'\0'); } } return 0; }
pid_t Systemd::getPid(string processName) { pid_t pid = -1; // Open the /proc directory DIR* dir = opendir("/proc"); if (dir == NULL) return -1; // Enumerate all entries in directory until process found struct dirent* entry; while (pid < 0 && (entry = readdir(dir))) { // Skip non-numeric entries int id = atoi(entry->d_name); if (id <= 0) continue; // Read contents of virtual /proc/{pid}/cmdline file string cmdlinePath = string("/proc/") + entry->d_name + "/cmdline"; ifstream cmdlineStream(cmdlinePath.c_str()); string cmdline; getline(cmdlineStream, cmdline); // /proc/{pid}/cmdline is empty if (cmdline.empty()) continue; // Keep first cmdline item which contains the program path size_t pos = cmdline.find('\0'); if (pos != string::npos) cmdline = cmdline.substr(0, pos); // Keep program name only, removing the path pos = cmdline.rfind('/'); if (pos != string::npos) cmdline = cmdline.substr(pos + 1); // Compare against requested process name if (processName == cmdline) pid = id; } closedir(dir); return pid; }