From e07a62ede32467089c1e4c57f7fed141ea009c49 Mon Sep 17 00:00:00 2001 From: "Martin T. H. Sandsmark" Date: Sat, 13 Aug 2016 23:23:11 +0200 Subject: [PATCH] Remove unused environment reading code REVIEW: 128674 CCBUG: 325442 --- src/ProcessInfo.cpp | 176 +++++++------------------------------------- src/ProcessInfo.h | 59 +++------------ 2 files changed, 37 insertions(+), 198 deletions(-) diff --git a/src/ProcessInfo.cpp b/src/ProcessInfo.cpp index c89d5521e..b3c02c76f 100644 --- a/src/ProcessInfo.cpp +++ b/src/ProcessInfo.cpp @@ -68,15 +68,13 @@ using namespace Konsole; -ProcessInfo::ProcessInfo(int aPid , bool enableEnvironmentRead) - : _fields(ARGUMENTS | ENVIRONMENT) // arguments and environments +ProcessInfo::ProcessInfo(int aPid) + : _fields(ARGUMENTS) // arguments // are currently always valid, // they just return an empty // vector / map respectively // if no arguments - // or environment bindings // have been explicitly set - , _enableEnvironmentRead(enableEnvironmentRead) , _pid(aPid) , _parentPid(0) , _foregroundPid(0) @@ -99,7 +97,7 @@ void ProcessInfo::setError(Error error) void ProcessInfo::update() { - readProcessInfo(_pid, _enableEnvironmentRead); + readProcessInfo(_pid); } QString ProcessInfo::validCurrentDir() const @@ -202,13 +200,6 @@ QVector ProcessInfo::arguments(bool* ok) const return _arguments; } -QMap ProcessInfo::environment(bool* ok) const -{ - *ok = _fields.testFlag(ENVIRONMENT); - - return _environment; -} - bool ProcessInfo::isValid() const { return _fields.testFlag(PROCESS_ID); @@ -340,11 +331,6 @@ void ProcessInfo::clearArguments() _arguments.clear(); } -void ProcessInfo::addEnvironmentBinding(const QString& name , const QString& value) -{ - _environment.insert(name, value); -} - void ProcessInfo::setFileError(QFile::FileError error) { switch (error) { @@ -364,12 +350,12 @@ void ProcessInfo::setFileError(QFile::FileError error) // implementations of the UnixProcessInfo abstract class. // -NullProcessInfo::NullProcessInfo(int aPid, const QString& /*titleFormat*/, bool enableEnvironmentRead) - : ProcessInfo(aPid, enableEnvironmentRead) +NullProcessInfo::NullProcessInfo(int aPid, const QString& /*titleFormat*/) + : ProcessInfo(aPid) { } -bool NullProcessInfo::readProcessInfo(int /*pid*/ , bool /*enableEnvironmentRead*/) +bool NullProcessInfo::readProcessInfo(int /*pid*/) { return false; } @@ -379,13 +365,13 @@ void NullProcessInfo::readUserName() } #if !defined(Q_OS_WIN) -UnixProcessInfo::UnixProcessInfo(int aPid, const QString& titleFormat, bool enableEnvironmentRead) - : ProcessInfo(aPid, enableEnvironmentRead) +UnixProcessInfo::UnixProcessInfo(int aPid, const QString& titleFormat) + : ProcessInfo(aPid) { setUserNameRequired(titleFormat.contains(QLatin1String("%u"))); } -bool UnixProcessInfo::readProcessInfo(int aPid , bool enableEnvironmentRead) +bool UnixProcessInfo::readProcessInfo(int aPid) { // prevent _arguments from growing longer and longer each time this // method is called. @@ -395,9 +381,6 @@ bool UnixProcessInfo::readProcessInfo(int aPid , bool enableEnvironmentRead) if (ok) { ok |= readArguments(aPid); ok |= readCurrentDir(aPid); - if (enableEnvironmentRead) { - ok |= readEnvironment(aPid); - } } return ok; } @@ -436,8 +419,8 @@ void UnixProcessInfo::readUserName() class LinuxProcessInfo : public UnixProcessInfo { public: - LinuxProcessInfo(int aPid, const QString& titleFormat, bool env) : - UnixProcessInfo(aPid, titleFormat, env) { + LinuxProcessInfo(int aPid, const QString& titleFormat) : + UnixProcessInfo(aPid, titleFormat) { } private: @@ -593,46 +576,14 @@ private: setCurrentDir(path); return true; } - - virtual bool readEnvironment(int aPid) { - // read environment bindings file found at /proc//environ - // the expected format is a list of KEY=VALUE strings delimited by null - // characters and ending in a double null character pair. - - QFile environmentFile(QStringLiteral("/proc/%1/environ").arg(aPid)); - if (environmentFile.open(QIODevice::ReadOnly)) { - QTextStream stream(&environmentFile); - const QString& data = stream.readAll(); - - const QStringList& bindingList = data.split(QChar('\0')); - - foreach(const QString & entry , bindingList) { - QString name; - QString value; - - const int splitPos = entry.indexOf('='); - - if (splitPos != -1) { - name = entry.mid(0, splitPos); - value = entry.mid(splitPos + 1, -1); - - addEnvironmentBinding(name, value); - } - } - } else { - setFileError(environmentFile.error()); - } - - return true; - } }; #elif defined(Q_OS_FREEBSD) class FreeBSDProcessInfo : public UnixProcessInfo { public: - FreeBSDProcessInfo(int aPid, const QString& titleFormat, bool readEnvironment) : - UnixProcessInfo(aPid, titleFormat, readEnvironment) { + FreeBSDProcessInfo(int aPid, const QString& titleFormat) : + UnixProcessInfo(aPid, titleFormat) { } private: @@ -703,50 +654,6 @@ private: return true; } - virtual bool readEnvironment(int aPid) { - - struct procstat *prstat = procstat_open_sysctl(); - if (prstat == nullptr) { - return false; - } - - kinfo_proc *procinfo; - unsigned int cnt; - procinfo = procstat_getprocs(prstat, KERN_PROC_PID, aPid, &cnt); - if (procinfo == nullptr || cnt != 1) - { - procstat_close(prstat); - return false; - } - - // pass 0, as the third argument, as we want to have every environment - // variable defined -- code courtesy of procstats procstats_arg.c - char **envs = procstat_getenvv(prstat, procinfo, 0); - if (envs == nullptr) - { - procstat_close(prstat); - return false; - } - int i; - QString name, value; - for (i = 0; envs[i] != nullptr; i++) - { - QString entry = QString::fromLocal8Bit(envs[i]); - const int splitPos = entry.indexOf('='); - - if (splitPos != -1) { - name = entry.mid(0, splitPos); - value = entry.mid(splitPos + 1, -1); - - addEnvironmentBinding(name, value); - } - } - - procstat_freeenvv(prstat); - procstat_close(prstat); - return true; - } - virtual bool readCurrentDir(int aPid) { #if defined(HAVE_OS_DRAGONFLYBSD) char buf[PATH_MAX]; @@ -793,8 +700,8 @@ private: class OpenBSDProcessInfo : public UnixProcessInfo { public: - OpenBSDProcessInfo(int aPid, const QString& titleFormat, bool readEnvironment) : - UnixProcessInfo(aPid, titleFormat, readEnvironment) { + OpenBSDProcessInfo(int aPid, const QString& titleFormat) : + UnixProcessInfo(aPid, titleFormat) { } private: @@ -881,28 +788,6 @@ private: return true; } - virtual bool readEnvironment(int aPid) { - char** envp; - char* eqsign; - - envp = readProcArgs(aPid, KERN_PROC_ENV); - if (envp == NULL) { - return false; - } - - for (char **p = envp; *p != NULL; p++) { - eqsign = strchr(*p, '='); - if (eqsign == NULL || eqsign[1] == '\0') { - continue; - } - *eqsign = '\0'; - addEnvironmentBinding(QString((const char *)p), - QString((const char *)eqsign + 1)); - } - free(envp); - return true; - } - virtual bool readCurrentDir(int aPid) { char buf[PATH_MAX]; int managementInfoBase[3]; @@ -927,8 +812,8 @@ private: class MacProcessInfo : public UnixProcessInfo { public: - MacProcessInfo(int aPid, const QString& titleFormat, bool env) : - UnixProcessInfo(aPid, titleFormat, env) { + MacProcessInfo(int aPid, const QString& titleFormat) : + UnixProcessInfo(aPid, titleFormat) { } private: @@ -999,10 +884,6 @@ private: } return false; } - virtual bool readEnvironment(int aPid) { - Q_UNUSED(aPid); - return false; - } }; #elif defined(Q_OS_SOLARIS) @@ -1020,8 +901,8 @@ private: class SolarisProcessInfo : public UnixProcessInfo { public: - SolarisProcessInfo(int aPid, const QString& titleFormat, bool readEnvironment) - : UnixProcessInfo(aPid, titleFormat, readEnvironment) { + SolarisProcessInfo(int aPid, const QString& titleFormat) + : UnixProcessInfo(aPid, titleFormat) { } private: virtual bool readProcInfo(int aPid) { @@ -1049,11 +930,6 @@ private: return false; } - virtual bool readEnvironment(int /*pid*/) { - // Not supported in Solaris - return false; - } - // FIXME: This will have the same issues as BKO 251351; the Linux // version uses readlink. virtual bool readCurrentDir(int aPid) { @@ -1246,20 +1122,20 @@ QString SSHProcessInfo::format(const QString& input) const return output; } -ProcessInfo* ProcessInfo::newInstance(int aPid, const QString& titleFormat, bool enableEnvironmentRead) +ProcessInfo* ProcessInfo::newInstance(int aPid, const QString& titleFormat) { #if defined(Q_OS_LINUX) - return new LinuxProcessInfo(aPid, titleFormat, enableEnvironmentRead); + return new LinuxProcessInfo(aPid, titleFormat); #elif defined(Q_OS_SOLARIS) - return new SolarisProcessInfo(aPid, titleFormat, enableEnvironmentRead); + return new SolarisProcessInfo(aPid, titleFormat); #elif defined(Q_OS_OSX) - return new MacProcessInfo(aPid, titleFormat, enableEnvironmentRead); + return new MacProcessInfo(aPid, titleFormat); #elif defined(Q_OS_FREEBSD) - return new FreeBSDProcessInfo(aPid, titleFormat, enableEnvironmentRead); + return new FreeBSDProcessInfo(aPid, titleFormat); #elif defined(Q_OS_OPENBSD) - return new OpenBSDProcessInfo(aPid, titleFormat, enableEnvironmentRead); + return new OpenBSDProcessInfo(aPid, titleFormat); #else - return new NullProcessInfo(aPid, titleFormat, enableEnvironmentRead); + return new NullProcessInfo(aPid, titleFormat); #endif } diff --git a/src/ProcessInfo.h b/src/ProcessInfo.h index 7c846b626..f2c348afd 100644 --- a/src/ProcessInfo.h +++ b/src/ProcessInfo.h @@ -32,8 +32,7 @@ namespace Konsole * Takes a snapshot of the state of a process and provides access to * information such as the process name, parent process, * the foreground process in the controlling terminal, - * the arguments with which the process was started and the - * environment. + * the arguments with which the process was started. * * To create a new snapshot, construct a new ProcessInfo instance, * using ProcessInfo::newInstance(), @@ -79,13 +78,8 @@ public: * the current platform which provides information about a given process. * * @param pid The pid of the process to examine - * @param readEnvironment Specifies whether environment bindings should - * be read. If this is false, then environment() calls will - * always fail. This is an optimization to avoid the overhead - * of reading the (potentially large) environment data when it - * is not required. */ - static ProcessInfo* newInstance(int pid, const QString& titleFormat, bool readEnvironment = false); + static ProcessInfo* newInstance(int pid, const QString& titleFormat); virtual ~ProcessInfo() {} @@ -145,15 +139,6 @@ public: * @param ok Set to true if the arguments were read successfully or false otherwise. */ QVector arguments(bool* ok) const; - /** - * Returns the environment bindings which the process - * was started with. - * In the returned map, the key is the name of the environment variable, - * and the value is the corresponding value. - * - * @param ok Set to true if the environment bindings were read successfully or false otherwise - */ - QMap environment(bool* ok) const; /** * Returns the current working directory of the process @@ -213,10 +198,9 @@ public: PARENT_PID = 2, FOREGROUND_PID = 4, ARGUMENTS = 8, - ENVIRONMENT = 16, - NAME = 32, - CURRENT_DIR = 64, - UID = 128 + NAME = 16, + CURRENT_DIR = 32, + UID = 64 }; Q_DECLARE_FLAGS(Fields, Field) @@ -227,7 +211,7 @@ protected: * static ProcessInfo::newInstance() method which will return * a suitable ProcessInfo instance for the current platform. */ - explicit ProcessInfo(int pid , bool readEnvironment = false); + explicit ProcessInfo(int pid ); /** * This is called on construction to read the process state @@ -244,10 +228,8 @@ protected: * has been set using setPid() * * @param pid The process id of the process to read - * @param readEnvironment Specifies whether the environment bindings - * for the process should be read */ - virtual bool readProcessInfo(int pid , bool readEnvironment) = 0; + virtual bool readProcessInfo(int pid) = 0; /* Read the user name */ virtual void readUserName(void) = 0; @@ -285,15 +267,6 @@ protected: */ void clearArguments(); - /** - * Adds an environment binding for the process, as returned by - * environment() - * - * @param name The name of the environment variable, eg. "PATH" - * @param value The value of the environment variable, eg. "/bin" - */ - void addEnvironmentBinding(const QString& name , const QString& value); - void setUserNameRequired(bool need); bool userNameRequired() const; @@ -305,8 +278,6 @@ private: Fields _fields; - bool _enableEnvironmentRead; // specifies whether to read the environment - // bindings when update() is called int _pid; int _parentPid; int _foregroundPid; @@ -322,7 +293,6 @@ private: bool _userNameRequired; QVector _arguments; - QMap _environment; static QSet commonDirNames(); static QSet _commonDirNames; @@ -343,9 +313,9 @@ public: * Constructs a new NullProcessInfo instance. * See ProcessInfo::newInstance() */ - explicit NullProcessInfo(int pid, const QString& titleFormat, bool readEnvironment = false); + explicit NullProcessInfo(int pid, const QString& titleFormat); protected: - virtual bool readProcessInfo(int pid, bool readEnvironment); + virtual bool readProcessInfo(int pid); virtual void readUserName(void); }; @@ -361,14 +331,14 @@ public: * Constructs a new instance of UnixProcessInfo. * See ProcessInfo::newInstance() */ - explicit UnixProcessInfo(int pid, const QString& titleFormat, bool readEnvironment = false); + explicit UnixProcessInfo(int pid, const QString& titleFormat); protected: /** * Implementation of ProcessInfo::readProcessInfo(); calls the * four private methods below in turn. */ - virtual bool readProcessInfo(int pid , bool readEnvironment); + virtual bool readProcessInfo(int pid ); virtual void readUserName(void); @@ -380,13 +350,6 @@ private: */ virtual bool readProcInfo(int pid) = 0; - /** - * Read the environment of the process. Sets _environment. - * @param pid process ID to use - * @return true on success - */ - virtual bool readEnvironment(int pid) = 0; - /** * Determine what arguments were passed to the process. Sets _arguments. * @param pid process ID to use