vt_ioctl.c 34 KB

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