chiark / gitweb /
Prep v238: Uncomment now needed headers and unmask now needed functions in src/basic...
[elogind.git] / src / basic / process-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   This file is part of systemd.
6
7   Copyright 2010 Lennart Poettering
8
9   systemd is free software; you can redistribute it and/or modify it
10   under the terms of the GNU Lesser General Public License as published by
11   the Free Software Foundation; either version 2.1 of the License, or
12   (at your option) any later version.
13
14   systemd is distributed in the hope that it will be useful, but
15   WITHOUT ANY WARRANTY; without even the implied warranty of
16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   Lesser General Public License for more details.
18
19   You should have received a copy of the GNU Lesser General Public License
20   along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <alloca.h>
24 #include <errno.h>
25 #include <sched.h>
26 #include <signal.h>
27 #include <stdbool.h>
28 #include <stddef.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <sys/resource.h>
32 #include <sys/types.h>
33
34 #include "format-util.h"
35 //#include "ioprio.h"
36 #include "macro.h"
37 #include "time-util.h"
38
39 #define procfs_file_alloca(pid, field)                                  \
40         ({                                                              \
41                 pid_t _pid_ = (pid);                                    \
42                 const char *_r_;                                        \
43                 if (_pid_ == 0) {                                       \
44                         _r_ = ("/proc/self/" field);                    \
45                 } else {                                                \
46                         _r_ = alloca(STRLEN("/proc/") + DECIMAL_STR_MAX(pid_t) + 1 + sizeof(field)); \
47                         sprintf((char*) _r_, "/proc/"PID_FMT"/" field, _pid_);                       \
48                 }                                                       \
49                 _r_;                                                    \
50         })
51
52 int get_process_state(pid_t pid);
53 int get_process_comm(pid_t pid, char **name);
54 int get_process_cmdline(pid_t pid, size_t max_length, bool comm_fallback, char **line);
55 int get_process_exe(pid_t pid, char **name);
56 #if 0 /// UNNEEDED by elogind
57 int get_process_uid(pid_t pid, uid_t *uid);
58 int get_process_gid(pid_t pid, gid_t *gid);
59 int get_process_capeff(pid_t pid, char **capeff);
60 int get_process_cwd(pid_t pid, char **cwd);
61 int get_process_root(pid_t pid, char **root);
62 int get_process_environ(pid_t pid, char **environ);
63 int get_process_ppid(pid_t pid, pid_t *ppid);
64 #endif // 0
65
66 int wait_for_terminate(pid_t pid, siginfo_t *status);
67
68 typedef enum WaitFlags {
69         WAIT_LOG_ABNORMAL             = 1U << 0,
70         WAIT_LOG_NON_ZERO_EXIT_STATUS = 1U << 1,
71
72         /* A shortcut for requesting the most complete logging */
73         WAIT_LOG = WAIT_LOG_ABNORMAL|WAIT_LOG_NON_ZERO_EXIT_STATUS,
74 } WaitFlags;
75
76 int wait_for_terminate_and_check(const char *name, pid_t pid, WaitFlags flags);
77 int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout);
78 #if 0 /// UNNEEDED by elogind
79
80 void sigkill_wait(pid_t pid);
81 void sigkill_waitp(pid_t *pid);
82 #endif // 0
83 void sigterm_wait(pid_t pid);
84
85 int kill_and_sigcont(pid_t pid, int sig);
86
87 int rename_process(const char name[]);
88 int is_kernel_thread(pid_t pid);
89
90 int getenv_for_pid(pid_t pid, const char *field, char **_value);
91
92 bool pid_is_alive(pid_t pid);
93 bool pid_is_unwaited(pid_t pid);
94 #if 0 /// UNNEEDED by elogind
95 int pid_from_same_root_fs(pid_t pid);
96 #endif // 0
97
98 bool is_main_thread(void);
99
100 #if 0 /// UNNEEDED by elogind
101 noreturn void freeze(void);
102
103 bool oom_score_adjust_is_valid(int oa);
104 #endif // 0
105
106 #ifndef PERSONALITY_INVALID
107 /* personality(7) documents that 0xffffffffUL is used for querying the
108  * current personality, hence let's use that here as error
109  * indicator. */
110 #define PERSONALITY_INVALID 0xffffffffLU
111 #endif
112
113 #if 0 /// UNNEEDED by elogind
114 unsigned long personality_from_string(const char *p);
115 const char *personality_to_string(unsigned long);
116
117 int safe_personality(unsigned long p);
118 int opinionated_personality(unsigned long *ret);
119
120 int ioprio_class_to_string_alloc(int i, char **s);
121 int ioprio_class_from_string(const char *s);
122
123 const char *sigchld_code_to_string(int i) _const_;
124 int sigchld_code_from_string(const char *s) _pure_;
125 #endif // 0
126
127 int sched_policy_to_string_alloc(int i, char **s);
128 #if 0 /// UNNEEDED by elogind
129 int sched_policy_from_string(const char *s);
130 #endif // 0
131
132 static inline pid_t PTR_TO_PID(const void *p) {
133         return (pid_t) ((uintptr_t) p);
134 }
135
136 static inline void* PID_TO_PTR(pid_t pid) {
137         return (void*) ((uintptr_t) pid);
138 }
139
140 void valgrind_summary_hack(void);
141
142 int pid_compare_func(const void *a, const void *b);
143
144 #if 0 /// UNNEEDED by elogind
145 static inline bool nice_is_valid(int n) {
146         return n >= PRIO_MIN && n < PRIO_MAX;
147 }
148 #endif // 0
149
150 static inline bool sched_policy_is_valid(int i) {
151         return IN_SET(i, SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_FIFO, SCHED_RR);
152 }
153
154 #if 0 /// UNNEEDED by elogind
155 static inline bool sched_priority_is_valid(int i) {
156         return i >= 0 && i <= sched_get_priority_max(SCHED_RR);
157 }
158
159 static inline bool ioprio_class_is_valid(int i) {
160         return IN_SET(i, IOPRIO_CLASS_NONE, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE);
161 }
162
163 static inline bool ioprio_priority_is_valid(int i) {
164         return i >= 0 && i < IOPRIO_BE_NR;
165 }
166 #endif // 0
167
168 static inline bool pid_is_valid(pid_t p) {
169         return p > 0;
170 }
171
172 static inline int sched_policy_to_string_alloc_with_check(int n, char **s) {
173         if (!sched_policy_is_valid(n))
174                 return -EINVAL;
175
176         return sched_policy_to_string_alloc(n, s);
177 }
178
179 #if 0 /// UNNEEDED by elogind
180 int ioprio_parse_priority(const char *s, int *ret);
181 #endif // 0
182
183 pid_t getpid_cached(void);
184 void reset_cached_pid(void);
185
186 int must_be_root(void);
187
188 typedef enum ForkFlags {
189         FORK_RESET_SIGNALS = 1U << 0,
190         FORK_CLOSE_ALL_FDS = 1U << 1,
191         FORK_DEATHSIG      = 1U << 2,
192         FORK_NULL_STDIO    = 1U << 3,
193         FORK_REOPEN_LOG    = 1U << 4,
194         FORK_LOG           = 1U << 5,
195         FORK_WAIT          = 1U << 6,
196         FORK_NEW_MOUNTNS   = 1U << 7,
197 } ForkFlags;
198
199 int safe_fork_full(const char *name, const int except_fds[], size_t n_except_fds, ForkFlags flags, pid_t *ret_pid);
200
201 static inline int safe_fork(const char *name, ForkFlags flags, pid_t *ret_pid) {
202         return safe_fork_full(name, NULL, 0, flags, ret_pid);
203 }
204
205 int fork_agent(const char *name, const int except[], unsigned n_except, pid_t *pid, const char *path, ...);
206
207 #if SIZEOF_PID_T == 4
208 /* The highest possibly (theoretic) pid_t value on this architecture. */
209 #define PID_T_MAX ((pid_t) INT32_MAX)
210 /* The maximum number of concurrent processes Linux allows on this architecture, as well as the highest valid PID value
211  * the kernel will potentially assign. This reflects a value compiled into the kernel (PID_MAX_LIMIT), and sets the
212  * upper boundary on what may be written to the /proc/sys/kernel/pid_max sysctl (but do note that the sysctl is off by
213  * 1, since PID 0 can never exist and there can hence only be one process less than the limit would suggest). Since
214  * these values are documented in proc(5) we feel quite confident that they are stable enough for the near future at
215  * least to define them here too. */
216 #define TASKS_MAX 4194303U
217 #elif SIZEOF_PID_T == 2
218 #define PID_T_MAX ((pid_t) INT16_MAX)
219 #define TASKS_MAX 32767U
220 #else
221 #error "Unknown pid_t size"
222 #endif
223
224 assert_cc(TASKS_MAX <= (unsigned long) PID_T_MAX)