-
Notifications
You must be signed in to change notification settings - Fork 1
/
sessionmgt_logind.c
155 lines (132 loc) · 4.05 KB
/
sessionmgt_logind.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
#include "main.h"
#include <errno.h>
#include <limits.h>
#include <string.h>
#include <systemd/sd-login.h>
#include <unistd.h>
char *
session_get(pid_t pid)
{
char *result;
if (sd_pid_get_session(pid, &result) < 0)
return NULL;
return result;
}
char *
seat_get(pid_t pid)
{
char *session;
char *result;
session = session_get(pid);
if (!session)
return NULL;
if (sd_session_get_seat(session, &result) < 0) {
result = NULL;
}
free(session);
return result;
}
char *
current_session_get(void)
{
pid_t pid = getpid();
return session_get(pid);
}
void
session_enumerate(const char *seat, char ***handles, unsigned int *len)
{
int entries = sd_seat_get_sessions(seat, handles, NULL, NULL);
if (entries < 0) {
ERR("Failed to get sessions.");
*handles = NULL;
*len = 0;
return;
}
*len = entries;
}
void
session_enumerate_free(char **handles, unsigned int len)
{
for (unsigned int i = 0; i < len; ++i) {
free(handles[i]);
}
free(handles);
}
void
session_activate(char *handle)
{
char buf[PATH_MAX];
if (!handle)
return;
snprintf(buf, sizeof(buf), "loginctl activate %s", handle);
system(buf);
}
#define GET_FIELD(func, field, errval) \
ret = func(handle, field); \
switch (ret) { \
case -ENXIO: \
INF("Error, The specified session does not exist."); \
*field = errval; \
return false; \
break; \
case -ENODATA: \
INF("Error, The given field is not specified for the described " \
"session."); \
*field = errval; \
break; \
case -EINVAL: \
INF("Error, An input parameter was invalid (out of range, or NULL, " \
"where that is not accepted)."); \
*field = errval; \
break; \
case -ENOMEM: \
INF("Error, Memory allocation failed."); \
*field = errval; \
break; \
}
bool
session_details(char *handle, uid_t *uid, char **name, char **icon, int *vtnr)
{
int ret = -1;
unsigned int vtnr_raw;
char max_name[1024];
char *tmp_desktop;
char *tmp_type;
GET_FIELD(sd_session_get_vt, &vtnr_raw, 0)
if (ret < 0) {
if (vtnr) {
*vtnr = -1;
}
} else if (vtnr) {
if (vtnr) {
*vtnr = vtnr_raw;
}
}
if (uid) {
GET_FIELD(sd_session_get_uid, uid, 0)
}
GET_FIELD(sd_session_get_desktop, &tmp_desktop, NULL)
GET_FIELD(sd_session_get_type, &tmp_type, NULL)
if (tmp_type && tmp_desktop)
snprintf(max_name, sizeof(max_name), "%s - %s", tmp_type, tmp_desktop);
else if (tmp_type)
snprintf(max_name, sizeof(max_name), "%s", tmp_type);
else if (tmp_desktop)
snprintf(max_name, sizeof(max_name), "%s", tmp_desktop);
if (tmp_desktop)
free(tmp_desktop);
if (tmp_type)
free(tmp_type);
if (name)
*name = strdup(max_name);
if (icon)
*icon = NULL;
return true;
}
void
wait_session_active(char *handle)
{
while (!sd_session_is_active(handle)) {
sleep(1);
}
}