2

I'm using QLocalServer to create a local socket so that other processes can connect to my Qt application. My problem is that Qt seems hellbent on creating the socket file in /tmp/, but I would prefer to follow XDG guidelines and use the XDG_RUNTIME_DIR environment variable to determine the correct user-specific socket location instead.

If I use:

QLocalServer socketServer;
socketServer.setSocketOptions(QLocalServer::UserAccessOption);
socketServer.listen("appname.socket"); //creates socket file, returns true.

then the socket is created in /tmp/, but If I attempt to specify an absolute or relative path instead then Qt doesn't seem to bother creating the socket at all.

QLocalServer socketServer;
socketServer.setSocketOptions(QLocalServer::UserAccessOption);
socketServer.listen("/tmp/appname.socket"); //returns false, does not create socket.

Edit: on closer inspection, the setSocketOptions(QLocalServer::UserAccessOption) -- which sets permissions for the socket file -- is what is preventing me from specifying socket paths. Commenting out this line removes the restriction. But surely this isn't how Qt is expected to behave?

  • Weird. The Qt docs specifically mention that this is possible: *Usually you would just pass in a name like "foo", but on Unix this could also be a path such as "/tmp/foo" and on Windows this could be a pipe path such as "\\.\pipe\foo"* – Georg Schölly Mar 05 '16 at 13:46
  • What is the return value of `socketServer.listen()`? And can you show an example of how you use an absolute or relative path? – Georg Schölly Mar 05 '16 at 20:26
  • I've edited my question with more information. It seems as though Qt only blocks you from specifying the socket path if you use setSocketOptions() to specify file permissions. – Username Obfuscation Mar 07 '16 at 09:00
  • I believe Qt uses [`QLocalServerPrivate::listen(QString)`](http://code.qt.io/cgit/qt/qtbase.git/tree/src/network/socket/qlocalserver_unix.cpp?h=5.5#n75) internally. It seems that the only place where the socket options are important start at line 156. Without trying this, I would guess that the problem is [on line 168](http://code.qt.io/cgit/qt/qtbase.git/tree/src/network/socket/qlocalserver_unix.cpp?h=5.5#n168). Can you try attaching a debugger or see if you can run this line manually? If I am correct, the problem is that you are not allowed to change permissions of the socket. – Georg Schölly Mar 07 '16 at 13:40
  • 1
    I appreciate the detective work, though I'm afraid I can't recompile Qt with debugging symbols right now, and setting permissions doesn't seem to be it. On the other hand, lines 93-100 (and later 120-131) of the source file you linked are **very** interesting: if any access options are set at all, that if() predicate on line 93 returns true and Qt produces the path string with completely different behaviour to what it uses if no access options were set. – Username Obfuscation Mar 10 '16 at 09:06
  • 1
    Interesting! I also just noticed that I completely misunderstood the flags, I somehow thought `socketOptions & QLocalServer::WorldAccessOption` was always true. It's a shame that the error strings are so unhelpful. Maybe `errno` can point you in the right direction. Or alternatively, have you looked into creating your own socket and [handing it to `QLocalServer`](http://doc.qt.io/qt-5/qlocalserver.html#listen-1)? – Georg Schölly Mar 10 '16 at 09:42

0 Answers0