vt_ioctl.c 29 KB

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