vt_ioctl.c 33 KB

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