zhangzengfei
2020-03-31 0ce893695d32ab686f9e2309509e80c6feb0d380
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
package com.basic.security.manager.erlang;
 
import android.os.SystemClock;
import android.util.Log;
 
import com.basic.security.base.BaseApplication;
import com.basic.security.utils.Constants;
import com.jaredrummler.android.shell.CommandResult;
import com.jaredrummler.android.shell.Shell;
 
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
 
public class ErlangProcess {
 
    public static boolean needRestartErlProcess = true;
    public static boolean erlangProcessStarted = false;
    public static boolean erlangProcessCanStart = false;
 
 
    public static void restartErlangProcess() {
        try {
            /*new Thread(){
                @Override
                public void run() {
                    try {
                        erlangProcessStarted = false;
                        stopErlangProcess();
                        long start = System.currentTimeMillis();
                        CommandResult commandResult = Shell.SU.run("cd /data/data/com.basic.security/files/erlang/",
                                "export LD_LIBRARY_PATH=/data/data/com.termux/files/usr/lib/",
                                "/data/data/com.termux/files/usr/bin/erl -pa /data/data/com.basic.security/files/erlang/ -name " + Constants.erlangLocalNode + " -setcookie 123 -s socket_server start_server");
 
                        System.out.println("end-begin="+(System.currentTimeMillis()-start));
                        System.out.println(commandResult);
                        erlangProcessStarted = true;
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }.start();*/
            new MyThread().start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public static void start() {
        BaseApplication.getApplication().executorService.execute(() -> {
            SqlCacheDeviceManager.downloadSqlCacheList();
            DeviceManager.syncDeviceInfoAtAppStart();
            SystemClock.sleep(1000);
            while (true) {
                if (erlangProcessCanStart) {
                    try {
                        if (erlProcessCount() != 3) {
                            restartErlangProcess();
                        } else {
                            if (needRestartErlProcess) {
                                restartErlangProcess();
                                needRestartErlProcess = false;
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                SystemClock.sleep(1000 * 3);
            }
        });
    }
 
    public static List<String> getErlPIDs() {
        boolean mUseRoot = true;
        String TAG = "erl";
        List<String> syncthingPIDs = new ArrayList<String>();
        Process ps = null;
        DataOutputStream psOut = null;
        BufferedReader br = null;
        try {
            ps = Runtime.getRuntime().exec((mUseRoot) ? "su" : "sh");
            psOut = new DataOutputStream(ps.getOutputStream());
            psOut.writeBytes("ps\n");
            psOut.writeBytes("exit\n");
            psOut.flush();
            ps.waitFor();
            br = new BufferedReader(new InputStreamReader(ps.getInputStream(), "UTF-8"));
            String line;
            while ((line = br.readLine()) != null) {
                if (line.contains("erl")) {
                    String syncthingPID = line.trim().split("\\s+")[1];
                    syncthingPIDs.add(syncthingPID);
                }
            }
        } catch (IOException | InterruptedException e) {
            Log.w(TAG, "Failed to list Syncthing processes", e);
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (psOut != null) {
                    psOut.close();
                }
            } catch (IOException e) {
                Log.w(TAG, "Failed to close psOut stream", e);
            }
            if (ps != null) {
                ps.destroy();
            }
        }
        return syncthingPIDs;
    }
 
    public static int erlProcessCount() {
        return getErlPIDs().size();
    }
 
    public static void stopErlangProcess() {
        try {
            List<String> erlPids = getErlPIDs();
            for (String erlPid : erlPids) {
                try {
                    Shell.SU.run("kill -9 " + erlPid);
                } catch (Exception e) {
                    System.out.println("stopErlangProcess " + e.getMessage());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    // 将线程类声明为私有的静态内部类避免了内存泄漏问题
    private static class MyThread extends Thread {
        @Override
        public void run() {
            try {
                erlangProcessStarted = false;
                stopErlangProcess();
                long start = System.currentTimeMillis();
                CommandResult commandResult = Shell.SU.run("cd /data/data/com.basic.security/files/erlang/",
                        "export LD_LIBRARY_PATH=/data/data/com.termux/files/usr/lib/",
                        "/data/data/com.termux/files/usr/bin/erl -pa /data/data/com.basic.security/files/erlang/ -name " + Constants.erlangLocalNode + " -setcookie 123 -s socket_server start_server");
 
                System.out.println("end-begin=" + (System.currentTimeMillis() - start));
                System.out.println(commandResult);
                erlangProcessStarted = true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
 
}