27
27
28
28
#include " swift/Demangling/Demangler.h"
29
29
30
+ #ifdef __linux__
31
+ #include < sys/auxv.h>
32
+ #endif
33
+
30
34
#ifdef _WIN32
31
35
#include < windows.h>
32
36
#else
@@ -212,17 +216,29 @@ const char *presetToString(Preset preset) {
212
216
}
213
217
#endif
214
218
219
+ #ifdef __linux__
220
+ bool isPrivileged () {
221
+ return getauxval (AT_SECURE);
222
+ }
223
+ #elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
224
+ bool isPrivileged () {
225
+ return issetugid ();
226
+ }
227
+ #elif _WIN32
228
+ bool isPrivileged () {
229
+ return false ;
230
+ }
231
+ #endif
232
+
215
233
} // namespace
216
234
217
235
BacktraceInitializer::BacktraceInitializer () {
218
236
const char *backtracing = swift::runtime::environment::SWIFT_BACKTRACE ();
219
237
220
- #if !_WIN32
221
238
// Force off for setuid processes.
222
- if (issetugid ()) {
239
+ if (isPrivileged ()) {
223
240
_swift_backtraceSettings.enabled = OnOffTty::Off;
224
241
}
225
- #endif
226
242
227
243
if (backtracing)
228
244
_swift_parseBacktracingSettings (backtracing);
@@ -246,25 +262,22 @@ BacktraceInitializer::BacktraceInitializer() {
246
262
_swift_backtraceSettings.enabled = OnOffTty::Off;
247
263
}
248
264
#else
249
- #if !_WIN32
250
- if (issetugid ()) {
251
- if (_swift_backtraceSettings.enabled != OnOffTty::Off) {
252
- // You'll only see this warning if you do e.g.
253
- //
254
- // SWIFT_BACKTRACE=enable=on /path/to/some/setuid/binary
255
- //
256
- // as opposed to
257
- //
258
- // /path/to/some/setuid/binary
259
- //
260
- // i.e. when you're trying to force matters.
261
- swift::warning (0 ,
262
- " swift runtime: backtrace-on-crash is not supported for "
263
- " setuid executables.\n " );
264
- _swift_backtraceSettings.enabled = OnOffTty::Off;
265
- }
265
+
266
+ if (isPrivileged () && _swift_backtraceSettings.enabled != OnOffTty::Off) {
267
+ // You'll only see this warning if you do e.g.
268
+ //
269
+ // SWIFT_BACKTRACE=enable=on /path/to/some/setuid/binary
270
+ //
271
+ // as opposed to
272
+ //
273
+ // /path/to/some/setuid/binary
274
+ //
275
+ // i.e. when you're trying to force matters.
276
+ swift::warning (0 ,
277
+ " swift runtime: backtrace-on-crash is not supported for "
278
+ " privileged executables.\n " );
279
+ _swift_backtraceSettings.enabled = OnOffTty::Off;
266
280
}
267
- #endif // !_WIN32
268
281
269
282
if (_swift_backtraceSettings.enabled == OnOffTty::TTY)
270
283
_swift_backtraceSettings.enabled =
0 commit comments