blob: ff01da512731b124f16c560c611f40c318f26b49 [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>
Elliott Hughesd2a04922014-07-18 17:54:09 -070029#include <sys/prctl.h>
Colin Crossf45fa6b2012-03-26 12:38:26 -070030
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");
Matthew Xief3381cf2014-07-11 13:58:17 -070097 dump_file("KERNEL BLUEDROID", "/d/bluedroid");
Colin Crossf45fa6b2012-03-26 12:38:26 -070098
99 run_command("PROCESSES", 10, "ps", "-P", NULL);
100 run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
Nick Kralevich76b45c12013-07-15 12:21:40 -0700101 run_command("PROCESSES (SELINUX LABELS)", 10, "ps", "-Z", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700102 run_command("LIBRANK", 10, "librank", NULL);
103
104 do_dmesg();
105
106 run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
107
Jeff Sharkey5a930032013-03-19 15:05:19 -0700108 if (screenshot_path[0]) {
109 ALOGI("taking screenshot\n");
110 run_command(NULL, 10, "/system/bin/screencap", "-p", screenshot_path, NULL);
111 ALOGI("wrote screenshot: %s\n", screenshot_path);
112 }
113
Colin Crossf45fa6b2012-03-26 12:38:26 -0700114 for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
Colin Cross0c22e8b2012-11-02 15:46:56 -0700115 for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700116
117 // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
118 run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
119 run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
120 run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
121
122
123 /* show the traces we collected in main(), if that was done */
124 if (dump_traces_path != NULL) {
125 dump_file("VM TRACES JUST NOW", dump_traces_path);
126 }
127
128 /* only show ANR traces if they're less than 15 minutes old */
129 struct stat st;
130 char anr_traces_path[PATH_MAX];
131 property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
132 if (!anr_traces_path[0]) {
133 printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
134 } else if (stat(anr_traces_path, &st)) {
135 printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
136 } else {
137 dump_file("VM TRACES AT LAST ANR", anr_traces_path);
138 }
139
140 /* slow traces for slow operations */
141 if (anr_traces_path[0] != 0) {
142 int tail = strlen(anr_traces_path)-1;
143 while (tail > 0 && anr_traces_path[tail] != '/') {
144 tail--;
145 }
146 int i = 0;
147 while (1) {
148 sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
149 if (stat(anr_traces_path, &st)) {
150 // No traces file at this index, done with the files.
151 break;
152 }
153 dump_file("VM TRACES WHEN SLOW", anr_traces_path);
154 i++;
155 }
156 }
157
158 dump_file("NETWORK DEV INFO", "/proc/net/dev");
159 dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
JP Abgrall012c2ea2012-05-16 20:49:29 -0700160 dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700161 dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
162 dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
163
164 dump_file("NETWORK ROUTES", "/proc/net/route");
165 dump_file("NETWORK ROUTES IPV6", "/proc/net/ipv6_route");
166
Todd Poynor2a83daa2013-11-22 15:44:22 -0800167 if (!stat(PSTORE_LAST_KMSG, &st)) {
168 /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
169 dump_file("LAST KMSG", PSTORE_LAST_KMSG);
170 } else {
171 /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
172 dump_file("LAST KMSG", "/proc/last_kmsg");
173 }
174
Colin Crossf45fa6b2012-03-26 12:38:26 -0700175 dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
176 dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
177
John Spurlock5ecd4be2014-01-29 14:14:40 -0500178 for_each_userid(do_dump_settings, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700179
180 /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
181 run_command("NETWORK INTERFACES", 10, SU_PATH, "root", "netcfg", NULL);
182 run_command("IP RULES", 10, "ip", "rule", "show", NULL);
183 run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
Sreeram Ramachandran2b3bba32014-07-08 15:40:55 -0700184
185 dump_route_tables();
186
Colin Crossf45fa6b2012-03-26 12:38:26 -0700187 dump_file("ARP CACHE", "/proc/net/arp");
188 run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
189 run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
JP Abgrall012c2ea2012-05-16 20:49:29 -0700190 run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
191 /* no ip6 nat */
192 run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
193 run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700194
195 run_command("WIFI NETWORKS", 20,
Dmitry Shmidt1d6b97c2013-08-21 10:58:29 -0700196 SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700197
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800198#ifdef FWDUMP_bcmdhd
199 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
200 SU_PATH, "root", "wlutil", "counters", NULL);
201#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800202 dump_file("INTERRUPTS (1)", "/proc/interrupts");
203
Colin Crossf45fa6b2012-03-26 12:38:26 -0700204 property_get("dhcp.wlan0.gateway", network, "");
205 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800206 run_command("PING GATEWAY", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700207 property_get("dhcp.wlan0.dns1", network, "");
208 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800209 run_command("PING DNS1", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700210 property_get("dhcp.wlan0.dns2", network, "");
211 if (network[0])
Dmitry Shmidt5b817642013-02-22 11:27:58 -0800212 run_command("PING DNS2", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
Dmitry Shmidtc11f56e2012-11-07 10:42:05 -0800213#ifdef FWDUMP_bcmdhd
Colin Crossf45fa6b2012-03-26 12:38:26 -0700214 run_command("DUMP WIFI STATUS", 20,
215 SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
216 run_command("DUMP WIFI INTERNAL COUNTERS", 20,
217 SU_PATH, "root", "wlutil", "counters", NULL);
218#endif
Dmitry Shmidt0b2c9262012-11-07 11:09:46 -0800219 dump_file("INTERRUPTS (2)", "/proc/interrupts");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700220
221 print_properties();
222
223 run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
224 run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
225
Ken Sumrall8f75fa72013-02-08 17:35:58 -0800226 run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700227
Jeff Sharkey13461c22012-05-17 12:40:11 -0700228 run_command("PACKAGE SETTINGS", 20, SU_PATH, "root", "cat", "/data/system/packages.xml", NULL);
Colin Crossf45fa6b2012-03-26 12:38:26 -0700229 dump_file("PACKAGE UID ERRORS", "/data/system/uiderrors.txt");
230
231 run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
232
233 printf("------ BACKLIGHTS ------\n");
234 printf("LCD brightness=");
235 dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
236 printf("Button brightness=");
237 dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
238 printf("Keyboard brightness=");
239 dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
240 printf("ALS mode=");
241 dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
242 printf("LCD driver registers:\n");
243 dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
244 printf("\n");
245
246 /* Binder state is expensive to look at as it uses a lot of memory. */
247 dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
248 dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
249 dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
250 dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
251 dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
252
Colin Crossf45fa6b2012-03-26 12:38:26 -0700253 printf("========================================================\n");
254 printf("== Board\n");
255 printf("========================================================\n");
256
257 dumpstate_board();
258 printf("\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700259
260 /* Migrate the ril_dumpstate to a dumpstate_board()? */
261 char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
262 property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
263 if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
264 if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
265 // su does not exist on user builds, so try running without it.
266 // This way any implementations of vril-dump that do not require
267 // root can run on user builds.
268 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
269 "vril-dump", NULL);
270 } else {
271 run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
272 SU_PATH, "root", "vril-dump", NULL);
273 }
274 }
275
276 printf("========================================================\n");
277 printf("== Android Framework Services\n");
278 printf("========================================================\n");
279
280 /* the full dumpsys is starting to take a long time, so we need
281 to increase its timeout. we really need to do the timeouts in
282 dumpsys itself... */
283 run_command("DUMPSYS", 60, "dumpsys", NULL);
284
285 printf("========================================================\n");
Dianne Hackborn02bea972013-06-26 18:59:09 -0700286 printf("== Checkins\n");
287 printf("========================================================\n");
288
Dianne Hackborn59b15162013-09-04 18:04:14 -0700289 run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
Dianne Hackborn3e5fa732013-07-03 16:51:15 -0700290 run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700291 run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
Dianne Hackborn5cd46aa2013-07-09 15:01:40 -0700292 run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
Dianne Hackborn1bd50682013-07-11 11:45:18 -0700293 run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
Dianne Hackborn02bea972013-06-26 18:59:09 -0700294
295 printf("========================================================\n");
Colin Crossf45fa6b2012-03-26 12:38:26 -0700296 printf("== Running Application Activities\n");
297 printf("========================================================\n");
298
299 run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
300
301 printf("========================================================\n");
302 printf("== Running Application Services\n");
303 printf("========================================================\n");
304
305 run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
306
307 printf("========================================================\n");
308 printf("== Running Application Providers\n");
309 printf("========================================================\n");
310
311 run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
312
313
314 printf("========================================================\n");
315 printf("== dumpstate: done\n");
316 printf("========================================================\n");
317}
318
319static void usage() {
John Michelau1f794c42012-09-17 11:20:19 -0500320 fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
Colin Crossf45fa6b2012-03-26 12:38:26 -0700321 " -o: write to file (instead of stdout)\n"
322 " -d: append date to filename (requires -o)\n"
323 " -z: gzip output (requires -o)\n"
324 " -p: capture screenshot to filename.png (requires -o)\n"
325 " -s: write output to control socket (for init)\n"
326 " -b: play sound file instead of vibrate, at beginning of job\n"
327 " -e: play sound file instead of vibrate, at end of job\n"
John Michelau1f794c42012-09-17 11:20:19 -0500328 " -q: disable vibrate\n"
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700329 " -B: send broadcast when finished (requires -o and -p)\n"
Todd Poynor2a83daa2013-11-22 15:44:22 -0800330 );
Colin Crossf45fa6b2012-03-26 12:38:26 -0700331}
332
John Michelau885f8882013-05-06 16:42:02 -0500333static void sigpipe_handler(int n) {
334 (void)n;
335 exit(EXIT_FAILURE);
336}
337
Colin Crossf45fa6b2012-03-26 12:38:26 -0700338int main(int argc, char *argv[]) {
John Michelau885f8882013-05-06 16:42:02 -0500339 struct sigaction sigact;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700340 int do_add_date = 0;
341 int do_compress = 0;
John Michelau1f794c42012-09-17 11:20:19 -0500342 int do_vibrate = 1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700343 char* use_outfile = 0;
344 char* begin_sound = 0;
345 char* end_sound = 0;
346 int use_socket = 0;
347 int do_fb = 0;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700348 int do_broadcast = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700349
Nick Kralevich1e339872012-04-25 13:38:45 -0700350 if (getuid() != 0) {
351 // Old versions of the adb client would call the
352 // dumpstate command directly. Newer clients
353 // call /system/bin/bugreport instead. If we detect
354 // we're being called incorrectly, then exec the
355 // correct program.
356 return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
357 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700358 ALOGI("begin\n");
359
John Michelau885f8882013-05-06 16:42:02 -0500360 memset(&sigact, 0, sizeof(sigact));
361 sigact.sa_handler = sigpipe_handler;
362 sigaction(SIGPIPE, &sigact, NULL);
JP Abgrall3e03d3f2012-05-11 14:14:09 -0700363
Colin Crossf45fa6b2012-03-26 12:38:26 -0700364 /* set as high priority, and protect from OOM killer */
365 setpriority(PRIO_PROCESS, 0, -20);
366 FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
367 if (oom_adj) {
368 fputs("-17", oom_adj);
369 fclose(oom_adj);
370 }
371
Jeff Brownbf7f4922012-06-07 16:40:01 -0700372 /* very first thing, collect stack traces from Dalvik and native processes (needs root) */
373 dump_traces_path = dump_traces();
Colin Crossf45fa6b2012-03-26 12:38:26 -0700374
375 int c;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700376 while ((c = getopt(argc, argv, "b:de:ho:svqzpB")) != -1) {
Colin Crossf45fa6b2012-03-26 12:38:26 -0700377 switch (c) {
378 case 'b': begin_sound = optarg; break;
379 case 'd': do_add_date = 1; break;
380 case 'e': end_sound = optarg; break;
381 case 'o': use_outfile = optarg; break;
382 case 's': use_socket = 1; break;
383 case 'v': break; // compatibility no-op
John Michelau1f794c42012-09-17 11:20:19 -0500384 case 'q': do_vibrate = 0; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700385 case 'z': do_compress = 6; break;
386 case 'p': do_fb = 1; break;
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700387 case 'B': do_broadcast = 1; break;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700388 case '?': printf("\n");
389 case 'h':
390 usage();
391 exit(1);
392 }
393 }
394
John Michelau1f794c42012-09-17 11:20:19 -0500395 FILE *vibrator = 0;
396 if (do_vibrate) {
397 /* open the vibrator before dropping root */
398 vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
399 if (vibrator) fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
400 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700401
402 /* read /proc/cmdline before dropping root */
403 FILE *cmdline = fopen("/proc/cmdline", "r");
404 if (cmdline != NULL) {
405 fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
406 fclose(cmdline);
407 }
408
Nick Kralevich1e339872012-04-25 13:38:45 -0700409 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
410 ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
411 return -1;
412 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700413
Nick Kralevich1e339872012-04-25 13:38:45 -0700414 /* switch to non-root user and group */
415 gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
416 AID_MOUNT, AID_INET, AID_NET_BW_STATS };
417 if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
418 ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
419 return -1;
420 }
421 if (setgid(AID_SHELL) != 0) {
422 ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
423 return -1;
424 }
425 if (setuid(AID_SHELL) != 0) {
426 ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
427 return -1;
428 }
Colin Crossf45fa6b2012-03-26 12:38:26 -0700429
Nick Kralevich1e339872012-04-25 13:38:45 -0700430 struct __user_cap_header_struct capheader;
431 struct __user_cap_data_struct capdata[2];
432 memset(&capheader, 0, sizeof(capheader));
433 memset(&capdata, 0, sizeof(capdata));
434 capheader.version = _LINUX_CAPABILITY_VERSION_3;
435 capheader.pid = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700436
Nick Kralevich1e339872012-04-25 13:38:45 -0700437 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
438 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
439 capdata[0].inheritable = 0;
440 capdata[1].inheritable = 0;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700441
Nick Kralevich1e339872012-04-25 13:38:45 -0700442 if (capset(&capheader, &capdata[0]) < 0) {
443 ALOGE("capset failed: %s\n", strerror(errno));
444 return -1;
Colin Crossf45fa6b2012-03-26 12:38:26 -0700445 }
446
447 char path[PATH_MAX], tmp_path[PATH_MAX];
448 pid_t gzip_pid = -1;
449
450 if (use_socket) {
451 redirect_to_socket(stdout, "dumpstate");
452 } else if (use_outfile) {
453 strlcpy(path, use_outfile, sizeof(path));
454 if (do_add_date) {
455 char date[80];
456 time_t now = time(NULL);
457 strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
458 strlcat(path, date, sizeof(path));
459 }
460 if (do_fb) {
461 strlcpy(screenshot_path, path, sizeof(screenshot_path));
462 strlcat(screenshot_path, ".png", sizeof(screenshot_path));
463 }
464 strlcat(path, ".txt", sizeof(path));
465 if (do_compress) strlcat(path, ".gz", sizeof(path));
466 strlcpy(tmp_path, path, sizeof(tmp_path));
467 strlcat(tmp_path, ".tmp", sizeof(tmp_path));
468 gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
469 }
470
471 if (begin_sound) {
472 play_sound(begin_sound);
473 } else if (vibrator) {
474 fputs("150", vibrator);
475 fflush(vibrator);
476 }
477
478 dumpstate();
479
480 if (end_sound) {
481 play_sound(end_sound);
482 } else if (vibrator) {
483 int i;
484 for (i = 0; i < 3; i++) {
485 fputs("75\n", vibrator);
486 fflush(vibrator);
487 usleep((75 + 50) * 1000);
488 }
489 fclose(vibrator);
490 }
491
492 /* wait for gzip to finish, otherwise it might get killed when we exit */
493 if (gzip_pid > 0) {
494 fclose(stdout);
495 waitpid(gzip_pid, NULL, 0);
496 }
497
498 /* rename the (now complete) .tmp file to its final location */
499 if (use_outfile && rename(tmp_path, path)) {
500 fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
501 }
502
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700503 if (do_broadcast && use_outfile && do_fb) {
Jeff Sharkeyaaaa57b2013-03-25 17:10:45 -0700504 run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0",
Jeff Sharkey27f9e6d2013-03-13 15:45:50 -0700505 "-a", "android.intent.action.BUGREPORT_FINISHED",
506 "--es", "android.intent.extra.BUGREPORT", path,
507 "--es", "android.intent.extra.SCREENSHOT", screenshot_path,
508 "--receiver-permission", "android.permission.DUMP", NULL);
509 }
510
Colin Crossf45fa6b2012-03-26 12:38:26 -0700511 ALOGI("done\n");
512
513 return 0;
514}