xref: /aosp_15_r20/external/skia/src/utils/SkGetExecutablePath_linux.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
1 /*
2  * Copyright 2022 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "src/utils/SkGetExecutablePath.h"
9 
10 #include <linux/limits.h>
11 #include <sys/types.h>
12 #include <unistd.h>
13 #include <cstddef>
14 
15 // Note that /proc/self/exe is Linux-specific; this won't work on other UNIX systems.
16 
SkGetExecutablePath()17 std::string SkGetExecutablePath() {
18     std::string result(PATH_MAX, '\0');
19     ssize_t len = readlink("/proc/self/exe", result.data(), result.size() - 1);
20     if (len < 0 || static_cast<size_t>(len) >= PATH_MAX - 1) {
21         result.clear();
22     } else {
23         result.resize(len);
24     }
25     return result;
26 }
27