diff options
author | Rainer Keller <rainer.keller@theqtcompany.com> | 2015-06-03 09:37:54 +0200 |
---|---|---|
committer | Rainer Keller <rainer.keller@theqtcompany.com> | 2015-06-08 11:05:06 +0300 |
commit | 866aa6f727c096feada0f1d39cce44ca660ad787 (patch) | |
tree | 5226f919c67e727db74443e1e7261d2ddb248707 | |
parent | f055f3c9fe5825f09af54ef4ccc2b24ae0181c22 (diff) |
Send command string between appcontroller instances
Task-number: QTEE-931
Change-Id: If0efdafdd5e39523d315be86cda07f69e2387e16
Reviewed-by: Ulf Hermann <ulf.hermann@theqtcompany.com>
-rw-r--r-- | main.cpp | 50 | ||||
-rw-r--r-- | process.cpp | 30 |
2 files changed, 59 insertions, 21 deletions
@@ -21,6 +21,7 @@ #include "perfprocesshandler.h" #include <QCoreApplication> #include <QDir> +#include <QLocalSocket> #include <QTcpServer> #include <QProcess> #include <errno.h> @@ -73,28 +74,39 @@ static void setupAddressStruct(struct sockaddr_un &address) address.sun_path[0] = 0; } -static int connectSocket() +static int connectSocket(const QByteArray &command) { - int create_socket; - struct sockaddr_un address; + int fd = 0; + struct sockaddr_un address; - if ((create_socket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { - perror("Could not create socket"); - return -1; - } + if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) { + perror("Could not create socket"); + return -1; + } - if (fcntl(create_socket, F_SETFD, FD_CLOEXEC) == -1) { - perror("Unable to set CLOEXEC"); - } + if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1) + perror("Unable to set CLOEXEC"); - setupAddressStruct(address); + setupAddressStruct(address); - if (connect(create_socket, (struct sockaddr *) &address, sizeof (address)) != 0) { - perror("Could not connect"); - return -1; - } - close(create_socket); - return 0; + if (connect(fd, (struct sockaddr *) &address, sizeof (address)) != 0) { + perror("Could not connect"); + return -1; + } + + QLocalSocket localSocket; + if (!localSocket.setSocketDescriptor(fd)) { + fprintf(stderr, "Unable to initialize local socket from descriptor.\n"); + close(fd); + return -1; + } + + if (localSocket.write(command) != command.size()) { + fprintf(stderr, "Could not send command"); + return -1; + } + localSocket.waitForBytesWritten(); + return 0; } static int createServerSocket() @@ -122,7 +134,7 @@ static int createServerSocket() return -1; } - if (connectSocket() != 0) { + if (connectSocket("stop") != 0) { fprintf(stderr, "Failed to connect to process\n"); } @@ -143,7 +155,7 @@ static int createServerSocket() static void stop() { - connectSocket(); + connectSocket("stop"); } static int openServer(QTcpServer *s, Utils::PortList &range) diff --git a/process.cpp b/process.cpp index 7842deb..ce699c0 100644 --- a/process.cpp +++ b/process.cpp @@ -21,6 +21,7 @@ #include <unistd.h> #include <QDebug> #include <QFile> +#include <QLocalSocket> #include <QSocketNotifier> #include <sys/socket.h> #include <signal.h> @@ -261,8 +262,33 @@ void Process::stop() void Process::incomingConnection(int i) { - accept(i, NULL, NULL); - stop(); + int fd = accept(i, NULL, NULL); + if (fd < 0 ) { + perror("Could not accept connection"); + stop(); + return; + } + + QLocalSocket localSocket; + if (!localSocket.setSocketDescriptor(fd)) { + fprintf(stderr, "Could not initialize local socket from descriptor.\n"); + close(fd); + stop(); + return; + } + + if (!localSocket.waitForReadyRead()) { + fprintf(stderr, "No command received.\n"); + stop(); // default + return; + } + + QByteArray command = localSocket.readAll(); + + if (command == "stop") + stop(); + else + stop(); } void Process::setSocketNotifier(QSocketNotifier *s) |