chiark / gitweb /
82b1de03c8c8a54b853d8002e1f4f67c4b9f0151
[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 void sigterm_wait(pid_t pid);
83
84 int kill_and_sigcont(pid_t pid, int sig);
85
86 int rename_process(const char name[]);
87 #endif // 0
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
126 int sched_policy_to_string_alloc(int i, char **s);
127 int sched_policy_from_string(const char *s);
128 #endif // 0
129
130 static inline pid_t PTR_TO_PID(const void *p) {
131         return (pid_t) ((uintptr_t) p);
132 }
133
134 static inline void* PID_TO_PTR(pid_t pid) {
135         return (void*) ((uintptr_t) pid);
136 }
137
138 void valgrind_summary_hack(void);
139
140 int pid_compare_func(const void *a, const void *b);
141
142 #if 0 /// UNNEEDED by elogind
143 static inline bool nice_is_valid(int n) {
144         return n >= PRIO_MIN && n < PRIO_MAX;
145 }
146
147 static inline bool sched_policy_is_valid(int i) {
148         return IN_SET(i, SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_FIFO, SCHED_RR);
149 }
150
151 static inline bool sched_priority_is_valid(int i) {
152         return i >= 0 && i <= sched_get_priority_max(SCHED_RR);
153 }
154
155 static inline bool ioprio_class_is_valid(int i) {
156         return IN_SET(i, IOPRIO_CLASS_NONE, IOPRIO_CLASS_RT, IOPRIO_CLASS_BE, IOPRIO_CLASS_IDLE);
157 }
158
159 static inline bool ioprio_priority_is_valid(int i) {
160         return i >= 0 && i < IOPRIO_BE_NR;
161 }
162 #endif // 0
163
164 static inline bool pid_is_valid(pid_t p) {
165         return p > 0;
166 }
167
168 static inline int sched_policy_to_string_alloc_with_check(int n, char **s) {
169         if (!sched_policy_is_valid(n))
170                 return -EINVAL;
171
172         return sched_policy_to_string_alloc(n, s);
173 }
174
175 #if 0 /// UNNEEDED by elogind
176 int ioprio_parse_priority(const char *s, int *ret);
177 #endif // 0
178
179 pid_t getpid_cached(void);
180 void reset_cached_pid(void);
181
182 int must_be_root(void);
183
184 typedef enum ForkFlags {
185         FORK_RESET_SIGNALS = 1U << 0,
186         FORK_CLOSE_ALL_FDS = 1U << 1,
187         FORK_DEATHSIG      = 1U << 2,
188         FORK_NULL_STDIO    = 1U << 3,
189         FORK_REOPEN_LOG    = 1U << 4,
190         FORK_LOG           = 1U << 5,
191         FORK_WAIT          = 1U << 6,
192         FORK_NEW_MOUNTNS   = 1U << 7,
193 } ForkFlags;
194
195 int safe_fork_full(const char *name, const int except_fds[], size_t n_except_fds, ForkFlags flags, pid_t *ret_pid);
196
197 static inline int safe_fork(const char *name, ForkFlags flags, pid_t *ret_pid) {
198         return safe_fork_full(name, NULL, 0, flags, ret_pid);
199 }
200
201 int fork_agent(const char *name, const int except[], unsigned n_except, pid_t *pid, const char *path, ...);
202
203 #if SIZEOF_PID_T == 4
204 /* The highest possibly (theoretic) pid_t value on this architecture. */
205 #define PID_T_MAX ((pid_t) INT32_MAX)
206 /* The maximum number of concurrent processes Linux allows on this architecture, as well as the highest valid PID value
207  * the kernel will potentially assign. This reflects a value compiled into the kernel (PID_MAX_LIMIT), and sets the
208  * upper boundary on what may be written to the /proc/sys/kernel/pid_max sysctl (but do note that the sysctl is off by
209  * 1, since PID 0 can never exist and there can hence only be one process less than the limit would suggest). Since
210  * these values are documented in proc(5) we feel quite confident that they are stable enough for the near future at
211  * least to define them here too. */
212 #define TASKS_MAX 4194303U
213 #elif SIZEOF_PID_T == 2
214 #define PID_T_MAX ((pid_t) INT16_MAX)
215 #define TASKS_MAX 32767U
216 #else
217 #error "Unknown pid_t size"
218 #endif
219
220 assert_cc(TASKS_MAX <= (unsigned long) PID_T_MAX)