Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <array>
#include <cctype>
#include <cstdlib>
#include <optional>
#include <string>
#include <string_view>
 
#include "core/platform/telemetry_strings.h"
 
#ifdef _WIN32
#include <Windows.h>
#include "core/common/path_string.h"
#endif
 
namespace onnxruntime {
namespace telemetry_detail {
 
// Well-known CI / build-pipeline environment variables. Mirrors the list used by Foundry Local /
// neutron-server so that ORT's CI telemetry suppression behaves consistently across stacks; keep the
// two lists in sync if either changes.
inline constexpr std::array<const char*, 13> kCiEnvironmentVariableNames = {
    "CI",                                  // Generic CI flag used by many providers
    "TF_BUILD",                            // Azure Pipelines
    "GITHUB_ACTIONS",                      // GitHub Actions
    "GITLAB_CI",                           // GitLab CI
    "CIRCLECI",                            // CircleCI
    "TRAVIS",                              // Travis CI
    "JENKINS_URL",                         // Jenkins
    "CODEBUILD_BUILD_ID",                  // AWS CodeBuild
    "BUILDKITE",                           // Buildkite
    "TEAMCITY_VERSION",                    // TeamCity
    "APPVEYOR",                            // AppVeyor
    "BITBUCKET_BUILD_NUMBER",              // Bitbucket Pipelines
    "SYSTEM_TEAMFOUNDATIONCOLLECTIONURI",  // Azure DevOps
};
 
// Unset/empty values succeed; oversized, unreadable, or unstable values return nullopt.
inline std::optional<std::string> ReadTelemetryEnvironment(
    const char* name, size_t max_bytes = kMaxTelemetryProbeBytes) {
#ifdef _WIN32
  const auto wide_name = ToWideString(name);
  ::SetLastError(ERROR_SUCCESS);
  DWORD required_size = ::GetEnvironmentVariableW(wide_name.c_str(), nullptr, 0);
  for (int attempt = 0; attempt < 3; ++attempt) {
    if (required_size == 0) {
      const DWORD error = ::GetLastError();
      if (error != ERROR_SUCCESS && error != ERROR_ENVVAR_NOT_FOUND) return std::nullopt;
      return std::string{};
    }
    if (required_size - 1 > max_bytes) return std::nullopt;
    std::wstring value(required_size, L'\0');
    ::SetLastError(ERROR_SUCCESS);
    const DWORD written = ::GetEnvironmentVariableW(wide_name.c_str(), value.data(), required_size);
    if (written == 0) {
      required_size = 0;
      continue;
    }
    if (written < required_size) {
      value.resize(written);
      if (TelemetryWideStringView(value, max_bytes).size() != value.size()) return std::nullopt;
      return ToUTF8String(value);
    }
    required_size = written;
  }
  return std::nullopt;
#else
  const char* value = std::getenv(name);
  const auto prefix = TelemetryCStringView(value, max_bytes);
  if (prefix.size() > max_bytes) return std::nullopt;
  return std::string(prefix);
#endif
}
 
inline std::string_view TrimAscii(std::string_view s) {
  size_t begin = 0;
  size_t end = s.size();
  while (begin < end && std::isspace(static_cast<unsigned char>(s[begin]))) {
    ++begin;
  }
  while (end > begin && std::isspace(static_cast<unsigned char>(s[end - 1]))) {
    --end;
  }
  return s.substr(begin, end - begin);
}
 
inline std::string ToLowerAscii(std::string_view s) {
  std::string out(TelemetryStringView(s, kMaxTelemetryProbeBytes));
  for (char& c : out) {
    c = static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
  }
  return out;
}
 
// A CI variable counts as present unless its (trimmed) value is empty or an explicit falsey token, so
// that a runner exporting e.g. CI=false does not trip detection.
inline bool IsTruthyCiValue(std::string_view value) {
  if (value.size() > kMaxTelemetryProbeBytes) return true;
  const std::string v = ToLowerAscii(TrimAscii(value));
  return !v.empty() && v != "0" && v != "false" && v != "no" && v != "off";
}
 
struct HostEnvironmentEvidence {
  bool docker_marker = false;
  bool podman_marker = false;
  bool kubernetes = false;
  bool aws_ecs = false;
  bool generic_container = false;
  bool android_emulator = false;
  bool apple_virtual_machine = false;
  std::string systemd_container;
  std::string cgroup;
  std::string dmi;
  std::string cpu_info;
  std::string kernel_release;
};
 
struct HostEnvironmentInfo {
  bool is_container;
  bool is_virtual_machine;
  bool is_emulator;
  const char* container_type;
  const char* virtualization_type;
  const char* environment_class;
  const char* detection_confidence;
  const char* device_id_scope;
};
 
inline bool ContainsAscii(std::string_view haystack, std::string_view needle) {
  return ToLowerAscii(haystack).find(ToLowerAscii(needle)) != std::string::npos;
}
 
// Classifies only positive evidence. "undetected" deliberately does not claim bare metal.
inline HostEnvironmentInfo ClassifyHostEnvironment(const HostEnvironmentEvidence& evidence) {
  const std::string container_name = ToLowerAscii(TrimAscii(TelemetryStringView(evidence.systemd_container)));
  const std::string combined_container_evidence =
      ToLowerAscii(TelemetryStringView(evidence.cgroup, kMaxTelemetryProbeBytes)) + " " + container_name;
 
  const char* container_type = "none";
  int container_confidence = 0;
  if (evidence.kubernetes || ContainsAscii(combined_container_evidence, "kubepods")) {
    container_type = "kubernetes";
    container_confidence = 2;
  } else if (evidence.aws_ecs) {
    container_type = "amazonECS";
    container_confidence = 2;
  } else if (evidence.podman_marker || ContainsAscii(combined_container_evidence, "libpod") ||
             ContainsAscii(combined_container_evidence, "podman")) {
    container_type = "podman";
    container_confidence = 2;
  } else if (evidence.docker_marker || ContainsAscii(combined_container_evidence, "docker")) {
    container_type = "docker";
    container_confidence = 2;
  } else if (ContainsAscii(combined_container_evidence, "containerd")) {
    container_type = "containerd";
    container_confidence = 1;
  } else if (ContainsAscii(combined_container_evidence, "lxc")) {
    container_type = "lxc";
    container_confidence = 1;
  } else if (!container_name.empty() && container_name != "none") {
    container_type = "other";
    container_confidence = 2;
  } else if (evidence.generic_container) {
    container_type = "other";
    container_confidence = 1;
  }
 
  const std::string dmi = ToLowerAscii(evidence.dmi);
  const std::string cpu_info = ToLowerAscii(evidence.cpu_info);
  const std::string kernel_release = ToLowerAscii(evidence.kernel_release);
  const char* virtualization_type = "none";
  int virtualization_confidence = 0;
  if (evidence.android_emulator) {
    virtualization_type = "androidEmulator";
    virtualization_confidence = 2;
  } else if (evidence.apple_virtual_machine) {
    virtualization_type = "appleVirtualMachine";
    virtualization_confidence = 2;
  } else if (kernel_release.find("microsoft") != std::string::npos ||
             kernel_release.find("wsl") != std::string::npos) {
    virtualization_type = "wsl";
    virtualization_confidence = 2;
  } else if (dmi.find("vmware") != std::string::npos) {
    virtualization_type = "vmware";
    virtualization_confidence = 2;
  } else if (dmi.find("virtualbox") != std::string::npos || dmi.find("innotek") != std::string::npos) {
    virtualization_type = "virtualBox";
    virtualization_confidence = 2;
  } else if (dmi.find("microsoft corporation") != std::string::npos &&
             dmi.find("virtual machine") != std::string::npos) {
    virtualization_type = "hyperV";
    virtualization_confidence = 2;
  } else if (dmi.find("amazon ec2") != std::string::npos) {
    virtualization_type = "amazonEC2";
    virtualization_confidence = 2;
  } else if (dmi.find("google compute engine") != std::string::npos) {
    virtualization_type = "googleComputeEngine";
    virtualization_confidence = 2;
  } else if (dmi.find("openstack") != std::string::npos) {
    virtualization_type = "openStack";
    virtualization_confidence = 2;
  } else if (dmi.find("kvm") != std::string::npos) {
    virtualization_type = "kvm";
    virtualization_confidence = 2;
  } else if (dmi.find("qemu") != std::string::npos) {
    virtualization_type = "qemu";
    virtualization_confidence = 2;
  } else if (dmi.find("xen") != std::string::npos) {
    virtualization_type = "xen";
    virtualization_confidence = 2;
  } else if (dmi.find("parallels") != std::string::npos) {
    virtualization_type = "parallels";
    virtualization_confidence = 2;
  } else if (dmi.find("bhyve") != std::string::npos) {
    virtualization_type = "bhyve";
    virtualization_confidence = 2;
  } else if (cpu_info.find("hypervisor") != std::string::npos) {
    virtualization_type = "other";
    virtualization_confidence = 1;
  }
 
  const bool is_container = container_confidence != 0;
  const bool is_virtual_machine = virtualization_confidence != 0;
  const bool is_emulator = evidence.android_emulator;
  const char* environment_class = "undetected";
  if (is_container && is_virtual_machine) {
    environment_class = "containerOnVirtualMachine";
  } else if (is_container) {
    environment_class = "container";
  } else if (is_emulator) {
    environment_class = "emulator";
  } else if (is_virtual_machine) {
    environment_class = "virtualMachine";
  }
 
  const int confidence =
      container_confidence > virtualization_confidence ? container_confidence : virtualization_confidence;
  return {is_container,
          is_virtual_machine,
          is_emulator,
          container_type,
          virtualization_type,
          environment_class,
          confidence == 2 ? "high" : confidence == 1 ? "medium"
                                                     : "none",
          is_container ? "container" : is_virtual_machine ? "virtualMachine"
                                                          : "installation"};
}
 
}  // namespace telemetry_detail
 
// True if a well-known CI / build-pipeline environment variable is set to a truthy value. ORT's
// telemetry providers suppress all telemetry when this holds, matching Olive and Foundry Local.
inline bool IsRunningInCI() {
  for (const char* name : telemetry_detail::kCiEnvironmentVariableNames) {
    const auto value = telemetry_detail::ReadTelemetryEnvironment(name);
    if (!value || telemetry_detail::IsTruthyCiValue(*value)) {
      return true;
    }
  }
  return false;
}
 
// True if ORT_RUNNING_UNIT_TESTS is set to a truthy value. ORT's own unit-test entry points set this
// before creating any environment, so local (non-CI) test runs never initialize the telemetry uploader
// or emit events. This is an internal harness signal, not a user-facing opt-out.
inline bool IsRunningUnitTests() {
  const auto value = telemetry_detail::ReadTelemetryEnvironment("ORT_RUNNING_UNIT_TESTS");
  return !value || telemetry_detail::IsTruthyCiValue(*value);
}
 
// True if ORT_DISABLE_TELEMETRY is set to a truthy value (1/true/yes/on/y, case-insensitive).
// The POSIX 1DS provider latches this full opt-out during initialization. Windows ETW retains its
// separate API/trace-session control model and does not consult this environment variable.
inline bool IsTelemetryDisabledByEnvironment() {
  const auto input = telemetry_detail::ReadTelemetryEnvironment("ORT_DISABLE_TELEMETRY");
  if (!input) return true;
  const std::string value = telemetry_detail::ToLowerAscii(
      telemetry_detail::TrimAscii(*input));
  return value == "1" || value == "true" || value == "yes" || value == "on" || value == "y";
}
 
}  // namespace onnxruntime