vt_ioctl.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782
  1. /*
  2. * linux/drivers/char/vt_ioctl.c
  3. *
  4. * Copyright (C) 1992 obz under the linux copyright
  5. *
  6. * Dynamic diacritical handling - aeb@cwi.nl - Dec 1993
  7. * Dynamic keymap and string allocation - aeb@cwi.nl - May 1994
  8. * Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995
  9. * Some code moved for less code duplication - Andi Kleen - Mar 1997
  10. * Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001
  11. */
  12. #include <linux/types.h>
  13. #include <linux/errno.h>
  14. #include <linux/sched.h>
  15. #include <linux/tty.h>
  16. #include <linux/timer.h>
  17. #include <linux/kernel.h>
  18. #include <linux/compat.h>
  19. #include <linux/module.h>
  20. #include <linux/kd.h>
  21. #include <linux/vt.h>
  22. #include <linux/string.h>
  23. #include <linux/slab.h>
  24. #include <linux/major.h>
  25. #include <linux/fs.h>
  26. #include <linux/console.h>
  27. #include <linux/consolemap.h>
  28. #include <linux/signal.h>
  29. #include <linux/smp_lock.h>
  30. #include <linux/timex.h>
  31. #include <asm/io.h>
  32. #include <asm/uaccess.h>
  33. #include <linux/kbd_kern.h>
  34. #include <linux/vt_kern.h>
  35. #include <linux/kbd_diacr.h>
  36. #include <linux/selection.h>
  37. char vt_dont_switch;
  38. extern struct tty_driver *console_driver;
  39. #define VT_IS_IN_USE(i) (console_driver->ttys[i] && console_driver->ttys[i]->count)
  40. #define VT_BUSY(i) (VT_IS_IN_USE(i) || i == fg_console || vc_cons[i].d == sel_cons)
  41. /*
  42. * Console (vt and kd) routines, as defined by USL SVR4 manual, and by
  43. * experimentation and study of X386 SYSV handling.
  44. *
  45. * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and
  46. * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console,
  47. * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will
  48. * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to
  49. * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using
  50. * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing
  51. * to the current console is done by the main ioctl code.
  52. */
  53. #ifdef CONFIG_X86
  54. #include <linux/syscalls.h>
  55. #endif
  56. static void complete_change_console(struct vc_data *vc);
  57. /*
  58. * User space VT_EVENT handlers
  59. */
  60. struct vt_event_wait {
  61. struct list_head list;
  62. struct vt_event event;
  63. int done;
  64. };
  65. static LIST_HEAD(vt_events);
  66. static DEFINE_SPINLOCK(vt_event_lock);
  67. static DECLARE_WAIT_QUEUE_HEAD(vt_event_waitqueue);
  68. /**
  69. * vt_event_post
  70. * @event: the event that occurred
  71. * @old: old console
  72. * @new: new console
  73. *
  74. * Post an VT event to interested VT handlers
  75. */
  76. void vt_event_post(unsigned int event, unsigned int old, unsigned int new)
  77. {
  78. struct list_head *pos, *head;
  79. unsigned long flags;
  80. int wake = 0;
  81. spin_lock_irqsave(&vt_event_lock, flags);
  82. head = &vt_events;
  83. list_for_each(pos, head) {
  84. struct vt_event_wait *ve = list_entry(pos,
  85. struct vt_event_wait, list);
  86. if (!(ve->event.event & event))
  87. continue;
  88. ve->event.event = event;
  89. /* kernel view is consoles 0..n-1, user space view is
  90. console 1..n with 0 meaning current, so we must bias */
  91. ve->event.oldev = old + 1;
  92. ve->event.newev = new + 1;
  93. wake = 1;
  94. ve->done = 1;
  95. }
  96. spin_unlock_irqrestore(&vt_event_lock, flags);
  97. if (wake)
  98. wake_up_interruptible(&vt_event_waitqueue);
  99. }
  100. /**
  101. * vt_event_wait - wait for an event
  102. * @vw: our event
  103. *
  104. * Waits for an event to occur which completes our vt_event_wait
  105. * structure. On return the structure has wv->done set to 1 for success
  106. * or 0 if some event such as a signal ended the wait.
  107. */
  108. static void vt_event_wait(struct vt_event_wait *vw)
  109. {
  110. unsigned long flags;
  111. /* Prepare the event */
  112. INIT_LIST_HEAD(&vw->list);
  113. vw->done = 0;
  114. /* Queue our event */
  115. spin_lock_irqsave(&vt_event_lock, flags);
  116. list_add(&vw->list, &vt_events);
  117. spin_unlock_irqrestore(&vt_event_lock, flags);
  118. /* Wait for it to pass */
  119. wait_event_interruptible(vt_event_waitqueue, vw->done);
  120. /* Dequeue it */
  121. spin_lock_irqsave(&vt_event_lock, flags);
  122. list_del(&vw->list);
  123. spin_unlock_irqrestore(&vt_event_lock, flags);
  124. }
  125. /**
  126. * vt_event_wait_ioctl - event ioctl handler
  127. * @arg: argument to ioctl
  128. *
  129. * Implement the VT_WAITEVENT ioctl using the VT event interface
  130. */
  131. static int vt_event_wait_ioctl(struct vt_event __user *event)
  132. {
  133. struct vt_event_wait vw;
  134. if (copy_from_user(&vw.event, event, sizeof(struct vt_event)))
  135. return -EFAULT;
  136. /* Highest supported event for now */
  137. if (vw.event.event & ~VT_MAX_EVENT)
  138. return -EINVAL;
  139. vt_event_wait(&vw);
  140. /* If it occurred report it */
  141. if (vw.done) {
  142. if (copy_to_user(event, &vw.event, sizeof(struct vt_event)))
  143. return -EFAULT;
  144. return 0;
  145. }
  146. return -EINTR;
  147. }
  148. /**
  149. * vt_waitactive - active console wait
  150. * @event: event code
  151. * @n: new console
  152. *
  153. * Helper for event waits. Used to implement the legacy
  154. * event waiting ioctls in terms of events
  155. */
  156. int vt_waitactive(int n)
  157. {
  158. struct vt_event_wait vw;
  159. do {
  160. if (n == fg_console + 1)
  161. break;
  162. vw.event.event = VT_EVENT_SWITCH;
  163. vt_event_wait(&vw);
  164. if (vw.done == 0)
  165. return -EINTR;
  166. } while (vw.event.newev != n);
  167. return 0;
  168. }
  169. /*
  170. * these are the valid i/o ports we're allowed to change. they map all the
  171. * video ports
  172. */
  173. #define GPFIRST 0x3b4
  174. #define GPLAST 0x3df
  175. #define GPNUM (GPLAST - GPFIRST + 1)
  176. #define i (tmp.kb_index)
  177. #define s (tmp.kb_table)
  178. #define v (tmp.kb_value)
  179. static inline int
  180. do_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd)
  181. {
  182. struct kbentry tmp;
  183. ushort *key_map, val, ov;
  184. if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))
  185. return -EFAULT;
  186. if (!capable(CAP_SYS_TTY_CONFIG))
  187. perm = 0;
  188. switch (cmd) {
  189. case KDGKBENT:
  190. key_map = key_maps[s];
  191. if (key_map) {
  192. val = U(key_map[i]);
  193. if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)
  194. val = K_HOLE;
  195. } else
  196. val = (i ? K_HOLE : K_NOSUCHMAP);
  197. return put_user(val, &user_kbe->kb_value);
  198. case KDSKBENT:
  199. if (!perm)
  200. return -EPERM;
  201. if (!i && v == K_NOSUCHMAP) {
  202. /* deallocate map */
  203. key_map = key_maps[s];
  204. if (s && key_map) {
  205. key_maps[s] = NULL;
  206. if (key_map[0] == U(K_ALLOCATED)) {
  207. kfree(key_map);
  208. keymap_count--;
  209. }
  210. }
  211. break;
  212. }
  213. if (KTYP(v) < NR_TYPES) {
  214. if (KVAL(v) > max_vals[KTYP(v)])
  215. return -EINVAL;
  216. } else
  217. if (kbd->kbdmode != VC_UNICODE)
  218. return -EINVAL;
  219. /* ++Geert: non-PC keyboards may generate keycode zero */
  220. #if !defined(__mc68000__) && !defined(__powerpc__)
  221. /* assignment to entry 0 only tests validity of args */
  222. if (!i)
  223. break;
  224. #endif
  225. if (!(key_map = key_maps[s])) {
  226. int j;
  227. if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&
  228. !capable(CAP_SYS_RESOURCE))
  229. return -EPERM;
  230. key_map = kmalloc(sizeof(plain_map),
  231. GFP_KERNEL);
  232. if (!key_map)
  233. return -ENOMEM;
  234. key_maps[s] = key_map;
  235. key_map[0] = U(K_ALLOCATED);
  236. for (j = 1; j < NR_KEYS; j++)
  237. key_map[j] = U(K_HOLE);
  238. keymap_count++;
  239. }
  240. ov = U(key_map[i]);
  241. if (v == ov)
  242. break; /* nothing to do */
  243. /*
  244. * Attention Key.
  245. */
  246. if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))
  247. return -EPERM;
  248. key_map[i] = U(v);
  249. if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))
  250. compute_shiftstate();
  251. break;
  252. }
  253. return 0;
  254. }
  255. #undef i
  256. #undef s
  257. #undef v
  258. static inline int
  259. do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm)
  260. {
  261. struct kbkeycode tmp;
  262. int kc = 0;
  263. if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))
  264. return -EFAULT;
  265. switch (cmd) {
  266. case KDGETKEYCODE:
  267. kc = getkeycode(tmp.scancode);
  268. if (kc >= 0)
  269. kc = put_user(kc, &user_kbkc->keycode);
  270. break;
  271. case KDSETKEYCODE:
  272. if (!perm)
  273. return -EPERM;
  274. kc = setkeycode(tmp.scancode, tmp.keycode);
  275. break;
  276. }
  277. return kc;
  278. }
  279. static inline int
  280. do_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm)
  281. {
  282. struct kbsentry *kbs;
  283. char *p;
  284. u_char *q;
  285. u_char __user *up;
  286. int sz;
  287. int delta;
  288. char *first_free, *fj, *fnw;
  289. int i, j, k;
  290. int ret;
  291. if (!capable(CAP_SYS_TTY_CONFIG))
  292. perm = 0;
  293. kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);
  294. if (!kbs) {
  295. ret = -ENOMEM;
  296. goto reterr;
  297. }
  298. /* we mostly copy too much here (512bytes), but who cares ;) */
  299. if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {
  300. ret = -EFAULT;
  301. goto reterr;
  302. }
  303. kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';
  304. i = kbs->kb_func;
  305. switch (cmd) {
  306. case KDGKBSENT:
  307. sz = sizeof(kbs->kb_string) - 1; /* sz should have been
  308. a struct member */
  309. up = user_kdgkb->kb_string;
  310. p = func_table[i];
  311. if(p)
  312. for ( ; *p && sz; p++, sz--)
  313. if (put_user(*p, up++)) {
  314. ret = -EFAULT;
  315. goto reterr;
  316. }
  317. if (put_user('\0', up)) {
  318. ret = -EFAULT;
  319. goto reterr;
  320. }
  321. kfree(kbs);
  322. return ((p && *p) ? -EOVERFLOW : 0);
  323. case KDSKBSENT:
  324. if (!perm) {
  325. ret = -EPERM;
  326. goto reterr;
  327. }
  328. q = func_table[i];
  329. first_free = funcbufptr + (funcbufsize - funcbufleft);
  330. for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++)
  331. ;
  332. if (j < MAX_NR_FUNC)
  333. fj = func_table[j];
  334. else
  335. fj = first_free;
  336. delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);
  337. if (delta <= funcbufleft) { /* it fits in current buf */
  338. if (j < MAX_NR_FUNC) {
  339. memmove(fj + delta, fj, first_free - fj);
  340. for (k = j; k < MAX_NR_FUNC; k++)
  341. if (func_table[k])
  342. func_table[k] += delta;
  343. }
  344. if (!q)
  345. func_table[i] = fj;
  346. funcbufleft -= delta;
  347. } else { /* allocate a larger buffer */
  348. sz = 256;
  349. while (sz < funcbufsize - funcbufleft + delta)
  350. sz <<= 1;
  351. fnw = kmalloc(sz, GFP_KERNEL);
  352. if(!fnw) {
  353. ret = -ENOMEM;
  354. goto reterr;
  355. }
  356. if (!q)
  357. func_table[i] = fj;
  358. if (fj > funcbufptr)
  359. memmove(fnw, funcbufptr, fj - funcbufptr);
  360. for (k = 0; k < j; k++)
  361. if (func_table[k])
  362. func_table[k] = fnw + (func_table[k] - funcbufptr);
  363. if (first_free > fj) {
  364. memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);
  365. for (k = j; k < MAX_NR_FUNC; k++)
  366. if (func_table[k])
  367. func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;
  368. }
  369. if (funcbufptr != func_buf)
  370. kfree(funcbufptr);
  371. funcbufptr = fnw;
  372. funcbufleft = funcbufleft - delta + sz - funcbufsize;
  373. funcbufsize = sz;
  374. }
  375. strcpy(func_table[i], kbs->kb_string);
  376. break;
  377. }
  378. ret = 0;
  379. reterr:
  380. kfree(kbs);
  381. return ret;
  382. }
  383. static inline int
  384. do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op)
  385. {
  386. struct consolefontdesc cfdarg;
  387. int i;
  388. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc)))
  389. return -EFAULT;
  390. switch (cmd) {
  391. case PIO_FONTX:
  392. if (!perm)
  393. return -EPERM;
  394. op->op = KD_FONT_OP_SET;
  395. op->flags = KD_FONT_FLAG_OLD;
  396. op->width = 8;
  397. op->height = cfdarg.charheight;
  398. op->charcount = cfdarg.charcount;
  399. op->data = cfdarg.chardata;
  400. return con_font_op(vc_cons[fg_console].d, op);
  401. case GIO_FONTX: {
  402. op->op = KD_FONT_OP_GET;
  403. op->flags = KD_FONT_FLAG_OLD;
  404. op->width = 8;
  405. op->height = cfdarg.charheight;
  406. op->charcount = cfdarg.charcount;
  407. op->data = cfdarg.chardata;
  408. i = con_font_op(vc_cons[fg_console].d, op);
  409. if (i)
  410. return i;
  411. cfdarg.charheight = op->height;
  412. cfdarg.charcount = op->charcount;
  413. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))
  414. return -EFAULT;
  415. return 0;
  416. }
  417. }
  418. return -EINVAL;
  419. }
  420. static inline int
  421. do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, struct vc_data *vc)
  422. {
  423. struct unimapdesc tmp;
  424. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  425. return -EFAULT;
  426. if (tmp.entries)
  427. if (!access_ok(VERIFY_WRITE, tmp.entries,
  428. tmp.entry_ct*sizeof(struct unipair)))
  429. return -EFAULT;
  430. switch (cmd) {
  431. case PIO_UNIMAP:
  432. if (!perm)
  433. return -EPERM;
  434. return con_set_unimap(vc, tmp.entry_ct, tmp.entries);
  435. case GIO_UNIMAP:
  436. if (!perm && fg_console != vc->vc_num)
  437. return -EPERM;
  438. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);
  439. }
  440. return 0;
  441. }
  442. /*
  443. * We handle the console-specific ioctl's here. We allow the
  444. * capability to modify any console, not just the fg_console.
  445. */
  446. int vt_ioctl(struct tty_struct *tty, struct file * file,
  447. unsigned int cmd, unsigned long arg)
  448. {
  449. struct vc_data *vc = tty->driver_data;
  450. struct console_font_op op; /* used in multiple places here */
  451. struct kbd_struct * kbd;
  452. unsigned int console;
  453. unsigned char ucval;
  454. void __user *up = (void __user *)arg;
  455. int i, perm;
  456. int ret = 0;
  457. console = vc->vc_num;
  458. lock_kernel();
  459. if (!vc_cons_allocated(console)) { /* impossible? */
  460. ret = -ENOIOCTLCMD;
  461. goto out;
  462. }
  463. /*
  464. * To have permissions to do most of the vt ioctls, we either have
  465. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  466. */
  467. perm = 0;
  468. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  469. perm = 1;
  470. kbd = kbd_table + console;
  471. switch (cmd) {
  472. case TIOCLINUX:
  473. ret = tioclinux(tty, arg);
  474. break;
  475. case KIOCSOUND:
  476. if (!perm)
  477. goto eperm;
  478. /* FIXME: This is an old broken API but we need to keep it
  479. supported and somehow separate the historic advertised
  480. tick rate from any real one */
  481. if (arg)
  482. arg = CLOCK_TICK_RATE / arg;
  483. kd_mksound(arg, 0);
  484. break;
  485. case KDMKTONE:
  486. if (!perm)
  487. goto eperm;
  488. {
  489. unsigned int ticks, count;
  490. /*
  491. * Generate the tone for the appropriate number of ticks.
  492. * If the time is zero, turn off sound ourselves.
  493. */
  494. ticks = HZ * ((arg >> 16) & 0xffff) / 1000;
  495. count = ticks ? (arg & 0xffff) : 0;
  496. /* FIXME: This is an old broken API but we need to keep it
  497. supported and somehow separate the historic advertised
  498. tick rate from any real one */
  499. if (count)
  500. count = CLOCK_TICK_RATE / count;
  501. kd_mksound(count, ticks);
  502. break;
  503. }
  504. case KDGKBTYPE:
  505. /*
  506. * this is naive.
  507. */
  508. ucval = KB_101;
  509. goto setchar;
  510. /*
  511. * These cannot be implemented on any machine that implements
  512. * ioperm() in user level (such as Alpha PCs) or not at all.
  513. *
  514. * XXX: you should never use these, just call ioperm directly..
  515. */
  516. #ifdef CONFIG_X86
  517. case KDADDIO:
  518. case KDDELIO:
  519. /*
  520. * KDADDIO and KDDELIO may be able to add ports beyond what
  521. * we reject here, but to be safe...
  522. */
  523. if (arg < GPFIRST || arg > GPLAST) {
  524. ret = -EINVAL;
  525. break;
  526. }
  527. ret = sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;
  528. break;
  529. case KDENABIO:
  530. case KDDISABIO:
  531. ret = sys_ioperm(GPFIRST, GPNUM,
  532. (cmd == KDENABIO)) ? -ENXIO : 0;
  533. break;
  534. #endif
  535. /* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */
  536. case KDKBDREP:
  537. {
  538. struct kbd_repeat kbrep;
  539. if (!capable(CAP_SYS_TTY_CONFIG))
  540. goto eperm;
  541. if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat))) {
  542. ret = -EFAULT;
  543. break;
  544. }
  545. ret = kbd_rate(&kbrep);
  546. if (ret)
  547. break;
  548. if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))
  549. ret = -EFAULT;
  550. break;
  551. }
  552. case KDSETMODE:
  553. /*
  554. * currently, setting the mode from KD_TEXT to KD_GRAPHICS
  555. * doesn't do a whole lot. i'm not sure if it should do any
  556. * restoration of modes or what...
  557. *
  558. * XXX It should at least call into the driver, fbdev's definitely
  559. * need to restore their engine state. --BenH
  560. */
  561. if (!perm)
  562. goto eperm;
  563. switch (arg) {
  564. case KD_GRAPHICS:
  565. break;
  566. case KD_TEXT0:
  567. case KD_TEXT1:
  568. arg = KD_TEXT;
  569. case KD_TEXT:
  570. break;
  571. default:
  572. ret = -EINVAL;
  573. goto out;
  574. }
  575. if (vc->vc_mode == (unsigned char) arg)
  576. break;
  577. vc->vc_mode = (unsigned char) arg;
  578. if (console != fg_console)
  579. break;
  580. /*
  581. * explicitly blank/unblank the screen if switching modes
  582. */
  583. acquire_console_sem();
  584. if (arg == KD_TEXT)
  585. do_unblank_screen(1);
  586. else
  587. do_blank_screen(1);
  588. release_console_sem();
  589. break;
  590. case KDGETMODE:
  591. ucval = vc->vc_mode;
  592. goto setint;
  593. case KDMAPDISP:
  594. case KDUNMAPDISP:
  595. /*
  596. * these work like a combination of mmap and KDENABIO.
  597. * this could be easily finished.
  598. */
  599. ret = -EINVAL;
  600. break;
  601. case KDSKBMODE:
  602. if (!perm)
  603. goto eperm;
  604. switch(arg) {
  605. case K_RAW:
  606. kbd->kbdmode = VC_RAW;
  607. break;
  608. case K_MEDIUMRAW:
  609. kbd->kbdmode = VC_MEDIUMRAW;
  610. break;
  611. case K_XLATE:
  612. kbd->kbdmode = VC_XLATE;
  613. compute_shiftstate();
  614. break;
  615. case K_UNICODE:
  616. kbd->kbdmode = VC_UNICODE;
  617. compute_shiftstate();
  618. break;
  619. default:
  620. ret = -EINVAL;
  621. goto out;
  622. }
  623. tty_ldisc_flush(tty);
  624. break;
  625. case KDGKBMODE:
  626. ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :
  627. (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :
  628. (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :
  629. K_XLATE);
  630. goto setint;
  631. /* this could be folded into KDSKBMODE, but for compatibility
  632. reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */
  633. case KDSKBMETA:
  634. switch(arg) {
  635. case K_METABIT:
  636. clr_vc_kbd_mode(kbd, VC_META);
  637. break;
  638. case K_ESCPREFIX:
  639. set_vc_kbd_mode(kbd, VC_META);
  640. break;
  641. default:
  642. ret = -EINVAL;
  643. }
  644. break;
  645. case KDGKBMETA:
  646. ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);
  647. setint:
  648. ret = put_user(ucval, (int __user *)arg);
  649. break;
  650. case KDGETKEYCODE:
  651. case KDSETKEYCODE:
  652. if(!capable(CAP_SYS_TTY_CONFIG))
  653. perm = 0;
  654. ret = do_kbkeycode_ioctl(cmd, up, perm);
  655. break;
  656. case KDGKBENT:
  657. case KDSKBENT:
  658. ret = do_kdsk_ioctl(cmd, up, perm, kbd);
  659. break;
  660. case KDGKBSENT:
  661. case KDSKBSENT:
  662. ret = do_kdgkb_ioctl(cmd, up, perm);
  663. break;
  664. case KDGKBDIACR:
  665. {
  666. struct kbdiacrs __user *a = up;
  667. struct kbdiacr diacr;
  668. int i;
  669. if (put_user(accent_table_size, &a->kb_cnt)) {
  670. ret = -EFAULT;
  671. break;
  672. }
  673. for (i = 0; i < accent_table_size; i++) {
  674. diacr.diacr = conv_uni_to_8bit(accent_table[i].diacr);
  675. diacr.base = conv_uni_to_8bit(accent_table[i].base);
  676. diacr.result = conv_uni_to_8bit(accent_table[i].result);
  677. if (copy_to_user(a->kbdiacr + i, &diacr, sizeof(struct kbdiacr))) {
  678. ret = -EFAULT;
  679. break;
  680. }
  681. }
  682. break;
  683. }
  684. case KDGKBDIACRUC:
  685. {
  686. struct kbdiacrsuc __user *a = up;
  687. if (put_user(accent_table_size, &a->kb_cnt))
  688. ret = -EFAULT;
  689. else if (copy_to_user(a->kbdiacruc, accent_table,
  690. accent_table_size*sizeof(struct kbdiacruc)))
  691. ret = -EFAULT;
  692. break;
  693. }
  694. case KDSKBDIACR:
  695. {
  696. struct kbdiacrs __user *a = up;
  697. struct kbdiacr diacr;
  698. unsigned int ct;
  699. int i;
  700. if (!perm)
  701. goto eperm;
  702. if (get_user(ct,&a->kb_cnt)) {
  703. ret = -EFAULT;
  704. break;
  705. }
  706. if (ct >= MAX_DIACR) {
  707. ret = -EINVAL;
  708. break;
  709. }
  710. accent_table_size = ct;
  711. for (i = 0; i < ct; i++) {
  712. if (copy_from_user(&diacr, a->kbdiacr + i, sizeof(struct kbdiacr))) {
  713. ret = -EFAULT;
  714. break;
  715. }
  716. accent_table[i].diacr = conv_8bit_to_uni(diacr.diacr);
  717. accent_table[i].base = conv_8bit_to_uni(diacr.base);
  718. accent_table[i].result = conv_8bit_to_uni(diacr.result);
  719. }
  720. break;
  721. }
  722. case KDSKBDIACRUC:
  723. {
  724. struct kbdiacrsuc __user *a = up;
  725. unsigned int ct;
  726. if (!perm)
  727. goto eperm;
  728. if (get_user(ct,&a->kb_cnt)) {
  729. ret = -EFAULT;
  730. break;
  731. }
  732. if (ct >= MAX_DIACR) {
  733. ret = -EINVAL;
  734. break;
  735. }
  736. accent_table_size = ct;
  737. if (copy_from_user(accent_table, a->kbdiacruc, ct*sizeof(struct kbdiacruc)))
  738. ret = -EFAULT;
  739. break;
  740. }
  741. /* the ioctls below read/set the flags usually shown in the leds */
  742. /* don't use them - they will go away without warning */
  743. case KDGKBLED:
  744. ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);
  745. goto setchar;
  746. case KDSKBLED:
  747. if (!perm)
  748. goto eperm;
  749. if (arg & ~0x77) {
  750. ret = -EINVAL;
  751. break;
  752. }
  753. kbd->ledflagstate = (arg & 7);
  754. kbd->default_ledflagstate = ((arg >> 4) & 7);
  755. set_leds();
  756. break;
  757. /* the ioctls below only set the lights, not the functions */
  758. /* for those, see KDGKBLED and KDSKBLED above */
  759. case KDGETLED:
  760. ucval = getledstate();
  761. setchar:
  762. ret = put_user(ucval, (char __user *)arg);
  763. break;
  764. case KDSETLED:
  765. if (!perm)
  766. goto eperm;
  767. setledstate(kbd, arg);
  768. break;
  769. /*
  770. * A process can indicate its willingness to accept signals
  771. * generated by pressing an appropriate key combination.
  772. * Thus, one can have a daemon that e.g. spawns a new console
  773. * upon a keypress and then changes to it.
  774. * See also the kbrequest field of inittab(5).
  775. */
  776. case KDSIGACCEPT:
  777. {
  778. if (!perm || !capable(CAP_KILL))
  779. goto eperm;
  780. if (!valid_signal(arg) || arg < 1 || arg == SIGKILL)
  781. ret = -EINVAL;
  782. else {
  783. spin_lock_irq(&vt_spawn_con.lock);
  784. put_pid(vt_spawn_con.pid);
  785. vt_spawn_con.pid = get_pid(task_pid(current));
  786. vt_spawn_con.sig = arg;
  787. spin_unlock_irq(&vt_spawn_con.lock);
  788. }
  789. break;
  790. }
  791. case VT_SETMODE:
  792. {
  793. struct vt_mode tmp;
  794. if (!perm)
  795. goto eperm;
  796. if (copy_from_user(&tmp, up, sizeof(struct vt_mode))) {
  797. ret = -EFAULT;
  798. goto out;
  799. }
  800. if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS) {
  801. ret = -EINVAL;
  802. goto out;
  803. }
  804. acquire_console_sem();
  805. vc->vt_mode = tmp;
  806. /* the frsig is ignored, so we set it to 0 */
  807. vc->vt_mode.frsig = 0;
  808. put_pid(vc->vt_pid);
  809. vc->vt_pid = get_pid(task_pid(current));
  810. /* no switch is required -- saw@shade.msu.ru */
  811. vc->vt_newvt = -1;
  812. release_console_sem();
  813. break;
  814. }
  815. case VT_GETMODE:
  816. {
  817. struct vt_mode tmp;
  818. int rc;
  819. acquire_console_sem();
  820. memcpy(&tmp, &vc->vt_mode, sizeof(struct vt_mode));
  821. release_console_sem();
  822. rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));
  823. if (rc)
  824. ret = -EFAULT;
  825. break;
  826. }
  827. /*
  828. * Returns global vt state. Note that VT 0 is always open, since
  829. * it's an alias for the current VT, and people can't use it here.
  830. * We cannot return state for more than 16 VTs, since v_state is short.
  831. */
  832. case VT_GETSTATE:
  833. {
  834. struct vt_stat __user *vtstat = up;
  835. unsigned short state, mask;
  836. if (put_user(fg_console + 1, &vtstat->v_active))
  837. ret = -EFAULT;
  838. else {
  839. state = 1; /* /dev/tty0 is always open */
  840. for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask;
  841. ++i, mask <<= 1)
  842. if (VT_IS_IN_USE(i))
  843. state |= mask;
  844. ret = put_user(state, &vtstat->v_state);
  845. }
  846. break;
  847. }
  848. /*
  849. * Returns the first available (non-opened) console.
  850. */
  851. case VT_OPENQRY:
  852. for (i = 0; i < MAX_NR_CONSOLES; ++i)
  853. if (! VT_IS_IN_USE(i))
  854. break;
  855. ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;
  856. goto setint;
  857. /*
  858. * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,
  859. * with num >= 1 (switches to vt 0, our console, are not allowed, just
  860. * to preserve sanity).
  861. */
  862. case VT_ACTIVATE:
  863. if (!perm)
  864. goto eperm;
  865. if (arg == 0 || arg > MAX_NR_CONSOLES)
  866. ret = -ENXIO;
  867. else {
  868. arg--;
  869. acquire_console_sem();
  870. ret = vc_allocate(arg);
  871. release_console_sem();
  872. if (ret)
  873. break;
  874. set_console(arg);
  875. }
  876. break;
  877. case VT_SETACTIVATE:
  878. {
  879. struct vt_setactivate vsa;
  880. if (!perm)
  881. goto eperm;
  882. if (copy_from_user(&vsa, (struct vt_setactivate __user *)arg,
  883. sizeof(struct vt_setactivate))) {
  884. ret = -EFAULT;
  885. goto out;
  886. }
  887. if (vsa.console == 0 || vsa.console > MAX_NR_CONSOLES)
  888. ret = -ENXIO;
  889. else {
  890. vsa.console--;
  891. acquire_console_sem();
  892. ret = vc_allocate(vsa.console);
  893. if (ret == 0) {
  894. struct vc_data *nvc;
  895. /* This is safe providing we don't drop the
  896. console sem between vc_allocate and
  897. finishing referencing nvc */
  898. nvc = vc_cons[vsa.console].d;
  899. nvc->vt_mode = vsa.mode;
  900. nvc->vt_mode.frsig = 0;
  901. put_pid(nvc->vt_pid);
  902. nvc->vt_pid = get_pid(task_pid(current));
  903. }
  904. release_console_sem();
  905. if (ret)
  906. break;
  907. /* Commence switch and lock */
  908. set_console(arg);
  909. }
  910. }
  911. /*
  912. * wait until the specified VT has been activated
  913. */
  914. case VT_WAITACTIVE:
  915. if (!perm)
  916. goto eperm;
  917. if (arg == 0 || arg > MAX_NR_CONSOLES)
  918. ret = -ENXIO;
  919. else
  920. ret = vt_waitactive(arg);
  921. break;
  922. /*
  923. * If a vt is under process control, the kernel will not switch to it
  924. * immediately, but postpone the operation until the process calls this
  925. * ioctl, allowing the switch to complete.
  926. *
  927. * According to the X sources this is the behavior:
  928. * 0: pending switch-from not OK
  929. * 1: pending switch-from OK
  930. * 2: completed switch-to OK
  931. */
  932. case VT_RELDISP:
  933. if (!perm)
  934. goto eperm;
  935. if (vc->vt_mode.mode != VT_PROCESS) {
  936. ret = -EINVAL;
  937. break;
  938. }
  939. /*
  940. * Switching-from response
  941. */
  942. acquire_console_sem();
  943. if (vc->vt_newvt >= 0) {
  944. if (arg == 0)
  945. /*
  946. * Switch disallowed, so forget we were trying
  947. * to do it.
  948. */
  949. vc->vt_newvt = -1;
  950. else {
  951. /*
  952. * The current vt has been released, so
  953. * complete the switch.
  954. */
  955. int newvt;
  956. newvt = vc->vt_newvt;
  957. vc->vt_newvt = -1;
  958. ret = vc_allocate(newvt);
  959. if (ret) {
  960. release_console_sem();
  961. break;
  962. }
  963. /*
  964. * When we actually do the console switch,
  965. * make sure we are atomic with respect to
  966. * other console switches..
  967. */
  968. complete_change_console(vc_cons[newvt].d);
  969. }
  970. } else {
  971. /*
  972. * Switched-to response
  973. */
  974. /*
  975. * If it's just an ACK, ignore it
  976. */
  977. if (arg != VT_ACKACQ)
  978. ret = -EINVAL;
  979. }
  980. release_console_sem();
  981. break;
  982. /*
  983. * Disallocate memory associated to VT (but leave VT1)
  984. */
  985. case VT_DISALLOCATE:
  986. if (arg > MAX_NR_CONSOLES) {
  987. ret = -ENXIO;
  988. break;
  989. }
  990. if (arg == 0) {
  991. /* deallocate all unused consoles, but leave 0 */
  992. acquire_console_sem();
  993. for (i=1; i<MAX_NR_CONSOLES; i++)
  994. if (! VT_BUSY(i))
  995. vc_deallocate(i);
  996. release_console_sem();
  997. } else {
  998. /* deallocate a single console, if possible */
  999. arg--;
  1000. if (VT_BUSY(arg))
  1001. ret = -EBUSY;
  1002. else if (arg) { /* leave 0 */
  1003. acquire_console_sem();
  1004. vc_deallocate(arg);
  1005. release_console_sem();
  1006. }
  1007. }
  1008. break;
  1009. case VT_RESIZE:
  1010. {
  1011. struct vt_sizes __user *vtsizes = up;
  1012. struct vc_data *vc;
  1013. ushort ll,cc;
  1014. if (!perm)
  1015. goto eperm;
  1016. if (get_user(ll, &vtsizes->v_rows) ||
  1017. get_user(cc, &vtsizes->v_cols))
  1018. ret = -EFAULT;
  1019. else {
  1020. acquire_console_sem();
  1021. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1022. vc = vc_cons[i].d;
  1023. if (vc) {
  1024. vc->vc_resize_user = 1;
  1025. vc_resize(vc_cons[i].d, cc, ll);
  1026. }
  1027. }
  1028. release_console_sem();
  1029. }
  1030. break;
  1031. }
  1032. case VT_RESIZEX:
  1033. {
  1034. struct vt_consize __user *vtconsize = up;
  1035. ushort ll,cc,vlin,clin,vcol,ccol;
  1036. if (!perm)
  1037. goto eperm;
  1038. if (!access_ok(VERIFY_READ, vtconsize,
  1039. sizeof(struct vt_consize))) {
  1040. ret = -EFAULT;
  1041. break;
  1042. }
  1043. /* FIXME: Should check the copies properly */
  1044. __get_user(ll, &vtconsize->v_rows);
  1045. __get_user(cc, &vtconsize->v_cols);
  1046. __get_user(vlin, &vtconsize->v_vlin);
  1047. __get_user(clin, &vtconsize->v_clin);
  1048. __get_user(vcol, &vtconsize->v_vcol);
  1049. __get_user(ccol, &vtconsize->v_ccol);
  1050. vlin = vlin ? vlin : vc->vc_scan_lines;
  1051. if (clin) {
  1052. if (ll) {
  1053. if (ll != vlin/clin) {
  1054. /* Parameters don't add up */
  1055. ret = -EINVAL;
  1056. break;
  1057. }
  1058. } else
  1059. ll = vlin/clin;
  1060. }
  1061. if (vcol && ccol) {
  1062. if (cc) {
  1063. if (cc != vcol/ccol) {
  1064. ret = -EINVAL;
  1065. break;
  1066. }
  1067. } else
  1068. cc = vcol/ccol;
  1069. }
  1070. if (clin > 32) {
  1071. ret = -EINVAL;
  1072. break;
  1073. }
  1074. for (i = 0; i < MAX_NR_CONSOLES; i++) {
  1075. if (!vc_cons[i].d)
  1076. continue;
  1077. acquire_console_sem();
  1078. if (vlin)
  1079. vc_cons[i].d->vc_scan_lines = vlin;
  1080. if (clin)
  1081. vc_cons[i].d->vc_font.height = clin;
  1082. vc_cons[i].d->vc_resize_user = 1;
  1083. vc_resize(vc_cons[i].d, cc, ll);
  1084. release_console_sem();
  1085. }
  1086. break;
  1087. }
  1088. case PIO_FONT: {
  1089. if (!perm)
  1090. goto eperm;
  1091. op.op = KD_FONT_OP_SET;
  1092. op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC; /* Compatibility */
  1093. op.width = 8;
  1094. op.height = 0;
  1095. op.charcount = 256;
  1096. op.data = up;
  1097. ret = con_font_op(vc_cons[fg_console].d, &op);
  1098. break;
  1099. }
  1100. case GIO_FONT: {
  1101. op.op = KD_FONT_OP_GET;
  1102. op.flags = KD_FONT_FLAG_OLD;
  1103. op.width = 8;
  1104. op.height = 32;
  1105. op.charcount = 256;
  1106. op.data = up;
  1107. ret = con_font_op(vc_cons[fg_console].d, &op);
  1108. break;
  1109. }
  1110. case PIO_CMAP:
  1111. if (!perm)
  1112. ret = -EPERM;
  1113. else
  1114. ret = con_set_cmap(up);
  1115. break;
  1116. case GIO_CMAP:
  1117. ret = con_get_cmap(up);
  1118. break;
  1119. case PIO_FONTX:
  1120. case GIO_FONTX:
  1121. ret = do_fontx_ioctl(cmd, up, perm, &op);
  1122. break;
  1123. case PIO_FONTRESET:
  1124. {
  1125. if (!perm)
  1126. goto eperm;
  1127. #ifdef BROKEN_GRAPHICS_PROGRAMS
  1128. /* With BROKEN_GRAPHICS_PROGRAMS defined, the default
  1129. font is not saved. */
  1130. ret = -ENOSYS;
  1131. break;
  1132. #else
  1133. {
  1134. op.op = KD_FONT_OP_SET_DEFAULT;
  1135. op.data = NULL;
  1136. ret = con_font_op(vc_cons[fg_console].d, &op);
  1137. if (ret)
  1138. break;
  1139. con_set_default_unimap(vc_cons[fg_console].d);
  1140. break;
  1141. }
  1142. #endif
  1143. }
  1144. case KDFONTOP: {
  1145. if (copy_from_user(&op, up, sizeof(op))) {
  1146. ret = -EFAULT;
  1147. break;
  1148. }
  1149. if (!perm && op.op != KD_FONT_OP_GET)
  1150. goto eperm;
  1151. ret = con_font_op(vc, &op);
  1152. if (ret)
  1153. break;
  1154. if (copy_to_user(up, &op, sizeof(op)))
  1155. ret = -EFAULT;
  1156. break;
  1157. }
  1158. case PIO_SCRNMAP:
  1159. if (!perm)
  1160. ret = -EPERM;
  1161. else
  1162. ret = con_set_trans_old(up);
  1163. break;
  1164. case GIO_SCRNMAP:
  1165. ret = con_get_trans_old(up);
  1166. break;
  1167. case PIO_UNISCRNMAP:
  1168. if (!perm)
  1169. ret = -EPERM;
  1170. else
  1171. ret = con_set_trans_new(up);
  1172. break;
  1173. case GIO_UNISCRNMAP:
  1174. ret = con_get_trans_new(up);
  1175. break;
  1176. case PIO_UNIMAPCLR:
  1177. { struct unimapinit ui;
  1178. if (!perm)
  1179. goto eperm;
  1180. ret = copy_from_user(&ui, up, sizeof(struct unimapinit));
  1181. if (!ret)
  1182. con_clear_unimap(vc, &ui);
  1183. break;
  1184. }
  1185. case PIO_UNIMAP:
  1186. case GIO_UNIMAP:
  1187. ret = do_unimap_ioctl(cmd, up, perm, vc);
  1188. break;
  1189. case VT_LOCKSWITCH:
  1190. if (!capable(CAP_SYS_TTY_CONFIG))
  1191. goto eperm;
  1192. vt_dont_switch = 1;
  1193. break;
  1194. case VT_UNLOCKSWITCH:
  1195. if (!capable(CAP_SYS_TTY_CONFIG))
  1196. goto eperm;
  1197. vt_dont_switch = 0;
  1198. break;
  1199. case VT_GETHIFONTMASK:
  1200. ret = put_user(vc->vc_hi_font_mask,
  1201. (unsigned short __user *)arg);
  1202. break;
  1203. case VT_WAITEVENT:
  1204. ret = vt_event_wait_ioctl((struct vt_event __user *)arg);
  1205. break;
  1206. default:
  1207. ret = -ENOIOCTLCMD;
  1208. }
  1209. out:
  1210. unlock_kernel();
  1211. return ret;
  1212. eperm:
  1213. ret = -EPERM;
  1214. goto out;
  1215. }
  1216. void reset_vc(struct vc_data *vc)
  1217. {
  1218. vc->vc_mode = KD_TEXT;
  1219. kbd_table[vc->vc_num].kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
  1220. vc->vt_mode.mode = VT_AUTO;
  1221. vc->vt_mode.waitv = 0;
  1222. vc->vt_mode.relsig = 0;
  1223. vc->vt_mode.acqsig = 0;
  1224. vc->vt_mode.frsig = 0;
  1225. put_pid(vc->vt_pid);
  1226. vc->vt_pid = NULL;
  1227. vc->vt_newvt = -1;
  1228. if (!in_interrupt()) /* Via keyboard.c:SAK() - akpm */
  1229. reset_palette(vc);
  1230. }
  1231. void vc_SAK(struct work_struct *work)
  1232. {
  1233. struct vc *vc_con =
  1234. container_of(work, struct vc, SAK_work);
  1235. struct vc_data *vc;
  1236. struct tty_struct *tty;
  1237. acquire_console_sem();
  1238. vc = vc_con->d;
  1239. if (vc) {
  1240. tty = vc->vc_tty;
  1241. /*
  1242. * SAK should also work in all raw modes and reset
  1243. * them properly.
  1244. */
  1245. if (tty)
  1246. __do_SAK(tty);
  1247. reset_vc(vc);
  1248. }
  1249. release_console_sem();
  1250. }
  1251. #ifdef CONFIG_COMPAT
  1252. struct compat_consolefontdesc {
  1253. unsigned short charcount; /* characters in font (256 or 512) */
  1254. unsigned short charheight; /* scan lines per character (1-32) */
  1255. compat_caddr_t chardata; /* font data in expanded form */
  1256. };
  1257. static inline int
  1258. compat_fontx_ioctl(int cmd, struct compat_consolefontdesc __user *user_cfd,
  1259. int perm, struct console_font_op *op)
  1260. {
  1261. struct compat_consolefontdesc cfdarg;
  1262. int i;
  1263. if (copy_from_user(&cfdarg, user_cfd, sizeof(struct compat_consolefontdesc)))
  1264. return -EFAULT;
  1265. switch (cmd) {
  1266. case PIO_FONTX:
  1267. if (!perm)
  1268. return -EPERM;
  1269. op->op = KD_FONT_OP_SET;
  1270. op->flags = KD_FONT_FLAG_OLD;
  1271. op->width = 8;
  1272. op->height = cfdarg.charheight;
  1273. op->charcount = cfdarg.charcount;
  1274. op->data = compat_ptr(cfdarg.chardata);
  1275. return con_font_op(vc_cons[fg_console].d, op);
  1276. case GIO_FONTX:
  1277. op->op = KD_FONT_OP_GET;
  1278. op->flags = KD_FONT_FLAG_OLD;
  1279. op->width = 8;
  1280. op->height = cfdarg.charheight;
  1281. op->charcount = cfdarg.charcount;
  1282. op->data = compat_ptr(cfdarg.chardata);
  1283. i = con_font_op(vc_cons[fg_console].d, op);
  1284. if (i)
  1285. return i;
  1286. cfdarg.charheight = op->height;
  1287. cfdarg.charcount = op->charcount;
  1288. if (copy_to_user(user_cfd, &cfdarg, sizeof(struct compat_consolefontdesc)))
  1289. return -EFAULT;
  1290. return 0;
  1291. }
  1292. return -EINVAL;
  1293. }
  1294. struct compat_console_font_op {
  1295. compat_uint_t op; /* operation code KD_FONT_OP_* */
  1296. compat_uint_t flags; /* KD_FONT_FLAG_* */
  1297. compat_uint_t width, height; /* font size */
  1298. compat_uint_t charcount;
  1299. compat_caddr_t data; /* font data with height fixed to 32 */
  1300. };
  1301. static inline int
  1302. compat_kdfontop_ioctl(struct compat_console_font_op __user *fontop,
  1303. int perm, struct console_font_op *op, struct vc_data *vc)
  1304. {
  1305. int i;
  1306. if (copy_from_user(op, fontop, sizeof(struct compat_console_font_op)))
  1307. return -EFAULT;
  1308. if (!perm && op->op != KD_FONT_OP_GET)
  1309. return -EPERM;
  1310. op->data = compat_ptr(((struct compat_console_font_op *)op)->data);
  1311. op->flags |= KD_FONT_FLAG_OLD;
  1312. i = con_font_op(vc, op);
  1313. if (i)
  1314. return i;
  1315. ((struct compat_console_font_op *)op)->data = (unsigned long)op->data;
  1316. if (copy_to_user(fontop, op, sizeof(struct compat_console_font_op)))
  1317. return -EFAULT;
  1318. return 0;
  1319. }
  1320. struct compat_unimapdesc {
  1321. unsigned short entry_ct;
  1322. compat_caddr_t entries;
  1323. };
  1324. static inline int
  1325. compat_unimap_ioctl(unsigned int cmd, struct compat_unimapdesc __user *user_ud,
  1326. int perm, struct vc_data *vc)
  1327. {
  1328. struct compat_unimapdesc tmp;
  1329. struct unipair __user *tmp_entries;
  1330. if (copy_from_user(&tmp, user_ud, sizeof tmp))
  1331. return -EFAULT;
  1332. tmp_entries = compat_ptr(tmp.entries);
  1333. if (tmp_entries)
  1334. if (!access_ok(VERIFY_WRITE, tmp_entries,
  1335. tmp.entry_ct*sizeof(struct unipair)))
  1336. return -EFAULT;
  1337. switch (cmd) {
  1338. case PIO_UNIMAP:
  1339. if (!perm)
  1340. return -EPERM;
  1341. return con_set_unimap(vc, tmp.entry_ct, tmp_entries);
  1342. case GIO_UNIMAP:
  1343. if (!perm && fg_console != vc->vc_num)
  1344. return -EPERM;
  1345. return con_get_unimap(vc, tmp.entry_ct, &(user_ud->entry_ct), tmp_entries);
  1346. }
  1347. return 0;
  1348. }
  1349. long vt_compat_ioctl(struct tty_struct *tty, struct file * file,
  1350. unsigned int cmd, unsigned long arg)
  1351. {
  1352. struct vc_data *vc = tty->driver_data;
  1353. struct console_font_op op; /* used in multiple places here */
  1354. struct kbd_struct *kbd;
  1355. unsigned int console;
  1356. void __user *up = (void __user *)arg;
  1357. int perm;
  1358. int ret = 0;
  1359. console = vc->vc_num;
  1360. lock_kernel();
  1361. if (!vc_cons_allocated(console)) { /* impossible? */
  1362. ret = -ENOIOCTLCMD;
  1363. goto out;
  1364. }
  1365. /*
  1366. * To have permissions to do most of the vt ioctls, we either have
  1367. * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.
  1368. */
  1369. perm = 0;
  1370. if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))
  1371. perm = 1;
  1372. kbd = kbd_table + console;
  1373. switch (cmd) {
  1374. /*
  1375. * these need special handlers for incompatible data structures
  1376. */
  1377. case PIO_FONTX:
  1378. case GIO_FONTX:
  1379. ret = compat_fontx_ioctl(cmd, up, perm, &op);
  1380. break;
  1381. case KDFONTOP:
  1382. ret = compat_kdfontop_ioctl(up, perm, &op, vc);
  1383. break;
  1384. case PIO_UNIMAP:
  1385. case GIO_UNIMAP:
  1386. ret = compat_unimap_ioctl(cmd, up, perm, vc);
  1387. break;
  1388. /*
  1389. * all these treat 'arg' as an integer
  1390. */
  1391. case KIOCSOUND:
  1392. case KDMKTONE:
  1393. #ifdef CONFIG_X86
  1394. case KDADDIO:
  1395. case KDDELIO:
  1396. #endif
  1397. case KDSETMODE:
  1398. case KDMAPDISP:
  1399. case KDUNMAPDISP:
  1400. case KDSKBMODE:
  1401. case KDSKBMETA:
  1402. case KDSKBLED:
  1403. case KDSETLED:
  1404. case KDSIGACCEPT:
  1405. case VT_ACTIVATE:
  1406. case VT_WAITACTIVE:
  1407. case VT_RELDISP:
  1408. case VT_DISALLOCATE:
  1409. case VT_RESIZE:
  1410. case VT_RESIZEX:
  1411. goto fallback;
  1412. /*
  1413. * the rest has a compatible data structure behind arg,
  1414. * but we have to convert it to a proper 64 bit pointer.
  1415. */
  1416. default:
  1417. arg = (unsigned long)compat_ptr(arg);
  1418. goto fallback;
  1419. }
  1420. out:
  1421. unlock_kernel();
  1422. return ret;
  1423. fallback:
  1424. unlock_kernel();
  1425. return vt_ioctl(tty, file, cmd, arg);
  1426. }
  1427. #endif /* CONFIG_COMPAT */
  1428. /*
  1429. * Performs the back end of a vt switch. Called under the console
  1430. * semaphore.
  1431. */
  1432. static void complete_change_console(struct vc_data *vc)
  1433. {
  1434. unsigned char old_vc_mode;
  1435. int old = fg_console;
  1436. last_console = fg_console;
  1437. /*
  1438. * If we're switching, we could be going from KD_GRAPHICS to
  1439. * KD_TEXT mode or vice versa, which means we need to blank or
  1440. * unblank the screen later.
  1441. */
  1442. old_vc_mode = vc_cons[fg_console].d->vc_mode;
  1443. switch_screen(vc);
  1444. /*
  1445. * This can't appear below a successful kill_pid(). If it did,
  1446. * then the *blank_screen operation could occur while X, having
  1447. * received acqsig, is waking up on another processor. This
  1448. * condition can lead to overlapping accesses to the VGA range
  1449. * and the framebuffer (causing system lockups).
  1450. *
  1451. * To account for this we duplicate this code below only if the
  1452. * controlling process is gone and we've called reset_vc.
  1453. */
  1454. if (old_vc_mode != vc->vc_mode) {
  1455. if (vc->vc_mode == KD_TEXT)
  1456. do_unblank_screen(1);
  1457. else
  1458. do_blank_screen(1);
  1459. }
  1460. /*
  1461. * If this new console is under process control, send it a signal
  1462. * telling it that it has acquired. Also check if it has died and
  1463. * clean up (similar to logic employed in change_console())
  1464. */
  1465. if (vc->vt_mode.mode == VT_PROCESS) {
  1466. /*
  1467. * Send the signal as privileged - kill_pid() will
  1468. * tell us if the process has gone or something else
  1469. * is awry
  1470. */
  1471. if (kill_pid(vc->vt_pid, vc->vt_mode.acqsig, 1) != 0) {
  1472. /*
  1473. * The controlling process has died, so we revert back to
  1474. * normal operation. In this case, we'll also change back
  1475. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1476. * but it saves the agony when the X server dies and the screen
  1477. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1478. * this outside of VT_PROCESS but there is no single process
  1479. * to account for and tracking tty count may be undesirable.
  1480. */
  1481. reset_vc(vc);
  1482. if (old_vc_mode != vc->vc_mode) {
  1483. if (vc->vc_mode == KD_TEXT)
  1484. do_unblank_screen(1);
  1485. else
  1486. do_blank_screen(1);
  1487. }
  1488. }
  1489. }
  1490. /*
  1491. * Wake anyone waiting for their VT to activate
  1492. */
  1493. vt_event_post(VT_EVENT_SWITCH, old, vc->vc_num);
  1494. return;
  1495. }
  1496. /*
  1497. * Performs the front-end of a vt switch
  1498. */
  1499. void change_console(struct vc_data *new_vc)
  1500. {
  1501. struct vc_data *vc;
  1502. if (!new_vc || new_vc->vc_num == fg_console || vt_dont_switch)
  1503. return;
  1504. /*
  1505. * If this vt is in process mode, then we need to handshake with
  1506. * that process before switching. Essentially, we store where that
  1507. * vt wants to switch to and wait for it to tell us when it's done
  1508. * (via VT_RELDISP ioctl).
  1509. *
  1510. * We also check to see if the controlling process still exists.
  1511. * If it doesn't, we reset this vt to auto mode and continue.
  1512. * This is a cheap way to track process control. The worst thing
  1513. * that can happen is: we send a signal to a process, it dies, and
  1514. * the switch gets "lost" waiting for a response; hopefully, the
  1515. * user will try again, we'll detect the process is gone (unless
  1516. * the user waits just the right amount of time :-) and revert the
  1517. * vt to auto control.
  1518. */
  1519. vc = vc_cons[fg_console].d;
  1520. if (vc->vt_mode.mode == VT_PROCESS) {
  1521. /*
  1522. * Send the signal as privileged - kill_pid() will
  1523. * tell us if the process has gone or something else
  1524. * is awry.
  1525. *
  1526. * We need to set vt_newvt *before* sending the signal or we
  1527. * have a race.
  1528. */
  1529. vc->vt_newvt = new_vc->vc_num;
  1530. if (kill_pid(vc->vt_pid, vc->vt_mode.relsig, 1) == 0) {
  1531. /*
  1532. * It worked. Mark the vt to switch to and
  1533. * return. The process needs to send us a
  1534. * VT_RELDISP ioctl to complete the switch.
  1535. */
  1536. return;
  1537. }
  1538. /*
  1539. * The controlling process has died, so we revert back to
  1540. * normal operation. In this case, we'll also change back
  1541. * to KD_TEXT mode. I'm not sure if this is strictly correct
  1542. * but it saves the agony when the X server dies and the screen
  1543. * remains blanked due to KD_GRAPHICS! It would be nice to do
  1544. * this outside of VT_PROCESS but there is no single process
  1545. * to account for and tracking tty count may be undesirable.
  1546. */
  1547. reset_vc(vc);
  1548. /*
  1549. * Fall through to normal (VT_AUTO) handling of the switch...
  1550. */
  1551. }
  1552. /*
  1553. * Ignore all switches in KD_GRAPHICS+VT_AUTO mode
  1554. */
  1555. if (vc->vc_mode == KD_GRAPHICS)
  1556. return;
  1557. complete_change_console(new_vc);
  1558. }
  1559. /* Perform a kernel triggered VT switch for suspend/resume */
  1560. static int disable_vt_switch;
  1561. int vt_move_to_console(unsigned int vt, int alloc)
  1562. {
  1563. int prev;
  1564. acquire_console_sem();
  1565. /* Graphics mode - up to X */
  1566. if (disable_vt_switch) {
  1567. release_console_sem();
  1568. return 0;
  1569. }
  1570. prev = fg_console;
  1571. if (alloc && vc_allocate(vt)) {
  1572. /* we can't have a free VC for now. Too bad,
  1573. * we don't want to mess the screen for now. */
  1574. release_console_sem();
  1575. return -ENOSPC;
  1576. }
  1577. if (set_console(vt)) {
  1578. /*
  1579. * We're unable to switch to the SUSPEND_CONSOLE.
  1580. * Let the calling function know so it can decide
  1581. * what to do.
  1582. */
  1583. release_console_sem();
  1584. return -EIO;
  1585. }
  1586. release_console_sem();
  1587. if (vt_waitactive(vt + 1)) {
  1588. pr_debug("Suspend: Can't switch VCs.");
  1589. return -EINTR;
  1590. }
  1591. return prev;
  1592. }
  1593. /*
  1594. * Normally during a suspend, we allocate a new console and switch to it.
  1595. * When we resume, we switch back to the original console. This switch
  1596. * can be slow, so on systems where the framebuffer can handle restoration
  1597. * of video registers anyways, there's little point in doing the console
  1598. * switch. This function allows you to disable it by passing it '0'.
  1599. */
  1600. void pm_set_vt_switch(int do_switch)
  1601. {
  1602. acquire_console_sem();
  1603. disable_vt_switch = !do_switch;
  1604. release_console_sem();
  1605. }
  1606. EXPORT_SYMBOL(pm_set_vt_switch);