1f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross/*
2f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * Copyright (C) 2008 The Android Open Source Project
3f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross *
4f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * Licensed under the Apache License, Version 2.0 (the "License");
5f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * you may not use this file except in compliance with the License.
6f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * You may obtain a copy of the License at
7f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross *
8f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross *      http://www.apache.org/licenses/LICENSE-2.0
9f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross *
10f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * Unless required by applicable law or agreed to in writing, software
11f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * distributed under the License is distributed on an "AS IS" BASIS,
12f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * See the License for the specific language governing permissions and
14f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross * limitations under the License.
15f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross */
16f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
17f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <dirent.h>
18f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <errno.h>
19f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <fcntl.h>
20f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <limits.h>
21f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <poll.h>
22f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <signal.h>
23f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <stdarg.h>
24f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <stdio.h>
25f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <stdlib.h>
26f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <string.h>
27f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/inotify.h>
28f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/stat.h>
29f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/time.h>
30f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/wait.h>
31f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/klog.h>
32f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <time.h>
33f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <unistd.h>
34e7b6cf13f94f66ea446c95ab34040b0a577e43dcJohn Michelau#include <sys/prctl.h>
35f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
36bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown#include <cutils/debugger.h>
37f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <cutils/properties.h>
38f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <cutils/sockets.h>
39f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <private/android_filesystem_config.h>
40f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
41957983787d7a753d34b582ae0165160f4ebf815cRobert Craig#include <selinux/android.h>
42957983787d7a753d34b582ae0165160f4ebf815cRobert Craig
43f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include "dumpstate.h"
44f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
451dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brownstatic const int64_t NANOS_PER_SEC = 1000000000;
461dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown
47bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown/* list of native processes to include in the native dumps */
48bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brownstatic const char* native_processes_to_dump[] = {
491fc4f80fc38f782f128102795481924a97a2d3c5James Dong        "/system/bin/drmserver",
50bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        "/system/bin/mediaserver",
51bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        "/system/bin/sdcard",
52bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        "/system/bin/surfaceflinger",
53bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        NULL,
54bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown};
55bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown
5654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferrisstatic uint64_t nanotime() {
5754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    struct timespec ts;
5854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    clock_gettime(CLOCK_MONOTONIC, &ts);
5954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    return (uint64_t)ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec;
6054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris}
6154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris
625ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlockvoid for_each_userid(void (*func)(int), const char *header) {
635ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    DIR *d;
645ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    struct dirent *de;
655ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock
665ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    if (header) printf("\n------ %s ------\n", header);
675ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    func(0);
685ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock
695ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    if (!(d = opendir("/data/system/users"))) {
705ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        printf("Failed to open /data/system/users (%s)\n", strerror(errno));
715ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        return;
725ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    }
735ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock
745ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    while ((de = readdir(d))) {
755ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        int userid;
765ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) {
775ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock            continue;
785ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        }
795ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        func(userid);
805ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    }
815ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock
825ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    closedir(d);
835ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock}
845ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock
850c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossstatic void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
86f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    DIR *d;
87f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    struct dirent *de;
88f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
89f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (!(d = opendir("/proc"))) {
90f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("Failed to open /proc (%s)\n", strerror(errno));
91f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return;
92f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
93f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
94f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n------ %s ------\n", header);
95f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    while ((de = readdir(d))) {
96f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        int pid;
97f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        int fd;
98f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        char cmdpath[255];
99f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        char cmdline[255];
100f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
101f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        if (!(pid = atoi(de->d_name))) {
102f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            continue;
103f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
104f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
105f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        sprintf(cmdpath,"/proc/%d/cmdline", pid);
106f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        memset(cmdline, 0, sizeof(cmdline));
107cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich        if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) < 0) {
108f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            strcpy(cmdline, "N/A");
109f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        } else {
1100c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            read(fd, cmdline, sizeof(cmdline) - 1);
111f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            close(fd);
112f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
1130c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        helper(pid, cmdline, arg);
114f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
115f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
116f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    closedir(d);
117f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
118f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1190c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossstatic void for_each_pid_helper(int pid, const char *cmdline, void *arg) {
1200c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    for_each_pid_func *func = arg;
1210c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    func(pid, cmdline);
1220c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross}
1230c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1240c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossvoid for_each_pid(for_each_pid_func func, const char *header) {
1250c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    __for_each_pid(for_each_pid_helper, header, func);
1260c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross}
1270c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1280c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossstatic void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
1290c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    DIR *d;
1300c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    struct dirent *de;
1310c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    char taskpath[255];
1320c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    for_each_tid_func *func = arg;
1330c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1340c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    sprintf(taskpath, "/proc/%d/task", pid);
1350c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1360c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    if (!(d = opendir(taskpath))) {
1370c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
1380c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        return;
1390c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    }
1400c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1410c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    func(pid, pid, cmdline);
1420c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1430c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    while ((de = readdir(d))) {
1440c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        int tid;
1450c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        int fd;
1460c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        char commpath[255];
1470c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        char comm[255];
1480c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1490c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        if (!(tid = atoi(de->d_name))) {
1500c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            continue;
1510c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        }
1520c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1530c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        if (tid == pid)
1540c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            continue;
1550c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1560c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        sprintf(commpath,"/proc/%d/comm", tid);
1571493a3974a725d6873f317b774be333ca035a64cColin Cross        memset(comm, 0, sizeof(comm));
158cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich        if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) {
1590c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            strcpy(comm, "N/A");
1600c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        } else {
1610c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            char *c;
1620c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            read(fd, comm, sizeof(comm) - 1);
1630c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            close(fd);
1640c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1650c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            c = strrchr(comm, '\n');
1660c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            if (c) {
1670c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross                *c = '\0';
1680c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            }
1690c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        }
1700c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        func(pid, tid, comm);
1710c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    }
1720c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1730c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    closedir(d);
1740c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross}
1750c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1760c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossvoid for_each_tid(for_each_tid_func func, const char *header) {
1770c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    __for_each_pid(for_each_tid_helper, header, func);
1780c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross}
1790c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1800c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossvoid show_wchan(int pid, int tid, const char *name) {
181f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char path[255];
182f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char buffer[255];
183f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int fd;
1840c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    char name_buffer[255];
185f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
186f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    memset(buffer, 0, sizeof(buffer));
187f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1880c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    sprintf(path, "/proc/%d/wchan", tid);
189cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich    if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
190f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("Failed to open '%s' (%s)\n", path, strerror(errno));
191f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return;
192f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
193f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
194f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (read(fd, buffer, sizeof(buffer)) < 0) {
195f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("Failed to read '%s' (%s)\n", path, strerror(errno));
196f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        goto out_close;
197f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
198f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1990c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    snprintf(name_buffer, sizeof(name_buffer), "%*s%s",
2000c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross             pid == tid ? 0 : 3, "", name);
2010c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
2020c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    printf("%-7d %-32s %s\n", tid, name_buffer, buffer);
203f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
204f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossout_close:
205f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    close(fd);
206f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    return;
207f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
208f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
209f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossvoid do_dmesg() {
210f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("------ KERNEL LOG (dmesg) ------\n");
2115f87b31d8076dff08b2363961b07fc052364fe95Elliott Hughes    /* Get size of kernel buffer */
2125f87b31d8076dff08b2363961b07fc052364fe95Elliott Hughes    int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
213f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (size <= 0) {
214f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("Unexpected klogctl return value: %d\n\n", size);
215f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return;
216f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
217f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char *buf = (char *) malloc(size + 1);
218f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (buf == NULL) {
219f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("memory allocation failed\n\n");
220f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return;
221f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
222f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int retval = klogctl(KLOG_READ_ALL, buf, size);
223f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (retval < 0) {
224f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("klogctl failure\n\n");
225f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        free(buf);
226f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return;
227f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
228f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    buf[retval] = '\0';
229f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("%s\n\n", buf);
230f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    free(buf);
231f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    return;
232f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
233f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
234f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossvoid do_showmap(int pid, const char *name) {
235f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char title[255];
236f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char arg[255];
237f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
238f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    sprintf(title, "SHOW MAP %d (%s)", pid, name);
239f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    sprintf(arg, "%d", pid);
240f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    run_command(title, 10, SU_PATH, "root", "showmap", arg, NULL);
241f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
242f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
24354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferrisstatic int _dump_file_from_fd(const char *title, const char *path, int fd) {
244f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (title) printf("------ %s (%s", title, path);
245f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
246f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (title) {
247f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        struct stat st;
248f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        if (memcmp(path, "/proc/", 6) && memcmp(path, "/sys/", 5) && !fstat(fd, &st)) {
249f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            char stamp[80];
250f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            time_t mtime = st.st_mtime;
251f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
252f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            printf(": %s", stamp);
253f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
254f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf(") ------\n");
255f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
256f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
25754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    bool newline = false;
25854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    fd_set read_set;
25954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    struct timeval tm;
26054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    while (1) {
26154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        FD_ZERO(&read_set);
26254bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        FD_SET(fd, &read_set);
26354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        /* Timeout if no data is read for 30 seconds. */
26454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        tm.tv_sec = 30;
26554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        tm.tv_usec = 0;
26654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        uint64_t elapsed = nanotime();
26754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm));
26854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        if (ret == -1) {
26954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            printf("*** %s: select failed: %s\n", path, strerror(errno));
27054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            newline = true;
27154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            break;
27254bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        } else if (ret == 0) {
27354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            elapsed = nanotime() - elapsed;
27454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            printf("*** %s: Timed out after %.3fs\n", path,
27554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                   (float) elapsed / NANOS_PER_SEC);
27654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            newline = true;
27754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            break;
27854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        } else {
27954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            char buffer[65536];
28054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
28154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            if (bytes_read > 0) {
28254bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                fwrite(buffer, bytes_read, 1, stdout);
28354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                newline = (buffer[bytes_read-1] == '\n');
28454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            } else {
28554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                if (bytes_read == -1) {
28654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                    printf("*** %s: Failed to read from fd: %s", path, strerror(errno));
28754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                    newline = true;
28854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                }
28954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                break;
29054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            }
291f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
292f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
293bfb1f1cc832840b7232c25a91d49eb39aa79687fElliott Hughes    close(fd);
2947dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
295f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (!newline) printf("\n");
296f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (title) printf("\n");
297f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    return 0;
298f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
299f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
30054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris/* prints the contents of a file */
30154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferrisint dump_file(const char *title, const char *path) {
30254bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
30354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    if (fd < 0) {
30454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        int err = errno;
30554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        if (title) printf("------ %s (%s) ------\n", title, path);
30654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        printf("*** %s: %s\n", path, strerror(err));
30754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        if (title) printf("\n");
30854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        return -1;
30954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    }
31054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    return _dump_file_from_fd(title, path, fd);
31154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris}
31254bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris
313326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn/* calls skip to gate calling dump_from_fd recursively
314326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn * in the specified directory. dump_from_fd defaults to
315326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn * dump_file_from_fd above when set to NULL. skip defaults
316326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn * to false when set to NULL. dump_from_fd will always be
317326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn * called with title NULL.
318326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn */
319326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzynint dump_files(const char *title, const char *dir,
320326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        bool (*skip)(const char *path),
321326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        int (*dump_from_fd)(const char *title, const char *path, int fd)) {
322326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    DIR *dirp;
323326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    struct dirent *d;
324326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    char *newpath = NULL;
325326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    char *slash = "/";
326326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    int fd, retval = 0;
327326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
328326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (title) {
329326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        printf("------ %s (%s) ------\n", title, dir);
330326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
331326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
332326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (dir[strlen(dir) - 1] == '/') {
333326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        ++slash;
334326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
335326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    dirp = opendir(dir);
336326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (dirp == NULL) {
337326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        retval = -errno;
338326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        fprintf(stderr, "%s: %s\n", dir, strerror(errno));
339326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return retval;
340326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
341326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
342326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (!dump_from_fd) {
343326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        dump_from_fd = dump_file_from_fd;
344326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
345326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) {
346326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if ((d->d_name[0] == '.')
347326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn         && (((d->d_name[1] == '.') && (d->d_name[2] == '\0'))
348326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn          || (d->d_name[1] == '\0'))) {
349326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            continue;
350326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
351326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name,
352326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn                 (d->d_type == DT_DIR) ? "/" : "");
353326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if (!newpath) {
354326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            retval = -errno;
355326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            continue;
356326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
357326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if (skip && (*skip)(newpath)) {
358326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            continue;
359326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
360326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if (d->d_type == DT_DIR) {
361326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            int ret = dump_files(NULL, newpath, skip, dump_from_fd);
362326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            if (ret < 0) {
363326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn                retval = ret;
364326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            }
365326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            continue;
366326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
367326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
368326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if (fd < 0) {
369326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            retval = fd;
370326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            printf("*** %s: %s\n", newpath, strerror(errno));
371326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            continue;
372326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
373326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        (*dump_from_fd)(NULL, newpath, fd);
374326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
375326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    closedir(dirp);
376326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (title) {
377326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        printf("\n");
378326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
379326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    return retval;
380326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn}
381326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
38254bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris/* fd must have been opened with the flag O_NONBLOCK. With this flag set,
38354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris * it's possible to avoid issues where opening the file itself can get
38454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris * stuck.
38554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris */
38654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferrisint dump_file_from_fd(const char *title, const char *path, int fd) {
38754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    int flags = fcntl(fd, F_GETFL);
38854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    if (flags == -1) {
38954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno));
39054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        return -1;
39154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    } else if (!(flags & O_NONBLOCK)) {
39254bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        printf("*** %s: fd must have O_NONBLOCK set.\n", path);
39354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        return -1;
39454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    }
39554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    return _dump_file_from_fd(title, path, fd);
3961dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown}
3971dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown
3981a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferrisbool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) {
3991a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    sigset_t child_mask, old_mask;
4001a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    sigemptyset(&child_mask);
4011a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    sigaddset(&child_mask, SIGCHLD);
4021a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris
4031a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) {
4041a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        printf("*** sigprocmask failed: %s\n", strerror(errno));
4051a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        return false;
4061a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    }
4071a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris
4081a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    struct timespec ts;
4091a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    ts.tv_sec = timeout_seconds;
4101a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    ts.tv_nsec = 0;
4111a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts));
4121a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    int saved_errno = errno;
4131a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    // Set the signals back the way they were.
4141a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) {
4151a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        printf("*** sigprocmask failed: %s\n", strerror(errno));
4161a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        if (ret == 0) {
4171a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            return false;
4181a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        }
4191a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    }
4201a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (ret == -1) {
4211a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        errno = saved_errno;
4221a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        if (errno == EAGAIN) {
4231a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            errno = ETIMEDOUT;
4241a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        } else {
4251a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            printf("*** sigtimedwait failed: %s\n", strerror(errno));
4261a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        }
4271a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        return false;
4281a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    }
4291a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris
4301a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    pid_t child_pid = waitpid(pid, status, WNOHANG);
4311a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (child_pid != pid) {
4321a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        if (child_pid != -1) {
4331a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid);
4341a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        } else {
4351a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            printf("*** waitpid failed: %s\n", strerror(errno));
4361a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        }
4371a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        return false;
4381a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    }
4391a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    return true;
4401a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris}
4411a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris
442f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross/* forks a command and waits for it to finish */
443f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossint run_command(const char *title, int timeout_seconds, const char *command, ...) {
444f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    fflush(stdout);
44554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    uint64_t start = nanotime();
446f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    pid_t pid = fork();
447f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
448f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* handle error case */
449f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (pid < 0) {
450f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("*** fork: %s\n", strerror(errno));
451f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return pid;
452f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
453f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
454f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* handle child case */
455f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (pid == 0) {
456f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        const char *args[1024] = {command};
457f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        size_t arg;
458f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
459e7b6cf13f94f66ea446c95ab34040b0a577e43dcJohn Michelau        /* make sure the child dies when dumpstate dies */
460e7b6cf13f94f66ea446c95ab34040b0a577e43dcJohn Michelau        prctl(PR_SET_PDEATHSIG, SIGKILL);
461e7b6cf13f94f66ea446c95ab34040b0a577e43dcJohn Michelau
4622e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales        /* just ignore SIGPIPE, will go down with parent's */
4632e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales        struct sigaction sigact;
4642e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales        memset(&sigact, 0, sizeof(sigact));
4652e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales        sigact.sa_handler = SIG_IGN;
4662e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales        sigaction(SIGPIPE, &sigact, NULL);
4672e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales
468f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        va_list ap;
469f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        va_start(ap, command);
470f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        if (title) printf("------ %s (%s", title, command);
471f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
472f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            args[arg] = va_arg(ap, const char *);
473f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            if (args[arg] == NULL) break;
474f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            if (title) printf(" %s", args[arg]);
475f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
476f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        if (title) printf(") ------\n");
477f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fflush(stdout);
478f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
479f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        execvp(command, (char**) args);
480f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("*** exec(%s): %s\n", command, strerror(errno));
481f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fflush(stdout);
482f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        _exit(-1);
483f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
484f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
485f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* handle parent case */
4861a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    int status;
4871a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    bool ret = waitpid_with_timeout(pid, timeout_seconds, &status);
4881a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    uint64_t elapsed = nanotime() - start;
4891a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (!ret) {
4901a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        if (errno == ETIMEDOUT) {
4911a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            printf("*** %s: Timed out after %.3fs (killing pid %d)\n", command,
4921a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris                   (float) elapsed / NANOS_PER_SEC, pid);
4931a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        } else {
4941a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            printf("*** %s: Error after %.4fs (killing pid %d)\n", command,
4951a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris                   (float) elapsed / NANOS_PER_SEC, pid);
496f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
4971a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        kill(pid, SIGTERM);
4981a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        if (!waitpid_with_timeout(pid, 5, NULL)) {
4991a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            kill(pid, SIGKILL);
5001a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            if (!waitpid_with_timeout(pid, 5, NULL)) {
5011a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris                printf("*** %s: Cannot kill %d even with SIGKILL.\n", command, pid);
5021a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            }
503f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
5041a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        return -1;
5051a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    }
506f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
5071a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (WIFSIGNALED(status)) {
5081a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
5091a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
5101a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
511f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
5121a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (title) printf("[%s: %.3fs elapsed]\n\n", command, (float)elapsed / NANOS_PER_SEC);
5131a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris
5141a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    return status;
515f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
516f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
517f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crosssize_t num_props = 0;
518f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic char* props[2000];
519f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
520f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic void print_prop(const char *key, const char *name, void *user) {
521f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    (void) user;
522f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (num_props < sizeof(props) / sizeof(props[0])) {
523f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10];
524f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name);
525f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        props[num_props++] = strdup(buf);
526f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
527f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
528f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
529f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic int compare_prop(const void *a, const void *b) {
530f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    return strcmp(*(char * const *) a, *(char * const *) b);
531f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
532f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
533f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross/* prints all the system properties */
534f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossvoid print_properties() {
535f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    size_t i;
536f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    num_props = 0;
537f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_list(print_prop, NULL);
538f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    qsort(&props, num_props, sizeof(props[0]), compare_prop);
539f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
540f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("------ SYSTEM PROPERTIES ------\n");
541f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    for (i = 0; i < num_props; ++i) {
542f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fputs(props[i], stdout);
543f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        free(props[i]);
544f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
545f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
546f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
547f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
548f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross/* redirect output to a service control socket */
549f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossvoid redirect_to_socket(FILE *redirect, const char *service) {
550f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int s = android_get_control_socket(service);
551f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (s < 0) {
552f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fprintf(stderr, "android_get_control_socket(%s): %s\n", service, strerror(errno));
553f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        exit(1);
554f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
555cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich    fcntl(s, F_SETFD, FD_CLOEXEC);
556f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (listen(s, 4) < 0) {
557f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fprintf(stderr, "listen(control socket): %s\n", strerror(errno));
558f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        exit(1);
559f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
560f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
561f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    struct sockaddr addr;
562f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    socklen_t alen = sizeof(addr);
563f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int fd = accept(s, &addr, &alen);
564f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (fd < 0) {
565f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fprintf(stderr, "accept(control socket): %s\n", strerror(errno));
566f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        exit(1);
567f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
568f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
569f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    fflush(redirect);
570f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dup2(fd, fileno(redirect));
571f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    close(fd);
572f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
573f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
574ff4a4dc7e6e3ead12020b4dc17548d767a419ddeChristopher Ferris/* redirect output to a file */
575ff4a4dc7e6e3ead12020b4dc17548d767a419ddeChristopher Ferrisvoid redirect_to_file(FILE *redirect, char *path) {
576f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char *chp = path;
577f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
578f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* skip initial slash */
579f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (chp[0] == '/')
580f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        chp++;
581f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
582f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* create leading directories, if necessary */
583f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    while (chp && chp[0]) {
584f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        chp = strchr(chp, '/');
585f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        if (chp) {
586f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            *chp = 0;
58727f9e6d849fce956c9b8f1ad5c3d9a954501a76bJeff Sharkey            mkdir(path, 0770);  /* drwxrwx--- */
588f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            *chp++ = '/';
589f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
590f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
591f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
592cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich    int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,
593ff4a4dc7e6e3ead12020b4dc17548d767a419ddeChristopher Ferris                                     S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
594f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (fd < 0) {
595f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fprintf(stderr, "%s: %s\n", path, strerror(errno));
596f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        exit(1);
597f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
598f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
599ff4a4dc7e6e3ead12020b4dc17548d767a419ddeChristopher Ferris    TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
600f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    close(fd);
601f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
602f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
603bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brownstatic bool should_dump_native_traces(const char* path) {
604bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    for (const char** p = native_processes_to_dump; *p; p++) {
605bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        if (!strcmp(*p, path)) {
606bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            return true;
607bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        }
608bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    }
609bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    return false;
610bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown}
611bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown
612bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
613bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brownconst char *dump_traces() {
614bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    const char* result = NULL;
615bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown
616f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char traces_path[PROPERTY_VALUE_MAX] = "";
617f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_get("dalvik.vm.stack-trace-file", traces_path, "");
618f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (!traces_path[0]) return NULL;
619f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
620f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* move the old traces.txt (if any) out of the way temporarily */
621f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char anr_traces_path[PATH_MAX];
622f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
623f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
624f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
625f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fprintf(stderr, "rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
626f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return NULL;  // Can't rename old traces.txt -- no permission? -- leave it alone instead
627f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
628f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
629f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* make the directory if necessary */
630f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char anr_traces_dir[PATH_MAX];
631f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    strlcpy(anr_traces_dir, traces_path, sizeof(anr_traces_dir));
632f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char *slash = strrchr(anr_traces_dir, '/');
633f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (slash != NULL) {
634f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        *slash = '\0';
635f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        if (!mkdir(anr_traces_dir, 0775)) {
636f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            chown(anr_traces_dir, AID_SYSTEM, AID_SYSTEM);
637c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich            chmod(anr_traces_dir, 0775);
63826288202e7bdf2e897a11bf31a15685d7c20945fStephen Smalley            if (selinux_android_restorecon(anr_traces_dir, 0) == -1) {
639957983787d7a753d34b582ae0165160f4ebf815cRobert Craig                fprintf(stderr, "restorecon failed for %s: %s\n", anr_traces_dir, strerror(errno));
640957983787d7a753d34b582ae0165160f4ebf815cRobert Craig            }
641f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        } else if (errno != EEXIST) {
642f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            fprintf(stderr, "mkdir(%s): %s\n", anr_traces_dir, strerror(errno));
643f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            return NULL;
644f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
645f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
646f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
647f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* create a new, empty traces.txt file to receive stack dumps */
648cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich    int fd = TEMP_FAILURE_RETRY(open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC,
64954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                                     0666));  /* -rw-rw-rw- */
650f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (fd < 0) {
651f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fprintf(stderr, "%s: %s\n", traces_path, strerror(errno));
652f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return NULL;
653f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
654c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich    int chmod_ret = fchmod(fd, 0666);
655c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich    if (chmod_ret < 0) {
656c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich        fprintf(stderr, "fchmod on %s failed: %s\n", traces_path, strerror(errno));
657c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich        close(fd);
658c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich        return NULL;
659c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich    }
660f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
661f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* walk /proc and kill -QUIT all Dalvik processes */
662f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    DIR *proc = opendir("/proc");
663f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (proc == NULL) {
664f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fprintf(stderr, "/proc: %s\n", strerror(errno));
665bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        goto error_close_fd;
666f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
667f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
668f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* use inotify to find when processes are done dumping */
669f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int ifd = inotify_init();
670f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (ifd < 0) {
671f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fprintf(stderr, "inotify_init: %s\n", strerror(errno));
672bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        goto error_close_fd;
673f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
674f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
675f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
676f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (wfd < 0) {
677f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fprintf(stderr, "inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
678bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        goto error_close_ifd;
679f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
680f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
681f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    struct dirent *d;
682f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int dalvik_found = 0;
683f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    while ((d = readdir(proc))) {
684f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        int pid = atoi(d->d_name);
685f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        if (pid <= 0) continue;
686f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
687bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        char path[PATH_MAX];
688bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        char data[PATH_MAX];
689f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        snprintf(path, sizeof(path), "/proc/%d/exe", pid);
690bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        ssize_t len = readlink(path, data, sizeof(data) - 1);
691bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        if (len <= 0) {
692f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            continue;
693f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
694bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        data[len] = '\0';
695f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
6960d6180f122127a9541e6193e0e6eceb5b08c608cColin Cross        if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
697bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            /* skip zygote -- it won't dump its stack anyway */
698bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
699cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich            int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
7001dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown            len = read(cfd, data, sizeof(data) - 1);
7011dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown            close(cfd);
702bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            if (len <= 0) {
703bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown                continue;
704bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            }
705bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            data[len] = '\0';
7060d6180f122127a9541e6193e0e6eceb5b08c608cColin Cross            if (!strncmp(data, "zygote", strlen("zygote"))) {
707bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown                continue;
708bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            }
709bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown
710bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            ++dalvik_found;
71154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            uint64_t start = nanotime();
712bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            if (kill(pid, SIGQUIT)) {
713bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown                fprintf(stderr, "kill(%d, SIGQUIT): %s\n", pid, strerror(errno));
714bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown                continue;
715bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            }
716bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown
717bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            /* wait for the writable-close notification from inotify */
718bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            struct pollfd pfd = { ifd, POLLIN, 0 };
71985453ecc90a94564e586f0f97c9ec2c0cf831501Nick Vaccaro            int ret = poll(&pfd, 1, 5000);  /* 5 sec timeout */
720bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            if (ret < 0) {
721bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown                fprintf(stderr, "poll: %s\n", strerror(errno));
722bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            } else if (ret == 0) {
723bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown                fprintf(stderr, "warning: timed out dumping pid %d\n", pid);
724bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            } else {
725bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown                struct inotify_event ie;
726bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown                read(ifd, &ie, sizeof(ie));
727bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            }
7281dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown
7291dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown            if (lseek(fd, 0, SEEK_END) < 0) {
7301dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown                fprintf(stderr, "lseek: %s\n", strerror(errno));
7311dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown            } else {
73231ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                dprintf(fd, "[dump dalvik stack %d: %.3fs elapsed]\n",
7331dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown                        pid, (float)(nanotime() - start) / NANOS_PER_SEC);
7341dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown            }
735bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        } else if (should_dump_native_traces(data)) {
736bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            /* dump native process if appropriate */
737bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            if (lseek(fd, 0, SEEK_END) < 0) {
738bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown                fprintf(stderr, "lseek: %s\n", strerror(errno));
739bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            } else {
74031ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                static uint16_t timeout_failures = 0;
74154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                uint64_t start = nanotime();
74231ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris
74331ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                /* If 3 backtrace dumps fail in a row, consider debuggerd dead. */
74431ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                if (timeout_failures == 3) {
74531ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                    dprintf(fd, "too many stack dump failures, skipping...\n");
74631ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                } else if (dump_backtrace_to_file_timeout(pid, fd, 20) == -1) {
74731ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                    dprintf(fd, "dumping failed, likely due to a timeout\n");
74831ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                    timeout_failures++;
74931ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                } else {
75031ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                    timeout_failures = 0;
75131ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                }
75231ef85529d86a9db68b002f51ec84fd9938908abChristopher Ferris                dprintf(fd, "[dump native stack %d: %.3fs elapsed]\n",
7531dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown                        pid, (float)(nanotime() - start) / NANOS_PER_SEC);
754bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            }
755f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
756f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
757f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
758f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (dalvik_found == 0) {
759f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fprintf(stderr, "Warning: no Dalvik processes found to dump stacks\n");
760f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
761f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
762f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    static char dump_traces_path[PATH_MAX];
763f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    strlcpy(dump_traces_path, traces_path, sizeof(dump_traces_path));
764f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    strlcat(dump_traces_path, ".bugreport", sizeof(dump_traces_path));
765f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (rename(traces_path, dump_traces_path)) {
766f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fprintf(stderr, "rename(%s, %s): %s\n", traces_path, dump_traces_path, strerror(errno));
767bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        goto error_close_ifd;
768f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
769bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    result = dump_traces_path;
770f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
771f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* replace the saved [ANR] traces.txt file */
772f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    rename(anr_traces_path, traces_path);
773bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown
774bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brownerror_close_ifd:
775bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    close(ifd);
776bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brownerror_close_fd:
777bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    close(fd);
778bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    return result;
779f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
780f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
7812b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandranvoid dump_route_tables() {
7822b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    const char* const RT_TABLES_PATH = "/data/misc/net/rt_tables";
7832b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    dump_file("RT_TABLES", RT_TABLES_PATH);
784cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich    FILE* fp = fopen(RT_TABLES_PATH, "re");
7852b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    if (!fp) {
7862b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran        printf("*** %s: %s\n", RT_TABLES_PATH, strerror(errno));
7872b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran        return;
7882b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    }
7892b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    char table[16];
7902b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    // Each line has an integer (the table number), a space, and a string (the table name). We only
7912b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    // need the table number. It's a 32-bit unsigned number, so max 10 chars. Skip the table name.
7922b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    // Add a fixed max limit so this doesn't go awry.
7932b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    for (int i = 0; i < 64 && fscanf(fp, " %10s %*s", table) == 1; ++i) {
7942b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran        run_command("ROUTE TABLE IPv4", 10, "ip", "-4", "route", "show", "table", table, NULL);
7952b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran        run_command("ROUTE TABLE IPv6", 10, "ip", "-6", "route", "show", "table", table, NULL);
7962b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    }
7972b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran    fclose(fp);
7982b3bba34aec65b612be8d1f52cd124d9c30955f9Sreeram Ramachandran}
799