vt_ioctl.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 1992 obz under the linux copyright
  4. *
  5. * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
  6. * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
  7. * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
  8. * Some code moved for less code duplication - Andi Kleen - Mar 1997
  9. * Check put/get_user, cleanups - acme@conectiva.com.br - 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. * @arg: argument to ioctl
  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. * @event: event code
  181. * @n: new console
  182. *
  183. * Helper for event waits. Used to implement the legacy
  184. * event waiting ioctls in terms of events
  185. */
  186. int vt_waitactive(int n)
  187. {
  188. struct vt_event_wait vw;
  189. do {
  190. vw.event.event = VT_EVENT_SWITCH;
  191. __vt_event_queue(&vw);
  192. if (n == fg_console + 1) {
  193. __vt_event_dequeue(&vw);
  194. break;
  195. }
  196. __vt_event_wait(&vw);
  197. __vt_event_dequeue(&vw);
  198. if (vw.done == 0)
  199. return -EINTR;
  200. } while (vw.event.newev != n);
  201. return 0;
  202. }
  203. /*
  204. * these are the valid i/o ports we're allowed to change. they map all the
  205. * video ports
  206. */
  207. #define GPFIRST 0x3b4
  208. #define GPLAST 0x3df
  209. #define GPNUM (GPLAST - GPFIRST + 1)
  210. static inline int
  211. do_fontx_ioctl(struct vc_data *vc, int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
  212. {
  213. struct consolefontdesc cfdarg;
  214. int i;
  215. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
  216. return -EFAULT;
  217. switch (cmd) {
  218. case PIO_FONTX:
  219. if (!perm)
  220. return -EPERM;
  221. op->op = KD_FONT_OP_SET;
  222. op->flags = KD_FONT_FLAG_OLD;
  223. op->width = 8;
  224. op->height = cfdarg.charheight;
  225. op->charcount = cfdarg.charcount;
  226. op->data = cfdarg.chardata;
  227. return con_font_op(vc, op);
  228. case GIO_FONTX:
  229. op->op = KD_FONT_OP_GET;
  230. op->flags = KD_FONT_FLAG_OLD;
  231. op->width = 8;
  232. op->height = cfdarg.charheight;
  233. op->charcount = cfdarg.charcount;
  234. op->data = cfdarg.chardata;
  235. i = con_font_op(vc, op);
  236. if (i)
  237. return i;
  238. cfdarg.charheight = op->height;
  239. cfdarg.charcount = op->charcount;
  240. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
  241. return -EFAULT;
  242. return 0;
  243. }
  244. return -EINVAL;
  245. }
  246. static inline int
  247. do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
  248. {
  249. struct unimapdesc tmp;
  250. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  251. return -EFAULT;
  252. switch (cmd) {
  253. case PIO_UNIMAP:
  254. if (!perm)
  255. return -EPERM;
  256. return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
  257. case GIO_UNIMAP:
  258. if (!perm && fg_console != vc->vc_num)
  259. return -EPERM;
  260. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
  261. }
  262. return 0;
  263. }
  264. /* deallocate a single console, if possible (leave 0) */
  265. static int vt_disallocate(unsigned int vc_num)
  266. {
  267. struct vc_data *vc = NULL;
  268. int ret = 0;
  269. console_lock();
  270. if (vt_busy(vc_num))
  271. ret = -EBUSY;
  272. else if (vc_num)
  273. vc = vc_deallocate(vc_num);
  274. console_unlock();
  275. if (vc && vc_num >= MIN_NR_CONSOLES)
  276. tty_port_put(&vc->port);
  277. return ret;
  278. }
  279. /* deallocate all unused consoles, but leave 0 */
  280. static void vt_disallocate_all(void)
  281. {
  282. struct vc_data *vc[MAX_NR_CONSOLES];
  283. int i;
  284. console_lock();
  285. for (i = 1; i < MAX_NR_CONSOLES; i++)
  286. if (!vt_busy(i))
  287. vc[i] = vc_deallocate(i);
  288. else
  289. vc[i] = NULL;
  290. console_unlock();
  291. for (i = 1; i < MAX_NR_CONSOLES; i++) {
  292. if (vc[i] && i >= MIN_NR_CONSOLES)
  293. tty_port_put(&vc[i]->port);
  294. }
  295. }
  296. /*
  297. * We handle the console-specific ioctl's here. We allow the
  298. * capability to modify any console, not just the fg_console.
  299. */
  300. int vt_ioctl(struct tty_struct *tty,
  301. unsigned int cmd, unsigned long arg)
  302. {
  303. struct vc_data *vc = tty->driver_data;
  304. struct console_font_op op; /* used in multiple places here */
  305. unsigned int console = vc->vc_num;
  306. unsigned char ucval;
  307. unsigned int uival;
  308. void __user *up = (void __user *)arg;
  309. int i, perm;
  310. int ret = 0;
  311. /*
  312. * To have permissions to do most of the vt ioctls, we either have
  313. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  314. */
  315. perm = 0;
  316. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  317. perm = 1;
  318. switch (cmd) {
  319. case TIOCLINUX:
  320. ret = tioclinux(tty, arg);
  321. break;
  322. case KIOCSOUND:
  323. if (!perm)
  324. return -EPERM;
  325. /*
  326. * The use of PIT_TICK_RATE is historic, it used to be
  327. * the platform-dependent CLOCK_TICK_RATE between 2.6.12
  328. * and 2.6.36, which was a minor but unfortunate ABI
  329. * change. kd_mksound is locked by the input layer.
  330. */
  331. if (arg)
  332. arg = PIT_TICK_RATE / arg;
  333. kd_mksound(arg, 0);
  334. break;
  335. case KDMKTONE:
  336. if (!perm)
  337. return -EPERM;
  338. {
  339. unsigned int ticks, count;
  340. /*
  341. * Generate the tone for the appropriate number of ticks.
  342. * If the time is zero, turn off sound ourselves.
  343. */
  344. ticks = msecs_to_jiffies((arg >> 16) & 0xffff);
  345. count = ticks ? (arg & 0xffff) : 0;
  346. if (count)
  347. count = PIT_TICK_RATE / count;
  348. kd_mksound(count, ticks);
  349. break;
  350. }
  351. case KDGKBTYPE:
  352. /*
  353. * this is naïve.
  354. */
  355. ucval = KB_101;
  356. ret = put_user(ucval, (char __user *)arg);
  357. break;
  358. /*
  359. * These cannot be implemented on any machine that implements
  360. * ioperm() in user level (such as Alpha PCs) or not at all.
  361. *
  362. * XXX: you should never use these, just call ioperm directly..
  363. */
  364. #ifdef CONFIG_X86
  365. case KDADDIO:
  366. case KDDELIO:
  367. /*
  368. * KDADDIO and KDDELIO may be able to add ports beyond what
  369. * we reject here, but to be safe...
  370. *
  371. * These are locked internally via sys_ioperm
  372. */
  373. if (arg < GPFIRST || arg > GPLAST) {
  374. ret = -EINVAL;
  375. break;
  376. }
  377. ret = ksys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
  378. break;
  379. case KDENABIO:
  380. case KDDISABIO:
  381. ret = ksys_ioperm(GPFIRST, GPNUM,
  382. (cmd == KDENABIO)) ? -ENXIO : 0;
  383. break;
  384. #endif
  385. /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
  386. case KDKBDREP:
  387. {
  388. struct kbd_repeat kbrep;
  389. if (!capable(CAP_SYS_TTY_CONFIG))
  390. return -EPERM;
  391. if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
  392. ret = -EFAULT;
  393. break;
  394. }
  395. ret = kbd_rate(&kbrep);
  396. if (ret)
  397. break;
  398. if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
  399. ret = -EFAULT;
  400. break;
  401. }
  402. case KDSETMODE:
  403. /*
  404. * currently, setting the mode from KD_TEXT to KD_GRAPHICS
  405. * doesn't do a whole lot. i'm not sure if it should do any
  406. * restoration of modes or what...
  407. *
  408. * XXX It should at least call into the driver, fbdev's definitely
  409. * need to restore their engine state. --BenH
  410. */
  411. if (!perm)
  412. return -EPERM;
  413. switch (arg) {
  414. case KD_GRAPHICS:
  415. break;
  416. case KD_TEXT0:
  417. case KD_TEXT1:
  418. arg = KD_TEXT;
  419. case KD_TEXT:
  420. break;
  421. default:
  422. ret = -EINVAL;
  423. goto out;
  424. }
  425. /* FIXME: this needs the console lock extending */
  426. if (vc->vc_mode == (unsigned char) arg)
  427. break;
  428. vc->vc_mode = (unsigned char) arg;
  429. if (console != fg_console)
  430. break;
  431. /*
  432. * explicitly blank/unblank the screen if switching modes
  433. */
  434. console_lock();
  435. if (arg == KD_TEXT)
  436. do_unblank_screen(1);
  437. else
  438. do_blank_screen(1);
  439. console_unlock();
  440. break;
  441. case KDGETMODE:
  442. uival = vc->vc_mode;
  443. goto setint;
  444. case KDMAPDISP:
  445. case KDUNMAPDISP:
  446. /*
  447. * these work like a combination of mmap and KDENABIO.
  448. * this could be easily finished.
  449. */
  450. ret = -EINVAL;
  451. break;
  452. case KDSKBMODE:
  453. if (!perm)
  454. return -EPERM;
  455. ret = vt_do_kdskbmode(console, arg);
  456. if (ret == 0)
  457. tty_ldisc_flush(tty);
  458. break;
  459. case KDGKBMODE:
  460. uival = vt_do_kdgkbmode(console);
  461. ret = put_user(uival, (int __user *)arg);
  462. break;
  463. /* this could be folded into KDSKBMODE, but for compatibility
  464. reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
  465. case KDSKBMETA:
  466. ret = vt_do_kdskbmeta(console, arg);
  467. break;
  468. case KDGKBMETA:
  469. /* FIXME: should review whether this is worth locking */
  470. uival = vt_do_kdgkbmeta(console);
  471. setint:
  472. ret = put_user(uival, (int __user *)arg);
  473. break;
  474. case KDGETKEYCODE:
  475. case KDSETKEYCODE:
  476. if(!capable(CAP_SYS_TTY_CONFIG))
  477. perm = 0;
  478. ret = vt_do_kbkeycode_ioctl(cmd, up, perm);
  479. break;
  480. case KDGKBENT:
  481. case KDSKBENT:
  482. ret = vt_do_kdsk_ioctl(cmd, up, perm, console);
  483. break;
  484. case KDGKBSENT:
  485. case KDSKBSENT:
  486. ret = vt_do_kdgkb_ioctl(cmd, up, perm);
  487. break;
  488. /* Diacritical processing. Handled in keyboard.c as it has
  489. to operate on the keyboard locks and structures */
  490. case KDGKBDIACR:
  491. case KDGKBDIACRUC:
  492. case KDSKBDIACR:
  493. case KDSKBDIACRUC:
  494. ret = vt_do_diacrit(cmd, up, perm);
  495. break;
  496. /* the ioctls below read/set the flags usually shown in the leds */
  497. /* don't use them - they will go away without warning */
  498. case KDGKBLED:
  499. case KDSKBLED:
  500. case KDGETLED:
  501. case KDSETLED:
  502. ret = vt_do_kdskled(console, cmd, arg, perm);
  503. break;
  504. /*
  505. * A process can indicate its willingness to accept signals
  506. * generated by pressing an appropriate key combination.
  507. * Thus, one can have a daemon that e.g. spawns a new console
  508. * upon a keypress and then changes to it.
  509. * See also the kbrequest field of inittab(5).
  510. */
  511. case KDSIGACCEPT:
  512. {
  513. if (!perm || !capable(CAP_KILL))
  514. return -EPERM;
  515. if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
  516. ret = -EINVAL;
  517. else {
  518. spin_lock_irq(&vt_spawn_con.lock);
  519. put_pid(vt_spawn_con.pid);
  520. vt_spawn_con.pid = get_pid(task_pid(current));
  521. vt_spawn_con.sig = arg;
  522. spin_unlock_irq(&vt_spawn_con.lock);
  523. }
  524. break;
  525. }
  526. case VT_SETMODE:
  527. {
  528. struct vt_mode tmp;
  529. if (!perm)
  530. return -EPERM;
  531. if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
  532. ret = -EFAULT;
  533. goto out;
  534. }
  535. if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
  536. ret = -EINVAL;
  537. goto out;
  538. }
  539. console_lock();
  540. vc->vt_mode = tmp;
  541. /* the frsig is ignored, so we set it to 0 */
  542. vc->vt_mode.frsig = 0;
  543. put_pid(vc->vt_pid);
  544. vc->vt_pid = get_pid(task_pid(current));
  545. /* no switch is required -- saw@shade.msu.ru */
  546. vc->vt_newvt = -1;
  547. console_unlock();
  548. break;
  549. }
  550. case VT_GETMODE:
  551. {
  552. struct vt_mode tmp;
  553. int rc;
  554. console_lock();
  555. memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
  556. console_unlock();
  557. rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
  558. if (rc)
  559. ret = -EFAULT;
  560. break;
  561. }
  562. /*
  563. * Returns global vt state. Note that VT 0 is always open, since
  564. * it's an alias for the current VT, and people can't use it here.
  565. * We cannot return state for more than 16 VTs, since v_state is short.
  566. */
  567. case VT_GETSTATE:
  568. {
  569. struct vt_stat __user *vtstat = up;
  570. unsigned short state, mask;
  571. if (put_user(fg_console + 1, &vtstat->v_active))
  572. ret = -EFAULT;
  573. else {
  574. state = 1; /* /dev/tty0 is always open */
  575. console_lock(); /* required by vt_in_use() */
  576. for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
  577. ++i, mask <<= 1)
  578. if (vt_in_use(i))
  579. state |= mask;
  580. console_unlock();
  581. ret = put_user(state, &vtstat->v_state);
  582. }
  583. break;
  584. }
  585. /*
  586. * Returns the first available (non-opened) console.
  587. */
  588. case VT_OPENQRY:
  589. console_lock(); /* required by vt_in_use() */
  590. for (i = 0; i < MAX_NR_CONSOLES; ++i)
  591. if (!vt_in_use(i))
  592. break;
  593. console_unlock();
  594. uival = i < MAX_NR_CONSOLES ? (i+1) : -1;
  595. goto setint;
  596. /*
  597. * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
  598. * with num >= 1 (switches to vt 0, our console, are not allowed, just
  599. * to preserve sanity).
  600. */
  601. case VT_ACTIVATE:
  602. if (!perm)
  603. return -EPERM;
  604. if (arg == 0 || arg > MAX_NR_CONSOLES)
  605. ret = -ENXIO;
  606. else {
  607. arg--;
  608. console_lock();
  609. ret = vc_allocate(arg);
  610. console_unlock();
  611. if (ret)
  612. break;
  613. set_console(arg);
  614. }
  615. break;
  616. case VT_SETACTIVATE:
  617. {
  618. struct vt_setactivate vsa;
  619. if (!perm)
  620. return -EPERM;
  621. if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
  622. sizeof(struct vt_setactivate))) {
  623. ret = -EFAULT;
  624. goto out;
  625. }
  626. if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
  627. ret = -ENXIO;
  628. else {
  629. vsa.console = array_index_nospec(vsa.console,
  630. MAX_NR_CONSOLES + 1);
  631. vsa.console--;
  632. console_lock();
  633. ret = vc_allocate(vsa.console);
  634. if (ret == 0) {
  635. struct vc_data *nvc;
  636. /* This is safe providing we don't drop the
  637. console sem between vc_allocate and
  638. finishing referencing nvc */
  639. nvc = vc_cons[vsa.console].d;
  640. nvc->vt_mode = vsa.mode;
  641. nvc->vt_mode.frsig = 0;
  642. put_pid(nvc->vt_pid);
  643. nvc->vt_pid = get_pid(task_pid(current));
  644. }
  645. console_unlock();
  646. if (ret)
  647. break;
  648. /* Commence switch and lock */
  649. /* Review set_console locks */
  650. set_console(vsa.console);
  651. }
  652. break;
  653. }
  654. /*
  655. * wait until the specified VT has been activated
  656. */
  657. case VT_WAITACTIVE:
  658. if (!perm)
  659. return -EPERM;
  660. if (arg == 0 || arg > MAX_NR_CONSOLES)
  661. ret = -ENXIO;
  662. else
  663. ret = vt_waitactive(arg);
  664. break;
  665. /*
  666. * If a vt is under process control, the kernel will not switch to it
  667. * immediately, but postpone the operation until the process calls this
  668. * ioctl, allowing the switch to complete.
  669. *
  670. * According to the X sources this is the behavior:
  671. * 0: pending switch-from not OK
  672. * 1: pending switch-from OK
  673. * 2: completed switch-to OK
  674. */
  675. case VT_RELDISP:
  676. if (!perm)
  677. return -EPERM;
  678. console_lock();
  679. if (vc->vt_mode.mode != VT_PROCESS) {
  680. console_unlock();
  681. ret = -EINVAL;
  682. break;
  683. }
  684. /*
  685. * Switching-from response
  686. */
  687. if (vc->vt_newvt >= 0) {
  688. if (arg == 0)
  689. /*
  690. * Switch disallowed, so forget we were trying
  691. * to do it.
  692. */
  693. vc->vt_newvt = -1;
  694. else {
  695. /*
  696. * The current vt has been released, so
  697. * complete the switch.
  698. */
  699. int newvt;
  700. newvt = vc->vt_newvt;
  701. vc->vt_newvt = -1;
  702. ret = vc_allocate(newvt);
  703. if (ret) {
  704. console_unlock();
  705. break;
  706. }
  707. /*
  708. * When we actually do the console switch,
  709. * make sure we are atomic with respect to
  710. * other console switches..
  711. */
  712. complete_change_console(vc_cons[newvt].d);
  713. }
  714. } else {
  715. /*
  716. * Switched-to response
  717. */
  718. /*
  719. * If it's just an ACK, ignore it
  720. */
  721. if (arg != VT_ACKACQ)
  722. ret = -EINVAL;
  723. }
  724. console_unlock();
  725. break;
  726. /*
  727. * Disallocate memory associated to VT (but leave VT1)
  728. */
  729. case VT_DISALLOCATE:
  730. if (arg > MAX_NR_CONSOLES) {
  731. ret = -ENXIO;
  732. break;
  733. }
  734. if (arg == 0)
  735. vt_disallocate_all();
  736. else
  737. ret = vt_disallocate(--arg);
  738. break;
  739. case VT_RESIZE:
  740. {
  741. struct vt_sizes __user *vtsizes = up;
  742. struct vc_data *vc;
  743. ushort ll,cc;
  744. if (!perm)
  745. return -EPERM;
  746. if (get_user(ll, &vtsizes->v_rows) ||
  747. get_user(cc, &vtsizes->v_cols))
  748. ret = -EFAULT;
  749. else {
  750. console_lock();
  751. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  752. vc = vc_cons[i].d;
  753. if (vc) {
  754. vc->vc_resize_user = 1;
  755. /* FIXME: review v tty lock */
  756. vc_resize(vc_cons[i].d, cc, ll);
  757. }
  758. }
  759. console_unlock();
  760. }
  761. break;
  762. }
  763. case VT_RESIZEX:
  764. {
  765. struct vt_consize v;
  766. if (!perm)
  767. return -EPERM;
  768. if (copy_from_user(&v, up, sizeof(struct vt_consize)))
  769. return -EFAULT;
  770. /* FIXME: Should check the copies properly */
  771. if (!v.v_vlin)
  772. v.v_vlin = vc->vc_scan_lines;
  773. if (v.v_clin) {
  774. int rows = v.v_vlin/v.v_clin;
  775. if (v.v_rows != rows) {
  776. if (v.v_rows) /* Parameters don't add up */
  777. return -EINVAL;
  778. v.v_rows = rows;
  779. }
  780. }
  781. if (v.v_vcol && v.v_ccol) {
  782. int cols = v.v_vcol/v.v_ccol;
  783. if (v.v_cols != cols) {
  784. if (v.v_cols)
  785. return -EINVAL;
  786. v.v_cols = cols;
  787. }
  788. }
  789. if (v.v_clin > 32)
  790. return -EINVAL;
  791. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  792. struct vc_data *vcp;
  793. if (!vc_cons[i].d)
  794. continue;
  795. console_lock();
  796. vcp = vc_cons[i].d;
  797. if (vcp) {
  798. int ret;
  799. int save_scan_lines = vcp->vc_scan_lines;
  800. int save_cell_height = vcp->vc_cell_height;
  801. if (v.v_vlin)
  802. vcp->vc_scan_lines = v.v_vlin;
  803. if (v.v_clin)
  804. vcp->vc_cell_height = v.v_clin;
  805. vcp->vc_resize_user = 1;
  806. ret = vc_resize(vcp, v.v_cols, v.v_rows);
  807. if (ret) {
  808. vcp->vc_scan_lines = save_scan_lines;
  809. vcp->vc_cell_height = save_cell_height;
  810. console_unlock();
  811. return ret;
  812. }
  813. }
  814. console_unlock();
  815. }
  816. break;
  817. }
  818. case PIO_FONT: {
  819. if (!perm)
  820. return -EPERM;
  821. op.op = KD_FONT_OP_SET;
  822. op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
  823. op.width = 8;
  824. op.height = 0;
  825. op.charcount = 256;
  826. op.data = up;
  827. ret = con_font_op(vc, &op);
  828. break;
  829. }
  830. case GIO_FONT: {
  831. op.op = KD_FONT_OP_GET;
  832. op.flags = KD_FONT_FLAG_OLD;
  833. op.width = 8;
  834. op.height = 32;
  835. op.charcount = 256;
  836. op.data = up;
  837. ret = con_font_op(vc, &op);
  838. break;
  839. }
  840. case PIO_CMAP:
  841. if (!perm)
  842. ret = -EPERM;
  843. else
  844. ret = con_set_cmap(up);
  845. break;
  846. case GIO_CMAP:
  847. ret = con_get_cmap(up);
  848. break;
  849. case PIO_FONTX:
  850. case GIO_FONTX:
  851. ret = do_fontx_ioctl(vc, cmd, up, perm, &op);
  852. break;
  853. case PIO_FONTRESET:
  854. {
  855. if (!perm)
  856. return -EPERM;
  857. #ifdef BROKEN_GRAPHICS_PROGRAMS
  858. /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
  859. font is not saved. */
  860. ret = -ENOSYS;
  861. break;
  862. #else
  863. {
  864. op.op = KD_FONT_OP_SET_DEFAULT;
  865. op.data = NULL;
  866. ret = con_font_op(vc, &op);
  867. if (ret)
  868. break;
  869. console_lock();
  870. con_set_default_unimap(vc);
  871. console_unlock();
  872. break;
  873. }
  874. #endif
  875. }
  876. case KDFONTOP: {
  877. if (copy_from_user(&op, up, sizeof(op))) {
  878. ret = -EFAULT;
  879. break;
  880. }
  881. if (!perm && op.op != KD_FONT_OP_GET)
  882. return -EPERM;
  883. ret = con_font_op(vc, &op);
  884. if (ret)
  885. break;
  886. if (copy_to_user(up, &op, sizeof(op)))
  887. ret = -EFAULT;
  888. break;
  889. }
  890. case PIO_SCRNMAP:
  891. if (!perm)
  892. ret = -EPERM;
  893. else
  894. ret = con_set_trans_old(up);
  895. break;
  896. case GIO_SCRNMAP:
  897. ret = con_get_trans_old(up);
  898. break;
  899. case PIO_UNISCRNMAP:
  900. if (!perm)
  901. ret = -EPERM;
  902. else
  903. ret = con_set_trans_new(up);
  904. break;
  905. case GIO_UNISCRNMAP:
  906. ret = con_get_trans_new(up);
  907. break;
  908. case PIO_UNIMAPCLR:
  909. if (!perm)
  910. return -EPERM;
  911. con_clear_unimap(vc);
  912. break;
  913. case PIO_UNIMAP:
  914. case GIO_UNIMAP:
  915. ret = do_unimap_ioctl(cmd, up, perm, vc);
  916. break;
  917. case VT_LOCKSWITCH:
  918. if (!capable(CAP_SYS_TTY_CONFIG))
  919. return -EPERM;
  920. vt_dont_switch = true;
  921. break;
  922. case VT_UNLOCKSWITCH:
  923. if (!capable(CAP_SYS_TTY_CONFIG))
  924. return -EPERM;
  925. vt_dont_switch = false;
  926. break;
  927. case VT_GETHIFONTMASK:
  928. ret = put_user(vc->vc_hi_font_mask,
  929. (unsigned short __user *)arg);
  930. break;
  931. case VT_WAITEVENT:
  932. ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
  933. break;
  934. default:
  935. ret = -ENOIOCTLCMD;
  936. }
  937. out:
  938. return ret;
  939. }
  940. void reset_vc(struct vc_data *vc)
  941. {
  942. vc->vc_mode = KD_TEXT;
  943. vt_reset_unicode(vc->vc_num);
  944. vc->vt_mode.mode = VT_AUTO;
  945. vc->vt_mode.waitv = 0;
  946. vc->vt_mode.relsig = 0;
  947. vc->vt_mode.acqsig = 0;
  948. vc->vt_mode.frsig = 0;
  949. put_pid(vc->vt_pid);
  950. vc->vt_pid = NULL;
  951. vc->vt_newvt = -1;
  952. if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
  953. reset_palette(vc);
  954. }
  955. void vc_SAK(struct work_struct *work)
  956. {
  957. struct vc *vc_con =
  958. container_of(work, struct vc, SAK_work);
  959. struct vc_data *vc;
  960. struct tty_struct *tty;
  961. console_lock();
  962. vc = vc_con->d;
  963. if (vc) {
  964. /* FIXME: review tty ref counting */
  965. tty = vc->port.tty;
  966. /*
  967. * SAK should also work in all raw modes and reset
  968. * them properly.
  969. */
  970. if (tty)
  971. __do_SAK(tty);
  972. reset_vc(vc);
  973. }
  974. console_unlock();
  975. }
  976. #ifdef CONFIG_COMPAT
  977. struct compat_consolefontdesc {
  978. unsigned short charcount; /* characters in font (256 or 512) */
  979. unsigned short charheight; /* scan lines per character (1-32) */
  980. compat_caddr_t chardata; /* font data in expanded form */
  981. };
  982. static inline int
  983. compat_fontx_ioctl(struct vc_data *vc, int cmd,
  984. struct compat_consolefontdesc __user *user_cfd,
  985. int perm, struct console_font_op *op)
  986. {
  987. struct compat_consolefontdesc cfdarg;
  988. int i;
  989. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
  990. return -EFAULT;
  991. switch (cmd) {
  992. case PIO_FONTX:
  993. if (!perm)
  994. return -EPERM;
  995. op->op = KD_FONT_OP_SET;
  996. op->flags = KD_FONT_FLAG_OLD;
  997. op->width = 8;
  998. op->height = cfdarg.charheight;
  999. op->charcount = cfdarg.charcount;
  1000. op->data = compat_ptr(cfdarg.chardata);
  1001. return con_font_op(vc, op);
  1002. case GIO_FONTX:
  1003. op->op = KD_FONT_OP_GET;
  1004. op->flags = KD_FONT_FLAG_OLD;
  1005. op->width = 8;
  1006. op->height = cfdarg.charheight;
  1007. op->charcount = cfdarg.charcount;
  1008. op->data = compat_ptr(cfdarg.chardata);
  1009. i = con_font_op(vc, op);
  1010. if (i)
  1011. return i;
  1012. cfdarg.charheight = op->height;
  1013. cfdarg.charcount = op->charcount;
  1014. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
  1015. return -EFAULT;
  1016. return 0;
  1017. }
  1018. return -EINVAL;
  1019. }
  1020. struct compat_console_font_op {
  1021. compat_uint_t op; /* operation code KD_FONT_OP_* */
  1022. compat_uint_t flags; /* KD_FONT_FLAG_* */
  1023. compat_uint_t width, height; /* font size */
  1024. compat_uint_t charcount;
  1025. compat_caddr_t data; /* font data with height fixed to 32 */
  1026. };
  1027. static inline int
  1028. compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
  1029. int perm, struct console_font_op *op, struct vc_data *vc)
  1030. {
  1031. int i;
  1032. if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
  1033. return -EFAULT;
  1034. if (!perm && op->op != KD_FONT_OP_GET)
  1035. return -EPERM;
  1036. op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
  1037. i = con_font_op(vc, op);
  1038. if (i)
  1039. return i;
  1040. ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
  1041. if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
  1042. return -EFAULT;
  1043. return 0;
  1044. }
  1045. struct compat_unimapdesc {
  1046. unsigned short entry_ct;
  1047. compat_caddr_t entries;
  1048. };
  1049. static inline int
  1050. compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
  1051. int perm, struct vc_data *vc)
  1052. {
  1053. struct compat_unimapdesc tmp;
  1054. struct unipair __user *tmp_entries;
  1055. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  1056. return -EFAULT;
  1057. tmp_entries = compat_ptr(tmp.entries);
  1058. switch (cmd) {
  1059. case PIO_UNIMAP:
  1060. if (!perm)
  1061. return -EPERM;
  1062. return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
  1063. case GIO_UNIMAP:
  1064. if (!perm && fg_console != vc->vc_num)
  1065. return -EPERM;
  1066. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
  1067. }
  1068. return 0;
  1069. }
  1070. long vt_compat_ioctl(struct tty_struct *tty,
  1071. unsigned int cmd, unsigned long arg)
  1072. {
  1073. struct vc_data *vc = tty->driver_data;
  1074. struct console_font_op op; /* used in multiple places here */
  1075. void __user *up = (void __user *)arg;
  1076. int perm;
  1077. int ret = 0;
  1078. /*
  1079. * To have permissions to do most of the vt ioctls, we either have
  1080. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  1081. */
  1082. perm = 0;
  1083. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  1084. perm = 1;
  1085. switch (cmd) {
  1086. /*
  1087. * these need special handlers for incompatible data structures
  1088. */
  1089. case PIO_FONTX:
  1090. case GIO_FONTX:
  1091. ret = compat_fontx_ioctl(vc, cmd, up, perm, &op);
  1092. break;
  1093. case KDFONTOP:
  1094. ret = compat_kdfontop_ioctl(up, perm, &op, vc);
  1095. break;
  1096. case PIO_UNIMAP:
  1097. case GIO_UNIMAP:
  1098. ret = compat_unimap_ioctl(cmd, up, perm, vc);
  1099. break;
  1100. /*
  1101. * all these treat 'arg' as an integer
  1102. */
  1103. case KIOCSOUND:
  1104. case KDMKTONE:
  1105. #ifdef CONFIG_X86
  1106. case KDADDIO:
  1107. case KDDELIO:
  1108. #endif
  1109. case KDSETMODE:
  1110. case KDMAPDISP:
  1111. case KDUNMAPDISP:
  1112. case KDSKBMODE:
  1113. case KDSKBMETA:
  1114. case KDSKBLED:
  1115. case KDSETLED:
  1116. case KDSIGACCEPT:
  1117. case VT_ACTIVATE:
  1118. case VT_WAITACTIVE:
  1119. case VT_RELDISP:
  1120. case VT_DISALLOCATE:
  1121. case VT_RESIZE:
  1122. case VT_RESIZEX:
  1123. goto fallback;
  1124. /*
  1125. * the rest has a compatible data structure behind arg,
  1126. * but we have to convert it to a proper 64 bit pointer.
  1127. */
  1128. default:
  1129. arg = (unsigned long)compat_ptr(arg);
  1130. goto fallback;
  1131. }
  1132. return ret;
  1133. fallback:
  1134. return vt_ioctl(tty, cmd, arg);
  1135. }
  1136. #endif /* CONFIG_COMPAT */
  1137. /*
  1138. * Performs the back end of a vt switch. Called under the console
  1139. * semaphore.
  1140. */
  1141. static void complete_change_console(struct vc_data *vc)
  1142. {
  1143. unsigned char old_vc_mode;
  1144. int old = fg_console;
  1145. last_console = fg_console;
  1146. /*
  1147. * If we're switching, we could be going from KD_GRAPHICS to
  1148. * KD_TEXT mode or vice versa, which means we need to blank or
  1149. * unblank the screen later.
  1150. */
  1151. old_vc_mode = vc_cons[fg_console].d->vc_mode;
  1152. switch_screen(vc);
  1153. /*
  1154. * This can't appear below a successful kill_pid(). If it did,
  1155. * then the *blank_screen operation could occur while X, having
  1156. * received acqsig, is waking up on another processor. This
  1157. * condition can lead to overlapping accesses to the VGA range
  1158. * and the framebuffer (causing system lockups).
  1159. *
  1160. * To account for this we duplicate this code below only if the
  1161. * controlling process is gone and we've called reset_vc.
  1162. */
  1163. if (old_vc_mode != vc->vc_mode) {
  1164. if (vc->vc_mode == KD_TEXT)
  1165. do_unblank_screen(1);
  1166. else
  1167. do_blank_screen(1);
  1168. }
  1169. /*
  1170. * If this new console is under process control, send it a signal
  1171. * telling it that it has acquired. Also check if it has died and
  1172. * clean up (similar to logic employed in change_console())
  1173. */
  1174. if (vc->vt_mode.mode == VT_PROCESS) {
  1175. /*
  1176. * Send the signal as privileged - kill_pid() will
  1177. * tell us if the process has gone or something else
  1178. * is awry
  1179. */
  1180. if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
  1181. /*
  1182. * The controlling process has died, so we revert back to
  1183. * normal operation. In this case, we'll also change back
  1184. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1185. * but it saves the agony when the X server dies and the screen
  1186. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1187. * this outside of VT_PROCESS but there is no single process
  1188. * to account for and tracking tty count may be undesirable.
  1189. */
  1190. reset_vc(vc);
  1191. if (old_vc_mode != vc->vc_mode) {
  1192. if (vc->vc_mode == KD_TEXT)
  1193. do_unblank_screen(1);
  1194. else
  1195. do_blank_screen(1);
  1196. }
  1197. }
  1198. }
  1199. /*
  1200. * Wake anyone waiting for their VT to activate
  1201. */
  1202. vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
  1203. return;
  1204. }
  1205. /*
  1206. * Performs the front-end of a vt switch
  1207. */
  1208. void change_console(struct vc_data *new_vc)
  1209. {
  1210. struct vc_data *vc;
  1211. if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
  1212. return;
  1213. /*
  1214. * If this vt is in process mode, then we need to handshake with
  1215. * that process before switching. Essentially, we store where that
  1216. * vt wants to switch to and wait for it to tell us when it's done
  1217. * (via VT_RELDISP ioctl).
  1218. *
  1219. * We also check to see if the controlling process still exists.
  1220. * If it doesn't, we reset this vt to auto mode and continue.
  1221. * This is a cheap way to track process control. The worst thing
  1222. * that can happen is: we send a signal to a process, it dies, and
  1223. * the switch gets "lost" waiting for a response; hopefully, the
  1224. * user will try again, we'll detect the process is gone (unless
  1225. * the user waits just the right amount of time :-) and revert the
  1226. * vt to auto control.
  1227. */
  1228. vc = vc_cons[fg_console].d;
  1229. if (vc->vt_mode.mode == VT_PROCESS) {
  1230. /*
  1231. * Send the signal as privileged - kill_pid() will
  1232. * tell us if the process has gone or something else
  1233. * is awry.
  1234. *
  1235. * We need to set vt_newvt *before* sending the signal or we
  1236. * have a race.
  1237. */
  1238. vc->vt_newvt = new_vc->vc_num;
  1239. if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
  1240. /*
  1241. * It worked. Mark the vt to switch to and
  1242. * return. The process needs to send us a
  1243. * VT_RELDISP ioctl to complete the switch.
  1244. */
  1245. return;
  1246. }
  1247. /*
  1248. * The controlling process has died, so we revert back to
  1249. * normal operation. In this case, we'll also change back
  1250. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1251. * but it saves the agony when the X server dies and the screen
  1252. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1253. * this outside of VT_PROCESS but there is no single process
  1254. * to account for and tracking tty count may be undesirable.
  1255. */
  1256. reset_vc(vc);
  1257. /*
  1258. * Fall through to normal (VT_AUTO) handling of the switch...
  1259. */
  1260. }
  1261. /*
  1262. * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
  1263. */
  1264. if (vc->vc_mode == KD_GRAPHICS)
  1265. return;
  1266. complete_change_console(new_vc);
  1267. }
  1268. /* Perform a kernel triggered VT switch for suspend/resume */
  1269. static int disable_vt_switch;
  1270. int vt_move_to_console(unsigned int vt, int alloc)
  1271. {
  1272. int prev;
  1273. console_lock();
  1274. /* Graphics mode - up to X */
  1275. if (disable_vt_switch) {
  1276. console_unlock();
  1277. return 0;
  1278. }
  1279. prev = fg_console;
  1280. if (alloc && vc_allocate(vt)) {
  1281. /* we can't have a free VC for now. Too bad,
  1282. * we don't want to mess the screen for now. */
  1283. console_unlock();
  1284. return -ENOSPC;
  1285. }
  1286. if (set_console(vt)) {
  1287. /*
  1288. * We're unable to switch to the SUSPEND_CONSOLE.
  1289. * Let the calling function know so it can decide
  1290. * what to do.
  1291. */
  1292. console_unlock();
  1293. return -EIO;
  1294. }
  1295. console_unlock();
  1296. if (vt_waitactive(vt + 1)) {
  1297. pr_debug("Suspend: Can't switch VCs.");
  1298. return -EINTR;
  1299. }
  1300. return prev;
  1301. }
  1302. /*
  1303. * Normally during a suspend, we allocate a new console and switch to it.
  1304. * When we resume, we switch back to the original console. This switch
  1305. * can be slow, so on systems where the framebuffer can handle restoration
  1306. * of video registers anyways, there's little point in doing the console
  1307. * switch. This function allows you to disable it by passing it '0'.
  1308. */
  1309. void pm_set_vt_switch(int do_switch)
  1310. {
  1311. console_lock();
  1312. disable_vt_switch = !do_switch;
  1313. console_unlock();
  1314. }
  1315. EXPORT_SYMBOL(pm_set_vt_switch);