-
Notifications
You must be signed in to change notification settings - Fork 0
/
introspect_async.cpp
110 lines (94 loc) · 3.4 KB
/
introspect_async.cpp
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
// Copyright 2021-2024 Kevin Backhouse.
//
// This file is part of DBusParseDemo.
//
// DBusParseDemo is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DBusParseDemo is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with DBusParseDemo. If not, see <https://www.gnu.org/licenses/>.
#include <EPollLoopDBusHandler.hpp>
#include <dbus_utils.hpp>
#include <string.h>
#include <unistd.h>
class IntrospectHandler : public DBusHandler {
const std::string destination_;
const std::string objectpath_;
public:
IntrospectHandler(const char *socketpath, const char *destination,
const char *objectpath)
: DBusHandler(socketpath), destination_(destination),
objectpath_(objectpath) {}
void accept() override final {
send_hello([this](const std::string &busname) -> int {
fprintf(stdout, "Unique bus name: %s\n", busname.c_str());
fflush(stdout);
send_call(DBusMessageBody::mk0(), _s(objectpath_),
_s("org.freedesktop.DBus.Introspectable"), _s(destination_),
_s("Introspect"), [](const DBusMessage &message, bool) -> int {
print_dbus_message(STDOUT_FILENO, message);
return -1;
});
return 0;
});
}
// We don't expect to receive any calls.
void receive_call(const DBusMessage &) override final {
logerror("Unexpected incoming call.");
}
// We don't expect to receive any calls.
void receive_signal(const DBusMessage &) override final {
logerror("Received a signal.");
}
void receive_error(const DBusMessage &) override final {
logerror("Received an error.");
}
void disconnect() noexcept override final {}
void logerror(const char *errmsg) noexcept override final {
fprintf(stderr, "%s\n", errmsg);
}
};
void run(const uid_t uid, const char *socketpath, const char *destination,
const char *objectpath) {
EPollLoop loop;
DBusAuthHandler *handler = new DBusAuthHandler(
uid, new IntrospectHandler(socketpath, destination, objectpath));
if (loop.add_handler(handler) < 0) {
throw Error(_s("Failed to add IntrospectHandler"));
}
loop.run();
}
int main(int argc, char *argv[]) {
const char *progname = argc > 0 ? argv[0] : "a.out";
if (argc < 4) {
fprintf(
stderr,
"usage: %s <unix socket path> <destination> <object path>\n"
"example: %s /var/run/dbus/system_bus_socket "
"org.freedesktop.PolicyKit1 /org/freedesktop/PolicyKit1/Authority\n",
progname, progname);
return 1;
}
uid_t uid = getuid();
const char *socketpath = argv[1];
const char *destination = argv[2];
const char *objectpath = argv[3];
try {
run(uid, socketpath, destination, objectpath);
return EXIT_SUCCESS;
} catch (ErrorWithErrno &e) {
const int err = e.getErrno();
fprintf(stderr, "%s\n%s\n", e.what(), strerror(err));
return EXIT_FAILURE;
} catch (std::exception &e) {
fprintf(stderr, "%s\n", e.what());
return EXIT_FAILURE;
}
}