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
|
/*
* Copyright (C) 2016 Brian P. Hinz. All Rights Reserved.
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
package com.tigervnc.vncviewer;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import com.tigervnc.rdr.*;
import com.tigervnc.rfb.*;
import com.tigervnc.rfb.Exception;
import com.tigervnc.network.*;
import static com.tigervnc.vncviewer.Parameters.*;
public class ExtProcess implements Runnable {
private String cmd = null;
private LogWriter vlog = null;
private boolean shutdown = false;
private Process pid = null;
private static class MyProcessLogger extends Thread {
private final BufferedReader err;
private final LogWriter vlog;
public MyProcessLogger(Process p, LogWriter vlog) {
InputStreamReader reader =
new InputStreamReader(p.getErrorStream());
err = new BufferedReader(reader);
this.vlog = vlog;
}
@Override
public void run() {
try {
while (true) {
String msg = err.readLine();
if (msg != null)
vlog.info(msg);
}
} catch(java.io.IOException e) {
vlog.info(e.getMessage());
} finally {
try {
if (err != null)
err.close();
} catch (java.io.IOException e ) { }
}
}
}
private static class MyShutdownHook extends Thread {
private Process proc = null;
public MyShutdownHook(Process p) {
proc = p;
}
@Override
public void run() {
try {
proc.exitValue();
} catch (IllegalThreadStateException e) {
try {
// wait for CConn to shutdown the socket
Thread.sleep(500);
} catch(InterruptedException ie) { }
proc.destroy();
}
}
}
public ExtProcess(String command, LogWriter vlog, boolean shutdown) {
cmd = command;
this.vlog = vlog;
this.shutdown = shutdown;
}
public ExtProcess(String command, LogWriter vlog) {
this(command, vlog, false);
}
public ExtProcess(String command) {
this(command, null, false);
}
public void run() {
try {
Runtime runtime = Runtime.getRuntime();
pid = runtime.exec(cmd);
if (shutdown)
runtime.addShutdownHook(new MyShutdownHook(pid));
if (vlog != null)
new MyProcessLogger(pid, vlog).start();
pid.waitFor();
} catch(InterruptedException e) {
vlog.info(e.getMessage());
} catch(java.io.IOException e) {
vlog.info(e.getMessage());
}
}
//static LogWriter vlog = new LogWriter("ExtProcess");
}
|