vt_ioctl.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1992 obz under the linux copyright
  4. *
  5. * Dynamic diacritical handling - [email protected] - Dec 1993
  6. * Dynamic keymap and string allocation - [email protected] - May 1994
  7. * Restrict VT switching via ioctl() - [email protected] - Dec 1995
  8. * Some code moved for less code duplication - Andi Kleen - Mar 1997
  9. * Check put/get_user, cleanups - [email protected] - Jun 2001
  10. */
  11. #include <linux/types.h>
  12. #include <linux/errno.h>
  13. #include <linux/sched/signal.h>
  14. #include <linux/tty.h>
  15. #include <linux/timer.h>
  16. #include <linux/kernel.h>
  17. #include <linux/compat.h>
  18. #include <linux/module.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 <linux/consolemap.h>
  27. #include <linux/signal.h>
  28. #include <linux/suspend.h>
  29. #include <linux/timex.h>
  30. #include <asm/io.h>
  31. #include <linux/uaccess.h>
  32. #include <linux/nospec.h>
  33. #include <linux/kbd_kern.h>
  34. #include <linux/vt_kern.h>
  35. #include <linux/kbd_diacr.h>
  36. #include <linux/selection.h>
  37. bool vt_dont_switch;
  38. static inline bool vt_in_use(unsigned int i)
  39. {
  40. const struct vc_data *vc = vc_cons[i].d;
  41. /*
  42. * console_lock must be held to prevent the vc from being deallocated
  43. * while we're checking whether it's in-use.
  44. */
  45. WARN_CONSOLE_UNLOCKED();
  46. return vc && kref_read(&vc->port.kref) > 1;
  47. }
  48. static inline bool vt_busy(int i)
  49. {
  50. if (vt_in_use(i))
  51. return true;
  52. if (i == fg_console)
  53. return true;
  54. if (vc_is_sel(vc_cons[i].d))
  55. return true;
  56. return false;
  57. }
  58. /*
  59. * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
  60. * experimentation and study of X386 SYSV handling.
  61. *
  62. * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
  63. * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
  64. * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
  65. * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
  66. * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
  67. * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
  68. * to the current console is done by the main ioctl code.
  69. */
  70. #ifdef CONFIG_X86
  71. #include <asm/syscalls.h>
  72. #endif
  73. static void complete_change_console(struct vc_data *vc);
  74. /*
  75. * User space VT_EVENT handlers
  76. */
  77. struct vt_event_wait {
  78. struct list_head list;
  79. struct vt_event event;
  80. int done;
  81. };
  82. static LIST_HEAD(vt_events);
  83. static DEFINE_SPINLOCK(vt_event_lock);
  84. static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
  85. /**
  86. * vt_event_post
  87. * @event: the event that occurred
  88. * @old: old console
  89. * @new: new console
  90. *
  91. * Post an VT event to interested VT handlers
  92. */
  93. void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
  94. {
  95. struct list_head *pos, *head;
  96. unsigned long flags;
  97. int wake = 0;
  98. spin_lock_irqsave(&vt_event_lock, flags);
  99. head = &vt_events;
  100. list_for_each(pos, head) {
  101. struct vt_event_wait *ve = list_entry(pos,
  102. struct vt_event_wait, list);
  103. if (!(ve->event.event & event))
  104. continue;
  105. ve->event.event = event;
  106. /* kernel view is consoles 0..n-1, user space view is
  107. console 1..n with 0 meaning current, so we must bias */
  108. ve->event.oldev = old + 1;
  109. ve->event.newev = new + 1;
  110. wake = 1;
  111. ve->done = 1;
  112. }
  113. spin_unlock_irqrestore(&vt_event_lock, flags);
  114. if (wake)
  115. wake_up_interruptible(&vt_event_waitqueue);
  116. }
  117. static void __vt_event_queue(struct vt_event_wait *vw)
  118. {
  119. unsigned long flags;
  120. /* Prepare the event */
  121. INIT_LIST_HEAD(&vw->list);
  122. vw->done = 0;
  123. /* Queue our event */
  124. spin_lock_irqsave(&vt_event_lock, flags);
  125. list_add(&vw->list, &vt_events);
  126. spin_unlock_irqrestore(&vt_event_lock, flags);
  127. }
  128. static void __vt_event_wait(struct vt_event_wait *vw)
  129. {
  130. /* Wait for it to pass */
  131. wait_event_interruptible(vt_event_waitqueue, vw->done);
  132. }
  133. static void __vt_event_dequeue(struct vt_event_wait *vw)
  134. {
  135. unsigned long flags;
  136. /* Dequeue it */
  137. spin_lock_irqsave(&vt_event_lock, flags);
  138. list_del(&vw->list);
  139. spin_unlock_irqrestore(&vt_event_lock, flags);
  140. }
  141. /**
  142. * vt_event_wait - wait for an event
  143. * @vw: our event
  144. *
  145. * Waits for an event to occur which completes our vt_event_wait
  146. * structure. On return the structure has wv->done set to 1 for success
  147. * or 0 if some event such as a signal ended the wait.
  148. */
  149. static void vt_event_wait(struct vt_event_wait *vw)
  150. {
  151. __vt_event_queue(vw);
  152. __vt_event_wait(vw);
  153. __vt_event_dequeue(vw);
  154. }
  155. /**
  156. * vt_event_wait_ioctl - event ioctl handler
  157. * @event: argument to ioctl (the event)
  158. *
  159. * Implement the VT_WAITEVENT ioctl using the VT event interface
  160. */
  161. static int vt_event_wait_ioctl(struct vt_event __user *event)
  162. {
  163. struct vt_event_wait vw;
  164. if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
  165. return -EFAULT;
  166. /* Highest supported event for now */
  167. if (vw.event.event & ~VT_MAX_EVENT)
  168. return -EINVAL;
  169. vt_event_wait(&vw);
  170. /* If it occurred report it */
  171. if (vw.done) {
  172. if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
  173. return -EFAULT;
  174. return 0;
  175. }
  176. return -EINTR;
  177. }
  178. /**
  179. * vt_waitactive - active console wait
  180. * @n: new console
  181. *
  182. * Helper for event waits. Used to implement the legacy
  183. * event waiting ioctls in terms of events
  184. */
  185. int vt_waitactive(int n)
  186. {
  187. struct vt_event_wait vw;
  188. do {
  189. vw.event.event = VT_EVENT_SWITCH;
  190. __vt_event_queue(&vw);
  191. if (n == fg_console + 1) {
  192. __vt_event_dequeue(&vw);
  193. break;
  194. }
  195. __vt_event_wait(&vw);
  196. __vt_event_dequeue(&vw);
  197. if (vw.done == 0)
  198. return -EINTR;
  199. } while (vw.event.newev != n);
  200. return 0;
  201. }
  202. /*
  203. * these are the valid i/o ports we're allowed to change. they map all the
  204. * video ports
  205. */
  206. #define GPFIRST 0x3b4
  207. #define GPLAST 0x3df
  208. #define GPNUM (GPLAST - GPFIRST + 1)
  209. /*
  210. * currently, setting the mode from KD_TEXT to KD_GRAPHICS doesn't do a whole
  211. * lot. i'm not sure if it should do any restoration of modes or what...
  212. *
  213. * XXX It should at least call into the driver, fbdev's definitely need to
  214. * restore their engine state. --BenH
  215. *
  216. * Called with the console lock held.
  217. */
  218. static int vt_kdsetmode(struct vc_data *vc, unsigned long mode)
  219. {
  220. switch (mode) {
  221. case KD_GRAPHICS:
  222. break;
  223. case KD_TEXT0:
  224. case KD_TEXT1:
  225. mode = KD_TEXT;
  226. fallthrough;
  227. case KD_TEXT:
  228. break;
  229. default:
  230. return -EINVAL;
  231. }
  232. if (vc->vc_mode == mode)
  233. return 0;
  234. vc->vc_mode = mode;
  235. if (vc->vc_num != fg_console)
  236. return 0;
  237. /* explicitly blank/unblank the screen if switching modes */
  238. if (mode == KD_TEXT)
  239. do_unblank_screen(1);
  240. else
  241. do_blank_screen(1);
  242. return 0;
  243. }
  244. static int vt_k_ioctl(struct tty_struct *tty, unsigned int cmd,
  245. unsigned long arg, bool perm)
  246. {
  247. struct vc_data *vc = tty->driver_data;
  248. void __user *up = (void __user *)arg;
  249. unsigned int console = vc->vc_num;
  250. int ret;
  251. switch (cmd) {
  252. case KIOCSOUND:
  253. if (!perm)
  254. return -EPERM;
  255. /*
  256. * The use of PIT_TICK_RATE is historic, it used to be
  257. * the platform-dependent CLOCK_TICK_RATE between 2.6.12
  258. * and 2.6.36, which was a minor but unfortunate ABI
  259. * change. kd_mksound is locked by the input layer.
  260. */
  261. if (arg)
  262. arg = PIT_TICK_RATE / arg;
  263. kd_mksound(arg, 0);
  264. break;
  265. case KDMKTONE:
  266. if (!perm)
  267. return -EPERM;
  268. {
  269. unsigned int ticks, count;
  270. /*
  271. * Generate the tone for the appropriate number of ticks.
  272. * If the time is zero, turn off sound ourselves.
  273. */
  274. ticks = msecs_to_jiffies((arg >> 16) & 0xffff);
  275. count = ticks ? (arg & 0xffff) : 0;
  276. if (count)
  277. count = PIT_TICK_RATE / count;
  278. kd_mksound(count, ticks);
  279. break;
  280. }
  281. case KDGKBTYPE:
  282. /*
  283. * this is naïve.
  284. */
  285. return put_user(KB_101, (char __user *)arg);
  286. /*
  287. * These cannot be implemented on any machine that implements
  288. * ioperm() in user level (such as Alpha PCs) or not at all.
  289. *
  290. * XXX: you should never use these, just call ioperm directly..
  291. */
  292. #ifdef CONFIG_X86
  293. case KDADDIO:
  294. case KDDELIO:
  295. /*
  296. * KDADDIO and KDDELIO may be able to add ports beyond what
  297. * we reject here, but to be safe...
  298. *
  299. * These are locked internally via sys_ioperm
  300. */
  301. if (arg < GPFIRST || arg > GPLAST)
  302. return -EINVAL;
  303. return ksys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
  304. case KDENABIO:
  305. case KDDISABIO:
  306. return ksys_ioperm(GPFIRST, GPNUM,
  307. (cmd == KDENABIO)) ? -ENXIO : 0;
  308. #endif
  309. /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
  310. case KDKBDREP:
  311. {
  312. struct kbd_repeat kbrep;
  313. if (!capable(CAP_SYS_TTY_CONFIG))
  314. return -EPERM;
  315. if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat)))
  316. return -EFAULT;
  317. ret = kbd_rate(&kbrep);
  318. if (ret)
  319. return ret;
  320. if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
  321. return -EFAULT;
  322. break;
  323. }
  324. case KDSETMODE:
  325. if (!perm)
  326. return -EPERM;
  327. console_lock();
  328. ret = vt_kdsetmode(vc, arg);
  329. console_unlock();
  330. return ret;
  331. case KDGETMODE:
  332. return put_user(vc->vc_mode, (int __user *)arg);
  333. case KDMAPDISP:
  334. case KDUNMAPDISP:
  335. /*
  336. * these work like a combination of mmap and KDENABIO.
  337. * this could be easily finished.
  338. */
  339. return -EINVAL;
  340. case KDSKBMODE:
  341. if (!perm)
  342. return -EPERM;
  343. ret = vt_do_kdskbmode(console, arg);
  344. if (ret)
  345. return ret;
  346. tty_ldisc_flush(tty);
  347. break;
  348. case KDGKBMODE:
  349. return put_user(vt_do_kdgkbmode(console), (int __user *)arg);
  350. /* this could be folded into KDSKBMODE, but for compatibility
  351. reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
  352. case KDSKBMETA:
  353. return vt_do_kdskbmeta(console, arg);
  354. case KDGKBMETA:
  355. /* FIXME: should review whether this is worth locking */
  356. return put_user(vt_do_kdgkbmeta(console), (int __user *)arg);
  357. case KDGETKEYCODE:
  358. case KDSETKEYCODE:
  359. if(!capable(CAP_SYS_TTY_CONFIG))
  360. perm = 0;
  361. return vt_do_kbkeycode_ioctl(cmd, up, perm);
  362. case KDGKBENT:
  363. case KDSKBENT:
  364. return vt_do_kdsk_ioctl(cmd, up, perm, console);
  365. case KDGKBSENT:
  366. case KDSKBSENT:
  367. return vt_do_kdgkb_ioctl(cmd, up, perm);
  368. /* Diacritical processing. Handled in keyboard.c as it has
  369. to operate on the keyboard locks and structures */
  370. case KDGKBDIACR:
  371. case KDGKBDIACRUC:
  372. case KDSKBDIACR:
  373. case KDSKBDIACRUC:
  374. return vt_do_diacrit(cmd, up, perm);
  375. /* the ioctls below read/set the flags usually shown in the leds */
  376. /* don't use them - they will go away without warning */
  377. case KDGKBLED:
  378. case KDSKBLED:
  379. case KDGETLED:
  380. case KDSETLED:
  381. return vt_do_kdskled(console, cmd, arg, perm);
  382. /*
  383. * A process can indicate its willingness to accept signals
  384. * generated by pressing an appropriate key combination.
  385. * Thus, one can have a daemon that e.g. spawns a new console
  386. * upon a keypress and then changes to it.
  387. * See also the kbrequest field of inittab(5).
  388. */
  389. case KDSIGACCEPT:
  390. if (!perm || !capable(CAP_KILL))
  391. return -EPERM;
  392. if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
  393. return -EINVAL;
  394. spin_lock_irq(&vt_spawn_con.lock);
  395. put_pid(vt_spawn_con.pid);
  396. vt_spawn_con.pid = get_pid(task_pid(current));
  397. vt_spawn_con.sig = arg;
  398. spin_unlock_irq(&vt_spawn_con.lock);
  399. break;
  400. case KDFONTOP: {
  401. struct console_font_op op;
  402. if (copy_from_user(&op, up, sizeof(op)))
  403. return -EFAULT;
  404. if (!perm && op.op != KD_FONT_OP_GET)
  405. return -EPERM;
  406. ret = con_font_op(vc, &op);
  407. if (ret)
  408. return ret;
  409. if (copy_to_user(up, &op, sizeof(op)))
  410. return -EFAULT;
  411. break;
  412. }
  413. default:
  414. return -ENOIOCTLCMD;
  415. }
  416. return 0;
  417. }
  418. static inline int do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud,
  419. bool perm, struct vc_data *vc)
  420. {
  421. struct unimapdesc tmp;
  422. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  423. return -EFAULT;
  424. switch (cmd) {
  425. case PIO_UNIMAP:
  426. if (!perm)
  427. return -EPERM;
  428. return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
  429. case GIO_UNIMAP:
  430. if (!perm && fg_console != vc->vc_num)
  431. return -EPERM;
  432. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct),
  433. tmp.entries);
  434. }
  435. return 0;
  436. }
  437. static int vt_io_ioctl(struct vc_data *vc, unsigned int cmd, void __user *up,
  438. bool perm)
  439. {
  440. switch (cmd) {
  441. case PIO_CMAP:
  442. if (!perm)
  443. return -EPERM;
  444. return con_set_cmap(up);
  445. case GIO_CMAP:
  446. return con_get_cmap(up);
  447. case PIO_SCRNMAP:
  448. if (!perm)
  449. return -EPERM;
  450. return con_set_trans_old(up);
  451. case GIO_SCRNMAP:
  452. return con_get_trans_old(up);
  453. case PIO_UNISCRNMAP:
  454. if (!perm)
  455. return -EPERM;
  456. return con_set_trans_new(up);
  457. case GIO_UNISCRNMAP:
  458. return con_get_trans_new(up);
  459. case PIO_UNIMAPCLR:
  460. if (!perm)
  461. return -EPERM;
  462. con_clear_unimap(vc);
  463. break;
  464. case PIO_UNIMAP:
  465. case GIO_UNIMAP:
  466. return do_unimap_ioctl(cmd, up, perm, vc);
  467. default:
  468. return -ENOIOCTLCMD;
  469. }
  470. return 0;
  471. }
  472. static int vt_reldisp(struct vc_data *vc, unsigned int swtch)
  473. {
  474. int newvt, ret;
  475. if (vc->vt_mode.mode != VT_PROCESS)
  476. return -EINVAL;
  477. /* Switched-to response */
  478. if (vc->vt_newvt < 0) {
  479. /* If it's just an ACK, ignore it */
  480. return swtch == VT_ACKACQ ? 0 : -EINVAL;
  481. }
  482. /* Switching-from response */
  483. if (swtch == 0) {
  484. /* Switch disallowed, so forget we were trying to do it. */
  485. vc->vt_newvt = -1;
  486. return 0;
  487. }
  488. /* The current vt has been released, so complete the switch. */
  489. newvt = vc->vt_newvt;
  490. vc->vt_newvt = -1;
  491. ret = vc_allocate(newvt);
  492. if (ret)
  493. return ret;
  494. /*
  495. * When we actually do the console switch, make sure we are atomic with
  496. * respect to other console switches..
  497. */
  498. complete_change_console(vc_cons[newvt].d);
  499. return 0;
  500. }
  501. static int vt_setactivate(struct vt_setactivate __user *sa)
  502. {
  503. struct vt_setactivate vsa;
  504. struct vc_data *nvc;
  505. int ret;
  506. if (copy_from_user(&vsa, sa, sizeof(vsa)))
  507. return -EFAULT;
  508. if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
  509. return -ENXIO;
  510. vsa.console--;
  511. vsa.console = array_index_nospec(vsa.console, MAX_NR_CONSOLES);
  512. console_lock();
  513. ret = vc_allocate(vsa.console);
  514. if (ret) {
  515. console_unlock();
  516. return ret;
  517. }
  518. /*
  519. * This is safe providing we don't drop the console sem between
  520. * vc_allocate and finishing referencing nvc.
  521. */
  522. nvc = vc_cons[vsa.console].d;
  523. nvc->vt_mode = vsa.mode;
  524. nvc->vt_mode.frsig = 0;
  525. put_pid(nvc->vt_pid);
  526. nvc->vt_pid = get_pid(task_pid(current));
  527. console_unlock();
  528. /* Commence switch and lock */
  529. /* Review set_console locks */
  530. set_console(vsa.console);
  531. return 0;
  532. }
  533. /* deallocate a single console, if possible (leave 0) */
  534. static int vt_disallocate(unsigned int vc_num)
  535. {
  536. struct vc_data *vc = NULL;
  537. int ret = 0;
  538. console_lock();
  539. if (vt_busy(vc_num))
  540. ret = -EBUSY;
  541. else if (vc_num)
  542. vc = vc_deallocate(vc_num);
  543. console_unlock();
  544. if (vc && vc_num >= MIN_NR_CONSOLES)
  545. tty_port_put(&vc->port);
  546. return ret;
  547. }
  548. /* deallocate all unused consoles, but leave 0 */
  549. static void vt_disallocate_all(void)
  550. {
  551. struct vc_data *vc[MAX_NR_CONSOLES];
  552. int i;
  553. console_lock();
  554. for (i = 1; i < MAX_NR_CONSOLES; i++)
  555. if (!vt_busy(i))
  556. vc[i] = vc_deallocate(i);
  557. else
  558. vc[i] = NULL;
  559. console_unlock();
  560. for (i = 1; i < MAX_NR_CONSOLES; i++) {
  561. if (vc[i] && i >= MIN_NR_CONSOLES)
  562. tty_port_put(&vc[i]->port);
  563. }
  564. }
  565. static int vt_resizex(struct vc_data *vc, struct vt_consize __user *cs)
  566. {
  567. struct vt_consize v;
  568. int i;
  569. if (copy_from_user(&v, cs, sizeof(struct vt_consize)))
  570. return -EFAULT;
  571. /* FIXME: Should check the copies properly */
  572. if (!v.v_vlin)
  573. v.v_vlin = vc->vc_scan_lines;
  574. if (v.v_clin) {
  575. int rows = v.v_vlin / v.v_clin;
  576. if (v.v_rows != rows) {
  577. if (v.v_rows) /* Parameters don't add up */
  578. return -EINVAL;
  579. v.v_rows = rows;
  580. }
  581. }
  582. if (v.v_vcol && v.v_ccol) {
  583. int cols = v.v_vcol / v.v_ccol;
  584. if (v.v_cols != cols) {
  585. if (v.v_cols)
  586. return -EINVAL;
  587. v.v_cols = cols;
  588. }
  589. }
  590. if (v.v_clin > 32)
  591. return -EINVAL;
  592. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  593. struct vc_data *vcp;
  594. if (!vc_cons[i].d)
  595. continue;
  596. console_lock();
  597. vcp = vc_cons[i].d;
  598. if (vcp) {
  599. int ret;
  600. int save_scan_lines = vcp->vc_scan_lines;
  601. int save_cell_height = vcp->vc_cell_height;
  602. if (v.v_vlin)
  603. vcp->vc_scan_lines = v.v_vlin;
  604. if (v.v_clin)
  605. vcp->vc_cell_height = v.v_clin;
  606. vcp->vc_resize_user = 1;
  607. ret = vc_resize(vcp, v.v_cols, v.v_rows);
  608. if (ret) {
  609. vcp->vc_scan_lines = save_scan_lines;
  610. vcp->vc_cell_height = save_cell_height;
  611. console_unlock();
  612. return ret;
  613. }
  614. }
  615. console_unlock();
  616. }
  617. return 0;
  618. }
  619. /*
  620. * We handle the console-specific ioctl's here. We allow the
  621. * capability to modify any console, not just the fg_console.
  622. */
  623. int vt_ioctl(struct tty_struct *tty,
  624. unsigned int cmd, unsigned long arg)
  625. {
  626. struct vc_data *vc = tty->driver_data;
  627. void __user *up = (void __user *)arg;
  628. int i, perm;
  629. int ret;
  630. /*
  631. * To have permissions to do most of the vt ioctls, we either have
  632. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  633. */
  634. perm = 0;
  635. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  636. perm = 1;
  637. ret = vt_k_ioctl(tty, cmd, arg, perm);
  638. if (ret != -ENOIOCTLCMD)
  639. return ret;
  640. ret = vt_io_ioctl(vc, cmd, up, perm);
  641. if (ret != -ENOIOCTLCMD)
  642. return ret;
  643. switch (cmd) {
  644. case TIOCLINUX:
  645. return tioclinux(tty, arg);
  646. case VT_SETMODE:
  647. {
  648. struct vt_mode tmp;
  649. if (!perm)
  650. return -EPERM;
  651. if (copy_from_user(&tmp, up, sizeof(struct vt_mode)))
  652. return -EFAULT;
  653. if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)
  654. return -EINVAL;
  655. console_lock();
  656. vc->vt_mode = tmp;
  657. /* the frsig is ignored, so we set it to 0 */
  658. vc->vt_mode.frsig = 0;
  659. put_pid(vc->vt_pid);
  660. vc->vt_pid = get_pid(task_pid(current));
  661. /* no switch is required -- [email protected] */
  662. vc->vt_newvt = -1;
  663. console_unlock();
  664. break;
  665. }
  666. case VT_GETMODE:
  667. {
  668. struct vt_mode tmp;
  669. int rc;
  670. console_lock();
  671. memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
  672. console_unlock();
  673. rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
  674. if (rc)
  675. return -EFAULT;
  676. break;
  677. }
  678. /*
  679. * Returns global vt state. Note that VT 0 is always open, since
  680. * it's an alias for the current VT, and people can't use it here.
  681. * We cannot return state for more than 16 VTs, since v_state is short.
  682. */
  683. case VT_GETSTATE:
  684. {
  685. struct vt_stat __user *vtstat = up;
  686. unsigned short state, mask;
  687. if (put_user(fg_console + 1, &vtstat->v_active))
  688. return -EFAULT;
  689. state = 1; /* /dev/tty0 is always open */
  690. console_lock(); /* required by vt_in_use() */
  691. for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
  692. ++i, mask <<= 1)
  693. if (vt_in_use(i))
  694. state |= mask;
  695. console_unlock();
  696. return put_user(state, &vtstat->v_state);
  697. }
  698. /*
  699. * Returns the first available (non-opened) console.
  700. */
  701. case VT_OPENQRY:
  702. console_lock(); /* required by vt_in_use() */
  703. for (i = 0; i < MAX_NR_CONSOLES; ++i)
  704. if (!vt_in_use(i))
  705. break;
  706. console_unlock();
  707. i = i < MAX_NR_CONSOLES ? (i+1) : -1;
  708. return put_user(i, (int __user *)arg);
  709. /*
  710. * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
  711. * with num >= 1 (switches to vt 0, our console, are not allowed, just
  712. * to preserve sanity).
  713. */
  714. case VT_ACTIVATE:
  715. if (!perm)
  716. return -EPERM;
  717. if (arg == 0 || arg > MAX_NR_CONSOLES)
  718. return -ENXIO;
  719. arg--;
  720. arg = array_index_nospec(arg, MAX_NR_CONSOLES);
  721. console_lock();
  722. ret = vc_allocate(arg);
  723. console_unlock();
  724. if (ret)
  725. return ret;
  726. set_console(arg);
  727. break;
  728. case VT_SETACTIVATE:
  729. if (!perm)
  730. return -EPERM;
  731. return vt_setactivate(up);
  732. /*
  733. * wait until the specified VT has been activated
  734. */
  735. case VT_WAITACTIVE:
  736. if (!perm)
  737. return -EPERM;
  738. if (arg == 0 || arg > MAX_NR_CONSOLES)
  739. return -ENXIO;
  740. return vt_waitactive(arg);
  741. /*
  742. * If a vt is under process control, the kernel will not switch to it
  743. * immediately, but postpone the operation until the process calls this
  744. * ioctl, allowing the switch to complete.
  745. *
  746. * According to the X sources this is the behavior:
  747. * 0: pending switch-from not OK
  748. * 1: pending switch-from OK
  749. * 2: completed switch-to OK
  750. */
  751. case VT_RELDISP:
  752. if (!perm)
  753. return -EPERM;
  754. console_lock();
  755. ret = vt_reldisp(vc, arg);
  756. console_unlock();
  757. return ret;
  758. /*
  759. * Disallocate memory associated to VT (but leave VT1)
  760. */
  761. case VT_DISALLOCATE:
  762. if (arg > MAX_NR_CONSOLES)
  763. return -ENXIO;
  764. if (arg == 0) {
  765. vt_disallocate_all();
  766. break;
  767. }
  768. arg = array_index_nospec(arg - 1, MAX_NR_CONSOLES);
  769. return vt_disallocate(arg);
  770. case VT_RESIZE:
  771. {
  772. struct vt_sizes __user *vtsizes = up;
  773. struct vc_data *vc;
  774. ushort ll,cc;
  775. if (!perm)
  776. return -EPERM;
  777. if (get_user(ll, &vtsizes->v_rows) ||
  778. get_user(cc, &vtsizes->v_cols))
  779. return -EFAULT;
  780. console_lock();
  781. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  782. vc = vc_cons[i].d;
  783. if (vc) {
  784. vc->vc_resize_user = 1;
  785. /* FIXME: review v tty lock */
  786. vc_resize(vc_cons[i].d, cc, ll);
  787. }
  788. }
  789. console_unlock();
  790. break;
  791. }
  792. case VT_RESIZEX:
  793. if (!perm)
  794. return -EPERM;
  795. return vt_resizex(vc, up);
  796. case VT_LOCKSWITCH:
  797. if (!capable(CAP_SYS_TTY_CONFIG))
  798. return -EPERM;
  799. vt_dont_switch = true;
  800. break;
  801. case VT_UNLOCKSWITCH:
  802. if (!capable(CAP_SYS_TTY_CONFIG))
  803. return -EPERM;
  804. vt_dont_switch = false;
  805. break;
  806. case VT_GETHIFONTMASK:
  807. return put_user(vc->vc_hi_font_mask,
  808. (unsigned short __user *)arg);
  809. case VT_WAITEVENT:
  810. return vt_event_wait_ioctl((struct vt_event __user *)arg);
  811. default:
  812. return -ENOIOCTLCMD;
  813. }
  814. return 0;
  815. }
  816. void reset_vc(struct vc_data *vc)
  817. {
  818. vc->vc_mode = KD_TEXT;
  819. vt_reset_unicode(vc->vc_num);
  820. vc->vt_mode.mode = VT_AUTO;
  821. vc->vt_mode.waitv = 0;
  822. vc->vt_mode.relsig = 0;
  823. vc->vt_mode.acqsig = 0;
  824. vc->vt_mode.frsig = 0;
  825. put_pid(vc->vt_pid);
  826. vc->vt_pid = NULL;
  827. vc->vt_newvt = -1;
  828. reset_palette(vc);
  829. }
  830. void vc_SAK(struct work_struct *work)
  831. {
  832. struct vc *vc_con =
  833. container_of(work, struct vc, SAK_work);
  834. struct vc_data *vc;
  835. struct tty_struct *tty;
  836. console_lock();
  837. vc = vc_con->d;
  838. if (vc) {
  839. /* FIXME: review tty ref counting */
  840. tty = vc->port.tty;
  841. /*
  842. * SAK should also work in all raw modes and reset
  843. * them properly.
  844. */
  845. if (tty)
  846. __do_SAK(tty);
  847. reset_vc(vc);
  848. }
  849. console_unlock();
  850. }
  851. #ifdef CONFIG_COMPAT
  852. struct compat_console_font_op {
  853. compat_uint_t op; /* operation code KD_FONT_OP_* */
  854. compat_uint_t flags; /* KD_FONT_FLAG_* */
  855. compat_uint_t width, height; /* font size */
  856. compat_uint_t charcount;
  857. compat_caddr_t data; /* font data with height fixed to 32 */
  858. };
  859. static inline int
  860. compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
  861. int perm, struct console_font_op *op, struct vc_data *vc)
  862. {
  863. int i;
  864. if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
  865. return -EFAULT;
  866. if (!perm && op->op != KD_FONT_OP_GET)
  867. return -EPERM;
  868. op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
  869. i = con_font_op(vc, op);
  870. if (i)
  871. return i;
  872. ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
  873. if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
  874. return -EFAULT;
  875. return 0;
  876. }
  877. struct compat_unimapdesc {
  878. unsigned short entry_ct;
  879. compat_caddr_t entries;
  880. };
  881. static inline int
  882. compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
  883. int perm, struct vc_data *vc)
  884. {
  885. struct compat_unimapdesc tmp;
  886. struct unipair __user *tmp_entries;
  887. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  888. return -EFAULT;
  889. tmp_entries = compat_ptr(tmp.entries);
  890. switch (cmd) {
  891. case PIO_UNIMAP:
  892. if (!perm)
  893. return -EPERM;
  894. return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
  895. case GIO_UNIMAP:
  896. if (!perm && fg_console != vc->vc_num)
  897. return -EPERM;
  898. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
  899. }
  900. return 0;
  901. }
  902. long vt_compat_ioctl(struct tty_struct *tty,
  903. unsigned int cmd, unsigned long arg)
  904. {
  905. struct vc_data *vc = tty->driver_data;
  906. struct console_font_op op; /* used in multiple places here */
  907. void __user *up = compat_ptr(arg);
  908. int perm;
  909. /*
  910. * To have permissions to do most of the vt ioctls, we either have
  911. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  912. */
  913. perm = 0;
  914. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  915. perm = 1;
  916. switch (cmd) {
  917. /*
  918. * these need special handlers for incompatible data structures
  919. */
  920. case KDFONTOP:
  921. return compat_kdfontop_ioctl(up, perm, &op, vc);
  922. case PIO_UNIMAP:
  923. case GIO_UNIMAP:
  924. return compat_unimap_ioctl(cmd, up, perm, vc);
  925. /*
  926. * all these treat 'arg' as an integer
  927. */
  928. case KIOCSOUND:
  929. case KDMKTONE:
  930. #ifdef CONFIG_X86
  931. case KDADDIO:
  932. case KDDELIO:
  933. #endif
  934. case KDSETMODE:
  935. case KDMAPDISP:
  936. case KDUNMAPDISP:
  937. case KDSKBMODE:
  938. case KDSKBMETA:
  939. case KDSKBLED:
  940. case KDSETLED:
  941. case KDSIGACCEPT:
  942. case VT_ACTIVATE:
  943. case VT_WAITACTIVE:
  944. case VT_RELDISP:
  945. case VT_DISALLOCATE:
  946. case VT_RESIZE:
  947. case VT_RESIZEX:
  948. return vt_ioctl(tty, cmd, arg);
  949. /*
  950. * the rest has a compatible data structure behind arg,
  951. * but we have to convert it to a proper 64 bit pointer.
  952. */
  953. default:
  954. return vt_ioctl(tty, cmd, (unsigned long)up);
  955. }
  956. }
  957. #endif /* CONFIG_COMPAT */
  958. /*
  959. * Performs the back end of a vt switch. Called under the console
  960. * semaphore.
  961. */
  962. static void complete_change_console(struct vc_data *vc)
  963. {
  964. unsigned char old_vc_mode;
  965. int old = fg_console;
  966. last_console = fg_console;
  967. /*
  968. * If we're switching, we could be going from KD_GRAPHICS to
  969. * KD_TEXT mode or vice versa, which means we need to blank or
  970. * unblank the screen later.
  971. */
  972. old_vc_mode = vc_cons[fg_console].d->vc_mode;
  973. switch_screen(vc);
  974. /*
  975. * This can't appear below a successful kill_pid(). If it did,
  976. * then the *blank_screen operation could occur while X, having
  977. * received acqsig, is waking up on another processor. This
  978. * condition can lead to overlapping accesses to the VGA range
  979. * and the framebuffer (causing system lockups).
  980. *
  981. * To account for this we duplicate this code below only if the
  982. * controlling process is gone and we've called reset_vc.
  983. */
  984. if (old_vc_mode != vc->vc_mode) {
  985. if (vc->vc_mode == KD_TEXT)
  986. do_unblank_screen(1);
  987. else
  988. do_blank_screen(1);
  989. }
  990. /*
  991. * If this new console is under process control, send it a signal
  992. * telling it that it has acquired. Also check if it has died and
  993. * clean up (similar to logic employed in change_console())
  994. */
  995. if (vc->vt_mode.mode == VT_PROCESS) {
  996. /*
  997. * Send the signal as privileged - kill_pid() will
  998. * tell us if the process has gone or something else
  999. * is awry
  1000. */
  1001. if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
  1002. /*
  1003. * The controlling process has died, so we revert back to
  1004. * normal operation. In this case, we'll also change back
  1005. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1006. * but it saves the agony when the X server dies and the screen
  1007. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1008. * this outside of VT_PROCESS but there is no single process
  1009. * to account for and tracking tty count may be undesirable.
  1010. */
  1011. reset_vc(vc);
  1012. if (old_vc_mode != vc->vc_mode) {
  1013. if (vc->vc_mode == KD_TEXT)
  1014. do_unblank_screen(1);
  1015. else
  1016. do_blank_screen(1);
  1017. }
  1018. }
  1019. }
  1020. /*
  1021. * Wake anyone waiting for their VT to activate
  1022. */
  1023. vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
  1024. return;
  1025. }
  1026. /*
  1027. * Performs the front-end of a vt switch
  1028. */
  1029. void change_console(struct vc_data *new_vc)
  1030. {
  1031. struct vc_data *vc;
  1032. if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
  1033. return;
  1034. /*
  1035. * If this vt is in process mode, then we need to handshake with
  1036. * that process before switching. Essentially, we store where that
  1037. * vt wants to switch to and wait for it to tell us when it's done
  1038. * (via VT_RELDISP ioctl).
  1039. *
  1040. * We also check to see if the controlling process still exists.
  1041. * If it doesn't, we reset this vt to auto mode and continue.
  1042. * This is a cheap way to track process control. The worst thing
  1043. * that can happen is: we send a signal to a process, it dies, and
  1044. * the switch gets "lost" waiting for a response; hopefully, the
  1045. * user will try again, we'll detect the process is gone (unless
  1046. * the user waits just the right amount of time :-) and revert the
  1047. * vt to auto control.
  1048. */
  1049. vc = vc_cons[fg_console].d;
  1050. if (vc->vt_mode.mode == VT_PROCESS) {
  1051. /*
  1052. * Send the signal as privileged - kill_pid() will
  1053. * tell us if the process has gone or something else
  1054. * is awry.
  1055. *
  1056. * We need to set vt_newvt *before* sending the signal or we
  1057. * have a race.
  1058. */
  1059. vc->vt_newvt = new_vc->vc_num;
  1060. if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
  1061. /*
  1062. * It worked. Mark the vt to switch to and
  1063. * return. The process needs to send us a
  1064. * VT_RELDISP ioctl to complete the switch.
  1065. */
  1066. return;
  1067. }
  1068. /*
  1069. * The controlling process has died, so we revert back to
  1070. * normal operation. In this case, we'll also change back
  1071. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1072. * but it saves the agony when the X server dies and the screen
  1073. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1074. * this outside of VT_PROCESS but there is no single process
  1075. * to account for and tracking tty count may be undesirable.
  1076. */
  1077. reset_vc(vc);
  1078. /*
  1079. * Fall through to normal (VT_AUTO) handling of the switch...
  1080. */
  1081. }
  1082. /*
  1083. * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
  1084. */
  1085. if (vc->vc_mode == KD_GRAPHICS)
  1086. return;
  1087. complete_change_console(new_vc);
  1088. }
  1089. /* Perform a kernel triggered VT switch for suspend/resume */
  1090. static int disable_vt_switch;
  1091. int vt_move_to_console(unsigned int vt, int alloc)
  1092. {
  1093. int prev;
  1094. console_lock();
  1095. /* Graphics mode - up to X */
  1096. if (disable_vt_switch) {
  1097. console_unlock();
  1098. return 0;
  1099. }
  1100. prev = fg_console;
  1101. if (alloc && vc_allocate(vt)) {
  1102. /* we can't have a free VC for now. Too bad,
  1103. * we don't want to mess the screen for now. */
  1104. console_unlock();
  1105. return -ENOSPC;
  1106. }
  1107. if (set_console(vt)) {
  1108. /*
  1109. * We're unable to switch to the SUSPEND_CONSOLE.
  1110. * Let the calling function know so it can decide
  1111. * what to do.
  1112. */
  1113. console_unlock();
  1114. return -EIO;
  1115. }
  1116. console_unlock();
  1117. if (vt_waitactive(vt + 1)) {
  1118. pr_debug("Suspend: Can't switch VCs.");
  1119. return -EINTR;
  1120. }
  1121. return prev;
  1122. }
  1123. /*
  1124. * Normally during a suspend, we allocate a new console and switch to it.
  1125. * When we resume, we switch back to the original console. This switch
  1126. * can be slow, so on systems where the framebuffer can handle restoration
  1127. * of video registers anyways, there's little point in doing the console
  1128. * switch. This function allows you to disable it by passing it '0'.
  1129. */
  1130. void pm_set_vt_switch(int do_switch)
  1131. {
  1132. console_lock();
  1133. disable_vt_switch = !do_switch;
  1134. console_unlock();
  1135. }
  1136. EXPORT_SYMBOL(pm_set_vt_switch);