| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2008 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 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 17 | #include <errno.h> | 
|  | 18 | #include <fcntl.h> | 
|  | 19 | #include <limits.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | #include <stdio.h> | 
|  | 21 | #include <stdlib.h> | 
|  | 22 | #include <string.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include <sys/resource.h> | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 24 | #include <sys/stat.h> | 
|  | 25 | #include <sys/time.h> | 
| Dan Egnor | efd1393 | 2010-03-08 13:04:13 -0800 | [diff] [blame] | 26 | #include <sys/wait.h> | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 27 | #include <unistd.h> | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 29 | #include <cutils/properties.h> | 
|  | 30 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | #include "private/android_filesystem_config.h" | 
|  | 32 |  | 
| Mike Lockwood | bb6fa17 | 2009-10-05 23:23:40 -0400 | [diff] [blame] | 33 | #define LOG_TAG "dumpstate" | 
|  | 34 | #include <utils/Log.h> | 
|  | 35 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | #include "dumpstate.h" | 
|  | 37 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 38 | /* read before root is shed */ | 
|  | 39 | static char cmdline_buf[16384] = "(unknown)"; | 
|  | 40 | static const char *dump_traces_path = NULL; | 
| San Mehat | 30b9f57 | 2009-09-01 13:27:20 -0700 | [diff] [blame] | 41 |  | 
| Daniel Sandler | 27e1a79 | 2010-07-27 14:46:34 -0400 | [diff] [blame] | 42 | static char screenshot_path[PATH_MAX] = ""; | 
|  | 43 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | /* dumps the current system state to stdout */ | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 45 | static void dumpstate() { | 
|  | 46 | time_t now = time(NULL); | 
|  | 47 | char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX]; | 
|  | 48 | char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX]; | 
|  | 49 | char network[PROPERTY_VALUE_MAX], date[80]; | 
|  | 50 |  | 
|  | 51 | property_get("ro.build.display.id", build, "(unknown)"); | 
|  | 52 | property_get("ro.build.fingerprint", fingerprint, "(unknown)"); | 
|  | 53 | property_get("ro.baseband", radio, "(unknown)"); | 
|  | 54 | property_get("ro.bootloader", bootloader, "(unknown)"); | 
|  | 55 | property_get("gsm.operator.alpha", network, "(unknown)"); | 
|  | 56 | strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now)); | 
|  | 57 |  | 
|  | 58 | printf("========================================================\n"); | 
|  | 59 | printf("== dumpstate: %s\n", date); | 
|  | 60 | printf("========================================================\n"); | 
|  | 61 |  | 
|  | 62 | printf("\n"); | 
|  | 63 | printf("Build: %s\n", build); | 
|  | 64 | printf("Bootloader: %s\n", bootloader); | 
|  | 65 | printf("Radio: %s\n", radio); | 
|  | 66 | printf("Network: %s\n", network); | 
|  | 67 |  | 
|  | 68 | printf("Kernel: "); | 
|  | 69 | dump_file(NULL, "/proc/version"); | 
|  | 70 | printf("Command line: %s\n", strtok(cmdline_buf, "\n")); | 
|  | 71 | printf("\n"); | 
|  | 72 |  | 
|  | 73 | dump_file("MEMORY INFO", "/proc/meminfo"); | 
|  | 74 | run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL); | 
|  | 75 | run_command("PROCRANK", 20, "procrank", NULL); | 
|  | 76 | dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat"); | 
|  | 77 | dump_file("VMALLOC INFO", "/proc/vmallocinfo"); | 
|  | 78 | dump_file("SLAB INFO", "/proc/slabinfo"); | 
|  | 79 | dump_file("ZONEINFO", "/proc/zoneinfo"); | 
|  | 80 |  | 
| Daniel Sandler | 27e1a79 | 2010-07-27 14:46:34 -0400 | [diff] [blame] | 81 | if (screenshot_path[0]) { | 
|  | 82 | LOGI("taking screenshot\n"); | 
|  | 83 | run_command(NULL, 5, "su", "root", "screenshot", screenshot_path, NULL); | 
|  | 84 | LOGI("wrote screenshot: %s\n", screenshot_path); | 
|  | 85 | } | 
|  | 86 |  | 
| Wink Saville | 2f791fd | 2010-10-07 13:38:09 -0700 | [diff] [blame] | 87 | run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL); | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 88 |  | 
|  | 89 | /* show the traces we collected in main(), if that was done */ | 
|  | 90 | if (dump_traces_path != NULL) { | 
|  | 91 | dump_file("VM TRACES JUST NOW", dump_traces_path); | 
|  | 92 | } | 
|  | 93 |  | 
|  | 94 | /* only show ANR traces if they're less than 15 minutes old */ | 
|  | 95 | struct stat st; | 
|  | 96 | char anr_traces_path[PATH_MAX]; | 
|  | 97 | property_get("dalvik.vm.stack-trace-file", anr_traces_path, ""); | 
| Dan Egnor | efd1393 | 2010-03-08 13:04:13 -0800 | [diff] [blame] | 98 | if (!anr_traces_path[0]) { | 
|  | 99 | printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n"); | 
|  | 100 | } else if (stat(anr_traces_path, &st)) { | 
|  | 101 | printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno)); | 
|  | 102 | } else { | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 103 | dump_file("VM TRACES AT LAST ANR", anr_traces_path); | 
|  | 104 | } | 
|  | 105 |  | 
|  | 106 | // dump_file("EVENT LOG TAGS", "/etc/event-log-tags"); | 
| Wink Saville | 2f791fd | 2010-10-07 13:38:09 -0700 | [diff] [blame] | 107 | run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL); | 
|  | 108 | run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL); | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 109 |  | 
| Dmitry Shmidt | 2c3197b | 2010-11-03 16:59:47 -0700 | [diff] [blame] | 110 | run_command("NETWORK INTERFACES", 10, "su", "root", "netcfg", NULL); | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 111 | dump_file("NETWORK ROUTES", "/proc/net/route"); | 
| Robert Greenwalt | 5f2ff42 | 2011-04-28 17:10:23 -0700 | [diff] [blame] | 112 | dump_file("NETWORK ROUTES IPV6", "/proc/net/ipv6_route"); | 
| Dmitry Shmidt | 84b72ec | 2010-02-23 15:21:11 -0800 | [diff] [blame] | 113 | dump_file("ARP CACHE", "/proc/net/arp"); | 
| Robert Greenwalt | 5f2ff42 | 2011-04-28 17:10:23 -0700 | [diff] [blame] | 114 | run_command("IPTABLES", 10, "su", "root", "iptables", "-L", NULL); | 
|  | 115 | run_command("IPTABLE NAT", 10, "su", "root", "iptables", "-t", "nat", "-L", NULL); | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 116 |  | 
| Dmitry Shmidt | 631e0f1 | 2010-05-26 10:57:11 -0700 | [diff] [blame] | 117 | run_command("WIFI NETWORKS", 20, | 
|  | 118 | "su", "root", "wpa_cli", "list_networks", NULL); | 
|  | 119 |  | 
| Dmitry Shmidt | 64bf3d5 | 2009-12-16 16:05:08 -0800 | [diff] [blame] | 120 | #ifdef FWDUMP_bcm4329 | 
| Dmitry Shmidt | 5500c4f | 2010-09-01 16:18:14 -0700 | [diff] [blame] | 121 | run_command("DUMP WIFI STATUS", 20, | 
| Dmitry Shmidt | 285a8f5 | 2011-02-14 11:08:01 -0800 | [diff] [blame] | 122 | "su", "root", "dhdutil", "-i", "wlan0", "dump", NULL); | 
| Dmitry Shmidt | 2b79919 | 2010-11-04 14:39:24 -0700 | [diff] [blame] | 123 | run_command("DUMP WIFI INTERNAL COUNTERS", 20, | 
|  | 124 | "su", "root", "wlutil", "counters", NULL); | 
| Dmitry Shmidt | 64bf3d5 | 2009-12-16 16:05:08 -0800 | [diff] [blame] | 125 | #endif | 
| San Mehat | 30b9f57 | 2009-09-01 13:27:20 -0700 | [diff] [blame] | 126 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 127 | print_properties(); | 
| Iliyan Malchev | 326e3e2 | 2009-11-15 18:28:06 -0800 | [diff] [blame] | 128 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 129 | run_command("KERNEL LOG", 20, "dmesg", NULL); | 
| Iliyan Malchev | 326e3e2 | 2009-11-15 18:28:06 -0800 | [diff] [blame] | 130 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 131 | dump_file("KERNEL WAKELOCKS", "/proc/wakelocks"); | 
|  | 132 | dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state"); | 
| Mike Lockwood | 2ecf3f5e0 | 2009-10-04 17:21:05 -0400 | [diff] [blame] | 133 |  | 
| San Mehat | 9e3f8c6 | 2010-03-17 09:50:25 -0700 | [diff] [blame] | 134 | run_command("VOLD DUMP", 10, "vdc", "dump", NULL); | 
| San Mehat | 54aa577 | 2010-03-08 08:58:03 -0800 | [diff] [blame] | 135 | run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL); | 
| San Mehat | 54aa577 | 2010-03-08 08:58:03 -0800 | [diff] [blame] | 136 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 137 | run_command("PROCESSES", 10, "ps", "-P", NULL); | 
|  | 138 | run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL); | 
|  | 139 | run_command("LIBRANK", 10, "librank", NULL); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 |  | 
| Dianne Hackborn | f123e49 | 2010-09-24 11:16:23 -0700 | [diff] [blame] | 141 | dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log"); | 
|  | 142 | dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log"); | 
|  | 143 | dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions"); | 
|  | 144 | dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats"); | 
|  | 145 | dump_file("BINDER STATE", "/sys/kernel/debug/binder/state"); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 |  | 
| JP Abgrall | 8935bca | 2011-01-21 22:32:00 -0800 | [diff] [blame] | 147 | run_command("FILESYSTEMS & FREE SPACE", 10, "su", "root", "df", NULL); | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 148 |  | 
|  | 149 | dump_file("PACKAGE SETTINGS", "/data/system/packages.xml"); | 
|  | 150 | dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt"); | 
|  | 151 |  | 
|  | 152 | dump_file("LAST KMSG", "/proc/last_kmsg"); | 
|  | 153 | run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL); | 
|  | 154 | dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console"); | 
|  | 155 | dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads"); | 
|  | 156 |  | 
| San Mehat | 57fff78 | 2010-04-27 10:53:35 -0700 | [diff] [blame] | 157 | for_each_pid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS"); | 
|  | 158 |  | 
| Dan Egnor | ea11654 | 2010-01-26 17:04:26 -0800 | [diff] [blame] | 159 | printf("------ BACKLIGHTS ------\n"); | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 160 | printf("LCD brightness="); | 
|  | 161 | dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness"); | 
|  | 162 | printf("Button brightness="); | 
|  | 163 | dump_file(NULL, "/sys/class/leds/button-backlight/brightness"); | 
|  | 164 | printf("Keyboard brightness="); | 
|  | 165 | dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness"); | 
|  | 166 | printf("ALS mode="); | 
|  | 167 | dump_file(NULL, "/sys/class/leds/lcd-backlight/als"); | 
|  | 168 | printf("LCD driver registers:\n"); | 
|  | 169 | dump_file(NULL, "/sys/class/leds/lcd-backlight/registers"); | 
|  | 170 | printf("\n"); | 
|  | 171 |  | 
|  | 172 | printf("========================================================\n"); | 
|  | 173 | printf("== Android Framework Services\n"); | 
|  | 174 | printf("========================================================\n"); | 
|  | 175 |  | 
|  | 176 | /* the full dumpsys is starting to take a long time, so we need | 
|  | 177 | to increase its timeout.  we really need to do the timeouts in | 
|  | 178 | dumpsys itself... */ | 
|  | 179 | run_command("DUMPSYS", 60, "dumpsys", NULL); | 
| Joe Onorato | eb95b08 | 2010-10-21 14:54:19 -0400 | [diff] [blame] | 180 |  | 
|  | 181 | printf("========================================================\n"); | 
|  | 182 | printf("== Application Services\n"); | 
|  | 183 | printf("========================================================\n"); | 
|  | 184 |  | 
|  | 185 | /* Instead of a 60s timeout, we should give each service a 5 second timeout */ | 
|  | 186 | run_command("APP SERVICES", 60, "dumpsys", "activity", "service", NULL); | 
|  | 187 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | } | 
|  | 189 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 190 | static void usage() { | 
| Daniel Sandler | 27e1a79 | 2010-07-27 14:46:34 -0400 | [diff] [blame] | 191 | fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s]\n" | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 192 | "  -o: write to file (instead of stdout)\n" | 
| Daniel Sandler | 27e1a79 | 2010-07-27 14:46:34 -0400 | [diff] [blame] | 193 | "  -d: append date to filename (requires -o)\n" | 
|  | 194 | "  -z: gzip output (requires -o)\n" | 
|  | 195 | "  -p: capture screenshot to filename.png (requires -o)\n" | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 196 | "  -s: write output to control socket (for init)\n" | 
| Daniel Sandler | 27e1a79 | 2010-07-27 14:46:34 -0400 | [diff] [blame] | 197 | "  -b: play sound file instead of vibrate, at beginning of job\n" | 
|  | 198 | "  -e: play sound file instead of vibrate, at end of job\n" | 
|  | 199 | ); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | } | 
|  | 201 |  | 
|  | 202 | int main(int argc, char *argv[]) { | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 203 | int do_add_date = 0; | 
|  | 204 | int do_compress = 0; | 
|  | 205 | char* use_outfile = 0; | 
| Paul Eastham | bfb071d | 2010-08-06 14:11:55 -0700 | [diff] [blame] | 206 | char* begin_sound = 0; | 
|  | 207 | char* end_sound = 0; | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 208 | int use_socket = 0; | 
| Daniel Sandler | 27e1a79 | 2010-07-27 14:46:34 -0400 | [diff] [blame] | 209 | int do_fb = 0; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 |  | 
| Mike Lockwood | bb6fa17 | 2009-10-05 23:23:40 -0400 | [diff] [blame] | 211 | LOGI("begin\n"); | 
|  | 212 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 213 | /* set as high priority, and protect from OOM killer */ | 
|  | 214 | setpriority(PRIO_PROCESS, 0, -20); | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 215 | FILE *oom_adj = fopen("/proc/self/oom_adj", "w"); | 
|  | 216 | if (oom_adj) { | 
|  | 217 | fputs("-17", oom_adj); | 
|  | 218 | fclose(oom_adj); | 
|  | 219 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 221 | /* very first thing, collect VM traces from Dalvik (needs root) */ | 
|  | 222 | dump_traces_path = dump_vm_traces(); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 224 | int c; | 
| Daniel Sandler | 27e1a79 | 2010-07-27 14:46:34 -0400 | [diff] [blame] | 225 | while ((c = getopt(argc, argv, "b:de:ho:svzp")) != -1) { | 
| Mike Lockwood | 8d53373 | 2009-09-02 18:01:05 -0400 | [diff] [blame] | 226 | switch (c) { | 
| Paul Eastham | bfb071d | 2010-08-06 14:11:55 -0700 | [diff] [blame] | 227 | case 'b': begin_sound = optarg;  break; | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 228 | case 'd': do_add_date = 1;       break; | 
| Paul Eastham | bfb071d | 2010-08-06 14:11:55 -0700 | [diff] [blame] | 229 | case 'e': end_sound = optarg;    break; | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 230 | case 'o': use_outfile = optarg;  break; | 
|  | 231 | case 's': use_socket = 1;        break; | 
|  | 232 | case 'v': break;  // compatibility no-op | 
|  | 233 | case 'z': do_compress = 6;       break; | 
| Daniel Sandler | 27e1a79 | 2010-07-27 14:46:34 -0400 | [diff] [blame] | 234 | case 'p': do_fb = 1;             break; | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 235 | case '?': printf("\n"); | 
|  | 236 | case 'h': | 
|  | 237 | usage(); | 
| Mike Lockwood | 8d53373 | 2009-09-02 18:01:05 -0400 | [diff] [blame] | 238 | exit(1); | 
|  | 239 | } | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 240 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 241 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 242 | /* open the vibrator before dropping root */ | 
|  | 243 | FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w"); | 
|  | 244 | if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC); | 
|  | 245 |  | 
|  | 246 | /* read /proc/cmdline before dropping root */ | 
|  | 247 | FILE *cmdline = fopen("/proc/cmdline", "r"); | 
|  | 248 | if (cmdline != NULL) { | 
|  | 249 | fgets(cmdline_buf, sizeof(cmdline_buf), cmdline); | 
|  | 250 | fclose(cmdline); | 
|  | 251 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 |  | 
| Nick Kralevich | 77d87aa | 2010-10-21 09:14:14 -0700 | [diff] [blame] | 253 | if (getuid() == 0) { | 
|  | 254 | /* switch to non-root user and group */ | 
|  | 255 | gid_t groups[] = { AID_LOG, AID_SDCARD_RW, AID_MOUNT }; | 
|  | 256 | if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) { | 
|  | 257 | LOGE("Unable to setgroups, aborting: %s\n", strerror(errno)); | 
|  | 258 | return -1; | 
|  | 259 | } | 
|  | 260 | if (setgid(AID_SHELL) != 0) { | 
|  | 261 | LOGE("Unable to setgid, aborting: %s\n", strerror(errno)); | 
|  | 262 | return -1; | 
|  | 263 | } | 
|  | 264 | if (setuid(AID_SHELL) != 0) { | 
|  | 265 | LOGE("Unable to setuid, aborting: %s\n", strerror(errno)); | 
|  | 266 | return -1; | 
|  | 267 | } | 
| Nick Kralevich | 05f0382 | 2010-08-31 18:17:31 -0700 | [diff] [blame] | 268 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 270 | char path[PATH_MAX], tmp_path[PATH_MAX]; | 
|  | 271 | pid_t gzip_pid = -1; | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 |  | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 273 | if (use_socket) { | 
|  | 274 | redirect_to_socket(stdout, "dumpstate"); | 
|  | 275 | } else if (use_outfile) { | 
|  | 276 | strlcpy(path, use_outfile, sizeof(path)); | 
|  | 277 | if (do_add_date) { | 
|  | 278 | char date[80]; | 
|  | 279 | time_t now = time(NULL); | 
|  | 280 | strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now)); | 
|  | 281 | strlcat(path, date, sizeof(path)); | 
| Mike Lockwood | 8d53373 | 2009-09-02 18:01:05 -0400 | [diff] [blame] | 282 | } | 
| Daniel Sandler | 27e1a79 | 2010-07-27 14:46:34 -0400 | [diff] [blame] | 283 | if (do_fb) { | 
|  | 284 | strlcpy(screenshot_path, path, sizeof(screenshot_path)); | 
|  | 285 | strlcat(screenshot_path, ".png", sizeof(screenshot_path)); | 
|  | 286 | } | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 287 | strlcat(path, ".txt", sizeof(path)); | 
|  | 288 | if (do_compress) strlcat(path, ".gz", sizeof(path)); | 
|  | 289 | strlcpy(tmp_path, path, sizeof(tmp_path)); | 
|  | 290 | strlcat(tmp_path, ".tmp", sizeof(tmp_path)); | 
|  | 291 | gzip_pid = redirect_to_file(stdout, tmp_path, do_compress); | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | } | 
|  | 293 |  | 
| Paul Eastham | bfb071d | 2010-08-06 14:11:55 -0700 | [diff] [blame] | 294 | if (begin_sound) { | 
|  | 295 | play_sound(begin_sound); | 
|  | 296 | } else if (vibrator) { | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 297 | fputs("150", vibrator); | 
|  | 298 | fflush(vibrator); | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | dumpstate(); | 
|  | 302 |  | 
| Paul Eastham | bfb071d | 2010-08-06 14:11:55 -0700 | [diff] [blame] | 303 | if (end_sound) { | 
|  | 304 | play_sound(end_sound); | 
|  | 305 | } else if (vibrator) { | 
| Dan Egnor | 52952b1 | 2010-01-13 12:27:50 -0800 | [diff] [blame] | 306 | int i; | 
|  | 307 | for (i = 0; i < 3; i++) { | 
|  | 308 | fputs("75\n", vibrator); | 
|  | 309 | fflush(vibrator); | 
|  | 310 | usleep((75 + 50) * 1000); | 
|  | 311 | } | 
|  | 312 | fclose(vibrator); | 
|  | 313 | } | 
|  | 314 |  | 
|  | 315 | /* wait for gzip to finish, otherwise it might get killed when we exit */ | 
|  | 316 | if (gzip_pid > 0) { | 
|  | 317 | fclose(stdout); | 
|  | 318 | waitpid(gzip_pid, NULL, 0); | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | /* rename the (now complete) .tmp file to its final location */ | 
|  | 322 | if (use_outfile && rename(tmp_path, path)) { | 
|  | 323 | fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno)); | 
|  | 324 | } | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 |  | 
| Mike Lockwood | bb6fa17 | 2009-10-05 23:23:40 -0400 | [diff] [blame] | 326 | LOGI("done\n"); | 
|  | 327 |  | 
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 328 | return 0; | 
|  | 329 | } |