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 #include "perfetto/tracing/default_socket.h"
18
19 #include "perfetto/base/build_config.h"
20 #include "perfetto/base/logging.h"
21 #include "perfetto/ext/base/android_utils.h"
22 #include "perfetto/ext/base/string_utils.h"
23 #include "perfetto/ext/base/utils.h"
24 #include "perfetto/ext/ipc/basic_types.h"
25 #include "perfetto/ext/tracing/core/basic_types.h"
26
27 #include <stdlib.h>
28
29 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX) || \
30 PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID) || \
31 PERFETTO_BUILDFLAG(PERFETTO_OS_APPLE)
32 #include <unistd.h>
33 #endif
34
35 namespace perfetto {
36 namespace {
37
38 const char* kRunPerfettoBaseDir = "/run/perfetto/";
39
40 // On Linux and CrOS, check /run/perfetto/ before using /tmp/ as the socket
41 // base directory.
UseRunPerfettoBaseDir()42 bool UseRunPerfettoBaseDir() {
43 #if PERFETTO_BUILDFLAG(PERFETTO_OS_LINUX)
44 // Note that the trailing / in |kRunPerfettoBaseDir| ensures we are checking
45 // against a directory, not a file.
46 int res = PERFETTO_EINTR(access(kRunPerfettoBaseDir, X_OK));
47 if (!res)
48 return true;
49
50 // If the path doesn't exist (ENOENT), fail silently to the caller. Otherwise,
51 // fail with an explicit error message.
52 if (errno != ENOENT
53 #if PERFETTO_BUILDFLAG(PERFETTO_CHROMIUM_BUILD)
54 // access(2) won't return EPERM, but Chromium sandbox returns EPERM if the
55 // sandbox doesn't allow the call (e.g. in the child processes).
56 && errno != EPERM
57 #endif
58 ) {
59 PERFETTO_PLOG("%s exists but cannot be accessed. Falling back on /tmp/ ",
60 kRunPerfettoBaseDir);
61 }
62 return false;
63 #else
64 base::ignore_result(kRunPerfettoBaseDir);
65 return false;
66 #endif
67 }
68
69 } // anonymous namespace
70
GetProducerSocket()71 const char* GetProducerSocket() {
72 const char* name = getenv("PERFETTO_PRODUCER_SOCK_NAME");
73 if (name == nullptr) {
74 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
75 name = "127.0.0.1:32278";
76 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
77 name = "/dev/socket/traced_producer";
78 #else
79 // Use /run/perfetto if it exists. Then fallback to /tmp.
80 static const char* producer_socket =
81 UseRunPerfettoBaseDir() ? "/run/perfetto/traced-producer.sock"
82 : "/tmp/perfetto-producer";
83 name = producer_socket;
84 #endif
85 }
86 base::ignore_result(UseRunPerfettoBaseDir); // Silence unused func warnings.
87 return name;
88 }
89
GetRelaySocket()90 std::string GetRelaySocket() {
91 // The relay socket is optional and is connected only when the env var is set.
92 // In Android, if the env var isn't set then we check the
93 // |traced_relay.relay_port| system property.
94 const char* name = getenv("PERFETTO_RELAY_SOCK_NAME");
95 if (name != nullptr)
96 return std::string(name);
97 #if PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
98 return base::GetAndroidProp("traced_relay.relay_port");
99 #else
100 return std::string();
101 #endif
102 }
103
TokenizeProducerSockets(const char * producer_socket_names)104 std::vector<std::string> TokenizeProducerSockets(
105 const char* producer_socket_names) {
106 return base::SplitString(producer_socket_names, ",");
107 }
108
GetConsumerSocket()109 const char* GetConsumerSocket() {
110 const char* name = getenv("PERFETTO_CONSUMER_SOCK_NAME");
111 if (name == nullptr) {
112 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN)
113 name = "127.0.0.1:32279";
114 #elif PERFETTO_BUILDFLAG(PERFETTO_OS_ANDROID)
115 name = "/dev/socket/traced_consumer";
116 #else
117 // Use /run/perfetto if it exists. Then fallback to /tmp.
118 static const char* consumer_socket =
119 UseRunPerfettoBaseDir() ? "/run/perfetto/traced-consumer.sock"
120 : "/tmp/perfetto-consumer";
121 name = consumer_socket;
122 #endif
123 }
124 return name;
125 }
126
127 } // namespace perfetto
128