1

In order to properly launch a little Qt app on an embedded system, I need a reliable way to find out whether a Qt QWS server is already running.

Otherwise I'd need to supply the -qwsoption to the app to run the server itself. Is there a way of doing it?

László Papp
  • 51,870
  • 39
  • 111
  • 135
Ber
  • 40,356
  • 16
  • 72
  • 88
  • What is wrong about reading the process table and the passed options? – László Papp Jun 03 '14 at 07:13
  • @FinalContest What's wrong about posting a proper answer? You might even get upvoted :) – Ber Jun 03 '14 at 11:35
  • Sorry, I do not personally think this is the best way, but I also do not think there is a simpler way, unfortunately. Fwiw, I do not understand why this question got downvoted. Perhaps someone thought it is too short or so. I compensated that now. :) – László Papp Jun 03 '14 at 11:38
  • @FinalContest Thanks. No offense take, none intended. Happy hacking on. – Ber Jun 03 '14 at 11:49

2 Answers2

3

Here is the solution I settled for:

The QWS server uses to socket to talk to its clients. I test for the existence of this socket.

I also test whether the server actually has the socked open in order to avoid falling for orphaned sockets left over after a QWS server crash. This is done using lsof (list open files) on the socket. If the server is running, the list will not be empty and lsof will return true. If the server is not running, lsof will return false.

On my system the socket was located in /tmp/qtembedded-0/QtEmbedded-0

So here is the bash code:

QWSSOCK=/tmp/qtembedded-0/QtEmbedded-0

if [ ! -S $QWSSOCK ] ; then
    echo "No socket $QWSSOCK"
    QWSOPT=-qws
elif lsof $QWSSOCK ; then
    echo "Server running on $QWSSOCK"
    QWSOPT=
else
    echo "No server on $QWSSOCK"
    QWSOPT=-qws
fi

After this I can run my Qt app using the $QWSOPT variable:

app $QWSOPT
Ber
  • 40,356
  • 16
  • 72
  • 88
  • The directory is still there, even when the server stops. So, this method is not very reliable – BЈовић Jul 08 '14 at 12:23
  • @BЈовић Yes, the directory, and even the socket may remain if the server crahsed. That's why the code uses the `lsof`command to see if the server actually has the socked open. This make the method very reliable. – Ber Jul 09 '14 at 09:24
  • doh sorry. Somehow I missed `lsof` – BЈовић Jul 09 '14 at 09:38
1

The only quick workaround coming to mind is this:

ps aux | grep "\-qws"

and check if that returns anything. Other than that, I think you should obsolete qws in your project as it is a relatively old and broken concept with today's standards.

László Papp
  • 51,870
  • 39
  • 111
  • 135
  • Unfortuately, this is no reliable, as Qt programs may run a QWS servers without having the `-qws` option on their command line. Also, the string might be part of some other command line element. – Ber Jun 03 '14 at 12:13
  • @Ber: to the former, yes, if the QApplication is constructed with that type. I am not sure you can detect that without some additional code, i.e. config, IPC, etc. As for the latter, that is true theoretically, but I am yet to find a case where that happens in the practice. ;-) – László Papp Jun 03 '14 at 12:14