blob: 2d74377168f03e73522cc27df72833af1d638834 [file] [log] [blame]
Felipe Lemef0292972016-11-22 13:57:05 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "dumpstate"
18
19#include "DumpstateInternal.h"
20
21#include <stdint.h>
22#include <stdio.h>
23#include <string.h>
24#include <sys/capability.h>
25#include <sys/prctl.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28
29#include <cstdint>
30#include <string>
31#include <vector>
32
33#include <android-base/file.h>
34#include <cutils/log.h>
35#include <private/android_filesystem_config.h>
36
37uint64_t Nanotime() {
38 timespec ts;
39 clock_gettime(CLOCK_MONOTONIC, &ts);
40 return static_cast<uint64_t>(ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec);
41}
42
43// Switches to non-root user and group.
44bool DropRootUser() {
45 if (getgid() == AID_SHELL && getuid() == AID_SHELL) {
46 MYLOGD("drop_root_user(): already running as Shell\n");
47 return true;
48 }
49 /* ensure we will keep capabilities when we drop root */
50 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
51 MYLOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
52 return false;
53 }
54
55 gid_t groups[] = {AID_LOG, AID_SDCARD_R, AID_SDCARD_RW, AID_MOUNT, AID_INET,
56 AID_NET_BW_STATS, AID_READPROC, AID_WAKELOCK, AID_BLUETOOTH};
57 if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) != 0) {
58 MYLOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
59 return false;
60 }
61 if (setgid(AID_SHELL) != 0) {
62 MYLOGE("Unable to setgid, aborting: %s\n", strerror(errno));
63 return false;
64 }
65 if (setuid(AID_SHELL) != 0) {
66 MYLOGE("Unable to setuid, aborting: %s\n", strerror(errno));
67 return false;
68 }
69
70 struct __user_cap_header_struct capheader;
71 struct __user_cap_data_struct capdata[2];
72 memset(&capheader, 0, sizeof(capheader));
73 memset(&capdata, 0, sizeof(capdata));
74 capheader.version = _LINUX_CAPABILITY_VERSION_3;
75 capheader.pid = 0;
76
77 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted =
78 (CAP_TO_MASK(CAP_SYSLOG) | CAP_TO_MASK(CAP_BLOCK_SUSPEND));
79 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective =
80 (CAP_TO_MASK(CAP_SYSLOG) | CAP_TO_MASK(CAP_BLOCK_SUSPEND));
81 capdata[0].inheritable = 0;
82 capdata[1].inheritable = 0;
83
84 if (capset(&capheader, &capdata[0]) < 0) {
85 MYLOGE("capset failed: %s\n", strerror(errno));
86 return false;
87 }
88
89 return true;
90}
91
92int DumpFileFromFdToFd(const std::string& title, const std::string& path_string, int fd, int out_fd,
93 bool dry_run) {
94 const char* path = path_string.c_str();
95 if (!title.empty()) {
96 dprintf(out_fd, "------ %s (%s", title.c_str(), path);
97
98 struct stat st;
99 // Only show the modification time of non-device files.
100 size_t path_len = strlen(path);
101 if ((path_len < 6 || memcmp(path, "/proc/", 6)) &&
102 (path_len < 5 || memcmp(path, "/sys/", 5)) &&
103 (path_len < 3 || memcmp(path, "/d/", 3)) && !fstat(fd, &st)) {
104 char stamp[80];
105 time_t mtime = st.st_mtime;
106 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
107 dprintf(out_fd, ": %s", stamp);
108 }
109 dprintf(out_fd, ") ------\n");
110 fsync(out_fd);
111 }
112 if (dry_run) {
113 if (out_fd != STDOUT_FILENO) {
114 // There is no title, but we should still print a dry-run message
115 dprintf(out_fd, "%s: skipped on dry run\n", path);
116 } else if (!title.empty()) {
117 dprintf(out_fd, "\t(skipped on dry run)\n");
118 }
119 fsync(out_fd);
120 return 0;
121 }
122 bool newline = false;
123 fd_set read_set;
124 timeval tm;
125 while (true) {
126 FD_ZERO(&read_set);
127 FD_SET(fd, &read_set);
128 /* Timeout if no data is read for 30 seconds. */
129 tm.tv_sec = 30;
130 tm.tv_usec = 0;
131 uint64_t elapsed = Nanotime();
132 int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, nullptr, nullptr, &tm));
133 if (ret == -1) {
134 dprintf(out_fd, "*** %s: select failed: %s\n", path, strerror(errno));
135 newline = true;
136 break;
137 } else if (ret == 0) {
138 elapsed = Nanotime() - elapsed;
139 dprintf(out_fd, "*** %s: Timed out after %.3fs\n", path, (float)elapsed / NANOS_PER_SEC);
140 newline = true;
141 break;
142 } else {
143 char buffer[65536];
144 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
145 if (bytes_read > 0) {
146 android::base::WriteFully(out_fd, buffer, bytes_read);
147 newline = (buffer[bytes_read - 1] == '\n');
148 } else {
149 if (bytes_read == -1) {
150 dprintf(out_fd, "*** %s: Failed to read from fd: %s", path, strerror(errno));
151 newline = true;
152 }
153 break;
154 }
155 }
156 }
157 close(fd);
158
159 if (!newline) dprintf(out_fd, "\n");
160 if (!title.empty()) dprintf(out_fd, "\n");
161 return 0;
162}