vt_ioctl.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 1,220 行 · 第 1/2 页

C
1,220
字号
/* *  linux/drivers/char/vt_ioctl.c * *  Copyright (C) 1992 obz under the linux copyright * *  Dynamic diacritical handling - aeb@cwi.nl - Dec 1993 *  Dynamic keymap and string allocation - aeb@cwi.nl - May 1994 *  Restrict VT switching via ioctl() - grif@cs.ucr.edu - Dec 1995 *  Some code moved for less code duplication - Andi Kleen - Mar 1997 *  Check put/get_user, cleanups - acme@conectiva.com.br - Jun 2001 */#include <linux/config.h>#include <linux/types.h>#include <linux/errno.h>#include <linux/sched.h>#include <linux/tty.h>#include <linux/timer.h>#include <linux/kernel.h>#include <linux/kd.h>#include <linux/vt.h>#include <linux/string.h>#include <linux/slab.h>#include <linux/major.h>#include <linux/fs.h>#include <linux/console.h>#include <asm/io.h>#include <asm/uaccess.h>#include <linux/kbd_kern.h>#include <linux/vt_kern.h>#include <linux/kbd_diacr.h>#include <linux/selection.h>char vt_dont_switch;extern struct tty_driver *console_driver;#define VT_IS_IN_USE(i)	(console_driver->ttys[i] && console_driver->ttys[i]->count)#define VT_BUSY(i)	(VT_IS_IN_USE(i) || i == fg_console || i == sel_cons)/* * Console (vt and kd) routines, as defined by USL SVR4 manual, and by * experimentation and study of X386 SYSV handling. * * One point of difference: SYSV vt's are /dev/vtX, which X >= 0, and * /dev/console is a separate ttyp. Under Linux, /dev/tty0 is /dev/console, * and the vc start at /dev/ttyX, X >= 1. We maintain that here, so we will * always treat our set of vt as numbered 1..MAX_NR_CONSOLES (corresponding to * ttys 0..MAX_NR_CONSOLES-1). Explicitly naming VT 0 is illegal, but using * /dev/tty0 (fg_console) as a target is legal, since an implicit aliasing * to the current console is done by the main ioctl code. */struct vt_struct *vt_cons[MAX_NR_CONSOLES];/* Keyboard type: Default is KB_101, but can be set by machine * specific code. */unsigned char keyboard_type = KB_101;#ifdef CONFIG_X86#include <linux/syscalls.h>#endif/* * these are the valid i/o ports we're allowed to change. they map all the * video ports */#define GPFIRST 0x3b4#define GPLAST 0x3df#define GPNUM (GPLAST - GPFIRST + 1)#define i (tmp.kb_index)#define s (tmp.kb_table)#define v (tmp.kb_value)static inline intdo_kdsk_ioctl(int cmd, struct kbentry __user *user_kbe, int perm, struct kbd_struct *kbd){	struct kbentry tmp;	ushort *key_map, val, ov;	if (copy_from_user(&tmp, user_kbe, sizeof(struct kbentry)))		return -EFAULT;	switch (cmd) {	case KDGKBENT:		key_map = key_maps[s];		if (key_map) {		    val = U(key_map[i]);		    if (kbd->kbdmode != VC_UNICODE && KTYP(val) >= NR_TYPES)			val = K_HOLE;		} else		    val = (i ? K_HOLE : K_NOSUCHMAP);		return put_user(val, &user_kbe->kb_value);	case KDSKBENT:		if (!perm)			return -EPERM;		if (!i && v == K_NOSUCHMAP) {			/* disallocate map */			key_map = key_maps[s];			if (s && key_map) {			    key_maps[s] = NULL;			    if (key_map[0] == U(K_ALLOCATED)) {					kfree(key_map);					keymap_count--;			    }			}			break;		}		if (KTYP(v) < NR_TYPES) {		    if (KVAL(v) > max_vals[KTYP(v)])				return -EINVAL;		} else		    if (kbd->kbdmode != VC_UNICODE)				return -EINVAL;		/* ++Geert: non-PC keyboards may generate keycode zero */#if !defined(__mc68000__) && !defined(__powerpc__)		/* assignment to entry 0 only tests validity of args */		if (!i)			break;#endif		if (!(key_map = key_maps[s])) {			int j;			if (keymap_count >= MAX_NR_OF_USER_KEYMAPS &&			    !capable(CAP_SYS_RESOURCE))				return -EPERM;			key_map = (ushort *) kmalloc(sizeof(plain_map),						     GFP_KERNEL);			if (!key_map)				return -ENOMEM;			key_maps[s] = key_map;			key_map[0] = U(K_ALLOCATED);			for (j = 1; j < NR_KEYS; j++)				key_map[j] = U(K_HOLE);			keymap_count++;		}		ov = U(key_map[i]);		if (v == ov)			break;	/* nothing to do */		/*		 * Attention Key.		 */		if (((ov == K_SAK) || (v == K_SAK)) && !capable(CAP_SYS_ADMIN))			return -EPERM;		key_map[i] = U(v);		if (!s && (KTYP(ov) == KT_SHIFT || KTYP(v) == KT_SHIFT))			compute_shiftstate();		break;	}	return 0;}#undef i#undef s#undef vstatic inline int do_kbkeycode_ioctl(int cmd, struct kbkeycode __user *user_kbkc, int perm){	struct kbkeycode tmp;	int kc = 0;	if (copy_from_user(&tmp, user_kbkc, sizeof(struct kbkeycode)))		return -EFAULT;	switch (cmd) {	case KDGETKEYCODE:		kc = getkeycode(tmp.scancode);		if (kc >= 0)			kc = put_user(kc, &user_kbkc->keycode);		break;	case KDSETKEYCODE:		if (!perm)			return -EPERM;		kc = setkeycode(tmp.scancode, tmp.keycode);		break;	}	return kc;}static inline intdo_kdgkb_ioctl(int cmd, struct kbsentry __user *user_kdgkb, int perm){	struct kbsentry *kbs;	char *p;	u_char *q;	u_char __user *up;	int sz;	int delta;	char *first_free, *fj, *fnw;	int i, j, k;	int ret;	kbs = kmalloc(sizeof(*kbs), GFP_KERNEL);	if (!kbs) {		ret = -ENOMEM;		goto reterr;	}	/* we mostly copy too much here (512bytes), but who cares ;) */	if (copy_from_user(kbs, user_kdgkb, sizeof(struct kbsentry))) {		ret = -EFAULT;		goto reterr;	}	kbs->kb_string[sizeof(kbs->kb_string)-1] = '\0';	i = kbs->kb_func;	switch (cmd) {	case KDGKBSENT:		sz = sizeof(kbs->kb_string) - 1; /* sz should have been						  a struct member */		up = user_kdgkb->kb_string;		p = func_table[i];		if(p)			for ( ; *p && sz; p++, sz--)				if (put_user(*p, up++)) {					ret = -EFAULT;					goto reterr;				}		if (put_user('\0', up)) {			ret = -EFAULT;			goto reterr;		}		kfree(kbs);		return ((p && *p) ? -EOVERFLOW : 0);	case KDSKBSENT:		if (!perm) {			ret = -EPERM;			goto reterr;		}		q = func_table[i];		first_free = funcbufptr + (funcbufsize - funcbufleft);		for (j = i+1; j < MAX_NR_FUNC && !func_table[j]; j++) 			;		if (j < MAX_NR_FUNC)			fj = func_table[j];		else			fj = first_free;		delta = (q ? -strlen(q) : 1) + strlen(kbs->kb_string);		if (delta <= funcbufleft) { 	/* it fits in current buf */		    if (j < MAX_NR_FUNC) {			memmove(fj + delta, fj, first_free - fj);			for (k = j; k < MAX_NR_FUNC; k++)			    if (func_table[k])				func_table[k] += delta;		    }		    if (!q)		      func_table[i] = fj;		    funcbufleft -= delta;		} else {			/* allocate a larger buffer */		    sz = 256;		    while (sz < funcbufsize - funcbufleft + delta)		      sz <<= 1;		    fnw = (char *) kmalloc(sz, GFP_KERNEL);		    if(!fnw) {		      ret = -ENOMEM;		      goto reterr;		    }		    if (!q)		      func_table[i] = fj;		    if (fj > funcbufptr)			memmove(fnw, funcbufptr, fj - funcbufptr);		    for (k = 0; k < j; k++)		      if (func_table[k])			func_table[k] = fnw + (func_table[k] - funcbufptr);		    if (first_free > fj) {			memmove(fnw + (fj - funcbufptr) + delta, fj, first_free - fj);			for (k = j; k < MAX_NR_FUNC; k++)			  if (func_table[k])			    func_table[k] = fnw + (func_table[k] - funcbufptr) + delta;		    }		    if (funcbufptr != func_buf)		      kfree(funcbufptr);		    funcbufptr = fnw;		    funcbufleft = funcbufleft - delta + sz - funcbufsize;		    funcbufsize = sz;		}		strcpy(func_table[i], kbs->kb_string);		break;	}	ret = 0;reterr:	kfree(kbs);	return ret;}static inline int do_fontx_ioctl(int cmd, struct consolefontdesc __user *user_cfd, int perm, struct console_font_op *op){	struct consolefontdesc cfdarg;	int i;	if (copy_from_user(&cfdarg, user_cfd, sizeof(struct consolefontdesc))) 		return -EFAULT; 		switch (cmd) {	case PIO_FONTX:		if (!perm)			return -EPERM;		op->op = KD_FONT_OP_SET;		op->flags = KD_FONT_FLAG_OLD;		op->width = 8;		op->height = cfdarg.charheight;		op->charcount = cfdarg.charcount;		op->data = cfdarg.chardata;		return con_font_op(fg_console, op);	case GIO_FONTX: {		op->op = KD_FONT_OP_GET;		op->flags = KD_FONT_FLAG_OLD;		op->width = 8;		op->height = cfdarg.charheight;		op->charcount = cfdarg.charcount;		op->data = cfdarg.chardata;		i = con_font_op(fg_console, op);		if (i)			return i;		cfdarg.charheight = op->height;		cfdarg.charcount = op->charcount;		if (copy_to_user(user_cfd, &cfdarg, sizeof(struct consolefontdesc)))			return -EFAULT;		return 0;		}	}	return -EINVAL;}static inline int do_unimap_ioctl(int cmd, struct unimapdesc __user *user_ud, int perm, unsigned int console){	struct unimapdesc tmp;	int i = 0; 	if (copy_from_user(&tmp, user_ud, sizeof tmp))		return -EFAULT;	if (tmp.entries) {		i = verify_area(VERIFY_WRITE, tmp.entries, 						tmp.entry_ct*sizeof(struct unipair));		if (i) return i;	}	switch (cmd) {	case PIO_UNIMAP:		if (!perm)			return -EPERM;		return con_set_unimap(console, tmp.entry_ct, tmp.entries);	case GIO_UNIMAP:		if (!perm && fg_console != console)			return -EPERM;		return con_get_unimap(console, tmp.entry_ct, &(user_ud->entry_ct), tmp.entries);	}	return 0;}/* * We handle the console-specific ioctl's here.  We allow the * capability to modify any console, not just the fg_console.  */int vt_ioctl(struct tty_struct *tty, struct file * file,	     unsigned int cmd, unsigned long arg){	struct vt_struct *vt = (struct vt_struct *)tty->driver_data;	struct vc_data *vc = vc_cons[vt->vc_num].d;	struct console_font_op op;	/* used in multiple places here */	struct kbd_struct * kbd;	unsigned int console;	unsigned char ucval;	void __user *up = (void __user *)arg;	int i, perm;		console = vt->vc_num;	if (!vc_cons_allocated(console)) 	/* impossible? */		return -ENOIOCTLCMD;	/*	 * To have permissions to do most of the vt ioctls, we either have	 * to be the owner of the tty, or have CAP_SYS_TTY_CONFIG.	 */	perm = 0;	if (current->signal->tty == tty || capable(CAP_SYS_TTY_CONFIG))		perm = 1; 	kbd = kbd_table + console;	switch (cmd) {	case KIOCSOUND:		if (!perm)			return -EPERM;		if (arg)			arg = 1193182 / arg;		kd_mksound(arg, 0);		return 0;	case KDMKTONE:		if (!perm)			return -EPERM;	{		unsigned int ticks, count;				/*		 * Generate the tone for the appropriate number of ticks.		 * If the time is zero, turn off sound ourselves.		 */		ticks = HZ * ((arg >> 16) & 0xffff) / 1000;		count = ticks ? (arg & 0xffff) : 0;		if (count)			count = 1193182 / count;		kd_mksound(count, ticks);		return 0;	}	case KDGKBTYPE:		/*		 * this is naive.		 */		ucval = keyboard_type;		goto setchar;		/*		 * These cannot be implemented on any machine that implements		 * ioperm() in user level (such as Alpha PCs) or not at all.		 *		 * XXX: you should never use these, just call ioperm directly..		 */#ifdef CONFIG_X86	case KDADDIO:	case KDDELIO:		/*		 * KDADDIO and KDDELIO may be able to add ports beyond what		 * we reject here, but to be safe...		 */		if (arg < GPFIRST || arg > GPLAST)			return -EINVAL;		return sys_ioperm(arg, 1, (cmd == KDADDIO)) ? -ENXIO : 0;	case KDENABIO:	case KDDISABIO:		return sys_ioperm(GPFIRST, GPNUM,				  (cmd == KDENABIO)) ? -ENXIO : 0;#endif	/* Linux m68k/i386 interface for setting the keyboard delay/repeat rate */			case KDKBDREP:	{		struct kbd_repeat kbrep;		int err;				if (!capable(CAP_SYS_TTY_CONFIG))			return -EPERM;		if (copy_from_user(&kbrep, up, sizeof(struct kbd_repeat)))			return -EFAULT;		err = kbd_rate(&kbrep);		if (err)			return err;		if (copy_to_user(up, &kbrep, sizeof(struct kbd_repeat)))			return -EFAULT;		return 0;	}	case KDSETMODE:		/*		 * currently, setting the mode from KD_TEXT to KD_GRAPHICS		 * doesn't do a whole lot. i'm not sure if it should do any		 * restoration of modes or what...		 *		 * XXX It should at least call into the driver, fbdev's definitely		 * need to restore their engine state. --BenH		 */		if (!perm)			return -EPERM;		switch (arg) {		case KD_GRAPHICS:			break;		case KD_TEXT0:		case KD_TEXT1:			arg = KD_TEXT;		case KD_TEXT:			break;		default:			return -EINVAL;		}		if (vt_cons[console]->vc_mode == (unsigned char) arg)			return 0;		vt_cons[console]->vc_mode = (unsigned char) arg;		if (console != fg_console)			return 0;		/*		 * explicitly blank/unblank the screen if switching modes		 */		acquire_console_sem();		if (arg == KD_TEXT)			do_unblank_screen(1);		else			do_blank_screen(1);		release_console_sem();		return 0;	case KDGETMODE:		ucval = vt_cons[console]->vc_mode;		goto setint;	case KDMAPDISP:	case KDUNMAPDISP:		/*		 * these work like a combination of mmap and KDENABIO.		 * this could be easily finished.		 */		return -EINVAL;	case KDSKBMODE:		if (!perm)			return -EPERM;		switch(arg) {		  case K_RAW:			kbd->kbdmode = VC_RAW;			break;		  case K_MEDIUMRAW:			kbd->kbdmode = VC_MEDIUMRAW;			break;		  case K_XLATE:			kbd->kbdmode = VC_XLATE;			compute_shiftstate();			break;		  case K_UNICODE:			kbd->kbdmode = VC_UNICODE;			compute_shiftstate();			break;		  default:			return -EINVAL;		}		tty_ldisc_flush(tty);		return 0;	case KDGKBMODE:		ucval = ((kbd->kbdmode == VC_RAW) ? K_RAW :				 (kbd->kbdmode == VC_MEDIUMRAW) ? K_MEDIUMRAW :				 (kbd->kbdmode == VC_UNICODE) ? K_UNICODE :				 K_XLATE);		goto setint;	/* this could be folded into KDSKBMODE, but for compatibility	   reasons it is not so easy to fold KDGKBMETA into KDGKBMODE */	case KDSKBMETA:		switch(arg) {		  case K_METABIT:			clr_vc_kbd_mode(kbd, VC_META);			break;		  case K_ESCPREFIX:			set_vc_kbd_mode(kbd, VC_META);			break;		  default:			return -EINVAL;		}		return 0;	case KDGKBMETA:		ucval = (vc_kbd_mode(kbd, VC_META) ? K_ESCPREFIX : K_METABIT);	setint:		return put_user(ucval, (int __user *)arg); 	case KDGETKEYCODE:	case KDSETKEYCODE:		if(!capable(CAP_SYS_TTY_CONFIG))			perm=0;		return do_kbkeycode_ioctl(cmd, up, perm);	case KDGKBENT:	case KDSKBENT:		return do_kdsk_ioctl(cmd, up, perm, kbd);	case KDGKBSENT:	case KDSKBSENT:		return do_kdgkb_ioctl(cmd, up, perm);	case KDGKBDIACR:	{		struct kbdiacrs __user *a = up;		if (put_user(accent_table_size, &a->kb_cnt))			return -EFAULT;		if (copy_to_user(a->kbdiacr, accent_table, accent_table_size*sizeof(struct kbdiacr)))			return -EFAULT;		return 0;	}	case KDSKBDIACR:	{		struct kbdiacrs __user *a = up;		unsigned int ct;		if (!perm)			return -EPERM;		if (get_user(ct,&a->kb_cnt))			return -EFAULT;		if (ct >= MAX_DIACR)			return -EINVAL;		accent_table_size = ct;		if (copy_from_user(accent_table, a->kbdiacr, ct*sizeof(struct kbdiacr)))			return -EFAULT;		return 0;	}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?