vt_ioctl.c 33 KB

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