blob: f69a8258095cf6de5129f0ba9190f060ae5e5628 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/drivers/char/vt_ioctl.c
3 *
4 * Copyright (C) 1992 obz under the linux copyright
5 *
6 * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
7 * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
8 * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
9 * Some code moved for less code duplication - Andi Kleen - Mar 1997
10 * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/types.h>
14#include <linux/errno.h>
15#include <linux/sched.h>
16#include <linux/tty.h>
17#include <linux/timer.h>
18#include <linux/kernel.h>
19#include <linux/kd.h>
20#include <linux/vt.h>
21#include <linux/string.h>
22#include <linux/slab.h>
23#include <linux/major.h>
24#include <linux/fs.h>
25#include <linux/console.h>
Jesper Juhl7ed20e12005-05-01 08:59:14 -070026#include <linux/signal.h>
Emmanuel Colbusbcc8ca02005-06-28 20:44:49 -070027#include <linux/timex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <asm/io.h>
30#include <asm/uaccess.h>
31
32#include <linux/kbd_kern.h>
33#include <linux/vt_kern.h>
34#include <linux/kbd_diacr.h>
35#include <linux/selection.h>
36
Andrew Johnsonb257bc02007-03-16 13:38:24 -080037char vt_dont_switch;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038extern struct tty_driver *console_driver;
39
40#define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
41#define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
42
43/*
44 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
45 * experimentation and study of X386 SYSV handling.
46 *
47 * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
48 * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
49 * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
50 * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
51 * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
52 * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
53 * to the current console is done by the main ioctl code.
54 */
55
56#ifdef CONFIG_X86
57#include <linux/syscalls.h>
58#endif
59
60static void complete_change_console(struct vc_data *vc);
61
62/*
63 * these are the valid i/o ports we're allowed to change. they map all the
64 * video ports
65 */
66#define GPFIRST 0x3b4
67#define GPLAST 0x3df
68#define GPNUM (GPLAST - GPFIRST + 1)
69
70#define i (tmp.kb_index)
71#define s (tmp.kb_table)
72#define v (tmp.kb_value)
73static inline int
74do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
75{
76 struct kbentry tmp;
77 ushort *key_map, val, ov;
78
79 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
80 return -EFAULT;
81
Marcelo Tosattie3f17f02005-11-07 00:59:34 -080082 if (!capable(CAP_SYS_TTY_CONFIG))
83 perm = 0;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 switch (cmd) {
86 case KDGKBENT:
87 key_map = key_maps[s];
88 if (key_map) {
89 val = U(key_map[i]);
90 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
91 val = K_HOLE;
92 } else
93 val = (i ? K_HOLE : K_NOSUCHMAP);
94 return put_user(val, &user_kbe->kb_value);
95 case KDSKBENT:
96 if (!perm)
97 return -EPERM;
98 if (!i && v == K_NOSUCHMAP) {
Alan Coxca9bda02006-09-29 02:00:03 -070099 /* deallocate map */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 key_map = key_maps[s];
101 if (s && key_map) {
102 key_maps[s] = NULL;
103 if (key_map[0] == U(K_ALLOCATED)) {
104 kfree(key_map);
105 keymap_count--;
106 }
107 }
108 break;
109 }
110
111 if (KTYP(v) < NR_TYPES) {
112 if (KVAL(v) > max_vals[KTYP(v)])
113 return -EINVAL;
114 } else
115 if (kbd->kbdmode != VC_UNICODE)
116 return -EINVAL;
117
118 /* ++Geert: non-PC keyboards may generate keycode zero */
119#if !defined(__mc68000__) && !defined(__powerpc__)
120 /* assignment to entry 0 only tests validity of args */
121 if (!i)
122 break;
123#endif
124
125 if (!(key_map = key_maps[s])) {
126 int j;
127
128 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
129 !capable(CAP_SYS_RESOURCE))
130 return -EPERM;
131
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800132 key_map = kmalloc(sizeof(plain_map),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 GFP_KERNEL);
134 if (!key_map)
135 return -ENOMEM;
136 key_maps[s] = key_map;
137 key_map[0] = U(K_ALLOCATED);
138 for (j = 1; j < NR_KEYS; j++)
139 key_map[j] = U(K_HOLE);
140 keymap_count++;
141 }
142 ov = U(key_map[i]);
143 if (v == ov)
144 break; /* nothing to do */
145 /*
146 * Attention Key.
147 */
148 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
149 return -EPERM;
150 key_map[i] = U(v);
151 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
152 compute_shiftstate();
153 break;
154 }
155 return 0;
156}
157#undef i
158#undef s
159#undef v
160
161static inline int
162do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
163{
164 struct kbkeycode tmp;
165 int kc = 0;
166
167 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
168 return -EFAULT;
169 switch (cmd) {
170 case KDGETKEYCODE:
171 kc = getkeycode(tmp.scancode);
172 if (kc >= 0)
173 kc = put_user(kc, &user_kbkc->keycode);
174 break;
175 case KDSETKEYCODE:
176 if (!perm)
177 return -EPERM;
178 kc = setkeycode(tmp.scancode, tmp.keycode);
179 break;
180 }
181 return kc;
182}
183
184static inline int
185do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
186{
187 struct kbsentry *kbs;
188 char *p;
189 u_char *q;
190 u_char __user *up;
191 int sz;
192 int delta;
193 char *first_free, *fj, *fnw;
194 int i, j, k;
195 int ret;
196
Andrew Morton0b360ad2005-10-30 15:03:02 -0800197 if (!capable(CAP_SYS_TTY_CONFIG))
Marcelo Tosattie3f17f02005-11-07 00:59:34 -0800198 perm = 0;
Andrew Morton0b360ad2005-10-30 15:03:02 -0800199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
201 if (!kbs) {
202 ret = -ENOMEM;
203 goto reterr;
204 }
205
206 /* we mostly copy too much here (512bytes), but who cares ;) */
207 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
208 ret = -EFAULT;
209 goto reterr;
210 }
211 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
212 i = kbs->kb_func;
213
214 switch (cmd) {
215 case KDGKBSENT:
216 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
217 a struct member */
218 up = user_kdgkb->kb_string;
219 p = func_table[i];
220 if(p)
221 for ( ; *p && sz; p++, sz--)
222 if (put_user(*p, up++)) {
223 ret = -EFAULT;
224 goto reterr;
225 }
226 if (put_user('\0', up)) {
227 ret = -EFAULT;
228 goto reterr;
229 }
230 kfree(kbs);
231 return ((p && *p) ? -EOVERFLOW : 0);
232 case KDSKBSENT:
233 if (!perm) {
234 ret = -EPERM;
235 goto reterr;
236 }
237
238 q = func_table[i];
239 first_free = funcbufptr + (funcbufsize - funcbufleft);
240 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
241 ;
242 if (j < MAX_NR_FUNC)
243 fj = func_table[j];
244 else
245 fj = first_free;
246
247 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
248 if (delta <= funcbufleft) { /* it fits in current buf */
249 if (j < MAX_NR_FUNC) {
250 memmove(fj + delta, fj, first_free - fj);
251 for (k = j; k < MAX_NR_FUNC; k++)
252 if (func_table[k])
253 func_table[k] += delta;
254 }
255 if (!q)
256 func_table[i] = fj;
257 funcbufleft -= delta;
258 } else { /* allocate a larger buffer */
259 sz = 256;
260 while (sz < funcbufsize - funcbufleft + delta)
261 sz <<= 1;
Robert P. J. Day5cbded52006-12-13 00:35:56 -0800262 fnw = kmalloc(sz, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if(!fnw) {
264 ret = -ENOMEM;
265 goto reterr;
266 }
267
268 if (!q)
269 func_table[i] = fj;
270 if (fj > funcbufptr)
271 memmove(fnw, funcbufptr, fj - funcbufptr);
272 for (k = 0; k < j; k++)
273 if (func_table[k])
274 func_table[k] = fnw + (func_table[k] - funcbufptr);
275
276 if (first_free > fj) {
277 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
278 for (k = j; k < MAX_NR_FUNC; k++)
279 if (func_table[k])
280 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
281 }
282 if (funcbufptr != func_buf)
283 kfree(funcbufptr);
284 funcbufptr = fnw;
285 funcbufleft = funcbufleft - delta + sz - funcbufsize;
286 funcbufsize = sz;
287 }
288 strcpy(func_table[i], kbs->kb_string);
289 break;
290 }
291 ret = 0;
292reterr:
293 kfree(kbs);
294 return ret;
295}
296
297static inline int
298do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
299{
300 struct consolefontdesc cfdarg;
301 int i;
302
303 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
304 return -EFAULT;
305
306 switch (cmd) {
307 case PIO_FONTX:
308 if (!perm)
309 return -EPERM;
310 op->op = KD_FONT_OP_SET;
311 op->flags = KD_FONT_FLAG_OLD;
312 op->width = 8;
313 op->height = cfdarg.charheight;
314 op->charcount = cfdarg.charcount;
315 op->data = cfdarg.chardata;
316 return con_font_op(vc_cons[fg_console].d, op);
317 case GIO_FONTX: {
318 op->op = KD_FONT_OP_GET;
319 op->flags = KD_FONT_FLAG_OLD;
320 op->width = 8;
321 op->height = cfdarg.charheight;
322 op->charcount = cfdarg.charcount;
323 op->data = cfdarg.chardata;
324 i = con_font_op(vc_cons[fg_console].d, op);
325 if (i)
326 return i;
327 cfdarg.charheight = op->height;
328 cfdarg.charcount = op->charcount;
329 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
330 return -EFAULT;
331 return 0;
332 }
333 }
334 return -EINVAL;
335}
336
337static inline int
338do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
339{
340 struct unimapdesc tmp;
341
342 if (copy_from_user(&tmp, user_ud, sizeof tmp))
343 return -EFAULT;
344 if (tmp.entries)
345 if (!access_ok(VERIFY_WRITE, tmp.entries,
346 tmp.entry_ct*sizeof(struct unipair)))
347 return -EFAULT;
348 switch (cmd) {
349 case PIO_UNIMAP:
350 if (!perm)
351 return -EPERM;
352 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
353 case GIO_UNIMAP:
354 if (!perm && fg_console != vc->vc_num)
355 return -EPERM;
356 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
357 }
358 return 0;
359}
360
361/*
362 * We handle the console-specific ioctl's here. We allow the
363 * capability to modify any console, not just the fg_console.
364 */
365int vt_ioctl(struct tty_struct *tty, struct file * file,
366 unsigned int cmd, unsigned long arg)
367{
368 struct vc_data *vc = (struct vc_data *)tty->driver_data;
369 struct console_font_op op; /* used in multiple places here */
370 struct kbd_struct * kbd;
371 unsigned int console;
372 unsigned char ucval;
373 void __user *up = (void __user *)arg;
374 int i, perm;
375
376 console = vc->vc_num;
377
378 if (!vc_cons_allocated(console)) /* impossible? */
379 return -ENOIOCTLCMD;
380
381 /*
382 * To have permissions to do most of the vt ioctls, we either have
383 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
384 */
385 perm = 0;
386 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
387 perm = 1;
388
389 kbd = kbd_table + console;
390 switch (cmd) {
391 case KIOCSOUND:
392 if (!perm)
393 return -EPERM;
394 if (arg)
Emmanuel Colbusbcc8ca02005-06-28 20:44:49 -0700395 arg = CLOCK_TICK_RATE / arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 kd_mksound(arg, 0);
397 return 0;
398
399 case KDMKTONE:
400 if (!perm)
401 return -EPERM;
402 {
403 unsigned int ticks, count;
404
405 /*
406 * Generate the tone for the appropriate number of ticks.
407 * If the time is zero, turn off sound ourselves.
408 */
409 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
410 count = ticks ? (arg & 0xffff) : 0;
411 if (count)
Emmanuel Colbusbcc8ca02005-06-28 20:44:49 -0700412 count = CLOCK_TICK_RATE / count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 kd_mksound(count, ticks);
414 return 0;
415 }
416
417 case KDGKBTYPE:
418 /*
419 * this is naive.
420 */
421 ucval = KB_101;
422 goto setchar;
423
424 /*
425 * These cannot be implemented on any machine that implements
426 * ioperm() in user level (such as Alpha PCs) or not at all.
427 *
428 * XXX: you should never use these, just call ioperm directly..
429 */
430#ifdef CONFIG_X86
431 case KDADDIO:
432 case KDDELIO:
433 /*
434 * KDADDIO and KDDELIO may be able to add ports beyond what
435 * we reject here, but to be safe...
436 */
437 if (arg < GPFIRST || arg > GPLAST)
438 return -EINVAL;
439 return sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
440
441 case KDENABIO:
442 case KDDISABIO:
443 return sys_ioperm(GPFIRST, GPNUM,
444 (cmd == KDENABIO)) ? -ENXIO : 0;
445#endif
446
447 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
448
449 case KDKBDREP:
450 {
451 struct kbd_repeat kbrep;
452 int err;
453
454 if (!capable(CAP_SYS_TTY_CONFIG))
455 return -EPERM;
456
457 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat)))
458 return -EFAULT;
459 err = kbd_rate(&kbrep);
460 if (err)
461 return err;
462 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
463 return -EFAULT;
464 return 0;
465 }
466
467 case KDSETMODE:
468 /*
469 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
470 * doesn't do a whole lot. i'm not sure if it should do any
471 * restoration of modes or what...
472 *
473 * XXX It should at least call into the driver, fbdev's definitely
474 * need to restore their engine state. --BenH
475 */
476 if (!perm)
477 return -EPERM;
478 switch (arg) {
479 case KD_GRAPHICS:
480 break;
481 case KD_TEXT0:
482 case KD_TEXT1:
483 arg = KD_TEXT;
484 case KD_TEXT:
485 break;
486 default:
487 return -EINVAL;
488 }
489 if (vc->vc_mode == (unsigned char) arg)
490 return 0;
491 vc->vc_mode = (unsigned char) arg;
492 if (console != fg_console)
493 return 0;
494 /*
495 * explicitly blank/unblank the screen if switching modes
496 */
497 acquire_console_sem();
498 if (arg == KD_TEXT)
499 do_unblank_screen(1);
500 else
501 do_blank_screen(1);
502 release_console_sem();
503 return 0;
504
505 case KDGETMODE:
506 ucval = vc->vc_mode;
507 goto setint;
508
509 case KDMAPDISP:
510 case KDUNMAPDISP:
511 /*
512 * these work like a combination of mmap and KDENABIO.
513 * this could be easily finished.
514 */
515 return -EINVAL;
516
517 case KDSKBMODE:
518 if (!perm)
519 return -EPERM;
520 switch(arg) {
521 case K_RAW:
522 kbd->kbdmode = VC_RAW;
523 break;
524 case K_MEDIUMRAW:
525 kbd->kbdmode = VC_MEDIUMRAW;
526 break;
527 case K_XLATE:
528 kbd->kbdmode = VC_XLATE;
529 compute_shiftstate();
530 break;
531 case K_UNICODE:
532 kbd->kbdmode = VC_UNICODE;
533 compute_shiftstate();
534 break;
535 default:
536 return -EINVAL;
537 }
538 tty_ldisc_flush(tty);
539 return 0;
540
541 case KDGKBMODE:
542 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
543 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
544 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
545 K_XLATE);
546 goto setint;
547
548 /* this could be folded into KDSKBMODE, but for compatibility
549 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
550 case KDSKBMETA:
551 switch(arg) {
552 case K_METABIT:
553 clr_vc_kbd_mode(kbd, VC_META);
554 break;
555 case K_ESCPREFIX:
556 set_vc_kbd_mode(kbd, VC_META);
557 break;
558 default:
559 return -EINVAL;
560 }
561 return 0;
562
563 case KDGKBMETA:
564 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
565 setint:
566 return put_user(ucval, (int __user *)arg);
567
568 case KDGETKEYCODE:
569 case KDSETKEYCODE:
570 if(!capable(CAP_SYS_TTY_CONFIG))
571 perm=0;
572 return do_kbkeycode_ioctl(cmd, up, perm);
573
574 case KDGKBENT:
575 case KDSKBENT:
576 return do_kdsk_ioctl(cmd, up, perm, kbd);
577
578 case KDGKBSENT:
579 case KDSKBSENT:
580 return do_kdgkb_ioctl(cmd, up, perm);
581
582 case KDGKBDIACR:
583 {
584 struct kbdiacrs __user *a = up;
585
586 if (put_user(accent_table_size, &a->kb_cnt))
587 return -EFAULT;
588 if (copy_to_user(a->kbdiacr, accent_table, accent_table_size*sizeof(struct kbdiacr)))
589 return -EFAULT;
590 return 0;
591 }
592
593 case KDSKBDIACR:
594 {
595 struct kbdiacrs __user *a = up;
596 unsigned int ct;
597
598 if (!perm)
599 return -EPERM;
600 if (get_user(ct,&a->kb_cnt))
601 return -EFAULT;
602 if (ct >= MAX_DIACR)
603 return -EINVAL;
604 accent_table_size = ct;
605 if (copy_from_user(accent_table, a->kbdiacr, ct*sizeof(struct kbdiacr)))
606 return -EFAULT;
607 return 0;
608 }
609
610 /* the ioctls below read/set the flags usually shown in the leds */
611 /* don't use them - they will go away without warning */
612 case KDGKBLED:
613 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
614 goto setchar;
615
616 case KDSKBLED:
617 if (!perm)
618 return -EPERM;
619 if (arg & ~0x77)
620 return -EINVAL;
621 kbd->ledflagstate = (arg & 7);
622 kbd->default_ledflagstate = ((arg >> 4) & 7);
623 set_leds();
624 return 0;
625
626 /* the ioctls below only set the lights, not the functions */
627 /* for those, see KDGKBLED and KDSKBLED above */
628 case KDGETLED:
629 ucval = getledstate();
630 setchar:
631 return put_user(ucval, (char __user *)arg);
632
633 case KDSETLED:
634 if (!perm)
635 return -EPERM;
636 setledstate(kbd, arg);
637 return 0;
638
639 /*
640 * A process can indicate its willingness to accept signals
641 * generated by pressing an appropriate key combination.
642 * Thus, one can have a daemon that e.g. spawns a new console
643 * upon a keypress and then changes to it.
644 * See also the kbrequest field of inittab(5).
645 */
646 case KDSIGACCEPT:
647 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 if (!perm || !capable(CAP_KILL))
649 return -EPERM;
Jesper Juhl7ed20e12005-05-01 08:59:14 -0700650 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return -EINVAL;
Eric W. Biederman81af8d62006-10-02 02:17:13 -0700652
653 spin_lock_irq(&vt_spawn_con.lock);
654 put_pid(vt_spawn_con.pid);
655 vt_spawn_con.pid = get_pid(task_pid(current));
656 vt_spawn_con.sig = arg;
657 spin_unlock_irq(&vt_spawn_con.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return 0;
659 }
660
661 case VT_SETMODE:
662 {
663 struct vt_mode tmp;
664
665 if (!perm)
666 return -EPERM;
667 if (copy_from_user(&tmp, up, sizeof(struct vt_mode)))
668 return -EFAULT;
669 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)
670 return -EINVAL;
671 acquire_console_sem();
672 vc->vt_mode = tmp;
673 /* the frsig is ignored, so we set it to 0 */
674 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800675 put_pid(vc->vt_pid);
676 vc->vt_pid = get_pid(task_pid(current));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 /* no switch is required -- saw@shade.msu.ru */
678 vc->vt_newvt = -1;
679 release_console_sem();
680 return 0;
681 }
682
683 case VT_GETMODE:
684 {
685 struct vt_mode tmp;
686 int rc;
687
688 acquire_console_sem();
689 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
690 release_console_sem();
691
692 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
693 return rc ? -EFAULT : 0;
694 }
695
696 /*
697 * Returns global vt state. Note that VT 0 is always open, since
698 * it's an alias for the current VT, and people can't use it here.
699 * We cannot return state for more than 16 VTs, since v_state is short.
700 */
701 case VT_GETSTATE:
702 {
703 struct vt_stat __user *vtstat = up;
704 unsigned short state, mask;
705
706 if (put_user(fg_console + 1, &vtstat->v_active))
707 return -EFAULT;
708 state = 1; /* /dev/tty0 is always open */
709 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; ++i, mask <<= 1)
710 if (VT_IS_IN_USE(i))
711 state |= mask;
712 return put_user(state, &vtstat->v_state);
713 }
714
715 /*
716 * Returns the first available (non-opened) console.
717 */
718 case VT_OPENQRY:
719 for (i = 0; i < MAX_NR_CONSOLES; ++i)
720 if (! VT_IS_IN_USE(i))
721 break;
722 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
723 goto setint;
724
725 /*
726 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
727 * with num >= 1 (switches to vt 0, our console, are not allowed, just
728 * to preserve sanity).
729 */
730 case VT_ACTIVATE:
731 if (!perm)
732 return -EPERM;
733 if (arg == 0 || arg > MAX_NR_CONSOLES)
734 return -ENXIO;
735 arg--;
736 acquire_console_sem();
737 i = vc_allocate(arg);
738 release_console_sem();
739 if (i)
740 return i;
741 set_console(arg);
742 return 0;
743
744 /*
745 * wait until the specified VT has been activated
746 */
747 case VT_WAITACTIVE:
748 if (!perm)
749 return -EPERM;
750 if (arg == 0 || arg > MAX_NR_CONSOLES)
751 return -ENXIO;
752 return vt_waitactive(arg-1);
753
754 /*
755 * If a vt is under process control, the kernel will not switch to it
756 * immediately, but postpone the operation until the process calls this
757 * ioctl, allowing the switch to complete.
758 *
759 * According to the X sources this is the behavior:
760 * 0: pending switch-from not OK
761 * 1: pending switch-from OK
762 * 2: completed switch-to OK
763 */
764 case VT_RELDISP:
765 if (!perm)
766 return -EPERM;
767 if (vc->vt_mode.mode != VT_PROCESS)
768 return -EINVAL;
769
770 /*
771 * Switching-from response
772 */
Samuel Ortiz8792f962007-10-01 01:20:12 -0700773 acquire_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (vc->vt_newvt >= 0) {
775 if (arg == 0)
776 /*
777 * Switch disallowed, so forget we were trying
778 * to do it.
779 */
780 vc->vt_newvt = -1;
781
782 else {
783 /*
784 * The current vt has been released, so
785 * complete the switch.
786 */
787 int newvt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 newvt = vc->vt_newvt;
789 vc->vt_newvt = -1;
790 i = vc_allocate(newvt);
791 if (i) {
792 release_console_sem();
793 return i;
794 }
795 /*
796 * When we actually do the console switch,
797 * make sure we are atomic with respect to
798 * other console switches..
799 */
800 complete_change_console(vc_cons[newvt].d);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802 }
803
804 /*
805 * Switched-to response
806 */
807 else
808 {
809 /*
810 * If it's just an ACK, ignore it
811 */
Samuel Ortiz8792f962007-10-01 01:20:12 -0700812 if (arg != VT_ACKACQ) {
813 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 return -EINVAL;
Samuel Ortiz8792f962007-10-01 01:20:12 -0700815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 }
Samuel Ortiz8792f962007-10-01 01:20:12 -0700817 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 return 0;
820
821 /*
822 * Disallocate memory associated to VT (but leave VT1)
823 */
824 case VT_DISALLOCATE:
825 if (arg > MAX_NR_CONSOLES)
826 return -ENXIO;
827 if (arg == 0) {
Alan Coxca9bda02006-09-29 02:00:03 -0700828 /* deallocate all unused consoles, but leave 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 acquire_console_sem();
830 for (i=1; i<MAX_NR_CONSOLES; i++)
831 if (! VT_BUSY(i))
Alan Coxca9bda02006-09-29 02:00:03 -0700832 vc_deallocate(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 release_console_sem();
834 } else {
Alan Coxca9bda02006-09-29 02:00:03 -0700835 /* deallocate a single console, if possible */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 arg--;
837 if (VT_BUSY(arg))
838 return -EBUSY;
839 if (arg) { /* leave 0 */
840 acquire_console_sem();
Alan Coxca9bda02006-09-29 02:00:03 -0700841 vc_deallocate(arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 release_console_sem();
843 }
844 }
845 return 0;
846
847 case VT_RESIZE:
848 {
849 struct vt_sizes __user *vtsizes = up;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700850 struct vc_data *vc;
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 ushort ll,cc;
853 if (!perm)
854 return -EPERM;
855 if (get_user(ll, &vtsizes->v_rows) ||
856 get_user(cc, &vtsizes->v_cols))
857 return -EFAULT;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700858
859 for (i = 0; i < MAX_NR_CONSOLES; i++) {
860 vc = vc_cons[i].d;
861
862 if (vc) {
863 vc->vc_resize_user = 1;
864 vc_lock_resize(vc_cons[i].d, cc, ll);
865 }
866 }
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 return 0;
869 }
870
871 case VT_RESIZEX:
872 {
873 struct vt_consize __user *vtconsize = up;
874 ushort ll,cc,vlin,clin,vcol,ccol;
875 if (!perm)
876 return -EPERM;
877 if (!access_ok(VERIFY_READ, vtconsize,
878 sizeof(struct vt_consize)))
879 return -EFAULT;
880 __get_user(ll, &vtconsize->v_rows);
881 __get_user(cc, &vtconsize->v_cols);
882 __get_user(vlin, &vtconsize->v_vlin);
883 __get_user(clin, &vtconsize->v_clin);
884 __get_user(vcol, &vtconsize->v_vcol);
885 __get_user(ccol, &vtconsize->v_ccol);
886 vlin = vlin ? vlin : vc->vc_scan_lines;
887 if (clin) {
888 if (ll) {
889 if (ll != vlin/clin)
890 return -EINVAL; /* Parameters don't add up */
891 } else
892 ll = vlin/clin;
893 }
894 if (vcol && ccol) {
895 if (cc) {
896 if (cc != vcol/ccol)
897 return -EINVAL;
898 } else
899 cc = vcol/ccol;
900 }
901
902 if (clin > 32)
903 return -EINVAL;
904
905 for (i = 0; i < MAX_NR_CONSOLES; i++) {
906 if (!vc_cons[i].d)
907 continue;
908 acquire_console_sem();
909 if (vlin)
910 vc_cons[i].d->vc_scan_lines = vlin;
911 if (clin)
912 vc_cons[i].d->vc_font.height = clin;
Antonino A. Daplase400b6e2007-10-16 01:29:35 -0700913 vc_cons[i].d->vc_resize_user = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 vc_resize(vc_cons[i].d, cc, ll);
915 release_console_sem();
916 }
917 return 0;
918 }
919
920 case PIO_FONT: {
921 if (!perm)
922 return -EPERM;
923 op.op = KD_FONT_OP_SET;
924 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
925 op.width = 8;
926 op.height = 0;
927 op.charcount = 256;
928 op.data = up;
929 return con_font_op(vc_cons[fg_console].d, &op);
930 }
931
932 case GIO_FONT: {
933 op.op = KD_FONT_OP_GET;
934 op.flags = KD_FONT_FLAG_OLD;
935 op.width = 8;
936 op.height = 32;
937 op.charcount = 256;
938 op.data = up;
939 return con_font_op(vc_cons[fg_console].d, &op);
940 }
941
942 case PIO_CMAP:
943 if (!perm)
944 return -EPERM;
945 return con_set_cmap(up);
946
947 case GIO_CMAP:
948 return con_get_cmap(up);
949
950 case PIO_FONTX:
951 case GIO_FONTX:
952 return do_fontx_ioctl(cmd, up, perm, &op);
953
954 case PIO_FONTRESET:
955 {
956 if (!perm)
957 return -EPERM;
958
959#ifdef BROKEN_GRAPHICS_PROGRAMS
960 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
961 font is not saved. */
962 return -ENOSYS;
963#else
964 {
965 op.op = KD_FONT_OP_SET_DEFAULT;
966 op.data = NULL;
967 i = con_font_op(vc_cons[fg_console].d, &op);
968 if (i)
969 return i;
970 con_set_default_unimap(vc_cons[fg_console].d);
971 return 0;
972 }
973#endif
974 }
975
976 case KDFONTOP: {
977 if (copy_from_user(&op, up, sizeof(op)))
978 return -EFAULT;
979 if (!perm && op.op != KD_FONT_OP_GET)
980 return -EPERM;
981 i = con_font_op(vc, &op);
982 if (i) return i;
983 if (copy_to_user(up, &op, sizeof(op)))
984 return -EFAULT;
985 return 0;
986 }
987
988 case PIO_SCRNMAP:
989 if (!perm)
990 return -EPERM;
991 return con_set_trans_old(up);
992
993 case GIO_SCRNMAP:
994 return con_get_trans_old(up);
995
996 case PIO_UNISCRNMAP:
997 if (!perm)
998 return -EPERM;
999 return con_set_trans_new(up);
1000
1001 case GIO_UNISCRNMAP:
1002 return con_get_trans_new(up);
1003
1004 case PIO_UNIMAPCLR:
1005 { struct unimapinit ui;
1006 if (!perm)
1007 return -EPERM;
1008 i = copy_from_user(&ui, up, sizeof(struct unimapinit));
1009 if (i) return -EFAULT;
1010 con_clear_unimap(vc, &ui);
1011 return 0;
1012 }
1013
1014 case PIO_UNIMAP:
1015 case GIO_UNIMAP:
1016 return do_unimap_ioctl(cmd, up, perm, vc);
1017
1018 case VT_LOCKSWITCH:
1019 if (!capable(CAP_SYS_TTY_CONFIG))
1020 return -EPERM;
1021 vt_dont_switch = 1;
1022 return 0;
1023 case VT_UNLOCKSWITCH:
1024 if (!capable(CAP_SYS_TTY_CONFIG))
1025 return -EPERM;
1026 vt_dont_switch = 0;
1027 return 0;
Samuel Thibault533475d2006-08-27 01:23:39 -07001028 case VT_GETHIFONTMASK:
1029 return put_user(vc->vc_hi_font_mask, (unsigned short __user *)arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 default:
1031 return -ENOIOCTLCMD;
1032 }
1033}
1034
1035/*
1036 * Sometimes we want to wait until a particular VT has been activated. We
1037 * do it in a very simple manner. Everybody waits on a single queue and
1038 * get woken up at once. Those that are satisfied go on with their business,
1039 * while those not ready go back to sleep. Seems overkill to add a wait
1040 * to each vt just for this - usually this does nothing!
1041 */
1042static DECLARE_WAIT_QUEUE_HEAD(vt_activate_queue);
1043
1044/*
1045 * Sleeps until a vt is activated, or the task is interrupted. Returns
Linus Torvalds70cb9792007-10-07 16:02:55 -07001046 * 0 if activation, -EINTR if interrupted by a signal handler.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 */
1048int vt_waitactive(int vt)
1049{
1050 int retval;
1051 DECLARE_WAITQUEUE(wait, current);
1052
1053 add_wait_queue(&vt_activate_queue, &wait);
1054 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 retval = 0;
Michal Januszewskif9915192007-04-01 23:49:51 -07001056
1057 /*
1058 * Synchronize with redraw_screen(). By acquiring the console
1059 * semaphore we make sure that the console switch is completed
1060 * before we return. If we didn't wait for the semaphore, we
1061 * could return at a point where fg_console has already been
1062 * updated, but the console switch hasn't been completed.
1063 */
1064 acquire_console_sem();
1065 set_current_state(TASK_INTERRUPTIBLE);
1066 if (vt == fg_console) {
1067 release_console_sem();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 break;
Michal Januszewskif9915192007-04-01 23:49:51 -07001069 }
1070 release_console_sem();
Linus Torvalds70cb9792007-10-07 16:02:55 -07001071 retval = -ERESTARTNOHAND;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (signal_pending(current))
1073 break;
1074 schedule();
1075 }
1076 remove_wait_queue(&vt_activate_queue, &wait);
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -07001077 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return retval;
1079}
1080
1081#define vt_wake_waitactive() wake_up(&vt_activate_queue)
1082
1083void reset_vc(struct vc_data *vc)
1084{
1085 vc->vc_mode = KD_TEXT;
1086 kbd_table[vc->vc_num].kbdmode = VC_XLATE;
1087 vc->vt_mode.mode = VT_AUTO;
1088 vc->vt_mode.waitv = 0;
1089 vc->vt_mode.relsig = 0;
1090 vc->vt_mode.acqsig = 0;
1091 vc->vt_mode.frsig = 0;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001092 put_pid(vc->vt_pid);
1093 vc->vt_pid = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 vc->vt_newvt = -1;
1095 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1096 reset_palette(vc);
1097}
1098
Eric W. Biederman8b6312f2007-02-10 01:44:34 -08001099void vc_SAK(struct work_struct *work)
1100{
1101 struct vc *vc_con =
1102 container_of(work, struct vc, SAK_work);
1103 struct vc_data *vc;
1104 struct tty_struct *tty;
1105
1106 acquire_console_sem();
1107 vc = vc_con->d;
1108 if (vc) {
1109 tty = vc->vc_tty;
1110 /*
1111 * SAK should also work in all raw modes and reset
1112 * them properly.
1113 */
1114 if (tty)
1115 __do_SAK(tty);
1116 reset_vc(vc);
1117 }
1118 release_console_sem();
1119}
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121/*
1122 * Performs the back end of a vt switch
1123 */
1124static void complete_change_console(struct vc_data *vc)
1125{
1126 unsigned char old_vc_mode;
1127
1128 last_console = fg_console;
1129
1130 /*
1131 * If we're switching, we could be going from KD_GRAPHICS to
1132 * KD_TEXT mode or vice versa, which means we need to blank or
1133 * unblank the screen later.
1134 */
1135 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1136 switch_screen(vc);
1137
1138 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001139 * This can't appear below a successful kill_pid(). If it did,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * then the *blank_screen operation could occur while X, having
1141 * received acqsig, is waking up on another processor. This
1142 * condition can lead to overlapping accesses to the VGA range
1143 * and the framebuffer (causing system lockups).
1144 *
1145 * To account for this we duplicate this code below only if the
1146 * controlling process is gone and we've called reset_vc.
1147 */
1148 if (old_vc_mode != vc->vc_mode) {
1149 if (vc->vc_mode == KD_TEXT)
1150 do_unblank_screen(1);
1151 else
1152 do_blank_screen(1);
1153 }
1154
1155 /*
1156 * If this new console is under process control, send it a signal
1157 * telling it that it has acquired. Also check if it has died and
1158 * clean up (similar to logic employed in change_console())
1159 */
1160 if (vc->vt_mode.mode == VT_PROCESS) {
1161 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001162 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 * tell us if the process has gone or something else
1164 * is awry
1165 */
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001166 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 /*
1168 * The controlling process has died, so we revert back to
1169 * normal operation. In this case, we'll also change back
1170 * to KD_TEXT mode. I'm not sure if this is strictly correct
1171 * but it saves the agony when the X server dies and the screen
1172 * remains blanked due to KD_GRAPHICS! It would be nice to do
1173 * this outside of VT_PROCESS but there is no single process
1174 * to account for and tracking tty count may be undesirable.
1175 */
1176 reset_vc(vc);
1177
1178 if (old_vc_mode != vc->vc_mode) {
1179 if (vc->vc_mode == KD_TEXT)
1180 do_unblank_screen(1);
1181 else
1182 do_blank_screen(1);
1183 }
1184 }
1185 }
1186
1187 /*
1188 * Wake anyone waiting for their VT to activate
1189 */
1190 vt_wake_waitactive();
1191 return;
1192}
1193
1194/*
1195 * Performs the front-end of a vt switch
1196 */
1197void change_console(struct vc_data *new_vc)
1198{
1199 struct vc_data *vc;
1200
1201 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1202 return;
1203
1204 /*
1205 * If this vt is in process mode, then we need to handshake with
1206 * that process before switching. Essentially, we store where that
1207 * vt wants to switch to and wait for it to tell us when it's done
1208 * (via VT_RELDISP ioctl).
1209 *
1210 * We also check to see if the controlling process still exists.
1211 * If it doesn't, we reset this vt to auto mode and continue.
1212 * This is a cheap way to track process control. The worst thing
1213 * that can happen is: we send a signal to a process, it dies, and
1214 * the switch gets "lost" waiting for a response; hopefully, the
1215 * user will try again, we'll detect the process is gone (unless
1216 * the user waits just the right amount of time :-) and revert the
1217 * vt to auto control.
1218 */
1219 vc = vc_cons[fg_console].d;
1220 if (vc->vt_mode.mode == VT_PROCESS) {
1221 /*
Eric W. Biederman3dfcaf162006-12-13 00:34:05 -08001222 * Send the signal as privileged - kill_pid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 * tell us if the process has gone or something else
Jan Lübbea64314e2007-09-29 18:47:51 +02001224 * is awry.
1225 *
1226 * We need to set vt_newvt *before* sending the signal or we
1227 * have a race.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 */
Jan Lübbea64314e2007-09-29 18:47:51 +02001229 vc->vt_newvt = new_vc->vc_num;
Eric W. Biedermanbde0d2c92006-10-02 02:17:14 -07001230 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 /*
1232 * It worked. Mark the vt to switch to and
1233 * return. The process needs to send us a
1234 * VT_RELDISP ioctl to complete the switch.
1235 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return;
1237 }
1238
1239 /*
1240 * The controlling process has died, so we revert back to
1241 * normal operation. In this case, we'll also change back
1242 * to KD_TEXT mode. I'm not sure if this is strictly correct
1243 * but it saves the agony when the X server dies and the screen
1244 * remains blanked due to KD_GRAPHICS! It would be nice to do
1245 * this outside of VT_PROCESS but there is no single process
1246 * to account for and tracking tty count may be undesirable.
1247 */
1248 reset_vc(vc);
1249
1250 /*
1251 * Fall through to normal (VT_AUTO) handling of the switch...
1252 */
1253 }
1254
1255 /*
1256 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1257 */
1258 if (vc->vc_mode == KD_GRAPHICS)
1259 return;
1260
1261 complete_change_console(new_vc);
1262}