blob: 220af478acd5366f4be366aa6f19d3f07fed5573 [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
Todd Poynor2a83daa2013-11-22 15:44:22 -080046#define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
47
Colin Crossf45fa6b2012-03-26 12:38:26 -070048/* dumps the current system state to stdout */
49static void dumpstate() {
50 time_t now = time(NULL);
51 char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
52 char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
53 char network[PROPERTY_VALUE_MAX], date[80];
54 char build_type[PROPERTY_VALUE_MAX];
55
56 property_get("ro.build.display.id", build, "(unknown)");
57 property_get("ro.build.fingerprint", fingerprint, "(unknown)");
58 property_get("ro.build.type", build_type, "(unknown)");
59 property_get("ro.baseband", radio, "(unknown)");
60 property_get("ro.bootloader", bootloader, "(unknown)");
61 property_get("gsm.operator.alpha", network, "(unknown)");
62 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
63
64 printf("========================================================\n");
65 printf("== dumpstate: %s\n", date);
66 printf("========================================================\n");
67
68 printf("\n");
69 printf("Build: %s\n", build);
70 printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
71 printf("Bootloader: %s\n", bootloader);
72 printf("Radio: %s\n", radio);
73 printf("Network: %s\n", network);
74
75 printf("Kernel: ");
76 dump_file(NULL, "/proc/version");
77 printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
78 printf("\n");
79
80 run_command("UPTIME", 10, "uptime", NULL);
81 dump_file("MEMORY INFO", "/proc/meminfo");
82 run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
83 run_command("PROCRANK", 20, "procrank", NULL);
84 dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
85 dump_file("VMALLOC INFO", "/proc/vmallocinfo");
86 dump_file("SLAB INFO", "/proc/slabinfo");
87 dump_file("ZONEINFO", "/proc/zoneinfo");
88 dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
89 dump_file("BUDDYINFO", "/proc/buddyinfo");
Colin Cross2281af92012-10-28 22:41:06 -070090 dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
Colin Crossf45fa6b2012-03-26 12:38:26 -070091
92
93 dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
Todd Poynor29e27a82012-05-22 17:54:59 -070094 dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
Colin Crossf45fa6b2012-03-26 12:38:26 -070095 dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
Mathias Agopian85aea742012-08-08 15:32:02 -070096 dump_file("KERNEL SYNC", "/d/sync");
Colin Crossf45fa6b2012-03-26 12:38:26 -070097
98 run_command("PROCESSES", 10, "ps", "-P", NULL);
99 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
Nick Kralevich76b45c12013-07-15 12:21:40 -0700100 run_command("PROCESSES (SELINUX LABELS)", 10, "ps", "-Z", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700101 run_command("LIBRANK", 10, "librank", NULL);
102
103 do_dmesg();
104
105 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
106
Jeff Sharkey5a930032013-03-19 15:05:19 -0700107 if (screenshot_path[0]) {
108 ALOGI("taking screenshot\n");
109 run_command(NULL, 10, "/system/bin/screencap", "-p", screenshot_path, NULL);
110 ALOGI("wrote screenshot: %s\n", screenshot_path);
111 }
112
Colin Crossf45fa6b2012-03-26 12:38:26 -0700113 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
Colin Cross0c22e8b2012-11-02 15:46:56 -0700114 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700115
116 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
117 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
118 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);
120
121
122 /* show the traces we collected in main(), if that was done */
123 if (dump_traces_path != NULL) {
124 dump_file("VM TRACES JUST NOW", dump_traces_path);
125 }
126
127 /* only show ANR traces if they're less than 15 minutes old */
128 struct stat st;
129 char anr_traces_path[PATH_MAX];
130 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
131 if (!anr_traces_path[0]) {
132 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
133 } else if (stat(anr_traces_path, &st)) {
134 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
135 } else {
136 dump_file("VM TRACES AT LAST ANR", anr_traces_path);
137 }
138
139 /* slow traces for slow operations */
140 if (anr_traces_path[0] != 0) {
141 int tail = strlen(anr_traces_path)-1;
142 while (tail > 0 && anr_traces_path[tail] != '/') {
143 tail--;
144 }
145 int i = 0;
146 while (1) {
147 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
148 if (stat(anr_traces_path, &st)) {
149 // No traces file at this index, done with the files.
150 break;
151 }
152 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
153 i++;
154 }
155 }
156
157 dump_file("NETWORK DEV INFO", "/proc/net/dev");
158 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700159 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700160 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
161 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
162
163 dump_file("NETWORK ROUTES", "/proc/net/route");
164 dump_file("NETWORK ROUTES IPV6", "/proc/net/ipv6_route");
165
Todd Poynor2a83daa2013-11-22 15:44:22 -0800166 if (!stat(PSTORE_LAST_KMSG, &st)) {
167 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
168 dump_file("LAST KMSG", PSTORE_LAST_KMSG);
169 } else {
170 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
171 dump_file("LAST KMSG", "/proc/last_kmsg");
172 }
173
Colin Crossf45fa6b2012-03-26 12:38:26 -0700174 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
175 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
176
Colin Crossf45fa6b2012-03-26 12:38:26 -0700177 run_command("SYSTEM SETTINGS", 20, SU_PATH, "root", "sqlite3",
178 "/data/data/com.android.providers.settings/databases/settings.db",
Jeff Sharkey719b8aa2012-10-01 12:52:42 -0700179 "pragma user_version; select * from system; select * from secure; select * from global;", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700180
181 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
182 run_command("NETWORK INTERFACES", 10, SU_PATH, "root", "netcfg", NULL);
183 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
184 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
185 run_command("ROUTE TABLE 60", 10, "ip", "route", "show", "table", "60", NULL);
186 run_command("ROUTE TABLE 61 v6", 10, "ip", "-6", "route", "show", "table", "60", NULL);
187 run_command("ROUTE TABLE 61", 10, "ip", "route", "show", "table", "61", NULL);
188 run_command("ROUTE TABLE 61 v6", 10, "ip", "-6", "route", "show", "table", "61", NULL);
189 dump_file("ARP CACHE", "/proc/net/arp");
190 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
191 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700192 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
193 /* no ip6 nat */
194 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
195 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700196
197 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700198 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700199
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800200#ifdef FWDUMP_bcmdhd
201 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
202 SU_PATH, "root", "wlutil", "counters", NULL);
203#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800204 dump_file("INTERRUPTS (1)", "/proc/interrupts");
205
Colin Crossf45fa6b2012-03-26 12:38:26 -0700206 property_get("dhcp.wlan0.gateway", network, "");
207 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800208 run_command("PING GATEWAY", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700209 property_get("dhcp.wlan0.dns1", network, "");
210 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800211 run_command("PING DNS1", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700212 property_get("dhcp.wlan0.dns2", network, "");
213 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800214 run_command("PING DNS2", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800215#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700216 run_command("DUMP WIFI STATUS", 20,
217 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
218 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
219 SU_PATH, "root", "wlutil", "counters", NULL);
220#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800221 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700222
223 print_properties();
224
225 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
226 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
227
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800228 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700229
Jeff Sharkey13461c22012-05-17 12:40:11 -0700230 run_command("PACKAGE SETTINGS", 20, SU_PATH, "root", "cat", "/data/system/packages.xml", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700231 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
232
233 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
234
235 printf("------ BACKLIGHTS ------\n");
236 printf("LCD brightness=");
237 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
238 printf("Button brightness=");
239 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
240 printf("Keyboard brightness=");
241 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
242 printf("ALS mode=");
243 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
244 printf("LCD driver registers:\n");
245 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
246 printf("\n");
247
248 /* Binder state is expensive to look at as it uses a lot of memory. */
249 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
250 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
251 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
252 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
253 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
254
Colin Crossf45fa6b2012-03-26 12:38:26 -0700255 printf("========================================================\n");
256 printf("== Board\n");
257 printf("========================================================\n");
258
259 dumpstate_board();
260 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700261
262 /* Migrate the ril_dumpstate to a dumpstate_board()? */
263 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
264 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
265 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
266 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
267 // su does not exist on user builds, so try running without it.
268 // This way any implementations of vril-dump that do not require
269 // root can run on user builds.
270 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
271 "vril-dump", NULL);
272 } else {
273 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
274 SU_PATH, "root", "vril-dump", NULL);
275 }
276 }
277
278 printf("========================================================\n");
279 printf("== Android Framework Services\n");
280 printf("========================================================\n");
281
282 /* the full dumpsys is starting to take a long time, so we need
283 to increase its timeout. we really need to do the timeouts in
284 dumpsys itself... */
285 run_command("DUMPSYS", 60, "dumpsys", NULL);
286
287 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700288 printf("== Checkins\n");
289 printf("========================================================\n");
290
Dianne Hackborn59b15162013-09-04 18:04:14 -0700291 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700292 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700293 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700294 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700295 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700296
297 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700298 printf("== Running Application Activities\n");
299 printf("========================================================\n");
300
301 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
302
303 printf("========================================================\n");
304 printf("== Running Application Services\n");
305 printf("========================================================\n");
306
307 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
308
309 printf("========================================================\n");
310 printf("== Running Application Providers\n");
311 printf("========================================================\n");
312
313 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
314
315
316 printf("========================================================\n");
317 printf("== dumpstate: done\n");
318 printf("========================================================\n");
319}
320
321static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500322 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700323 " -o: write to file (instead of stdout)\n"
324 " -d: append date to filename (requires -o)\n"
325 " -z: gzip output (requires -o)\n"
326 " -p: capture screenshot to filename.png (requires -o)\n"
327 " -s: write output to control socket (for init)\n"
328 " -b: play sound file instead of vibrate, at beginning of job\n"
329 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500330 " -q: disable vibrate\n"
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700331 " -B: send broadcast when finished (requires -o and -p)\n"
Todd Poynor2a83daa2013-11-22 15:44:22 -0800332 );
Colin Crossf45fa6b2012-03-26 12:38:26 -0700333}
334
John Michelau885f8882013-05-06 16:42:02 -0500335static void sigpipe_handler(int n) {
336 (void)n;
337 exit(EXIT_FAILURE);
338}
339
Colin Crossf45fa6b2012-03-26 12:38:26 -0700340int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500341 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700342 int do_add_date = 0;
343 int do_compress = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500344 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700345 char* use_outfile = 0;
346 char* begin_sound = 0;
347 char* end_sound = 0;
348 int use_socket = 0;
349 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700350 int do_broadcast = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700351
Nick Kralevich1e339872012-04-25 13:38:45 -0700352 if (getuid() != 0) {
353 // Old versions of the adb client would call the
354 // dumpstate command directly. Newer clients
355 // call /system/bin/bugreport instead. If we detect
356 // we're being called incorrectly, then exec the
357 // correct program.
358 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
359 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700360 ALOGI("begin\n");
361
John Michelau885f8882013-05-06 16:42:02 -0500362 memset(&sigact, 0, sizeof(sigact));
363 sigact.sa_handler = sigpipe_handler;
364 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700365
Colin Crossf45fa6b2012-03-26 12:38:26 -0700366 /* set as high priority, and protect from OOM killer */
367 setpriority(PRIO_PROCESS, 0, -20);
368 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
369 if (oom_adj) {
370 fputs("-17", oom_adj);
371 fclose(oom_adj);
372 }
373
Jeff Brownbf7f4922012-06-07 16:40:01 -0700374 /* very first thing, collect stack traces from Dalvik and native processes (needs root) */
375 dump_traces_path = dump_traces();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700376
377 int c;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700378 while ((c = getopt(argc, argv, "b:de:ho:svqzpB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700379 switch (c) {
380 case 'b': begin_sound = optarg; break;
381 case 'd': do_add_date = 1; break;
382 case 'e': end_sound = optarg; break;
383 case 'o': use_outfile = optarg; break;
384 case 's': use_socket = 1; break;
385 case 'v': break; // compatibility no-op
John Michelau1f794c42012-09-17 11:20:19 -0500386 case 'q': do_vibrate = 0; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700387 case 'z': do_compress = 6; break;
388 case 'p': do_fb = 1; break;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700389 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700390 case '?': printf("\n");
391 case 'h':
392 usage();
393 exit(1);
394 }
395 }
396
John Michelau1f794c42012-09-17 11:20:19 -0500397 FILE *vibrator = 0;
398 if (do_vibrate) {
399 /* open the vibrator before dropping root */
400 vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
401 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
402 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700403
404 /* read /proc/cmdline before dropping root */
405 FILE *cmdline = fopen("/proc/cmdline", "r");
406 if (cmdline != NULL) {
407 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
408 fclose(cmdline);
409 }
410
Nick Kralevich1e339872012-04-25 13:38:45 -0700411 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
412 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
413 return -1;
414 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700415
Nick Kralevich1e339872012-04-25 13:38:45 -0700416 /* switch to non-root user and group */
417 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
418 AID_MOUNT, AID_INET, AID_NET_BW_STATS };
419 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
420 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
421 return -1;
422 }
423 if (setgid(AID_SHELL) != 0) {
424 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
425 return -1;
426 }
427 if (setuid(AID_SHELL) != 0) {
428 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
429 return -1;
430 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700431
Nick Kralevich1e339872012-04-25 13:38:45 -0700432 struct __user_cap_header_struct capheader;
433 struct __user_cap_data_struct capdata[2];
434 memset(&capheader, 0, sizeof(capheader));
435 memset(&capdata, 0, sizeof(capdata));
436 capheader.version = _LINUX_CAPABILITY_VERSION_3;
437 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700438
Nick Kralevich1e339872012-04-25 13:38:45 -0700439 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
440 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
441 capdata[0].inheritable = 0;
442 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700443
Nick Kralevich1e339872012-04-25 13:38:45 -0700444 if (capset(&capheader, &capdata[0]) < 0) {
445 ALOGE("capset failed: %s\n", strerror(errno));
446 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700447 }
448
449 char path[PATH_MAX], tmp_path[PATH_MAX];
450 pid_t gzip_pid = -1;
451
452 if (use_socket) {
453 redirect_to_socket(stdout, "dumpstate");
454 } else if (use_outfile) {
455 strlcpy(path, use_outfile, sizeof(path));
456 if (do_add_date) {
457 char date[80];
458 time_t now = time(NULL);
459 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
460 strlcat(path, date, sizeof(path));
461 }
462 if (do_fb) {
463 strlcpy(screenshot_path, path, sizeof(screenshot_path));
464 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
465 }
466 strlcat(path, ".txt", sizeof(path));
467 if (do_compress) strlcat(path, ".gz", sizeof(path));
468 strlcpy(tmp_path, path, sizeof(tmp_path));
469 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
470 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
471 }
472
473 if (begin_sound) {
474 play_sound(begin_sound);
475 } else if (vibrator) {
476 fputs("150", vibrator);
477 fflush(vibrator);
478 }
479
480 dumpstate();
481
482 if (end_sound) {
483 play_sound(end_sound);
484 } else if (vibrator) {
485 int i;
486 for (i = 0; i < 3; i++) {
487 fputs("75\n", vibrator);
488 fflush(vibrator);
489 usleep((75 + 50) * 1000);
490 }
491 fclose(vibrator);
492 }
493
494 /* wait for gzip to finish, otherwise it might get killed when we exit */
495 if (gzip_pid > 0) {
496 fclose(stdout);
497 waitpid(gzip_pid, NULL, 0);
498 }
499
500 /* rename the (now complete) .tmp file to its final location */
501 if (use_outfile && rename(tmp_path, path)) {
502 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
503 }
504
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700505 if (do_broadcast && use_outfile && do_fb) {
Jeff Sharkeyaaaa57b2013-03-25 17:10:45 -0700506 run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0",
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700507 "-a", "android.intent.action.BUGREPORT_FINISHED",
508 "--es", "android.intent.extra.BUGREPORT", path,
509 "--es", "android.intent.extra.SCREENSHOT", screenshot_path,
510 "--receiver-permission", "android.permission.DUMP", NULL);
511 }
512
Colin Crossf45fa6b2012-03-26 12:38:26 -0700513 ALOGI("done\n");
514
515 return 0;
516}