vt_ioctl.c 31 KB

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