Skip to content

Commit 866aa6f

Browse files
author
Rainer Keller
committed
Send command string between appcontroller instances
Task-number: QTEE-931 Change-Id: If0efdafdd5e39523d315be86cda07f69e2387e16 Reviewed-by: Ulf Hermann <[email protected]>
1 parent f055f3c commit 866aa6f

File tree

2 files changed

+59
-21
lines changed

2 files changed

+59
-21
lines changed

main.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "perfprocesshandler.h"
2222
#include <QCoreApplication>
2323
#include <QDir>
24+
#include <QLocalSocket>
2425
#include <QTcpServer>
2526
#include <QProcess>
2627
#include <errno.h>
@@ -73,28 +74,39 @@ static void setupAddressStruct(struct sockaddr_un &address)
7374
address.sun_path[0] = 0;
7475
}
7576

76-
static int connectSocket()
77+
static int connectSocket(const QByteArray &command)
7778
{
78-
int create_socket;
79-
struct sockaddr_un address;
79+
int fd = 0;
80+
struct sockaddr_un address;
8081

81-
if ((create_socket = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
82-
perror("Could not create socket");
83-
return -1;
84-
}
82+
if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) {
83+
perror("Could not create socket");
84+
return -1;
85+
}
8586

86-
if (fcntl(create_socket, F_SETFD, FD_CLOEXEC) == -1) {
87-
perror("Unable to set CLOEXEC");
88-
}
87+
if (fcntl(fd, F_SETFD, FD_CLOEXEC) == -1)
88+
perror("Unable to set CLOEXEC");
8989

90-
setupAddressStruct(address);
90+
setupAddressStruct(address);
9191

92-
if (connect(create_socket, (struct sockaddr *) &address, sizeof (address)) != 0) {
93-
perror("Could not connect");
94-
return -1;
95-
}
96-
close(create_socket);
97-
return 0;
92+
if (connect(fd, (struct sockaddr *) &address, sizeof (address)) != 0) {
93+
perror("Could not connect");
94+
return -1;
95+
}
96+
97+
QLocalSocket localSocket;
98+
if (!localSocket.setSocketDescriptor(fd)) {
99+
fprintf(stderr, "Unable to initialize local socket from descriptor.\n");
100+
close(fd);
101+
return -1;
102+
}
103+
104+
if (localSocket.write(command) != command.size()) {
105+
fprintf(stderr, "Could not send command");
106+
return -1;
107+
}
108+
localSocket.waitForBytesWritten();
109+
return 0;
98110
}
99111

100112
static int createServerSocket()
@@ -122,7 +134,7 @@ static int createServerSocket()
122134
return -1;
123135
}
124136

125-
if (connectSocket() != 0) {
137+
if (connectSocket("stop") != 0) {
126138
fprintf(stderr, "Failed to connect to process\n");
127139
}
128140

@@ -143,7 +155,7 @@ static int createServerSocket()
143155

144156
static void stop()
145157
{
146-
connectSocket();
158+
connectSocket("stop");
147159
}
148160

149161
static int openServer(QTcpServer *s, Utils::PortList &range)

process.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include <unistd.h>
2222
#include <QDebug>
2323
#include <QFile>
24+
#include <QLocalSocket>
2425
#include <QSocketNotifier>
2526
#include <sys/socket.h>
2627
#include <signal.h>
@@ -261,8 +262,33 @@ void Process::stop()
261262

262263
void Process::incomingConnection(int i)
263264
{
264-
accept(i, NULL, NULL);
265-
stop();
265+
int fd = accept(i, NULL, NULL);
266+
if (fd < 0 ) {
267+
perror("Could not accept connection");
268+
stop();
269+
return;
270+
}
271+
272+
QLocalSocket localSocket;
273+
if (!localSocket.setSocketDescriptor(fd)) {
274+
fprintf(stderr, "Could not initialize local socket from descriptor.\n");
275+
close(fd);
276+
stop();
277+
return;
278+
}
279+
280+
if (!localSocket.waitForReadyRead()) {
281+
fprintf(stderr, "No command received.\n");
282+
stop(); // default
283+
return;
284+
}
285+
286+
QByteArray command = localSocket.readAll();
287+
288+
if (command == "stop")
289+
stop();
290+
else
291+
stop();
266292
}
267293

268294
void Process::setSocketNotifier(QSocketNotifier *s)

0 commit comments

Comments
 (0)