vt_ioctl.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  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. * @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_fontx_ioctl(struct vc_data *vc, int cmd,
  419. struct consolefontdesc __user *user_cfd,
  420. struct console_font_op *op)
  421. {
  422. struct consolefontdesc cfdarg;
  423. int i;
  424. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
  425. return -EFAULT;
  426. switch (cmd) {
  427. case PIO_FONTX:
  428. op->op = KD_FONT_OP_SET;
  429. op->flags = KD_FONT_FLAG_OLD;
  430. op->width = 8;
  431. op->height = cfdarg.charheight;
  432. op->charcount = cfdarg.charcount;
  433. op->data = cfdarg.chardata;
  434. return con_font_op(vc, op);
  435. case GIO_FONTX:
  436. op->op = KD_FONT_OP_GET;
  437. op->flags = KD_FONT_FLAG_OLD;
  438. op->width = 8;
  439. op->height = cfdarg.charheight;
  440. op->charcount = cfdarg.charcount;
  441. op->data = cfdarg.chardata;
  442. i = con_font_op(vc, op);
  443. if (i)
  444. return i;
  445. cfdarg.charheight = op->height;
  446. cfdarg.charcount = op->charcount;
  447. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
  448. return -EFAULT;
  449. return 0;
  450. }
  451. return -EINVAL;
  452. }
  453. static int vt_io_fontreset(struct vc_data *vc, struct console_font_op *op)
  454. {
  455. int ret;
  456. if (__is_defined(BROKEN_GRAPHICS_PROGRAMS)) {
  457. /*
  458. * With BROKEN_GRAPHICS_PROGRAMS defined, the default font is
  459. * not saved.
  460. */
  461. return -ENOSYS;
  462. }
  463. op->op = KD_FONT_OP_SET_DEFAULT;
  464. op->data = NULL;
  465. ret = con_font_op(vc, op);
  466. if (ret)
  467. return ret;
  468. console_lock();
  469. con_set_default_unimap(vc);
  470. console_unlock();
  471. return 0;
  472. }
  473. static inline int do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud,
  474. bool perm, struct vc_data *vc)
  475. {
  476. struct unimapdesc tmp;
  477. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  478. return -EFAULT;
  479. switch (cmd) {
  480. case PIO_UNIMAP:
  481. if (!perm)
  482. return -EPERM;
  483. return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
  484. case GIO_UNIMAP:
  485. if (!perm && fg_console != vc->vc_num)
  486. return -EPERM;
  487. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct),
  488. tmp.entries);
  489. }
  490. return 0;
  491. }
  492. static int vt_io_ioctl(struct vc_data *vc, unsigned int cmd, void __user *up,
  493. bool perm)
  494. {
  495. struct console_font_op op; /* used in multiple places here */
  496. switch (cmd) {
  497. case PIO_FONT:
  498. if (!perm)
  499. return -EPERM;
  500. op.op = KD_FONT_OP_SET;
  501. op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
  502. op.width = 8;
  503. op.height = 0;
  504. op.charcount = 256;
  505. op.data = up;
  506. return con_font_op(vc, &op);
  507. case GIO_FONT:
  508. op.op = KD_FONT_OP_GET;
  509. op.flags = KD_FONT_FLAG_OLD;
  510. op.width = 8;
  511. op.height = 32;
  512. op.charcount = 256;
  513. op.data = up;
  514. return con_font_op(vc, &op);
  515. case PIO_CMAP:
  516. if (!perm)
  517. return -EPERM;
  518. return con_set_cmap(up);
  519. case GIO_CMAP:
  520. return con_get_cmap(up);
  521. case PIO_FONTX:
  522. if (!perm)
  523. return -EPERM;
  524. fallthrough;
  525. case GIO_FONTX:
  526. return do_fontx_ioctl(vc, cmd, up, &op);
  527. case PIO_FONTRESET:
  528. if (!perm)
  529. return -EPERM;
  530. return vt_io_fontreset(vc, &op);
  531. case PIO_SCRNMAP:
  532. if (!perm)
  533. return -EPERM;
  534. return con_set_trans_old(up);
  535. case GIO_SCRNMAP:
  536. return con_get_trans_old(up);
  537. case PIO_UNISCRNMAP:
  538. if (!perm)
  539. return -EPERM;
  540. return con_set_trans_new(up);
  541. case GIO_UNISCRNMAP:
  542. return con_get_trans_new(up);
  543. case PIO_UNIMAPCLR:
  544. if (!perm)
  545. return -EPERM;
  546. con_clear_unimap(vc);
  547. break;
  548. case PIO_UNIMAP:
  549. case GIO_UNIMAP:
  550. return do_unimap_ioctl(cmd, up, perm, vc);
  551. default:
  552. return -ENOIOCTLCMD;
  553. }
  554. return 0;
  555. }
  556. static int vt_reldisp(struct vc_data *vc, unsigned int swtch)
  557. {
  558. int newvt, ret;
  559. if (vc->vt_mode.mode != VT_PROCESS)
  560. return -EINVAL;
  561. /* Switched-to response */
  562. if (vc->vt_newvt < 0) {
  563. /* If it's just an ACK, ignore it */
  564. return swtch == VT_ACKACQ ? 0 : -EINVAL;
  565. }
  566. /* Switching-from response */
  567. if (swtch == 0) {
  568. /* Switch disallowed, so forget we were trying to do it. */
  569. vc->vt_newvt = -1;
  570. return 0;
  571. }
  572. /* The current vt has been released, so complete the switch. */
  573. newvt = vc->vt_newvt;
  574. vc->vt_newvt = -1;
  575. ret = vc_allocate(newvt);
  576. if (ret)
  577. return ret;
  578. /*
  579. * When we actually do the console switch, make sure we are atomic with
  580. * respect to other console switches..
  581. */
  582. complete_change_console(vc_cons[newvt].d);
  583. return 0;
  584. }
  585. static int vt_setactivate(struct vt_setactivate __user *sa)
  586. {
  587. struct vt_setactivate vsa;
  588. struct vc_data *nvc;
  589. int ret;
  590. if (copy_from_user(&vsa, sa, sizeof(vsa)))
  591. return -EFAULT;
  592. if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
  593. return -ENXIO;
  594. vsa.console--;
  595. vsa.console = array_index_nospec(vsa.console, MAX_NR_CONSOLES);
  596. console_lock();
  597. ret = vc_allocate(vsa.console);
  598. if (ret) {
  599. console_unlock();
  600. return ret;
  601. }
  602. /*
  603. * This is safe providing we don't drop the console sem between
  604. * vc_allocate and finishing referencing nvc.
  605. */
  606. nvc = vc_cons[vsa.console].d;
  607. nvc->vt_mode = vsa.mode;
  608. nvc->vt_mode.frsig = 0;
  609. put_pid(nvc->vt_pid);
  610. nvc->vt_pid = get_pid(task_pid(current));
  611. console_unlock();
  612. /* Commence switch and lock */
  613. /* Review set_console locks */
  614. set_console(vsa.console);
  615. return 0;
  616. }
  617. /* deallocate a single console, if possible (leave 0) */
  618. static int vt_disallocate(unsigned int vc_num)
  619. {
  620. struct vc_data *vc = NULL;
  621. int ret = 0;
  622. console_lock();
  623. if (vt_busy(vc_num))
  624. ret = -EBUSY;
  625. else if (vc_num)
  626. vc = vc_deallocate(vc_num);
  627. console_unlock();
  628. if (vc && vc_num >= MIN_NR_CONSOLES)
  629. tty_port_put(&vc->port);
  630. return ret;
  631. }
  632. /* deallocate all unused consoles, but leave 0 */
  633. static void vt_disallocate_all(void)
  634. {
  635. struct vc_data *vc[MAX_NR_CONSOLES];
  636. int i;
  637. console_lock();
  638. for (i = 1; i < MAX_NR_CONSOLES; i++)
  639. if (!vt_busy(i))
  640. vc[i] = vc_deallocate(i);
  641. else
  642. vc[i] = NULL;
  643. console_unlock();
  644. for (i = 1; i < MAX_NR_CONSOLES; i++) {
  645. if (vc[i] && i >= MIN_NR_CONSOLES)
  646. tty_port_put(&vc[i]->port);
  647. }
  648. }
  649. static int vt_resizex(struct vc_data *vc, struct vt_consize __user *cs)
  650. {
  651. struct vt_consize v;
  652. int i;
  653. if (copy_from_user(&v, cs, sizeof(struct vt_consize)))
  654. return -EFAULT;
  655. /* FIXME: Should check the copies properly */
  656. if (!v.v_vlin)
  657. v.v_vlin = vc->vc_scan_lines;
  658. if (v.v_clin) {
  659. int rows = v.v_vlin / v.v_clin;
  660. if (v.v_rows != rows) {
  661. if (v.v_rows) /* Parameters don't add up */
  662. return -EINVAL;
  663. v.v_rows = rows;
  664. }
  665. }
  666. if (v.v_vcol && v.v_ccol) {
  667. int cols = v.v_vcol / v.v_ccol;
  668. if (v.v_cols != cols) {
  669. if (v.v_cols)
  670. return -EINVAL;
  671. v.v_cols = cols;
  672. }
  673. }
  674. if (v.v_clin > 32)
  675. return -EINVAL;
  676. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  677. struct vc_data *vcp;
  678. if (!vc_cons[i].d)
  679. continue;
  680. console_lock();
  681. vcp = vc_cons[i].d;
  682. if (vcp) {
  683. int ret;
  684. int save_scan_lines = vcp->vc_scan_lines;
  685. int save_cell_height = vcp->vc_cell_height;
  686. if (v.v_vlin)
  687. vcp->vc_scan_lines = v.v_vlin;
  688. if (v.v_clin)
  689. vcp->vc_cell_height = v.v_clin;
  690. vcp->vc_resize_user = 1;
  691. ret = vc_resize(vcp, v.v_cols, v.v_rows);
  692. if (ret) {
  693. vcp->vc_scan_lines = save_scan_lines;
  694. vcp->vc_cell_height = save_cell_height;
  695. console_unlock();
  696. return ret;
  697. }
  698. }
  699. console_unlock();
  700. }
  701. return 0;
  702. }
  703. /*
  704. * We handle the console-specific ioctl's here. We allow the
  705. * capability to modify any console, not just the fg_console.
  706. */
  707. int vt_ioctl(struct tty_struct *tty,
  708. unsigned int cmd, unsigned long arg)
  709. {
  710. struct vc_data *vc = tty->driver_data;
  711. void __user *up = (void __user *)arg;
  712. int i, perm;
  713. int ret;
  714. /*
  715. * To have permissions to do most of the vt ioctls, we either have
  716. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  717. */
  718. perm = 0;
  719. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  720. perm = 1;
  721. ret = vt_k_ioctl(tty, cmd, arg, perm);
  722. if (ret != -ENOIOCTLCMD)
  723. return ret;
  724. ret = vt_io_ioctl(vc, cmd, up, perm);
  725. if (ret != -ENOIOCTLCMD)
  726. return ret;
  727. switch (cmd) {
  728. case TIOCLINUX:
  729. return tioclinux(tty, arg);
  730. case VT_SETMODE:
  731. {
  732. struct vt_mode tmp;
  733. if (!perm)
  734. return -EPERM;
  735. if (copy_from_user(&tmp, up, sizeof(struct vt_mode)))
  736. return -EFAULT;
  737. if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)
  738. return -EINVAL;
  739. console_lock();
  740. vc->vt_mode = tmp;
  741. /* the frsig is ignored, so we set it to 0 */
  742. vc->vt_mode.frsig = 0;
  743. put_pid(vc->vt_pid);
  744. vc->vt_pid = get_pid(task_pid(current));
  745. /* no switch is required -- saw@shade.msu.ru */
  746. vc->vt_newvt = -1;
  747. console_unlock();
  748. break;
  749. }
  750. case VT_GETMODE:
  751. {
  752. struct vt_mode tmp;
  753. int rc;
  754. console_lock();
  755. memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
  756. console_unlock();
  757. rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
  758. if (rc)
  759. return -EFAULT;
  760. break;
  761. }
  762. /*
  763. * Returns global vt state. Note that VT 0 is always open, since
  764. * it's an alias for the current VT, and people can't use it here.
  765. * We cannot return state for more than 16 VTs, since v_state is short.
  766. */
  767. case VT_GETSTATE:
  768. {
  769. struct vt_stat __user *vtstat = up;
  770. unsigned short state, mask;
  771. if (put_user(fg_console + 1, &vtstat->v_active))
  772. return -EFAULT;
  773. state = 1; /* /dev/tty0 is always open */
  774. console_lock(); /* required by vt_in_use() */
  775. for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
  776. ++i, mask <<= 1)
  777. if (vt_in_use(i))
  778. state |= mask;
  779. console_unlock();
  780. return put_user(state, &vtstat->v_state);
  781. }
  782. /*
  783. * Returns the first available (non-opened) console.
  784. */
  785. case VT_OPENQRY:
  786. console_lock(); /* required by vt_in_use() */
  787. for (i = 0; i < MAX_NR_CONSOLES; ++i)
  788. if (!vt_in_use(i))
  789. break;
  790. console_unlock();
  791. i = i < MAX_NR_CONSOLES ? (i+1) : -1;
  792. return put_user(i, (int __user *)arg);
  793. /*
  794. * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
  795. * with num >= 1 (switches to vt 0, our console, are not allowed, just
  796. * to preserve sanity).
  797. */
  798. case VT_ACTIVATE:
  799. if (!perm)
  800. return -EPERM;
  801. if (arg == 0 || arg > MAX_NR_CONSOLES)
  802. return -ENXIO;
  803. arg--;
  804. arg = array_index_nospec(arg, MAX_NR_CONSOLES);
  805. console_lock();
  806. ret = vc_allocate(arg);
  807. console_unlock();
  808. if (ret)
  809. return ret;
  810. set_console(arg);
  811. break;
  812. case VT_SETACTIVATE:
  813. if (!perm)
  814. return -EPERM;
  815. return vt_setactivate(up);
  816. /*
  817. * wait until the specified VT has been activated
  818. */
  819. case VT_WAITACTIVE:
  820. if (!perm)
  821. return -EPERM;
  822. if (arg == 0 || arg > MAX_NR_CONSOLES)
  823. return -ENXIO;
  824. return vt_waitactive(arg);
  825. /*
  826. * If a vt is under process control, the kernel will not switch to it
  827. * immediately, but postpone the operation until the process calls this
  828. * ioctl, allowing the switch to complete.
  829. *
  830. * According to the X sources this is the behavior:
  831. * 0: pending switch-from not OK
  832. * 1: pending switch-from OK
  833. * 2: completed switch-to OK
  834. */
  835. case VT_RELDISP:
  836. if (!perm)
  837. return -EPERM;
  838. console_lock();
  839. ret = vt_reldisp(vc, arg);
  840. console_unlock();
  841. return ret;
  842. /*
  843. * Disallocate memory associated to VT (but leave VT1)
  844. */
  845. case VT_DISALLOCATE:
  846. if (arg > MAX_NR_CONSOLES)
  847. return -ENXIO;
  848. if (arg == 0)
  849. vt_disallocate_all();
  850. else
  851. return vt_disallocate(--arg);
  852. break;
  853. case VT_RESIZE:
  854. {
  855. struct vt_sizes __user *vtsizes = up;
  856. struct vc_data *vc;
  857. ushort ll,cc;
  858. if (!perm)
  859. return -EPERM;
  860. if (get_user(ll, &vtsizes->v_rows) ||
  861. get_user(cc, &vtsizes->v_cols))
  862. return -EFAULT;
  863. console_lock();
  864. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  865. vc = vc_cons[i].d;
  866. if (vc) {
  867. vc->vc_resize_user = 1;
  868. /* FIXME: review v tty lock */
  869. vc_resize(vc_cons[i].d, cc, ll);
  870. }
  871. }
  872. console_unlock();
  873. break;
  874. }
  875. case VT_RESIZEX:
  876. if (!perm)
  877. return -EPERM;
  878. return vt_resizex(vc, up);
  879. case VT_LOCKSWITCH:
  880. if (!capable(CAP_SYS_TTY_CONFIG))
  881. return -EPERM;
  882. vt_dont_switch = true;
  883. break;
  884. case VT_UNLOCKSWITCH:
  885. if (!capable(CAP_SYS_TTY_CONFIG))
  886. return -EPERM;
  887. vt_dont_switch = false;
  888. break;
  889. case VT_GETHIFONTMASK:
  890. return put_user(vc->vc_hi_font_mask,
  891. (unsigned short __user *)arg);
  892. case VT_WAITEVENT:
  893. return vt_event_wait_ioctl((struct vt_event __user *)arg);
  894. default:
  895. return -ENOIOCTLCMD;
  896. }
  897. return 0;
  898. }
  899. void reset_vc(struct vc_data *vc)
  900. {
  901. vc->vc_mode = KD_TEXT;
  902. vt_reset_unicode(vc->vc_num);
  903. vc->vt_mode.mode = VT_AUTO;
  904. vc->vt_mode.waitv = 0;
  905. vc->vt_mode.relsig = 0;
  906. vc->vt_mode.acqsig = 0;
  907. vc->vt_mode.frsig = 0;
  908. put_pid(vc->vt_pid);
  909. vc->vt_pid = NULL;
  910. vc->vt_newvt = -1;
  911. if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
  912. reset_palette(vc);
  913. }
  914. void vc_SAK(struct work_struct *work)
  915. {
  916. struct vc *vc_con =
  917. container_of(work, struct vc, SAK_work);
  918. struct vc_data *vc;
  919. struct tty_struct *tty;
  920. console_lock();
  921. vc = vc_con->d;
  922. if (vc) {
  923. /* FIXME: review tty ref counting */
  924. tty = vc->port.tty;
  925. /*
  926. * SAK should also work in all raw modes and reset
  927. * them properly.
  928. */
  929. if (tty)
  930. __do_SAK(tty);
  931. reset_vc(vc);
  932. }
  933. console_unlock();
  934. }
  935. #ifdef CONFIG_COMPAT
  936. struct compat_consolefontdesc {
  937. unsigned short charcount; /* characters in font (256 or 512) */
  938. unsigned short charheight; /* scan lines per character (1-32) */
  939. compat_caddr_t chardata; /* font data in expanded form */
  940. };
  941. static inline int
  942. compat_fontx_ioctl(struct vc_data *vc, int cmd,
  943. struct compat_consolefontdesc __user *user_cfd,
  944. int perm, struct console_font_op *op)
  945. {
  946. struct compat_consolefontdesc cfdarg;
  947. int i;
  948. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
  949. return -EFAULT;
  950. switch (cmd) {
  951. case PIO_FONTX:
  952. if (!perm)
  953. return -EPERM;
  954. op->op = KD_FONT_OP_SET;
  955. op->flags = KD_FONT_FLAG_OLD;
  956. op->width = 8;
  957. op->height = cfdarg.charheight;
  958. op->charcount = cfdarg.charcount;
  959. op->data = compat_ptr(cfdarg.chardata);
  960. return con_font_op(vc, op);
  961. case GIO_FONTX:
  962. op->op = KD_FONT_OP_GET;
  963. op->flags = KD_FONT_FLAG_OLD;
  964. op->width = 8;
  965. op->height = cfdarg.charheight;
  966. op->charcount = cfdarg.charcount;
  967. op->data = compat_ptr(cfdarg.chardata);
  968. i = con_font_op(vc, op);
  969. if (i)
  970. return i;
  971. cfdarg.charheight = op->height;
  972. cfdarg.charcount = op->charcount;
  973. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
  974. return -EFAULT;
  975. return 0;
  976. }
  977. return -EINVAL;
  978. }
  979. struct compat_console_font_op {
  980. compat_uint_t op; /* operation code KD_FONT_OP_* */
  981. compat_uint_t flags; /* KD_FONT_FLAG_* */
  982. compat_uint_t width, height; /* font size */
  983. compat_uint_t charcount;
  984. compat_caddr_t data; /* font data with height fixed to 32 */
  985. };
  986. static inline int
  987. compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
  988. int perm, struct console_font_op *op, struct vc_data *vc)
  989. {
  990. int i;
  991. if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
  992. return -EFAULT;
  993. if (!perm && op->op != KD_FONT_OP_GET)
  994. return -EPERM;
  995. op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
  996. i = con_font_op(vc, op);
  997. if (i)
  998. return i;
  999. ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
  1000. if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
  1001. return -EFAULT;
  1002. return 0;
  1003. }
  1004. struct compat_unimapdesc {
  1005. unsigned short entry_ct;
  1006. compat_caddr_t entries;
  1007. };
  1008. static inline int
  1009. compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
  1010. int perm, struct vc_data *vc)
  1011. {
  1012. struct compat_unimapdesc tmp;
  1013. struct unipair __user *tmp_entries;
  1014. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  1015. return -EFAULT;
  1016. tmp_entries = compat_ptr(tmp.entries);
  1017. switch (cmd) {
  1018. case PIO_UNIMAP:
  1019. if (!perm)
  1020. return -EPERM;
  1021. return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
  1022. case GIO_UNIMAP:
  1023. if (!perm && fg_console != vc->vc_num)
  1024. return -EPERM;
  1025. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
  1026. }
  1027. return 0;
  1028. }
  1029. long vt_compat_ioctl(struct tty_struct *tty,
  1030. unsigned int cmd, unsigned long arg)
  1031. {
  1032. struct vc_data *vc = tty->driver_data;
  1033. struct console_font_op op; /* used in multiple places here */
  1034. void __user *up = compat_ptr(arg);
  1035. int perm;
  1036. /*
  1037. * To have permissions to do most of the vt ioctls, we either have
  1038. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  1039. */
  1040. perm = 0;
  1041. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  1042. perm = 1;
  1043. switch (cmd) {
  1044. /*
  1045. * these need special handlers for incompatible data structures
  1046. */
  1047. case PIO_FONTX:
  1048. case GIO_FONTX:
  1049. return compat_fontx_ioctl(vc, cmd, up, perm, &op);
  1050. case KDFONTOP:
  1051. return compat_kdfontop_ioctl(up, perm, &op, vc);
  1052. case PIO_UNIMAP:
  1053. case GIO_UNIMAP:
  1054. return compat_unimap_ioctl(cmd, up, perm, vc);
  1055. /*
  1056. * all these treat 'arg' as an integer
  1057. */
  1058. case KIOCSOUND:
  1059. case KDMKTONE:
  1060. #ifdef CONFIG_X86
  1061. case KDADDIO:
  1062. case KDDELIO:
  1063. #endif
  1064. case KDSETMODE:
  1065. case KDMAPDISP:
  1066. case KDUNMAPDISP:
  1067. case KDSKBMODE:
  1068. case KDSKBMETA:
  1069. case KDSKBLED:
  1070. case KDSETLED:
  1071. case KDSIGACCEPT:
  1072. case VT_ACTIVATE:
  1073. case VT_WAITACTIVE:
  1074. case VT_RELDISP:
  1075. case VT_DISALLOCATE:
  1076. case VT_RESIZE:
  1077. case VT_RESIZEX:
  1078. return vt_ioctl(tty, cmd, arg);
  1079. /*
  1080. * the rest has a compatible data structure behind arg,
  1081. * but we have to convert it to a proper 64 bit pointer.
  1082. */
  1083. default:
  1084. return vt_ioctl(tty, cmd, (unsigned long)up);
  1085. }
  1086. }
  1087. #endif /* CONFIG_COMPAT */
  1088. /*
  1089. * Performs the back end of a vt switch. Called under the console
  1090. * semaphore.
  1091. */
  1092. static void complete_change_console(struct vc_data *vc)
  1093. {
  1094. unsigned char old_vc_mode;
  1095. int old = fg_console;
  1096. last_console = fg_console;
  1097. /*
  1098. * If we're switching, we could be going from KD_GRAPHICS to
  1099. * KD_TEXT mode or vice versa, which means we need to blank or
  1100. * unblank the screen later.
  1101. */
  1102. old_vc_mode = vc_cons[fg_console].d->vc_mode;
  1103. switch_screen(vc);
  1104. /*
  1105. * This can't appear below a successful kill_pid(). If it did,
  1106. * then the *blank_screen operation could occur while X, having
  1107. * received acqsig, is waking up on another processor. This
  1108. * condition can lead to overlapping accesses to the VGA range
  1109. * and the framebuffer (causing system lockups).
  1110. *
  1111. * To account for this we duplicate this code below only if the
  1112. * controlling process is gone and we've called reset_vc.
  1113. */
  1114. if (old_vc_mode != vc->vc_mode) {
  1115. if (vc->vc_mode == KD_TEXT)
  1116. do_unblank_screen(1);
  1117. else
  1118. do_blank_screen(1);
  1119. }
  1120. /*
  1121. * If this new console is under process control, send it a signal
  1122. * telling it that it has acquired. Also check if it has died and
  1123. * clean up (similar to logic employed in change_console())
  1124. */
  1125. if (vc->vt_mode.mode == VT_PROCESS) {
  1126. /*
  1127. * Send the signal as privileged - kill_pid() will
  1128. * tell us if the process has gone or something else
  1129. * is awry
  1130. */
  1131. if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
  1132. /*
  1133. * The controlling process has died, so we revert back to
  1134. * normal operation. In this case, we'll also change back
  1135. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1136. * but it saves the agony when the X server dies and the screen
  1137. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1138. * this outside of VT_PROCESS but there is no single process
  1139. * to account for and tracking tty count may be undesirable.
  1140. */
  1141. reset_vc(vc);
  1142. if (old_vc_mode != vc->vc_mode) {
  1143. if (vc->vc_mode == KD_TEXT)
  1144. do_unblank_screen(1);
  1145. else
  1146. do_blank_screen(1);
  1147. }
  1148. }
  1149. }
  1150. /*
  1151. * Wake anyone waiting for their VT to activate
  1152. */
  1153. vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
  1154. return;
  1155. }
  1156. /*
  1157. * Performs the front-end of a vt switch
  1158. */
  1159. void change_console(struct vc_data *new_vc)
  1160. {
  1161. struct vc_data *vc;
  1162. if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
  1163. return;
  1164. /*
  1165. * If this vt is in process mode, then we need to handshake with
  1166. * that process before switching. Essentially, we store where that
  1167. * vt wants to switch to and wait for it to tell us when it's done
  1168. * (via VT_RELDISP ioctl).
  1169. *
  1170. * We also check to see if the controlling process still exists.
  1171. * If it doesn't, we reset this vt to auto mode and continue.
  1172. * This is a cheap way to track process control. The worst thing
  1173. * that can happen is: we send a signal to a process, it dies, and
  1174. * the switch gets "lost" waiting for a response; hopefully, the
  1175. * user will try again, we'll detect the process is gone (unless
  1176. * the user waits just the right amount of time :-) and revert the
  1177. * vt to auto control.
  1178. */
  1179. vc = vc_cons[fg_console].d;
  1180. if (vc->vt_mode.mode == VT_PROCESS) {
  1181. /*
  1182. * Send the signal as privileged - kill_pid() will
  1183. * tell us if the process has gone or something else
  1184. * is awry.
  1185. *
  1186. * We need to set vt_newvt *before* sending the signal or we
  1187. * have a race.
  1188. */
  1189. vc->vt_newvt = new_vc->vc_num;
  1190. if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
  1191. /*
  1192. * It worked. Mark the vt to switch to and
  1193. * return. The process needs to send us a
  1194. * VT_RELDISP ioctl to complete the switch.
  1195. */
  1196. return;
  1197. }
  1198. /*
  1199. * The controlling process has died, so we revert back to
  1200. * normal operation. In this case, we'll also change back
  1201. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1202. * but it saves the agony when the X server dies and the screen
  1203. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1204. * this outside of VT_PROCESS but there is no single process
  1205. * to account for and tracking tty count may be undesirable.
  1206. */
  1207. reset_vc(vc);
  1208. /*
  1209. * Fall through to normal (VT_AUTO) handling of the switch...
  1210. */
  1211. }
  1212. /*
  1213. * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
  1214. */
  1215. if (vc->vc_mode == KD_GRAPHICS)
  1216. return;
  1217. complete_change_console(new_vc);
  1218. }
  1219. /* Perform a kernel triggered VT switch for suspend/resume */
  1220. static int disable_vt_switch;
  1221. int vt_move_to_console(unsigned int vt, int alloc)
  1222. {
  1223. int prev;
  1224. console_lock();
  1225. /* Graphics mode - up to X */
  1226. if (disable_vt_switch) {
  1227. console_unlock();
  1228. return 0;
  1229. }
  1230. prev = fg_console;
  1231. if (alloc && vc_allocate(vt)) {
  1232. /* we can't have a free VC for now. Too bad,
  1233. * we don't want to mess the screen for now. */
  1234. console_unlock();
  1235. return -ENOSPC;
  1236. }
  1237. if (set_console(vt)) {
  1238. /*
  1239. * We're unable to switch to the SUSPEND_CONSOLE.
  1240. * Let the calling function know so it can decide
  1241. * what to do.
  1242. */
  1243. console_unlock();
  1244. return -EIO;
  1245. }
  1246. console_unlock();
  1247. if (vt_waitactive(vt + 1)) {
  1248. pr_debug("Suspend: Can't switch VCs.");
  1249. return -EINTR;
  1250. }
  1251. return prev;
  1252. }
  1253. /*
  1254. * Normally during a suspend, we allocate a new console and switch to it.
  1255. * When we resume, we switch back to the original console. This switch
  1256. * can be slow, so on systems where the framebuffer can handle restoration
  1257. * of video registers anyways, there's little point in doing the console
  1258. * switch. This function allows you to disable it by passing it '0'.
  1259. */
  1260. void pm_set_vt_switch(int do_switch)
  1261. {
  1262. console_lock();
  1263. disable_vt_switch = !do_switch;
  1264. console_unlock();
  1265. }
  1266. EXPORT_SYMBOL(pm_set_vt_switch);