diff options
author | Jonas Kvinge <jonas@jkvinge.net> | 2019-09-22 19:39:45 +0200 |
---|---|---|
committer | Itay Grudev <itay+89bf5c@grudev.com> | 2019-09-22 20:39:45 +0300 |
commit | 2c959069b7f9385107570b4d441781ef8466f717 (patch) | |
tree | 028a067a545da361c58dd47ec073a7a312205c44 | |
parent | v3.0.15 (diff) | |
download | singleapplication-2c959069b7f9385107570b4d441781ef8466f717.tar.xz |
Use geteuid and getpwuid to get username on Unix, fallback to environment variable (#72)
* Use geteuid and getpwuid to get username on Unix, fallback to environment variable
* Remove QProcess include
-rw-r--r-- | singleapplication_p.cpp | 33 |
1 files changed, 18 insertions, 15 deletions
diff --git a/singleapplication_p.cpp b/singleapplication_p.cpp index de4945e..a129aa0 100644 --- a/singleapplication_p.cpp +++ b/singleapplication_p.cpp @@ -33,11 +33,8 @@ #include <cstddef> #include <QtCore/QDir> -#include <QtCore/QProcess> #include <QtCore/QByteArray> -#include <QtCore/QSemaphore> #include <QtCore/QDataStream> -#include <QtCore/QStandardPaths> #include <QtCore/QCryptographicHash> #include <QtNetwork/QLocalServer> #include <QtNetwork/QLocalSocket> @@ -45,6 +42,12 @@ #include "singleapplication.h" #include "singleapplication_p.h" +#ifdef Q_OS_UNIX + #include <unistd.h> + #include <sys/types.h> + #include <pwd.h> +#endif + #ifdef Q_OS_WIN #include <windows.h> #include <lmcons.h> @@ -109,22 +112,22 @@ void SingleApplicationPrivate::genBlockServerName() if( GetUserNameW( username, &usernameLength ) ) { appData.addData( QString::fromWCharArray(username).toUtf8() ); } else { - appData.addData( QStandardPaths::standardLocations( QStandardPaths::HomeLocation ).join("").toUtf8() ); + appData.addData( qgetenv("USERNAME") ); } #endif #ifdef Q_OS_UNIX - QProcess process; - process.start( "whoami" ); - if( process.waitForFinished( 100 ) && - process.exitCode() == QProcess::NormalExit) { - appData.addData( process.readLine() ); - } else { - appData.addData( - QDir( - QStandardPaths::standardLocations( QStandardPaths::HomeLocation ).first() - ).absolutePath().toUtf8() - ); + QByteArray username; + uid_t uid = geteuid(); + if( uid != -1 ) { + struct passwd *pw = getpwuid(uid); + if( pw ) { + username = pw->pw_name; + } + } + if( username.isEmpty() ) { + username = qgetenv("USER"); } + appData.addData(username); #endif } |