vt_ioctl.c 30 KB

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