Home | History | Annotate | Download | only in dumpstate
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include <dirent.h>
     18 #include <errno.h>
     19 #include <fcntl.h>
     20 #include <limits.h>
     21 #include <stdio.h>
     22 #include <stdlib.h>
     23 #include <string.h>
     24 #include <sys/capability.h>
     25 #include <sys/prctl.h>
     26 #include <sys/resource.h>
     27 #include <sys/stat.h>
     28 #include <sys/time.h>
     29 #include <sys/wait.h>
     30 #include <unistd.h>
     31 
     32 #include <cutils/properties.h>
     33 
     34 #include "private/android_filesystem_config.h"
     35 
     36 #define LOG_TAG "dumpstate"
     37 #include <cutils/log.h>
     38 
     39 #include "dumpstate.h"
     40 
     41 /* read before root is shed */
     42 static char cmdline_buf[16384] = "(unknown)";
     43 static const char *dump_traces_path = NULL;
     44 
     45 static char screenshot_path[PATH_MAX] = "";
     46 
     47 #define PSTORE_LAST_KMSG "/sys/fs/pstore/console-ramoops"
     48 
     49 #define TOMBSTONE_DIR "/data/tombstones"
     50 #define TOMBSTONE_FILE_PREFIX TOMBSTONE_DIR "/tombstone_"
     51 /* Can accomodate a tombstone number up to 9999. */
     52 #define TOMBSTONE_MAX_LEN (sizeof(TOMBSTONE_FILE_PREFIX) + 4)
     53 #define NUM_TOMBSTONES  10
     54 
     55 typedef struct {
     56   char name[TOMBSTONE_MAX_LEN];
     57   int fd;
     58 } tombstone_data_t;
     59 
     60 static tombstone_data_t tombstone_data[NUM_TOMBSTONES];
     61 
     62 /* Get the fds of any tombstone that was modified in the last half an hour. */
     63 static void get_tombstone_fds(tombstone_data_t data[NUM_TOMBSTONES]) {
     64     time_t thirty_minutes_ago = time(NULL) - 60*30;
     65     for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
     66         snprintf(data[i].name, sizeof(data[i].name), "%s%02zu", TOMBSTONE_FILE_PREFIX, i);
     67         int fd = open(data[i].name, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
     68         struct stat st;
     69         if (fstat(fd, &st) == 0 && S_ISREG(st.st_mode) &&
     70                 (time_t) st.st_mtime >= thirty_minutes_ago) {
     71             data[i].fd = fd;
     72         } else {
     73             close(fd);
     74             data[i].fd = -1;
     75         }
     76     }
     77 }
     78 
     79 static void dump_dev_files(const char *title, const char *driverpath, const char *filename)
     80 {
     81     DIR *d;
     82     struct dirent *de;
     83     char path[PATH_MAX];
     84 
     85     d = opendir(driverpath);
     86     if (d == NULL) {
     87         return;
     88     }
     89 
     90     while ((de = readdir(d))) {
     91         if (de->d_type != DT_LNK) {
     92             continue;
     93         }
     94         snprintf(path, sizeof(path), "%s/%s/%s", driverpath, de->d_name, filename);
     95         dump_file(title, path);
     96     }
     97 
     98     closedir(d);
     99 }
    100 
    101 /* dumps the current system state to stdout */
    102 static void dumpstate() {
    103     time_t now = time(NULL);
    104     char build[PROPERTY_VALUE_MAX], fingerprint[PROPERTY_VALUE_MAX];
    105     char radio[PROPERTY_VALUE_MAX], bootloader[PROPERTY_VALUE_MAX];
    106     char network[PROPERTY_VALUE_MAX], date[80];
    107     char build_type[PROPERTY_VALUE_MAX];
    108 
    109     property_get("ro.build.display.id", build, "(unknown)");
    110     property_get("ro.build.fingerprint", fingerprint, "(unknown)");
    111     property_get("ro.build.type", build_type, "(unknown)");
    112     property_get("ro.baseband", radio, "(unknown)");
    113     property_get("ro.bootloader", bootloader, "(unknown)");
    114     property_get("gsm.operator.alpha", network, "(unknown)");
    115     strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", localtime(&now));
    116 
    117     printf("========================================================\n");
    118     printf("== dumpstate: %s\n", date);
    119     printf("========================================================\n");
    120 
    121     printf("\n");
    122     printf("Build: %s\n", build);
    123     printf("Build fingerprint: '%s'\n", fingerprint); /* format is important for other tools */
    124     printf("Bootloader: %s\n", bootloader);
    125     printf("Radio: %s\n", radio);
    126     printf("Network: %s\n", network);
    127 
    128     printf("Kernel: ");
    129     dump_file(NULL, "/proc/version");
    130     printf("Command line: %s\n", strtok(cmdline_buf, "\n"));
    131     printf("\n");
    132 
    133     dump_dev_files("TRUSTY VERSION", "/sys/bus/platform/drivers/trusty", "trusty_version");
    134     run_command("UPTIME", 10, "uptime", NULL);
    135     dump_file("MMC PERF", "/sys/block/mmcblk0/stat");
    136     dump_file("MEMORY INFO", "/proc/meminfo");
    137     run_command("CPU INFO", 10, "top", "-n", "1", "-d", "1", "-m", "30", "-t", NULL);
    138     run_command("PROCRANK", 20, "procrank", NULL);
    139     dump_file("VIRTUAL MEMORY STATS", "/proc/vmstat");
    140     dump_file("VMALLOC INFO", "/proc/vmallocinfo");
    141     dump_file("SLAB INFO", "/proc/slabinfo");
    142     dump_file("ZONEINFO", "/proc/zoneinfo");
    143     dump_file("PAGETYPEINFO", "/proc/pagetypeinfo");
    144     dump_file("BUDDYINFO", "/proc/buddyinfo");
    145     dump_file("FRAGMENTATION INFO", "/d/extfrag/unusable_index");
    146 
    147     dump_file("KERNEL WAKELOCKS", "/proc/wakelocks");
    148     dump_file("KERNEL WAKE SOURCES", "/d/wakeup_sources");
    149     dump_file("KERNEL CPUFREQ", "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state");
    150     dump_file("KERNEL SYNC", "/d/sync");
    151     dump_file("KERNEL BLUEDROID", "/d/bluedroid");
    152 
    153     run_command("PROCESSES", 10, "ps", "-P", NULL);
    154     run_command("PROCESSES AND THREADS", 10, "ps", "-t", "-p", "-P", NULL);
    155     run_command("PROCESSES (SELINUX LABELS)", 10, "ps", "-Z", NULL);
    156     run_command("LIBRANK", 10, "librank", NULL);
    157 
    158     do_dmesg();
    159 
    160     run_command("LIST OF OPEN FILES", 10, SU_PATH, "root", "lsof", NULL);
    161     for_each_pid(do_showmap, "SMAPS OF ALL PROCESSES");
    162     for_each_tid(show_wchan, "BLOCKED PROCESS WAIT-CHANNELS");
    163 
    164     if (screenshot_path[0]) {
    165         ALOGI("taking screenshot\n");
    166         run_command(NULL, 10, "/system/bin/screencap", "-p", screenshot_path, NULL);
    167         ALOGI("wrote screenshot: %s\n", screenshot_path);
    168     }
    169 
    170     // dump_file("EVENT LOG TAGS", "/etc/event-log-tags");
    171     run_command("SYSTEM LOG", 20, "logcat", "-v", "threadtime", "-d", "*:v", NULL);
    172     run_command("EVENT LOG", 20, "logcat", "-b", "events", "-v", "threadtime", "-d", "*:v", NULL);
    173     run_command("RADIO LOG", 20, "logcat", "-b", "radio", "-v", "threadtime", "-d", "*:v", NULL);
    174 
    175     /* show the traces we collected in main(), if that was done */
    176     if (dump_traces_path != NULL) {
    177         dump_file("VM TRACES JUST NOW", dump_traces_path);
    178     }
    179 
    180     /* only show ANR traces if they're less than 15 minutes old */
    181     struct stat st;
    182     char anr_traces_path[PATH_MAX];
    183     property_get("dalvik.vm.stack-trace-file", anr_traces_path, "");
    184     if (!anr_traces_path[0]) {
    185         printf("*** NO VM TRACES FILE DEFINED (dalvik.vm.stack-trace-file)\n\n");
    186     } else {
    187       int fd = open(anr_traces_path, O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
    188       if (fd < 0) {
    189           printf("*** NO ANR VM TRACES FILE (%s): %s\n\n", anr_traces_path, strerror(errno));
    190       } else {
    191           dump_file_from_fd("VM TRACES AT LAST ANR", anr_traces_path, fd);
    192       }
    193     }
    194 
    195     /* slow traces for slow operations */
    196     if (anr_traces_path[0] != 0) {
    197         int tail = strlen(anr_traces_path)-1;
    198         while (tail > 0 && anr_traces_path[tail] != '/') {
    199             tail--;
    200         }
    201         int i = 0;
    202         while (1) {
    203             sprintf(anr_traces_path+tail+1, "slow%02d.txt", i);
    204             if (stat(anr_traces_path, &st)) {
    205                 // No traces file at this index, done with the files.
    206                 break;
    207             }
    208             dump_file("VM TRACES WHEN SLOW", anr_traces_path);
    209             i++;
    210         }
    211     }
    212 
    213     int dumped = 0;
    214     for (size_t i = 0; i < NUM_TOMBSTONES; i++) {
    215         if (tombstone_data[i].fd != -1) {
    216             dumped = 1;
    217             dump_file_from_fd("TOMBSTONE", tombstone_data[i].name, tombstone_data[i].fd);
    218             tombstone_data[i].fd = -1;
    219         }
    220     }
    221     if (!dumped) {
    222         printf("*** NO TOMBSTONES to dump in %s\n\n", TOMBSTONE_DIR);
    223     }
    224 
    225     dump_file("NETWORK DEV INFO", "/proc/net/dev");
    226     dump_file("QTAGUID NETWORK INTERFACES INFO", "/proc/net/xt_qtaguid/iface_stat_all");
    227     dump_file("QTAGUID NETWORK INTERFACES INFO (xt)", "/proc/net/xt_qtaguid/iface_stat_fmt");
    228     dump_file("QTAGUID CTRL INFO", "/proc/net/xt_qtaguid/ctrl");
    229     dump_file("QTAGUID STATS INFO", "/proc/net/xt_qtaguid/stats");
    230 
    231     if (!stat(PSTORE_LAST_KMSG, &st)) {
    232         /* Also TODO: Make console-ramoops CAP_SYSLOG protected. */
    233         dump_file("LAST KMSG", PSTORE_LAST_KMSG);
    234     } else {
    235         /* TODO: Make last_kmsg CAP_SYSLOG protected. b/5555691 */
    236         dump_file("LAST KMSG", "/proc/last_kmsg");
    237     }
    238 
    239     dump_file("LAST PANIC CONSOLE", "/data/dontpanic/apanic_console");
    240     dump_file("LAST PANIC THREADS", "/data/dontpanic/apanic_threads");
    241 
    242     for_each_userid(do_dump_settings, NULL);
    243 
    244     /* The following have a tendency to get wedged when wifi drivers/fw goes belly-up. */
    245     run_command("NETWORK INTERFACES", 10, SU_PATH, "root", "netcfg", NULL);
    246 
    247     run_command("IPv4 ADDRESSES", 10, "ip", "-4", "addr", "show", NULL);
    248     run_command("IPv6 ADDRESSES", 10, "ip", "-6", "addr", "show", NULL);
    249 
    250     run_command("IP RULES", 10, "ip", "rule", "show", NULL);
    251     run_command("IP RULES v6", 10, "ip", "-6", "rule", "show", NULL);
    252 
    253     dump_route_tables();
    254 
    255     run_command("ARP CACHE", 10, "ip", "-4", "neigh", "show", NULL);
    256     run_command("IPv6 ND CACHE", 10, "ip", "-6", "neigh", "show", NULL);
    257 
    258     run_command("IPTABLES", 10, SU_PATH, "root", "iptables", "-L", "-nvx", NULL);
    259     run_command("IP6TABLES", 10, SU_PATH, "root", "ip6tables", "-L", "-nvx", NULL);
    260     run_command("IPTABLE NAT", 10, SU_PATH, "root", "iptables", "-t", "nat", "-L", "-nvx", NULL);
    261     /* no ip6 nat */
    262     run_command("IPTABLE RAW", 10, SU_PATH, "root", "iptables", "-t", "raw", "-L", "-nvx", NULL);
    263     run_command("IP6TABLE RAW", 10, SU_PATH, "root", "ip6tables", "-t", "raw", "-L", "-nvx", NULL);
    264 
    265     run_command("WIFI NETWORKS", 20,
    266             SU_PATH, "root", "wpa_cli", "IFNAME=wlan0", "list_networks", NULL);
    267 
    268 #ifdef FWDUMP_bcmdhd
    269     run_command("DUMP WIFI INTERNAL COUNTERS", 20,
    270             SU_PATH, "root", "wlutil", "counters", NULL);
    271 #endif
    272     dump_file("INTERRUPTS (1)", "/proc/interrupts");
    273 
    274     property_get("dhcp.wlan0.gateway", network, "");
    275     if (network[0])
    276         run_command("PING GATEWAY", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
    277     property_get("dhcp.wlan0.dns1", network, "");
    278     if (network[0])
    279         run_command("PING DNS1", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
    280     property_get("dhcp.wlan0.dns2", network, "");
    281     if (network[0])
    282         run_command("PING DNS2", 10, "ping", "-c", "3", "-i", ".5", network, NULL);
    283 #ifdef FWDUMP_bcmdhd
    284     run_command("DUMP WIFI STATUS", 20,
    285             SU_PATH, "root", "dhdutil", "-i", "wlan0", "dump", NULL);
    286     run_command("DUMP WIFI INTERNAL COUNTERS", 20,
    287             SU_PATH, "root", "wlutil", "counters", NULL);
    288 #endif
    289     dump_file("INTERRUPTS (2)", "/proc/interrupts");
    290 
    291     print_properties();
    292 
    293     run_command("VOLD DUMP", 10, "vdc", "dump", NULL);
    294     run_command("SECURE CONTAINERS", 10, "vdc", "asec", "list", NULL);
    295 
    296     run_command("FILESYSTEMS & FREE SPACE", 10, "df", NULL);
    297 
    298     run_command("LAST RADIO LOG", 10, "parse_radio_log", "/proc/last_radio_log", NULL);
    299 
    300     printf("------ BACKLIGHTS ------\n");
    301     printf("LCD brightness=");
    302     dump_file(NULL, "/sys/class/leds/lcd-backlight/brightness");
    303     printf("Button brightness=");
    304     dump_file(NULL, "/sys/class/leds/button-backlight/brightness");
    305     printf("Keyboard brightness=");
    306     dump_file(NULL, "/sys/class/leds/keyboard-backlight/brightness");
    307     printf("ALS mode=");
    308     dump_file(NULL, "/sys/class/leds/lcd-backlight/als");
    309     printf("LCD driver registers:\n");
    310     dump_file(NULL, "/sys/class/leds/lcd-backlight/registers");
    311     printf("\n");
    312 
    313     /* Binder state is expensive to look at as it uses a lot of memory. */
    314     dump_file("BINDER FAILED TRANSACTION LOG", "/sys/kernel/debug/binder/failed_transaction_log");
    315     dump_file("BINDER TRANSACTION LOG", "/sys/kernel/debug/binder/transaction_log");
    316     dump_file("BINDER TRANSACTIONS", "/sys/kernel/debug/binder/transactions");
    317     dump_file("BINDER STATS", "/sys/kernel/debug/binder/stats");
    318     dump_file("BINDER STATE", "/sys/kernel/debug/binder/state");
    319 
    320     printf("========================================================\n");
    321     printf("== Board\n");
    322     printf("========================================================\n");
    323 
    324     dumpstate_board();
    325     printf("\n");
    326 
    327     /* Migrate the ril_dumpstate to a dumpstate_board()? */
    328     char ril_dumpstate_timeout[PROPERTY_VALUE_MAX] = {0};
    329     property_get("ril.dumpstate.timeout", ril_dumpstate_timeout, "30");
    330     if (strnlen(ril_dumpstate_timeout, PROPERTY_VALUE_MAX - 1) > 0) {
    331         if (0 == strncmp(build_type, "user", PROPERTY_VALUE_MAX - 1)) {
    332             // su does not exist on user builds, so try running without it.
    333             // This way any implementations of vril-dump that do not require
    334             // root can run on user builds.
    335             run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
    336                     "vril-dump", NULL);
    337         } else {
    338             run_command("DUMP VENDOR RIL LOGS", atoi(ril_dumpstate_timeout),
    339                     SU_PATH, "root", "vril-dump", NULL);
    340         }
    341     }
    342 
    343     printf("========================================================\n");
    344     printf("== Android Framework Services\n");
    345     printf("========================================================\n");
    346 
    347     /* the full dumpsys is starting to take a long time, so we need
    348        to increase its timeout.  we really need to do the timeouts in
    349        dumpsys itself... */
    350     run_command("DUMPSYS", 60, "dumpsys", NULL);
    351 
    352     printf("========================================================\n");
    353     printf("== Checkins\n");
    354     printf("========================================================\n");
    355 
    356     run_command("CHECKIN BATTERYSTATS", 30, "dumpsys", "batterystats", "-c", NULL);
    357     run_command("CHECKIN MEMINFO", 30, "dumpsys", "meminfo", "--checkin", NULL);
    358     run_command("CHECKIN NETSTATS", 30, "dumpsys", "netstats", "--checkin", NULL);
    359     run_command("CHECKIN PROCSTATS", 30, "dumpsys", "procstats", "-c", NULL);
    360     run_command("CHECKIN USAGESTATS", 30, "dumpsys", "usagestats", "-c", NULL);
    361 
    362     printf("========================================================\n");
    363     printf("== Running Application Activities\n");
    364     printf("========================================================\n");
    365 
    366     run_command("APP ACTIVITIES", 30, "dumpsys", "activity", "all", NULL);
    367 
    368     printf("========================================================\n");
    369     printf("== Running Application Services\n");
    370     printf("========================================================\n");
    371 
    372     run_command("APP SERVICES", 30, "dumpsys", "activity", "service", "all", NULL);
    373 
    374     printf("========================================================\n");
    375     printf("== Running Application Providers\n");
    376     printf("========================================================\n");
    377 
    378     run_command("APP SERVICES", 30, "dumpsys", "activity", "provider", "all", NULL);
    379 
    380 
    381     printf("========================================================\n");
    382     printf("== dumpstate: done\n");
    383     printf("========================================================\n");
    384 }
    385 
    386 static void usage() {
    387     fprintf(stderr, "usage: dumpstate [-b soundfile] [-e soundfile] [-o file [-d] [-p] [-z]] [-s] [-q]\n"
    388             "  -o: write to file (instead of stdout)\n"
    389             "  -d: append date to filename (requires -o)\n"
    390             "  -z: gzip output (requires -o)\n"
    391             "  -p: capture screenshot to filename.png (requires -o)\n"
    392             "  -s: write output to control socket (for init)\n"
    393             "  -b: play sound file instead of vibrate, at beginning of job\n"
    394             "  -e: play sound file instead of vibrate, at end of job\n"
    395             "  -q: disable vibrate\n"
    396             "  -B: send broadcast when finished (requires -o and -p)\n"
    397                 );
    398 }
    399 
    400 static void sigpipe_handler(int n) {
    401     // don't complain to stderr or stdout
    402     _exit(EXIT_FAILURE);
    403 }
    404 
    405 static void vibrate(FILE* vibrator, int ms) {
    406     fprintf(vibrator, "%d\n", ms);
    407     fflush(vibrator);
    408 }
    409 
    410 int main(int argc, char *argv[]) {
    411     struct sigaction sigact;
    412     int do_add_date = 0;
    413     int do_compress = 0;
    414     int do_vibrate = 1;
    415     char* use_outfile = 0;
    416     int use_socket = 0;
    417     int do_fb = 0;
    418     int do_broadcast = 0;
    419 
    420     if (getuid() != 0) {
    421         // Old versions of the adb client would call the
    422         // dumpstate command directly. Newer clients
    423         // call /system/bin/bugreport instead. If we detect
    424         // we're being called incorrectly, then exec the
    425         // correct program.
    426         return execl("/system/bin/bugreport", "/system/bin/bugreport", NULL);
    427     }
    428 
    429     ALOGI("begin\n");
    430 
    431     /* clear SIGPIPE handler */
    432     memset(&sigact, 0, sizeof(sigact));
    433     sigact.sa_handler = sigpipe_handler;
    434     sigaction(SIGPIPE, &sigact, NULL);
    435 
    436     /* set as high priority, and protect from OOM killer */
    437     setpriority(PRIO_PROCESS, 0, -20);
    438     FILE *oom_adj = fopen("/proc/self/oom_adj", "w");
    439     if (oom_adj) {
    440         fputs("-17", oom_adj);
    441         fclose(oom_adj);
    442     }
    443 
    444     /* parse arguments */
    445     int c;
    446     while ((c = getopt(argc, argv, "dho:svqzpB")) != -1) {
    447         switch (c) {
    448             case 'd': do_add_date = 1;       break;
    449             case 'o': use_outfile = optarg;  break;
    450             case 's': use_socket = 1;        break;
    451             case 'v': break;  // compatibility no-op
    452             case 'q': do_vibrate = 0;        break;
    453             case 'z': do_compress = 6;       break;
    454             case 'p': do_fb = 1;             break;
    455             case 'B': do_broadcast = 1;      break;
    456             case '?': printf("\n");
    457             case 'h':
    458                 usage();
    459                 exit(1);
    460         }
    461     }
    462 
    463     // If we are going to use a socket, do it as early as possible
    464     // to avoid timeouts from bugreport.
    465     if (use_socket) {
    466         redirect_to_socket(stdout, "dumpstate");
    467     }
    468 
    469     /* open the vibrator before dropping root */
    470     FILE *vibrator = 0;
    471     if (do_vibrate) {
    472         vibrator = fopen("/sys/class/timed_output/vibrator/enable", "w");
    473         if (vibrator) {
    474             fcntl(fileno(vibrator), F_SETFD, FD_CLOEXEC);
    475             vibrate(vibrator, 150);
    476         }
    477     }
    478 
    479     /* read /proc/cmdline before dropping root */
    480     FILE *cmdline = fopen("/proc/cmdline", "r");
    481     if (cmdline != NULL) {
    482         fgets(cmdline_buf, sizeof(cmdline_buf), cmdline);
    483         fclose(cmdline);
    484     }
    485 
    486     /* collect stack traces from Dalvik and native processes (needs root) */
    487     dump_traces_path = dump_traces();
    488 
    489     /* Get the tombstone fds here while we are running as root. */
    490     get_tombstone_fds(tombstone_data);
    491 
    492     /* ensure we will keep capabilities when we drop root */
    493     if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
    494         ALOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
    495         return -1;
    496     }
    497 
    498     /* switch to non-root user and group */
    499     gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
    500             AID_MOUNT, AID_INET, AID_NET_BW_STATS };
    501     if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
    502         ALOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
    503         return -1;
    504     }
    505     if (setgid(AID_SHELL) != 0) {
    506         ALOGE("Unable to setgid, aborting: %s\n", strerror(errno));
    507         return -1;
    508     }
    509     if (setuid(AID_SHELL) != 0) {
    510         ALOGE("Unable to setuid, aborting: %s\n", strerror(errno));
    511         return -1;
    512     }
    513 
    514     struct __user_cap_header_struct capheader;
    515     struct __user_cap_data_struct capdata[2];
    516     memset(&capheader, 0, sizeof(capheader));
    517     memset(&capdata, 0, sizeof(capdata));
    518     capheader.version = _LINUX_CAPABILITY_VERSION_3;
    519     capheader.pid = 0;
    520 
    521     capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
    522     capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
    523     capdata[0].inheritable = 0;
    524     capdata[1].inheritable = 0;
    525 
    526     if (capset(&capheader, &capdata[0]) < 0) {
    527         ALOGE("capset failed: %s\n", strerror(errno));
    528         return -1;
    529     }
    530 
    531     /* redirect output if needed */
    532     char path[PATH_MAX], tmp_path[PATH_MAX];
    533     pid_t gzip_pid = -1;
    534 
    535     if (!use_socket && use_outfile) {
    536         strlcpy(path, use_outfile, sizeof(path));
    537         if (do_add_date) {
    538             char date[80];
    539             time_t now = time(NULL);
    540             strftime(date, sizeof(date), "-%Y-%m-%d-%H-%M-%S", localtime(&now));
    541             strlcat(path, date, sizeof(path));
    542         }
    543         if (do_fb) {
    544             strlcpy(screenshot_path, path, sizeof(screenshot_path));
    545             strlcat(screenshot_path, ".png", sizeof(screenshot_path));
    546         }
    547         strlcat(path, ".txt", sizeof(path));
    548         if (do_compress) strlcat(path, ".gz", sizeof(path));
    549         strlcpy(tmp_path, path, sizeof(tmp_path));
    550         strlcat(tmp_path, ".tmp", sizeof(tmp_path));
    551         gzip_pid = redirect_to_file(stdout, tmp_path, do_compress);
    552     }
    553 
    554     dumpstate();
    555 
    556     /* done */
    557     if (vibrator) {
    558         for (int i = 0; i < 3; i++) {
    559             vibrate(vibrator, 75);
    560             usleep((75 + 50) * 1000);
    561         }
    562         fclose(vibrator);
    563     }
    564 
    565     /* wait for gzip to finish, otherwise it might get killed when we exit */
    566     if (gzip_pid > 0) {
    567         fclose(stdout);
    568         waitpid(gzip_pid, NULL, 0);
    569     }
    570 
    571     /* rename the (now complete) .tmp file to its final location */
    572     if (use_outfile && rename(tmp_path, path)) {
    573         fprintf(stderr, "rename(%s, %s): %s\n", tmp_path, path, strerror(errno));
    574     }
    575 
    576     /* tell activity manager we're done */
    577     if (do_broadcast && use_outfile && do_fb) {
    578         run_command(NULL, 5, "/system/bin/am", "broadcast", "--user", "0",
    579                 "-a", "android.intent.action.BUGREPORT_FINISHED",
    580                 "--es", "android.intent.extra.BUGREPORT", path,
    581                 "--es", "android.intent.extra.SCREENSHOT", screenshot_path,
    582                 "--receiver-permission", "android.permission.DUMP", NULL);
    583     }
    584 
    585     ALOGI("done\n");
    586 
    587     return 0;
    588 }
    589