summaryrefslogtreecommitdiff
path: root/src/ck-vt-monitor.c
blob: a0b5cf0b401750cbd916d0ac6707c73a9079b1f7 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2006-2007 William Jon McCann <mccann@jhu.edu>
 *
 * 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/vt.h>

#if defined (__linux__)
#include <linux/tty.h>
#include <linux/kd.h>
#endif /* linux */

#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gstdio.h>
#include <glib-object.h>

#define DBUS_API_SUBJECT_TO_CHANGE
#include <dbus/dbus-glib.h>
#include <dbus/dbus-glib-lowlevel.h>

#include "ck-vt-monitor.h"
#include "ck-marshal.h"
#include "ck-debug.h"

#define ERROR -1
#define DEBUG_ENABLED 1

#define CK_VT_MONITOR_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), CK_TYPE_VT_MONITOR, CkVtMonitorPrivate))

struct CkVtMonitorPrivate
{
        int              vfd;
        GHashTable      *vt_thread_hash;
        guint            active_num;

        GAsyncQueue     *event_queue;
        guint            process_queue_id;
};

enum {
        ACTIVE_CHANGED,
        LAST_SIGNAL
};

enum {
        PROP_0,
};

static guint signals [LAST_SIGNAL] = { 0, };

static void     ck_vt_monitor_class_init  (CkVtMonitorClass *klass);
static void     ck_vt_monitor_init        (CkVtMonitor      *vt_monitor);
static void     ck_vt_monitor_finalize    (GObject          *object);

static void     vt_add_watches            (CkVtMonitor      *vt_monitor);

G_DEFINE_TYPE (CkVtMonitor, ck_vt_monitor, G_TYPE_OBJECT)

G_LOCK_DEFINE_STATIC (hash_lock);
G_LOCK_DEFINE_STATIC (schedule_lock);

static gpointer vt_object = NULL;

GQuark
ck_vt_monitor_error_quark (void)
{
        static GQuark ret = 0;
        if (ret == 0) {
                ret = g_quark_from_static_string ("ck_vt_monitor_error");
        }

        return ret;
}

gboolean
ck_vt_monitor_set_active (CkVtMonitor    *vt_monitor,
                          guint32         num,
                          GError        **error)
{
        gboolean ret;
        int      res;

        g_return_val_if_fail (CK_IS_VT_MONITOR (vt_monitor), FALSE);

        if (num == vt_monitor->priv->active_num) {
                g_set_error (error,
                             CK_VT_MONITOR_ERROR,
                             CK_VT_MONITOR_ERROR_GENERAL,
                             _("Session is already active"));
                return FALSE;
        }

        res = ioctl (vt_monitor->priv->vfd, VT_ACTIVATE, num);
        if (res == 0) {
                ret = TRUE;
        } else {
                g_set_error (error,
                             CK_VT_MONITOR_ERROR,
                             CK_VT_MONITOR_ERROR_GENERAL,
                             _("Unable to activate session"));
                ret = FALSE;
        }

        return ret;
}

gboolean
ck_vt_monitor_get_active (CkVtMonitor    *vt_monitor,
                          guint32        *num,
                          GError        **error)
{
        g_return_val_if_fail (CK_IS_VT_MONITOR (vt_monitor), FALSE);

        if (num != NULL) {
                *num = vt_monitor->priv->active_num;
        }

        return TRUE;
}

static void
change_active_num (CkVtMonitor *vt_monitor,
                   guint        num)
{

        if (vt_monitor->priv->active_num != num) {
                ck_debug ("Changing active VT: %d", num);

                vt_monitor->priv->active_num = num;

                /* add a watch to every vt without a thread */
                vt_add_watches (vt_monitor);

                g_signal_emit (vt_monitor, signals[ACTIVE_CHANGED], 0, num);
        } else {
                ck_debug ("VT activated but already active: %d", num);
        }
}

typedef struct {
        gint32       num;
        CkVtMonitor *vt_monitor;
} ThreadData;

typedef struct {
        gint32       num;
} EventData;

static void
thread_data_free (ThreadData *data)
{
        if (data == NULL) {
                return;
        }

        g_free (data);
}

