vt_ioctl.c 28 KB

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