vt_ioctl.c 28 KB

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