[TCP]: Fix bug #5070: kernel BUG at net/ipv4/tcp_output.c:864
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / vt_ioctl.c
blob1d44f69e1fda12bfb0abd092435d53ab04d9c610
1 /*
2 * linux/drivers/char/vt_ioctl.c
4 * Copyright (C) 1992 obz under the linux copyright
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
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>
30 #include <asm/io.h>
31 #include <asm/uaccess.h>
33 #include <linux/kbd_kern.h>
34 #include <linux/vt_kern.h>
35 #include <linux/kbd_diacr.h>
36 #include <linux/selection.h>
38 static char vt_dont_switch;
39 extern struct tty_driver *console_driver;
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)
45 * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
46 * experimentation and study of X386 SYSV handling.
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.
57 #ifdef CONFIG_X86
58 #include <linux/syscalls.h>
59 #endif
61 static void complete_change_console(struct vc_data *vc);
64 * these are the valid i/o ports we're allowed to change. they map all the
65 * video ports
67 #define GPFIRST 0x3b4
68 #define GPLAST 0x3df
69 #define GPNUM (GPLAST - GPFIRST + 1)
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)
77 struct kbentry tmp;
78 ushort *key_map, val, ov;
80 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
81 return -EFAULT;
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--;
106 break;
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;
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
123 if (!(key_map = key_maps[s])) {
124 int j;
126 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
127 !capable(CAP_SYS_RESOURCE))
128 return -EPERM;
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++;
140 ov = U(key_map[i]);
141 if (v == ov)
142 break; /* nothing to do */
144 * Attention Key.
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;
153 return 0;
155 #undef i
156 #undef s
157 #undef v
159 static inline int
160 do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
162 struct kbkeycode tmp;
163 int kc = 0;
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;
179 return kc;
182 static inline int
183 do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
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;
195 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
196 if (!kbs) {
197 ret = -ENOMEM;
198 goto reterr;
201 /* we mostly copy too much here (512bytes), but who cares ;) */
202 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
203 ret = -EFAULT;
204 goto reterr;
206 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
207 i = kbs->kb_func;
209 switch (cmd) {
210 case KDGKBSENT:
211 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
212 a struct member */
213 up = user_kdgkb->kb_string;
214 p = func_table[i];
215 if(p)
216 for ( ; *p && sz; p++, sz--)
217 if (put_user(*p, up++)) {
218 ret = -EFAULT;
219 goto reterr;
221 if (put_user('\0', up)) {
222 ret = -EFAULT;
223 goto reterr;
225 kfree(kbs);
226 return ((p && *p) ? -EOVERFLOW : 0);
227 case KDSKBSENT:
228 if (!perm) {
229 ret = -EPERM;
230 goto reterr;
233 q = func_table[i];
234 first_free = funcbufptr + (funcbufsize - funcbufleft);
235 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
237 if (j < MAX_NR_FUNC)
238 fj = func_table[j];
239 else
240 fj = first_free;
242 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
243 if (delta <= funcbufleft) { /* it fits in current buf */
244 if (j < MAX_NR_FUNC) {
245 memmove(fj + delta, fj, first_free - fj);
246 for (k = j; k < MAX_NR_FUNC; k++)
247 if (func_table[k])
248 func_table[k] += delta;
250 if (!q)
251 func_table[i] = fj;
252 funcbufleft -= delta;
253 } else { /* allocate a larger buffer */
254 sz = 256;
255 while (sz < funcbufsize - funcbufleft + delta)
256 sz <<= 1;
257 fnw = (char *) kmalloc(sz, GFP_KERNEL);
258 if(!fnw) {
259 ret = -ENOMEM;
260 goto reterr;
263 if (!q)
264 func_table[i] = fj;
265 if (fj > funcbufptr)
266 memmove(fnw, funcbufptr, fj - funcbufptr);
267 for (k = 0; k < j; k++)
268 if (func_table[k])
269 func_table[k] = fnw + (func_table[k] - funcbufptr);
271 if (first_free > fj) {
272 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
273 for (k = j; k < MAX_NR_FUNC; k++)
274 if (func_table[k])
275 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
277 if (funcbufptr != func_buf)
278 kfree(funcbufptr);
279 funcbufptr = fnw;
280 funcbufleft = funcbufleft - delta + sz - funcbufsize;
281 funcbufsize = sz;
283 strcpy(func_table[i], kbs->kb_string);
284 break;
286 ret = 0;
287 reterr:
288 kfree(kbs);
289 return ret;
292 static inline int
293 do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
295 struct consolefontdesc cfdarg;
296 int i;
298 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
299 return -EFAULT;
301 switch (cmd) {
302 case PIO_FONTX:
303 if (!perm)
304 return -EPERM;
305 op->op = KD_FONT_OP_SET;
306 op->flags = KD_FONT_FLAG_OLD;
307 op->width = 8;
308 op->height = cfdarg.charheight;
309 op->charcount = cfdarg.charcount;
310 op->data = cfdarg.chardata;
311 return con_font_op(vc_cons[fg_console].d, op);
312 case GIO_FONTX: {
313 op->op = KD_FONT_OP_GET;
314 op->flags = KD_FONT_FLAG_OLD;
315 op->width = 8;
316 op->height = cfdarg.charheight;
317 op->charcount = cfdarg.charcount;
318 op->data = cfdarg.chardata;
319 i = con_font_op(vc_cons[fg_console].d, op);
320 if (i)
321 return i;
322 cfdarg.charheight = op->height;
323 cfdarg.charcount = op->charcount;
324 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
325 return -EFAULT;
326 return 0;
329 return -EINVAL;
332 static inline int
333 do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
335 struct unimapdesc tmp;
337 if (copy_from_user(&tmp, user_ud, sizeof tmp))
338 return -EFAULT;
339 if (tmp.entries)
340 if (!access_ok(VERIFY_WRITE, tmp.entries,
341 tmp.entry_ct*sizeof(struct unipair)))
342 return -EFAULT;
343 switch (cmd) {
344 case PIO_UNIMAP:
345 if (!perm)
346 return -EPERM;
347 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
348 case GIO_UNIMAP:
349 if (!perm && fg_console != vc->vc_num)
350 return -EPERM;
351 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
353 return 0;
357 * We handle the console-specific ioctl's here. We allow the
358 * capability to modify any console, not just the fg_console.
360 int vt_ioctl(struct tty_struct *tty, struct file * file,
361 unsigned int cmd, unsigned long arg)
363 struct vc_data *vc = (struct vc_data *)tty->driver_data;
364 struct console_font_op op; /* used in multiple places here */
365 struct kbd_struct * kbd;
366 unsigned int console;
367 unsigned char ucval;
368 void __user *up = (void __user *)arg;
369 int i, perm;
371 console = vc->vc_num;
373 if (!vc_cons_allocated(console)) /* impossible? */
374 return -ENOIOCTLCMD;
377 * To have permissions to do most of the vt ioctls, we either have
378 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
380 perm = 0;
381 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
382 perm = 1;
384 kbd = kbd_table + console;
385 switch (cmd) {
386 case KIOCSOUND:
387 if (!perm)
388 return -EPERM;
389 if (arg)
390 arg = CLOCK_TICK_RATE / arg;
391 kd_mksound(arg, 0);
392 return 0;
394 case KDMKTONE:
395 if (!perm)
396 return -EPERM;
398 unsigned int ticks, count;
401 * Generate the tone for the appropriate number of ticks.
402 * If the time is zero, turn off sound ourselves.
404 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
405 count = ticks ? (arg & 0xffff) : 0;
406 if (count)
407 count = CLOCK_TICK_RATE / count;
408 kd_mksound(count, ticks);
409 return 0;
412 case KDGKBTYPE:
414 * this is naive.
416 ucval = KB_101;
417 goto setchar;
420 * These cannot be implemented on any machine that implements
421 * ioperm() in user level (such as Alpha PCs) or not at all.
423 * XXX: you should never use these, just call ioperm directly..
425 #ifdef CONFIG_X86
426 case KDADDIO:
427 case KDDELIO:
429 * KDADDIO and KDDELIO may be able to add ports beyond what
430 * we reject here, but to be safe...
432 if (arg < GPFIRST || arg > GPLAST)
433 return -EINVAL;
434 return sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
436 case KDENABIO:
437 case KDDISABIO:
438 return sys_ioperm(GPFIRST, GPNUM,
439 (cmd == KDENABIO)) ? -ENXIO : 0;
440 #endif
442 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
444 case KDKBDREP:
446 struct kbd_repeat kbrep;
447 int err;
449 if (!capable(CAP_SYS_TTY_CONFIG))
450 return -EPERM;
452 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat)))
453 return -EFAULT;
454 err = kbd_rate(&kbrep);
455 if (err)
456 return err;
457 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
458 return -EFAULT;
459 return 0;
462 case KDSETMODE:
464 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
465 * doesn't do a whole lot. i'm not sure if it should do any
466 * restoration of modes or what...
468 * XXX It should at least call into the driver, fbdev's definitely
469 * need to restore their engine state. --BenH
471 if (!perm)
472 return -EPERM;
473 switch (arg) {
474 case KD_GRAPHICS:
475 break;
476 case KD_TEXT0:
477 case KD_TEXT1:
478 arg = KD_TEXT;
479 case KD_TEXT:
480 break;
481 default:
482 return -EINVAL;
484 if (vc->vc_mode == (unsigned char) arg)
485 return 0;
486 vc->vc_mode = (unsigned char) arg;
487 if (console != fg_console)
488 return 0;
490 * explicitly blank/unblank the screen if switching modes
492 acquire_console_sem();
493 if (arg == KD_TEXT)
494 do_unblank_screen(1);
495 else
496 do_blank_screen(1);
497 release_console_sem();
498 return 0;
500 case KDGETMODE:
501 ucval = vc->vc_mode;
502 goto setint;
504 case KDMAPDISP:
505 case KDUNMAPDISP:
507 * these work like a combination of mmap and KDENABIO.
508 * this could be easily finished.
510 return -EINVAL;
512 case KDSKBMODE:
513 if (!perm)
514 return -EPERM;
515 switch(arg) {
516 case K_RAW:
517 kbd->kbdmode = VC_RAW;
518 break;
519 case K_MEDIUMRAW:
520 kbd->kbdmode = VC_MEDIUMRAW;
521 break;
522 case K_XLATE:
523 kbd->kbdmode = VC_XLATE;
524 compute_shiftstate();
525 break;
526 case K_UNICODE:
527 kbd->kbdmode = VC_UNICODE;
528 compute_shiftstate();
529 break;
530 default:
531 return -EINVAL;
533 tty_ldisc_flush(tty);
534 return 0;
536 case KDGKBMODE:
537 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
538 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
539 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
540 K_XLATE);
541 goto setint;
543 /* this could be folded into KDSKBMODE, but for compatibility
544 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
545 case KDSKBMETA:
546 switch(arg) {
547 case K_METABIT:
548 clr_vc_kbd_mode(kbd, VC_META);
549 break;
550 case K_ESCPREFIX:
551 set_vc_kbd_mode(kbd, VC_META);
552 break;
553 default:
554 return -EINVAL;
556 return 0;
558 case KDGKBMETA:
559 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
560 setint:
561 return put_user(ucval, (int __user *)arg);
563 case KDGETKEYCODE:
564 case KDSETKEYCODE:
565 if(!capable(CAP_SYS_TTY_CONFIG))
566 perm=0;
567 return do_kbkeycode_ioctl(cmd, up, perm);
569 case KDGKBENT:
570 case KDSKBENT:
571 return do_kdsk_ioctl(cmd, up, perm, kbd);
573 case KDGKBSENT:
574 case KDSKBSENT:
575 return do_kdgkb_ioctl(cmd, up, perm);
577 case KDGKBDIACR:
579 struct kbdiacrs __user *a = up;
581 if (put_user(accent_table_size, &a->kb_cnt))
582 return -EFAULT;
583 if (copy_to_user(a->kbdiacr, accent_table, accent_table_size*sizeof(struct kbdiacr)))
584 return -EFAULT;
585 return 0;
588 case KDSKBDIACR:
590 struct kbdiacrs __user *a = up;
591 unsigned int ct;
593 if (!perm)
594 return -EPERM;
595 if (get_user(ct,&a->kb_cnt))
596 return -EFAULT;
597 if (ct >= MAX_DIACR)
598 return -EINVAL;
599 accent_table_size = ct;
600 if (copy_from_user(accent_table, a->kbdiacr, ct*sizeof(struct kbdiacr)))
601 return -EFAULT;
602 return 0;
605 /* the ioctls below read/set the flags usually shown in the leds */
606 /* don't use them - they will go away without warning */
607 case KDGKBLED:
608 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
609 goto setchar;
611 case KDSKBLED:
612 if (!perm)
613 return -EPERM;
614 if (arg & ~0x77)
615 return -EINVAL;
616 kbd->ledflagstate = (arg & 7);
617 kbd->default_ledflagstate = ((arg >> 4) & 7);
618 set_leds();
619 return 0;
621 /* the ioctls below only set the lights, not the functions */
622 /* for those, see KDGKBLED and KDSKBLED above */
623 case KDGETLED:
624 ucval = getledstate();
625 setchar:
626 return put_user(ucval, (char __user *)arg);
628 case KDSETLED:
629 if (!perm)
630 return -EPERM;
631 setledstate(kbd, arg);
632 return 0;
635 * A process can indicate its willingness to accept signals
636 * generated by pressing an appropriate key combination.
637 * Thus, one can have a daemon that e.g. spawns a new console
638 * upon a keypress and then changes to it.
639 * See also the kbrequest field of inittab(5).
641 case KDSIGACCEPT:
643 extern int spawnpid, spawnsig;
644 if (!perm || !capable(CAP_KILL))
645 return -EPERM;
646 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
647 return -EINVAL;
648 spawnpid = current->pid;
649 spawnsig = arg;
650 return 0;
653 case VT_SETMODE:
655 struct vt_mode tmp;
657 if (!perm)
658 return -EPERM;
659 if (copy_from_user(&tmp, up, sizeof(struct vt_mode)))
660 return -EFAULT;
661 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)
662 return -EINVAL;
663 acquire_console_sem();
664 vc->vt_mode = tmp;
665 /* the frsig is ignored, so we set it to 0 */
666 vc->vt_mode.frsig = 0;
667 vc->vt_pid = current->pid;
668 /* no switch is required -- saw@shade.msu.ru */
669 vc->vt_newvt = -1;
670 release_console_sem();
671 return 0;
674 case VT_GETMODE:
676 struct vt_mode tmp;
677 int rc;
679 acquire_console_sem();
680 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
681 release_console_sem();
683 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
684 return rc ? -EFAULT : 0;
688 * Returns global vt state. Note that VT 0 is always open, since
689 * it's an alias for the current VT, and people can't use it here.
690 * We cannot return state for more than 16 VTs, since v_state is short.
692 case VT_GETSTATE:
694 struct vt_stat __user *vtstat = up;
695 unsigned short state, mask;
697 if (put_user(fg_console + 1, &vtstat->v_active))
698 return -EFAULT;
699 state = 1; /* /dev/tty0 is always open */
700 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; ++i, mask <<= 1)
701 if (VT_IS_IN_USE(i))
702 state |= mask;
703 return put_user(state, &vtstat->v_state);
707 * Returns the first available (non-opened) console.
709 case VT_OPENQRY:
710 for (i = 0; i < MAX_NR_CONSOLES; ++i)
711 if (! VT_IS_IN_USE(i))
712 break;
713 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
714 goto setint;
717 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
718 * with num >= 1 (switches to vt 0, our console, are not allowed, just
719 * to preserve sanity).
721 case VT_ACTIVATE:
722 if (!perm)
723 return -EPERM;
724 if (arg == 0 || arg > MAX_NR_CONSOLES)
725 return -ENXIO;
726 arg--;
727 acquire_console_sem();
728 i = vc_allocate(arg);
729 release_console_sem();
730 if (i)
731 return i;
732 set_console(arg);
733 return 0;
736 * wait until the specified VT has been activated
738 case VT_WAITACTIVE:
739 if (!perm)
740 return -EPERM;
741 if (arg == 0 || arg > MAX_NR_CONSOLES)
742 return -ENXIO;
743 return vt_waitactive(arg-1);
746 * If a vt is under process control, the kernel will not switch to it
747 * immediately, but postpone the operation until the process calls this
748 * ioctl, allowing the switch to complete.
750 * According to the X sources this is the behavior:
751 * 0: pending switch-from not OK
752 * 1: pending switch-from OK
753 * 2: completed switch-to OK
755 case VT_RELDISP:
756 if (!perm)
757 return -EPERM;
758 if (vc->vt_mode.mode != VT_PROCESS)
759 return -EINVAL;
762 * Switching-from response
764 if (vc->vt_newvt >= 0) {
765 if (arg == 0)
767 * Switch disallowed, so forget we were trying
768 * to do it.
770 vc->vt_newvt = -1;
772 else {
774 * The current vt has been released, so
775 * complete the switch.
777 int newvt;
778 acquire_console_sem();
779 newvt = vc->vt_newvt;
780 vc->vt_newvt = -1;
781 i = vc_allocate(newvt);
782 if (i) {
783 release_console_sem();
784 return i;
787 * When we actually do the console switch,
788 * make sure we are atomic with respect to
789 * other console switches..
791 complete_change_console(vc_cons[newvt].d);
792 release_console_sem();
797 * Switched-to response
799 else
802 * If it's just an ACK, ignore it
804 if (arg != VT_ACKACQ)
805 return -EINVAL;
808 return 0;
811 * Disallocate memory associated to VT (but leave VT1)
813 case VT_DISALLOCATE:
814 if (arg > MAX_NR_CONSOLES)
815 return -ENXIO;
816 if (arg == 0) {
817 /* disallocate all unused consoles, but leave 0 */
818 acquire_console_sem();
819 for (i=1; i<MAX_NR_CONSOLES; i++)
820 if (! VT_BUSY(i))
821 vc_disallocate(i);
822 release_console_sem();
823 } else {
824 /* disallocate a single console, if possible */
825 arg--;
826 if (VT_BUSY(arg))
827 return -EBUSY;
828 if (arg) { /* leave 0 */
829 acquire_console_sem();
830 vc_disallocate(arg);
831 release_console_sem();
834 return 0;
836 case VT_RESIZE:
838 struct vt_sizes __user *vtsizes = up;
839 ushort ll,cc;
840 if (!perm)
841 return -EPERM;
842 if (get_user(ll, &vtsizes->v_rows) ||
843 get_user(cc, &vtsizes->v_cols))
844 return -EFAULT;
845 for (i = 0; i < MAX_NR_CONSOLES; i++) {
846 acquire_console_sem();
847 vc_resize(vc_cons[i].d, cc, ll);
848 release_console_sem();
850 return 0;
853 case VT_RESIZEX:
855 struct vt_consize __user *vtconsize = up;
856 ushort ll,cc,vlin,clin,vcol,ccol;
857 if (!perm)
858 return -EPERM;
859 if (!access_ok(VERIFY_READ, vtconsize,
860 sizeof(struct vt_consize)))
861 return -EFAULT;
862 __get_user(ll, &vtconsize->v_rows);
863 __get_user(cc, &vtconsize->v_cols);
864 __get_user(vlin, &vtconsize->v_vlin);
865 __get_user(clin, &vtconsize->v_clin);
866 __get_user(vcol, &vtconsize->v_vcol);
867 __get_user(ccol, &vtconsize->v_ccol);
868 vlin = vlin ? vlin : vc->vc_scan_lines;
869 if (clin) {
870 if (ll) {
871 if (ll != vlin/clin)
872 return -EINVAL; /* Parameters don't add up */
873 } else
874 ll = vlin/clin;
876 if (vcol && ccol) {
877 if (cc) {
878 if (cc != vcol/ccol)
879 return -EINVAL;
880 } else
881 cc = vcol/ccol;
884 if (clin > 32)
885 return -EINVAL;
887 for (i = 0; i < MAX_NR_CONSOLES; i++) {
888 if (!vc_cons[i].d)
889 continue;
890 acquire_console_sem();
891 if (vlin)
892 vc_cons[i].d->vc_scan_lines = vlin;
893 if (clin)
894 vc_cons[i].d->vc_font.height = clin;
895 vc_resize(vc_cons[i].d, cc, ll);
896 release_console_sem();
898 return 0;
901 case PIO_FONT: {
902 if (!perm)
903 return -EPERM;
904 op.op = KD_FONT_OP_SET;
905 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
906 op.width = 8;
907 op.height = 0;
908 op.charcount = 256;
909 op.data = up;
910 return con_font_op(vc_cons[fg_console].d, &op);
913 case GIO_FONT: {
914 op.op = KD_FONT_OP_GET;
915 op.flags = KD_FONT_FLAG_OLD;
916 op.width = 8;
917 op.height = 32;
918 op.charcount = 256;
919 op.data = up;
920 return con_font_op(vc_cons[fg_console].d, &op);
923 case PIO_CMAP:
924 if (!perm)
925 return -EPERM;
926 return con_set_cmap(up);
928 case GIO_CMAP:
929 return con_get_cmap(up);
931 case PIO_FONTX:
932 case GIO_FONTX:
933 return do_fontx_ioctl(cmd, up, perm, &op);
935 case PIO_FONTRESET:
937 if (!perm)
938 return -EPERM;
940 #ifdef BROKEN_GRAPHICS_PROGRAMS
941 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
942 font is not saved. */
943 return -ENOSYS;
944 #else
946 op.op = KD_FONT_OP_SET_DEFAULT;
947 op.data = NULL;
948 i = con_font_op(vc_cons[fg_console].d, &op);
949 if (i)
950 return i;
951 con_set_default_unimap(vc_cons[fg_console].d);
952 return 0;
954 #endif
957 case KDFONTOP: {
958 if (copy_from_user(&op, up, sizeof(op)))
959 return -EFAULT;
960 if (!perm && op.op != KD_FONT_OP_GET)
961 return -EPERM;
962 i = con_font_op(vc, &op);
963 if (i) return i;
964 if (copy_to_user(up, &op, sizeof(op)))
965 return -EFAULT;
966 return 0;
969 case PIO_SCRNMAP:
970 if (!perm)
971 return -EPERM;
972 return con_set_trans_old(up);
974 case GIO_SCRNMAP:
975 return con_get_trans_old(up);
977 case PIO_UNISCRNMAP:
978 if (!perm)
979 return -EPERM;
980 return con_set_trans_new(up);
982 case GIO_UNISCRNMAP:
983 return con_get_trans_new(up);
985 case PIO_UNIMAPCLR:
986 { struct unimapinit ui;
987 if (!perm)
988 return -EPERM;
989 i = copy_from_user(&ui, up, sizeof(struct unimapinit));
990 if (i) return -EFAULT;
991 con_clear_unimap(vc, &ui);
992 return 0;
995 case PIO_UNIMAP:
996 case GIO_UNIMAP:
997 return do_unimap_ioctl(cmd, up, perm, vc);
999 case VT_LOCKSWITCH:
1000 if (!capable(CAP_SYS_TTY_CONFIG))
1001 return -EPERM;
1002 vt_dont_switch = 1;
1003 return 0;
1004 case VT_UNLOCKSWITCH:
1005 if (!capable(CAP_SYS_TTY_CONFIG))
1006 return -EPERM;
1007 vt_dont_switch = 0;
1008 return 0;
1009 default:
1010 return -ENOIOCTLCMD;
1015 * Sometimes we want to wait until a particular VT has been activated. We
1016 * do it in a very simple manner. Everybody waits on a single queue and
1017 * get woken up at once. Those that are satisfied go on with their business,
1018 * while those not ready go back to sleep. Seems overkill to add a wait
1019 * to each vt just for this - usually this does nothing!
1021 static DECLARE_WAIT_QUEUE_HEAD(vt_activate_queue);
1024 * Sleeps until a vt is activated, or the task is interrupted. Returns
1025 * 0 if activation, -EINTR if interrupted.
1027 int vt_waitactive(int vt)
1029 int retval;
1030 DECLARE_WAITQUEUE(wait, current);
1032 add_wait_queue(&vt_activate_queue, &wait);
1033 for (;;) {
1034 set_current_state(TASK_INTERRUPTIBLE);
1035 retval = 0;
1036 if (vt == fg_console)
1037 break;
1038 retval = -EINTR;
1039 if (signal_pending(current))
1040 break;
1041 schedule();
1043 remove_wait_queue(&vt_activate_queue, &wait);
1044 current->state = TASK_RUNNING;
1045 return retval;
1048 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
1050 void reset_vc(struct vc_data *vc)
1052 vc->vc_mode = KD_TEXT;
1053 kbd_table[vc->vc_num].kbdmode = VC_XLATE;
1054 vc->vt_mode.mode = VT_AUTO;
1055 vc->vt_mode.waitv = 0;
1056 vc->vt_mode.relsig = 0;
1057 vc->vt_mode.acqsig = 0;
1058 vc->vt_mode.frsig = 0;
1059 vc->vt_pid = -1;
1060 vc->vt_newvt = -1;
1061 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1062 reset_palette(vc);
1066 * Performs the back end of a vt switch
1068 static void complete_change_console(struct vc_data *vc)
1070 unsigned char old_vc_mode;
1072 last_console = fg_console;
1075 * If we're switching, we could be going from KD_GRAPHICS to
1076 * KD_TEXT mode or vice versa, which means we need to blank or
1077 * unblank the screen later.
1079 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1080 switch_screen(vc);
1083 * This can't appear below a successful kill_proc(). If it did,
1084 * then the *blank_screen operation could occur while X, having
1085 * received acqsig, is waking up on another processor. This
1086 * condition can lead to overlapping accesses to the VGA range
1087 * and the framebuffer (causing system lockups).
1089 * To account for this we duplicate this code below only if the
1090 * controlling process is gone and we've called reset_vc.
1092 if (old_vc_mode != vc->vc_mode) {
1093 if (vc->vc_mode == KD_TEXT)
1094 do_unblank_screen(1);
1095 else
1096 do_blank_screen(1);
1100 * If this new console is under process control, send it a signal
1101 * telling it that it has acquired. Also check if it has died and
1102 * clean up (similar to logic employed in change_console())
1104 if (vc->vt_mode.mode == VT_PROCESS) {
1106 * Send the signal as privileged - kill_proc() will
1107 * tell us if the process has gone or something else
1108 * is awry
1110 if (kill_proc(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
1112 * The controlling process has died, so we revert back to
1113 * normal operation. In this case, we'll also change back
1114 * to KD_TEXT mode. I'm not sure if this is strictly correct
1115 * but it saves the agony when the X server dies and the screen
1116 * remains blanked due to KD_GRAPHICS! It would be nice to do
1117 * this outside of VT_PROCESS but there is no single process
1118 * to account for and tracking tty count may be undesirable.
1120 reset_vc(vc);
1122 if (old_vc_mode != vc->vc_mode) {
1123 if (vc->vc_mode == KD_TEXT)
1124 do_unblank_screen(1);
1125 else
1126 do_blank_screen(1);
1132 * Wake anyone waiting for their VT to activate
1134 vt_wake_waitactive();
1135 return;
1139 * Performs the front-end of a vt switch
1141 void change_console(struct vc_data *new_vc)
1143 struct vc_data *vc;
1145 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1146 return;
1149 * If this vt is in process mode, then we need to handshake with
1150 * that process before switching. Essentially, we store where that
1151 * vt wants to switch to and wait for it to tell us when it's done
1152 * (via VT_RELDISP ioctl).
1154 * We also check to see if the controlling process still exists.
1155 * If it doesn't, we reset this vt to auto mode and continue.
1156 * This is a cheap way to track process control. The worst thing
1157 * that can happen is: we send a signal to a process, it dies, and
1158 * the switch gets "lost" waiting for a response; hopefully, the
1159 * user will try again, we'll detect the process is gone (unless
1160 * the user waits just the right amount of time :-) and revert the
1161 * vt to auto control.
1163 vc = vc_cons[fg_console].d;
1164 if (vc->vt_mode.mode == VT_PROCESS) {
1166 * Send the signal as privileged - kill_proc() will
1167 * tell us if the process has gone or something else
1168 * is awry
1170 if (kill_proc(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
1172 * It worked. Mark the vt to switch to and
1173 * return. The process needs to send us a
1174 * VT_RELDISP ioctl to complete the switch.
1176 vc->vt_newvt = new_vc->vc_num;
1177 return;
1181 * The controlling process has died, so we revert back to
1182 * normal operation. In this case, we'll also change back
1183 * to KD_TEXT mode. I'm not sure if this is strictly correct
1184 * but it saves the agony when the X server dies and the screen
1185 * remains blanked due to KD_GRAPHICS! It would be nice to do
1186 * this outside of VT_PROCESS but there is no single process
1187 * to account for and tracking tty count may be undesirable.
1189 reset_vc(vc);
1192 * Fall through to normal (VT_AUTO) handling of the switch...
1197 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1199 if (vc->vc_mode == KD_GRAPHICS)
1200 return;
1202 complete_change_console(new_vc);