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