blob: b6347196261108bbd729c6977bda320f3f530725 [file] [log] [blame]
Colin Crossf45fa6b2012-03-26 12:38:26 -07001/*
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
17#include <errno.h>
18#include <fcntl.h>
19#include <limits.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <sys/resource.h>
24#include <sys/stat.h>
25#include <sys/time.h>
26#include <sys/wait.h>
27#include <unistd.h>
Nick Kraleviche96b2d02013-02-28 16:46:22 -080028#include <sys/capability.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070029#include <linux/prctl.h>
30
31#include <cutils/properties.h>
32
33#include "private/android_filesystem_config.h"
34
35#define LOG_TAG "dumpstate"
Alex Ray656a6b92013-07-23 13:44:34 -070036#include <cutils/log.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070037
38#include "dumpstate.h"
39
40/* read before root is shed */
41static char cmdline_buf[16384] = "(unknown)";
42static const char *dump_traces_path = NULL;
43
44static char screenshot_path[PATH_MAX] = "";
45
46/* dumps the current system state to stdout */
47static 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];
52 char build_type[PROPERTY_VALUE_MAX];
53
54 property_get("ro.build.display.id", build, "(unknown)");
55 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
56 property_get("ro.build.type", build_type, "(unknown)");
57 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);
68 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
69 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
78 run_command("UPTIME", 10, "uptime", NULL);
79 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");
86 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
87 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -070088 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -070089
90
91 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -070092 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -070093 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -070094 dump_file("KERNEL SYNC", "/d/sync");
Colin Crossf45fa6b2012-03-26 12:38:26 -070095
96 run_command("PROCESSES", 10, "ps", "-P", NULL);
97 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
Nick Kralevich76b45c12013-07-15 12:21:40 -070098 run_command("PROCESSES (SELINUX LABELS)", 10, "ps", "-Z", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -070099 run_command("LIBRANK", 10, "librank", NULL);
100
101 do_dmesg();
102
103 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
104
Jeff Sharkey5a930032013-03-19 15:05:19 -0700105 if (screenshot_path[0]) {
106 ALOGI("taking screenshot\n");
107 run_command(NULL, 10, "/system/bin/screencap", "-p", screenshot_path, NULL);
108 ALOGI("wrote screenshot: %s\n", screenshot_path);
109 }
110
Colin Crossf45fa6b2012-03-26 12:38:26 -0700111 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
Colin Cross0c22e8b2012-11-02 15:46:56 -0700112 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700113
114 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
115 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
116 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
117 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
118
119
120 /* show the traces we collected in main(), if that was done */
121 if (dump_traces_path != NULL) {
122 dump_file("VM TRACES JUST NOW", dump_traces_path);
123 }
124
125 /* only show ANR traces if they're less than 15 minutes old */
126 struct stat st;
127 char anr_traces_path[PATH_MAX];
128 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
129 if (!anr_traces_path[0]) {
130 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
131 } else if (stat(anr_traces_path, &st)) {
132 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
133 } else {
134 dump_file("VM TRACES AT LAST ANR", anr_traces_path);
135 }
136
137 /* slow traces for slow operations */
138 if (anr_traces_path[0] != 0) {
139 int tail = strlen(anr_traces_path)-1;
140 while (tail > 0 && anr_traces_path[tail] != '/') {
141 tail--;
142 }
143 int i = 0;
144 while (1) {
145 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
146 if (stat(anr_traces_path, &st)) {
147 // No traces file at this index, done with the files.
148 break;
149 }
150 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
151 i++;
152 }
153 }
154
155 dump_file("NETWORK DEV INFO", "/proc/net/dev");
156 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700157 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700158 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
159 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
160
161 dump_file("NETWORK ROUTES", "/proc/net/route");
162 dump_file("NETWORK ROUTES IPV6", "/proc/net/ipv6_route");
163
164 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
165 dump_file("LAST KMSG", "/proc/last_kmsg");
166 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
167 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
168
Colin Crossf45fa6b2012-03-26 12:38:26 -0700169 run_command("SYSTEM SETTINGS", 20, SU_PATH, "root", "sqlite3",
170 "/data/data/com.android.providers.settings/databases/settings.db",
Jeff Sharkey719b8aa2012-10-01 12:52:42 -0700171 "pragma user_version; select * from system; select * from secure; select * from global;", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700172
173 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
174 run_command("NETWORK INTERFACES", 10, SU_PATH, "root", "netcfg", NULL);
175 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
176 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700177
178 dump_route_tables();
179
Colin Crossf45fa6b2012-03-26 12:38:26 -0700180 dump_file("ARP CACHE", "/proc/net/arp");
181 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
182 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700183 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
184 /* no ip6 nat */
185 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
186 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700187
188 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700189 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700190
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800191#ifdef FWDUMP_bcmdhd
192 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
193 SU_PATH, "root", "wlutil", "counters", NULL);
194#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800195 dump_file("INTERRUPTS (1)", "/proc/interrupts");
196
Colin Crossf45fa6b2012-03-26 12:38:26 -0700197 property_get("dhcp.wlan0.gateway", network, "");
198 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800199 run_command("PING GATEWAY", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700200 property_get("dhcp.wlan0.dns1", network, "");
201 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800202 run_command("PING DNS1", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700203 property_get("dhcp.wlan0.dns2", network, "");
204 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800205 run_command("PING DNS2", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800206#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700207 run_command("DUMP WIFI STATUS", 20,
208 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
209 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
210 SU_PATH, "root", "wlutil", "counters", NULL);
211#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800212 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700213
214 print_properties();
215
216 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
217 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
218
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800219 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700220
Jeff Sharkey13461c22012-05-17 12:40:11 -0700221 run_command("PACKAGE SETTINGS", 20, SU_PATH, "root", "cat", "/data/system/packages.xml", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700222 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
223
224 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
225
226 printf("------ BACKLIGHTS ------\n");
227 printf("LCD brightness=");
228 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
229 printf("Button brightness=");
230 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
231 printf("Keyboard brightness=");
232 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
233 printf("ALS mode=");
234 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
235 printf("LCD driver registers:\n");
236 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
237 printf("\n");
238
239 /* Binder state is expensive to look at as it uses a lot of memory. */
240 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
241 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
242 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
243 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
244 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
245
Colin Crossf45fa6b2012-03-26 12:38:26 -0700246 printf("========================================================\n");
247 printf("== Board\n");
248 printf("========================================================\n");
249
250 dumpstate_board();
251 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700252
253 /* Migrate the ril_dumpstate to a dumpstate_board()? */
254 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
255 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
256 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
257 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
258 // su does not exist on user builds, so try running without it.
259 // This way any implementations of vril-dump that do not require
260 // root can run on user builds.
261 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
262 "vril-dump", NULL);
263 } else {
264 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
265 SU_PATH, "root", "vril-dump", NULL);
266 }
267 }
268
269 printf("========================================================\n");
270 printf("== Android Framework Services\n");
271 printf("========================================================\n");
272
273 /* the full dumpsys is starting to take a long time, so we need
274 to increase its timeout. we really need to do the timeouts in
275 dumpsys itself... */
276 run_command("DUMPSYS", 60, "dumpsys", NULL);
277
278 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700279 printf("== Checkins\n");
280 printf("========================================================\n");
281
Dianne Hackborn59b15162013-09-04 18:04:14 -0700282 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700283 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700284 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700285 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700286 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700287
288 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700289 printf("== Running Application Activities\n");
290 printf("========================================================\n");
291
292 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
293
294 printf("========================================================\n");
295 printf("== Running Application Services\n");
296 printf("========================================================\n");
297
298 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
299
300 printf("========================================================\n");
301 printf("== Running Application Providers\n");
302 printf("========================================================\n");
303
304 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
305
306
307 printf("========================================================\n");
308 printf("== dumpstate: done\n");
309 printf("========================================================\n");
310}
311
312static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500313 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700314 " -o: write to file (instead of stdout)\n"
315 " -d: append date to filename (requires -o)\n"
316 " -z: gzip output (requires -o)\n"
317 " -p: capture screenshot to filename.png (requires -o)\n"
318 " -s: write output to control socket (for init)\n"
319 " -b: play sound file instead of vibrate, at beginning of job\n"
320 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500321 " -q: disable vibrate\n"
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700322 " -B: send broadcast when finished (requires -o and -p)\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700323 );
324}
325
John Michelau885f8882013-05-06 16:42:02 -0500326static void sigpipe_handler(int n) {
327 (void)n;
328 exit(EXIT_FAILURE);
329}
330
Colin Crossf45fa6b2012-03-26 12:38:26 -0700331int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500332 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700333 int do_add_date = 0;
334 int do_compress = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500335 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700336 char* use_outfile = 0;
337 char* begin_sound = 0;
338 char* end_sound = 0;
339 int use_socket = 0;
340 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700341 int do_broadcast = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700342
Nick Kralevich1e339872012-04-25 13:38:45 -0700343 if (getuid() != 0) {
344 // Old versions of the adb client would call the
345 // dumpstate command directly. Newer clients
346 // call /system/bin/bugreport instead. If we detect
347 // we're being called incorrectly, then exec the
348 // correct program.
349 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
350 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700351 ALOGI("begin\n");
352
John Michelau885f8882013-05-06 16:42:02 -0500353 memset(&sigact, 0, sizeof(sigact));
354 sigact.sa_handler = sigpipe_handler;
355 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700356
Colin Crossf45fa6b2012-03-26 12:38:26 -0700357 /* set as high priority, and protect from OOM killer */
358 setpriority(PRIO_PROCESS, 0, -20);
359 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
360 if (oom_adj) {
361 fputs("-17", oom_adj);
362 fclose(oom_adj);
363 }
364
Jeff Brownbf7f4922012-06-07 16:40:01 -0700365 /* very first thing, collect stack traces from Dalvik and native processes (needs root) */
366 dump_traces_path = dump_traces();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700367
368 int c;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700369 while ((c = getopt(argc, argv, "b:de:ho:svqzpB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700370 switch (c) {
371 case 'b': begin_sound = optarg; break;
372 case 'd': do_add_date = 1; break;
373 case 'e': end_sound = optarg; break;
374 case 'o': use_outfile = optarg; break;
375 case 's': use_socket = 1; break;
376 case 'v': break; // compatibility no-op
John Michelau1f794c42012-09-17 11:20:19 -0500377 case 'q': do_vibrate = 0; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700378 case 'z': do_compress = 6; break;
379 case 'p': do_fb = 1; break;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700380 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700381 case '?': printf("\n");
382 case 'h':
383 usage();
384 exit(1);
385 }
386 }
387
John Michelau1f794c42012-09-17 11:20:19 -0500388 FILE *vibrator = 0;
389 if (do_vibrate) {
390 /* open the vibrator before dropping root */
391 vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
392 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
393 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700394
395 /* read /proc/cmdline before dropping root */
396 FILE *cmdline = fopen("/proc/cmdline", "r");
397 if (cmdline != NULL) {
398 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
399 fclose(cmdline);
400 }
401
Nick Kralevich1e339872012-04-25 13:38:45 -0700402 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
403 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
404 return -1;
405 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700406
Nick Kralevich1e339872012-04-25 13:38:45 -0700407 /* switch to non-root user and group */
408 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
409 AID_MOUNT, AID_INET, AID_NET_BW_STATS };
410 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
411 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
412 return -1;
413 }
414 if (setgid(AID_SHELL) != 0) {
415 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
416 return -1;
417 }
418 if (setuid(AID_SHELL) != 0) {
419 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
420 return -1;
421 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700422
Nick Kralevich1e339872012-04-25 13:38:45 -0700423 struct __user_cap_header_struct capheader;
424 struct __user_cap_data_struct capdata[2];
425 memset(&capheader, 0, sizeof(capheader));
426 memset(&capdata, 0, sizeof(capdata));
427 capheader.version = _LINUX_CAPABILITY_VERSION_3;
428 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700429
Nick Kralevich1e339872012-04-25 13:38:45 -0700430 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
431 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
432 capdata[0].inheritable = 0;
433 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700434
Nick Kralevich1e339872012-04-25 13:38:45 -0700435 if (capset(&capheader, &capdata[0]) < 0) {
436 ALOGE("capset failed: %s\n", strerror(errno));
437 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700438 }
439
440 char path[PATH_MAX], tmp_path[PATH_MAX];
441 pid_t gzip_pid = -1;
442
443 if (use_socket) {
444 redirect_to_socket(stdout, "dumpstate");
445 } else if (use_outfile) {
446 strlcpy(path, use_outfile, sizeof(path));
447 if (do_add_date) {
448 char date[80];
449 time_t now = time(NULL);
450 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
451 strlcat(path, date, sizeof(path));
452 }
453 if (do_fb) {
454 strlcpy(screenshot_path, path, sizeof(screenshot_path));
455 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
456 }
457 strlcat(path, ".txt", sizeof(path));
458 if (do_compress) strlcat(path, ".gz", sizeof(path));
459 strlcpy(tmp_path, path, sizeof(tmp_path));
460 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
461 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
462 }
463
464 if (begin_sound) {
465 play_sound(begin_sound);
466 } else if (vibrator) {
467 fputs("150", vibrator);
468 fflush(vibrator);
469 }
470
471 dumpstate();
472
473 if (end_sound) {
474 play_sound(end_sound);
475 } else if (vibrator) {
476 int i;
477 for (i = 0; i < 3; i++) {
478 fputs("75\n", vibrator);
479 fflush(vibrator);
480 usleep((75 + 50) * 1000);
481 }
482 fclose(vibrator);
483 }
484
485 /* wait for gzip to finish, otherwise it might get killed when we exit */
486 if (gzip_pid > 0) {
487 fclose(stdout);
488 waitpid(gzip_pid, NULL, 0);
489 }
490
491 /* rename the (now complete) .tmp file to its final location */
492 if (use_outfile && rename(tmp_path, path)) {
493 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
494 }
495
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700496 if (do_broadcast && use_outfile && do_fb) {
Jeff Sharkeyaaaa57b2013-03-25 17:10:45 -0700497 run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0",
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700498 "-a", "android.intent.action.BUGREPORT_FINISHED",
499 "--es", "android.intent.extra.BUGREPORT", path,
500 "--es", "android.intent.extra.SCREENSHOT", screenshot_path,
501 "--receiver-permission", "android.permission.DUMP", NULL);
502 }
503
Colin Crossf45fa6b2012-03-26 12:38:26 -0700504 ALOGI("done\n");
505
506 return 0;
507}