PageRenderTime 1464ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/tty/vt/vt_ioctl.c

https://github.com/Mengqi/linux-2.6
C | 1800 lines | 1306 code | 176 blank | 318 comment | 300 complexity | e5a707f9d4d698c1f8ec27bfa461d2f1 MD5 | raw file
  1. /*
  2. * Copyright (C) 1992 obz under the linux copyright
  3. *
  4. * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
  5. * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
  6. * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
  7. * Some code moved for less code duplication - Andi Kleen - Mar 1997
  8. * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
  9. */
  10. #include <linux/types.h>
  11. #include <linux/errno.h>
  12. #include <linux/sched.h>
  13. #include <linux/tty.h>
  14. #include <linux/timer.h>
  15. #include <linux/kernel.h>
  16. #include <linux/compat.h>
  17. #include <linux/module.h>
  18. #include <linux/kd.h>
  19. #include <linux/vt.h>
  20. #include <linux/string.h>
  21. #include <linux/slab.h>
  22. #include <linux/major.h>
  23. #include <linux/fs.h>
  24. #include <linux/console.h>
  25. #include <linux/consolemap.h>
  26. #include <linux/signal.h>
  27. #include <linux/timex.h>
  28. #include <asm/io.h>
  29. #include <asm/uaccess.h>
  30. #include <linux/kbd_kern.h>
  31. #include <linux/vt_kern.h>
  32. #include <linux/kbd_diacr.h>
  33. #include <linux/selection.h>
  34. char vt_dont_switch;
  35. extern struct tty_driver *console_driver;
  36. #define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
  37. #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
  38. /*
  39. * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
  40. * experimentation and study of X386 SYSV handling.
  41. *
  42. * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
  43. * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
  44. * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
  45. * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
  46. * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
  47. * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
  48. * to the current console is done by the main ioctl code.
  49. */
  50. #ifdef CONFIG_X86
  51. #include <linux/syscalls.h>
  52. #endif
  53. static void complete_change_console(struct vc_data *vc);
  54. /*
  55. * User space VT_EVENT handlers
  56. */
  57. struct vt_event_wait {
  58. struct list_head list;
  59. struct vt_event event;
  60. int done;
  61. };
  62. static LIST_HEAD(vt_events);
  63. static DEFINE_SPINLOCK(vt_event_lock);
  64. static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
  65. /**
  66. * vt_event_post
  67. * @event: the event that occurred
  68. * @old: old console
  69. * @new: new console
  70. *
  71. * Post an VT event to interested VT handlers
  72. */
  73. void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
  74. {
  75. struct list_head *pos, *head;
  76. unsigned long flags;
  77. int wake = 0;
  78. spin_lock_irqsave(&vt_event_lock, flags);
  79. head = &vt_events;
  80. list_for_each(pos, head) {
  81. struct vt_event_wait *ve = list_entry(pos,
  82. struct vt_event_wait, list);
  83. if (!(ve->event.event & event))
  84. continue;
  85. ve->event.event = event;
  86. /* kernel view is consoles 0..n-1, user space view is
  87. console 1..n with 0 meaning current, so we must bias */
  88. ve->event.oldev = old + 1;
  89. ve->event.newev = new + 1;
  90. wake = 1;
  91. ve->done = 1;
  92. }
  93. spin_unlock_irqrestore(&vt_event_lock, flags);
  94. if (wake)
  95. wake_up_interruptible(&vt_event_waitqueue);
  96. }
  97. /**
  98. * vt_event_wait - wait for an event
  99. * @vw: our event
  100. *
  101. * Waits for an event to occur which completes our vt_event_wait
  102. * structure. On return the structure has wv->done set to 1 for success
  103. * or 0 if some event such as a signal ended the wait.
  104. */
  105. static void vt_event_wait(struct vt_event_wait *vw)
  106. {
  107. unsigned long flags;
  108. /* Prepare the event */
  109. INIT_LIST_HEAD(&vw->list);
  110. vw->done = 0;
  111. /* Queue our event */
  112. spin_lock_irqsave(&vt_event_lock, flags);
  113. list_add(&vw->list, &vt_events);
  114. spin_unlock_irqrestore(&vt_event_lock, flags);
  115. /* Wait for it to pass */
  116. wait_event_interruptible_tty(vt_event_waitqueue, vw->done);
  117. /* Dequeue it */
  118. spin_lock_irqsave(&vt_event_lock, flags);
  119. list_del(&vw->list);
  120. spin_unlock_irqrestore(&vt_event_lock, flags);
  121. }
  122. /**
  123. * vt_event_wait_ioctl - event ioctl handler
  124. * @arg: argument to ioctl
  125. *
  126. * Implement the VT_WAITEVENT ioctl using the VT event interface
  127. */
  128. static int vt_event_wait_ioctl(struct vt_event __user *event)
  129. {
  130. struct vt_event_wait vw;
  131. if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
  132. return -EFAULT;
  133. /* Highest supported event for now */
  134. if (vw.event.event & ~VT_MAX_EVENT)
  135. return -EINVAL;
  136. vt_event_wait(&vw);
  137. /* If it occurred report it */
  138. if (vw.done) {
  139. if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
  140. return -EFAULT;
  141. return 0;
  142. }
  143. return -EINTR;
  144. }
  145. /**
  146. * vt_waitactive - active console wait
  147. * @event: event code
  148. * @n: new console
  149. *
  150. * Helper for event waits. Used to implement the legacy
  151. * event waiting ioctls in terms of events
  152. */
  153. int vt_waitactive(int n)
  154. {
  155. struct vt_event_wait vw;
  156. do {
  157. if (n == fg_console + 1)
  158. break;
  159. vw.event.event = VT_EVENT_SWITCH;
  160. vt_event_wait(&vw);
  161. if (vw.done == 0)
  162. return -EINTR;
  163. } while (vw.event.newev != n);
  164. return 0;
  165. }
  166. /*
  167. * these are the valid i/o ports we're allowed to change. they map all the
  168. * video ports
  169. */
  170. #define GPFIRST 0x3b4
  171. #define GPLAST 0x3df
  172. #define GPNUM (GPLAST - GPFIRST + 1)
  173. #define i (tmp.kb_index)
  174. #define s (tmp.kb_table)
  175. #define v (tmp.kb_value)
  176. static inline int
  177. do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
  178. {
  179. struct kbentry tmp;
  180. ushort *key_map, val, ov;
  181. if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
  182. return -EFAULT;
  183. if (!capable(CAP_SYS_TTY_CONFIG))
  184. perm = 0;
  185. switch (cmd) {
  186. case KDGKBENT:
  187. key_map = key_maps[s];
  188. if (key_map) {
  189. val = U(key_map[i]);
  190. if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
  191. val = K_HOLE;
  192. } else
  193. val = (i ? K_HOLE : K_NOSUCHMAP);
  194. return put_user(val, &user_kbe->kb_value);
  195. case KDSKBENT:
  196. if (!perm)
  197. return -EPERM;
  198. if (!i && v == K_NOSUCHMAP) {
  199. /* deallocate map */
  200. key_map = key_maps[s];
  201. if (s && key_map) {
  202. key_maps[s] = NULL;
  203. if (key_map[0] == U(K_ALLOCATED)) {
  204. kfree(key_map);
  205. keymap_count--;
  206. }
  207. }
  208. break;
  209. }
  210. if (KTYP(v) < NR_TYPES) {
  211. if (KVAL(v) > max_vals[KTYP(v)])
  212. return -EINVAL;
  213. } else
  214. if (kbd->kbdmode != VC_UNICODE)
  215. return -EINVAL;
  216. /* ++Geert: non-PC keyboards may generate keycode zero */
  217. #if !defined(__mc68000__) && !defined(__powerpc__)
  218. /* assignment to entry 0 only tests validity of args */
  219. if (!i)
  220. break;
  221. #endif
  222. if (!(key_map = key_maps[s])) {
  223. int j;
  224. if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
  225. !capable(CAP_SYS_RESOURCE))
  226. return -EPERM;
  227. key_map = kmalloc(sizeof(plain_map),
  228. GFP_KERNEL);
  229. if (!key_map)
  230. return -ENOMEM;
  231. key_maps[s] = key_map;
  232. key_map[0] = U(K_ALLOCATED);
  233. for (j = 1; j < NR_KEYS; j++)
  234. key_map[j] = U(K_HOLE);
  235. keymap_count++;
  236. }
  237. ov = U(key_map[i]);
  238. if (v == ov)
  239. break; /* nothing to do */
  240. /*
  241. * Attention Key.
  242. */
  243. if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
  244. return -EPERM;
  245. key_map[i] = U(v);
  246. if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
  247. compute_shiftstate();
  248. break;
  249. }
  250. return 0;
  251. }
  252. #undef i
  253. #undef s
  254. #undef v
  255. static inline int
  256. do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
  257. {
  258. struct kbkeycode tmp;
  259. int kc = 0;
  260. if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
  261. return -EFAULT;
  262. switch (cmd) {
  263. case KDGETKEYCODE:
  264. kc = getkeycode(tmp.scancode);
  265. if (kc >= 0)
  266. kc = put_user(kc, &user_kbkc->keycode);
  267. break;
  268. case KDSETKEYCODE:
  269. if (!perm)
  270. return -EPERM;
  271. kc = setkeycode(tmp.scancode, tmp.keycode);
  272. break;
  273. }
  274. return kc;
  275. }
  276. static inline int
  277. do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
  278. {
  279. struct kbsentry *kbs;
  280. char *p;
  281. u_char *q;
  282. u_char __user *up;
  283. int sz;
  284. int delta;
  285. char *first_free, *fj, *fnw;
  286. int i, j, k;
  287. int ret;
  288. if (!capable(CAP_SYS_TTY_CONFIG))
  289. perm = 0;
  290. kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
  291. if (!kbs) {
  292. ret = -ENOMEM;
  293. goto reterr;
  294. }
  295. /* we mostly copy too much here (512bytes), but who cares ;) */
  296. if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
  297. ret = -EFAULT;
  298. goto reterr;
  299. }
  300. kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
  301. i = kbs->kb_func;
  302. switch (cmd) {
  303. case KDGKBSENT:
  304. sz = sizeof(kbs->kb_string) - 1; /* sz should have been
  305. a struct member */
  306. up = user_kdgkb->kb_string;
  307. p = func_table[i];
  308. if(p)
  309. for ( ; *p && sz; p++, sz--)
  310. if (put_user(*p, up++)) {
  311. ret = -EFAULT;
  312. goto reterr;
  313. }
  314. if (put_user('\0', up)) {
  315. ret = -EFAULT;
  316. goto reterr;
  317. }
  318. kfree(kbs);
  319. return ((p && *p) ? -EOVERFLOW : 0);
  320. case KDSKBSENT:
  321. if (!perm) {
  322. ret = -EPERM;
  323. goto reterr;
  324. }
  325. q = func_table[i];
  326. first_free = funcbufptr + (funcbufsize - funcbufleft);
  327. for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
  328. ;
  329. if (j < MAX_NR_FUNC)
  330. fj = func_table[j];
  331. else
  332. fj = first_free;
  333. delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
  334. if (delta <= funcbufleft) { /* it fits in current buf */
  335. if (j < MAX_NR_FUNC) {
  336. memmove(fj + delta, fj, first_free - fj);
  337. for (k = j; k < MAX_NR_FUNC; k++)
  338. if (func_table[k])
  339. func_table[k] += delta;
  340. }
  341. if (!q)
  342. func_table[i] = fj;
  343. funcbufleft -= delta;
  344. } else { /* allocate a larger buffer */
  345. sz = 256;
  346. while (sz < funcbufsize - funcbufleft + delta)
  347. sz <<= 1;
  348. fnw = kmalloc(sz, GFP_KERNEL);
  349. if(!fnw) {
  350. ret = -ENOMEM;
  351. goto reterr;
  352. }
  353. if (!q)
  354. func_table[i] = fj;
  355. if (fj > funcbufptr)
  356. memmove(fnw, funcbufptr, fj - funcbufptr);
  357. for (k = 0; k < j; k++)
  358. if (func_table[k])
  359. func_table[k] = fnw + (func_table[k] - funcbufptr);
  360. if (first_free > fj) {
  361. memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
  362. for (k = j; k < MAX_NR_FUNC; k++)
  363. if (func_table[k])
  364. func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
  365. }
  366. if (funcbufptr != func_buf)
  367. kfree(funcbufptr);
  368. funcbufptr = fnw;
  369. funcbufleft = funcbufleft - delta + sz - funcbufsize;
  370. funcbufsize = sz;
  371. }
  372. strcpy(func_table[i], kbs->kb_string);
  373. break;
  374. }
  375. ret = 0;
  376. reterr:
  377. kfree(kbs);
  378. return ret;
  379. }
  380. static inline int
  381. do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
  382. {
  383. struct consolefontdesc cfdarg;
  384. int i;
  385. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
  386. return -EFAULT;
  387. switch (cmd) {
  388. case PIO_FONTX:
  389. if (!perm)
  390. return -EPERM;
  391. op->op = KD_FONT_OP_SET;
  392. op->flags = KD_FONT_FLAG_OLD;
  393. op->width = 8;
  394. op->height = cfdarg.charheight;
  395. op->charcount = cfdarg.charcount;
  396. op->data = cfdarg.chardata;
  397. return con_font_op(vc_cons[fg_console].d, op);
  398. case GIO_FONTX: {
  399. op->op = KD_FONT_OP_GET;
  400. op->flags = KD_FONT_FLAG_OLD;
  401. op->width = 8;
  402. op->height = cfdarg.charheight;
  403. op->charcount = cfdarg.charcount;
  404. op->data = cfdarg.chardata;
  405. i = con_font_op(vc_cons[fg_console].d, op);
  406. if (i)
  407. return i;
  408. cfdarg.charheight = op->height;
  409. cfdarg.charcount = op->charcount;
  410. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
  411. return -EFAULT;
  412. return 0;
  413. }
  414. }
  415. return -EINVAL;
  416. }
  417. static inline int
  418. do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
  419. {
  420. struct unimapdesc tmp;
  421. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  422. return -EFAULT;
  423. if (tmp.entries)
  424. if (!access_ok(VERIFY_WRITE, tmp.entries,
  425. tmp.entry_ct*sizeof(struct unipair)))
  426. return -EFAULT;
  427. switch (cmd) {
  428. case PIO_UNIMAP:
  429. if (!perm)
  430. return -EPERM;
  431. return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
  432. case GIO_UNIMAP:
  433. if (!perm && fg_console != vc->vc_num)
  434. return -EPERM;
  435. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
  436. }
  437. return 0;
  438. }
  439. /*
  440. * We handle the console-specific ioctl's here. We allow the
  441. * capability to modify any console, not just the fg_console.
  442. */
  443. int vt_ioctl(struct tty_struct *tty,
  444. unsigned int cmd, unsigned long arg)
  445. {
  446. struct vc_data *vc = tty->driver_data;
  447. struct console_font_op op; /* used in multiple places here */
  448. struct kbd_struct * kbd;
  449. unsigned int console;
  450. unsigned char ucval;
  451. unsigned int uival;
  452. void __user *up = (void __user *)arg;
  453. int i, perm;
  454. int ret = 0;
  455. console = vc->vc_num;
  456. tty_lock();
  457. if (!vc_cons_allocated(console)) { /* impossible? */
  458. ret = -ENOIOCTLCMD;
  459. goto out;
  460. }
  461. /*
  462. * To have permissions to do most of the vt ioctls, we either have
  463. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  464. */
  465. perm = 0;
  466. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  467. perm = 1;
  468. kbd = kbd_table + console;
  469. switch (cmd) {
  470. case TIOCLINUX:
  471. ret = tioclinux(tty, arg);
  472. break;
  473. case KIOCSOUND:
  474. if (!perm)
  475. goto eperm;
  476. /*
  477. * The use of PIT_TICK_RATE is historic, it used to be
  478. * the platform-dependent CLOCK_TICK_RATE between 2.6.12
  479. * and 2.6.36, which was a minor but unfortunate ABI
  480. * change.
  481. */
  482. if (arg)
  483. arg = PIT_TICK_RATE / arg;
  484. kd_mksound(arg, 0);
  485. break;
  486. case KDMKTONE:
  487. if (!perm)
  488. goto eperm;
  489. {
  490. unsigned int ticks, count;
  491. /*
  492. * Generate the tone for the appropriate number of ticks.
  493. * If the time is zero, turn off sound ourselves.
  494. */
  495. ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
  496. count = ticks ? (arg & 0xffff) : 0;
  497. if (count)
  498. count = PIT_TICK_RATE / count;
  499. kd_mksound(count, ticks);
  500. break;
  501. }
  502. case KDGKBTYPE:
  503. /*
  504. * this is naive.
  505. */
  506. ucval = KB_101;
  507. goto setchar;
  508. /*
  509. * These cannot be implemented on any machine that implements
  510. * ioperm() in user level (such as Alpha PCs) or not at all.
  511. *
  512. * XXX: you should never use these, just call ioperm directly..
  513. */
  514. #ifdef CONFIG_X86
  515. case KDADDIO:
  516. case KDDELIO:
  517. /*
  518. * KDADDIO and KDDELIO may be able to add ports beyond what
  519. * we reject here, but to be safe...
  520. */
  521. if (arg < GPFIRST || arg > GPLAST) {
  522. ret = -EINVAL;
  523. break;
  524. }
  525. ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
  526. break;
  527. case KDENABIO:
  528. case KDDISABIO:
  529. ret = sys_ioperm(GPFIRST, GPNUM,
  530. (cmd == KDENABIO)) ? -ENXIO : 0;
  531. break;
  532. #endif
  533. /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
  534. case KDKBDREP:
  535. {
  536. struct kbd_repeat kbrep;
  537. if (!capable(CAP_SYS_TTY_CONFIG))
  538. goto eperm;
  539. if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
  540. ret = -EFAULT;
  541. break;
  542. }
  543. ret = kbd_rate(&kbrep);
  544. if (ret)
  545. break;
  546. if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
  547. ret = -EFAULT;
  548. break;
  549. }
  550. case KDSETMODE:
  551. /*
  552. * currently, setting the mode from KD_TEXT to KD_GRAPHICS
  553. * doesn't do a whole lot. i'm not sure if it should do any
  554. * restoration of modes or what...
  555. *
  556. * XXX It should at least call into the driver, fbdev's definitely
  557. * need to restore their engine state. --BenH
  558. */
  559. if (!perm)
  560. goto eperm;
  561. switch (arg) {
  562. case KD_GRAPHICS:
  563. break;
  564. case KD_TEXT0:
  565. case KD_TEXT1:
  566. arg = KD_TEXT;
  567. case KD_TEXT:
  568. break;
  569. default:
  570. ret = -EINVAL;
  571. goto out;
  572. }
  573. if (vc->vc_mode == (unsigned char) arg)
  574. break;
  575. vc->vc_mode = (unsigned char) arg;
  576. if (console != fg_console)
  577. break;
  578. /*
  579. * explicitly blank/unblank the screen if switching modes
  580. */
  581. console_lock();
  582. if (arg == KD_TEXT)
  583. do_unblank_screen(1);
  584. else
  585. do_blank_screen(1);
  586. console_unlock();
  587. break;
  588. case KDGETMODE:
  589. uival = vc->vc_mode;
  590. goto setint;
  591. case KDMAPDISP:
  592. case KDUNMAPDISP:
  593. /*
  594. * these work like a combination of mmap and KDENABIO.
  595. * this could be easily finished.
  596. */
  597. ret = -EINVAL;
  598. break;
  599. case KDSKBMODE:
  600. if (!perm)
  601. goto eperm;
  602. switch(arg) {
  603. case K_RAW:
  604. kbd->kbdmode = VC_RAW;
  605. break;
  606. case K_MEDIUMRAW:
  607. kbd->kbdmode = VC_MEDIUMRAW;
  608. break;
  609. case K_XLATE:
  610. kbd->kbdmode = VC_XLATE;
  611. compute_shiftstate();
  612. break;
  613. case K_UNICODE:
  614. kbd->kbdmode = VC_UNICODE;
  615. compute_shiftstate();
  616. break;
  617. case K_OFF:
  618. kbd->kbdmode = VC_OFF;
  619. break;
  620. default:
  621. ret = -EINVAL;
  622. goto out;
  623. }
  624. tty_ldisc_flush(tty);
  625. break;
  626. case KDGKBMODE:
  627. switch (kbd->kbdmode) {
  628. case VC_RAW:
  629. uival = K_RAW;
  630. break;
  631. case VC_MEDIUMRAW:
  632. uival = K_MEDIUMRAW;
  633. break;
  634. case VC_UNICODE:
  635. uival = K_UNICODE;
  636. break;
  637. case VC_OFF:
  638. uival = K_OFF;
  639. break;
  640. default:
  641. uival = K_XLATE;
  642. break;
  643. }
  644. goto setint;
  645. /* this could be folded into KDSKBMODE, but for compatibility
  646. reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
  647. case KDSKBMETA:
  648. switch(arg) {
  649. case K_METABIT:
  650. clr_vc_kbd_mode(kbd, VC_META);
  651. break;
  652. case K_ESCPREFIX:
  653. set_vc_kbd_mode(kbd, VC_META);
  654. break;
  655. default:
  656. ret = -EINVAL;
  657. }
  658. break;
  659. case KDGKBMETA:
  660. uival = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
  661. setint:
  662. ret = put_user(uival, (int __user *)arg);
  663. break;
  664. case KDGETKEYCODE:
  665. case KDSETKEYCODE:
  666. if(!capable(CAP_SYS_TTY_CONFIG))
  667. perm = 0;
  668. ret = do_kbkeycode_ioctl(cmd, up, perm);
  669. break;
  670. case KDGKBENT:
  671. case KDSKBENT:
  672. ret = do_kdsk_ioctl(cmd, up, perm, kbd);
  673. break;
  674. case KDGKBSENT:
  675. case KDSKBSENT:
  676. ret = do_kdgkb_ioctl(cmd, up, perm);
  677. break;
  678. case KDGKBDIACR:
  679. {
  680. struct kbdiacrs __user *a = up;
  681. struct kbdiacr diacr;
  682. int i;
  683. if (put_user(accent_table_size, &a->kb_cnt)) {
  684. ret = -EFAULT;
  685. break;
  686. }
  687. for (i = 0; i < accent_table_size; i++) {
  688. diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
  689. diacr.base = conv_uni_to_8bit(accent_table[i].base);
  690. diacr.result = conv_uni_to_8bit(accent_table[i].result);
  691. if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
  692. ret = -EFAULT;
  693. break;
  694. }
  695. }
  696. break;
  697. }
  698. case KDGKBDIACRUC:
  699. {
  700. struct kbdiacrsuc __user *a = up;
  701. if (put_user(accent_table_size, &a->kb_cnt))
  702. ret = -EFAULT;
  703. else if (copy_to_user(a->kbdiacruc, accent_table,
  704. accent_table_size*sizeof(struct kbdiacruc)))
  705. ret = -EFAULT;
  706. break;
  707. }
  708. case KDSKBDIACR:
  709. {
  710. struct kbdiacrs __user *a = up;
  711. struct kbdiacr diacr;
  712. unsigned int ct;
  713. int i;
  714. if (!perm)
  715. goto eperm;
  716. if (get_user(ct,&a->kb_cnt)) {
  717. ret = -EFAULT;
  718. break;
  719. }
  720. if (ct >= MAX_DIACR) {
  721. ret = -EINVAL;
  722. break;
  723. }
  724. accent_table_size = ct;
  725. for (i = 0; i < ct; i++) {
  726. if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
  727. ret = -EFAULT;
  728. break;
  729. }
  730. accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
  731. accent_table[i].base = conv_8bit_to_uni(diacr.base);
  732. accent_table[i].result = conv_8bit_to_uni(diacr.result);
  733. }
  734. break;
  735. }
  736. case KDSKBDIACRUC:
  737. {
  738. struct kbdiacrsuc __user *a = up;
  739. unsigned int ct;
  740. if (!perm)
  741. goto eperm;
  742. if (get_user(ct,&a->kb_cnt)) {
  743. ret = -EFAULT;
  744. break;
  745. }
  746. if (ct >= MAX_DIACR) {
  747. ret = -EINVAL;
  748. break;
  749. }
  750. accent_table_size = ct;
  751. if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
  752. ret = -EFAULT;
  753. break;
  754. }
  755. /* the ioctls below read/set the flags usually shown in the leds */
  756. /* don't use them - they will go away without warning */
  757. case KDGKBLED:
  758. ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
  759. goto setchar;
  760. case KDSKBLED:
  761. if (!perm)
  762. goto eperm;
  763. if (arg & ~0x77) {
  764. ret = -EINVAL;
  765. break;
  766. }
  767. kbd->ledflagstate = (arg & 7);
  768. kbd->default_ledflagstate = ((arg >> 4) & 7);
  769. set_leds();
  770. break;
  771. /* the ioctls below only set the lights, not the functions */
  772. /* for those, see KDGKBLED and KDSKBLED above */
  773. case KDGETLED:
  774. ucval = getledstate();
  775. setchar:
  776. ret = put_user(ucval, (char __user *)arg);
  777. break;
  778. case KDSETLED:
  779. if (!perm)
  780. goto eperm;
  781. setledstate(kbd, arg);
  782. break;
  783. /*
  784. * A process can indicate its willingness to accept signals
  785. * generated by pressing an appropriate key combination.
  786. * Thus, one can have a daemon that e.g. spawns a new console
  787. * upon a keypress and then changes to it.
  788. * See also the kbrequest field of inittab(5).
  789. */
  790. case KDSIGACCEPT:
  791. {
  792. if (!perm || !capable(CAP_KILL))
  793. goto eperm;
  794. if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
  795. ret = -EINVAL;
  796. else {
  797. spin_lock_irq(&vt_spawn_con.lock);
  798. put_pid(vt_spawn_con.pid);
  799. vt_spawn_con.pid = get_pid(task_pid(current));
  800. vt_spawn_con.sig = arg;
  801. spin_unlock_irq(&vt_spawn_con.lock);
  802. }
  803. break;
  804. }
  805. case VT_SETMODE:
  806. {
  807. struct vt_mode tmp;
  808. if (!perm)
  809. goto eperm;
  810. if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
  811. ret = -EFAULT;
  812. goto out;
  813. }
  814. if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
  815. ret = -EINVAL;
  816. goto out;
  817. }
  818. console_lock();
  819. vc->vt_mode = tmp;
  820. /* the frsig is ignored, so we set it to 0 */
  821. vc->vt_mode.frsig = 0;
  822. put_pid(vc->vt_pid);
  823. vc->vt_pid = get_pid(task_pid(current));
  824. /* no switch is required -- saw@shade.msu.ru */
  825. vc->vt_newvt = -1;
  826. console_unlock();
  827. break;
  828. }
  829. case VT_GETMODE:
  830. {
  831. struct vt_mode tmp;
  832. int rc;
  833. console_lock();
  834. memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
  835. console_unlock();
  836. rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
  837. if (rc)
  838. ret = -EFAULT;
  839. break;
  840. }
  841. /*
  842. * Returns global vt state. Note that VT 0 is always open, since
  843. * it's an alias for the current VT, and people can't use it here.
  844. * We cannot return state for more than 16 VTs, since v_state is short.
  845. */
  846. case VT_GETSTATE:
  847. {
  848. struct vt_stat __user *vtstat = up;
  849. unsigned short state, mask;
  850. if (put_user(fg_console + 1, &vtstat->v_active))
  851. ret = -EFAULT;
  852. else {
  853. state = 1; /* /dev/tty0 is always open */
  854. for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
  855. ++i, mask <<= 1)
  856. if (VT_IS_IN_USE(i))
  857. state |= mask;
  858. ret = put_user(state, &vtstat->v_state);
  859. }
  860. break;
  861. }
  862. /*
  863. * Returns the first available (non-opened) console.
  864. */
  865. case VT_OPENQRY:
  866. for (i = 0; i < MAX_NR_CONSOLES; ++i)
  867. if (! VT_IS_IN_USE(i))
  868. break;
  869. uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
  870. goto setint;
  871. /*
  872. * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
  873. * with num >= 1 (switches to vt 0, our console, are not allowed, just
  874. * to preserve sanity).
  875. */
  876. case VT_ACTIVATE:
  877. if (!perm)
  878. goto eperm;
  879. if (arg == 0 || arg > MAX_NR_CONSOLES)
  880. ret = -ENXIO;
  881. else {
  882. arg--;
  883. console_lock();
  884. ret = vc_allocate(arg);
  885. console_unlock();
  886. if (ret)
  887. break;
  888. set_console(arg);
  889. }
  890. break;
  891. case VT_SETACTIVATE:
  892. {
  893. struct vt_setactivate vsa;
  894. if (!perm)
  895. goto eperm;
  896. if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
  897. sizeof(struct vt_setactivate))) {
  898. ret = -EFAULT;
  899. goto out;
  900. }
  901. if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
  902. ret = -ENXIO;
  903. else {
  904. vsa.console--;
  905. console_lock();
  906. ret = vc_allocate(vsa.console);
  907. if (ret == 0) {
  908. struct vc_data *nvc;
  909. /* This is safe providing we don't drop the
  910. console sem between vc_allocate and
  911. finishing referencing nvc */
  912. nvc = vc_cons[vsa.console].d;
  913. nvc->vt_mode = vsa.mode;
  914. nvc->vt_mode.frsig = 0;
  915. put_pid(nvc->vt_pid);
  916. nvc->vt_pid = get_pid(task_pid(current));
  917. }
  918. console_unlock();
  919. if (ret)
  920. break;
  921. /* Commence switch and lock */
  922. set_console(vsa.console);
  923. }
  924. break;
  925. }
  926. /*
  927. * wait until the specified VT has been activated
  928. */
  929. case VT_WAITACTIVE:
  930. if (!perm)
  931. goto eperm;
  932. if (arg == 0 || arg > MAX_NR_CONSOLES)
  933. ret = -ENXIO;
  934. else
  935. ret = vt_waitactive(arg);
  936. break;
  937. /*
  938. * If a vt is under process control, the kernel will not switch to it
  939. * immediately, but postpone the operation until the process calls this
  940. * ioctl, allowing the switch to complete.
  941. *
  942. * According to the X sources this is the behavior:
  943. * 0: pending switch-from not OK
  944. * 1: pending switch-from OK
  945. * 2: completed switch-to OK
  946. */
  947. case VT_RELDISP:
  948. if (!perm)
  949. goto eperm;
  950. if (vc->vt_mode.mode != VT_PROCESS) {
  951. ret = -EINVAL;
  952. break;
  953. }
  954. /*
  955. * Switching-from response
  956. */
  957. console_lock();
  958. if (vc->vt_newvt >= 0) {
  959. if (arg == 0)
  960. /*
  961. * Switch disallowed, so forget we were trying
  962. * to do it.
  963. */
  964. vc->vt_newvt = -1;
  965. else {
  966. /*
  967. * The current vt has been released, so
  968. * complete the switch.
  969. */
  970. int newvt;
  971. newvt = vc->vt_newvt;
  972. vc->vt_newvt = -1;
  973. ret = vc_allocate(newvt);
  974. if (ret) {
  975. console_unlock();
  976. break;
  977. }
  978. /*
  979. * When we actually do the console switch,
  980. * make sure we are atomic with respect to
  981. * other console switches..
  982. */
  983. complete_change_console(vc_cons[newvt].d);
  984. }
  985. } else {
  986. /*
  987. * Switched-to response
  988. */
  989. /*
  990. * If it's just an ACK, ignore it
  991. */
  992. if (arg != VT_ACKACQ)
  993. ret = -EINVAL;
  994. }
  995. console_unlock();
  996. break;
  997. /*
  998. * Disallocate memory associated to VT (but leave VT1)
  999. */
  1000. case VT_DISALLOCATE:
  1001. if (arg > MAX_NR_CONSOLES) {
  1002. ret = -ENXIO;
  1003. break;
  1004. }
  1005. if (arg == 0) {
  1006. /* deallocate all unused consoles, but leave 0 */
  1007. console_lock();
  1008. for (i=1; i<MAX_NR_CONSOLES; i++)
  1009. if (! VT_BUSY(i))
  1010. vc_deallocate(i);
  1011. console_unlock();
  1012. } else {
  1013. /* deallocate a single console, if possible */
  1014. arg--;
  1015. if (VT_BUSY(arg))
  1016. ret = -EBUSY;
  1017. else if (arg) { /* leave 0 */
  1018. console_lock();
  1019. vc_deallocate(arg);
  1020. console_unlock();
  1021. }
  1022. }
  1023. break;
  1024. case VT_RESIZE:
  1025. {
  1026. struct vt_sizes __user *vtsizes = up;
  1027. struct vc_data *vc;
  1028. ushort ll,cc;
  1029. if (!perm)
  1030. goto eperm;
  1031. if (get_user(ll, &vtsizes->v_rows) ||
  1032. get_user(cc, &vtsizes->v_cols))
  1033. ret = -EFAULT;
  1034. else {
  1035. console_lock();
  1036. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1037. vc = vc_cons[i].d;
  1038. if (vc) {
  1039. vc->vc_resize_user = 1;
  1040. vc_resize(vc_cons[i].d, cc, ll);
  1041. }
  1042. }
  1043. console_unlock();
  1044. }
  1045. break;
  1046. }
  1047. case VT_RESIZEX:
  1048. {
  1049. struct vt_consize __user *vtconsize = up;
  1050. ushort ll,cc,vlin,clin,vcol,ccol;
  1051. if (!perm)
  1052. goto eperm;
  1053. if (!access_ok(VERIFY_READ, vtconsize,
  1054. sizeof(struct vt_consize))) {
  1055. ret = -EFAULT;
  1056. break;
  1057. }
  1058. /* FIXME: Should check the copies properly */
  1059. __get_user(ll, &vtconsize->v_rows);
  1060. __get_user(cc, &vtconsize->v_cols);
  1061. __get_user(vlin, &vtconsize->v_vlin);
  1062. __get_user(clin, &vtconsize->v_clin);
  1063. __get_user(vcol, &vtconsize->v_vcol);
  1064. __get_user(ccol, &vtconsize->v_ccol);
  1065. vlin = vlin ? vlin : vc->vc_scan_lines;
  1066. if (clin) {
  1067. if (ll) {
  1068. if (ll != vlin/clin) {
  1069. /* Parameters don't add up */
  1070. ret = -EINVAL;
  1071. break;
  1072. }
  1073. } else
  1074. ll = vlin/clin;
  1075. }
  1076. if (vcol && ccol) {
  1077. if (cc) {
  1078. if (cc != vcol/ccol) {
  1079. ret = -EINVAL;
  1080. break;
  1081. }
  1082. } else
  1083. cc = vcol/ccol;
  1084. }
  1085. if (clin > 32) {
  1086. ret = -EINVAL;
  1087. break;
  1088. }
  1089. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1090. if (!vc_cons[i].d)
  1091. continue;
  1092. console_lock();
  1093. if (vlin)
  1094. vc_cons[i].d->vc_scan_lines = vlin;
  1095. if (clin)
  1096. vc_cons[i].d->vc_font.height = clin;
  1097. vc_cons[i].d->vc_resize_user = 1;
  1098. vc_resize(vc_cons[i].d, cc, ll);
  1099. console_unlock();
  1100. }
  1101. break;
  1102. }
  1103. case PIO_FONT: {
  1104. if (!perm)
  1105. goto eperm;
  1106. op.op = KD_FONT_OP_SET;
  1107. op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
  1108. op.width = 8;
  1109. op.height = 0;
  1110. op.charcount = 256;
  1111. op.data = up;
  1112. ret = con_font_op(vc_cons[fg_console].d, &op);
  1113. break;
  1114. }
  1115. case GIO_FONT: {
  1116. op.op = KD_FONT_OP_GET;
  1117. op.flags = KD_FONT_FLAG_OLD;
  1118. op.width = 8;
  1119. op.height = 32;
  1120. op.charcount = 256;
  1121. op.data = up;
  1122. ret = con_font_op(vc_cons[fg_console].d, &op);
  1123. break;
  1124. }
  1125. case PIO_CMAP:
  1126. if (!perm)
  1127. ret = -EPERM;
  1128. else
  1129. ret = con_set_cmap(up);
  1130. break;
  1131. case GIO_CMAP:
  1132. ret = con_get_cmap(up);
  1133. break;
  1134. case PIO_FONTX:
  1135. case GIO_FONTX:
  1136. ret = do_fontx_ioctl(cmd, up, perm, &op);
  1137. break;
  1138. case PIO_FONTRESET:
  1139. {
  1140. if (!perm)
  1141. goto eperm;
  1142. #ifdef BROKEN_GRAPHICS_PROGRAMS
  1143. /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
  1144. font is not saved. */
  1145. ret = -ENOSYS;
  1146. break;
  1147. #else
  1148. {
  1149. op.op = KD_FONT_OP_SET_DEFAULT;
  1150. op.data = NULL;
  1151. ret = con_font_op(vc_cons[fg_console].d, &op);
  1152. if (ret)
  1153. break;
  1154. con_set_default_unimap(vc_cons[fg_console].d);
  1155. break;
  1156. }
  1157. #endif
  1158. }
  1159. case KDFONTOP: {
  1160. if (copy_from_user(&op, up, sizeof(op))) {
  1161. ret = -EFAULT;
  1162. break;
  1163. }
  1164. if (!perm && op.op != KD_FONT_OP_GET)
  1165. goto eperm;
  1166. ret = con_font_op(vc, &op);
  1167. if (ret)
  1168. break;
  1169. if (copy_to_user(up, &op, sizeof(op)))
  1170. ret = -EFAULT;
  1171. break;
  1172. }
  1173. case PIO_SCRNMAP:
  1174. if (!perm)
  1175. ret = -EPERM;
  1176. else
  1177. ret = con_set_trans_old(up);
  1178. break;
  1179. case GIO_SCRNMAP:
  1180. ret = con_get_trans_old(up);
  1181. break;
  1182. case PIO_UNISCRNMAP:
  1183. if (!perm)
  1184. ret = -EPERM;
  1185. else
  1186. ret = con_set_trans_new(up);
  1187. break;
  1188. case GIO_UNISCRNMAP:
  1189. ret = con_get_trans_new(up);
  1190. break;
  1191. case PIO_UNIMAPCLR:
  1192. { struct unimapinit ui;
  1193. if (!perm)
  1194. goto eperm;
  1195. ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
  1196. if (ret)
  1197. ret = -EFAULT;
  1198. else
  1199. con_clear_unimap(vc, &ui);
  1200. break;
  1201. }
  1202. case PIO_UNIMAP:
  1203. case GIO_UNIMAP:
  1204. ret = do_unimap_ioctl(cmd, up, perm, vc);
  1205. break;
  1206. case VT_LOCKSWITCH:
  1207. if (!capable(CAP_SYS_TTY_CONFIG))
  1208. goto eperm;
  1209. vt_dont_switch = 1;
  1210. break;
  1211. case VT_UNLOCKSWITCH:
  1212. if (!capable(CAP_SYS_TTY_CONFIG))
  1213. goto eperm;
  1214. vt_dont_switch = 0;
  1215. break;
  1216. case VT_GETHIFONTMASK:
  1217. ret = put_user(vc->vc_hi_font_mask,
  1218. (unsigned short __user *)arg);
  1219. break;
  1220. case VT_WAITEVENT:
  1221. ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
  1222. break;
  1223. default:
  1224. ret = -ENOIOCTLCMD;
  1225. }
  1226. out:
  1227. tty_unlock();
  1228. return ret;
  1229. eperm:
  1230. ret = -EPERM;
  1231. goto out;
  1232. }
  1233. void reset_vc(struct vc_data *vc)
  1234. {
  1235. vc->vc_mode = KD_TEXT;
  1236. kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
  1237. vc->vt_mode.mode = VT_AUTO;
  1238. vc->vt_mode.waitv = 0;
  1239. vc->vt_mode.relsig = 0;
  1240. vc->vt_mode.acqsig = 0;
  1241. vc->vt_mode.frsig = 0;
  1242. put_pid(vc->vt_pid);
  1243. vc->vt_pid = NULL;
  1244. vc->vt_newvt = -1;
  1245. if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
  1246. reset_palette(vc);
  1247. }
  1248. void vc_SAK(struct work_struct *work)
  1249. {
  1250. struct vc *vc_con =
  1251. container_of(work, struct vc, SAK_work);
  1252. struct vc_data *vc;
  1253. struct tty_struct *tty;
  1254. console_lock();
  1255. vc = vc_con->d;
  1256. if (vc) {
  1257. tty = vc->port.tty;
  1258. /*
  1259. * SAK should also work in all raw modes and reset
  1260. * them properly.
  1261. */
  1262. if (tty)
  1263. __do_SAK(tty);
  1264. reset_vc(vc);
  1265. }
  1266. console_unlock();
  1267. }
  1268. #ifdef CONFIG_COMPAT
  1269. struct compat_consolefontdesc {
  1270. unsigned short charcount; /* characters in font (256 or 512) */
  1271. unsigned short charheight; /* scan lines per character (1-32) */
  1272. compat_caddr_t chardata; /* font data in expanded form */
  1273. };
  1274. static inline int
  1275. compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
  1276. int perm, struct console_font_op *op)
  1277. {
  1278. struct compat_consolefontdesc cfdarg;
  1279. int i;
  1280. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
  1281. return -EFAULT;
  1282. switch (cmd) {
  1283. case PIO_FONTX:
  1284. if (!perm)
  1285. return -EPERM;
  1286. op->op = KD_FONT_OP_SET;
  1287. op->flags = KD_FONT_FLAG_OLD;
  1288. op->width = 8;
  1289. op->height = cfdarg.charheight;
  1290. op->charcount = cfdarg.charcount;
  1291. op->data = compat_ptr(cfdarg.chardata);
  1292. return con_font_op(vc_cons[fg_console].d, op);
  1293. case GIO_FONTX:
  1294. op->op = KD_FONT_OP_GET;
  1295. op->flags = KD_FONT_FLAG_OLD;
  1296. op->width = 8;
  1297. op->height = cfdarg.charheight;
  1298. op->charcount = cfdarg.charcount;
  1299. op->data = compat_ptr(cfdarg.chardata);
  1300. i = con_font_op(vc_cons[fg_console].d, op);
  1301. if (i)
  1302. return i;
  1303. cfdarg.charheight = op->height;
  1304. cfdarg.charcount = op->charcount;
  1305. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
  1306. return -EFAULT;
  1307. return 0;
  1308. }
  1309. return -EINVAL;
  1310. }
  1311. struct compat_console_font_op {
  1312. compat_uint_t op; /* operation code KD_FONT_OP_* */
  1313. compat_uint_t flags; /* KD_FONT_FLAG_* */
  1314. compat_uint_t width, height; /* font size */
  1315. compat_uint_t charcount;
  1316. compat_caddr_t data; /* font data with height fixed to 32 */
  1317. };
  1318. static inline int
  1319. compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
  1320. int perm, struct console_font_op *op, struct vc_data *vc)
  1321. {
  1322. int i;
  1323. if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
  1324. return -EFAULT;
  1325. if (!perm && op->op != KD_FONT_OP_GET)
  1326. return -EPERM;
  1327. op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
  1328. op->flags |= KD_FONT_FLAG_OLD;
  1329. i = con_font_op(vc, op);
  1330. if (i)
  1331. return i;
  1332. ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
  1333. if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
  1334. return -EFAULT;
  1335. return 0;
  1336. }
  1337. struct compat_unimapdesc {
  1338. unsigned short entry_ct;
  1339. compat_caddr_t entries;
  1340. };
  1341. static inline int
  1342. compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
  1343. int perm, struct vc_data *vc)
  1344. {
  1345. struct compat_unimapdesc tmp;
  1346. struct unipair __user *tmp_entries;
  1347. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  1348. return -EFAULT;
  1349. tmp_entries = compat_ptr(tmp.entries);
  1350. if (tmp_entries)
  1351. if (!access_ok(VERIFY_WRITE, tmp_entries,
  1352. tmp.entry_ct*sizeof(struct unipair)))
  1353. return -EFAULT;
  1354. switch (cmd) {
  1355. case PIO_UNIMAP:
  1356. if (!perm)
  1357. return -EPERM;
  1358. return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
  1359. case GIO_UNIMAP:
  1360. if (!perm && fg_console != vc->vc_num)
  1361. return -EPERM;
  1362. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
  1363. }
  1364. return 0;
  1365. }
  1366. long vt_compat_ioctl(struct tty_struct *tty,
  1367. unsigned int cmd, unsigned long arg)
  1368. {
  1369. struct vc_data *vc = tty->driver_data;
  1370. struct console_font_op op; /* used in multiple places here */
  1371. unsigned int console;
  1372. void __user *up = (void __user *)arg;
  1373. int perm;
  1374. int ret = 0;
  1375. console = vc->vc_num;
  1376. tty_lock();
  1377. if (!vc_cons_allocated(console)) { /* impossible? */
  1378. ret = -ENOIOCTLCMD;
  1379. goto out;
  1380. }
  1381. /*
  1382. * To have permissions to do most of the vt ioctls, we either have
  1383. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  1384. */
  1385. perm = 0;
  1386. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  1387. perm = 1;
  1388. switch (cmd) {
  1389. /*
  1390. * these need special handlers for incompatible data structures
  1391. */
  1392. case PIO_FONTX:
  1393. case GIO_FONTX:
  1394. ret = compat_fontx_ioctl(cmd, up, perm, &op);
  1395. break;
  1396. case KDFONTOP:
  1397. ret = compat_kdfontop_ioctl(up, perm, &op, vc);
  1398. break;
  1399. case PIO_UNIMAP:
  1400. case GIO_UNIMAP:
  1401. ret = compat_unimap_ioctl(cmd, up, perm, vc);
  1402. break;
  1403. /*
  1404. * all these treat 'arg' as an integer
  1405. */
  1406. case KIOCSOUND:
  1407. case KDMKTONE:
  1408. #ifdef CONFIG_X86
  1409. case KDADDIO:
  1410. case KDDELIO:
  1411. #endif
  1412. case KDSETMODE:
  1413. case KDMAPDISP:
  1414. case KDUNMAPDISP:
  1415. case KDSKBMODE:
  1416. case KDSKBMETA:
  1417. case KDSKBLED:
  1418. case KDSETLED:
  1419. case KDSIGACCEPT:
  1420. case VT_ACTIVATE:
  1421. case VT_WAITACTIVE:
  1422. case VT_RELDISP:
  1423. case VT_DISALLOCATE:
  1424. case VT_RESIZE:
  1425. case VT_RESIZEX:
  1426. goto fallback;
  1427. /*
  1428. * the rest has a compatible data structure behind arg,
  1429. * but we have to convert it to a proper 64 bit pointer.
  1430. */
  1431. default:
  1432. arg = (unsigned long)compat_ptr(arg);
  1433. goto fallback;
  1434. }
  1435. out:
  1436. tty_unlock();
  1437. return ret;
  1438. fallback:
  1439. tty_unlock();
  1440. return vt_ioctl(tty, cmd, arg);
  1441. }
  1442. #endif /* CONFIG_COMPAT */
  1443. /*
  1444. * Performs the back end of a vt switch. Called under the console
  1445. * semaphore.
  1446. */
  1447. static void complete_change_console(struct vc_data *vc)
  1448. {
  1449. unsigned char old_vc_mode;
  1450. int old = fg_console;
  1451. last_console = fg_console;
  1452. /*
  1453. * If we're switching, we could be going from KD_GRAPHICS to
  1454. * KD_TEXT mode or vice versa, which means we need to blank or
  1455. * unblank the screen later.
  1456. */
  1457. old_vc_mode = vc_cons[fg_console].d->vc_mode;
  1458. switch_screen(vc);
  1459. /*
  1460. * This can't appear below a successful kill_pid(). If it did,
  1461. * then the *blank_screen operation could occur while X, having
  1462. * received acqsig, is waking up on another processor. This
  1463. * condition can lead to overlapping accesses to the VGA range
  1464. * and the framebuffer (causing system lockups).
  1465. *
  1466. * To account for this we duplicate this code below only if the
  1467. * controlling process is gone and we've called reset_vc.
  1468. */
  1469. if (old_vc_mode != vc->vc_mode) {
  1470. if (vc->vc_mode == KD_TEXT)
  1471. do_unblank_screen(1);
  1472. else
  1473. do_blank_screen(1);
  1474. }
  1475. /*
  1476. * If this new console is under process control, send it a signal
  1477. * telling it that it has acquired. Also check if it has died and
  1478. * clean up (similar to logic employed in change_console())
  1479. */
  1480. if (vc->vt_mode.mode == VT_PROCESS) {
  1481. /*
  1482. * Send the signal as privileged - kill_pid() will
  1483. * tell us if the process has gone or something else
  1484. * is awry
  1485. */
  1486. if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
  1487. /*
  1488. * The controlling process has died, so we revert back to
  1489. * normal operation. In this case, we'll also change back
  1490. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1491. * but it saves the agony when the X server dies and the screen
  1492. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1493. * this outside of VT_PROCESS but there is no single process
  1494. * to account for and tracking tty count may be undesirable.
  1495. */
  1496. reset_vc(vc);
  1497. if (old_vc_mode != vc->vc_mode) {
  1498. if (vc->vc_mode == KD_TEXT)
  1499. do_unblank_screen(1);
  1500. else
  1501. do_blank_screen(1);
  1502. }
  1503. }
  1504. }
  1505. /*
  1506. * Wake anyone waiting for their VT to activate
  1507. */
  1508. vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
  1509. return;
  1510. }
  1511. /*
  1512. * Performs the front-end of a vt switch
  1513. */
  1514. void change_console(struct vc_data *new_vc)
  1515. {
  1516. struct vc_data *vc;
  1517. if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
  1518. return;
  1519. /*
  1520. * If this vt is in process mode, then we need to handshake with
  1521. * that process before switching. Essentially, we store where that
  1522. * vt wants to switch to and wait for it to tell us when it's done
  1523. * (via VT_RELDISP ioctl).
  1524. *
  1525. * We also check to see if the controlling process still exists.
  1526. * If it doesn't, we reset this vt to auto mode and continue.
  1527. * This is a cheap way to track process control. The worst thing
  1528. * that can happen is: we send a signal to a process, it dies, and
  1529. * the switch gets "lost" waiting for a response; hopefully, the
  1530. * user will try again, we'll detect the process is gone (unless
  1531. * the user waits just the right amount of time :-) and revert the
  1532. * vt to auto control.
  1533. */
  1534. vc = vc_cons[fg_console].d;
  1535. if (vc->vt_mode.mode == VT_PROCESS) {
  1536. /*
  1537. * Send the signal as privileged - kill_pid() will
  1538. * tell us if the process has gone or something else
  1539. * is awry.
  1540. *
  1541. * We need to set vt_newvt *before* sending the signal or we
  1542. * have a race.
  1543. */
  1544. vc->vt_newvt = new_vc->vc_num;
  1545. if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
  1546. /*
  1547. * It worked. Mark the vt to switch to and
  1548. * return. The process needs to send us a
  1549. * VT_RELDISP ioctl to complete the switch.
  1550. */
  1551. return;
  1552. }
  1553. /*
  1554. * The controlling process has died, so we revert back to
  1555. * normal operation. In this case, we'll also change back
  1556. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1557. * but it saves the agony when the X server dies and the screen
  1558. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1559. * this outside of VT_PROCESS but there is no single process
  1560. * to account for and tracking tty count may be undesirable.
  1561. */
  1562. reset_vc(vc);
  1563. /*
  1564. * Fall through to normal (VT_AUTO) handling of the switch...
  1565. */
  1566. }
  1567. /*
  1568. * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
  1569. */
  1570. if (vc->vc_mode == KD_GRAPHICS)
  1571. return;
  1572. complete_change_console(new_vc);
  1573. }
  1574. /* Perform a kernel triggered VT switch for suspend/resume */
  1575. static int disable_vt_switch;
  1576. int vt_move_to_console(unsigned int vt, int alloc)
  1577. {
  1578. int prev;
  1579. console_lock();
  1580. /* Graphics mode - up to X */
  1581. if (disable_vt_switch) {
  1582. console_unlock();
  1583. return 0;
  1584. }
  1585. prev = fg_console;
  1586. if (alloc && vc_allocate(vt)) {
  1587. /* we can't have a free VC for now. Too bad,
  1588. * we don't want to mess the screen for now. */
  1589. console_unlock();
  1590. return -ENOSPC;
  1591. }
  1592. if (set_console(vt)) {
  1593. /*
  1594. * We're unable to switch to the SUSPEND_CONSOLE.
  1595. * Let the calling function know so it can decide
  1596. * what to do.
  1597. */
  1598. console_unlock();
  1599. return -EIO;
  1600. }
  1601. console_unlock();
  1602. tty_lock();
  1603. if (vt_waitactive(vt + 1)) {
  1604. pr_debug("Suspend: Can't switch VCs.");
  1605. tty_unlock();
  1606. return -EINTR;
  1607. }
  1608. tty_unlock();
  1609. return prev;
  1610. }
  1611. /*
  1612. * Normally during a suspend, we allocate a new console and switch to it.
  1613. * When we resume, we switch back to the original console. This switch
  1614. * can be slow, so on systems where the framebuffer can handle restoration
  1615. * of video registers anyways, there's little point in doing the console
  1616. * switch. This function allows you to disable it by passing it '0'.
  1617. */
  1618. void pm_set_vt_switch(int do_switch)
  1619. {
  1620. console_lock();
  1621. disable_vt_switch = !do_switch;
  1622. console_unlock();
  1623. }
  1624. EXPORT_SYMBOL(pm_set_vt_switch);