1 /*
2 * Copyright (C) 2018 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 DEBUG false // STOPSHIP if true
18 #include "config/ConfigKey.h"
19 #include "Log.h"
20
21 #include "frameworks/base/cmds/statsd/src/statsd_config.pb.h" // Alert
22
23 #include <android-base/unique_fd.h>
24 #include <errno.h>
25 #include <fcntl.h>
26 #include <inttypes.h>
27 #include <sys/types.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30
31 #include <string>
32
33 namespace {
34 const char kDropboxTag[] = "perfetto";
35 }
36
37 namespace android {
38 namespace os {
39 namespace statsd {
40
CollectPerfettoTraceAndUploadToDropbox(const PerfettoDetails & config,int64_t subscription_id,int64_t alert_id,const ConfigKey & configKey)41 bool CollectPerfettoTraceAndUploadToDropbox(const PerfettoDetails& config,
42 int64_t subscription_id,
43 int64_t alert_id,
44 const ConfigKey& configKey) {
45 VLOG("Starting trace collection through perfetto");
46
47 if (!config.has_trace_config()) {
48 ALOGE("The perfetto trace config is empty, aborting");
49 return false;
50 }
51
52 char subscriptionId[25];
53 char alertId[25];
54 char configId[25];
55 char configUid[25];
56 snprintf(subscriptionId, sizeof(subscriptionId), "%" PRId64, subscription_id);
57 snprintf(alertId, sizeof(alertId), "%" PRId64, alert_id);
58 snprintf(configId, sizeof(configId), "%" PRId64, configKey.GetId());
59 snprintf(configUid, sizeof(configUid), "%d", configKey.GetUid());
60
61 android::base::unique_fd readPipe;
62 android::base::unique_fd writePipe;
63 if (!android::base::Pipe(&readPipe, &writePipe)) {
64 ALOGE("pipe() failed while calling the Perfetto client: %s", strerror(errno));
65 return false;
66 }
67
68 pid_t pid = fork();
69 if (pid < 0) {
70 ALOGE("fork() failed while calling the Perfetto client: %s", strerror(errno));
71 return false;
72 }
73
74 if (pid == 0) {
75 // Child process.
76
77 // No malloc calls or library calls after this point. Remember that even
78 // ALOGx (aka android_printLog()) can use dynamic memory for vsprintf().
79
80 writePipe.reset(); // Close the write end (owned by the main process).
81
82 // Replace stdin with |readPipe| so the main process can write into it.
83 if (dup2(readPipe.get(), STDIN_FILENO) < 0) _exit(1);
84 readPipe.reset();
85
86 // Replace stdout/stderr with /dev/null and close any other file
87 // descriptor. This is to avoid SELinux complaining about perfetto
88 // trying to access files accidentally left open by statsd (i.e. files
89 // that have been opened without the O_CLOEXEC flag).
90 int devNullFd = open("/dev/null", O_RDWR | O_CLOEXEC);
91 if (dup2(devNullFd, STDOUT_FILENO) < 0) _exit(2);
92 if (dup2(devNullFd, STDERR_FILENO) < 0) _exit(3);
93 close(devNullFd);
94 for (int i = 0; i < 1024; i++) {
95 if (i != STDIN_FILENO && i != STDOUT_FILENO && i != STDERR_FILENO) close(i);
96 }
97
98 execl("/system/bin/perfetto", "perfetto", "--background", "--config", "-", "--dropbox",
99 kDropboxTag, "--alert-id", alertId, "--config-id", configId, "--config-uid",
100 configUid, "--subscription-id", subscriptionId, nullptr);
101
102 // execl() doesn't return in case of success, if we get here something
103 // failed.
104 _exit(4);
105 }
106
107 // Main process.
108
109 readPipe.reset(); // Close the read end (owned by the child process).
110
111 // Using fdopen() because fwrite() has the right logic to chunking write()
112 // over a pipe (see __sfvwrite()).
113 FILE* writePipeStream = android::base::Fdopen(std::move(writePipe), "wb");
114 if (!writePipeStream) {
115 ALOGE("fdopen() failed while calling the Perfetto client: %s", strerror(errno));
116 return false;
117 }
118
119 const std::string& cfgProto = config.trace_config();
120 size_t bytesWritten = fwrite(cfgProto.data(), 1, cfgProto.size(), writePipeStream);
121 fclose(writePipeStream);
122 if (bytesWritten != cfgProto.size() || cfgProto.size() == 0) {
123 ALOGE("fwrite() failed (ret: %zd) while calling the Perfetto client: %s", bytesWritten,
124 strerror(errno));
125 return false;
126 }
127
128 // This does NOT wait for the full duration of the trace. It just waits until
129 // the process has read the config from stdin and detached.
130 int childStatus = 0;
131 waitpid(pid, &childStatus, 0);
132 if (!WIFEXITED(childStatus) || WEXITSTATUS(childStatus) != 0) {
133 ALOGE("Child process failed (0x%x) while calling the Perfetto client", childStatus);
134 return false;
135 }
136
137 VLOG("CollectPerfettoTraceAndUploadToDropbox() succeeded");
138 return true;
139 }
140
141 } // namespace statsd
142 } // namespace os
143 } // namespace android
144