aboutsummaryrefslogtreecommitdiffstats
path: root/vt.c
blob: 084a072592ad3434f992ab68a494fcb5d2e23255 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* 
 * Copyright 1996-2002 - Karl R. Hakimian and David Fries
 *
 * This file is part of datalink.
 *
 * Datalink is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Datalink is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with datalink (see COPYING); if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

/*
 * Linux version of Timex/Microsoft SDK for Timex datalink watches.
 * SVGA needs a terminal when going to graphics mode.  This will take
 * care of that.
 */

#include <unistd.h>
#include <stdio.h>
#include <sys/vt.h>
#include <sys/ioctl.h>
#include <sys/fcntl.h>

#define VTFMT "/dev/tty%d"

int open_vt()
{
	struct vt_stat vts;
	int fd;
	int newvt;
	char buf[1024];

/* Need to become root again to deal with vt's */
	seteuid(0);

	if ((fd = open("/dev/tty", O_RDWR)) == -1)
	{
		perror("open");
		return (-1);
	}

/* See if we are on a VT. */
	if (ioctl(fd, VT_GETSTATE, &vts) == 0)
	{
		close(fd);
		return (0);
	}

/* We are not on a VT, switch to one. */
	close(fd);

	if ((fd = open("/dev/tty0", O_RDWR)) == -1)
	{
		perror("open");
		return (-1);
	}

/* Get info on current vt. */
	if (ioctl(fd, VT_GETSTATE, &vts) == -1)
	{
		perror("VT_GETSTATE");
		return (-1);
	}

/* Open a new vt. */
	if (ioctl(fd, VT_OPENQRY, &newvt) == -1)
	{
		perror("VT_OPENQRY");
		return (-1);
	}

	if (newvt == -1)
	{
		fprintf(stderr, "No free vts to open\n");
		return (-1);
	}

/* Make the new vt, the active vt. */
	if (ioctl(fd, VT_ACTIVATE, newvt) == -1)
	{
		perror("VT_ACTIVATE");
		return (-1);
	}

	if (ioctl(fd, VT_WAITACTIVE, newvt) == -1)
	{
		perror("VT_WAITACTIVE");
		return (-1);
	}

	close(fd);
	close(2);
	close(1);
	close(0);
/*
   Detach from controlling terminal here so that we can get a new
   controlling terminal.
*/
	setsid();
	sprintf(buf, VTFMT, newvt);
	(void) open(buf, O_RDWR);
	(void) open(buf, O_RDWR);
	(void) open(buf, O_RDWR);

/* No longer need root privs - drop them*/
	seteuid(getuid());

	return (vts.v_active);
}

void close_vt(int oldvt)
{
	int fd;
	struct vt_stat vts;
	struct vt_mode VT;
	int vt;

	if (!oldvt)
		return;

/* Need to become root again to deal with vt's */
	seteuid(0);

/* SVGA lib sets the tty change mode to require a call back.
 * We aren't in graphics mode, so set it back to auto.
 */
	if( ioctl(0, VT_GETMODE, &VT) == -1)
	{
		perror("VT_GETMODE");
		return;
	}

	VT.mode = VT_AUTO;
	if( ioctl(0, VT_SETMODE, &VT)== -1)
	{
		perror("VT_SETMODE");
		return;
	}

/* Get info on current vt. */
	if (ioctl(0, VT_GETSTATE, &vts) == -1)
	{
		perror("VT_GETSTATE");
		return;
	}

	vt = vts.v_active;

/* Switch back to previous vt. */

	if (ioctl(0, VT_ACTIVATE, oldvt) == -1)
	{
		perror("VT_ACTIVATE");
		return;
	}

	if (ioctl(0, VT_WAITACTIVE, oldvt) == -1)
	{
		perror("VT_WAITACTIVE");
		return;
	}

	close(0);
	close(1);
	close(2);
	setsid();

/* Open current vt. */
	if ((fd = open("/dev/tty0", O_RDWR)) == -1)
	{
		perror("open");
		return;
	}

/* Free up our old vt. */
	if (ioctl(fd, VT_DISALLOCATE, vt) == -1)
	{
		perror("VT_DISALLOCATE");
		return;
	}

	close(fd);

/* No longer need root privs - drop them*/
	seteuid(getuid());
}