1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 import java.lang.reflect.*;
18 
19 public class SigQuit implements Runnable {
20     private final static int sigquit;
21     private final static Method kill;
22     private final static int pid;
23 
24     static {
25         int pidTemp = -1;
26         int sigquitTemp = -1;
27         Method killTemp = null;
28 
29         try {
30             Class<?> osClass = Class.forName("android.system.Os");
31             Method getpid = osClass.getDeclaredMethod("getpid");
32             pidTemp = (Integer) getpid.invoke(null);
33 
34             Class<?> osConstants = Class.forName("android.system.OsConstants");
35             Field sigquitField = osConstants.getDeclaredField("SIGQUIT");
36             sigquitTemp = (Integer) sigquitField.get(null);
37 
38             killTemp = osClass.getDeclaredMethod("kill", int.class, int.class);
39         } catch (Exception e) {
40             if (!e.getClass().getName().equals("ErrnoException")) {
41                 e.printStackTrace(System.out);
42             }
43         }
44 
45         pid = pidTemp;
46         sigquit = sigquitTemp;
47         kill = killTemp;
48     }
49 
perform()50     public boolean perform() {
51         try {
52             kill.invoke(null, pid, sigquit);
53         } catch (Exception e) {
54             if (!e.getClass().getName().equals("ErrnoException")) {
55                 e.printStackTrace(System.out);
56             }
57         }
58         return true;
59     }
60 
run()61     public void run() {
62         long endTime = System.currentTimeMillis() + 5000;
63         System.out.println("Performing sigquits for 5 seconds");
64         while (System.currentTimeMillis() < endTime) {
65             perform();
66         }
67     }
68 }
69