lib: add libxpc to implement functionality needed by xpc interfaces

This commit is contained in:
2026-03-10 19:14:37 +00:00
parent b0fda122e0
commit aef0163017
10 changed files with 516 additions and 0 deletions

View File

@@ -0,0 +1,86 @@
#ifndef XPC_BUFFER_H_
#define XPC_BUFFER_H_
#include <stddef.h>
#include <xpc/status.h>
#define XPC_BUFFER_IN(msg, offset, size) \
{ \
.buf_flags = XPC_BUFFER_F_IN | XPC_BUFFER_F_REMOTE, \
.buf_origin = (msg), \
.buf_offset = (offset), \
.buf_len = (size), \
}
#define XPC_BUFFER_OUT(msg, offset, size) \
{ \
.buf_flags = XPC_BUFFER_F_OUT | XPC_BUFFER_F_REMOTE, \
.buf_origin = (msg), \
.buf_offset = (offset), \
.buf_len = (size), \
}
struct xpc_msg;
typedef enum xpc_buffer_flags {
/* the buffer can be read from */
XPC_BUFFER_F_IN = 0x01u,
/* the buffer can be written to */
XPC_BUFFER_F_OUT = 0x02u,
/* the buffer is backed by a buffer located in another address space.
* the buffer can only be accessed via xpc_buffer_read and/or
* xpc_buffer_write */
XPC_BUFFER_F_REMOTE = 0x04u,
/* free the buffer backing this buffer when the buffer is discarded.
* this is only used for out-buffers. the buffer must have been
* allocated using xpc_context_alloc, as it will be freed via a call
* to xpc_context_free */
XPC_BUFFER_F_FREE_ON_DISCARD = 0x08u,
} xpc_buffer_flags_t;
typedef struct xpc_buffer {
xpc_buffer_flags_t buf_flags;
union {
/* fields that are only valid if F_OUT is set */
struct {
/* only valid if F_OUT is set. specifies the maximum
* number of chars that can be written to buf_buf,
* including the null terminator. */
size_t buf_max;
/* only valid if F_OUT is set.
* if F_FREE_ON_DISCARD is set, must be either NULL or
* allocated via xpc_context_alloc */
const char *buf_ptr;
};
/* fields that are only valid if F_IN is set */
struct {
/* only valid if F_IN is set. offset of the buffer data
* within the associated message. used when reading
* buffer data from a message. */
size_t buf_offset;
};
};
/* only valid if F_REMOTE is set.
* used to read/write buffer data from/to the sender's address
* space. */
const struct xpc_msg *buf_origin;
/* valid for both F_IN and F_OUT buffers.
* F_IN: specifies the length of the incoming buffer data.
* F_OUT: specifies how many bytes from buf_ptr to send. */
size_t buf_len;
} xpc_buffer_t;
extern xpc_status_t xpc_buffer_read(
const xpc_buffer_t *s,
void *out,
size_t max,
size_t *nr_read);
extern xpc_status_t xpc_buffer_write(
xpc_buffer_t *s,
const void *in,
size_t len,
size_t *nr_written);
#endif

View File

@@ -0,0 +1,8 @@
#ifndef XPC_CONTEXT_H_
#define XPC_CONTEXT_H_
typedef struct xpc_context {
} xpc_context_t;
#endif

View File

@@ -0,0 +1,13 @@
#ifndef XPC_ENDPOINT_H_
#define XPC_ENDPOINT_H_
#include <mango/types.h>
typedef struct xpc_endpoint {
kern_handle_t e_channel;
tid_t e_task;
koid_t e_port;
msgid_t e_msg;
} xpc_endpoint_t;
#endif

View File

