1 /*
2  * Copyright (C) 2017 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 #include "berberis/base/tracing.h"
18 
19 #include <fcntl.h>
20 #include <netdb.h>
21 #include <sys/socket.h>
22 #include <sys/stat.h>
23 #include <sys/types.h>
24 
25 #include <string>
26 
27 #include "berberis/base/checks.h"
28 #include "berberis/base/config_globals.h"
29 #include "berberis/base/file.h"
30 #include "berberis/base/logging.h"
31 #include "berberis/base/scoped_errno.h"
32 
33 namespace berberis {
34 
35 namespace {
36 
TraceToFile(std::string trace_filename)37 int TraceToFile(std::string trace_filename) {
38   if (trace_filename == "1") {
39     ALOGD("tracing to stdout");
40     return STDOUT_FILENO;
41   }
42 
43   if (trace_filename == "2") {
44     ALOGD("tracing to stderr");
45     return STDERR_FILENO;
46   }
47 
48   // If the provided path is relative set it up under app's private directory.
49   if (trace_filename.at(0) != '/') {
50     const char* app_private_dir = GetAppPrivateDir();
51     if (!app_private_dir) {
52       ALOGE("not tracing - app's private directory is undefined");
53       return -1;
54     }
55     trace_filename = std::string(app_private_dir) + "/" + trace_filename;
56   }
57 
58   if (uid_t uid = getuid()) {
59     // If not running as root, should be output file directory owner.
60     // To trace an app, use output file in app's data directory.
61     std::string dir = Dirname(trace_filename);
62     struct stat dir_stat;
63     if (stat(dir.c_str(), &dir_stat)) {
64       ALOGE("not tracing - failed to stat \"%s\"", dir.c_str());
65       return -1;
66     }
67     if (uid != dir_stat.st_uid) {
68       ALOGE("not tracing - uid mismatch of \"%s\"", dir.c_str());
69       return -1;
70     }
71   }
72 
73   int fd = open(trace_filename.c_str(), O_WRONLY | O_CREAT | O_APPEND | O_CLOEXEC, S_IWUSR);
74   if (fd == -1) {
75     ALOGE("not tracing - failed to open output file \"%s\"", trace_filename.c_str());
76     return -1;
77   }
78 
79   ALOGD("tracing to \"%s\"", trace_filename.c_str());
80   return fd;
81 }
82 
83 // At the moment, only accept ":<port>", assume localhost:<port>
TraceToSocket(const char * env)84 int TraceToSocket(const char* env) {
85   CHECK_EQ(':', env[0]);
86 
87   struct addrinfo hints {};
88   hints.ai_family = AF_UNSPEC;
89   hints.ai_socktype = SOCK_STREAM;
90 
91   struct addrinfo* results;
92   if (getaddrinfo("localhost", env + 1, &hints, &results) != 0) {
93     ALOGE("not tracing - failed to get info for \"%s\"", env);
94     return -1;
95   }
96 
97   for (auto info = results; info; info = info->ai_next) {
98     int fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol);
99     if (fd != -1) {
100       if (connect(fd, info->ai_addr, info->ai_addrlen) != -1) {
101         freeaddrinfo(results);
102         ALOGD("tracing to \"localhost%s\"", env);
103         return fd;
104       }
105 
106       close(fd);
107     }
108   }
109 
110   freeaddrinfo(results);
111   ALOGE("not tracing - failed to connect to \"%s\"", env);
112   return -1;
113 }
114 
115 }  // namespace
116 
117 int Tracing::fd_ = -1;
118 
InitImpl()119 void Tracing::InitImpl() {
120   ScopedErrno scoped_errno;
121 
122   auto env = GetTracingConfig();
123   if (!env || !env[0]) {
124     return;
125   }
126 
127   if (const char* filter_end = strchr(env, '=')) {
128     const char* app = GetAppPackageName();
129     if (!app || strncmp(app, env, filter_end - env) != 0) {
130       ALOGD("not tracing - filtered out");
131       return;
132     }
133     env = filter_end + 1;
134   }
135 
136   if (env[0] == ':') {
137     fd_ = TraceToSocket(env);
138   } else {
139     fd_ = TraceToFile(env);
140   }
141 }
142 
143 }  // namespace berberis
144