4
* Copyright 2015 Tony George <teejee2008@gmail.com>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
30
using TeeJee.FileSystem;
31
using TeeJee.JsonHelper;
32
using TeeJee.ProcessHelper;
33
using TeeJee.GtkHelper;
37
public class Bash : AsyncTask {
39
public Pid bash_pid = -1;
40
public int status_code = -1;
41
private static Gee.HashMap<string, Regex> regex_list;
44
var temp_dir = TEMP_DIR + "/" + timestamp_for_path();
45
log_file = temp_dir + "/log.txt";
46
script_file = temp_dir + "/bash.sh";
47
working_dir = temp_dir;
49
init_regular_expressions();
52
private static void init_regular_expressions(){
53
if (regex_list != null){
54
return; // already initialized
57
regex_list = new Gee.HashMap<string,Regex>();
61
regex_list["status"] = new Regex("""status=([0-9\-]+)""");
64
log_error (e.message);
68
// execution ----------------------------
70
public void start_shell() {
71
dir_create(working_dir);
73
var sh = "bash -c 'pkexec bash'";
74
save_bash_script_temp(sh, script_file);
76
log_debug("Started bash shell");
78
if (status == AppStatus.RUNNING){
80
while ((status == AppStatus.RUNNING) && (bash_pid == -1)) {
82
var children = get_process_children(child_pid);
83
if (children.length > 0){
84
bash_pid = children[0];
88
log_debug("script pid: %d".printf(child_pid));
89
log_debug("bash shell pid: %d".printf(bash_pid));
93
public override void parse_stdout_line(string out_line){
94
if ((out_line == null) || (out_line.length == 0)) {
98
if (regex_list["status"].match(out_line, 0, out match)) {
99
status_code = int.parse(match.fetch(1));
101
stdout.printf(out_line + "\n");
105
public override void parse_stderr_line(string err_line){
106
stdout.printf(err_line + "\n");
110
public int execute(string line){
113
write_stdin("echo status=$?");
115
while (status_code == -1){
123
protected override void finish_task(){
124
log_debug("Bash: finish_task()");
127
public int read_status(){
128
var status_file = working_dir + "/status";
129
var f = File.new_for_path(status_file);
130
if (f.query_exists()){
131
var txt = file_read(status_file);
132
return int.parse(txt);