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