vt_ioctl.c 28 KB

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