blob: da5701bbcea1ed7693c0c5b8b4353d74291c38bc [file] [log] [blame]
John Stiles964d8f12022-10-26 13:52:03 -04001/*
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 "tools/SkGetExecutablePath.h"
9#include <cstddef>
10#include <linux/limits.h>
11#include <sys/types.h>
12#include <unistd.h>
13
14// Note that /proc/self/exe is Linux-specific; this won't work on other UNIX systems.
15
John Stilesf04b8272022-11-04 13:58:26 -040016std::string SkGetExecutablePath() {
17 std::string result(PATH_MAX, '\0');
John Stiles1e64fb92022-10-27 14:02:39 -040018 ssize_t len = readlink("/proc/self/exe", result.data(), result.size() - 1);
John Stiles964d8f12022-10-26 13:52:03 -040019 if (len < 0 || static_cast<size_t>(len) >= PATH_MAX - 1) {
John Stilesf04b8272022-11-04 13:58:26 -040020 result.clear();
John Stiles964d8f12022-10-26 13:52:03 -040021 } else {
22 result.resize(len);
23 }
24 return result;
25}