aboutsummaryrefslogtreecommitdiff
path: root/singleapplication_p.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'singleapplication_p.cpp')
-rw-r--r--singleapplication_p.cpp60
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
*/