static void
event_data_free (EventData *data)
{
        if (data == NULL) {
                return;
        }

        g_free (data);
}

static gboolean
process_queue (CkVtMonitor *vt_monitor)
{
        int        i;
        int        queue_length;
        EventData *data;
        EventData *d;

        g_async_queue_lock (vt_monitor->priv->event_queue);

        ck_debug ("Processing VT event queue");

        queue_length = g_async_queue_length_unlocked (vt_monitor->priv->event_queue);
        data = NULL;

        G_LOCK (hash_lock);

        /* compress events in the queue */
        for (i = 0; i < queue_length; i++) {
                d = g_async_queue_try_pop_unlocked (vt_monitor->priv->event_queue);
                if (d == NULL) {
                        continue;

                }

                if (data != NULL) {
                        ck_debug ("Compressing queue; skipping event for VT %d", data->num);
                        event_data_free (data);
                }

                data = d;
        }

        G_UNLOCK (hash_lock);

        if (data != NULL) {
                change_active_num (vt_monitor, data->num);
                event_data_free (data);
        }

        G_LOCK (schedule_lock);
        vt_monitor->priv->process_queue_id = 0;
        G_UNLOCK (schedule_lock);

        g_async_queue_unlock (vt_monitor->priv->event_queue);

        return FALSE;
}

static void
schedule_process_queue (CkVtMonitor *vt_monitor)
{
        G_LOCK (schedule_lock);
        if (vt_monitor->priv->process_queue_id == 0) {
                vt_monitor->priv->process_queue_id = g_idle_add ((GSourceFunc)process_queue, vt_monitor);
        }
        G_UNLOCK (schedule_lock);
}

static void *
vt_thread_start (ThreadData *data)
{
        CkVtMonitor *vt_monitor;
        int          ret;
        gint32       num;

        vt_monitor = data->vt_monitor;
        num = data->num;

 again:
        ck_debug ("VT_WAITACTIVE for vt %d", num);
        ret = ioctl (vt_monitor->priv->vfd, VT_WAITACTIVE, num);

        ck_debug ("VT_WAITACTIVE for vt %d returned %d", num, ret);

        if (ret == ERROR) {

                if (errno == EINTR) {
                        ck_debug ("Interrupted waiting for native console %d activation: %s",
                                  num,
                                  g_strerror (errno));
                       goto again;
                } else {
                        ck_debug ("Error waiting for native console %d activation: %s",
                                   num,
                                   g_strerror (errno));

                        g_warning ("Error waiting for native console %d activation: %s",
                                   num,
                                   g_strerror (errno));
                }

                g_free (data);
        } else {
                EventData *event;

                /* add event to queue */
                event = g_new0 (EventData, 1);
                event->num = num;
                ck_debug ("Pushing activation event for VT %d onto queue", num);

                g_async_queue_push (vt_monitor->priv->event_queue, event);

                /* schedule processing of queue */
                schedule_process_queue (vt_monitor);
        }

        G_LOCK (hash_lock);
        if (vt_monitor->priv->vt_thread_hash != NULL) {
                g_hash_table_remove (vt_monitor->priv->vt_thread_hash, GUINT_TO_POINTER (num));
        }
        G_UNLOCK (hash_lock);

        g_thread_exit (NULL);
        thread_data_free (data);

        return NULL;
}

static void
vt_add_watch_unlocked (CkVtMonitor *vt_monitor,
                       gint32       num)
{
        GThread    *thread;
        GError     *error;
        ThreadData *data;
        gpointer    id;

        data = g_new0 (ThreadData, 1);
        data->num = num;
        data->vt_monitor = vt_monitor;

        ck_debug ("Creating thread for vt %d", num);

        id = GINT_TO_POINTER (num);

        error = NULL;
        thread = g_thread_create_full ((GThreadFunc)vt_thread_start, data, 65536, FALSE, TRUE, G_THREAD_PRIORITY_NORMAL, &error);
        if (thread == NULL) {
                ck_debug ("Unable to create thread: %s", error->message);
                g_error_free (error);
        } else {
                g_hash_table_insert (vt_monitor->priv->vt_thread_hash, id, thread);
        }
}

