vt_ioctl.c 28 KB

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