diff options
author | Itay Grudev <itay+git2020@grudev.com> | 2020-03-03 01:22:54 +0000 |
---|---|---|
committer | Itay Grudev <itay+89bf5c@grudev.com> | 2020-03-03 01:24:55 +0000 |
commit | 81cc2719beab0317b52764e8cf56afd8f56b0c89 (patch) | |
tree | 596bf4f020748b45b7bf1f262281bb477077ac79 /singleapplication_p.cpp | |
parent | Merge pull request #94 from itay-grudev/qwertysam (diff) | |
download | singleapplication-81cc2719beab0317b52764e8cf56afd8f56b0c89.tar.xz |
v3.1.0a Added primaryUser()
Closes #95
Diffstat (limited to 'singleapplication_p.cpp')
-rw-r--r-- | singleapplication_p.cpp | 60 |
1 files changed, 38 insertions, 22 deletions
diff --git a/singleapplication_p.cpp b/singleapplication_p.cpp index 884fe63..8eb49ab 100644 --- a/singleapplication_p.cpp +++ b/singleapplication_p.cpp @@ -76,6 +76,7 @@ SingleApplicationPrivate::~SingleApplicationPrivate() delete server; inst->primary = false; inst->primaryPid = -1; + inst->primaryUser[0] = '\0'; inst->checksum = blockChecksum(); } memory->unlock(); @@ -83,6 +84,27 @@ SingleApplicationPrivate::~SingleApplicationPrivate() delete memory; } +QByteArray SingleApplicationPrivate::getUsername(){ +#ifdef Q_OS_WIN + wchar_t username[UNLEN + 1]; + // Specifies size of the buffer on input + DWORD usernameLength = UNLEN + 1; + if( GetUserNameW( username, &usernameLength ) ) + return QString::fromWCharArray( username ).toUtf8(); + return qgetenv( "USERNAME" ); +#endif +#ifdef Q_OS_UNIX + QByteArray username; + uid_t uid = geteuid(); + struct passwd *pw = getpwuid( uid ); + if( pw ) + username = pw->pw_name; + if( username.isEmpty() ) + username = qgetenv( "USER" ); + return username; +#endif +} + void SingleApplicationPrivate::genBlockServerName() { QCryptographicHash appData( QCryptographicHash::Sha256 ); @@ -105,28 +127,7 @@ void SingleApplicationPrivate::genBlockServerName() // User level block requires a user specific data in the hash if( options & SingleApplication::Mode::User ) { -#ifdef Q_OS_WIN - wchar_t username [ UNLEN + 1 ]; - // Specifies size of the buffer on input - DWORD usernameLength = UNLEN + 1; - if( GetUserNameW( username, &usernameLength ) ) { - appData.addData( QString::fromWCharArray(username).toUtf8() ); - } else { - appData.addData( qgetenv("USERNAME") ); - } -#endif -#ifdef Q_OS_UNIX - QByteArray username; - uid_t uid = geteuid(); - struct passwd *pw = getpwuid(uid); - if( pw ) { - username = pw->pw_name; - } - if( username.isEmpty() ) { - username = qgetenv("USER"); - } - appData.addData(username); -#endif + appData.addData( getUsername() ); } // Replace the backslash in RFC 2045 Base64 [a-zA-Z0-9+/=] to comply with @@ -140,6 +141,7 @@ void SingleApplicationPrivate::initializeMemoryBlock() inst->primary = false; inst->secondary = 0; inst->primaryPid = -1; + inst->primaryUser[0] = '\0'; inst->checksum = blockChecksum(); } @@ -173,6 +175,8 @@ void SingleApplicationPrivate::startPrimary() inst->primary = true; inst->primaryPid = q->applicationPid(); + strncpy( inst->primaryUser, getUsername().data(), 127 ); + inst->primaryUser[127] = '\0'; inst->checksum = blockChecksum(); instanceNumber = 0; @@ -257,6 +261,18 @@ qint64 SingleApplicationPrivate::primaryPid() return pid; } +QString SingleApplicationPrivate::primaryUser() +{ + QByteArray username; + + memory->lock(); + InstancesInfo* inst = static_cast<InstancesInfo*>( memory->data() ); + username = inst->primaryUser; + memory->unlock(); + + return QString::fromUtf8( username ); +} + /** * @brief Executed when a connection has been made to the LocalServer */ |