static void
vt_add_watches (CkVtMonitor *vt_monitor)
{
        int    i;
        gint32 current_num;

        G_LOCK (hash_lock);

        current_num = vt_monitor->priv->active_num;

        for (i = 1; i < MAX_NR_CONSOLES; i++) {
                gpointer id;

                /* don't wait on the active vc */
                if (i == current_num) {
                        continue;
                }

                id = GINT_TO_POINTER (i);

                /* add a watch to all other VTs that don't have threads */
                if (g_hash_table_lookup (vt_monitor->priv->vt_thread_hash, id) == NULL) {
                        vt_add_watch_unlocked (vt_monitor, i);
                }
        }

        G_UNLOCK (hash_lock);
}

static guint
get_active_native (CkVtMonitor *vt_monitor)
{
        int            ret;
        struct vt_stat stat;

        ret = ioctl (vt_monitor->priv->vfd, VT_GETSTATE, &stat);
        if (ret == ERROR) {
                perror ("ioctl VT_GETSTATE");
                return -1;
        }

        {
                int i;

                ck_debug ("Current VT: tty%d", stat.v_active);
                for (i = 1; i <= 16; i++) {
                        gboolean is_on;
                        is_on = stat.v_state & (1 << i);

                        ck_debug ("VT %d:%s", i, is_on ? "on" : "off");
                }
        }

        return stat.v_active;
}

static void
ck_vt_monitor_class_init (CkVtMonitorClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);

        object_class->finalize = ck_vt_monitor_finalize;

        signals [ACTIVE_CHANGED] = g_signal_new ("active-changed",
                                                 G_TYPE_FROM_CLASS (object_class),
                                                 G_SIGNAL_RUN_LAST,
                                                 G_STRUCT_OFFSET (CkVtMonitorClass, active_changed),
                                                 NULL,
                                                 NULL,
                                                 g_cclosure_marshal_VOID__UINT,
                                                 G_TYPE_NONE,
                                                 1, G_TYPE_UINT);

        g_type_class_add_private (klass, sizeof (CkVtMonitorPrivate));
}

extern int getfd (void);

static void
ck_vt_monitor_init (CkVtMonitor *vt_monitor)
{
        int fd;

        vt_monitor->priv = CK_VT_MONITOR_GET_PRIVATE (vt_monitor);

        fd = getfd ();
        if (fd == ERROR) {
                ck_debug ("Unable to open console: %s", g_strerror (errno));
                g_critical ("Unable to open console: %s", g_strerror (errno));
        }

        vt_monitor->priv->event_queue = g_async_queue_new ();
        vt_monitor->priv->vfd = fd;
        vt_monitor->priv->vt_thread_hash = g_hash_table_new (g_direct_hash, g_direct_equal);

        vt_monitor->priv->active_num = get_active_native (vt_monitor);

        vt_add_watches (vt_monitor);
}

static void
ck_vt_monitor_finalize (GObject *object)
{
        CkVtMonitor *vt_monitor;

        g_return_if_fail (object != NULL);
        g_return_if_fail (CK_IS_VT_MONITOR (object));

        vt_monitor = CK_VT_MONITOR (object);

        g_return_if_fail (vt_monitor->priv != NULL);

        if (vt_monitor->priv->process_queue_id > 0) {
                g_source_remove (vt_monitor->priv->process_queue_id);
        }

        if (vt_monitor->priv->event_queue != NULL) {
                g_async_queue_unref (vt_monitor->priv->event_queue);
        }

        if (vt_monitor->priv->vt_thread_hash != NULL) {
                g_hash_table_destroy (vt_monitor->priv->vt_thread_hash);
        }

        close (vt_monitor->priv->vfd);

        G_OBJECT_CLASS (ck_vt_monitor_parent_class)->finalize (object);
}

CkVtMonitor *
ck_vt_monitor_new (void)
{
        if (vt_object != NULL) {
                g_object_ref (vt_object);
        } else {
                vt_object = g_object_new (CK_TYPE_VT_MONITOR, NULL);

                g_object_add_weak_pointer (vt_object,
                                           (gpointer *) &vt_object);
        }

        return CK_VT_MONITOR (vt_object);
}