blob: fa0658243a1895c9264618434db19b1add6d4c90 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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 Egnor52952b12010-01-13 12:27:50 -080017#include <errno.h>
18#include <fcntl.h>
19#include <limits.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#include <sys/resource.h>
Dan Egnor52952b12010-01-13 12:27:50 -080024#include <sys/stat.h>
25#include <sys/time.h>
Dan Egnorefd13932010-03-08 13:04:13 -080026#include <sys/wait.h>
Dan Egnor52952b12010-01-13 12:27:50 -080027#include <unistd.h>
Nick Kraleviched089c72011-11-28 14:48:18 -080028#include <linux/capability.h>
29#include <linux/prctl.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030
Dan Egnor52952b12010-01-13 12:27:50 -080031#include <cutils/properties.h>
32
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033#include "private/android_filesystem_config.h"
34
Mike Lockwoodbb6fa172009-10-05 23:23:40 -040035#define LOG_TAG "dumpstate"
36#include <utils/Log.h>
37
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038#include "dumpstate.h"
39
Dan Egnor52952b12010-01-13 12:27:50 -080040/* read before root is shed */
41static char cmdline_buf[16384] = "(unknown)";
42static const char *dump_traces_path = NULL;
San Mehat30b9f572009-09-01 13:27:20 -070043
Daniel Sandler27e1a792010-07-27 14:46:34 -040044static char screenshot_path[PATH_MAX] = "";
45
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046/* dumps the current system state to stdout */
Dan Egnor52952b12010-01-13 12:27:50 -080047static void dumpstate() {
48 time_t now = time(NULL);
49 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
50 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
51 char network[PROPERTY_VALUE_MAX], date[80];
John Michelau790d2e42011-07-05 15:20:03 -050052 char build_type[PROPERTY_VALUE_MAX];
Dan Egnor52952b12010-01-13 12:27:50 -080053
54 property_get("ro.build.display.id", build, "(unknown)");
55 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
John Michelau790d2e42011-07-05 15:20:03 -050056 property_get("ro.build.type", build_type, "(unknown)");
Dan Egnor52952b12010-01-13 12:27:50 -080057 property_get("ro.baseband", radio, "(unknown)");
58 property_get("ro.bootloader", bootloader, "(unknown)");
59 property_get("gsm.operator.alpha", network, "(unknown)");
60 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
61
62 printf("========================================================\n");
63 printf("== dumpstate: %s\n", date);
64 printf("========================================================\n");
65
66 printf("\n");
67 printf("Build: %s\n", build);
Jeff Brownad5b28a2011-11-11 14:56:27 -080068 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
Dan Egnor52952b12010-01-13 12:27:50 -080069 printf("Bootloader: %s\n", bootloader);
70 printf("Radio: %s\n", radio);
71 printf("Network: %s\n", network);
72
73 printf("Kernel: ");
74 dump_file(NULL, "/proc/version");
75 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
76 printf("\n");
77
Colin Cross5965ba32011-09-20 15:49:14 -070078 run_command("UPTIME", 10, "uptime", NULL);
Dan Egnor52952b12010-01-13 12:27:50 -080079 dump_file("MEMORY INFO", "/proc/meminfo");
80 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
81 run_command("PROCRANK", 20, "procrank", NULL);
82 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
83 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
84 dump_file("SLAB INFO", "/proc/slabinfo");
85 dump_file("ZONEINFO", "/proc/zoneinfo");
Arve Hjønnevåg351e74de12011-08-15 19:43:35 -070086 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
JP Abgrall2eb8e362011-08-24 11:24:33 -070087 dump_file("BUDDYINFO", "/proc/buddyinfo");
Dan Egnor52952b12010-01-13 12:27:50 -080088
Daniel Sandler27e1a792010-07-27 14:46:34 -040089 if (screenshot_path[0]) {
Steve Block6215d3f2012-01-04 20:05:49 +000090 ALOGI("taking screenshot\n");
Daniel Sandler27e1a792010-07-27 14:46:34 -040091 run_command(NULL, 5, "su", "root", "screenshot", screenshot_path, NULL);
Steve Block6215d3f2012-01-04 20:05:49 +000092 ALOGI("wrote screenshot: %s\n", screenshot_path);
Daniel Sandler27e1a792010-07-27 14:46:34 -040093 }
94
Amith Yamasanib066fcc2011-12-09 15:22:39 -080095 run_command("SYSTEM SETTINGS", 20, "su", "root", "sqlite3",
96 "/data/data/com.android.providers.settings/databases/settings.db",
97 "pragma user_version; select * from system; select * from secure;", NULL);
Wink Saville2f791fd2010-10-07 13:38:09 -070098 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
Dan Egnor52952b12010-01-13 12:27:50 -080099
100 /* show the traces we collected in main(), if that was done */
101 if (dump_traces_path != NULL) {
102 dump_file("VM TRACES JUST NOW", dump_traces_path);
103 }
104
105 /* only show ANR traces if they're less than 15 minutes old */
106 struct stat st;
107 char anr_traces_path[PATH_MAX];
108 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
Dan Egnorefd13932010-03-08 13:04:13 -0800109 if (!anr_traces_path[0]) {
110 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
111 } else if (stat(anr_traces_path, &st)) {
112 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
113 } else {
Dan Egnor52952b12010-01-13 12:27:50 -0800114 dump_file("VM TRACES AT LAST ANR", anr_traces_path);
115 }
116
117 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
Wink Saville2f791fd2010-10-07 13:38:09 -0700118 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
119 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
Dan Egnor52952b12010-01-13 12:27:50 -0800120
Dmitry Shmidt2c3197b2010-11-03 16:59:47 -0700121 run_command("NETWORK INTERFACES", 10, "su", "root", "netcfg", NULL);
JP Abgrall20e93c02011-09-19 22:29:18 -0700122 dump_file("NETWORK DEV INFO", "/proc/net/dev");
123 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
124 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
125 run_command("QTAGUID STATS INFO", 10, "su", "root", "cat", "/proc/net/xt_qtaguid/stats", NULL);
126
Dan Egnor52952b12010-01-13 12:27:50 -0800127 dump_file("NETWORK ROUTES", "/proc/net/route");
Robert Greenwalt5f2ff422011-04-28 17:10:23 -0700128 dump_file("NETWORK ROUTES IPV6", "/proc/net/ipv6_route");
Robert Greenwalte83d1812011-11-21 14:44:39 -0800129 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
130 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
131 run_command("ROUTE TABLE 60", 10, "ip", "route", "show", "table", "60", NULL);
132 run_command("ROUTE TABLE 61 v6", 10, "ip", "-6", "route", "show", "table", "60", NULL);
133 run_command("ROUTE TABLE 61", 10, "ip", "route", "show", "table", "61", NULL);
134 run_command("ROUTE TABLE 61 v6", 10, "ip", "-6", "route", "show", "table", "61", NULL);
Dmitry Shmidt84b72ec2010-02-23 15:21:11 -0800135 dump_file("ARP CACHE", "/proc/net/arp");
JP Abgrall2eb8e362011-08-24 11:24:33 -0700136 run_command("IPTABLES", 10, "su", "root", "iptables", "-L", "-nvx", NULL);
137 run_command("IP6TABLES", 10, "su", "root", "ip6tables", "-L", "-nvx", NULL);
John Michelauac745512011-07-25 20:42:50 -0500138 run_command("IPTABLE NAT", 10, "su", "root", "iptables", "-t", "nat", "-L", "-n", NULL);
JP Abgrall2eb8e362011-08-24 11:24:33 -0700139 run_command("IPT6ABLE NAT", 10, "su", "root", "ip6tables", "-t", "nat", "-L", "-n", NULL);
Dan Egnor52952b12010-01-13 12:27:50 -0800140
Dmitry Shmidt631e0f12010-05-26 10:57:11 -0700141 run_command("WIFI NETWORKS", 20,
142 "su", "root", "wpa_cli", "list_networks", NULL);
143
Dmitry Shmidt9abfeb42011-03-28 13:45:21 -0700144 property_get("dhcp.wlan0.gateway", network, "");
145 if (network[0])
146 run_command("PING GATEWAY", 10, "su", "root", "ping", "-c", "3", "-i", ".5", network, NULL);
147 property_get("dhcp.wlan0.dns1", network, "");
148 if (network[0])
149 run_command("PING DNS1", 10, "su", "root", "ping", "-c", "3", "-i", ".5", network, NULL);
150 property_get("dhcp.wlan0.dns2", network, "");
151 if (network[0])
152 run_command("PING DNS2", 10, "su", "root", "ping", "-c", "3", "-i", ".5", network, NULL);
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800153#ifdef FWDUMP_bcm4329
Dmitry Shmidt5500c4f2010-09-01 16:18:14 -0700154 run_command("DUMP WIFI STATUS", 20,
Dmitry Shmidt285a8f52011-02-14 11:08:01 -0800155 "su", "root", "dhdutil", "-i", "wlan0", "dump", NULL);
Dmitry Shmidt2b799192010-11-04 14:39:24 -0700156 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
157 "su", "root", "wlutil", "counters", NULL);
Dmitry Shmidt64bf3d52009-12-16 16:05:08 -0800158#endif
San Mehat30b9f572009-09-01 13:27:20 -0700159
Jake Hamby9c27e362011-11-17 13:20:35 -0800160 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
John Michelau71bbe7a2010-09-22 16:49:19 -0500161 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
John Michelau790d2e42011-07-05 15:20:03 -0500162 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
163 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
164 // su does not exist on user builds, so try running without it.
165 // This way any implementations of vril-dump that do not require
166 // root can run on user builds.
167 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
168 "vril-dump", NULL);
169 } else {
170 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
171 "su", "root", "vril-dump", NULL);
172 }
John Michelau71bbe7a2010-09-22 16:49:19 -0500173 }
174
Dan Egnor52952b12010-01-13 12:27:50 -0800175 print_properties();
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800176
Nick Kraleviched089c72011-11-28 14:48:18 -0800177 do_dmesg();
Iliyan Malchev326e3e22009-11-15 18:28:06 -0800178
Dan Egnor52952b12010-01-13 12:27:50 -0800179 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
180 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mike Lockwood2ecf3f5e02009-10-04 17:21:05 -0400181
San Mehat9e3f8c62010-03-17 09:50:25 -0700182 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
San Mehat54aa5772010-03-08 08:58:03 -0800183 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
San Mehat54aa5772010-03-08 08:58:03 -0800184
Dan Egnor52952b12010-01-13 12:27:50 -0800185 run_command("PROCESSES", 10, "ps", "-P", NULL);
186 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
187 run_command("LIBRANK", 10, "librank", NULL);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188
Dianne Hackbornf123e492010-09-24 11:16:23 -0700189 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
190 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
191 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
192 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
193 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194
JP Abgrall8935bca2011-01-21 22:32:00 -0800195 run_command("FILESYSTEMS & FREE SPACE", 10, "su", "root", "df", NULL);
Dan Egnor52952b12010-01-13 12:27:50 -0800196
197 dump_file("PACKAGE SETTINGS", "/data/system/packages.xml");
198 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
199
Nick Kraleviched089c72011-11-28 14:48:18 -0800200 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
Dan Egnor52952b12010-01-13 12:27:50 -0800201 dump_file("LAST KMSG", "/proc/last_kmsg");
202 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
203 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
204 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
205
San Mehat57fff782010-04-27 10:53:35 -0700206 for_each_pid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
207
Dan Egnorea116542010-01-26 17:04:26 -0800208 printf("------ BACKLIGHTS ------\n");
Dan Egnor52952b12010-01-13 12:27:50 -0800209 printf("LCD brightness=");
210 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
211 printf("Button brightness=");
212 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
213 printf("Keyboard brightness=");
214 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
215 printf("ALS mode=");
216 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
217 printf("LCD driver registers:\n");
218 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
219 printf("\n");
220
Jeff Brownc74a0282011-08-11 20:07:39 -0700221 run_command("LIST OF OPEN FILES", 10, "su", "root", "lsof", NULL);
222
Dianne Hackborn51234682011-11-03 19:19:28 -0700223 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
224
Colin Cross1e9f56c2011-07-18 16:56:35 -0700225#ifdef BOARD_HAS_DUMPSTATE
226 printf("========================================================\n");
227 printf("== Board\n");
228 printf("========================================================\n");
229
230 dumpstate_board();
231 printf("\n");
232#endif
233
Dan Egnor52952b12010-01-13 12:27:50 -0800234 printf("========================================================\n");
235 printf("== Android Framework Services\n");
236 printf("========================================================\n");
237
238 /* the full dumpsys is starting to take a long time, so we need
239 to increase its timeout. we really need to do the timeouts in
240 dumpsys itself... */
241 run_command("DUMPSYS", 60, "dumpsys", NULL);
Joe Onoratoeb95b082010-10-21 14:54:19 -0400242
243 printf("========================================================\n");
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700244 printf("== Running Application Activities\n");
Joe Onoratoeb95b082010-10-21 14:54:19 -0400245 printf("========================================================\n");
246
Dianne Hackborne17aeb32011-04-07 15:11:57 -0700247 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
248
249 printf("========================================================\n");
250 printf("== Running Application Services\n");
251 printf("========================================================\n");
252
253 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
Joe Onoratoeb95b082010-10-21 14:54:19 -0400254
Colin Cross1e9f56c2011-07-18 16:56:35 -0700255 printf("========================================================\n");
Marco Nelissen18cb2872011-11-15 11:19:53 -0800256 printf("== Running Application Providers\n");
257 printf("========================================================\n");
258
259 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
260
261
262 printf("========================================================\n");
Colin Cross1e9f56c2011-07-18 16:56:35 -0700263 printf("== dumpstate: done\n");
264 printf("========================================================\n");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265}
266
Dan Egnor52952b12010-01-13 12:27:50 -0800267static void usage() {
Daniel Sandler27e1a792010-07-27 14:46:34 -0400268 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s]\n"
Dan Egnor52952b12010-01-13 12:27:50 -0800269 " -o: write to file (instead of stdout)\n"
Daniel Sandler27e1a792010-07-27 14:46:34 -0400270 " -d: append date to filename (requires -o)\n"
271 " -z: gzip output (requires -o)\n"
272 " -p: capture screenshot to filename.png (requires -o)\n"
Dan Egnor52952b12010-01-13 12:27:50 -0800273 " -s: write output to control socket (for init)\n"
Daniel Sandler27e1a792010-07-27 14:46:34 -0400274 " -b: play sound file instead of vibrate, at beginning of job\n"
275 " -e: play sound file instead of vibrate, at end of job\n"
276 );
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800277}
278
279int main(int argc, char *argv[]) {
Dan Egnor52952b12010-01-13 12:27:50 -0800280 int do_add_date = 0;
281 int do_compress = 0;
282 char* use_outfile = 0;
Paul Easthambfb071d2010-08-06 14:11:55 -0700283 char* begin_sound = 0;
284 char* end_sound = 0;
Dan Egnor52952b12010-01-13 12:27:50 -0800285 int use_socket = 0;
Daniel Sandler27e1a792010-07-27 14:46:34 -0400286 int do_fb = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287
Steve Block6215d3f2012-01-04 20:05:49 +0000288 ALOGI("begin\n");
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400289
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 /* set as high priority, and protect from OOM killer */
291 setpriority(PRIO_PROCESS, 0, -20);
Dan Egnor52952b12010-01-13 12:27:50 -0800292 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
293 if (oom_adj) {
294 fputs("-17", oom_adj);
295 fclose(oom_adj);
296 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297
Dan Egnor52952b12010-01-13 12:27:50 -0800298 /* very first thing, collect VM traces from Dalvik (needs root) */
299 dump_traces_path = dump_vm_traces();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300
Dan Egnor52952b12010-01-13 12:27:50 -0800301 int c;
Daniel Sandler27e1a792010-07-27 14:46:34 -0400302 while ((c = getopt(argc, argv, "b:de:ho:svzp")) != -1) {
Mike Lockwood8d533732009-09-02 18:01:05 -0400303 switch (c) {
Paul Easthambfb071d2010-08-06 14:11:55 -0700304 case 'b': begin_sound = optarg; break;
Dan Egnor52952b12010-01-13 12:27:50 -0800305 case 'd': do_add_date = 1; break;
Paul Easthambfb071d2010-08-06 14:11:55 -0700306 case 'e': end_sound = optarg; break;
Dan Egnor52952b12010-01-13 12:27:50 -0800307 case 'o': use_outfile = optarg; break;
308 case 's': use_socket = 1; break;
309 case 'v': break; // compatibility no-op
310 case 'z': do_compress = 6; break;
Daniel Sandler27e1a792010-07-27 14:46:34 -0400311 case 'p': do_fb = 1; break;
Dan Egnor52952b12010-01-13 12:27:50 -0800312 case '?': printf("\n");
313 case 'h':
314 usage();
Mike Lockwood8d533732009-09-02 18:01:05 -0400315 exit(1);
316 }
Dan Egnor52952b12010-01-13 12:27:50 -0800317 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318
Dan Egnor52952b12010-01-13 12:27:50 -0800319 /* open the vibrator before dropping root */
320 FILE *vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
321 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
322
323 /* read /proc/cmdline before dropping root */
324 FILE *cmdline = fopen("/proc/cmdline", "r");
325 if (cmdline != NULL) {
326 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
327 fclose(cmdline);
328 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329
Nick Kralevich77d87aa2010-10-21 09:14:14 -0700330 if (getuid() == 0) {
Nick Kraleviched089c72011-11-28 14:48:18 -0800331 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
332 LOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
333 return -1;
334 }
335
Nick Kralevich77d87aa2010-10-21 09:14:14 -0700336 /* switch to non-root user and group */
John Michelau790d2e42011-07-05 15:20:03 -0500337 gid_t groups[] = { AID_LOG, AID_SDCARD_RW, AID_MOUNT, AID_INET };
Nick Kralevich77d87aa2010-10-21 09:14:14 -0700338 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
339 LOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
340 return -1;
341 }
342 if (setgid(AID_SHELL) != 0) {
343 LOGE("Unable to setgid, aborting: %s\n", strerror(errno));
344 return -1;
345 }
346 if (setuid(AID_SHELL) != 0) {
347 LOGE("Unable to setuid, aborting: %s\n", strerror(errno));
348 return -1;
349 }
Nick Kraleviched089c72011-11-28 14:48:18 -0800350
351 struct __user_cap_header_struct capheader;
352 struct __user_cap_data_struct capdata[2];
353 memset(&capheader, 0, sizeof(capheader));
354 memset(&capdata, 0, sizeof(capdata));
355 capheader.version = _LINUX_CAPABILITY_VERSION_3;
356 capheader.pid = 0;
357
358 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
359 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
360 capdata[0].inheritable = 0;
361 capdata[1].inheritable = 0;
362
363 if (capset(&capheader, &capdata[0]) < 0) {
364 LOGE("capset failed: %s\n", strerror(errno));
365 return -1;
366 }
Nick Kralevich05f03822010-08-31 18:17:31 -0700367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368
Dan Egnor52952b12010-01-13 12:27:50 -0800369 char path[PATH_MAX], tmp_path[PATH_MAX];
370 pid_t gzip_pid = -1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371
Dan Egnor52952b12010-01-13 12:27:50 -0800372 if (use_socket) {
373 redirect_to_socket(stdout, "dumpstate");
374 } else if (use_outfile) {
375 strlcpy(path, use_outfile, sizeof(path));
376 if (do_add_date) {
377 char date[80];
378 time_t now = time(NULL);
379 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
380 strlcat(path, date, sizeof(path));
Mike Lockwood8d533732009-09-02 18:01:05 -0400381 }
Daniel Sandler27e1a792010-07-27 14:46:34 -0400382 if (do_fb) {
383 strlcpy(screenshot_path, path, sizeof(screenshot_path));
384 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
385 }
Dan Egnor52952b12010-01-13 12:27:50 -0800386 strlcat(path, ".txt", sizeof(path));
387 if (do_compress) strlcat(path, ".gz", sizeof(path));
388 strlcpy(tmp_path, path, sizeof(tmp_path));
389 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
390 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 }
392
Paul Easthambfb071d2010-08-06 14:11:55 -0700393 if (begin_sound) {
394 play_sound(begin_sound);
395 } else if (vibrator) {
Dan Egnor52952b12010-01-13 12:27:50 -0800396 fputs("150", vibrator);
397 fflush(vibrator);
398 }
399
400 dumpstate();
401
Paul Easthambfb071d2010-08-06 14:11:55 -0700402 if (end_sound) {
403 play_sound(end_sound);
404 } else if (vibrator) {
Dan Egnor52952b12010-01-13 12:27:50 -0800405 int i;
406 for (i = 0; i < 3; i++) {
407 fputs("75\n", vibrator);
408 fflush(vibrator);
409 usleep((75 + 50) * 1000);
410 }
411 fclose(vibrator);
412 }
413
414 /* wait for gzip to finish, otherwise it might get killed when we exit */
415 if (gzip_pid > 0) {
416 fclose(stdout);
417 waitpid(gzip_pid, NULL, 0);
418 }
419
420 /* rename the (now complete) .tmp file to its final location */
421 if (use_outfile && rename(tmp_path, path)) {
422 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
423 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424
Steve Block6215d3f2012-01-04 20:05:49 +0000425 ALOGI("done\n");
Mike Lockwoodbb6fa172009-10-05 23:23:40 -0400426
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800427 return 0;
428}