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