vt_ioctl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251
  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. 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. /* deallocate 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 = 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 = 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. if (!perm || !capable(CAP_KILL))
  582. return -EPERM;
  583. if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
  584. return -EINVAL;
  585. spin_lock_irq(&vt_spawn_con.lock);
  586. put_pid(vt_spawn_con.pid);
  587. vt_spawn_con.pid = get_pid(task_pid(current));
  588. vt_spawn_con.sig = arg;
  589. spin_unlock_irq(&vt_spawn_con.lock);
  590. return 0;
  591. }
  592. case VT_SETMODE:
  593. {
  594. struct vt_mode tmp;
  595. if (!perm)
  596. return -EPERM;
  597. if (copy_from_user(&tmp, up, sizeof(struct vt_mode)))
  598. return -EFAULT;
  599. if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)
  600. return -EINVAL;
  601. acquire_console_sem();
  602. vc->vt_mode = tmp;
  603. /* the frsig is ignored, so we set it to 0 */
  604. vc->vt_mode.frsig = 0;
  605. put_pid(vc->vt_pid);
  606. vc->vt_pid = get_pid(task_pid(current));
  607. /* no switch is required -- saw@shade.msu.ru */
  608. vc->vt_newvt = -1;
  609. release_console_sem();
  610. return 0;
  611. }
  612. case VT_GETMODE:
  613. {
  614. struct vt_mode tmp;
  615. int rc;
  616. acquire_console_sem();
  617. memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
  618. release_console_sem();
  619. rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
  620. return rc ? -EFAULT : 0;
  621. }
  622. /*
  623. * Returns global vt state. Note that VT 0 is always open, since
  624. * it's an alias for the current VT, and people can't use it here.
  625. * We cannot return state for more than 16 VTs, since v_state is short.
  626. */
  627. case VT_GETSTATE:
  628. {
  629. struct vt_stat __user *vtstat = up;
  630. unsigned short state, mask;
  631. if (put_user(fg_console + 1, &vtstat->v_active))
  632. return -EFAULT;
  633. state = 1; /* /dev/tty0 is always open */
  634. for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; ++i, mask <<= 1)
  635. if (VT_IS_IN_USE(i))
  636. state |= mask;
  637. return put_user(state, &vtstat->v_state);
  638. }
  639. /*
  640. * Returns the first available (non-opened) console.
  641. */
  642. case VT_OPENQRY:
  643. for (i = 0; i < MAX_NR_CONSOLES; ++i)
  644. if (! VT_IS_IN_USE(i))
  645. break;
  646. ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
  647. goto setint;
  648. /*
  649. * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
  650. * with num >= 1 (switches to vt 0, our console, are not allowed, just
  651. * to preserve sanity).
  652. */
  653. case VT_ACTIVATE:
  654. if (!perm)
  655. return -EPERM;
  656. if (arg == 0 || arg > MAX_NR_CONSOLES)
  657. return -ENXIO;
  658. arg--;
  659. acquire_console_sem();
  660. i = vc_allocate(arg);
  661. release_console_sem();
  662. if (i)
  663. return i;
  664. set_console(arg);
  665. return 0;
  666. /*
  667. * wait until the specified VT has been activated
  668. */
  669. case VT_WAITACTIVE:
  670. if (!perm)
  671. return -EPERM;
  672. if (arg == 0 || arg > MAX_NR_CONSOLES)
  673. return -ENXIO;
  674. return vt_waitactive(arg-1);
  675. /*
  676. * If a vt is under process control, the kernel will not switch to it
  677. * immediately, but postpone the operation until the process calls this
  678. * ioctl, allowing the switch to complete.
  679. *
  680. * According to the X sources this is the behavior:
  681. * 0: pending switch-from not OK
  682. * 1: pending switch-from OK
  683. * 2: completed switch-to OK
  684. */
  685. case VT_RELDISP:
  686. if (!perm)
  687. return -EPERM;
  688. if (vc->vt_mode.mode != VT_PROCESS)
  689. return -EINVAL;
  690. /*
  691. * Switching-from response
  692. */
  693. acquire_console_sem();
  694. if (vc->vt_newvt >= 0) {
  695. if (arg == 0)
  696. /*
  697. * Switch disallowed, so forget we were trying
  698. * to do it.
  699. */
  700. vc->vt_newvt = -1;
  701. else {
  702. /*
  703. * The current vt has been released, so
  704. * complete the switch.
  705. */
  706. int newvt;
  707. newvt = vc->vt_newvt;
  708. vc->vt_newvt = -1;
  709. i = vc_allocate(newvt);
  710. if (i) {
  711. release_console_sem();
  712. return i;
  713. }
  714. /*
  715. * When we actually do the console switch,
  716. * make sure we are atomic with respect to
  717. * other console switches..
  718. */
  719. complete_change_console(vc_cons[newvt].d);
  720. }
  721. }
  722. /*
  723. * Switched-to response
  724. */
  725. else
  726. {
  727. /*
  728. * If it's just an ACK, ignore it
  729. */
  730. if (arg != VT_ACKACQ) {
  731. release_console_sem();
  732. return -EINVAL;
  733. }
  734. }
  735. release_console_sem();
  736. return 0;
  737. /*
  738. * Disallocate memory associated to VT (but leave VT1)
  739. */
  740. case VT_DISALLOCATE:
  741. if (arg > MAX_NR_CONSOLES)
  742. return -ENXIO;
  743. if (arg == 0) {
  744. /* deallocate all unused consoles, but leave 0 */
  745. acquire_console_sem();
  746. for (i=1; i<MAX_NR_CONSOLES; i++)
  747. if (! VT_BUSY(i))
  748. vc_deallocate(i);
  749. release_console_sem();
  750. } else {
  751. /* deallocate a single console, if possible */
  752. arg--;
  753. if (VT_BUSY(arg))
  754. return -EBUSY;
  755. if (arg) { /* leave 0 */
  756. acquire_console_sem();
  757. vc_deallocate(arg);
  758. release_console_sem();
  759. }
  760. }
  761. return 0;
  762. case VT_RESIZE:
  763. {
  764. struct vt_sizes __user *vtsizes = up;
  765. ushort ll,cc;
  766. if (!perm)
  767. return -EPERM;
  768. if (get_user(ll, &vtsizes->v_rows) ||
  769. get_user(cc, &vtsizes->v_cols))
  770. return -EFAULT;
  771. for (i = 0; i < MAX_NR_CONSOLES; i++)
  772. vc_lock_resize(vc_cons[i].d, cc, ll);
  773. return 0;
  774. }
  775. case VT_RESIZEX:
  776. {
  777. struct vt_consize __user *vtconsize = up;
  778. ushort ll,cc,vlin,clin,vcol,ccol;
  779. if (!perm)
  780. return -EPERM;
  781. if (!access_ok(VERIFY_READ, vtconsize,
  782. sizeof(struct vt_consize)))
  783. return -EFAULT;
  784. __get_user(ll, &vtconsize->v_rows);
  785. __get_user(cc, &vtconsize->v_cols);
  786. __get_user(vlin, &vtconsize->v_vlin);
  787. __get_user(clin, &vtconsize->v_clin);
  788. __get_user(vcol, &vtconsize->v_vcol);
  789. __get_user(ccol, &vtconsize->v_ccol);
  790. vlin = vlin ? vlin : vc->vc_scan_lines;
  791. if (clin) {
  792. if (ll) {
  793. if (ll != vlin/clin)
  794. return -EINVAL; /* Parameters don't add up */
  795. } else
  796. ll = vlin/clin;
  797. }
  798. if (vcol && ccol) {
  799. if (cc) {
  800. if (cc != vcol/ccol)
  801. return -EINVAL;
  802. } else
  803. cc = vcol/ccol;
  804. }
  805. if (clin > 32)
  806. return -EINVAL;
  807. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  808. if (!vc_cons[i].d)
  809. continue;
  810. acquire_console_sem();
  811. if (vlin)
  812. vc_cons[i].d->vc_scan_lines = vlin;
  813. if (clin)
  814. vc_cons[i].d->vc_font.height = clin;
  815. vc_resize(vc_cons[i].d, cc, ll);
  816. release_console_sem();
  817. }
  818. return 0;
  819. }
  820. case PIO_FONT: {
  821. if (!perm)
  822. return -EPERM;
  823. op.op = KD_FONT_OP_SET;
  824. op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
  825. op.width = 8;
  826. op.height = 0;
  827. op.charcount = 256;
  828. op.data = up;
  829. return con_font_op(vc_cons[fg_console].d, &op);
  830. }
  831. case GIO_FONT: {
  832. op.op = KD_FONT_OP_GET;
  833. op.flags = KD_FONT_FLAG_OLD;
  834. op.width = 8;
  835. op.height = 32;
  836. op.charcount = 256;
  837. op.data = up;
  838. return con_font_op(vc_cons[fg_console].d, &op);
  839. }
  840. case PIO_CMAP:
  841. if (!perm)
  842. return -EPERM;
  843. return con_set_cmap(up);
  844. case GIO_CMAP:
  845. return con_get_cmap(up);
  846. case PIO_FONTX:
  847. case GIO_FONTX:
  848. return do_fontx_ioctl(cmd, up, perm, &op);
  849. case PIO_FONTRESET:
  850. {
  851. if (!perm)
  852. return -EPERM;
  853. #ifdef BROKEN_GRAPHICS_PROGRAMS
  854. /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
  855. font is not saved. */
  856. return -ENOSYS;
  857. #else
  858. {
  859. op.op = KD_FONT_OP_SET_DEFAULT;
  860. op.data = NULL;
  861. i = con_font_op(vc_cons[fg_console].d, &op);
  862. if (i)
  863. return i;
  864. con_set_default_unimap(vc_cons[fg_console].d);
  865. return 0;
  866. }
  867. #endif
  868. }
  869. case KDFONTOP: {
  870. if (copy_from_user(&op, up, sizeof(op)))
  871. return -EFAULT;
  872. if (!perm && op.op != KD_FONT_OP_GET)
  873. return -EPERM;
  874. i = con_font_op(vc, &op);
  875. if (i) return i;
  876. if (copy_to_user(up, &op, sizeof(op)))
  877. return -EFAULT;
  878. return 0;
  879. }
  880. case PIO_SCRNMAP:
  881. if (!perm)
  882. return -EPERM;
  883. return con_set_trans_old(up);
  884. case GIO_SCRNMAP:
  885. return con_get_trans_old(up);
  886. case PIO_UNISCRNMAP:
  887. if (!perm)
  888. return -EPERM;
  889. return con_set_trans_new(up);
  890. case GIO_UNISCRNMAP:
  891. return con_get_trans_new(up);
  892. case PIO_UNIMAPCLR:
  893. { struct unimapinit ui;
  894. if (!perm)
  895. return -EPERM;
  896. i = copy_from_user(&ui, up, sizeof(struct unimapinit));
  897. if (i) return -EFAULT;
  898. con_clear_unimap(vc, &ui);
  899. return 0;
  900. }
  901. case PIO_UNIMAP:
  902. case GIO_UNIMAP:
  903. return do_unimap_ioctl(cmd, up, perm, vc);
  904. case VT_LOCKSWITCH:
  905. if (!capable(CAP_SYS_TTY_CONFIG))
  906. return -EPERM;
  907. vt_dont_switch = 1;
  908. return 0;
  909. case VT_UNLOCKSWITCH:
  910. if (!capable(CAP_SYS_TTY_CONFIG))
  911. return -EPERM;
  912. vt_dont_switch = 0;
  913. return 0;
  914. case VT_GETHIFONTMASK:
  915. return put_user(vc->vc_hi_font_mask, (unsigned short __user *)arg);
  916. default:
  917. return -ENOIOCTLCMD;
  918. }
  919. }
  920. /*
  921. * Sometimes we want to wait until a particular VT has been activated. We
  922. * do it in a very simple manner. Everybody waits on a single queue and
  923. * get woken up at once. Those that are satisfied go on with their business,
  924. * while those not ready go back to sleep. Seems overkill to add a wait
  925. * to each vt just for this - usually this does nothing!
  926. */
  927. static DECLARE_WAIT_QUEUE_HEAD(vt_activate_queue);
  928. /*
  929. * Sleeps until a vt is activated, or the task is interrupted. Returns
  930. * 0 if activation, -EINTR if interrupted by a signal handler.
  931. */
  932. int vt_waitactive(int vt)
  933. {
  934. int retval;
  935. DECLARE_WAITQUEUE(wait, current);
  936. add_wait_queue(&vt_activate_queue, &wait);
  937. for (;;) {
  938. retval = 0;
  939. /*
  940. * Synchronize with redraw_screen(). By acquiring the console
  941. * semaphore we make sure that the console switch is completed
  942. * before we return. If we didn't wait for the semaphore, we
  943. * could return at a point where fg_console has already been
  944. * updated, but the console switch hasn't been completed.
  945. */
  946. acquire_console_sem();
  947. set_current_state(TASK_INTERRUPTIBLE);
  948. if (vt == fg_console) {
  949. release_console_sem();
  950. break;
  951. }
  952. release_console_sem();
  953. retval = -ERESTARTNOHAND;
  954. if (signal_pending(current))
  955. break;
  956. schedule();
  957. }
  958. remove_wait_queue(&vt_activate_queue, &wait);
  959. __set_current_state(TASK_RUNNING);
  960. return retval;
  961. }
  962. #define vt_wake_waitactive() wake_up(&vt_activate_queue)
  963. void reset_vc(struct vc_data *vc)
  964. {
  965. vc->vc_mode = KD_TEXT;
  966. kbd_table[vc->vc_num].kbdmode = VC_XLATE;
  967. vc->vt_mode.mode = VT_AUTO;
  968. vc->vt_mode.waitv = 0;
  969. vc->vt_mode.relsig = 0;
  970. vc->vt_mode.acqsig = 0;
  971. vc->vt_mode.frsig = 0;
  972. put_pid(vc->vt_pid);
  973. vc->vt_pid = NULL;
  974. vc->vt_newvt = -1;
  975. if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
  976. reset_palette(vc);
  977. }
  978. void vc_SAK(struct work_struct *work)
  979. {
  980. struct vc *vc_con =
  981. container_of(work, struct vc, SAK_work);
  982. struct vc_data *vc;
  983. struct tty_struct *tty;
  984. acquire_console_sem();
  985. vc = vc_con->d;
  986. if (vc) {
  987. tty = vc->vc_tty;
  988. /*
  989. * SAK should also work in all raw modes and reset
  990. * them properly.
  991. */
  992. if (tty)
  993. __do_SAK(tty);
  994. reset_vc(vc);
  995. }
  996. release_console_sem();
  997. }
  998. /*
  999. * Performs the back end of a vt switch
  1000. */
  1001. static void complete_change_console(struct vc_data *vc)
  1002. {
  1003. unsigned char old_vc_mode;
  1004. last_console = fg_console;
  1005. /*
  1006. * If we're switching, we could be going from KD_GRAPHICS to
  1007. * KD_TEXT mode or vice versa, which means we need to blank or
  1008. * unblank the screen later.
  1009. */
  1010. old_vc_mode = vc_cons[fg_console].d->vc_mode;
  1011. switch_screen(vc);
  1012. /*
  1013. * This can't appear below a successful kill_pid(). If it did,
  1014. * then the *blank_screen operation could occur while X, having
  1015. * received acqsig, is waking up on another processor. This
  1016. * condition can lead to overlapping accesses to the VGA range
  1017. * and the framebuffer (causing system lockups).
  1018. *
  1019. * To account for this we duplicate this code below only if the
  1020. * controlling process is gone and we've called reset_vc.
  1021. */
  1022. if (old_vc_mode != vc->vc_mode) {
  1023. if (vc->vc_mode == KD_TEXT)
  1024. do_unblank_screen(1);
  1025. else
  1026. do_blank_screen(1);
  1027. }
  1028. /*
  1029. * If this new console is under process control, send it a signal
  1030. * telling it that it has acquired. Also check if it has died and
  1031. * clean up (similar to logic employed in change_console())
  1032. */
  1033. if (vc->vt_mode.mode == VT_PROCESS) {
  1034. /*
  1035. * Send the signal as privileged - kill_pid() will
  1036. * tell us if the process has gone or something else
  1037. * is awry
  1038. */
  1039. if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
  1040. /*
  1041. * The controlling process has died, so we revert back to
  1042. * normal operation. In this case, we'll also change back
  1043. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1044. * but it saves the agony when the X server dies and the screen
  1045. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1046. * this outside of VT_PROCESS but there is no single process
  1047. * to account for and tracking tty count may be undesirable.
  1048. */
  1049. reset_vc(vc);
  1050. if (old_vc_mode != vc->vc_mode) {
  1051. if (vc->vc_mode == KD_TEXT)
  1052. do_unblank_screen(1);
  1053. else
  1054. do_blank_screen(1);
  1055. }
  1056. }
  1057. }
  1058. /*
  1059. * Wake anyone waiting for their VT to activate
  1060. */
  1061. vt_wake_waitactive();
  1062. return;
  1063. }
  1064. /*
  1065. * Performs the front-end of a vt switch
  1066. */
  1067. void change_console(struct vc_data *new_vc)
  1068. {
  1069. struct vc_data *vc;
  1070. if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
  1071. return;
  1072. /*
  1073. * If this vt is in process mode, then we need to handshake with
  1074. * that process before switching. Essentially, we store where that
  1075. * vt wants to switch to and wait for it to tell us when it's done
  1076. * (via VT_RELDISP ioctl).
  1077. *
  1078. * We also check to see if the controlling process still exists.
  1079. * If it doesn't, we reset this vt to auto mode and continue.
  1080. * This is a cheap way to track process control. The worst thing
  1081. * that can happen is: we send a signal to a process, it dies, and
  1082. * the switch gets "lost" waiting for a response; hopefully, the
  1083. * user will try again, we'll detect the process is gone (unless
  1084. * the user waits just the right amount of time :-) and revert the
  1085. * vt to auto control.
  1086. */
  1087. vc = vc_cons[fg_console].d;
  1088. if (vc->vt_mode.mode == VT_PROCESS) {
  1089. /*
  1090. * Send the signal as privileged - kill_pid() will
  1091. * tell us if the process has gone or something else
  1092. * is awry.
  1093. *
  1094. * We need to set vt_newvt *before* sending the signal or we
  1095. * have a race.
  1096. */
  1097. vc->vt_newvt = new_vc->vc_num;
  1098. if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
  1099. /*
  1100. * It worked. Mark the vt to switch to and
  1101. * return. The process needs to send us a
  1102. * VT_RELDISP ioctl to complete the switch.
  1103. */
  1104. return;
  1105. }
  1106. /*
  1107. * The controlling process has died, so we revert back to
  1108. * normal operation. In this case, we'll also change back
  1109. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1110. * but it saves the agony when the X server dies and the screen
  1111. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1112. * this outside of VT_PROCESS but there is no single process
  1113. * to account for and tracking tty count may be undesirable.
  1114. */
  1115. reset_vc(vc);
  1116. /*
  1117. * Fall through to normal (VT_AUTO) handling of the switch...
  1118. */
  1119. }
  1120. /*
  1121. * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
  1122. */
  1123. if (vc->vc_mode == KD_GRAPHICS)
  1124. return;
  1125. complete_change_console(new_vc);
  1126. }