2026-02-23 13:22:21 -06:00
|
|
|
/*
|
|
|
|
|
ISC License
|
|
|
|
|
|
|
|
|
|
Copyright (c) 2026, Antonio SJ Musumeci <trapexit@spawn.link>
|
|
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
|
purpose with or without fee is hereby granted, provided that the above
|
|
|
|
|
copyright notice and this permission notice appear in all copies.
|
|
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
|
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
|
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
|
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
|
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-02-11 12:11:45 -05:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
|
#endif
|
|
|
|
|
|
2025-06-05 23:47:42 -05:00
|
|
|
#include "procfs.hpp"
|
2023-02-11 12:11:45 -05:00
|
|
|
|
|
|
|
|
#include "errno.hpp"
|
|
|
|
|
#include "fs_close.hpp"
|
|
|
|
|
#include "fs_open.hpp"
|
|
|
|
|
#include "fs_openat.hpp"
|
|
|
|
|
#include "fs_read.hpp"
|
|
|
|
|
|
2025-05-08 00:00:53 -05:00
|
|
|
#include "fmt/core.h"
|
|
|
|
|
|
|
|
|
|
#include <array>
|
2025-06-05 23:47:42 -05:00
|
|
|
#include <cassert>
|
2025-05-08 00:00:53 -05:00
|
|
|
|
2023-02-11 12:11:45 -05:00
|
|
|
#include <pthread.h>
|
|
|
|
|
|
|
|
|
|
static int g_PROCFS_DIR_FD = -1;
|
2025-06-05 23:47:42 -05:00
|
|
|
namespace procfs { int PROC_SELF_FD_FD = -1; }
|
|
|
|
|
static constexpr const char PROCFS_PATH[] = "/proc";
|
2023-02-11 12:11:45 -05:00
|
|
|
|
|
|
|
|
int
|
|
|
|
|
procfs::init()
|
|
|
|
|
{
|
2025-07-25 17:53:48 -05:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
|
|
if(g_PROCFS_DIR_FD >= 0)
|
2023-02-11 12:11:45 -05:00
|
|
|
return 0;
|
|
|
|
|
|
2025-07-25 17:53:48 -05:00
|
|
|
rv = fs::open(PROCFS_PATH,O_PATH|O_DIRECTORY);
|
|
|
|
|
if(rv < 0)
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
|
|
g_PROCFS_DIR_FD = rv;
|
2023-02-11 12:11:45 -05:00
|
|
|
|
2025-06-05 23:47:42 -05:00
|
|
|
#if defined(__linux__)
|
2025-11-01 18:04:28 -05:00
|
|
|
// This is critical to the function of the app. abort if failed to
|
|
|
|
|
// open.
|
|
|
|
|
static constexpr const char PROC_SELF_FD[] = "/proc/self/fd";
|
|
|
|
|
|
|
|
|
|
procfs::PROC_SELF_FD_FD = fs::open(PROC_SELF_FD,O_PATH|O_DIRECTORY);
|
|
|
|
|
assert(procfs::PROC_SELF_FD_FD >= 0);
|
2025-06-05 23:47:42 -05:00
|
|
|
#endif
|
|
|
|
|
|
2023-02-11 12:11:45 -05:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string
|
|
|
|
|
procfs::get_name(const int tid_)
|
|
|
|
|
{
|
|
|
|
|
int fd;
|
|
|
|
|
int rv;
|
2025-05-08 00:00:53 -05:00
|
|
|
std::array<char,256> commpath;
|
|
|
|
|
fmt::format_to_n_result<char*> frv;
|
2023-02-11 12:11:45 -05:00
|
|
|
|
2025-05-08 00:00:53 -05:00
|
|
|
frv = fmt::format_to_n(commpath.data(),commpath.size()-1,"{}/comm",tid_);
|
|
|
|
|
frv.out[0] = '\0';
|
2023-02-11 12:11:45 -05:00
|
|
|
|
2025-05-08 00:00:53 -05:00
|
|
|
fd = fs::openat(g_PROCFS_DIR_FD,commpath.data(),O_RDONLY);
|
2023-02-11 12:11:45 -05:00
|
|
|
if(fd < 0)
|
|
|
|
|
return {};
|
|
|
|
|
|
2025-05-08 00:00:53 -05:00
|
|
|
rv = fs::read(fd,commpath.data(),commpath.size());
|
2025-07-25 17:53:48 -05:00
|
|
|
if(rv < 0)
|
2023-02-11 12:11:45 -05:00
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
// Overwrite the newline with NUL
|
|
|
|
|
commpath[rv-1] = '\0';
|
|
|
|
|
|
|
|
|
|
fs::close(fd);
|
|
|
|
|
|
2025-05-08 00:00:53 -05:00
|
|
|
return commpath.data();
|
2023-02-11 12:11:45 -05:00
|
|
|
}
|