vt_ioctl.c 28 KB

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