blob: 9126a01290ea493354d74ad5760b230b8064ad17 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * This module exports the functions:
4 *
Okash Khawaja496124e2019-04-17 13:21:13 +01005 * 'int set_selection_user(struct tiocl_selection __user *,
6 * struct tty_struct *)'
7 * 'int set_selection_kernel(struct tiocl_selection *, struct tty_struct *)'
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 'void clear_selection(void)'
9 * 'int paste_selection(struct tty_struct *)'
10 * 'int sel_loadlut(char __user *)'
11 *
12 * Now that /dev/vcs exists, most of this can disappear again.
13 */
14
15#include <linux/module.h>
16#include <linux/tty.h>
17#include <linux/sched.h>
18#include <linux/mm.h>
Jiri Slaby07e61242020-02-10 09:11:31 +010019#include <linux/mutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/slab.h>
21#include <linux/types.h>
22
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080023#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Jan Engelhardt759448f2007-07-15 23:40:40 -070025#include <linux/kbd_kern.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/vt_kern.h>
27#include <linux/consolemap.h>
28#include <linux/selection.h>
29#include <linux/tiocl.h>
30#include <linux/console.h>
Peter Hurleya7c8d582013-06-15 09:36:15 -040031#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Jiri Slaby687bff02020-02-10 09:11:30 +010033#include <linux/sched/signal.h>
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/* Don't take this from <ctype.h>: 011-015 on the screen aren't spaces */
36#define isspace(c) ((c) == ' ')
37
38extern void poke_blanked_console(void);
39
Alan Cox079c9532012-02-28 14:49:23 +000040/* FIXME: all this needs locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -070041/* Variables for selection control. */
42/* Use a dynamic buffer, instead of static (Dec 1994) */
Alan Coxca9bda02006-09-29 02:00:03 -070043struct vc_data *sel_cons; /* must not be deallocated */
Jan Engelhardt759448f2007-07-15 23:40:40 -070044static int use_unicode;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static volatile int sel_start = -1; /* cleared by clear_selection */
46static int sel_end;
47static int sel_buffer_lth;
48static char *sel_buffer;
Jiri Slaby07e61242020-02-10 09:11:31 +010049static DEFINE_MUTEX(sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* clear_selection, highlight and highlight_pointer can be called
52 from interrupt (via scrollback/front) */
53
54/* set reverse video on characters s-e of console with selection. */
55static inline void highlight(const int s, const int e)
56{
57 invert_screen(sel_cons, s, e-s+2, 1);
58}
59
60/* use complementary color to show the pointer */
61static inline void highlight_pointer(const int where)
62{
63 complement_pos(sel_cons, where);
64}
65
Adam Borowski9bfdc262018-07-18 04:10:44 +020066static u32
Linus Torvalds1da177e2005-04-16 15:20:36 -070067sel_pos(int n)
68{
Adam Borowski9bfdc262018-07-18 04:10:44 +020069 if (use_unicode)
70 return screen_glyph_unicode(sel_cons, n / 2);
Jan Engelhardt759448f2007-07-15 23:40:40 -070071 return inverse_translate(sel_cons, screen_glyph(sel_cons, n),
Adam Borowski9bfdc262018-07-18 04:10:44 +020072 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073}
74
Alan Cox52894752012-03-02 15:00:02 +000075/**
76 * clear_selection - remove current selection
77 *
78 * Remove the current selection highlight, if any from the console
79 * holding the selection. The caller must hold the console lock.
80 */
81void clear_selection(void)
82{
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 highlight_pointer(-1); /* hide the pointer */
84 if (sel_start != -1) {
85 highlight(sel_start, sel_end);
86 sel_start = -1;
87 }
88}
Okash Khawaja496124e2019-04-17 13:21:13 +010089EXPORT_SYMBOL_GPL(clear_selection);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91/*
92 * User settable table: what characters are to be considered alphabetic?
Adam Borowski7f1534e2017-03-27 14:21:14 +020093 * 128 bits. Locked by the console lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 */
Adam Borowski7f1534e2017-03-27 14:21:14 +020095static u32 inwordLut[]={
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 0x00000000, /* control chars */
Adam Borowski7d6d44a2017-03-27 14:21:13 +020097 0x03FFE000, /* digits and "-./" */
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 0x87FFFFFE, /* uppercase and '_' */
99 0x07FFFFFE, /* lowercase */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100};
101
Adam Borowski9bfdc262018-07-18 04:10:44 +0200102static inline int inword(const u32 c)
103{
Adam Borowski7f1534e2017-03-27 14:21:14 +0200104 return c > 0x7f || (( inwordLut[c>>5] >> (c & 0x1F) ) & 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Alan Cox52894752012-03-02 15:00:02 +0000107/**
108 * set loadlut - load the LUT table
109 * @p: user table
110 *
111 * Load the LUT table from user space. The caller must hold the console
112 * lock. Make a temporary copy so a partial update doesn't make a mess.
113 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114int sel_loadlut(char __user *p)
115{
Adam Borowski7f1534e2017-03-27 14:21:14 +0200116 u32 tmplut[ARRAY_SIZE(inwordLut)];
117 if (copy_from_user(tmplut, (u32 __user *)(p+4), sizeof(inwordLut)))
Alan Cox52894752012-03-02 15:00:02 +0000118 return -EFAULT;
Adam Borowski7f1534e2017-03-27 14:21:14 +0200119 memcpy(inwordLut, tmplut, sizeof(inwordLut));
Alan Cox52894752012-03-02 15:00:02 +0000120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123/* does screen address p correspond to character at LH/RH edge of screen? */
124static inline int atedge(const int p, int size_row)
125{
126 return (!(p % size_row) || !((p + 2) % size_row));
127}
128
Adam Borowskidf155d22018-07-18 04:10:43 +0200129/* stores the char in UTF8 and returns the number of bytes used (1-4) */
130static int store_utf8(u32 c, char *p)
Jan Engelhardt759448f2007-07-15 23:40:40 -0700131{
132 if (c < 0x80) {
133 /* 0******* */
134 p[0] = c;
135 return 1;
136 } else if (c < 0x800) {
137 /* 110***** 10****** */
138 p[0] = 0xc0 | (c >> 6);
139 p[1] = 0x80 | (c & 0x3f);
140 return 2;
Adam Borowskidf155d22018-07-18 04:10:43 +0200141 } else if (c < 0x10000) {
Jan Engelhardt759448f2007-07-15 23:40:40 -0700142 /* 1110**** 10****** 10****** */
143 p[0] = 0xe0 | (c >> 12);
144 p[1] = 0x80 | ((c >> 6) & 0x3f);
145 p[2] = 0x80 | (c & 0x3f);
146 return 3;
Adam Borowskidf155d22018-07-18 04:10:43 +0200147 } else if (c < 0x110000) {
148 /* 11110*** 10****** 10****** 10****** */
149 p[0] = 0xf0 | (c >> 18);
150 p[1] = 0x80 | ((c >> 12) & 0x3f);
151 p[2] = 0x80 | ((c >> 6) & 0x3f);
152 p[3] = 0x80 | (c & 0x3f);
153 return 4;
154 } else {
155 /* outside Unicode, replace with U+FFFD */
156 p[0] = 0xef;
157 p[1] = 0xbf;
158 p[2] = 0xbd;
159 return 3;
160 }
Jan Engelhardt759448f2007-07-15 23:40:40 -0700161}
162
Alan Cox52894752012-03-02 15:00:02 +0000163/**
Okash Khawaja496124e2019-04-17 13:21:13 +0100164 * set_selection_user - set the current selection.
Alan Cox52894752012-03-02 15:00:02 +0000165 * @sel: user selection info
166 * @tty: the console tty
167 *
168 * Invoked by the ioctl handle for the vt layer.
169 *
170 * The entire selection process is managed under the console_lock. It's
171 * a lot under the lock but its hardly a performance path
172 */
Okash Khawaja496124e2019-04-17 13:21:13 +0100173int set_selection_user(const struct tiocl_selection __user *sel,
174 struct tty_struct *tty)
175{
176 struct tiocl_selection v;
177
178 if (copy_from_user(&v, sel, sizeof(*sel)))
179 return -EFAULT;
180
181 return set_selection_kernel(&v, tty);
182}
183
Jiri Slaby4b70dd52020-02-28 12:54:05 +0100184static int __set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct vc_data *vc = vc_cons[fg_console].d;
Al Viro2a479aa2017-09-29 12:40:54 -0400187 int new_sel_start, new_sel_end, spc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 char *bp, *obp;
Jan Engelhardt759448f2007-07-15 23:40:40 -0700189 int i, ps, pe, multiplier;
Adam Borowski9bfdc262018-07-18 04:10:44 +0200190 u32 c;
Jiri Slaby07e61242020-02-10 09:11:31 +0100191 int mode, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 poke_blanked_console();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Okash Khawaja496124e2019-04-17 13:21:13 +0100195 v->xs = min_t(u16, v->xs - 1, vc->vc_cols - 1);
196 v->ys = min_t(u16, v->ys - 1, vc->vc_rows - 1);
197 v->xe = min_t(u16, v->xe - 1, vc->vc_cols - 1);
198 v->ye = min_t(u16, v->ye - 1, vc->vc_rows - 1);
199 ps = v->ys * vc->vc_size_row + (v->xs << 1);
200 pe = v->ye * vc->vc_size_row + (v->xe << 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Okash Khawaja496124e2019-04-17 13:21:13 +0100202 if (v->sel_mode == TIOCL_SELCLEAR) {
Al Viro2a479aa2017-09-29 12:40:54 -0400203 /* useful for screendump without selection highlights */
204 clear_selection();
205 return 0;
206 }
207
Okash Khawaja496124e2019-04-17 13:21:13 +0100208 if (mouse_reporting() && (v->sel_mode & TIOCL_SELMOUSEREPORT)) {
209 mouse_report(tty, v->sel_mode & TIOCL_SELBUTTONMASK, v->xs,
210 v->ys);
Al Viro2a479aa2017-09-29 12:40:54 -0400211 return 0;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 if (ps > pe) /* make sel_start <= sel_end */
Gustavo A. R. Silvadf3d9a52017-11-10 16:05:45 -0600215 swap(ps, pe);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
Jiri Slaby07e61242020-02-10 09:11:31 +0100217 mutex_lock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (sel_cons != vc_cons[fg_console].d) {
219 clear_selection();
220 sel_cons = vc_cons[fg_console].d;
221 }
Alan Cox079c9532012-02-28 14:49:23 +0000222 mode = vt_do_kdgkbmode(fg_console);
223 if (mode == K_UNICODE)
224 use_unicode = 1;
225 else
226 use_unicode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
Okash Khawaja496124e2019-04-17 13:21:13 +0100228 switch (v->sel_mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 {
230 case TIOCL_SELCHAR: /* character-by-character selection */
231 new_sel_start = ps;
232 new_sel_end = pe;
233 break;
234 case TIOCL_SELWORD: /* word-by-word selection */
235 spc = isspace(sel_pos(ps));
236 for (new_sel_start = ps; ; ps -= 2)
237 {
238 if ((spc && !isspace(sel_pos(ps))) ||
239 (!spc && !inword(sel_pos(ps))))
240 break;
241 new_sel_start = ps;
242 if (!(ps % vc->vc_size_row))
243 break;
244 }
245 spc = isspace(sel_pos(pe));
246 for (new_sel_end = pe; ; pe += 2)
247 {
248 if ((spc && !isspace(sel_pos(pe))) ||
249 (!spc && !inword(sel_pos(pe))))
250 break;
251 new_sel_end = pe;
252 if (!((pe + 2) % vc->vc_size_row))
253 break;
254 }
255 break;
256 case TIOCL_SELLINE: /* line-by-line selection */
257 new_sel_start = ps - ps % vc->vc_size_row;
258 new_sel_end = pe + vc->vc_size_row
259 - pe % vc->vc_size_row - 2;
260 break;
261 case TIOCL_SELPOINTER:
262 highlight_pointer(pe);
Jiri Slaby07e61242020-02-10 09:11:31 +0100263 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 default:
Jiri Slaby07e61242020-02-10 09:11:31 +0100265 ret = -EINVAL;
266 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 }
268
269 /* remove the pointer */
270 highlight_pointer(-1);
271
272 /* select to end of line if on trailing space */
273 if (new_sel_end > new_sel_start &&
274 !atedge(new_sel_end, vc->vc_size_row) &&
275 isspace(sel_pos(new_sel_end))) {
276 for (pe = new_sel_end + 2; ; pe += 2)
277 if (!isspace(sel_pos(pe)) ||
278 atedge(pe, vc->vc_size_row))
279 break;
280 if (isspace(sel_pos(pe)))
281 new_sel_end = pe;
282 }
283 if (sel_start == -1) /* no current selection */
284 highlight(new_sel_start, new_sel_end);
285 else if (new_sel_start == sel_start)
286 {
287 if (new_sel_end == sel_end) /* no action required */
Jiri Slaby07e61242020-02-10 09:11:31 +0100288 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 else if (new_sel_end > sel_end) /* extend to right */
290 highlight(sel_end + 2, new_sel_end);
291 else /* contract from right */
292 highlight(new_sel_end + 2, sel_end);
293 }
294 else if (new_sel_end == sel_end)
295 {
296 if (new_sel_start < sel_start) /* extend to left */
297 highlight(new_sel_start, sel_start - 2);
298 else /* contract from left */
299 highlight(sel_start, new_sel_start - 2);
300 }
301 else /* some other case; start selection from scratch */
302 {
303 clear_selection();
304 highlight(new_sel_start, new_sel_end);
305 }
306 sel_start = new_sel_start;
307 sel_end = new_sel_end;
308
309 /* Allocate a new buffer before freeing the old one ... */
Adam Borowskidf155d22018-07-18 04:10:43 +0200310 multiplier = use_unicode ? 4 : 1; /* chars can take up to 4 bytes */
Kees Cook6da2ec52018-06-12 13:55:00 -0700311 bp = kmalloc_array((sel_end - sel_start) / 2 + 1, multiplier,
312 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (!bp) {
314 printk(KERN_WARNING "selection: kmalloc() failed\n");
315 clear_selection();
Jiri Slaby07e61242020-02-10 09:11:31 +0100316 ret = -ENOMEM;
317 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 }
Jesper Juhl735d5662005-11-07 01:01:29 -0800319 kfree(sel_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 sel_buffer = bp;
321
322 obp = bp;
323 for (i = sel_start; i <= sel_end; i += 2) {
Jan Engelhardt759448f2007-07-15 23:40:40 -0700324 c = sel_pos(i);
325 if (use_unicode)
326 bp += store_utf8(c, bp);
327 else
328 *bp++ = c;
329 if (!isspace(c))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 obp = bp;
331 if (! ((i + 2) % vc->vc_size_row)) {
332 /* strip trailing blanks from line and add newline,
333 unless non-space at end of line. */
334 if (obp != bp) {
335 bp = obp;
336 *bp++ = '\r';
337 }
338 obp = bp;
339 }
340 }
341 sel_buffer_lth = bp - sel_buffer;
Jiri Slaby07e61242020-02-10 09:11:31 +0100342unlock:
343 mutex_unlock(&sel_lock);
344 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
Jiri Slaby4b70dd52020-02-28 12:54:05 +0100346
347int set_selection_kernel(struct tiocl_selection *v, struct tty_struct *tty)
348{
349 int ret;
350
351 console_lock();
352 ret = __set_selection_kernel(v, tty);
353 console_unlock();
354
355 return ret;
356}
Okash Khawaja496124e2019-04-17 13:21:13 +0100357EXPORT_SYMBOL_GPL(set_selection_kernel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359/* Insert the contents of the selection buffer into the
360 * queue of the tty associated with the current console.
361 * Invoked by ioctl().
Jiri Slaby906cbe12011-07-14 14:35:14 +0200362 *
Alan Cox20f62572012-03-02 14:59:37 +0000363 * Locking: called without locks. Calls the ldisc wrongly with
364 * unsafe methods,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
366int paste_selection(struct tty_struct *tty)
367{
Alan Coxc9f19e92009-01-02 13:47:26 +0000368 struct vc_data *vc = tty->driver_data;
Alan Cox33f0f882006-01-09 20:54:13 -0800369 int pasted = 0;
370 unsigned int count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 struct tty_ldisc *ld;
372 DECLARE_WAITQUEUE(wait, current);
Jiri Slaby687bff02020-02-10 09:11:30 +0100373 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Torben Hohnac751efa2011-01-25 15:07:35 -0800375 console_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 poke_blanked_console();
Torben Hohnac751efa2011-01-25 15:07:35 -0800377 console_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Jiri Slaby7ee00fdb2012-10-18 22:26:32 +0200379 ld = tty_ldisc_ref_wait(tty);
Peter Hurleye55afd12016-01-10 22:41:01 -0800380 if (!ld)
381 return -EIO; /* ldisc was hung up */
Peter Hurleya7c8d582013-06-15 09:36:15 -0400382 tty_buffer_lock_exclusive(&vc->port);
Arnd Bergmann60af22d2010-06-01 22:53:06 +0200383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 add_wait_queue(&vc->paste_wait, &wait);
Jiri Slaby07e61242020-02-10 09:11:31 +0100385 mutex_lock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 while (sel_buffer && sel_buffer_lth > pasted) {
387 set_current_state(TASK_INTERRUPTIBLE);
Jiri Slaby687bff02020-02-10 09:11:30 +0100388 if (signal_pending(current)) {
389 ret = -EINTR;
390 break;
391 }
Peter Hurley97ef38b2016-04-09 17:11:36 -0700392 if (tty_throttled(tty)) {
Jiri Slaby07e61242020-02-10 09:11:31 +0100393 mutex_unlock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 schedule();
Jiri Slaby07e61242020-02-10 09:11:31 +0100395 mutex_lock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 continue;
397 }
Peter Hurley61e86cc2015-07-12 20:47:35 -0400398 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 count = sel_buffer_lth - pasted;
Peter Hurley24a89d12013-06-15 09:14:15 -0400400 count = tty_ldisc_receive_buf(ld, sel_buffer + pasted, NULL,
401 count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 pasted += count;
403 }
Jiri Slaby07e61242020-02-10 09:11:31 +0100404 mutex_unlock(&sel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 remove_wait_queue(&vc->paste_wait, &wait);
Milind Arun Choudharycc0a8fb2007-05-08 00:30:52 -0700406 __set_current_state(TASK_RUNNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Peter Hurleya7c8d582013-06-15 09:36:15 -0400408 tty_buffer_unlock_exclusive(&vc->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 tty_ldisc_deref(ld);
Jiri Slaby687bff02020-02-10 09:11:30 +0100410 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
Okash Khawaja496124e2019-04-17 13:21:13 +0100412EXPORT_SYMBOL_GPL(paste_selection);