-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathx2vnc.c
140 lines (113 loc) · 2.95 KB
/
x2vnc.c
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
/*
* Copyright (C) 1997, 1998 Olivetti & Oracle Research Laboratory
*
* This 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.
*
* This software 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 this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*
* Seriously modified by Fredrik Hübinette <[email protected]>
*/
/*
* x2vnc - control VNC displays without showing them
*/
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <x2vnc.h>
int temp_file_fd=0;
int main(int argc, char **argv)
{
fd_set fds;
struct timeval tv, *tvp;
int msWait;
processArgs(argc, argv);
if (listenSpecified) {
listenForIncomingConnections();
/* returns only with a succesful connection */
}
else
{
if(reconnect)
{
long last_fork=0;
char *tmpdir="/tmp";
char tmpfile[1024];
if(getenv("TMPDIR") && strlen(tmpdir) < 900)
tmpdir=getenv("TMPDIR");
sprintf(tmpfile, "%s/x2vnc-%d-%d",
tmpdir,
getpid(),
time(0));
temp_file_fd=open(tmpfile, O_RDWR | O_CREAT | O_EXCL, 0600);
unlink(tmpfile);
while(1)
{
int status, pid;
/* limit how often we restart */
if(time(0) - last_fork < 1) sleep(2);
last_fork=time(0);
switch (pid=fork())
{
case -1:
perror("fork");
exit(1);
case 0:
break;
default:
while(waitpid(pid, &status, 0) < 0 && errno==EINTR);
if(debug)
fprintf(stderr,"Child exited with status %d\n",status);
continue;
}
break;
}
}
if (!ConnectToRFBServer(hostname, port)) exit(1);
}
if (!InitialiseRFBConnection(rfbsock)) exit(1);
if (!CreateXWindow()) exit(1);
if (!SetFormatAndEncodings()) {
exit(1);
}
while (1)
{
/*
* Always handle all X events before doing select. This is the
* simplest way of ensuring that we don't block in select while
* Xlib has some events on its queue.
*/
if (!HandleXEvents()) {
exit(1);
}
tv.tv_sec = 5;
tv.tv_usec = 0;
FD_ZERO(&fds);
FD_SET(ConnectionNumber(dpy),&fds);
FD_SET(rfbsock,&fds);
if (select(FD_SETSIZE, &fds, NULL, NULL, &tv) < 0) {
perror("select");
exit(1);
}
if (FD_ISSET(rfbsock, &fds)) {
if (!HandleRFBServerMessage()) {
exit(1);
}
}
}
return 0;
}