@@ -0,0 +1,53 @@
#ifndef XPC_MSG_H_
#define XPC_MSG_H_
#include <mango/types.h>
#include <stdbool.h>
#include <stdint.h>
#include <xpc/endpoint.h>
#define XPC_MSG_MAGIC 0x5850434D
typedef struct xpc_msg_header {
uint32_t hdr_magic;
uint32_t hdr_interface;
uint16_t hdr_func;
uint16_t hdr_status;
} xpc_msg_header_t;
typedef struct xpc_msg {
xpc_endpoint_t msg_sender;
xpc_msg_header_t msg_header;
size_t msg_handles_count;
kern_msg_handle_t msg_handles[KERN_MSG_MAX_HANDLES];
} xpc_msg_t;
extern void xpc_msg_header_init(
xpc_msg_header_t *msg,
unsigned long interface,
unsigned short func);
extern bool xpc_msg_header_validate(const xpc_msg_header_t *msg);
extern kern_status_t xpc_msg_recv(kern_handle_t channel, xpc_msg_t *out);
extern kern_status_t xpc_msg_read(
const xpc_msg_t *msg,
size_t offset,
void *p,
size_t count);
extern kern_status_t xpc_msg_write(
const xpc_msg_t *msg,
size_t offset,
const void *p,
size_t count);
extern kern_status_t xpc_msg_reply(
const xpc_msg_t *msg,
kern_iovec_t *iov,
size_t iov_count,
kern_msg_handle_t *handles,
size_t handle_count);
extern kern_status_t xpc_msg_reply_error(
const xpc_msg_t *msg,
unsigned short code);
#endif

View File

@@ -0,0 +1,12 @@
#ifndef XPC_STATUS_H_
#define XPC_STATUS_H_
typedef enum xpc_status {
XPC_SUCCESS = 0,
XPC_ERR_BAD_STATE,
XPC_ERR_INVALID_ARGUMENT,
XPC_ERR_NO_MEMORY,
XPC_ERR_MEMORY_FAULT,
} xpc_status_t;
#endif

View File

@@ -0,0 +1,85 @@
#ifndef XPC_STRING_H_
#define XPC_STRING_H_
#include <stddef.h>
#include <xpc/status.h>
#define XPC_STRING_NPOS ((size_t)-1)
#define XPC_STRING_IN(msg, offset, size) \
{ \
.s_flags = XPC_STRING_F_IN | XPC_STRING_F_REMOTE, \
.s_origin = (msg), \
.s_offset = (offset), \
.s_len = (size), \
}
#define XPC_STRING_OUT(msg, offset, size) \
{ \
.s_flags = XPC_STRING_F_OUT | XPC_STRING_F_REMOTE, \
.s_origin = (msg), \
.s_offset = (offset), \
.s_len = (size), \
}
struct xpc_msg;
typedef enum xpc_string_flags {
/* the string can be read from */
XPC_STRING_F_IN = 0x01u,
/* the string can be written to */
XPC_STRING_F_OUT = 0x02u,
/* the string is backed by a buffer located in another address space.
* the string can only be accessed via xpc_string_read and/or
* xpc_string_write */
XPC_STRING_F_REMOTE = 0x04u,
/* free the buffer backing this string when the string is discarded.
* this is only used for out-strings. the buffer must have been
* allocated using xpc_context_alloc, as it will be freed via a call
* to xpc_context_free */
XPC_STRING_F_FREE_ON_DISCARD = 0x08u,
} xpc_string_flags_t;
typedef struct xpc_string {
xpc_string_flags_t s_flags;
union {
struct {
/* only valid if F_OUT is set. specifies the maximum
* number of chars that can be written to s_buf,
* including the null terminator. */
size_t s_max;
/* only valid if F_OUT is set.
* if F_FREE_ON_DISCARD is set, must be either NULL or
* allocated via xpc_context_alloc */
const char *s_buf;
};
struct {
/* only valid if F_IN is set. offset of the string data
* within the associated message. used when reading
* string data from a message. */
size_t s_offset;
};
};
/* only valid if F_REMOTE is set.
* used to read/write string data from/to the sender's address space. */
const struct xpc_msg *s_origin;
/* valid for both F_IN and F_OUT strings.
* F_IN: specifies the length of the incoming string data.
* F_OUT: specifies how many characters from s_buf to send. */
size_t s_len;
} xpc_string_t;
extern xpc_status_t xpc_string_read(
const xpc_string_t *s,
char *out,
size_t max,
size_t *nr_read);
extern xpc_status_t xpc_string_write(
xpc_string_t *s,
const char *in,
size_t len,
size_t *nr_written);
#endif