Repository navigation
Expand file tree
/
Copy pathCk.cpp
More file actions
211 lines (175 loc) · 5.13 KB
/
Copy pathCk.cpp
File metadata and controls
211 lines (175 loc) · 5.13 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
203
204
205
206
207
208
209
210
211
/* SLiM - Simple Login Manager
Copyright (C) 2011 David Hauweele
This program 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 2 of the License, or
(at your option) any later version.
*/
#include <cstdio>
#include <iostream>
#include <ck-connector.h>
#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <stdarg.h>
#include "Ck.h"
namespace Ck {
Exception::Exception(const std::string &func,
const std::string &errstr):
func(func),
errstr(errstr)
{}
dbus_bool_t Session::ck_connector_open_graphic_session(const std::string &display,
uid_t uid)
{
dbus_bool_t local = true;
const char *session_type = "x11";
const char *x11_display = display.c_str();
const char *x11_device = get_x11_device(display);
const char *remote_host = "";
const char *display_dev = "";
return ck_connector_open_session_with_parameters(ckc, &error,
"unix-user", &uid,
"session-type", &session_type,
"x11-display", &x11_display,
"x11-display-device", &x11_device,
"display-device", &display_dev,
"remote-host-name", &remote_host,
"is-local", &local,
NULL);
}
const char * Session::get_x11_device(const std::string &display)
{
static char device[64];
Display *xdisplay = XOpenDisplay(display.c_str());
if(!xdisplay)
throw Exception(__func__, "cannot open display");
/* Close this private connection on every return and throw path */
struct DisplayCloser {
Display *dpy;
~DisplayCloser() { XCloseDisplay(dpy); }
} closer = { xdisplay };
Window root;
Atom xfree86_vt_atom;
Atom return_type_atom;
int return_format;
unsigned long return_count;
unsigned long bytes_left;
unsigned char *return_value = NULL;
long vt;
const char *problem = NULL;
xfree86_vt_atom = XInternAtom(xdisplay, "XFree86_VT", true);
if(xfree86_vt_atom == None)
throw Exception(__func__, "cannot get XFree86_VT");
root = DefaultRootWindow(xdisplay);
if(XGetWindowProperty(xdisplay, root, xfree86_vt_atom,
0L, 1L, false, XA_INTEGER,
&return_type_atom, &return_format,
&return_count, &bytes_left,
&return_value) != Success)
throw Exception(__func__, "cannot get root window property");
if(return_type_atom != XA_INTEGER)
problem = "bad atom type";
else if(return_format != 32)
problem = "invalid return format";
else if(return_count != 1)
problem = "invalid count";
else if(bytes_left != 0)
problem = "invalid bytes left";
else if(!return_value)
problem = "null return value from XGetWindowProperty";
if(problem) {
if(return_value)
XFree(return_value);
throw Exception(__func__, problem);
}
vt = *((long *)return_value);
XFree(return_value);
snprintf(device, sizeof(device), "/dev/ttyv%ld", vt - 1);
return device;
}
/* Drop the connector, which also closes its private system bus
* connection. The session is considered open only while ckc is set. */
void Session::release()
{
if(ckc) {
ck_connector_unref(ckc);
ckc = nullptr;
}
}
void Session::open_session(const std::string &display, uid_t uid)
{
release();
ckc = ck_connector_new();
if(!ckc)
throw Exception(__func__, "error setting up connection to ConsoleKit");
dbus_bool_t opened;
try {
opened = ck_connector_open_graphic_session(display, uid);
}
catch(Exception &) {
release();
throw;
}
if(!opened) {
std::string errstr = dbus_error_is_set(&error) ? error.message :
"cannot open ConsoleKit session: OOM, DBus system bus "
" not available or insufficient privileges";
dbus_error_free(&error);
release();
throw Exception(__func__, errstr);
}
}
bool Session::is_open() const
{
return ckc != nullptr;
}
const char * Session::get_xdg_session_cookie()
{
return ckc ? ck_connector_get_cookie(ckc) : NULL;
}
/* Return a copy of the XDG runtime directory ConsoleKit manages for the
* session, or an empty string if it does not provide one. */
std::string Session::get_runtime_dir()
{
if(!ckc)
return "";
DBusError local_error;
dbus_error_init(&local_error);
const char *runtime_dir = ck_connector_get_runtime_dir(ckc, &local_error);
std::string result = runtime_dir ? runtime_dir : "";
std::string errstr = dbus_error_is_set(&local_error) ? local_error.message : "";
dbus_error_free(&local_error);
if(!runtime_dir && !errstr.empty())
throw Exception(__func__, errstr);
return result;
}
void Session::close_session()
{
if(!ckc)
return;
if(!ck_connector_close_session(ckc, &error)) {
std::string errstr = dbus_error_is_set(&error) ? error.message :
"cannot close ConsoleKit session: OOM, DBus system bus "
" not available or insufficient privileges";
dbus_error_free(&error);
release();
throw Exception(__func__, errstr);
}
release();
}
Session::Session()
: ckc(nullptr)
{
dbus_error_init(&error);
}
Session::~Session()
{
release();
dbus_error_free(&error);
}
}
std::ostream& operator<<( std::ostream& os, const Ck::Exception& e)
{
os << e.func << ": " << e.errstr;
return os;
}