vt_ioctl.c 33 KB

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