vt_ioctl.c

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

C
1,220
字号
	/* the ioctls below read/set the flags usually shown in the leds */	/* don't use them - they will go away without warning */	case KDGKBLED:		ucval = kbd->ledflagstate | (kbd->default_ledflagstate << 4);		goto setchar;	case KDSKBLED:		if (!perm)			return -EPERM;		if (arg & ~0x77)			return -EINVAL;		kbd->ledflagstate = (arg & 7);		kbd->default_ledflagstate = ((arg >> 4) & 7);		set_leds();		return 0;	/* the ioctls below only set the lights, not the functions */	/* for those, see KDGKBLED and KDSKBLED above */	case KDGETLED:		ucval = getledstate();	setchar:		return put_user(ucval, (char __user *)arg);	case KDSETLED:		if (!perm)		  return -EPERM;		setledstate(kbd, arg);		return 0;	/*	 * A process can indicate its willingness to accept signals	 * generated by pressing an appropriate key combination.	 * Thus, one can have a daemon that e.g. spawns a new console	 * upon a keypress and then changes to it.	 * See also the kbrequest field of inittab(5).	 */	case KDSIGACCEPT:	{		extern int spawnpid, spawnsig;		if (!perm || !capable(CAP_KILL))		  return -EPERM;		if (arg < 1 || arg > _NSIG || arg == SIGKILL)		  return -EINVAL;		spawnpid = current->pid;		spawnsig = arg;		return 0;	}	case VT_SETMODE:	{		struct vt_mode tmp;		if (!perm)			return -EPERM;		if (copy_from_user(&tmp, up, sizeof(struct vt_mode)))			return -EFAULT;		if (tmp.mode != VT_AUTO && tmp.mode != VT_PROCESS)			return -EINVAL;		acquire_console_sem();		vt_cons[console]->vt_mode = tmp;		/* the frsig is ignored, so we set it to 0 */		vt_cons[console]->vt_mode.frsig = 0;		vt_cons[console]->vt_pid = current->pid;		/* no switch is required -- saw@shade.msu.ru */		vt_cons[console]->vt_newvt = -1; 		release_console_sem();		return 0;	}	case VT_GETMODE:	{		struct vt_mode tmp;		int rc;		acquire_console_sem();		memcpy(&tmp, &vt_cons[console]->vt_mode, sizeof(struct vt_mode));		release_console_sem();		rc = copy_to_user(up, &tmp, sizeof(struct vt_mode));		return rc ? -EFAULT : 0;	}	/*	 * Returns global vt state. Note that VT 0 is always open, since	 * it's an alias for the current VT, and people can't use it here.	 * We cannot return state for more than 16 VTs, since v_state is short.	 */	case VT_GETSTATE:	{		struct vt_stat __user *vtstat = up;		unsigned short state, mask;		if (put_user(fg_console + 1, &vtstat->v_active))			return -EFAULT;		state = 1;	/* /dev/tty0 is always open */		for (i = 0, mask = 2; i < MAX_NR_CONSOLES && mask; ++i, mask <<= 1)			if (VT_IS_IN_USE(i))				state |= mask;		return put_user(state, &vtstat->v_state);	}	/*	 * Returns the first available (non-opened) console.	 */	case VT_OPENQRY:		for (i = 0; i < MAX_NR_CONSOLES; ++i)			if (! VT_IS_IN_USE(i))				break;		ucval = i < MAX_NR_CONSOLES ? (i+1) : -1;		goto setint;		 	/*	 * ioctl(fd, VT_ACTIVATE, num) will cause us to switch to vt # num,	 * with num >= 1 (switches to vt 0, our console, are not allowed, just	 * to preserve sanity).	 */	case VT_ACTIVATE:		if (!perm)			return -EPERM;		if (arg == 0 || arg > MAX_NR_CONSOLES)			return -ENXIO;		arg--;		acquire_console_sem();		i = vc_allocate(arg);		release_console_sem();		if (i)			return i;		set_console(arg);		return 0;	/*	 * wait until the specified VT has been activated	 */	case VT_WAITACTIVE:		if (!perm)			return -EPERM;		if (arg == 0 || arg > MAX_NR_CONSOLES)			return -ENXIO;		return vt_waitactive(arg-1);	/*	 * If a vt is under process control, the kernel will not switch to it	 * immediately, but postpone the operation until the process calls this	 * ioctl, allowing the switch to complete.	 *	 * According to the X sources this is the behavior:	 *	0:	pending switch-from not OK	 *	1:	pending switch-from OK	 *	2:	completed switch-to OK	 */	case VT_RELDISP:		if (!perm)			return -EPERM;		if (vt_cons[console]->vt_mode.mode != VT_PROCESS)			return -EINVAL;		/*		 * Switching-from response		 */		if (vt_cons[console]->vt_newvt >= 0)		{			if (arg == 0)				/*				 * Switch disallowed, so forget we were trying				 * to do it.				 */				vt_cons[console]->vt_newvt = -1;			else			{				/*				 * The current vt has been released, so				 * complete the switch.				 */				int newvt;				acquire_console_sem();				newvt = vt_cons[console]->vt_newvt;				vt_cons[console]->vt_newvt = -1;				i = vc_allocate(newvt);				if (i) {					release_console_sem();					return i;				}				/*				 * When we actually do the console switch,				 * make sure we are atomic with respect to				 * other console switches..				 */				complete_change_console(newvt);				release_console_sem();			}		}		/*		 * Switched-to response		 */		else		{			/*			 * If it's just an ACK, ignore it			 */			if (arg != VT_ACKACQ)				return -EINVAL;		}		return 0;	 /*	  * Disallocate memory associated to VT (but leave VT1)	  */	 case VT_DISALLOCATE:		if (arg > MAX_NR_CONSOLES)			return -ENXIO;		if (arg == 0) {		    /* disallocate all unused consoles, but leave 0 */			acquire_console_sem();			for (i=1; i<MAX_NR_CONSOLES; i++)				if (! VT_BUSY(i))					vc_disallocate(i);			release_console_sem();		} else {			/* disallocate a single console, if possible */			arg--;			if (VT_BUSY(arg))				return -EBUSY;			if (arg) {			      /* leave 0 */				acquire_console_sem();				vc_disallocate(arg);				release_console_sem();			}		}		return 0;	case VT_RESIZE:	{		struct vt_sizes __user *vtsizes = up;		ushort ll,cc;		if (!perm)			return -EPERM;		if (get_user(ll, &vtsizes->v_rows) ||		    get_user(cc, &vtsizes->v_cols))			return -EFAULT;		for (i = 0; i < MAX_NR_CONSOLES; i++) {			acquire_console_sem();                        vc_resize(i, cc, ll);			release_console_sem();		}		return 0;	}	case VT_RESIZEX:	{		struct vt_consize __user *vtconsize = up;		ushort ll,cc,vlin,clin,vcol,ccol;		if (!perm)			return -EPERM;		if (verify_area(VERIFY_READ, vtconsize,				sizeof(struct vt_consize)))			return -EFAULT;		__get_user(ll, &vtconsize->v_rows);		__get_user(cc, &vtconsize->v_cols);		__get_user(vlin, &vtconsize->v_vlin);		__get_user(clin, &vtconsize->v_clin);		__get_user(vcol, &vtconsize->v_vcol);		__get_user(ccol, &vtconsize->v_ccol);		vlin = vlin ? vlin : vc->vc_scan_lines;		if (clin) {			if (ll) {				if (ll != vlin/clin)					return -EINVAL; /* Parameters don't add up */			} else 				ll = vlin/clin;		}		if (vcol && ccol) {			if (cc) {				if (cc != vcol/ccol)					return -EINVAL;			} else				cc = vcol/ccol;		}		if (clin > 32)			return -EINVAL;		    		for (i = 0; i < MAX_NR_CONSOLES; i++) {			if (!vc_cons[i].d)				continue;			acquire_console_sem();			if (vlin)				vc_cons[i].d->vc_scan_lines = vlin;			if (clin)				vc_cons[i].d->vc_font.height = clin;			vc_resize(i, cc, ll);			release_console_sem();		}  		return 0;	}	case PIO_FONT: {		if (!perm)			return -EPERM;		op.op = KD_FONT_OP_SET;		op.flags = KD_FONT_FLAG_OLD | KD_FONT_FLAG_DONT_RECALC;	/* Compatibility */		op.width = 8;		op.height = 0;		op.charcount = 256;		op.data = up;		return con_font_op(fg_console, &op);	}	case GIO_FONT: {		op.op = KD_FONT_OP_GET;		op.flags = KD_FONT_FLAG_OLD;		op.width = 8;		op.height = 32;		op.charcount = 256;		op.data = up;		return con_font_op(fg_console, &op);	}	case PIO_CMAP:                if (!perm)			return -EPERM;                return con_set_cmap(up);	case GIO_CMAP:                return con_get_cmap(up);	case PIO_FONTX:	case GIO_FONTX:		return do_fontx_ioctl(cmd, up, perm, &op);	case PIO_FONTRESET:	{		if (!perm)			return -EPERM;#ifdef BROKEN_GRAPHICS_PROGRAMS		/* With BROKEN_GRAPHICS_PROGRAMS defined, the default		   font is not saved. */		return -ENOSYS;#else		{		op.op = KD_FONT_OP_SET_DEFAULT;		op.data = NULL;		i = con_font_op(fg_console, &op);		if (i) return i;		con_set_default_unimap(fg_console);		return 0;		}#endif	}	case KDFONTOP: {		if (copy_from_user(&op, up, sizeof(op)))			return -EFAULT;		if (!perm && op.op != KD_FONT_OP_GET)			return -EPERM;		i = con_font_op(console, &op);		if (i) return i;		if (copy_to_user(up, &op, sizeof(op)))			return -EFAULT;		return 0;	}	case PIO_SCRNMAP:		if (!perm)			return -EPERM;		return con_set_trans_old(up);	case GIO_SCRNMAP:		return con_get_trans_old(up);	case PIO_UNISCRNMAP:		if (!perm)			return -EPERM;		return con_set_trans_new(up);	case GIO_UNISCRNMAP:		return con_get_trans_new(up);	case PIO_UNIMAPCLR:	      { struct unimapinit ui;		if (!perm)			return -EPERM;		i = copy_from_user(&ui, up, sizeof(struct unimapinit));		if (i) return -EFAULT;		con_clear_unimap(console, &ui);		return 0;	      }	case PIO_UNIMAP:	case GIO_UNIMAP:		return do_unimap_ioctl(cmd, up, perm, console);	case VT_LOCKSWITCH:		if (!capable(CAP_SYS_TTY_CONFIG))		   return -EPERM;		vt_dont_switch = 1;		return 0;	case VT_UNLOCKSWITCH:		if (!capable(CAP_SYS_TTY_CONFIG))		   return -EPERM;		vt_dont_switch = 0;		return 0;	default:		return -ENOIOCTLCMD;	}}/* * Sometimes we want to wait until a particular VT has been activated. We * do it in a very simple manner. Everybody waits on a single queue and * get woken up at once. Those that are satisfied go on with their business, * while those not ready go back to sleep. Seems overkill to add a wait * to each vt just for this - usually this does nothing! */static DECLARE_WAIT_QUEUE_HEAD(vt_activate_queue);/* * Sleeps until a vt is activated, or the task is interrupted. Returns * 0 if activation, -EINTR if interrupted. */int vt_waitactive(int vt){	int retval;	DECLARE_WAITQUEUE(wait, current);	add_wait_queue(&vt_activate_queue, &wait);	for (;;) {		set_current_state(TASK_INTERRUPTIBLE);		retval = 0;		if (vt == fg_console)			break;		retval = -EINTR;		if (signal_pending(current))			break;		schedule();	}	remove_wait_queue(&vt_activate_queue, &wait);	current->state = TASK_RUNNING;	return retval;}#define vt_wake_waitactive() wake_up(&vt_activate_queue)void reset_vc(unsigned int new_console){	vt_cons[new_console]->vc_mode = KD_TEXT;	kbd_table[new_console].kbdmode = VC_XLATE;	vt_cons[new_console]->vt_mode.mode = VT_AUTO;	vt_cons[new_console]->vt_mode.waitv = 0;	vt_cons[new_console]->vt_mode.relsig = 0;	vt_cons[new_console]->vt_mode.acqsig = 0;	vt_cons[new_console]->vt_mode.frsig = 0;	vt_cons[new_console]->vt_pid = -1;	vt_cons[new_console]->vt_newvt = -1;	if (!in_interrupt())    /* Via keyboard.c:SAK() - akpm */		reset_palette(new_console) ;}/* * Performs the back end of a vt switch */void complete_change_console(unsigned int new_console){	unsigned char old_vc_mode;	last_console = fg_console;	/*	 * If we're switching, we could be going from KD_GRAPHICS to	 * KD_TEXT mode or vice versa, which means we need to blank or	 * unblank the screen later.	 */	old_vc_mode = vt_cons[fg_console]->vc_mode;	switch_screen(new_console);	/*	 * This can't appear below a successful kill_proc().  If it did,	 * then the *blank_screen operation could occur while X, having	 * received acqsig, is waking up on another processor.  This	 * condition can lead to overlapping accesses to the VGA range	 * and the framebuffer (causing system lockups).	 *	 * To account for this we duplicate this code below only if the	 * controlling process is gone and we've called reset_vc.	 */	if (old_vc_mode != vt_cons[new_console]->vc_mode)	{		if (vt_cons[new_console]->vc_mode == KD_TEXT)			do_unblank_screen(1);		else			do_blank_screen(1);	}	/*	 * If this new console is under process control, send it a signal	 * telling it that it has acquired. Also check if it has died and	 * clean up (similar to logic employed in change_console())	 */	if (vt_cons[new_console]->vt_mode.mode == VT_PROCESS)	{		/*		 * Send the signal as privileged - kill_proc() will		 * tell us if the process has gone or something else		 * is awry		 */		if (kill_proc(vt_cons[new_console]->vt_pid,			      vt_cons[new_console]->vt_mode.acqsig,			      1) != 0)		{		/*		 * The controlling process has died, so we revert back to		 * normal operation. In this case, we'll also change back		 * to KD_TEXT mode. I'm not sure if this is strictly correct		 * but it saves the agony when the X server dies and the screen		 * remains blanked due to KD_GRAPHICS! It would be nice to do		 * this outside of VT_PROCESS but there is no single process		 * to account for and tracking tty count may be undesirable.		 */		        reset_vc(new_console);			if (old_vc_mode != vt_cons[new_console]->vc_mode)			{				if (vt_cons[new_console]->vc_mode == KD_TEXT)					do_unblank_screen(1);				else					do_blank_screen(1);			}		}	}	/*	 * Wake anyone waiting for their VT to activate	 */	vt_wake_waitactive();	return;}/* * Performs the front-end of a vt switch */void change_console(unsigned int new_console){        if ((new_console == fg_console) || (vt_dont_switch))                return;        if (!vc_cons_allocated(new_console))		return;	/*	 * If this vt is in process mode, then we need to handshake with	 * that process before switching. Essentially, we store where that	 * vt wants to switch to and wait for it to tell us when it's done	 * (via VT_RELDISP ioctl).	 *	 * We also check to see if the controlling process still exists.	 * If it doesn't, we reset this vt to auto mode and continue.	 * This is a cheap way to track process control. The worst thing	 * that can happen is: we send a signal to a process, it dies, and	 * the switch gets "lost" waiting for a response; hopefully, the	 * user will try again, we'll detect the process is gone (unless	 * the user waits just the right amount of time :-) and revert the	 * vt to auto control.	 */	if (vt_cons[fg_console]->vt_mode.mode == VT_PROCESS)	{		/*		 * Send the signal as privileged - kill_proc() will		 * tell us if the process has gone or something else		 * is awry		 */		if (kill_proc(vt_cons[fg_console]->vt_pid,			      vt_cons[fg_console]->vt_mode.relsig,			      1) == 0)		{			/*			 * It worked. Mark the vt to switch to and			 * return. The process needs to send us a			 * VT_RELDISP ioctl to complete the switch.			 */			vt_cons[fg_console]->vt_newvt = new_console;			return;		}		/*		 * The controlling process has died, so we revert back to		 * normal operation. In this case, we'll also change back		 * to KD_TEXT mode. I'm not sure if this is strictly correct		 * but it saves the agony when the X server dies and the screen		 * remains blanked due to KD_GRAPHICS! It would be nice to do		 * this outside of VT_PROCESS but there is no single process		 * to account for and tracking tty count may be undesirable.		 */		reset_vc(fg_console);		/*		 * Fall through to normal (VT_AUTO) handling of the switch...		 */	}	/*	 * Ignore all switches in KD_GRAPHICS+VT_AUTO mode	 */	if (vt_cons[fg_console]->vc_mode == KD_GRAPHICS)		return;	complete_change_console(new_console);}

⌨️ 快捷键说明

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