From 2c959069b7f9385107570b4d441781ef8466f717 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Sun, 22 Sep 2019 19:39:45 +0200 Subject: 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 --- singleapplication_p.cpp | 33 ++++++++++++++++++--------------- 1 file 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 #include -#include #include -#include #include -#include #include #include #include @@ -45,6 +42,12 @@ #include "singleapplication.h" #include "singleapplication_p.h" +#ifdef Q_OS_UNIX + #include + #include + #include +#endif + #ifdef Q_OS_WIN #include #include @@ -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 } -- cgit v1.2.1