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/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>
21 #include <linux/string.h>
22 #include <linux/slab.h>
23 #include <linux/major.h>
25 #include <linux/console.h>
26 #include <linux/consolemap.h>
27 #include <linux/signal.h>
28 #include <linux/timex.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>
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.
58 #include <linux/syscalls.h>
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
69 #define GPNUM (GPLAST - GPFIRST + 1)
71 #define i (tmp.kb_index)
72 #define s (tmp.kb_table)
73 #define v (tmp.kb_value)
75 do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
78 ushort *key_map, val, ov;
80 if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
83 if (!capable(CAP_SYS_TTY_CONFIG))
88 key_map = key_maps[s];
91 if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
94 val = (i ? K_HOLE : K_NOSUCHMAP);
95 return put_user(val, &user_kbe->kb_value);
99 if (!i && v == K_NOSUCHMAP) {
101 key_map = key_maps[s];
104 if (key_map[0] == U(K_ALLOCATED)) {
112 if (KTYP(v) < NR_TYPES) {
113 if (KVAL(v) > max_vals[KTYP(v)])
116 if (kbd->kbdmode != VC_UNICODE)
119 /* ++Geert: non-PC keyboards may generate keycode zero */
120 #if !defined(__mc68000__) && !defined(__powerpc__)
121 /* assignment to entry 0 only tests validity of args */
126 if (!(key_map = key_maps[s])) {
129 if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
130 !capable(CAP_SYS_RESOURCE))
133 key_map = kmalloc(sizeof(plain_map),
137 key_maps[s] = key_map;
138 key_map[0] = U(K_ALLOCATED);
139 for (j = 1; j < NR_KEYS; j++)
140 key_map[j] = U(K_HOLE);
145 break; /* nothing to do */
149 if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
152 if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
153 compute_shiftstate();
163 do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
165 struct kbkeycode tmp;
168 if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
172 kc = getkeycode(tmp.scancode);
174 kc = put_user(kc, &user_kbkc->keycode);
179 kc = setkeycode(tmp.scancode, tmp.keycode);
186 do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
188 struct kbsentry *kbs;
194 char *first_free, *fj, *fnw;
198 if (!capable(CAP_SYS_TTY_CONFIG))
201 kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
207 /* we mostly copy too much here (512bytes), but who cares ;) */
208 if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
212 kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
217 sz = sizeof(kbs->kb_string) - 1; /* sz should have been
219 up = user_kdgkb->kb_string;
222 for ( ; *p && sz; p++, sz--)
223 if (put_user(*p, up++)) {
227 if (put_user('\0', up)) {
232 return ((p && *p) ? -EOVERFLOW : 0);
240 first_free = funcbufptr + (funcbufsize - funcbufleft);
241 for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
248 delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
249 if (delta <= funcbufleft) { /* it fits in current buf */
250 if (j < MAX_NR_FUNC) {
251 memmove(fj + delta, fj, first_free - fj);
252 for (k = j; k < MAX_NR_FUNC; k++)
254 func_table[k] += delta;
258 funcbufleft -= delta;
259 } else { /* allocate a larger buffer */
261 while (sz < funcbufsize - funcbufleft + delta)
263 fnw = kmalloc(sz, GFP_KERNEL);
272 memmove(fnw, funcbufptr, fj - funcbufptr);
273 for (k = 0; k < j; k++)
275 func_table[k] = fnw + (func_table[k] - funcbufptr);
277 if (first_free > fj) {
278 memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
279 for (k = j; k < MAX_NR_FUNC; k++)
281 func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
283 if (funcbufptr != func_buf)
286 funcbufleft = funcbufleft - delta + sz - funcbufsize;
289 strcpy(func_table[i], kbs->kb_string);
299 do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
301 struct consolefontdesc cfdarg;
304 if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
311 op->op = KD_FONT_OP_SET;
312 op->flags = KD_FONT_FLAG_OLD;
314 op->height = cfdarg.charheight;
315 op->charcount = cfdarg.charcount;
316 op->data = cfdarg.chardata;
317 return con_font_op(vc_cons[fg_console].d, op);
319 op->op = KD_FONT_OP_GET;
320 op->flags = KD_FONT_FLAG_OLD;
322 op->height = cfdarg.charheight;
323 op->charcount = cfdarg.charcount;
324 op->data = cfdarg.chardata;
325 i = con_font_op(vc_cons[fg_console].d, op);
328 cfdarg.charheight = op->height;
329 cfdarg.charcount = op->charcount;
330 if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
339 do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
341 struct unimapdesc tmp;
343 if (copy_from_user(&tmp, user_ud, sizeof tmp))
346 if (!access_ok(VERIFY_WRITE, tmp.entries,
347 tmp.entry_ct*sizeof(struct unipair)))
353 return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
355 if (!perm && fg_console != vc->vc_num)
357 return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
363 * We handle the console-specific ioctl's here. We allow the
364 * capability to modify any console, not just the fg_console.
366 int vt_ioctl(struct tty_struct *tty, struct file * file,
367 unsigned int cmd, unsigned long arg)
369 struct vc_data *vc = tty->driver_data;
370 struct console_font_op op; /* used in multiple places here */
371 struct kbd_struct * kbd;
372 unsigned int console;
374 void __user *up = (void __user *)arg;
378 console = vc->vc_num;
382 if (!vc_cons_allocated(console)) { /* impossible? */
389 * To have permissions to do most of the vt ioctls, we either have
390 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
393 if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
396 kbd = kbd_table + console;
399 return tioclinux(tty, arg);
403 /* FIXME: This is an old broken API but we need to keep it
404 supported and somehow separate the historic advertised
405 tick rate from any real one */
407 arg = CLOCK_TICK_RATE / arg;
415 unsigned int ticks, count;
418 * Generate the tone for the appropriate number of ticks.
419 * If the time is zero, turn off sound ourselves.
421 ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
422 count = ticks ? (arg & 0xffff) : 0;
423 /* FIXME: This is an old broken API but we need to keep it
424 supported and somehow separate the historic advertised
425 tick rate from any real one */
427 count = CLOCK_TICK_RATE / count;
428 kd_mksound(count, ticks);
440 * These cannot be implemented on any machine that implements
441 * ioperm() in user level (such as Alpha PCs) or not at all.
443 * XXX: you should never use these, just call ioperm directly..
449 * KDADDIO and KDDELIO may be able to add ports beyond what
450 * we reject here, but to be safe...
452 if (arg < GPFIRST || arg > GPLAST) {
456 ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
461 ret = sys_ioperm(GPFIRST, GPNUM,
462 (cmd == KDENABIO)) ? -ENXIO : 0;
466 /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
470 struct kbd_repeat kbrep;
472 if (!capable(CAP_SYS_TTY_CONFIG))
475 if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
479 ret = kbd_rate(&kbrep);
482 if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
489 * currently, setting the mode from KD_TEXT to KD_GRAPHICS
490 * doesn't do a whole lot. i'm not sure if it should do any
491 * restoration of modes or what...
493 * XXX It should at least call into the driver, fbdev's definitely
494 * need to restore their engine state. --BenH
510 if (vc->vc_mode == (unsigned char) arg)
512 vc->vc_mode = (unsigned char) arg;
513 if (console != fg_console)
516 * explicitly blank/unblank the screen if switching modes
518 acquire_console_sem();
520 do_unblank_screen(1);
523 release_console_sem();
533 * these work like a combination of mmap and KDENABIO.
534 * this could be easily finished.
544 kbd->kbdmode = VC_RAW;
547 kbd->kbdmode = VC_MEDIUMRAW;
550 kbd->kbdmode = VC_XLATE;
551 compute_shiftstate();
554 kbd->kbdmode = VC_UNICODE;
555 compute_shiftstate();
561 tty_ldisc_flush(tty);
565 ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
566 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
567 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
571 /* this could be folded into KDSKBMODE, but for compatibility
572 reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
576 clr_vc_kbd_mode(kbd, VC_META);
579 set_vc_kbd_mode(kbd, VC_META);
587 ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
589 ret = put_user(ucval, (int __user *)arg);
594 if(!capable(CAP_SYS_TTY_CONFIG))
596 ret = do_kbkeycode_ioctl(cmd, up, perm);
601 ret = do_kdsk_ioctl(cmd, up, perm, kbd);
606 ret = do_kdgkb_ioctl(cmd, up, perm);
611 struct kbdiacrs __user *a = up;
612 struct kbdiacr diacr;
615 if (put_user(accent_table_size, &a->kb_cnt)) {
619 for (i = 0; i < accent_table_size; i++) {
620 diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
621 diacr.base = conv_uni_to_8bit(accent_table[i].base);
622 diacr.result = conv_uni_to_8bit(accent_table[i].result);
623 if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
632 struct kbdiacrsuc __user *a = up;
634 if (put_user(accent_table_size, &a->kb_cnt))
636 else if (copy_to_user(a->kbdiacruc, accent_table,
637 accent_table_size*sizeof(struct kbdiacruc)))
644 struct kbdiacrs __user *a = up;
645 struct kbdiacr diacr;
651 if (get_user(ct,&a->kb_cnt)) {
655 if (ct >= MAX_DIACR) {
659 accent_table_size = ct;
660 for (i = 0; i < ct; i++) {
661 if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
665 accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
666 accent_table[i].base = conv_8bit_to_uni(diacr.base);
667 accent_table[i].result = conv_8bit_to_uni(diacr.result);
674 struct kbdiacrsuc __user *a = up;
679 if (get_user(ct,&a->kb_cnt)) {
683 if (ct >= MAX_DIACR) {
687 accent_table_size = ct;
688 if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
693 /* the ioctls below read/set the flags usually shown in the leds */
694 /* don't use them - they will go away without warning */
696 ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
706 kbd->ledflagstate = (arg & 7);
707 kbd->default_ledflagstate = ((arg >> 4) & 7);
711 /* the ioctls below only set the lights, not the functions */
712 /* for those, see KDGKBLED and KDSKBLED above */
714 ucval = getledstate();
716 ret = put_user(ucval, (char __user *)arg);
722 setledstate(kbd, arg);
726 * A process can indicate its willingness to accept signals
727 * generated by pressing an appropriate key combination.
728 * Thus, one can have a daemon that e.g. spawns a new console
729 * upon a keypress and then changes to it.
730 * See also the kbrequest field of inittab(5).
734 if (!perm || !capable(CAP_KILL))
736 if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
739 spin_lock_irq(&vt_spawn_con.lock);
740 put_pid(vt_spawn_con.pid);
741 vt_spawn_con.pid = get_pid(task_pid(current));
742 vt_spawn_con.sig = arg;
743 spin_unlock_irq(&vt_spawn_con.lock);
754 if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
758 if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
762 acquire_console_sem();
764 /* the frsig is ignored, so we set it to 0 */
765 vc->vt_mode.frsig = 0;
767 vc->vt_pid = get_pid(task_pid(current));
768 /* no switch is required -- saw@shade.msu.ru */
770 release_console_sem();
779 acquire_console_sem();
780 memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
781 release_console_sem();
783 rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
790 * Returns global vt state. Note that VT 0 is always open, since
791 * it's an alias for the current VT, and people can't use it here.
792 * We cannot return state for more than 16 VTs, since v_state is short.
796 struct vt_stat __user *vtstat = up;
797 unsigned short state, mask;
799 if (put_user(fg_console + 1, &vtstat->v_active))
802 state = 1; /* /dev/tty0 is always open */
803 for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
807 ret = put_user(state, &vtstat->v_state);
813 * Returns the first available (non-opened) console.
816 for (i = 0; i < MAX_NR_CONSOLES; ++i)
817 if (! VT_IS_IN_USE(i))
819 ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
823 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
824 * with num >= 1 (switches to vt 0, our console, are not allowed, just
825 * to preserve sanity).
830 if (arg == 0 || arg > MAX_NR_CONSOLES)
834 acquire_console_sem();
835 ret = vc_allocate(arg);
836 release_console_sem();
844 * wait until the specified VT has been activated
849 if (arg == 0 || arg > MAX_NR_CONSOLES)
852 ret = vt_waitactive(arg - 1);
856 * If a vt is under process control, the kernel will not switch to it
857 * immediately, but postpone the operation until the process calls this
858 * ioctl, allowing the switch to complete.
860 * According to the X sources this is the behavior:
861 * 0: pending switch-from not OK
862 * 1: pending switch-from OK
863 * 2: completed switch-to OK
869 if (vc->vt_mode.mode != VT_PROCESS) {
874 * Switching-from response
876 acquire_console_sem();
877 if (vc->vt_newvt >= 0) {
880 * Switch disallowed, so forget we were trying
887 * The current vt has been released, so
888 * complete the switch.
891 newvt = vc->vt_newvt;
893 ret = vc_allocate(newvt);
895 release_console_sem();
899 * When we actually do the console switch,
900 * make sure we are atomic with respect to
901 * other console switches..
903 complete_change_console(vc_cons[newvt].d);
907 * Switched-to response
910 * If it's just an ACK, ignore it
912 if (arg != VT_ACKACQ)
915 release_console_sem();
919 * Disallocate memory associated to VT (but leave VT1)
922 if (arg > MAX_NR_CONSOLES) {
927 /* deallocate all unused consoles, but leave 0 */
928 acquire_console_sem();
929 for (i=1; i<MAX_NR_CONSOLES; i++)
932 release_console_sem();
934 /* deallocate a single console, if possible */
938 else if (arg) { /* leave 0 */
939 acquire_console_sem();
941 release_console_sem();
948 struct vt_sizes __user *vtsizes = up;
954 if (get_user(ll, &vtsizes->v_rows) ||
955 get_user(cc, &vtsizes->v_cols))
958 acquire_console_sem();
959 for (i = 0; i < MAX_NR_CONSOLES; i++) {
963 vc->vc_resize_user = 1;
964 vc_resize(vc_cons[i].d, cc, ll);
967 release_console_sem();
974 struct vt_consize __user *vtconsize = up;
975 ushort ll,cc,vlin,clin,vcol,ccol;
978 if (!access_ok(VERIFY_READ, vtconsize,
979 sizeof(struct vt_consize))) {
983 /* FIXME: Should check the copies properly */
984 __get_user(ll, &vtconsize->v_rows);
985 __get_user(cc, &vtconsize->v_cols);
986 __get_user(vlin, &vtconsize->v_vlin);
987 __get_user(clin, &vtconsize->v_clin);
988 __get_user(vcol, &vtconsize->v_vcol);
989 __get_user(ccol, &vtconsize->v_ccol);
990 vlin = vlin ? vlin : vc->vc_scan_lines;
993 if (ll != vlin/clin) {
994 /* Parameters don't add up */
1003 if (cc != vcol/ccol) {
1016 for (i = 0; i < MAX_NR_CONSOLES; i++) {
1019 acquire_console_sem();
1021 vc_cons[i].d->vc_scan_lines = vlin;
1023 vc_cons[i].d->vc_font.height = clin;
1024 vc_cons[i].d->vc_resize_user = 1;
1025 vc_resize(vc_cons[i].d, cc, ll);
1026 release_console_sem();
1034 op.op = KD_FONT_OP_SET;
1035 op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
1040 ret = con_font_op(vc_cons[fg_console].d, &op);
1045 op.op = KD_FONT_OP_GET;
1046 op.flags = KD_FONT_FLAG_OLD;
1051 ret = con_font_op(vc_cons[fg_console].d, &op);
1059 ret = con_set_cmap(up);
1063 ret = con_get_cmap(up);
1068 ret = do_fontx_ioctl(cmd, up, perm, &op);
1076 #ifdef BROKEN_GRAPHICS_PROGRAMS
1077 /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
1078 font is not saved. */
1083 op.op = KD_FONT_OP_SET_DEFAULT;
1085 ret = con_font_op(vc_cons[fg_console].d, &op);
1088 con_set_default_unimap(vc_cons[fg_console].d);
1095 if (copy_from_user(&op, up, sizeof(op))) {
1099 if (!perm && op.op != KD_FONT_OP_GET)
1101 ret = con_font_op(vc, &op);
1104 if (copy_to_user(up, &op, sizeof(op)))
1113 ret = con_set_trans_old(up);
1117 ret = con_get_trans_old(up);
1120 case PIO_UNISCRNMAP:
1124 ret = con_set_trans_new(up);
1127 case GIO_UNISCRNMAP:
1128 ret = con_get_trans_new(up);
1132 { struct unimapinit ui;
1135 ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
1137 con_clear_unimap(vc, &ui);
1143 ret = do_unimap_ioctl(cmd, up, perm, vc);
1147 if (!capable(CAP_SYS_TTY_CONFIG))
1151 case VT_UNLOCKSWITCH:
1152 if (!capable(CAP_SYS_TTY_CONFIG))
1156 case VT_GETHIFONTMASK:
1157 ret = put_user(vc->vc_hi_font_mask,
1158 (unsigned short __user *)arg);
1172 * Sometimes we want to wait until a particular VT has been activated. We
1173 * do it in a very simple manner. Everybody waits on a single queue and
1174 * get woken up at once. Those that are satisfied go on with their business,
1175 * while those not ready go back to sleep. Seems overkill to add a wait
1176 * to each vt just for this - usually this does nothing!
1178 static DECLARE_WAIT_QUEUE_HEAD(vt_activate_queue);
1181 * Sleeps until a vt is activated, or the task is interrupted. Returns
1182 * 0 if activation, -EINTR if interrupted by a signal handler.
1184 int vt_waitactive(int vt)
1187 DECLARE_WAITQUEUE(wait, current);
1189 add_wait_queue(&vt_activate_queue, &wait);
1194 * Synchronize with redraw_screen(). By acquiring the console
1195 * semaphore we make sure that the console switch is completed
1196 * before we return. If we didn't wait for the semaphore, we
1197 * could return at a point where fg_console has already been
1198 * updated, but the console switch hasn't been completed.
1200 acquire_console_sem();
1201 set_current_state(TASK_INTERRUPTIBLE);
1202 if (vt == fg_console) {
1203 release_console_sem();
1206 release_console_sem();
1207 retval = -ERESTARTNOHAND;
1208 if (signal_pending(current))
1212 remove_wait_queue(&vt_activate_queue, &wait);
1213 __set_current_state(TASK_RUNNING);
1217 #define vt_wake_waitactive() wake_up(&vt_activate_queue)
1219 void reset_vc(struct vc_data *vc)
1221 vc->vc_mode = KD_TEXT;
1222 kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
1223 vc->vt_mode.mode = VT_AUTO;
1224 vc->vt_mode.waitv = 0;
1225 vc->vt_mode.relsig = 0;
1226 vc->vt_mode.acqsig = 0;
1227 vc->vt_mode.frsig = 0;
1228 put_pid(vc->vt_pid);
1231 if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
1235 void vc_SAK(struct work_struct *work)
1238 container_of(work, struct vc, SAK_work);
1240 struct tty_struct *tty;
1242 acquire_console_sem();
1247 * SAK should also work in all raw modes and reset
1254 release_console_sem();
1258 * Performs the back end of a vt switch
1260 static void complete_change_console(struct vc_data *vc)
1262 unsigned char old_vc_mode;
1264 last_console = fg_console;
1267 * If we're switching, we could be going from KD_GRAPHICS to
1268 * KD_TEXT mode or vice versa, which means we need to blank or
1269 * unblank the screen later.
1271 old_vc_mode = vc_cons[fg_console].d->vc_mode;
1275 * This can't appear below a successful kill_pid(). If it did,
1276 * then the *blank_screen operation could occur while X, having
1277 * received acqsig, is waking up on another processor. This
1278 * condition can lead to overlapping accesses to the VGA range
1279 * and the framebuffer (causing system lockups).
1281 * To account for this we duplicate this code below only if the
1282 * controlling process is gone and we've called reset_vc.
1284 if (old_vc_mode != vc->vc_mode) {
1285 if (vc->vc_mode == KD_TEXT)
1286 do_unblank_screen(1);
1292 * If this new console is under process control, send it a signal
1293 * telling it that it has acquired. Also check if it has died and
1294 * clean up (similar to logic employed in change_console())
1296 if (vc->vt_mode.mode == VT_PROCESS) {
1298 * Send the signal as privileged - kill_pid() will
1299 * tell us if the process has gone or something else
1302 if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
1304 * The controlling process has died, so we revert back to
1305 * normal operation. In this case, we'll also change back
1306 * to KD_TEXT mode. I'm not sure if this is strictly correct
1307 * but it saves the agony when the X server dies and the screen
1308 * remains blanked due to KD_GRAPHICS! It would be nice to do
1309 * this outside of VT_PROCESS but there is no single process
1310 * to account for and tracking tty count may be undesirable.
1314 if (old_vc_mode != vc->vc_mode) {
1315 if (vc->vc_mode == KD_TEXT)
1316 do_unblank_screen(1);
1324 * Wake anyone waiting for their VT to activate
1326 vt_wake_waitactive();
1331 * Performs the front-end of a vt switch
1333 void change_console(struct vc_data *new_vc)
1337 if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
1341 * If this vt is in process mode, then we need to handshake with
1342 * that process before switching. Essentially, we store where that
1343 * vt wants to switch to and wait for it to tell us when it's done
1344 * (via VT_RELDISP ioctl).
1346 * We also check to see if the controlling process still exists.
1347 * If it doesn't, we reset this vt to auto mode and continue.
1348 * This is a cheap way to track process control. The worst thing
1349 * that can happen is: we send a signal to a process, it dies, and
1350 * the switch gets "lost" waiting for a response; hopefully, the
1351 * user will try again, we'll detect the process is gone (unless
1352 * the user waits just the right amount of time :-) and revert the
1353 * vt to auto control.
1355 vc = vc_cons[fg_console].d;
1356 if (vc->vt_mode.mode == VT_PROCESS) {
1358 * Send the signal as privileged - kill_pid() will
1359 * tell us if the process has gone or something else
1362 * We need to set vt_newvt *before* sending the signal or we
1365 vc->vt_newvt = new_vc->vc_num;
1366 if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
1368 * It worked. Mark the vt to switch to and
1369 * return. The process needs to send us a
1370 * VT_RELDISP ioctl to complete the switch.
1376 * The controlling process has died, so we revert back to
1377 * normal operation. In this case, we'll also change back
1378 * to KD_TEXT mode. I'm not sure if this is strictly correct
1379 * but it saves the agony when the X server dies and the screen
1380 * remains blanked due to KD_GRAPHICS! It would be nice to do
1381 * this outside of VT_PROCESS but there is no single process
1382 * to account for and tracking tty count may be undesirable.
1387 * Fall through to normal (VT_AUTO) handling of the switch...
1392 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
1394 if (vc->vc_mode == KD_GRAPHICS)
1397 complete_change_console(new_vc);