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