Log in New API Help About

JohnnyK - Changing VT without keys

3.2 KB of Plain text
Created 11 months, 2 weeks ago — expires in 17 days
http://dpaste.com/3QZQ7Q5
SOFT WRAP RAW TEXT DUPLICATE
Hi Guys,
  Love the show.  I just wanted to write in to try and answer Marc’s question.  According to this https://forums.freebsd.org/threads/switch-active-console-programmatically.65336/ you just use vidcontrol -s 2 < /dev/ttyv0 To switch vt’s. The 2 there is the VT.  You could probably put this command in a simple shell script and pass it the VT to switch too.  I’m not at my BSD box right now so I can try it out.  Anyway love the show and keep up the good work.

-------------------------------------------

Hi Guys,

  Now that I made it home to my beloved FreeBSD box I was able to knock together some short C code that will let switch VT's just by doing as a user with sudo this.

$ sudo ./chvt 2
and it will switch you to that vt. 
I used clang to compile with the following line

$ cc -o chvt chvt.c
Again this is based on the same URL below.  It still needs to properly locate the main VT and detect when the user enters a number for a TTY that does not exist but so far it works for me.  I have attached the C code.  I have never contributed any code to FreeBSD so not sure where to go from here.  I know I probably need to clean it up more but wanted to get it too you as soon as possible and while I was thinking about it.

-------------------------------------------


  /***************************************************************************************
*  Author: Johnny Kinsey ph: 9044758422
*
*  Description: This is a simple program to switch between Virtual Terminals
*  in Freebsd.
*
*  compile: cc -o chvt chvt.c
*
*  usage: sudo ./chvt 0
*
*  The main part of this code is from Tobik@ user's post here
*  https://forums.freebsd.org/threads/switch-active-console-programmatically.65336/
*
*  I just made it into a reusable command line tool
***************************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/consio.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>


int main(int argc,char **argv)
{
	int virtualTerminalID = -1;
	int fileDescriptor = -1;
	if (argc == 2) {
		virtualTerminalID = atoi((char*)*++argv);
		if (virtualTerminalID > 9) {
			printf("Virtual Terminal ID %d out of range.\n", virtualTerminalID);
			return 250;
		}

		fileDescriptor = open("/dev/ttyv0", O_RDWR); // hard coded root tty but may get from /etc/ttys later
		if (fileDescriptor < 0 && errno == EACCES)
			fileDescriptor = open("/dev/ttyv0", O_RDONLY);
		if (fileDescriptor < 0 && errno == EACCES)
			fileDescriptor = open("/dev/ttyv0", O_WRONLY);
		if (fileDescriptor < 0)
			return -1;
				// When I tried to have the program retun the output of ioctl it got hung
		// probably because the vt that the program was initiated on left it's not
		// running in?  Need more knowlege of ioctl
		if (ioctl(fileDescriptor, VT_ACTIVATE, virtualTerminalID)) {
			printf("chvt: VT_ACTIVATE\n");
			exit(1);
		}
		if (ioctl(fileDescriptor, VT_WAITACTIVE, virtualTerminalID)) {
			printf("chvt: VT_WAITACTIVE\n");
			exit(1);
		}
	}
	else {
		printf("Example: %s <virtual terminal id number>\n", (char *)*argv);
		return 255;
	};
	return 0;
}