~ubuntu-branches/ubuntu/natty/lightning-extension/natty-security

« back to all changes in this revision

Viewing changes to mozilla/build/mobile/sutagent/android/DoCommand.java

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2012-09-03 14:00:01 UTC
  • mfrom: (1.1.24)
  • Revision ID: package-import@ubuntu.com-20120903140001-iee9509f51oz7pqs
Tags: 1.8+build1-0ubuntu0.11.04.1
* New upstream stable release to support Thunderbird 16 (CALENDAR_1_8_BUILD1)
  - LP: #1062587

* Add extra Makefiles that are needed for the build
  - update debian/rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
83
83
import android.os.Environment;
84
84
import android.os.StatFs;
85
85
import android.os.SystemClock;
 
86
import android.text.TextUtils;
86
87
import android.util.DisplayMetrics;
87
88
import android.util.Log;
88
89
import android.view.Surface;
105
106
    String ffxProvider = "org.mozilla.ffxcp";
106
107
    String fenProvider = "org.mozilla.fencp";
107
108
 
108
 
    private final String prgVersion = "SUTAgentAndroid Version 1.08";
 
109
    private final String prgVersion = "SUTAgentAndroid Version 1.09";
109
110
 
110
111
    public enum Command
111
112
        {
162
163
        INST ("inst"),
163
164
        UPDT ("updt"),
164
165
        UNINST ("uninst"),
 
166
        UNINSTALL ("uninstall"),
165
167
        TEST ("test"),
166
168
        DBG ("dbg"),
167
169
        TRACE ("trace"),
169
171
        TZGET ("tzget"),
170
172
        TZSET ("tzset"),
171
173
        ADB ("adb"),
 
174
        CHMOD ("chmod"),
172
175
        UNKNOWN ("unknown");
173
176
 
174
177
        private final String theCmd;
308
311
                    if (Argc > 2) {
309
312
                        try {
310
313
                            lOff = Long.parseLong(Argv[2].trim());
311
 
                            System.out.println("offset = " + lOff);
312
314
                        } catch (NumberFormatException nfe) {
313
315
                            lOff = 0;
314
316
                            System.out.println("NumberFormatException: " + nfe.getMessage());
317
319
                    if (Argc == 4) {
318
320
                        try {
319
321
                            lLen = Long.parseLong(Argv[3].trim());
320
 
                            System.out.println("length = " + lLen);
321
322
                        } catch (NumberFormatException nfe) {
322
323
                            lLen = -1;
323
324
                            System.out.println("NumberFormatException: " + nfe.getMessage());
336
337
                    try
337
338
                        {
338
339
                        lArg = Long.parseLong(Argv[2].trim());
339
 
                        System.out.println("long l = " + lArg);
340
340
                        }
341
341
                    catch (NumberFormatException nfe)
342
342
                        {
358
358
 
359
359
            case UNINST:
360
360
                if (Argc >= 2)
361
 
                    strReturn = UnInstallApp(Argv[1], cmdOut);
362
 
                else
363
 
                    strReturn = sErrorPrefix + "Wrong number of arguments for inst command!";
 
361
                    strReturn = UnInstallApp(Argv[1], cmdOut, true);
 
362
                else
 
363
                    strReturn = sErrorPrefix + "Wrong number of arguments for uninst command!";
 
364
                break;
 
365
 
 
366
            case UNINSTALL:
 
367
                if (Argc >= 2)
 
368
                    strReturn = UnInstallApp(Argv[1], cmdOut, false);
 
369
                else
 
370
                    strReturn = sErrorPrefix + "Wrong number of arguments for uninstall command!";
364
371
                break;
365
372
 
366
373
            case ALRT:
735
742
                strReturn = Zip(Argv[1], (Argc == 3 ? Argv[2] : ""));
736
743
                break;
737
744
 
 
745
            case CHMOD:
 
746
                if (Argc == 2)
 
747
                    strReturn = ChmodDir(Argv[1]);
 
748
                else
 
749
                    strReturn = sErrorPrefix + "Wrong number of arguments for chmod command!";
 
750
                break;
 
751
 
738
752
            case HELP:
739
753
                strReturn = PrintUsage();
740
754
                break;
1501
1515
            }
1502
1516
            catch (FileNotFoundException e) {
1503
1517
                sRet += " file not found";
1504
 
                Log.d("SUT", "HashFile: "+e);
1505
1518
            }
1506
1519
            catch (IOException e) {
1507
1520
                sRet += " io exception";
1914
1927
            if (dstFile != null) {
1915
1928
                dstFile.flush();
1916
1929
                dstFile.close();
1917
 
                // set the new file's permissions to rwxrwxrwx, if possible
1918
 
                Process pProc = Runtime.getRuntime().exec("chmod 777 "+sTmpFileName);
1919
 
                RedirOutputThread outThrd = new RedirOutputThread(pProc, null);
1920
 
                outThrd.start();
1921
 
                try {
1922
 
                    outThrd.join(5000);
1923
 
                } catch (InterruptedException e) {
1924
 
                }
1925
1930
            }
1926
1931
 
1927
1932
            if (lRead == lSize)    {
2262
2267
            File dir = new File(sTmpDir);
2263
2268
 
2264
2269
            if (dir.mkdirs()) {
2265
 
                // set the new dir's permissions to rwxrwxrwx, if possible
2266
 
                try {
2267
 
                    Process pProc = Runtime.getRuntime().exec("chmod 777 "+sTmpDir);
2268
 
                    RedirOutputThread outThrd = new RedirOutputThread(pProc, null);
2269
 
                    outThrd.start();
2270
 
                    try {
2271
 
                        outThrd.join(5000);
2272
 
                    } catch (InterruptedException e) {
2273
 
                    }
2274
 
                } catch (IOException e) {
2275
 
                    e.printStackTrace();
2276
 
                }
2277
2270
                sRet = sDir + " successfully created";
2278
2271
            }
2279
2272
        }
2380
2373
 
2381
2374
    public String KillProcess(String sProcName, OutputStream out)
2382
2375
        {
2383
 
        String sTmp = "";
2384
 
        String sCmd = "kill";
2385
 
 
2386
2376
        String sRet = sErrorPrefix + "Unable to kill " + sProcName + "\n";
2387
2377
        ActivityManager aMgr = (ActivityManager) contextWrapper.getSystemService(Activity.ACTIVITY_SERVICE);
2388
2378
        List <ActivityManager.RunningAppProcessInfo> lProcesses = aMgr.getRunningAppProcesses();
2389
2379
        int lcv = 0;
2390
 
        String strProcName = "";
2391
 
        int    nPID = 0;
 
2380
        String sFoundProcName = "";
2392
2381
        int nProcs = 0;
 
2382
        boolean bFoundAppProcess = false;
2393
2383
 
2394
 
        if (lProcesses != null) 
 
2384
        if (lProcesses != null)
2395
2385
            nProcs = lProcesses.size();
2396
2386
 
2397
2387
        for (lcv = 0; lcv < nProcs; lcv++)
2398
2388
            {
2399
2389
            if (lProcesses.get(lcv).processName.contains(sProcName))
2400
2390
                {
2401
 
                strProcName = lProcesses.get(lcv).processName;
2402
 
                nPID = lProcesses.get(lcv).pid;
2403
 
                sRet = sErrorPrefix + "Failed to kill " + nPID + " " + strProcName + "\n";
2404
 
 
2405
 
                sCmd += " " + nPID;
 
2391
                sFoundProcName = lProcesses.get(lcv).processName;
 
2392
                bFoundAppProcess = true;
2406
2393
 
2407
2394
                try
2408
2395
                    {
2409
 
                    pProc = Runtime.getRuntime().exec(this.getSuArgs(sCmd));
 
2396
                    pProc = Runtime.getRuntime().exec(this.getSuArgs("kill " + lProcesses.get(lcv).pid));
2410
2397
                    RedirOutputThread outThrd = new RedirOutputThread(pProc, null);
2411
2398
                    outThrd.start();
2412
2399
                    outThrd.join(15000);
2413
 
                    sTmp = outThrd.strOutput;
 
2400
                    String sTmp = outThrd.strOutput;
2414
2401
                    Log.e("KILLPROCESS", sTmp);
2415
 
                    if (outThrd.nExitCode == 0) {
2416
 
                        sRet = "Successfully killed " + nPID + " " + strProcName + "\n";
2417
 
                        nPID = 0;
2418
 
                        break;
2419
 
                    } else {
2420
 
                        sRet = sErrorPrefix + "Failed to kill " + nPID + " " + strProcName + "\n";
2421
 
                    }
2422
2402
                    }
2423
2403
                catch (IOException e)
2424
2404
                    {
2429
2409
                    {
2430
2410
                    e.printStackTrace();
2431
2411
                    }
2432
 
 
2433
 
                // Give the messages a chance to be processed
2434
 
                try {
2435
 
                    Thread.sleep(2000);
2436
 
                    }
2437
 
                catch (InterruptedException e)
2438
 
                    {
2439
 
                    e.printStackTrace();
2440
 
                    }
2441
 
                break;
2442
2412
                }
2443
2413
            }
2444
2414
 
2445
 
        if (nPID > 0)
 
2415
        if (bFoundAppProcess)
2446
2416
            {
2447
 
            sRet = "Successfully killed " + nPID + " " + strProcName + "\n";
 
2417
            // Give the messages a chance to be processed
 
2418
            try {
 
2419
                Thread.sleep(2000);
 
2420
                }
 
2421
            catch (InterruptedException e)
 
2422
                {
 
2423
                e.printStackTrace();
 
2424
                }
 
2425
 
 
2426
            sRet = "Successfully killed " + sProcName + "\n";
2448
2427
            lProcesses = aMgr.getRunningAppProcesses();
2449
2428
            nProcs = 0;
2450
 
            if (lProcesses != null) 
 
2429
            if (lProcesses != null)
2451
2430
                nProcs = lProcesses.size();
2452
2431
            for (lcv = 0; lcv < nProcs; lcv++)
2453
2432
                {
2454
2433
                if (lProcesses.get(lcv).processName.contains(sProcName))
2455
2434
                    {
2456
 
                    sRet = sErrorPrefix + "Unable to kill " + nPID + " " + strProcName + "\n";
 
2435
                    sRet = sErrorPrefix + "Unable to kill " + sProcName + " (couldn't kill " + sFoundProcName +")\n";
2457
2436
                    break;
2458
2437
                    }
2459
2438
                }
2469
2448
                RedirOutputThread outThrd = new RedirOutputThread(pProc, null);
2470
2449
                outThrd.start();
2471
2450
                outThrd.join(10000);
2472
 
                sTmp = outThrd.strOutput;
 
2451
                String sTmp = outThrd.strOutput;
2473
2452
                StringTokenizer stokLines = new StringTokenizer(sTmp, "\n");
2474
2453
                while(stokLines.hasMoreTokens())
2475
2454
                    {
2490
2469
                        if (sName.contains(sProcName))
2491
2470
                            {
2492
2471
                            NewKillProc(sPid, out);
2493
 
                            sRet = "Successfully killed " + sPid + " " + sName + "\n";
 
2472
                            sRet = "Successfully killed " + sName + "\n";
2494
2473
                            }
2495
2474
                        }
2496
2475
                    }
3145
3124
        return theArgs;
3146
3125
        }
3147
3126
 
3148
 
    public String UnInstallApp(String sApp, OutputStream out)
 
3127
    public String UnInstallApp(String sApp, OutputStream out, boolean reboot)
3149
3128
        {
3150
3129
        String sRet = "";
3151
3130
 
3152
3131
        try
3153
3132
            {
3154
 
            pProc = Runtime.getRuntime().exec(this.getSuArgs("pm uninstall " + sApp + ";reboot;exit"));
 
3133
            if (reboot == true) {
 
3134
                pProc = Runtime.getRuntime().exec(this.getSuArgs("pm uninstall " + sApp + ";reboot;exit"));
 
3135
            } else {
 
3136
                pProc = Runtime.getRuntime().exec(this.getSuArgs("pm uninstall " + sApp + ";exit"));
 
3137
            }
3155
3138
 
3156
3139
            RedirOutputThread outThrd = new RedirOutputThread(pProc, out);
3157
3140
            outThrd.start();
3502
3485
 
3503
3486
        try
3504
3487
            {
3505
 
            pProc = Runtime.getRuntime().exec(progArray);
 
3488
            pProc = Runtime.getRuntime().exec(this.getSuArgs(TextUtils.join(" ", progArray)));
3506
3489
            RedirOutputThread outThrd = new RedirOutputThread(pProc, out);
3507
3490
            outThrd.start();
3508
3491
            while (lcv < 30) {
3514
3497
                    }
3515
3498
                catch (IllegalThreadStateException itse) {
3516
3499
                    lcv++;
3517
 
                    Log.d("SUT", "StartPrg waited 10s for "+progArray[0]);
3518
3500
                    }
3519
3501
                }
3520
3502
            }
3645
3627
                        }
3646
3628
                    catch (IllegalThreadStateException itse) {
3647
3629
                        lcv++;
3648
 
                        Log.d("SUT", "StartPrg2 waited 10s for "+theArgs[0]);
3649
3630
                        }
3650
3631
                    }
3651
3632
                }
3692
3673
        return (sRet);
3693
3674
        }
3694
3675
 
 
3676
    public String ChmodDir(String sDir)
 
3677
        {
 
3678
        String sRet = "";
 
3679
        int nFiles = 0;
 
3680
        String sSubDir = null;
 
3681
        String sTmpDir = fixFileName(sDir);
 
3682
 
 
3683
        File dir = new File(sTmpDir);
 
3684
 
 
3685
        if (dir.isDirectory()) {
 
3686
            sRet = "Changing permissions for " + sTmpDir;
 
3687
 
 
3688
            File [] files = dir.listFiles();
 
3689
            if (files != null) {
 
3690
                if ((nFiles = files.length) > 0) {
 
3691
                    for (int lcv = 0; lcv < nFiles; lcv++) {
 
3692
                        if (files[lcv].isDirectory()) {
 
3693
                            sSubDir = files[lcv].getAbsolutePath();
 
3694
                            sRet += "\n" + ChmodDir(sSubDir);
 
3695
                        }
 
3696
                        else {
 
3697
                            // set the new file's permissions to rwxrwxrwx, if possible
 
3698
                            try {
 
3699
                                Process pProc = Runtime.getRuntime().exec("chmod 777 "+files[lcv]);
 
3700
                                RedirOutputThread outThrd = new RedirOutputThread(pProc, null);
 
3701
                                outThrd.start();
 
3702
                                outThrd.join(5000);
 
3703
                                sRet += "\n\tchmod " + files[lcv].getName() + " ok";
 
3704
                            } catch (InterruptedException e) {
 
3705
                                sRet += "\n\ttimeout waiting for chmod " + files[lcv].getName();
 
3706
                            } catch (IOException e) {
 
3707
                                sRet += "\n\tunable to chmod " + files[lcv].getName();
 
3708
                            }
 
3709
                        }
 
3710
                    }
 
3711
                }
 
3712
                else
 
3713
                    sRet += "\n\t<empty>";
 
3714
                }
 
3715
            }
 
3716
 
 
3717
        // set the new directory's (or file's) permissions to rwxrwxrwx, if possible
 
3718
        try {
 
3719
            Process pProc = Runtime.getRuntime().exec("chmod 777 "+sTmpDir);
 
3720
            RedirOutputThread outThrd = new RedirOutputThread(pProc, null);
 
3721
            outThrd.start();
 
3722
            outThrd.join(5000);
 
3723
            sRet += "\n\tchmod " + sTmpDir + " ok";
 
3724
        } catch (InterruptedException e) {
 
3725
            sRet += "\n\ttimeout waiting for chmod " + sTmpDir;
 
3726
        } catch (IOException e) {
 
3727
            sRet += "\n\tunable to chmod " + sTmpDir;
 
3728
        }
 
3729
 
 
3730
        return(sRet);
 
3731
        }
 
3732
 
3695
3733
    private String PrintUsage()
3696
3734
        {
3697
3735
        String sRet =
3726
3764
            "mkdr directory               - create directory\n" +
3727
3765
            "dirw directory               - tests whether the directory is writable\n" +
3728
3766
            "isdir directory              - test whether the directory exists\n" +
 
3767
            "chmod directory|file         - change permissions of directory and contents (or file) to 777\n" +
3729
3768
            "stat processid               - stat process\n" +
3730
3769
            "dead processid               - print whether the process is alive or hung\n" +
3731
3770
            "mems                         - dump memory stats\n" +
3736
3775
            "zip zipfile src              - zip the source file/dir into zipfile\n" +
3737
3776
            "rebt                         - reboot device\n" +
3738
3777
            "inst /path/filename.apk      - install the referenced apk file\n" +
3739
 
            "uninst packagename           - uninstall the referenced package\n" +
 
3778
            "uninst packagename           - uninstall the referenced package and reboot\n" +
 
3779
            "uninstall packagename        - uninstall the referenced package without a reboot\n" +
3740
3780
            "updt pkgname pkgfile         - unpdate the referenced package\n" +
3741
3781
            "clok                         - the current device time expressed as the" +
3742
3782
            "                               number of millisecs since epoch\n" +