blob: 7076791705ff4bc20090686959a00ec0b15eb76d [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
Dan Albert3c9c33a2017-10-11 12:42:46 -070021#include <errno.h>
Felipe Lemef0292972016-11-22 13:57:05 -080022#include <stdint.h>
23#include <stdio.h>
24#include <string.h>
25#include <sys/capability.h>
26#include <sys/prctl.h>
27#include <sys/stat.h>
28#include <sys/types.h>
Jiyong Park522ae9a2017-06-23 21:23:16 +090029#include <unistd.h>
Felipe Lemef0292972016-11-22 13:57:05 -080030
31#include <cstdint>
32#include <string>
33#include <vector>
34
35#include <android-base/file.h>
Jiyong Park522ae9a2017-06-23 21:23:16 +090036#include <log/log.h>
Felipe Lemef0292972016-11-22 13:57:05 -080037#include <private/android_filesystem_config.h>
38
39uint64_t Nanotime() {
40 timespec ts;
41 clock_gettime(CLOCK_MONOTONIC, &ts);
42 return static_cast<uint64_t>(ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec);
43}
44
45// Switches to non-root user and group.
46bool DropRootUser() {
47 if (getgid() == AID_SHELL && getuid() == AID_SHELL) {
48 MYLOGD("drop_root_user(): already running as Shell\n");
49 return true;
50 }
51 /* ensure we will keep capabilities when we drop root */
52 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
53 MYLOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
54 return false;
55 }
56
Felipe Leme6ae5c4f2017-01-10 14:13:22 -080057 gid_t groups[] = {AID_LOG, AID_SDCARD_R, AID_SDCARD_RW, AID_MOUNT,
58 AID_INET, AID_NET_BW_STATS, AID_READPROC, AID_BLUETOOTH};
Felipe Lemef0292972016-11-22 13:57:05 -080059 if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) != 0) {
60 MYLOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
61 return false;
62 }
63 if (setgid(AID_SHELL) != 0) {
64 MYLOGE("Unable to setgid, aborting: %s\n", strerror(errno));
65 return false;
66 }
67 if (setuid(AID_SHELL) != 0) {
68 MYLOGE("Unable to setuid, aborting: %s\n", strerror(errno));
69 return false;
70 }
71
72 struct __user_cap_header_struct capheader;
73 struct __user_cap_data_struct capdata[2];
74 memset(&capheader, 0, sizeof(capheader));
75 memset(&capdata, 0, sizeof(capdata));
76 capheader.version = _LINUX_CAPABILITY_VERSION_3;
77 capheader.pid = 0;
78
Felipe Leme6ae5c4f2017-01-10 14:13:22 -080079 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
80 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
Felipe Lemef0292972016-11-22 13:57:05 -080081 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 }
Felipe Lemef0292972016-11-22 13:57:05 -0800157
158 if (!newline) dprintf(out_fd, "\n");
159 if (!title.empty()) dprintf(out_fd, "\n");
160 return 0;
161}