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
This commit is contained in:
Jonas Kvinge 2019-09-22 19:39:45 +02:00 committed by Itay Grudev
parent fec44d13d5
commit 2c959069b7

View File

@ -33,11 +33,8 @@
#include <cstddef> #include <cstddef>
#include <QtCore/QDir> #include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtCore/QByteArray> #include <QtCore/QByteArray>
#include <QtCore/QSemaphore>
#include <QtCore/QDataStream> #include <QtCore/QDataStream>
#include <QtCore/QStandardPaths>
#include <QtCore/QCryptographicHash> #include <QtCore/QCryptographicHash>
#include <QtNetwork/QLocalServer> #include <QtNetwork/QLocalServer>
#include <QtNetwork/QLocalSocket> #include <QtNetwork/QLocalSocket>
@ -45,6 +42,12 @@
#include "singleapplication.h" #include "singleapplication.h"
#include "singleapplication_p.h" #include "singleapplication_p.h"
#ifdef Q_OS_UNIX
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#endif
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#include <windows.h> #include <windows.h>
#include <lmcons.h> #include <lmcons.h>
@ -109,22 +112,22 @@ void SingleApplicationPrivate::genBlockServerName()
if( GetUserNameW( username, &usernameLength ) ) { if( GetUserNameW( username, &usernameLength ) ) {
appData.addData( QString::fromWCharArray(username).toUtf8() ); appData.addData( QString::fromWCharArray(username).toUtf8() );
} else { } else {
appData.addData( QStandardPaths::standardLocations( QStandardPaths::HomeLocation ).join("").toUtf8() ); appData.addData( qgetenv("USERNAME") );
} }
#endif #endif
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
QProcess process; QByteArray username;
process.start( "whoami" ); uid_t uid = geteuid();
if( process.waitForFinished( 100 ) && if( uid != -1 ) {
process.exitCode() == QProcess::NormalExit) { struct passwd *pw = getpwuid(uid);
appData.addData( process.readLine() ); if( pw ) {
} else { username = pw->pw_name;
appData.addData(
QDir(
QStandardPaths::standardLocations( QStandardPaths::HomeLocation ).first()
).absolutePath().toUtf8()
);
} }
}
if( username.isEmpty() ) {
username = qgetenv("USER");
}
appData.addData(username);
#endif #endif
} }