|
QtPass 2.0.0
Developer documentation
|
QApplication subclass enforcing single-instance behavior via IPC. Detects whether another instance with the same unique key is running and enables forwarding messages (e.g., command-line arguments) to it. More...
#include <singleapplication.h>
Public Slots | |
| void | receiveMessage () |
| Slot for QLocalServer::newConnection: accept every pending connection and emit messageAvailable() with what it sent. | |
Signals | |
| void | messageAvailable (const QString &message) |
| messageAvailable notification from commandline | |
Public Member Functions | |
| SingleApplication (int &argc, char *argv[], QString uniqueKey) | |
| Construct a SingleApplication with a unique instance key. | |
| auto | isRunning () -> bool |
| Query whether another instance is already running. | |
| auto | sendMessage (const QString &message) -> bool |
| Send a message to the running instance. | |
Static Public Member Functions | |
| static auto | privateKey (const QString &base) -> QString |
| The key to construct with: a socket path in a folder only this user can write to (see privateKeyIn()). | |
| static auto | privateKeyIn (const QString &base, const QString &dir) -> QString |
| privateKey() with the folder given. | |
| static auto | lockPathFor (const QString &key) -> QString |
The lock file that marks the instance listening on key. | |
| static auto | serverRunsAsThisUser (const QLocalSocket &socket) -> bool |
Whether the server socket is connected to runs as this user. | |
Private Member Functions | |
| auto | peerIsListening () -> bool |
| Probe whether an instance is actually accepting connections. | |
| void | claimOrFollow () |
| Take the lock and listen, or become the lock holder's secondary. | |
| void | becomePrimary () |
| Listen on the key; this instance holds the lock. | |
| void | waitForPeer () |
| Become the secondary of the live instance that holds the lock. | |
| auto | forwardMessage (const QString &message) -> bool |
| Deliver a message over the local socket. | |
Private Attributes | |
| bool | _isRunning {false} |
| true when another instance holds the lock and answers on the socket; this instance should forward and exit. | |
| QString | _uniqueKey |
| Name of the local socket; also names the lock file. | |
| QLockFile | _lock |
| Held by the listening instance for as long as it runs. | |
| QScopedPointer< QLocalServer > | localServer |
| Server accepting forwarded command lines; only set in the primary instance, null in a secondary or when listening failed. | |
Static Private Attributes | |
| static const int | timeout = 1000 |
| Milliseconds to wait for a socket connect or write. | |
| static const int | maxMessageBytes = 64 * 1024 |
| Upper bound for one forwarded message (a command line). | |
| static const int | takeoverProbes = 5 |
| Probes for the lock's holder to start listening before giving up on IPC. | |
| static const int | takeoverProbeIntervalMs = 100 |
| Pause between two takeover probes, in milliseconds. | |
QApplication subclass enforcing single-instance behavior via IPC. Detects whether another instance with the same unique key is running and enables forwarding messages (e.g., command-line arguments) to it.
Definition at line 24 of file singleapplication.h.
| SingleApplication::SingleApplication | ( | int & | argc, |
| char * | argv[], | ||
| QString | uniqueKey ) |
Construct a SingleApplication with a unique instance key.
Takes the lock file for the key (lockPathFor()). Whoever holds it is the instance that listens; a lock left by a crashed instance is stale and taken over. When another live instance holds it this one probes the socket and, if it answers, becomes a secondary: isRunning() turns true.
| argc | Reference to program argc forwarded to QApplication. |
| argv | Program argv forwarded to QApplication. |
| uniqueKey | Name of the local socket: a full path (see privateKey()) or a bare name, which Qt puts in the temporary folder. |
Definition at line 26 of file singleapplication.cpp.
|
private |
Listen on the key; this instance holds the lock.
A socket file left by a crashed instance is removed first. When listen() fails the lock is released and this instance runs without IPC.
Definition at line 218 of file singleapplication.cpp.
|
private |
Take the lock and listen, or become the lock holder's secondary.
A free lock, or one left by a crashed instance, is taken and this instance listens (becomePrimary()). A lock held by a live instance makes this one wait for that instance's socket (waitForPeer()). A lock that cannot be taken at all leaves this instance without IPC.
Definition at line 199 of file singleapplication.cpp.
|
private |
Deliver a message over the local socket.
| message | Text to send; an empty string is sent as a single NUL so the peer still sees a connection with data. |
Definition at line 249 of file singleapplication.cpp.
| auto SingleApplication::isRunning | ( | ) | -> bool |
Query whether another instance is already running.
Definition at line 181 of file singleapplication.cpp.
|
static |
The lock file that marks the instance listening on key.
Next to the socket for a full path; for a bare name in the temporary folder, where Qt puts that socket too, with any slash replaced.
| key | The constructor's key. |
Definition at line 140 of file singleapplication.cpp.
|
signal |
messageAvailable notification from commandline
| message | args sent to qtpass executable |
|
private |
Probe whether an instance is actually accepting connections.
Definition at line 275 of file singleapplication.cpp.
|
static |
The key to construct with: a socket path in a folder only this user can write to (see privateKeyIn()).
The socket used to be named after the login name in the shared temporary folder, next to a shared-memory segment and its semaphore. Another local user could create those first and keep QtPass from starting (scan ANT-2026-1JUBRKLZ). The folder is QStandardPaths::RuntimeLocation, which Qt refuses when another user owns it. On Windows the key stays base: a pipe name cannot hold a path. Call it before the application object exists.
| base | Per-user name, such as the login name plus "QtPass". |
Definition at line 37 of file singleapplication.cpp.
|
static |
privateKey() with the folder given.
Without a folder the key is base plus a random suffix: the launch then runs as its own instance, without IPC, rather than on a name in the shared temporary folder that someone else can take.
| base | Per-user name, such as the login name plus "QtPass". |
| dir | Folder only this user can write to; empty when there is none. |
dir named after a hash of base, or base plus a random suffix. Definition at line 52 of file singleapplication.cpp.
|
slot |
Slot for QLocalServer::newConnection: accept every pending connection and emit messageAvailable() with what it sent.
Each accepted socket is read asynchronously so a peer that connects without sending anything cannot stall the GUI thread; the message is delivered once the peer disconnects and the socket is then deleted.
Definition at line 152 of file singleapplication.cpp.
| auto SingleApplication::sendMessage | ( | const QString & | message | ) | -> bool |
Send a message to the running instance.
When delivery fails the peer is taken to have exited since the constructor probed it: this instance takes its lock and becomes the listening instance, so isRunning() turns false and the window the caller opens next is reachable by later launches. If the peer still holds the lock it is probed again, as the constructor does.
| message | Text to deliver (typically forwarded command-line args). |
Definition at line 183 of file singleapplication.cpp.
|
static |
Whether the server socket is connected to runs as this user.
On Windows a named pipe's name is machine-wide: another user's session could open the pipe of the key first, and a launch would then forward its command line there and exit without a window. So the server's process is looked up (GetNamedPipeServerProcessId()) and its token's user compared with this process's; anything that cannot be checked counts as someone else's. Elsewhere the socket sits in a folder only this user can open (privateKey()), and this is always true.
| socket | A socket connected to the key. |
Definition at line 110 of file singleapplication.cpp.
|
private |
Become the secondary of the live instance that holds the lock.
It may still be starting, so it is probed a few times (see takeoverProbes); if it never answers, isRunning() stays false and the caller runs without IPC rather than not at all.
Definition at line 238 of file singleapplication.cpp.
|
private |
true when another instance holds the lock and answers on the socket; this instance should forward and exit.
Definition at line 170 of file singleapplication.h.
|
private |
Held by the listening instance for as long as it runs.
Definition at line 174 of file singleapplication.h.
|
private |
Name of the local socket; also names the lock file.
Definition at line 172 of file singleapplication.h.
|
private |
Server accepting forwarded command lines; only set in the primary instance, null in a secondary or when listening failed.
Definition at line 179 of file singleapplication.h.
|
staticprivate |
Upper bound for one forwarded message (a command line).
The socket is user-only, but a peer that keeps a connection open and streams data would otherwise grow the per-connection buffer without limit; anything past this is dropped and the peer disconnected.
Definition at line 190 of file singleapplication.h.
|
staticprivate |
Pause between two takeover probes, in milliseconds.
Definition at line 194 of file singleapplication.h.
|
staticprivate |
Probes for the lock's holder to start listening before giving up on IPC.
Definition at line 192 of file singleapplication.h.
|
staticprivate |
Milliseconds to wait for a socket connect or write.
Definition at line 182 of file singleapplication.h.