Blame shared/systemd/src/basic/process-util.h

Packit Service 87a54e
/* SPDX-License-Identifier: LGPL-2.1-or-later */
Packit 5756e2
#pragma once
Packit 5756e2
Packit 5756e2
#include <errno.h>
Packit 5756e2
#include <sched.h>
Packit 5756e2
#include <signal.h>
Packit 5756e2
#include <stdbool.h>
Packit 5756e2
#include <stddef.h>
Packit 5756e2
#include <stdio.h>
Packit 5756e2
#include <string.h>
Packit 5756e2
#include <sys/resource.h>
Packit 5756e2
#include <sys/types.h>
Packit 5756e2
Packit 5756e2
#include "alloc-util.h"
Packit 5756e2
#include "format-util.h"
Packit 5756e2
#include "ioprio.h"
Packit 5756e2
#include "macro.h"
Packit 5756e2
#include "time-util.h"
Packit 5756e2
Packit 5756e2
#define procfs_file_alloca(pid, field)                                  \
Packit 5756e2
        ({                                                              \
Packit 5756e2
                pid_t _pid_ = (pid);                                    \
Packit 5756e2
                const char *_field_ = (field);                          \
Packit 5756e2
                char *_r_;                                              \
Packit 5756e2
                if (_pid_ == 0) {                                       \
Packit 5756e2
                        _r_ = newa(char, STRLEN("/proc/self/") + strlen(_field_) + 1); \
Packit 5756e2
                        strcpy(stpcpy(_r_, "/proc/self/"), _field_);    \
Packit 5756e2
                } else {                                                \
Packit 5756e2
                        _r_ = newa(char, STRLEN("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + strlen(_field_) + 1); \
Packit 5756e2
                        sprintf(_r_, "/proc/" PID_FMT "/%s", _pid_, _field_); \
Packit 5756e2
                }                                                       \
Packit 5756e2
                (const char*) _r_;                                      \
Packit 5756e2
        })
Packit 5756e2
Packit 5756e2
typedef enum ProcessCmdlineFlags {
Packit 5756e2
        PROCESS_CMDLINE_COMM_FALLBACK = 1 << 0,
Packit 5756e2
        PROCESS_CMDLINE_USE_LOCALE    = 1 << 1,
Packit 5756e2
} ProcessCmdlineFlags;
Packit 5756e2
Packit 5756e2
int get_process_comm(pid_t pid, char **name);
Packit 5756e2
int get_process_cmdline(pid_t pid, size_t max_columns, ProcessCmdlineFlags flags, char **line);
Packit 5756e2
int get_process_exe(pid_t pid, char **name);
Packit 5756e2
int get_process_uid(pid_t pid, uid_t *uid);
Packit 5756e2
int get_process_gid(pid_t pid, gid_t *gid);
Packit 5756e2
int get_process_capeff(pid_t pid, char **capeff);
Packit 5756e2
int get_process_cwd(pid_t pid, char **cwd);
Packit 5756e2
int get_process_root(pid_t pid, char **root);
Packit 5756e2
int get_process_environ(pid_t pid, char **environ);
Packit 5756e2
int get_process_ppid(pid_t pid, pid_t *ppid);
Packit 5756e2
int get_process_umask(pid_t pid, mode_t *umask);
Packit 5756e2
Packit 5756e2
int wait_for_terminate(pid_t pid, siginfo_t *status);
Packit 5756e2
Packit 5756e2
typedef enum WaitFlags {
Packit 5756e2
        WAIT_LOG_ABNORMAL             = 1 << 0,
Packit 5756e2
        WAIT_LOG_NON_ZERO_EXIT_STATUS = 1 << 1,
Packit 5756e2
Packit 5756e2
        /* A shortcut for requesting the most complete logging */
Packit 5756e2
        WAIT_LOG = WAIT_LOG_ABNORMAL|WAIT_LOG_NON_ZERO_EXIT_STATUS,
Packit 5756e2
} WaitFlags;
Packit 5756e2
Packit 5756e2
int wait_for_terminate_and_check(const char *name, pid_t pid, WaitFlags flags);
Packit 5756e2
int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout);
Packit 5756e2
Packit 5756e2
void sigkill_wait(pid_t pid);
Packit 5756e2
void sigkill_waitp(pid_t *pid);
Packit 5756e2
void sigterm_wait(pid_t pid);
Packit 5756e2
Packit 5756e2
int kill_and_sigcont(pid_t pid, int sig);
Packit 5756e2
Packit 5756e2
int rename_process(const char name[]);
Packit 5756e2
int is_kernel_thread(pid_t pid);
Packit 5756e2
Packit 5756e2
int getenv_for_pid(pid_t pid, const char *field, char **_value);
Packit 5756e2
Packit 5756e2
bool pid_is_alive(pid_t pid);
Packit 5756e2
bool pid_is_unwaited(pid_t pid);
Packit 5756e2
int pid_is_my_child(pid_t pid);
Packit 5756e2
int pid_from_same_root_fs(pid_t pid);
Packit 5756e2
Packit 5756e2
bool is_main_thread(void);
Packit 5756e2
Packit 5756e2
_noreturn_ void freeze(void);
Packit 5756e2
Packit 5756e2
bool oom_score_adjust_is_valid(int oa);
Packit 5756e2
Packit 5756e2
#ifndef PERSONALITY_INVALID
Packit 5756e2
/* personality(7) documents that 0xffffffffUL is used for querying the
Packit 5756e2
 * current personality, hence let's use that here as error
Packit 5756e2
 * indicator. */
Packit 5756e2
#define PERSONALITY_INVALID 0xffffffffLU
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
unsigned long personality_from_string(const char *p);
Packit 5756e2
const char *personality_to_string(unsigned long);
Packit 5756e2
Packit 5756e2
int safe_personality(unsigned long p);
Packit 5756e2
int opinionated_personality(unsigned long *ret);
Packit 5756e2
Packit 5756e2
int ioprio_class_to_string_alloc(int i, char **s);
Packit 5756e2
int ioprio_class_from_string(const char *s);
Packit 5756e2
Packit 5756e2
const char *sigchld_code_to_string(int i) _const_;
Packit 5756e2
int sigchld_code_from_string(const char *s) _pure_;
Packit 5756e2
Packit 5756e2
int sched_policy_to_string_alloc(int i, char **s);
Packit 5756e2
int sched_policy_from_string(const char *s);
Packit 5756e2
Packit 5756e2
static inline pid_t PTR_TO_PID(const void *p) {
Packit 5756e2
        return (pid_t) ((uintptr_t) p);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline void* PID_TO_PTR(pid_t pid) {
Packit 5756e2
        return (void*) ((uintptr_t) pid);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
void valgrind_summary_hack(void);
Packit 5756e2
Packit 5756e2
int pid_compare_func(const pid_t *a, const pid_t *b);
Packit 5756e2
Packit 5756e2
#if 0 /* NM_IGNORED */
Packit 5756e2
static inline bool nice_is_valid(int n) {
Packit 5756e2
        return n >= PRIO_MIN && n < PRIO_MAX;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline bool sched_policy_is_valid(int i) {
Packit 5756e2
        return IN_SET(i, SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_FIFO, SCHED_RR);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline bool sched_priority_is_valid(int i) {
Packit 5756e2
        return i >= 0 && i <= sched_get_priority_max(SCHED_RR);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline bool ioprio_class_is_valid(int i) {
Packit 5756e2
        return IN_SET(i, IOPRIO_CLASS_NONE, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline bool ioprio_priority_is_valid(int i) {
Packit 5756e2
        return i >= 0 && i < IOPRIO_BE_NR;
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline bool pid_is_valid(pid_t p) {
Packit 5756e2
        return p > 0;
Packit 5756e2
}
Packit 5756e2
#endif /* NM_IGNORED */
Packit 5756e2
Packit 5756e2
int ioprio_parse_priority(const char *s, int *ret);
Packit 5756e2
Packit 5756e2
pid_t getpid_cached(void);
Packit 5756e2
void reset_cached_pid(void);
Packit 5756e2
Packit 5756e2
int must_be_root(void);
Packit 5756e2
Packit 5756e2
typedef enum ForkFlags {
Packit 5756e2
        FORK_RESET_SIGNALS      = 1 <<  0, /* Reset all signal handlers and signal mask */
Packit 5756e2
        FORK_CLOSE_ALL_FDS      = 1 <<  1, /* Close all open file descriptors in the child, except for 0,1,2 */
Packit 5756e2
        FORK_DEATHSIG           = 1 <<  2, /* Set PR_DEATHSIG in the child to SIGTERM */
Packit 5756e2
        FORK_DEATHSIG_SIGINT    = 1 <<  3, /* Set PR_DEATHSIG in the child to SIGINT */
Packit 5756e2
        FORK_NULL_STDIO         = 1 <<  4, /* Connect 0,1,2 to /dev/null */
Packit 5756e2
        FORK_REOPEN_LOG         = 1 <<  5, /* Reopen log connection */
Packit 5756e2
        FORK_LOG                = 1 <<  6, /* Log above LOG_DEBUG log level about failures */
Packit 5756e2
        FORK_WAIT               = 1 <<  7, /* Wait until child exited */
Packit 5756e2
        FORK_NEW_MOUNTNS        = 1 <<  8, /* Run child in its own mount namespace */
Packit 5756e2
        FORK_MOUNTNS_SLAVE      = 1 <<  9, /* Make child's mount namespace MS_SLAVE */
Packit 5756e2
        FORK_RLIMIT_NOFILE_SAFE = 1 << 10, /* Set RLIMIT_NOFILE soft limit to 1K for select() compat */
Packit 5756e2
        FORK_STDOUT_TO_STDERR   = 1 << 11, /* Make stdout a copy of stderr */
Packit 5756e2
} ForkFlags;
Packit 5756e2
Packit 5756e2
int safe_fork_full(const char *name, const int except_fds[], size_t n_except_fds, ForkFlags flags, pid_t *ret_pid);
Packit 5756e2
Packit 5756e2
static inline int safe_fork(const char *name, ForkFlags flags, pid_t *ret_pid) {
Packit 5756e2
        return safe_fork_full(name, NULL, 0, flags, ret_pid);
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
int namespace_fork(const char *outer_name, const char *inner_name, const int except_fds[], size_t n_except_fds, ForkFlags flags, int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int root_fd, pid_t *ret_pid);
Packit 5756e2
Packit 5756e2
int fork_agent(const char *name, const int except[], size_t n_except, pid_t *pid, const char *path, ...) _sentinel_;
Packit 5756e2
Packit 5756e2
int set_oom_score_adjust(int value);
Packit 5756e2
Packit 5756e2
/* The highest possibly (theoretic) pid_t value on this architecture. */
Packit 5756e2
#define PID_T_MAX ((pid_t) INT32_MAX)
Packit 5756e2
/* The maximum number of concurrent processes Linux allows on this architecture, as well as the highest valid PID value
Packit 5756e2
 * the kernel will potentially assign. This reflects a value compiled into the kernel (PID_MAX_LIMIT), and sets the
Packit 5756e2
 * upper boundary on what may be written to the /proc/sys/kernel/pid_max sysctl (but do note that the sysctl is off by
Packit 5756e2
 * 1, since PID 0 can never exist and there can hence only be one process less than the limit would suggest). Since
Packit 5756e2
 * these values are documented in proc(5) we feel quite confident that they are stable enough for the near future at
Packit 5756e2
 * least to define them here too. */
Packit 5756e2
#define TASKS_MAX 4194303U
Packit 5756e2
Packit 5756e2
assert_cc(TASKS_MAX <= (unsigned long) PID_T_MAX);
Packit 5756e2
Packit 5756e2
/* Like TAKE_PTR() but for child PIDs, resetting them to 0 */
Packit 5756e2
#define TAKE_PID(pid)                           \
Packit 5756e2
        ({                                      \
Packit 5756e2
                pid_t _pid_ = (pid);            \
Packit 5756e2
                (pid) = 0;                      \
Packit 5756e2
                _pid_;                          \
Packit 5756e2
        })
Packit 5756e2
Packit 5756e2
int pidfd_get_pid(int fd, pid_t *ret);
Packit 5756e2
Packit 5756e2
int setpriority_closest(int priority);