vt_ioctl.c 33 KB

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