Packages
backplane_agent_runtime
1.10.71
1.10.107
1.10.106
1.10.105
1.10.104
1.10.103
1.10.102
1.10.101
1.10.101-beta.1
1.10.100
1.10.99
1.10.98
1.10.97
1.10.96
1.10.95
1.10.94
1.10.93
1.10.92
1.10.91
1.10.90
1.10.89
1.10.88
1.10.87
1.10.86
1.10.85
1.10.84
1.10.83
1.10.82
1.10.81
1.10.80
1.10.79
1.10.78
1.10.77
1.10.76
1.10.75
1.10.74
1.10.73
1.10.72
1.10.71
1.10.70
1.10.69
1.10.68
1.10.67
1.10.66
1.10.65
1.10.63
1.10.62
1.10.61
1.10.60
1.10.57
1.10.56
1.10.55
1.10.54
1.10.53
1.10.52
1.10.51
1.10.50
1.10.49
1.10.48
1.10.47
1.10.46
1.10.45
1.10.44
1.10.43
1.10.42
1.10.41
1.10.40
1.10.39
1.10.38
1.10.37
1.10.36
1.10.35
1.10.34
1.10.32
1.10.31
1.10.30
1.10.29
1.10.28
1.10.27
1.10.26
1.10.25
1.10.24
1.10.23
1.10.22
1.10.21
1.10.20
1.10.19
1.10.18
1.10.17
1.10.16
1.10.15
1.10.14
1.10.13
1.10.12
1.10.11
1.10.10
1.10.9
1.10.8
1.10.7
1.10.6
1.10.5
1.10.4
1.10.3
1.10.2
1.10.1
1.10.0
1.9.0
1.8.3
1.8.2
1.8.1
1.8.0
1.7.9
1.7.8
1.7.7
1.7.6
1.7.5
1.7.4
1.7.3
1.7.2
1.7.1
1.7.0
1.6.0
Embedded Elixir/OTP runtime and opt-in tools for bounded independent agents.
Current section
Files
Jump to
Current section
Files
c_src/local_command_session.c
#define _POSIX_C_SOURCE 200809L
#ifdef __APPLE__
#define _DARWIN_C_SOURCE
#endif
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#ifdef __APPLE__
#include <libproc.h>
#include <sys/sysctl.h>
#endif
/* No shell interpretation: arguments pass unchanged into the blocked launcher. */
static int launch(char **argv) {
pid_t child = fork();
if (child < 0) return 125;
if (child == 0) {
if (setsid() < 0) _exit(125);
execv(argv[0], argv);
_exit(127);
}
int status;
while (waitpid(child, &status, 0) < 0) {
if (errno != EINTR) return 125;
}
if (WIFEXITED(status)) return WEXITSTATUS(status);
return WIFSIGNALED(status) ? 128 + WTERMSIG(status) : 125;
}
static int positive_pid(const char *text, pid_t *pid) {
char *end;
errno = 0;
long value = strtol(text, &end, 10);
if (errno || !*text || *end || value <= 0 || value > INT_MAX) return -1;
*pid = (pid_t)value;
return 0;
}
static int exists(pid_t target) {
if (kill(target, 0) == 0 || errno == EPERM) {
puts("present");
return 0;
}
if (errno == ESRCH) {
puts("absent");
return 0;
}
return 1;
}
static int identity(pid_t pid) {
#ifdef __APPLE__
struct proc_bsdinfo info;
int size = proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, &info, sizeof(info));
if (size != (int)sizeof(info)) return 1;
pid_t session = getsid(pid);
if (session < 0 || info.pbi_pid != (unsigned int)pid) return 1;
printf("%u %u %ld %llu:%llu\n", info.pbi_ppid, info.pbi_pgid, (long)session,
(unsigned long long)info.pbi_start_tvsec, (unsigned long long)info.pbi_start_tvusec);
return 0;
#else
(void)pid;
return 1;
#endif
}
/* Code Mode identity: start time plus Z for zombies, which libproc cannot read. */
static int process(pid_t pid) {
#ifdef __APPLE__
struct kinfo_proc info;
size_t size = sizeof(info);
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, pid};
if (sysctl(mib, 4, &info, &size, NULL, 0) != 0) return 1;
if (size == 0) {
puts("absent");
return 0;
}
if (size != sizeof(info) || info.kp_proc.p_pid != pid) return 1;
printf("%llu:%llu %s\n", (unsigned long long)info.kp_proc.p_starttime.tv_sec,
(unsigned long long)info.kp_proc.p_starttime.tv_usec,
info.kp_proc.p_stat == SZOMB ? "Z" : "R");
return 0;
#else
(void)pid;
return 1;
#endif
}
int main(int argc, char **argv) {
if (argc == 2 && strcmp(argv[1], "--probe") == 0) {
puts("BACKPLANE_SESSION 1");
return 0;
}
if (argc >= 3 && strcmp(argv[1], "--run") == 0) return launch(&argv[2]);
pid_t pid;
if (argc != 3 || positive_pid(argv[2], &pid) != 0) return 2;
if (strcmp(argv[1], "--term") == 0) return kill(-pid, SIGTERM) == 0 ? 0 : 1;
if (strcmp(argv[1], "--kill") == 0) return kill(-pid, SIGKILL) == 0 ? 0 : 1;
if (strcmp(argv[1], "--identity") == 0) return identity(pid);
if (strcmp(argv[1], "--process") == 0) return process(pid);
if (strcmp(argv[1], "--alive") == 0) return exists(pid);
if (strcmp(argv[1], "--group-exists") == 0) return exists(-pid);
return 2;
}