vt_ioctl.c 28 KB

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