21
21
#include " perfprocesshandler.h"
22
22
#include < QCoreApplication>
23
23
#include < QDir>
24
+ #include < QLocalSocket>
24
25
#include < QTcpServer>
25
26
#include < QProcess>
26
27
#include < errno.h>
@@ -73,28 +74,39 @@ static void setupAddressStruct(struct sockaddr_un &address)
73
74
address.sun_path [0 ] = 0 ;
74
75
}
75
76
76
- static int connectSocket ()
77
+ static int connectSocket (const QByteArray &command )
77
78
{
78
- int create_socket ;
79
- struct sockaddr_un address;
79
+ int fd = 0 ;
80
+ struct sockaddr_un address;
80
81
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
+ }
85
86
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" );
89
89
90
- setupAddressStruct (address);
90
+ setupAddressStruct (address);
91
91
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 ;
98
110
}
99
111
100
112
static int createServerSocket ()
@@ -122,7 +134,7 @@ static int createServerSocket()
122
134
return -1 ;
123
135
}
124
136
125
- if (connectSocket () != 0 ) {
137
+ if (connectSocket (" stop " ) != 0 ) {
126
138
fprintf (stderr, " Failed to connect to process\n " );
127
139
}
128
140
@@ -143,7 +155,7 @@ static int createServerSocket()
143
155
144
156
static void stop ()
145
157
{
146
- connectSocket ();
158
+ connectSocket (" stop " );
147
159
}
148
160
149
161
static int openServer (QTcpServer *s, Utils::PortList &range)
0 commit comments