Debian Codegraph @

psx/libpsx.h

raw
1 #ifndef LIBPSX_H
2 #define LIBPSX_H
3
4 /*
5 * Copyright (c) 2025 Andrew G. Morgan <[email protected]>
6 *
7 * These header definitions are private to the PSX mechanism. External
8 * API references are to be found in <sys/psx_syscall.h>
9 *
10 * Since we no longer (libcap-2.72) operate at the pthreads
11 * abstraction, we need our own mutex etc implementation.
12 */
13
14 typedef unsigned char psx_mutex_t;
15 #define _psx_mu_blocked(x) \
16 __atomic_test_and_set((void *)(x), __ATOMIC_SEQ_CST)
17 #define _psx_mu_lock(x) \
18 while (_psx_mu_blocked(x)) sched_yield()
19 #define _psx_mu_unlock(x) \
20 __atomic_clear((void *)(x), __ATOMIC_SEQ_CST)
21 #define _psx_mu_unlock_return(x, y) \
22 do { _psx_mu_unlock(x); return (y); } while (0)
23 #define _psx_mu_cond_wait(x) \
24 do { \
25 _psx_mu_unlock(x); \
26 sched_yield(); \
27 _psx_mu_lock(x); \
28 } while (0)
29
30 /* Not reliably defined by *libc so, alias the direct syscall. */
31 #define _psx_gettid() syscall(SYS_gettid)
32
33 extern void psx_lock(void);
34 extern void psx_unlock(void);
35 extern void psx_cond_wait(void);
36 extern long psx_mix(long value);
37
38 typedef enum {
39 _PSX_IDLE = 0,
40 _PSX_SETUP = 1,
41 _PSX_SYSCALL = 2,
42 _PSX_EXITING = 3,
43 } psx_tracker_state_t;
44
45 /*
46 * Tracking threads is done via a hash map of these objects.
47 */
48 typedef struct psx_thread_ref_s {
49 long sweep;
50 long pending;
51 long tid;
52 long retval;
53 } psx_thread_ref_t;
54
55 /*
56 * This global structure holds the global coordination state for
57 * libcap's psx_syscall() support.
58 */
59 typedef struct {
60 long pid;
61 char *pid_path;
62
63 psx_mutex_t state_mu;
64 psx_tracker_state_t state;
65 int initialized;
66 int incomplete;
67 int psx_sig;
68 int force_failure; /* leave this as zero to avoid forcing a crash */
69 psx_sensitivity_t sensitivity;
70
71 struct {
72 long syscall_nr;
73 long arg1, arg2, arg3, arg4, arg5, arg6;
74 int six;
75 int active;
76 } cmd;
77
78 /* This is kept opaque here, but its details are known to psx_calls.c */
79 void *actions;
80
81 int map_entries;
82 long map_mask;
83 psx_thread_ref_t *map;
84 } psx_tracker_t;
85
86 /* defined in psx_calls.c */
87 extern psx_tracker_t psx_tracker;
88 extern int psx_actions_size(void);
89 extern void psx_confirm_sigaction(void);
90
91 #endif /* def LIBPSX_H */