vt_ioctl.c 31 KB

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