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 <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
}