1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
|
#ifndef __INCUS_PROCESS_UTILS_H
#define __INCUS_PROCESS_UTILS_H
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <linux/sched.h>
#include <sched.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include <unistd.h>
#include "compiler.h"
#include "memory_utils.h"
#include "syscall_numbers.h"
#ifndef PIDFD_THREAD
#define PIDFD_THREAD O_EXCL
#endif
static inline int incus_pidfd_open(pid_t pid, unsigned int flags)
{
return syscall(__NR_pidfd_open, pid, flags);
}
static inline int incus_pidfd_send_signal(int pidfd, int sig, siginfo_t *info,
unsigned int flags)
{
return syscall(__NR_pidfd_send_signal, pidfd, sig, info, flags);
}
static inline bool process_still_alive(int pidfd)
{
return incus_pidfd_send_signal(pidfd, 0, NULL, 0) == 0;
}
static inline int wait_for_pid(pid_t pid)
{
int status, ret;
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
if (errno == EINTR)
goto again;
return -1;
}
if (ret != pid)
goto again;
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
return -1;
return 0;
}
static inline int wait_for_pid_status_nointr(pid_t pid)
{
int status, ret;
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
if (errno == EINTR)
goto again;
return -1;
}
if (ret != pid)
goto again;
return status;
}
static inline int append_null_to_list(void ***list)
{
int newentry = 0;
void **new_list;
if (*list)
for (; (*list)[newentry]; newentry++)
;
new_list = realloc(*list, (newentry + 2) * sizeof(void **));
if (!new_list)
return ret_errno(ENOMEM);
*list = new_list;
(*list)[newentry + 1] = NULL;
return newentry;
}
static inline int push_vargs(char ***list, char *entry)
{
__do_free char *copy = NULL;
int newentry;
copy = strdup(entry);
if (!copy)
return ret_errno(ENOMEM);
newentry = append_null_to_list((void ***)list);
if (newentry < 0)
return newentry;
(*list)[newentry] = move_ptr(copy);
return 0;
}
#endif /* __INCUS_PROCESS_UTILS_H */
|