blob: 0b42647c213f91ee6eb3e98f498212b8fa000321 [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>
29
30#include <cstdint>
31#include <string>
32#include <vector>
33
34#include <android-base/file.h>
35#include <cutils/log.h>
36#include <private/android_filesystem_config.h>
37
38uint64_t Nanotime() {
39 timespec ts;
40 clock_gettime(CLOCK_MONOTONIC, &ts);
41 return static_cast<uint64_t>(ts.tv_sec * NANOS_PER_SEC + ts.tv_nsec);
42}
43
44// Switches to non-root user and group.
45bool DropRootUser() {
46 if (getgid() == AID_SHELL && getuid() == AID_SHELL) {
47 MYLOGD("drop_root_user(): already running as Shell\n");
48 return true;
49 }
50 /* ensure we will keep capabilities when we drop root */
51 if (prctl(PR_SET_KEEPCAPS, 1) < 0) {
52 MYLOGE("prctl(PR_SET_KEEPCAPS) failed: %s\n", strerror(errno));
53 return false;
54 }
55
Felipe Leme6ae5c4f2017-01-10 14:13:22 -080056 gid_t groups[] = {AID_LOG, AID_SDCARD_R, AID_SDCARD_RW, AID_MOUNT,
57 AID_INET, AID_NET_BW_STATS, AID_READPROC, AID_BLUETOOTH};
Felipe Lemef0292972016-11-22 13:57:05 -080058 if (setgroups(sizeof(groups) / sizeof(groups[0]), groups) != 0) {
59 MYLOGE("Unable to setgroups, aborting: %s\n", strerror(errno));
60 return false;
61 }
62 if (setgid(AID_SHELL) != 0) {
63 MYLOGE("Unable to setgid, aborting: %s\n", strerror(errno));
64 return false;
65 }
66 if (setuid(AID_SHELL) != 0) {
67 MYLOGE("Unable to setuid, aborting: %s\n", strerror(errno));
68 return false;
69 }
70
71 struct __user_cap_header_struct capheader;
72 struct __user_cap_data_struct capdata[2];
73 memset(&capheader, 0, sizeof(capheader));
74 memset(&capdata, 0, sizeof(capdata));
75 capheader.version = _LINUX_CAPABILITY_VERSION_3;
76 capheader.pid = 0;
77
Felipe Leme6ae5c4f2017-01-10 14:13:22 -080078 capdata[CAP_TO_INDEX(CAP_SYSLOG)].permitted = CAP_TO_MASK(CAP_SYSLOG);
79 capdata[CAP_TO_INDEX(CAP_SYSLOG)].effective = CAP_TO_MASK(CAP_SYSLOG);
Felipe Lemef0292972016-11-22 13:57:05 -080080 capdata[0].inheritable = 0;
81 capdata[1].inheritable = 0;
82
83 if (capset(&capheader, &capdata[0]) < 0) {
84 MYLOGE("capset failed: %s\n", strerror(errno));
85 return false;
86 }
87
88 return true;
89}
90
91int DumpFileFromFdToFd(const std::string& title, const std::string& path_string, int fd, int out_fd,
92 bool dry_run) {
93 const char* path = path_string.c_str();
94 if (!title.empty()) {
95 dprintf(out_fd, "------ %s (%s", title.c_str(), path);
96
97 struct stat st;
98 // Only show the modification time of non-device files.
99 size_t path_len = strlen(path);
100 if ((path_len < 6 || memcmp(path, "/proc/", 6)) &&
101 (path_len < 5 || memcmp(path, "/sys/", 5)) &&
102 (path_len < 3 || memcmp(path, "/d/", 3)) && !fstat(fd, &st)) {
103 char stamp[80];
104 time_t mtime = st.st_mtime;
105 strftime(stamp, sizeof(stamp), "%Y-%m-%d %H:%M:%S", localtime(&mtime));
106 dprintf(out_fd, ": %s", stamp);
107 }
108 dprintf(out_fd, ") ------\n");
109 fsync(out_fd);
110 }
111 if (dry_run) {
112 if (out_fd != STDOUT_FILENO) {
113 // There is no title, but we should still print a dry-run message
114 dprintf(out_fd, "%s: skipped on dry run\n", path);
115 } else if (!title.empty()) {
116 dprintf(out_fd, "\t(skipped on dry run)\n");
117 }
118 fsync(out_fd);
119 return 0;
120 }
121 bool newline = false;
122 fd_set read_set;
123 timeval tm;
124 while (true) {
125 FD_ZERO(&read_set);
126 FD_SET(fd, &read_set);
127 /* Timeout if no data is read for 30 seconds. */
128 tm.tv_sec = 30;
129 tm.tv_usec = 0;
130 uint64_t elapsed = Nanotime();
131 int ret = TEMP_FAILURE_RETRY(select(fd + 1, &read_set, nullptr, nullptr, &tm));
132 if (ret == -1) {
133 dprintf(out_fd, "*** %s: select failed: %s\n", path, strerror(errno));
134 newline = true;
135 break;
136 } else if (ret == 0) {
137 elapsed = Nanotime() - elapsed;
138 dprintf(out_fd, "*** %s: Timed out after %.3fs\n", path, (float)elapsed / NANOS_PER_SEC);
139 newline = true;
140 break;
141 } else {
142 char buffer[65536];
143 ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd, buffer, sizeof(buffer)));
144 if (bytes_read > 0) {
145 android::base::WriteFully(out_fd, buffer, bytes_read);
146 newline = (buffer[bytes_read - 1] == '\n');
147 } else {
148 if (bytes_read == -1) {
149 dprintf(out_fd, "*** %s: Failed to read from fd: %s", path, strerror(errno));
150 newline = true;
151 }
152 break;
153 }
154 }
155 }
156 close(fd);
157
158 if (!newline) dprintf(out_fd, "\n");
159 if (!title.empty()) dprintf(out_fd, "\n");
160 return 0;
161}