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>
2636b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme#include <string>
27f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <string.h>
28cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme#include <sys/capability.h>
29f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/inotify.h>
30f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/stat.h>
31a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn#include <sys/sysconf.h>
32f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/time.h>
33f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/wait.h>
34f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <sys/klog.h>
35f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <time.h>
36f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <unistd.h>
3736b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme#include <vector>
38e7b6cf13f94f66ea446c95ab34040b0a577e43dcJohn Michelau#include <sys/prctl.h>
39f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
4071bbfc57974331dce79242ce806d92035fce06baFelipe Leme#define LOG_TAG "dumpstate"
41bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown#include <cutils/debugger.h>
4271bbfc57974331dce79242ce806d92035fce06baFelipe Leme#include <cutils/log.h>
43f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <cutils/properties.h>
44f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <cutils/sockets.h>
45f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include <private/android_filesystem_config.h>
46f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
47957983787d7a753d34b582ae0165160f4ebf815cRobert Craig#include <selinux/android.h>
48957983787d7a753d34b582ae0165160f4ebf815cRobert Craig
49f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross#include "dumpstate.h"
50f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
511dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brownstatic const int64_t NANOS_PER_SEC = 1000000000;
521dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown
53bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown/* list of native processes to include in the native dumps */
545c04e7465eda8a558244e772da1e7d2a0d5f94e2Andy Hung// This matches the /proc/pid/exe link instead of /proc/pid/cmdline.
55bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brownstatic const char* native_processes_to_dump[] = {
569609bbd79668dbc3c8c16c773c92637de98bfa70Andy Hung        "/system/bin/audioserver",
57f5248da50134b087cbe8b58d2e1d2310537911e3Chien-Yu Chen        "/system/bin/cameraserver",
581fc4f80fc38f782f128102795481924a97a2d3c5James Dong        "/system/bin/drmserver",
595c04e7465eda8a558244e772da1e7d2a0d5f94e2Andy Hung        "/system/bin/mediacodec",     // media.codec
605c04e7465eda8a558244e772da1e7d2a0d5f94e2Andy Hung        "/system/bin/mediadrmserver",
615c04e7465eda8a558244e772da1e7d2a0d5f94e2Andy Hung        "/system/bin/mediaextractor", // media.extractor
62bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        "/system/bin/mediaserver",
63bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        "/system/bin/sdcard",
64bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        "/system/bin/surfaceflinger",
65d907b32052aca02ad679e6ae786848d328723080keunyoung        "/system/bin/vehicle_network_service",
66bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        NULL,
67bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown};
68bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown
69608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe LemeDurationReporter::DurationReporter(const char *title) : DurationReporter(title, stdout) {}
70608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme
71608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe LemeDurationReporter::DurationReporter(const char *title, FILE *out) {
7278f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    title_ = title;
7378f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    if (title) {
7478f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme        started_ = DurationReporter::nanotime();
7578f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    }
76608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    out_ = out;
7778f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme}
7878f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme
7978f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe LemeDurationReporter::~DurationReporter() {
8078f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    if (title_) {
8178f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme        uint64_t elapsed = DurationReporter::nanotime() - started_;
8278f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme        // Use "Yoda grammar" to make it easier to grep|sort sections.
83608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme        if (out_) {
84608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme            fprintf(out_, "------ %.3fs was the duration of '%s' ------\n",
85608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme                   (float) elapsed / NANOS_PER_SEC, title_);
86608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme        } else {
87cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme            MYLOGD("Duration of '%s': %.3fs\n", title_, (float) elapsed / NANOS_PER_SEC);
88608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme        }
8978f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    }
9078f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme}
9178f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme
9278f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Lemeuint64_t DurationReporter::DurationReporter::nanotime() {
9354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    struct timespec ts;
9454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    clock_gettime(CLOCK_MONOTONIC, &ts);
9578f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    return (uint64_t) ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec;
9654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris}
9754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris
985ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlockvoid for_each_userid(void (*func)(int), const char *header) {
9993d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    ON_DRY_RUN_RETURN();
1005ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    DIR *d;
1015ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    struct dirent *de;
1025ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock
1035ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    if (header) printf("\n------ %s ------\n", header);
1045ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    func(0);
1055ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock
1065ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    if (!(d = opendir("/data/system/users"))) {
1075ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        printf("Failed to open /data/system/users (%s)\n", strerror(errno));
1085ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        return;
1095ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    }
1105ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock
1115ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    while ((de = readdir(d))) {
1125ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        int userid;
1135ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        if (de->d_type != DT_DIR || !(userid = atoi(de->d_name))) {
1145ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock            continue;
1155ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        }
1165ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock        func(userid);
1175ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    }
1185ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock
1195ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock    closedir(d);
1205ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock}
1215ecd4beef8f10f91febf916414c65d3d0e1cf734John Spurlock
1220c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossstatic void __for_each_pid(void (*helper)(int, const char *, void *), const char *header, void *arg) {
123f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    DIR *d;
124f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    struct dirent *de;
125f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
126f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (!(d = opendir("/proc"))) {
127f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("Failed to open /proc (%s)\n", strerror(errno));
128f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return;
129f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
130f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
131635ca31754ae734b0c540ac5600d58ae55cd4237Felipe Leme    if (header) printf("\n------ %s ------\n", header);
132f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    while ((de = readdir(d))) {
133f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        int pid;
134f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        int fd;
135f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        char cmdpath[255];
136f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        char cmdline[255];
137f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
138f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        if (!(pid = atoi(de->d_name))) {
139f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            continue;
140f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
141f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
142f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        memset(cmdline, 0, sizeof(cmdline));
143a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
144a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/cmdline", pid);
145a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) {
146a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn            TEMP_FAILURE_RETRY(read(fd, cmdline, sizeof(cmdline) - 2));
147a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn            close(fd);
148a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn            if (cmdline[0]) {
149a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn                helper(pid, cmdline, arg);
150a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn                continue;
151a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn            }
152a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        }
153a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
154a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        // if no cmdline, a kernel thread has comm
155a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        snprintf(cmdpath, sizeof(cmdpath), "/proc/%d/comm", pid);
156a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        if ((fd = TEMP_FAILURE_RETRY(open(cmdpath, O_RDONLY | O_CLOEXEC))) >= 0) {
157a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn            TEMP_FAILURE_RETRY(read(fd, cmdline + 1, sizeof(cmdline) - 4));
158f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            close(fd);
159a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn            if (cmdline[1]) {
160a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn                cmdline[0] = '[';
161a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn                size_t len = strcspn(cmdline, "\f\b\r\n");
162a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn                cmdline[len] = ']';
163a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn                cmdline[len+1] = '\0';
164a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn            }
165a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        }
166a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        if (!cmdline[0]) {
167a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn            strcpy(cmdline, "N/A");
168f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
1690c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        helper(pid, cmdline, arg);
170f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
171f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
172f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    closedir(d);
173f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
174f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1750c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossstatic void for_each_pid_helper(int pid, const char *cmdline, void *arg) {
1768620bb4118a68721d10c29529dc6978847d08d00Felipe Leme    for_each_pid_func *func = (for_each_pid_func*) arg;
1770c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    func(pid, cmdline);
1780c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross}
1790c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1800c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossvoid for_each_pid(for_each_pid_func func, const char *header) {
18193d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    ON_DRY_RUN_RETURN();
1828620bb4118a68721d10c29529dc6978847d08d00Felipe Leme  __for_each_pid(for_each_pid_helper, header, (void *)func);
1830c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross}
1840c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1850c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossstatic void for_each_tid_helper(int pid, const char *cmdline, void *arg) {
1860c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    DIR *d;
1870c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    struct dirent *de;
1880c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    char taskpath[255];
1898620bb4118a68721d10c29529dc6978847d08d00Felipe Leme    for_each_tid_func *func = (for_each_tid_func *) arg;
1900c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
191f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich    snprintf(taskpath, sizeof(taskpath), "/proc/%d/task", pid);
1920c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1930c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    if (!(d = opendir(taskpath))) {
1940c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        printf("Failed to open %s (%s)\n", taskpath, strerror(errno));
1950c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        return;
1960c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    }
1970c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
1980c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    func(pid, pid, cmdline);
1990c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
2000c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    while ((de = readdir(d))) {
2010c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        int tid;
2020c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        int fd;
2030c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        char commpath[255];
2040c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        char comm[255];
2050c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
2060c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        if (!(tid = atoi(de->d_name))) {
2070c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            continue;
2080c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        }
2090c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
2100c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        if (tid == pid)
2110c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            continue;
2120c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
213f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich        snprintf(commpath, sizeof(commpath), "/proc/%d/comm", tid);
2141493a3974a725d6873f317b774be333ca035a64cColin Cross        memset(comm, 0, sizeof(comm));
215cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich        if ((fd = TEMP_FAILURE_RETRY(open(commpath, O_RDONLY | O_CLOEXEC))) < 0) {
2160c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            strcpy(comm, "N/A");
2170c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        } else {
2180c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            char *c;
219a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn            TEMP_FAILURE_RETRY(read(fd, comm, sizeof(comm) - 2));
2200c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            close(fd);
2210c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
2220c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            c = strrchr(comm, '\n');
2230c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            if (c) {
2240c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross                *c = '\0';
2250c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross            }
2260c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        }
2270c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross        func(pid, tid, comm);
2280c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    }
2290c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
2300c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    closedir(d);
2310c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross}
2320c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
2330c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossvoid for_each_tid(for_each_tid_func func, const char *header) {
23493d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    ON_DRY_RUN_RETURN();
2358620bb4118a68721d10c29529dc6978847d08d00Felipe Leme    __for_each_pid(for_each_tid_helper, header, (void *) func);
2360c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross}
2370c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
2380c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Crossvoid show_wchan(int pid, int tid, const char *name) {
23993d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    ON_DRY_RUN_RETURN();
240f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char path[255];
241f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char buffer[255];
242a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    int fd, ret, save_errno;
2430c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    char name_buffer[255];
244f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
245f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    memset(buffer, 0, sizeof(buffer));
246f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
247f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich    snprintf(path, sizeof(path), "/proc/%d/wchan", tid);
248cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich    if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
249f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("Failed to open '%s' (%s)\n", path, strerror(errno));
250f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return;
251f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
252f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
253a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
254a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    save_errno = errno;
255a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    close(fd);
256a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
257a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    if (ret < 0) {
258a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        printf("Failed to read '%s' (%s)\n", path, strerror(save_errno));
259a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        return;
260f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
261f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
2620c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    snprintf(name_buffer, sizeof(name_buffer), "%*s%s",
2630c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross             pid == tid ? 0 : 3, "", name);
2640c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross
2650c22e8b31651caf12a2b4d4acef5bc65d486e570Colin Cross    printf("%-7d %-32s %s\n", tid, name_buffer, buffer);
266f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
267a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    return;
268a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn}
269a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
270a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn// print time in centiseconds
271a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzynstatic void snprcent(char *buffer, size_t len, size_t spc,
272a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn                     unsigned long long time) {
273a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    static long hz; // cache discovered hz
274a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
275a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    if (hz <= 0) {
276a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        hz = sysconf(_SC_CLK_TCK);
277a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        if (hz <= 0) {
278a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn            hz = 1000;
279a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        }
280a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    }
281a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
282a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    // convert to centiseconds
283a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    time = (time * 100 + (hz / 2)) / hz;
284a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
285a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    char str[16];
286a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
287a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    snprintf(str, sizeof(str), " %llu.%02u",
288a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn             time / 100, (unsigned)(time % 100));
289a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    size_t offset = strlen(buffer);
290a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    snprintf(buffer + offset, (len > offset) ? len - offset : 0,
291a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn             "%*s", (spc > offset) ? (int)(spc - offset) : 0, str);
292a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn}
293a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
294a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn// print permille as a percent
295a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzynstatic void snprdec(char *buffer, size_t len, size_t spc, unsigned permille) {
296a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    char str[16];
297a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
298a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    snprintf(str, sizeof(str), " %u.%u%%", permille / 10, permille % 10);
299a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    size_t offset = strlen(buffer);
300a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    snprintf(buffer + offset, (len > offset) ? len - offset : 0,
301a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn             "%*s", (spc > offset) ? (int)(spc - offset) : 0, str);
302a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn}
303a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
304a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzynvoid show_showtime(int pid, const char *name) {
305a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    ON_DRY_RUN_RETURN();
306a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    char path[255];
307a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    char buffer[1023];
308a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    int fd, ret, save_errno;
309a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
310a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    memset(buffer, 0, sizeof(buffer));
311a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
312f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich    snprintf(path, sizeof(path), "/proc/%d/stat", pid);
313a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    if ((fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC))) < 0) {
314a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        printf("Failed to open '%s' (%s)\n", path, strerror(errno));
315a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        return;
316a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    }
317a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
318a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    ret = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
319a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    save_errno = errno;
320f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    close(fd);
321a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
322a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    if (ret < 0) {
323a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        printf("Failed to read '%s' (%s)\n", path, strerror(save_errno));
324a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        return;
325a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    }
326a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
327a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    // field 14 is utime
328a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    // field 15 is stime
329a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    // field 42 is iotime
330a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    unsigned long long utime = 0, stime = 0, iotime = 0;
331a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    if (sscanf(buffer,
33260292e5c8324475c5afbd0a6703b6b3ae2c94b1aXia Yang               "%*u %*s %*s %*d %*d %*d %*d %*d %*d %*d %*d "
33360292e5c8324475c5afbd0a6703b6b3ae2c94b1aXia Yang               "%*d %*d %llu %llu %*d %*d %*d %*d %*d %*d "
33460292e5c8324475c5afbd0a6703b6b3ae2c94b1aXia Yang               "%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d "
33560292e5c8324475c5afbd0a6703b6b3ae2c94b1aXia Yang               "%*d %*d %*d %*d %*d %*d %*d %*d %*d %llu ",
336a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn               &utime, &stime, &iotime) != 3) {
337a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        return;
338a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    }
339a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
340a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    unsigned long long total = utime + stime;
341a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    if (!total) {
342a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        return;
343a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    }
344a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
345a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    unsigned permille = (iotime * 1000 + (total / 2)) / total;
346a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    if (permille > 1000) {
347a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        permille = 1000;
348a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    }
349a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
350a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    // try to beautify and stabilize columns at <80 characters
351a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    snprintf(buffer, sizeof(buffer), "%-6d%s", pid, name);
352a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    if ((name[0] != '[') || utime) {
353a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        snprcent(buffer, sizeof(buffer), 57, utime);
354a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    }
355a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    snprcent(buffer, sizeof(buffer), 65, stime);
356a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    if ((name[0] != '[') || iotime) {
357a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        snprcent(buffer, sizeof(buffer), 73, iotime);
358a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    }
359a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    if (iotime) {
360a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn        snprdec(buffer, sizeof(buffer), 79, permille);
361a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    }
362a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn    puts(buffer); // adds a trailing newline
363a297c3258f6f7ea52cc2dcc42d62f85fda12a163Mark Salyzyn
364f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    return;
365f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
366f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
367f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossvoid do_dmesg() {
36878f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    const char *title = "KERNEL LOG (dmesg)";
36978f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    DurationReporter duration_reporter(title);
37078f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    printf("------ %s ------\n", title);
37178f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme
37293d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    ON_DRY_RUN_RETURN();
3735f87b31d8076dff08b2363961b07fc052364fe95Elliott Hughes    /* Get size of kernel buffer */
3745f87b31d8076dff08b2363961b07fc052364fe95Elliott Hughes    int size = klogctl(KLOG_SIZE_BUFFER, NULL, 0);
375f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (size <= 0) {
376f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("Unexpected klogctl return value: %d\n\n", size);
377f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return;
378f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
379f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char *buf = (char *) malloc(size + 1);
380f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (buf == NULL) {
381f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("memory allocation failed\n\n");
382f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return;
383f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
384f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int retval = klogctl(KLOG_READ_ALL, buf, size);
385f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (retval < 0) {
386f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf("klogctl failure\n\n");
387f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        free(buf);
388f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return;
389f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
390f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    buf[retval] = '\0';
391f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("%s\n\n", buf);
392f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    free(buf);
393f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    return;
394f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
395f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
396f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossvoid do_showmap(int pid, const char *name) {
397f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char title[255];
398f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char arg[255];
399f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
400f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich    snprintf(title, sizeof(title), "SHOW MAP %d (%s)", pid, name);
401f0922cc1786c0c2fbf44c10b0005243ecbb4227dNick Kralevich    snprintf(arg, sizeof(arg), "%d", pid);
4023dba69aad214a873c619d125ff0441b817947100Felipe Leme    run_command(title, 10, SU_PATH, "root", "showmap", "-q", arg, NULL);
403f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
404f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
40554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferrisstatic int _dump_file_from_fd(const char *title, const char *path, int fd) {
406f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (title) {
407ed24d2ae2cd6e8b0afb86efa555f18bcfaf74a64Christopher Ferris        printf("------ %s (%s", title, path);
408ed24d2ae2cd6e8b0afb86efa555f18bcfaf74a64Christopher Ferris
409f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        struct stat st;
410ed24d2ae2cd6e8b0afb86efa555f18bcfaf74a64Christopher Ferris        // Only show the modification time of non-device files.
411ed24d2ae2cd6e8b0afb86efa555f18bcfaf74a64Christopher Ferris        size_t path_len = strlen(path);
412ed24d2ae2cd6e8b0afb86efa555f18bcfaf74a64Christopher Ferris        if ((path_len < 6 || memcmp(path, "/proc/", 6)) &&
413ed24d2ae2cd6e8b0afb86efa555f18bcfaf74a64Christopher Ferris                (path_len < 5 || memcmp(path, "/sys/", 5)) &&
414ed24d2ae2cd6e8b0afb86efa555f18bcfaf74a64Christopher Ferris                (path_len < 3 || memcmp(path, "/d/", 3)) &&
415ed24d2ae2cd6e8b0afb86efa555f18bcfaf74a64Christopher Ferris                !fstat(fd, &st)) {
416f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            char stamp[80];
417f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            time_t mtime = st.st_mtime;
418f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
419f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            printf(": %s", stamp);
420f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
421f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        printf(") ------\n");
422f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
42371bbfc57974331dce79242ce806d92035fce06baFelipe Leme    ON_DRY_RUN({ update_progress(WEIGHT_FILE); close(fd); return 0; });
424f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
42554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    bool newline = false;
42654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    fd_set read_set;
42754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    struct timeval tm;
42854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    while (1) {
42954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        FD_ZERO(&read_set);
43054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        FD_SET(fd, &read_set);
43154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        /* Timeout if no data is read for 30 seconds. */
43254bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        tm.tv_sec = 30;
43354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        tm.tv_usec = 0;
43478f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme        uint64_t elapsed = DurationReporter::nanotime();
43554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, NULL, NULL, &tm));
43654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        if (ret == -1) {
43754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            printf("*** %s: select failed: %s\n", path, strerror(errno));
43854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            newline = true;
43954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            break;
44054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        } else if (ret == 0) {
44178f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme            elapsed = DurationReporter::nanotime() - elapsed;
44254bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            printf("*** %s: Timed out after %.3fs\n", path,
44354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                   (float) elapsed / NANOS_PER_SEC);
44454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            newline = true;
44554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            break;
44654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        } else {
44754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            char buffer[65536];
44854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
44954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            if (bytes_read > 0) {
45054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                fwrite(buffer, bytes_read, 1, stdout);
45154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                newline = (buffer[bytes_read-1] == '\n');
45254bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            } else {
45354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                if (bytes_read == -1) {
45454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                    printf("*** %s: Failed to read from fd: %s", path, strerror(errno));
45554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                    newline = true;
45654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                }
45754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                break;
45854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris            }
459f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
460f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
46171bbfc57974331dce79242ce806d92035fce06baFelipe Leme    update_progress(WEIGHT_FILE);
462997abb668b9248c2174c6cf169a0b7c6a02fd2cbElliott Hughes    close(fd);
4637dc7f3221f26b771c266a26ec785eb74287922f1Christopher Ferris
464f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (!newline) printf("\n");
465f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (title) printf("\n");
466f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    return 0;
467f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
468f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
46954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris/* prints the contents of a file */
47054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferrisint dump_file(const char *title, const char *path) {
47178f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    DurationReporter duration_reporter(title);
47254bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
47354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    if (fd < 0) {
47454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        int err = errno;
47554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        printf("*** %s: %s\n", path, strerror(err));
47654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        if (title) printf("\n");
47754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        return -1;
47854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    }
47954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    return _dump_file_from_fd(title, path, fd);
48054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris}
48154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris
48271a74ac75c8687195d27642fa0db31a796102c59Felipe Lemeint read_file_as_long(const char *path, long int *output) {
48371a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    int fd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
48471a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    if (fd < 0) {
48571a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        int err = errno;
48671a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        MYLOGE("Error opening file descriptor for %s: %s\n", path, strerror(err));
48771a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return -1;
48871a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
48971a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    char buffer[50];
49071a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
49171a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    if (bytes_read == -1) {
49271a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        MYLOGE("Error reading file %s: %s\n", path, strerror(errno));
49371a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return -2;
49471a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
49571a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    if (bytes_read == 0) {
49671a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        MYLOGE("File %s is empty\n", path);
49771a74ac75c8687195d27642fa0db31a796102c59Felipe Leme        return -3;
49871a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    }
49971a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    *output = atoi(buffer);
50071a74ac75c8687195d27642fa0db31a796102c59Felipe Leme    return 0;
50171a74ac75c8687195d27642fa0db31a796102c59Felipe Leme}
50271a74ac75c8687195d27642fa0db31a796102c59Felipe Leme
503326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn/* calls skip to gate calling dump_from_fd recursively
504326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn * in the specified directory. dump_from_fd defaults to
505326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn * dump_file_from_fd above when set to NULL. skip defaults
506326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn * to false when set to NULL. dump_from_fd will always be
507326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn * called with title NULL.
508326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn */
509326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzynint dump_files(const char *title, const char *dir,
510326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        bool (*skip)(const char *path),
511326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        int (*dump_from_fd)(const char *title, const char *path, int fd)) {
51278f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    DurationReporter duration_reporter(title);
513326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    DIR *dirp;
514326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    struct dirent *d;
515326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    char *newpath = NULL;
5168620bb4118a68721d10c29529dc6978847d08d00Felipe Leme    const char *slash = "/";
517326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    int fd, retval = 0;
518326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
519326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (title) {
520326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        printf("------ %s (%s) ------\n", title, dir);
521326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
52293d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    ON_DRY_RUN_RETURN(0);
523326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
524326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (dir[strlen(dir) - 1] == '/') {
525326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        ++slash;
526326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
527326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    dirp = opendir(dir);
528326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (dirp == NULL) {
529326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        retval = -errno;
530107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("%s: %s\n", dir, strerror(errno));
531326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        return retval;
532326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
533326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
534326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (!dump_from_fd) {
535326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        dump_from_fd = dump_file_from_fd;
536326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
537326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    for (; ((d = readdir(dirp))); free(newpath), newpath = NULL) {
538326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if ((d->d_name[0] == '.')
539326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn         && (((d->d_name[1] == '.') && (d->d_name[2] == '\0'))
540326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn          || (d->d_name[1] == '\0'))) {
541326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            continue;
542326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
543326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        asprintf(&newpath, "%s%s%s%s", dir, slash, d->d_name,
544326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn                 (d->d_type == DT_DIR) ? "/" : "");
545326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if (!newpath) {
546326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            retval = -errno;
547326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            continue;
548326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
549326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if (skip && (*skip)(newpath)) {
550326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            continue;
551326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
552326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if (d->d_type == DT_DIR) {
553326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            int ret = dump_files(NULL, newpath, skip, dump_from_fd);
554326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            if (ret < 0) {
555326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn                retval = ret;
556326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            }
557326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            continue;
558326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
559326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        fd = TEMP_FAILURE_RETRY(open(newpath, O_RDONLY | O_NONBLOCK | O_CLOEXEC));
560326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        if (fd < 0) {
561326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            retval = fd;
562326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            printf("*** %s: %s\n", newpath, strerror(errno));
563326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn            continue;
564326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        }
565326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        (*dump_from_fd)(NULL, newpath, fd);
566326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
567326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    closedir(dirp);
568326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    if (title) {
569326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn        printf("\n");
570326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    }
571326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn    return retval;
572326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn}
573326842fca4883f1256aa9ed019bb3206ee76fca7Mark Salyzyn
57454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris/* fd must have been opened with the flag O_NONBLOCK. With this flag set,
57554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris * it's possible to avoid issues where opening the file itself can get
57654bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris * stuck.
57754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris */
57854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferrisint dump_file_from_fd(const char *title, const char *path, int fd) {
57954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    int flags = fcntl(fd, F_GETFL);
58054bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    if (flags == -1) {
58154bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        printf("*** %s: failed to get flags on fd %d: %s\n", path, fd, strerror(errno));
582ed24d2ae2cd6e8b0afb86efa555f18bcfaf74a64Christopher Ferris        close(fd);
58354bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        return -1;
58454bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    } else if (!(flags & O_NONBLOCK)) {
58554bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        printf("*** %s: fd must have O_NONBLOCK set.\n", path);
586ed24d2ae2cd6e8b0afb86efa555f18bcfaf74a64Christopher Ferris        close(fd);
58754bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris        return -1;
58854bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    }
58954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris    return _dump_file_from_fd(title, path, fd);
5901dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown}
5911dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown
5921a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferrisbool waitpid_with_timeout(pid_t pid, int timeout_seconds, int* status) {
5931a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    sigset_t child_mask, old_mask;
5941a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    sigemptyset(&child_mask);
5951a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    sigaddset(&child_mask, SIGCHLD);
5961a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris
5971a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (sigprocmask(SIG_BLOCK, &child_mask, &old_mask) == -1) {
5981a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        printf("*** sigprocmask failed: %s\n", strerror(errno));
5991a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        return false;
6001a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    }
6011a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris
6021a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    struct timespec ts;
6031a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    ts.tv_sec = timeout_seconds;
6041a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    ts.tv_nsec = 0;
6051a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    int ret = TEMP_FAILURE_RETRY(sigtimedwait(&child_mask, NULL, &ts));
6061a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    int saved_errno = errno;
6071a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    // Set the signals back the way they were.
6081a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (sigprocmask(SIG_SETMASK, &old_mask, NULL) == -1) {
6091a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        printf("*** sigprocmask failed: %s\n", strerror(errno));
6101a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        if (ret == 0) {
6111a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            return false;
6121a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        }
6131a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    }
6141a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (ret == -1) {
6151a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        errno = saved_errno;
6161a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        if (errno == EAGAIN) {
6171a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            errno = ETIMEDOUT;
6181a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        } else {
6191a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            printf("*** sigtimedwait failed: %s\n", strerror(errno));
6201a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        }
6211a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        return false;
6221a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    }
6231a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris
6241a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    pid_t child_pid = waitpid(pid, status, WNOHANG);
6251a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (child_pid != pid) {
6261a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        if (child_pid != -1) {
6271a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            printf("*** Waiting for pid %d, got pid %d instead\n", pid, child_pid);
6281a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        } else {
6291a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            printf("*** waitpid failed: %s\n", strerror(errno));
6301a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        }
6311a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        return false;
6321a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    }
6331a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    return true;
6341a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris}
6351a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris
636a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme// TODO: refactor all those commands that convert args
637a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Lemevoid format_args(const char* command, const char *args[], std::string *string);
638a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme
639f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossint run_command(const char *title, int timeout_seconds, const char *command, ...) {
64078f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    DurationReporter duration_reporter(title);
641f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    fflush(stdout);
64293d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme
64393d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    const char *args[1024] = {command};
64493d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    size_t arg;
64593d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    va_list ap;
64693d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    va_start(ap, command);
64793d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    if (title) printf("------ %s (%s", title, command);
648a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme    bool null_terminated = false;
64993d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
65093d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme        args[arg] = va_arg(ap, const char *);
651a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme        if (args[arg] == nullptr) {
652a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme            null_terminated = true;
653a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme            break;
654a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme        }
655ea160d134cd46c246859072f90ab5dd05ada6364Felipe Leme        // TODO: null_terminated check is not really working; line below would crash dumpstate if
656ea160d134cd46c246859072f90ab5dd05ada6364Felipe Leme        // nullptr is missing
65793d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme        if (title) printf(" %s", args[arg]);
65893d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    }
65993d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    if (title) printf(") ------\n");
66093d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    fflush(stdout);
661a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme    if (!null_terminated) {
662a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme        // Fail now, otherwise execvp() call on run_command_always() might hang.
663a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme        std::string cmd;
664a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme        format_args(command, args, &cmd);
665a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme        MYLOGE("skipping command %s because its args were not NULL-terminated", cmd.c_str());
666a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme        return -1;
667a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme    }
66893d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme
66971bbfc57974331dce79242ce806d92035fce06baFelipe Leme    ON_DRY_RUN({ update_progress(timeout_seconds); va_end(ap); return 0; });
67093d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme
67129c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme    int status = run_command_always(title, DONT_DROP_ROOT, NORMAL_STDOUT, timeout_seconds, args);
672cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    va_end(ap);
673cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    return status;
674cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme}
675cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme
676cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Lemeint run_command_as_shell(const char *title, int timeout_seconds, const char *command, ...) {
677cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    DurationReporter duration_reporter(title);
678cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    fflush(stdout);
679cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme
680cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    const char *args[1024] = {command};
681cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    size_t arg;
682cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    va_list ap;
683cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    va_start(ap, command);
684cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (title) printf("------ %s (%s", title, command);
685cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    bool null_terminated = false;
686cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    for (arg = 1; arg < sizeof(args) / sizeof(args[0]); ++arg) {
687cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        args[arg] = va_arg(ap, const char *);
688cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        if (args[arg] == nullptr) {
689cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme            null_terminated = true;
690cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme            break;
691cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        }
692ea160d134cd46c246859072f90ab5dd05ada6364Felipe Leme        // TODO: null_terminated check is not really working; line below would crash dumpstate if
693ea160d134cd46c246859072f90ab5dd05ada6364Felipe Leme        // nullptr is missing
694cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        if (title) printf(" %s", args[arg]);
695cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    }
696cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (title) printf(") ------\n");
697cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    fflush(stdout);
698cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (!null_terminated) {
699cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        // Fail now, otherwise execvp() call on run_command_always() might hang.
700cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        std::string cmd;
701cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        format_args(command, args, &cmd);
702cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        MYLOGE("skipping command %s because its args were not NULL-terminated", cmd.c_str());
703cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        return -1;
704cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    }
705cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme
706cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    ON_DRY_RUN({ update_progress(timeout_seconds); va_end(ap); return 0; });
707cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme
70829c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme    int status = run_command_always(title, DROP_ROOT, NORMAL_STDOUT, timeout_seconds, args);
70971bbfc57974331dce79242ce806d92035fce06baFelipe Leme    va_end(ap);
71071bbfc57974331dce79242ce806d92035fce06baFelipe Leme    return status;
71193d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme}
71293d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme
71393d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme/* forks a command and waits for it to finish */
71429c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Lemeint run_command_always(const char *title, RootMode root_mode, StdoutMode stdout_mode,
71529c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        int timeout_seconds, const char *args[]) {
71629c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme    bool silent = (stdout_mode == REDIRECT_TO_STDERR);
717ea160d134cd46c246859072f90ab5dd05ada6364Felipe Leme    // TODO: need to check if args is null-terminated, otherwise execvp will crash dumpstate
718ea160d134cd46c246859072f90ab5dd05ada6364Felipe Leme
71971bbfc57974331dce79242ce806d92035fce06baFelipe Leme    /* TODO: for now we're simplifying the progress calculation by using the timeout as the weight.
72071bbfc57974331dce79242ce806d92035fce06baFelipe Leme     * It's a good approximation for most cases, except when calling dumpsys, where its weight
72171bbfc57974331dce79242ce806d92035fce06baFelipe Leme     * should be much higher proportionally to its timeout. */
72271bbfc57974331dce79242ce806d92035fce06baFelipe Leme    int weight = timeout_seconds;
72393d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme
72436b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme    const char *command = args[0];
72578f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    uint64_t start = DurationReporter::nanotime();
726f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    pid_t pid = fork();
727f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
728f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* handle error case */
729f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (pid < 0) {
73029c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        if (!silent) printf("*** fork: %s\n", strerror(errno));
73129c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        MYLOGE("*** fork: %s\n", strerror(errno));
732f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return pid;
733f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
734f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
735f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* handle child case */
736f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (pid == 0) {
73729c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        if (root_mode == DROP_ROOT && !drop_root_user()) {
73829c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        if (!silent) printf("*** fail todrop root before running %s: %s\n", command,
73929c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme                strerror(errno));
74029c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme            MYLOGE("*** could not drop root before running %s: %s\n", command, strerror(errno));
74173f731c30a8b7d811127b8e89e01c427b6aab622Felipe Leme            return -1;
742cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        }
743f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
74429c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        if (silent) {
74529c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme            // Redirect stderr to stdout
74629c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme            dup2(STDERR_FILENO, STDOUT_FILENO);
74729c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        }
74829c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme
749e7b6cf13f94f66ea446c95ab34040b0a577e43dcJohn Michelau        /* make sure the child dies when dumpstate dies */
750e7b6cf13f94f66ea446c95ab34040b0a577e43dcJohn Michelau        prctl(PR_SET_PDEATHSIG, SIGKILL);
751e7b6cf13f94f66ea446c95ab34040b0a577e43dcJohn Michelau
7522e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales        /* just ignore SIGPIPE, will go down with parent's */
7532e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales        struct sigaction sigact;
7542e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales        memset(&sigact, 0, sizeof(sigact));
7552e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales        sigact.sa_handler = SIG_IGN;
7562e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales        sigaction(SIGPIPE, &sigact, NULL);
7572e671bbdb741c2926b6df7b240fdc31c7361f330Andres Morales
758f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        execvp(command, (char**) args);
759ea160d134cd46c246859072f90ab5dd05ada6364Felipe Leme        // execvp's result will be handled after waitpid_with_timeout() below, but if it failed,
760ea160d134cd46c246859072f90ab5dd05ada6364Felipe Leme        // it's safer to exit dumpstate.
761ea160d134cd46c246859072f90ab5dd05ada6364Felipe Leme        MYLOGD("execvp on command '%s' failed (error: %s)", command, strerror(errno));
762ec72578b8461a5601962ea8102fd74a1f86d91fbFelipe Leme        fflush(stdout);
763baa85bd893665a48aee73d7ca7b73cd39b48f998Felipe Leme        // Must call _exit (instead of exit), otherwise it will corrupt the zip file.
764baa85bd893665a48aee73d7ca7b73cd39b48f998Felipe Leme        _exit(EXIT_FAILURE);
765f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
766f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
767f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* handle parent case */
7681a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    int status;
7691a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    bool ret = waitpid_with_timeout(pid, timeout_seconds, &status);
77078f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    uint64_t elapsed = DurationReporter::nanotime() - start;
771a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme    std::string cmd; // used to log command and its args
7721a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (!ret) {
7731a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        if (errno == ETIMEDOUT) {
774a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme            format_args(command, args, &cmd);
77529c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme            if (!silent) printf("*** command '%s' timed out after %.3fs (killing pid %d)\n",
77629c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme            cmd.c_str(), (float) elapsed / NANOS_PER_SEC, pid);
777a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme            MYLOGE("command '%s' timed out after %.3fs (killing pid %d)\n", cmd.c_str(),
7781a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris                   (float) elapsed / NANOS_PER_SEC, pid);
7791a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        } else {
780a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme            format_args(command, args, &cmd);
78129c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme            if (!silent) printf("*** command '%s': Error after %.4fs (killing pid %d)\n",
78229c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme            cmd.c_str(), (float) elapsed / NANOS_PER_SEC, pid);
783a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme            MYLOGE("command '%s': Error after %.4fs (killing pid %d)\n", cmd.c_str(),
7841a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris                   (float) elapsed / NANOS_PER_SEC, pid);
785f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
7861a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        kill(pid, SIGTERM);
7871a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        if (!waitpid_with_timeout(pid, 5, NULL)) {
7881a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            kill(pid, SIGKILL);
7891a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            if (!waitpid_with_timeout(pid, 5, NULL)) {
79029c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme                if (!silent) printf("could not kill command '%s' (pid %d) even with SIGKILL.\n",
79129c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme                        command, pid);
79214e034a02ec34b2c76afb06975bdfc943a9b5607Felipe Leme                MYLOGE("could not kill command '%s' (pid %d) even with SIGKILL.\n", command, pid);
7931a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris            }
794f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
7951a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris        return -1;
796a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme    } else if (status) {
797a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme        format_args(command, args, &cmd);
79829c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        if (!silent) printf("*** command '%s' failed: %s\n", cmd.c_str(), strerror(errno));
799a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme        MYLOGE("command '%s' failed: %s\n", cmd.c_str(), strerror(errno));
800a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme        return -2;
8011a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    }
802f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
8031a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    if (WIFSIGNALED(status)) {
80429c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        if (!silent) printf("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
80529c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        MYLOGE("*** %s: Killed by signal %d\n", command, WTERMSIG(status));
8061a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    } else if (WIFEXITED(status) && WEXITSTATUS(status) > 0) {
80729c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        if (!silent) printf("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
80829c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme        MYLOGE("*** %s: Exit code %d\n", command, WEXITSTATUS(status));
809f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
8101a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris
81171bbfc57974331dce79242ce806d92035fce06baFelipe Leme    if (weight > 0) {
81271bbfc57974331dce79242ce806d92035fce06baFelipe Leme        update_progress(weight);
81371bbfc57974331dce79242ce806d92035fce06baFelipe Leme    }
8141a9a3386151d90152cc18d995b9877b50ca84d90Christopher Ferris    return status;
815f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
816f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
817cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Lemebool drop_root_user() {
818cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (getgid() == AID_SHELL && getuid() == AID_SHELL) {
819cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        MYLOGD("drop_root_user(): already running as Shell");
820cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        return true;
821cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    }
822cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    /* ensure we will keep capabilities when we drop root */
823cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
824cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        MYLOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
825cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        return false;
826cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    }
827cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme
828cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    gid_t groups[] = { AID_LOG, AID_SDCARD_R, AID_SDCARD_RW,
829cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme            AID_MOUNT, AID_INET, AID_NET_BW_STATS, AID_READPROC };
830cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (setgroups(sizeof(groups)/sizeof(groups[0]), groups) != 0) {
831cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        MYLOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
832cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        return false;
833cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    }
834cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (setgid(AID_SHELL) != 0) {
835cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        MYLOGE("Unable to setgid, aborting: %s\n", strerror(errno));
836cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        return false;
837cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    }
838cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (setuid(AID_SHELL) != 0) {
839cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        MYLOGE("Unable to setuid, aborting: %s\n", strerror(errno));
840cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        return false;
841cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    }
842cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme
843cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    struct __user_cap_header_struct capheader;
844cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    struct __user_cap_data_struct capdata[2];
845cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    memset(&capheader, 0, sizeof(capheader));
846cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    memset(&capdata, 0, sizeof(capdata));
847cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    capheader.version = _LINUX_CAPABILITY_VERSION_3;
848cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    capheader.pid = 0;
849cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme
850cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
851cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
852cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    capdata[0].inheritable = 0;
853cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    capdata[1].inheritable = 0;
854cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme
855cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    if (capset(&capheader, &capdata[0]) < 0) {
856cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        MYLOGE("capset failed: %s\n", strerror(errno));
857cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme        return false;
858cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    }
859cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme
860cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    return true;
861cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme}
862cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme
86336b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Lemevoid send_broadcast(const std::string& action, const std::vector<std::string>& args) {
86436b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme    if (args.size() > 1000) {
865107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("send_broadcast: too many arguments (%d)\n", (int) args.size());
86636b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme        return;
86736b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme    }
868cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    const char *am_args[1024] = { "/system/bin/am", "broadcast", "--user", "0", "-a",
869cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme                                  action.c_str() };
870cf6a8b47dc04f1c33ddc337b5e21f2c5b7354cb5Felipe Leme    size_t am_index = 5; // Starts at the index of last initial value above.
87136b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme    for (const std::string& arg : args) {
87236b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme        am_args[++am_index] = arg.c_str();
87336b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme    }
87436b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme    // Always terminate with NULL.
87536b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme    am_args[am_index + 1] = NULL;
876a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme    std::string args_string;
877a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme    format_args(am_index + 1, am_args, &args_string);
878a34efb7ae17a7ea1078cce75be634120bd72a421Felipe Leme    MYLOGD("send_broadcast command: %s\n", args_string.c_str());
87929c39715b8a45ca2e97aecd8ebb63183f63da72bFelipe Leme    run_command_always(NULL, DROP_ROOT, REDIRECT_TO_STDERR, 20, am_args);
88036b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme}
88136b3f6ff17e456dea81501006e33d5fdd1d3b480Felipe Leme
882f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crosssize_t num_props = 0;
883f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic char* props[2000];
884f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
885f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic void print_prop(const char *key, const char *name, void *user) {
886f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    (void) user;
887f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (num_props < sizeof(props) / sizeof(props[0])) {
888f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        char buf[PROPERTY_KEY_MAX + PROPERTY_VALUE_MAX + 10];
889f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        snprintf(buf, sizeof(buf), "[%s]: [%s]\n", key, name);
890f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        props[num_props++] = strdup(buf);
891f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
892f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
893f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
894f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossstatic int compare_prop(const void *a, const void *b) {
895f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    return strcmp(*(char * const *) a, *(char * const *) b);
896f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
897f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
898f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross/* prints all the system properties */
899f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Crossvoid print_properties() {
90078f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    const char* title = "SYSTEM PROPERTIES";
90178f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    DurationReporter duration_reporter(title);
90278f2c86235d5882a8dc84c85a1c1864062e5f3afFelipe Leme    printf("------ %s ------\n", title);
90393d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    ON_DRY_RUN_RETURN();
904f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    size_t i;
905f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    num_props = 0;
906f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_list(print_prop, NULL);
907f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    qsort(&props, num_props, sizeof(props[0]), compare_prop);
908f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
909f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    for (i = 0; i < num_props; ++i) {
910f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        fputs(props[i], stdout);
911f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        free(props[i]);
912f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
913f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    printf("\n");
914f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
915f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
9162628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Lemeint open_socket(const char *service) {
917f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int s = android_get_control_socket(service);
918f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (s < 0) {
919107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("android_get_control_socket(%s): %s\n", service, strerror(errno));
920f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        exit(1);
921f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
922cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich    fcntl(s, F_SETFD, FD_CLOEXEC);
923f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (listen(s, 4) < 0) {
924107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("listen(control socket): %s\n", strerror(errno));
925f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        exit(1);
926f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
927f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
928f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    struct sockaddr addr;
929f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    socklen_t alen = sizeof(addr);
930f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    int fd = accept(s, &addr, &alen);
931f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (fd < 0) {
932107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("accept(control socket): %s\n", strerror(errno));
933f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        exit(1);
934f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
935f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
9362628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    return fd;
9372628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme}
9382628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme
9392628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme/* redirect output to a service control socket */
9402628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Lemevoid redirect_to_socket(FILE *redirect, const char *service) {
9412628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme    int fd = open_socket(service);
942f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    fflush(redirect);
943f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    dup2(fd, fileno(redirect));
944f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    close(fd);
945f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
946f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
9472628e9e939fda323fa44c5cb743f4a77b12a312aFelipe Leme// TODO: should call is_valid_output_file and/or be merged into it.
948111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Lemevoid create_parent_dirs(const char *path) {
949fdf52d3697aa0396bd9d8883892937b99a6772a0Srinath Sridharan    char *chp = const_cast<char *> (path);
950f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
951f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* skip initial slash */
952f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (chp[0] == '/')
953f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        chp++;
954f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
955f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* create leading directories, if necessary */
956111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    struct stat dir_stat;
957f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    while (chp && chp[0]) {
958f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        chp = strchr(chp, '/');
959f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        if (chp) {
960f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            *chp = 0;
961111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme            if (stat(path, &dir_stat) == -1 || !S_ISDIR(dir_stat.st_mode)) {
962cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                MYLOGI("Creating directory %s\n", path);
963111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme                if (mkdir(path, 0770)) { /* drwxrwx--- */
964cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                    MYLOGE("Unable to create directory %s: %s\n", path, strerror(errno));
965111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme                } else if (chown(path, AID_SHELL, AID_SHELL)) {
966cbce55d4fdbdd2e5a5515054c48d2116c5db2712Felipe Leme                    MYLOGE("Unable to change ownership of dir %s: %s\n", path, strerror(errno));
967111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme                }
968111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme            }
969f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            *chp++ = '/';
970f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
971f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
972111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme}
973111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme
974111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme/* redirect output to a file */
975111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Lemevoid redirect_to_file(FILE *redirect, char *path) {
976111b9d06cc0fc72438782c9234f28675e5077ef4Felipe Leme    create_parent_dirs(path);
977f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
978608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    int fd = TEMP_FAILURE_RETRY(open(path, O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NOFOLLOW,
979ff4a4dc7e6e3ead12020b4dc17548d767a419ddeChristopher Ferris                                     S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH));
980f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (fd < 0) {
981107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("%s: %s\n", path, strerror(errno));
982f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        exit(1);
983f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
984f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
985ff4a4dc7e6e3ead12020b4dc17548d767a419ddeChristopher Ferris    TEMP_FAILURE_RETRY(dup2(fd, fileno(redirect)));
986f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    close(fd);
987f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross}
988f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
989bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brownstatic bool should_dump_native_traces(const char* path) {
990bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    for (const char** p = native_processes_to_dump; *p; p++) {
991bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        if (!strcmp(*p, path)) {
992bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            return true;
993bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        }
994bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    }
995bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    return false;
996bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown}
997bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown
998bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown/* dump Dalvik and native stack traces, return the trace file location (NULL if none) */
999bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brownconst char *dump_traces() {
1000608385dd151e36a93f3e3f4a7514b1e720d20ae9Felipe Leme    DurationReporter duration_reporter("DUMP TRACES", NULL);
100193d705b6d742e8f00167ff2384e35c2205a6f9daFelipe Leme    ON_DRY_RUN_RETURN(NULL);
1002bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown    const char* result = NULL;
1003bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown
1004f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char traces_path[PROPERTY_VALUE_MAX] = "";
1005f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    property_get("dalvik.vm.stack-trace-file", traces_path, "");
1006f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (!traces_path[0]) return NULL;
1007f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1008f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* move the old traces.txt (if any) out of the way temporarily */
1009f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    char anr_traces_path[PATH_MAX];
1010f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    strlcpy(anr_traces_path, traces_path, sizeof(anr_traces_path));
1011f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    strlcat(anr_traces_path, ".anr", sizeof(anr_traces_path));
1012f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (rename(traces_path, anr_traces_path) && errno != ENOENT) {
1013107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("rename(%s, %s): %s\n", traces_path, anr_traces_path, strerror(errno));
1014f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return NULL;  // Can't rename old traces.txt -- no permission? -- leave it alone instead
1015f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1016f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1017f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* create a new, empty traces.txt file to receive stack dumps */
1018cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich    int fd = TEMP_FAILURE_RETRY(open(traces_path, O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC,
101954bcc5ffd5a79f4f194089c58d3de571532bf39bChristopher Ferris                                     0666));  /* -rw-rw-rw- */
1020f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (fd < 0) {
1021107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("%s: %s\n", traces_path, strerror(errno));
1022f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        return NULL;
1023f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1024c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich    int chmod_ret = fchmod(fd, 0666);
1025c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich    if (chmod_ret < 0) {
1026107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("fchmod on %s failed: %s\n", traces_path, strerror(errno));
1027c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich        close(fd);
1028c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich        return NULL;
1029c7f1fe26804ab986b90f3ba007db94f71e94932aNick Kralevich    }
1030f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
10318620bb4118a68721d10c29529dc6978847d08d00Felipe Leme    /* Variables below must be initialized before 'goto' statements */
10328620bb4118a68721d10c29529dc6978847d08d00Felipe Leme    int dalvik_found = 0;
10338620bb4118a68721d10c29529dc6978847d08d00Felipe Leme    int ifd, wfd = -1;
10348620bb4118a68721d10c29529dc6978847d08d00Felipe Leme
1035f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* walk /proc and kill -QUIT all Dalvik processes */
1036f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    DIR *proc = opendir("/proc");
1037f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (proc == NULL) {
1038107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("/proc: %s\n", strerror(errno));
1039bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        goto error_close_fd;
1040f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1041f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1042f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    /* use inotify to find when processes are done dumping */
10438620bb4118a68721d10c29529dc6978847d08d00Felipe Leme    ifd = inotify_init();
1044f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (ifd < 0) {
1045107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("inotify_init: %s\n", strerror(errno));
1046bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        goto error_close_fd;
1047f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1048f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
10498620bb4118a68721d10c29529dc6978847d08d00Felipe Leme    wfd = inotify_add_watch(ifd, traces_path, IN_CLOSE_WRITE);
1050f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    if (wfd < 0) {
1051107a05f72e711c92545d0be648ab79c4f858f372Felipe Leme        MYLOGE("inotify_add_watch(%s): %s\n", traces_path, strerror(errno));
1052bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        goto error_close_ifd;
1053f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    }
1054f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1055f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    struct dirent *d;
1056f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross    while ((d = readdir(proc))) {
1057f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        int pid = atoi(d->d_name);
1058f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        if (pid <= 0) continue;
1059f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
1060bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        char path[PATH_MAX];
1061bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        char data[PATH_MAX];
1062f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        snprintf(path, sizeof(path), "/proc/%d/exe", pid);
1063bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        ssize_t len = readlink(path, data, sizeof(data) - 1);
1064bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        if (len <= 0) {
1065f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross            continue;
1066f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross        }
1067bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown        data[len] = '\0';
1068f45fa6b2853cc32385375a0b63ee39ad6a968869Colin Cross
10690d6180f122127a9541e6193e0e6eceb5b08c608cColin Cross        if (!strncmp(data, "/system/bin/app_process", strlen("/system/bin/app_process"))) {
1070bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            /* skip zygote -- it won't dump its stack anyway */
1071bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            snprintf(path, sizeof(path), "/proc/%d/cmdline", pid);
1072cd67e9f059e270017d5defd8784c89dd8e6bcde7Nick Kralevich            int cfd = TEMP_FAILURE_RETRY(open(path, O_RDONLY | O_CLOEXEC));
10731dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown            len = read(cfd, data, sizeof(data) - 1);
10741dc94e315680b47fe430ef90f46c50a25c92fb6dJeff Brown            close(cfd);
1075bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            if (len <= 0) {
1076bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown                continue;
1077bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            }
1078bf7f49238d0af497d4300f7e611cf831854d6434Jeff Brown            data[len] = '\0';
10790d6180f122127a9541e6193e0e6eceb5b08c608cColin Cross            if (!