2022-09-15 00:30:59 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <optional>
|
2022-09-15 20:18:26 +01:00
|
|
|
#include <memory>
|
2022-10-05 20:58:25 +01:00
|
|
|
#include <sys/types.h>
|
2022-09-15 00:30:59 +01:00
|
|
|
#include <proc/readproc.h>
|
|
|
|
|
|
2023-08-13 15:47:51 +01:00
|
|
|
namespace Services
|
2022-09-15 00:30:59 +01:00
|
|
|
{
|
2022-12-26 21:35:24 +00:00
|
|
|
class ProcessService
|
2022-09-15 00:30:59 +01:00
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Returns the process ID of the current process.
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
static ::pid_t getProcessId();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the process ID of the current process's parent.
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
static ::pid_t getParentProcessId();
|
|
|
|
|
|
2022-10-05 20:58:25 +01:00
|
|
|
/**
|
|
|
|
|
* Returns the effective user ID of the given process.
|
|
|
|
|
*
|
|
|
|
|
* @param processId
|
|
|
|
|
* If not provided, this function will use the current process ID.
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
static ::uid_t getEffectiveUserId(std::optional<::pid_t> processId = std::nullopt);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if the given process is running as root.
|
|
|
|
|
*
|
|
|
|
|
* @param processId
|
|
|
|
|
* If not provided, this function will perform the check against the current process.
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
|
|
|
|
static bool isRunningAsRoot(std::optional<::pid_t> processId = std::nullopt);
|
|
|
|
|
|
2022-09-15 00:30:59 +01:00
|
|
|
/**
|
|
|
|
|
* Returns true if the given process is managed by CLion.
|
|
|
|
|
*
|
2022-10-30 12:29:35 +00:00
|
|
|
* @param processId
|
2022-09-15 20:18:26 +01:00
|
|
|
* If not provided, this function will perform the check against the current process.
|
|
|
|
|
*
|
2022-09-15 00:30:59 +01:00
|
|
|
* @return
|
|
|
|
|
*/
|
2022-10-30 12:29:35 +00:00
|
|
|
static bool isManagedByClion(std::optional<::pid_t> processId = std::nullopt);
|
2022-09-15 20:18:26 +01:00
|
|
|
|
2022-09-15 00:30:59 +01:00
|
|
|
private:
|
2022-10-01 21:01:37 +01:00
|
|
|
using Proc = std::unique_ptr<::proc_t, decltype(&::freeproc)>;
|
|
|
|
|
static Proc getProcessInfo(::pid_t processId);
|
2022-09-15 00:30:59 +01:00
|
|
|
};
|
|
|
|
|
}
|