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