-
Notifications
You must be signed in to change notification settings - Fork 0
/
sd-bus.c
159 lines (141 loc) · 5.36 KB
/
sd-bus.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <systemd/sd-bus.h>
#include "sd-bus.h"
int terminate_current_session(void) {
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *m = NULL;
sd_bus *bus = NULL;
int r;
/* Connect to the system bus */
r = sd_bus_open_system(&bus);
if (r < 0) {
fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
goto finish;
}
/* Issue the method call and store the respons message in m */
r = sd_bus_call_method(bus,
"org.freedesktop.login1", /* service to contact */
"/org/freedesktop/login1/session/self", /* object path */
"org.freedesktop.login1.Session", /* interface name */
"Terminate", /* method name */
&error, /* object to return error in */
&m, /* return message on success */
"" /* input signature */
);
if (r < 0) {
fprintf(stderr, "Failed to issue method call: %s\n", error.message);
}
finish:
sd_bus_error_free(&error);
sd_bus_message_unref(m);
sd_bus_unref(bus);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
int get_session_id(char *session_id)
{
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *m = NULL;
sd_bus *bus = NULL;
int r;
const char *id;
/* Connect to the system bus */
r = sd_bus_open_system(&bus);
if (r < 0) {
fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
goto finish;
}
/* Issue the method call and store the respons message in m */
r = sd_bus_call_method(bus,
"org.freedesktop.login1", /* service to contact */
"/org/freedesktop/login1/session/self", /* object path */
"org.freedesktop.DBus.Properties", /* interface name */
"Get", /* method name */
&error, /* object to return error in */
&m, /* return message on success */
"ss", /* input signature */
"org.freedesktop.login1.Session", /* first argument */
"Id"); /* second argument */
if (r < 0) {
fprintf(stderr, "Failed to issue method call: %s\n", error.message);
goto finish;
}
// We need to open the variant container
char type;
r = sd_bus_message_peek_type(m, &type, &id);
if (r < 0) {
fprintf(stderr, "Failed to peek the type container: %s\n", strerror(-r));
goto finish;
}
r = sd_bus_message_enter_container(m, SD_BUS_TYPE_VARIANT, id);
if (r < 0) {
fprintf(stderr, "Failed to enter in the container: %s\n", strerror(-r));
goto finish;
}
r = sd_bus_message_read_basic(m, SD_BUS_TYPE_STRING, &id);
if (r < 0) {
fprintf(stderr, "Failed to parse response message: %s\n", strerror(-r));
goto finish;
}
if (id == NULL) {
fprintf(stderr, "Failed to get current session id: %s\n", strerror(-r));
goto finish;
}
size_t len_id = strlen(id);
if (len_id == 0) {
fprintf(stderr, "Failed to get current session id: %s\n", strerror(-r));
goto finish;
}
strcpy(session_id, id);
finish:
sd_bus_error_free(&error);
sd_bus_message_unref(m);
sd_bus_unref(bus);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
static int set_lock_status_dbus(char *session_id, const char *method_name) {
sd_bus_error error = SD_BUS_ERROR_NULL;
sd_bus_message *m = NULL;
sd_bus *bus = NULL;
int r;
/* Connect to the system bus */
r = sd_bus_open_system(&bus);
if (r < 0) {
fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
goto finish;
}
/* Issue the method call and store the respons message in m */
r = sd_bus_call_method(bus,
"org.cri.MachineState", /* service to contact */
"/org/cri/MachineState", /* object path */
"org.cri.MachineState", /* interface name */
method_name, /* method name */
&error, /* object to return error in */
&m, /* return message on success */
"s", /* input signature */
session_id /* first argument */
);
if (r < 0) {
fprintf(stderr, "Failed to issue method call: %s\n", error.message);
}
finish:
sd_bus_error_free(&error);
sd_bus_message_unref(m);
sd_bus_unref(bus);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
int set_lock_status(char *session_id, enum status new_status) {
const char *status[STATUS_SIZE] =
{
"UnlockSession",
"LockSession",
"OvertimeLockSession"
};
if (new_status < UNLOCKED || new_status > LOCKED_OVERTIME)
{
printf("Wrong status.\n");
return 0;
}
return set_lock_status_dbus(session_id, status[new_status]);
}