Repository navigation
Expand file tree
/
Copy pathQNetCtlTool.cpp
More file actions
202 lines (176 loc) · 7.01 KB
/
Copy pathQNetCtlTool.cpp
File metadata and controls
202 lines (176 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include "QNetCtlTool.h"
#include <QDBusConnection>
#include <QDBusInterface>
#include <QFile>
#include <QProcess>
#include <QProcessEnvironment>
#include <QTimer>
#include <unistd.h>
#include "paths.h"
static void debug(QString s) {
QFile file("/tmp/qnetctl.dbg");
file.open(QIODevice::Append);
file.write(s.append("\n").toLocal8Bit());
}
QNetCtlTool::QNetCtlTool(int &argc, char **argv) : QCoreApplication(argc, argv)
{
if (argc < 4) {
qWarning("Must pass clients DBus address, name and service!");
QMetaObject::invokeMethod(this, "quit", Qt::QueuedConnection);
return;
}
seteuid(1000);
QDBusConnection bus = QDBusConnection::connectToBus(argv[1], argv[2]);
seteuid(0);
myClient = new QDBusInterface(argv[3], "/QNetCtl", "org.archlinux.qnetctl", bus, this);
bus.connect(argv[3], "/QNetCtl", "org.archlinux.qnetctl", "request", this, SLOT(request(QString, QString)));
}
void QNetCtlTool::chain()
{
QProcess *proc = static_cast<QProcess*>(sender());
const QString tag = proc->property("QNetCtlTag").toString();
const QString info = proc->property("QNetCtlInfo").toString();
if (proc->exitStatus() != QProcess::NormalExit || proc->exitCode()) {
myClient->call(QDBus::NoBlock, "reply", tag, QString("ERROR: %1, %2").arg(proc->exitStatus()).arg(proc->exitCode()));
return;
}
myClient->call(QDBus::NoBlock, "reply", tag, QString::fromLocal8Bit(proc->readAllStandardOutput()));
QString cmd;
if (tag == "remove_profile") {
QFile::remove(gs_profilePath + info);
} else if (tag == "scan_wifi") {
myScanningDevices.removeAll(info);
myUplinkingDevices.removeAll(info);
cmd = TOOL(ip) + " link set " + info + " down";
}
if (cmd.isNull()) // should not happen
return;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.remove("LC_ALL");
env.remove("LANG");
proc = new QProcess(this);
proc->setProcessEnvironment(env);
// proc->setProperty("QNetCtlTag", tag);
// connect (proc, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(reply()));
connect (proc, SIGNAL(finished(int, QProcess::ExitStatus)), proc, SLOT(deleteLater()));
proc->start(cmd, QIODevice::ReadOnly);
}
void QNetCtlTool::scanWifi(QString device)
{
if (device.isNull() && sender())
device = sender()->property("QNetCtlScanDevice").toString();
if (myScanningDevices.contains(device))
return;
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.remove("LC_ALL");
env.remove("LANG");
QProcess *proc = new QProcess(this);
proc->setProcessEnvironment(env);
bool isDown = false;
proc->start(TOOL(ip) + " link show " + device, QIODevice::ReadOnly);
proc->waitForFinished();
if (proc->exitStatus() == QProcess::NormalExit && !proc->exitCode())
isDown = !QString::fromLocal8Bit(proc->readAllStandardOutput()).section('>', 0, 0).contains("UP");
bool waitsForUp = myUplinkingDevices.contains(device);
if (isDown) {
if (!waitsForUp) {
waitsForUp = true;
myUplinkingDevices << device;
proc->start(TOOL(ip) + " link set " + device + " up", QIODevice::ReadOnly);
proc->waitForFinished();
}
// we're waiting for the device to come up
delete proc;
QTimer *t = new QTimer(this);
t->setProperty("QNetCtlScanDevice", device);
t->setSingleShot(true);
connect(t, SIGNAL(timeout()), this, SLOT(scanWifi()));
connect(t, SIGNAL(timeout()), t, SLOT(deleteLater()));
t->start(500);
return;
}
myScanningDevices << device;
proc->setProperty("QNetCtlTag", "scan_wifi");
proc->setProperty("QNetCtlInfo", device);
// if we set it up, we've to set it back down through the chain slot
connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)), waitsForUp ? SLOT(chain()) : SLOT(reply()));
connect(proc, SIGNAL(finished(int, QProcess::ExitStatus)), proc, SLOT(deleteLater()));
proc->start(TOOL(iw) + " dev " + device + " scan");
}
void QNetCtlTool::reply()
{
QProcess *proc = static_cast<QProcess*>(sender());
const QString tag = proc->property("QNetCtlTag").toString();
if (proc->exitStatus() != QProcess::NormalExit || proc->exitCode()) {
myClient->call(QDBus::NoBlock, "reply", tag, QString("ERROR: %1, %2").arg(proc->exitStatus()).arg(proc->exitCode()));
return;
}
myClient->call(QDBus::NoBlock, "reply", tag, QString::fromLocal8Bit(proc->readAllStandardOutput()));
}
void QNetCtlTool::request(const QString tag, const QString information)
{
QString cmd;
bool chain = false;
// debug(tag + information);
if (tag == "switch_to_profile") {
cmd = TOOL(netctl) + " switch-to " + information;
} else if (tag == "stop_profile") {
cmd = TOOL(netctl) + " stop " + information;
} else if (tag == "scan_wifi") {
scanWifi(information);
return;
} else if (tag == "enable_profile") {
cmd = TOOL(netctl) + " enable " + information;
} else if (tag == "enable_service") {
if (information.startsWith("netctl-"))
cmd = TOOL(systemctl) + " enable " + information;
}
else if (tag == "disable_profile") {
cmd = TOOL(netctl) + " disable " + information;
} else if (tag == "disable_service") {
if (information.startsWith("netctl-"))
cmd = TOOL(systemctl) + " disable " + information;
} else if (tag == "remove_profile") {
chain = true;
cmd = TOOL(netctl) + " disable " + information;
} else if (tag.startsWith("write_profile")) {
QString name = tag.section(' ', 1);
QFile file(gs_profilePath + name);
if (file.open(QIODevice::WriteOnly|QIODevice::Text)) {
file.write(information.toLocal8Bit());
file.close();
myClient->call(QDBus::NoBlock, "reply", tag, "SUCCESS");
} else {
myClient->call(QDBus::NoBlock, "reply", tag, "ERROR");
}
return; // no process to run
} else if (tag == "reparse_config") {
return;
} else if (tag == "quit") {
quit();
return;
}
if (cmd.isNull()) {
myClient->call(QDBus::NoBlock, "reply", tag, "ERROR: unsupported command / request:" + information);
return;
}
QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
env.remove("LC_ALL");
env.remove("LANG");
QProcess *proc = new QProcess(this);
proc->setProcessEnvironment(env);
proc->setProperty("QNetCtlTag", tag);
if (chain) {
proc->setProperty("QNetCtlInfo", information);
connect (proc, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(chain()));
} else {
connect (proc, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(reply()));
}
connect (proc, SIGNAL(finished(int, QProcess::ExitStatus)), proc, SLOT(deleteLater()));
// debug(cmd);
proc->start(cmd, QIODevice::ReadOnly);
}
int main(int argc, char **argv)
{
return QNetCtlTool(argc, argv).exec();
}