/*
 * SPDX-License-Identifier: LGPL-2.1-only
 *
 * Copyright (C) 2011 EfficiOS Inc.
 * Copyright (C) 2011 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
 */

/*
 * This header is meant for liblttng and libust internal use ONLY.
 * These declarations should NOT be considered stable API.
 */

#ifndef _UST_COMMON_UST_COMM_H
#define _UST_COMMON_UST_COMM_H

#include <sys/uio.h>
#include <stdint.h>
#include <limits.h>
#include <unistd.h>
#include <lttng/ust-abi.h>
#include <lttng/ust-error.h>
#include <lttng/ust-compiler.h>
#include <lttng/ust-ctl.h>

/*
 * Default timeout the application waits for the sessiond to send its
 * "register done" command. Can be overridden with the environment
 * variable "LTTNG_UST_REGISTER_TIMEOUT". Note that if the sessiond is not
 * found, the application proceeds directly without any delay.
 */
#define LTTNG_UST_DEFAULT_CONSTRUCTOR_TIMEOUT_MS	3000

#define LTTNG_DEFAULT_RUNDIR				LTTNG_SYSTEM_RUNDIR
#define LTTNG_DEFAULT_HOME_RUNDIR			".lttng"

/* Queue size of listen(2) */
#define LTTNG_UST_COMM_MAX_LISTEN			10

struct lttng_ust_event_field;
struct lttng_ust_ctx_field;
struct lttng_ust_enum_entry;
struct lttng_integer_type;
struct lttng_ust_session;

/*
 * The maximum payload size is twice the maximum bytecode code for filters.
 *
 * That ought to be enough for extension in the future.  This is also small
 * enough to fit on stacks of threads created with the default glibc pthread
 * attribute: 2 MB on most architectures.
 */
#define LTTNG_UST_COMM_MAX_PAYLOAD_SIZE (1024 * 128)
#define LTTNG_UST_COMM_MAX_SEND_FDS	4

#define LTTNG_UST_COMM_REG_MSG_SIZE			136
struct lttng_ust_ctl_reg_msg {
	union {
		char padding[LTTNG_UST_COMM_REG_MSG_SIZE];
		struct {
			uint32_t magic;
			uint32_t major;
			uint32_t minor;
			uint32_t pid;
			uint32_t ppid;
			uint32_t uid;
			uint32_t gid;
			uint32_t bits_per_long;
			uint32_t uint8_t_alignment;
			uint32_t uint16_t_alignment;
			uint32_t uint32_t_alignment;
			uint32_t uint64_t_alignment;
			uint32_t long_alignment;
			uint32_t socket_type;			/* enum lttng_ust_ctl_socket_type */
			char name[LTTNG_UST_ABI_PROCNAME_LEN];	/* process name */
		} __attribute__((packed));
	};
} __attribute__((packed));

lttng_ust_static_assert(sizeof(struct lttng_ust_ctl_reg_msg) == LTTNG_UST_COMM_REG_MSG_SIZE,
			"Unexpected size for struct lttng_ust_ctl_reg_msg",
			Unexpected_size_for_struct_lttng_ust_ctl_reg_msg);

/*
 * Data structure for the commands sent from sessiond to UST.
 */
struct ustcomm_ust_msg_header {
	uint32_t handle;
	uint32_t cmd;
	uint32_t payload_size; /* in byte */
	uint32_t ancillary_size; /* in byte */
} __attribute__((packed));

/*
 * Data structure for the response from UST to the session daemon.
 * cmd_type is sent back in the reply for validation.
 */
#define USTCOMM_REPLY_HEADER_SIZE		32
#define USTCOMM_REPLY_CMD_SIZE			320
struct ustcomm_ust_reply {
	union {
		char padding[USTCOMM_REPLY_HEADER_SIZE];
		struct {
			uint32_t handle;
			uint32_t cmd;
			int32_t ret_code;	/* enum ustcomm_return_code */
			uint32_t ret_val;	/* return value */
		} __attribute__((packed));
	} header;
	union {
		char padding[USTCOMM_REPLY_CMD_SIZE];
		struct {
			uint64_t memory_map_size;
		} __attribute__((packed)) channel;
		struct {
			uint64_t memory_map_size;
		} __attribute__((packed)) stream;
		struct lttng_ust_abi_tracer_version version;
		struct lttng_ust_abi_tracepoint_iter tracepoint;
	} cmd;
} __attribute__((packed));

lttng_ust_static_assert(sizeof(struct ustcomm_ust_reply) == USTCOMM_REPLY_HEADER_SIZE + USTCOMM_REPLY_CMD_SIZE,
			"Unexpected size for struct ustcomm_ust_reply",
			Unexpected_size_for_struct_ustcomm_ust_reply);

struct ustcomm_notify_hdr {
	uint32_t notify_cmd;
} __attribute__((packed));

#define USTCOMM_NOTIFY_EVENT_MSG_SIZE		320
struct ustcomm_notify_event_msg {
	union {
		char padding[USTCOMM_NOTIFY_EVENT_MSG_SIZE];
		struct {
			uint32_t session_objd;
			uint32_t channel_objd;
			char event_name[LTTNG_UST_ABI_SYM_NAME_LEN];
			int32_t loglevel;
			uint32_t signature_len;
			uint32_t fields_len;
			uint32_t model_emf_uri_len;
			uint64_t user_token;
		} __attribute__((packed));
	};
	/* followed by signature, fields, and model_emf_uri */
} __attribute__((packed));

lttng_ust_static_assert(sizeof(struct ustcomm_notify_event_msg) == USTCOMM_NOTIFY_EVENT_MSG_SIZE,
			"Unexpected size for struct ustcomm_notify_event_msg",
			Unexpected_size_for_struct_ustcomm_notify_event_msg);

#define USTCOMM_NOTIFY_EVENT_REPLY_SIZE		32
struct ustcomm_notify_event_reply {
	union {
		char padding[USTCOMM_NOTIFY_EVENT_REPLY_SIZE];
		struct {
			int32_t ret_code;	/* 0: ok, negative: error code */
			uint32_t id;	/* 32-bit event id. */
		} __attribute__((packed));
	};
} __attribute__((packed));

lttng_ust_static_assert(sizeof(struct ustcomm_notify_event_reply) == USTCOMM_NOTIFY_EVENT_REPLY_SIZE,
			"Unexpected size for struct ustcomm_notify_event_reply",
			Unexpected_size_for_struct_ustcomm_notify_event_reply);

#define USTCOMM_NOTIFY_KEY_MSG_SIZE		64
struct ustcomm_notify_key_msg {
	union {
		char padding[USTCOMM_NOTIFY_KEY_MSG_SIZE];
		struct {
			uint32_t session_objd;
			uint32_t map_objd;
			uint32_t dimension;
			uint32_t key_string_len;
			uint64_t user_token;
		} __attribute__((packed));
	};
	/* followed by dimension_indexes (array of @dimension uint64_t items) and key_string. */
} __attribute__((packed));

lttng_ust_static_assert(sizeof(struct ustcomm_notify_key_msg) == USTCOMM_NOTIFY_KEY_MSG_SIZE,
			"Unexpected size for struct ustcomm_notify_key_msg",
			Unexpected_size_for_struct_ustcomm_notify_key_msg);

#define USTCOMM_NOTIFY_KEY_REPLY_SIZE		32
struct ustcomm_notify_key_reply {
	union {
		char padding[USTCOMM_NOTIFY_KEY_REPLY_SIZE];
		struct {
			int32_t ret_code;	/* 0: ok, negative: error code */
			uint64_t index;		/* 64-bit key index. */
		} __attribute__((packed));
	};
} __attribute__((packed));

lttng_ust_static_assert(sizeof(struct ustcomm_notify_key_reply) == USTCOMM_NOTIFY_KEY_REPLY_SIZE,
			"Unexpected size for struct ustcomm_notify_key_reply",
			Unexpected_size_for_struct_ustcomm_notify_key_reply);

#define USTCOMM_NOTIFY_ENUM_MSG_SIZE		320
struct ustcomm_notify_enum_msg {
	union {
		char padding[USTCOMM_NOTIFY_ENUM_MSG_SIZE];
		struct {
			uint32_t session_objd;
			char enum_name[LTTNG_UST_ABI_SYM_NAME_LEN];
			uint32_t entries_len;
		} __attribute__((packed));
	};
	/* followed by enum entries */
} __attribute__((packed));

lttng_ust_static_assert(sizeof(struct ustcomm_notify_enum_msg) == USTCOMM_NOTIFY_ENUM_MSG_SIZE,
			"Unexpected size for struct ustcomm_notify_enum_msg",
			Unexpected_size_for_struct_ustcomm_notify_enum_msg);

#define USTCOMM_NOTIFY_ENUM_REPLY_SIZE		32
struct ustcomm_notify_enum_reply {
	union {
		char padding[USTCOMM_NOTIFY_ENUM_REPLY_SIZE];
		struct {
			int32_t ret_code;	/* 0: ok, negative: error code */
			uint64_t enum_id;
		} __attribute__((packed));
	};
} __attribute__((packed));

lttng_ust_static_assert(sizeof(struct ustcomm_notify_enum_reply) == USTCOMM_NOTIFY_ENUM_REPLY_SIZE,
			"Unexpected size for struct ustcomm_notify_enum_reply",
			Unexpected_size_for_struct_ustcomm_notify_enum_reply);

#define USTCOMM_NOTIFY_CHANNEL_MSG_SIZE		32
struct ustcomm_notify_channel_msg {
	union {
		char padding[USTCOMM_NOTIFY_CHANNEL_MSG_SIZE];
		struct {
			uint32_t session_objd;
			uint32_t channel_objd;
			uint32_t ctx_fields_len;
		} __attribute__((packed));
	};
	/* followed by context fields */
} __attribute__((packed));

lttng_ust_static_assert(sizeof(struct ustcomm_notify_channel_msg) == USTCOMM_NOTIFY_CHANNEL_MSG_SIZE,
			"Unexpected size for struct ustcomm_notify_channel_msg",
			Unexpected_size_for_struct_ustcomm_notify_channel_msg);

#define USTCOMM_NOTIFY_CHANNEL_REPLY_SIZE	32
struct ustcomm_notify_channel_reply {
	union {
		char padding[USTCOMM_NOTIFY_CHANNEL_REPLY_SIZE];
		struct {
			int32_t ret_code;	/* 0: ok, negative: error code */
			uint32_t chan_id;
			uint32_t header_type;	/* enum lttng_ust_ctl_channel_header */
		} __attribute__((packed));
	};
} __attribute__((packed));

lttng_ust_static_assert(sizeof(struct ustcomm_notify_channel_reply) == USTCOMM_NOTIFY_CHANNEL_REPLY_SIZE,
			"Unexpected size for struct ustcomm_notify_channel_reply",
			Unexpected_size_for_struct_ustcomm_notify_channel_reply);

/*
 * LTTNG_UST_TRACEPOINT_FIELD_LIST reply is followed by a
 * struct lttng_ust_field_iter field.
 */

/*
 * Shutdown behavior on error for ustcomm socket functions.
 */
enum ustcomm_shutdown {
	USTCOMM_SHUTDOWN_NONE,
	USTCOMM_SHUTDOWN_RD,
	USTCOMM_SHUTDOWN_WR,
	USTCOMM_SHUTDOWN_RDWR,
};

/*
 * fd: The file descriptor on which I/O operations are made.
 *
 * shutdown_on_error: Shutdown behavior when an error is detected during an I/O
 * operation.
 */
struct ustcomm_sock {
	int fd;
	enum ustcomm_shutdown shutdown_on_error;
};

int ustcomm_create_unix_sock(const char *pathname)
	__attribute__((visibility("hidden")));

int ustcomm_connect_unix_sock(const char *pathname,
	long timeout)
	__attribute__((visibility("hidden")));

int ustcomm_accept_unix_sock(int sock)
	__attribute__((visibility("hidden")));

int ustcomm_listen_unix_sock(int sock)
	__attribute__((visibility("hidden")));

int ustcomm_shutdown_unix_sock(const struct ustcomm_sock *sock)
	__attribute__((visibility("hidden")));

int ustcomm_close_unix_sock(int sock)
	__attribute__((visibility("hidden")));

ssize_t ustcomm_recv_unix_sock(const struct ustcomm_sock *sock,
			void *buf, size_t len)
	__attribute__((visibility("hidden")));

ssize_t ustcomm_sendv_unix_sock(const struct ustcomm_sock *sock,
	const struct iovec *iov, int iovcnt)
	__attribute__((visibility("hidden")));

ssize_t ustcomm_send_unix_sock(const struct ustcomm_sock *sock,
	const void *buf, size_t len)
	__attribute__((visibility("hidden")));

ssize_t ustcomm_send_fds_unix_sock(const struct ustcomm_sock *sock,
				int *fds, size_t nb_fd)
	__attribute__((visibility("hidden")));

ssize_t ustcomm_recv_fds_unix_sock(const struct ustcomm_sock *sock,
				int *fds, size_t nb_fd)
	__attribute__((visibility("hidden")));

const char *ustcomm_get_readable_code(int code)
	__attribute__((visibility("hidden")));


int ustcomm_send_app_msg(const struct ustcomm_sock *sock,
			struct ustcomm_ust_msg_header *lum,
			struct iovec *iov, int iovcnt,
			const int *fds, size_t fds_cnt)
	__attribute__((visibility("hidden")));

int ustcomm_recv_app_reply(const struct ustcomm_sock *sock,
			struct ustcomm_ust_reply *lur,
			uint32_t expected_handle, uint32_t expected_cmd)
	__attribute__((visibility("hidden")));

int ustcomm_send_app_cmd(const struct ustcomm_sock *sock,
		struct ustcomm_ust_msg_header *lum,
		struct ustcomm_ust_reply *lur)
	__attribute__((visibility("hidden")));

int ustcomm_recv_fd(int sock)
	__attribute__((visibility("hidden")));

int ustcomm_recv_channel_wakeup_fd_from_sessiond(int sock)
	__attribute__((visibility("hidden")));

int ustcomm_recv_stream_fds_from_sessiond(int sock,
		int *shm_fd, int *wakeup_fd)
	__attribute__((visibility("hidden")));

ssize_t ustcomm_recv_event_notifier_notif_fd_from_sessiond(int sock,
		int *event_notifier_notif_fd)
	__attribute__((visibility("hidden")));

int ustcomm_recv_counter_shm_from_sessiond(int sock,
		int *shm_fd)
	__attribute__((visibility("hidden")));
/*
 * Returns 0 on success, negative error value on error.
 * Returns -EPIPE or -ECONNRESET if other end has hung up.
 */
int ustcomm_send_reg_msg(const struct ustcomm_sock *sock,
		enum lttng_ust_ctl_socket_type type,
		uint32_t bits_per_long,
		uint32_t uint8_t_alignment,
		uint32_t uint16_t_alignment,
		uint32_t uint32_t_alignment,
		uint32_t uint64_t_alignment,
		uint32_t long_alignment,
		const char *procname)
	__attribute__((visibility("hidden")));

/*
 * Returns 0 on success, negative error value on error.
 * Returns -EPIPE or -ECONNRESET if other end has hung up.
 */
int ustcomm_register_event(const struct ustcomm_sock *sock,
	struct lttng_ust_session *session,
	int session_objd,		/* session descriptor */
	int channel_objd,		/* channel descriptor */
	const char *event_name,		/* event name (input) */
	int loglevel,
	const char *signature,		/* event signature (input) */
	size_t nr_fields,		/* fields */
	const struct lttng_ust_event_field * const *fields,
	const char *model_emf_uri,
	uint64_t user_token,
	uint32_t *id)			/* (output) */
	__attribute__((visibility("hidden")));

/*
 * Returns 0 on success, negative error value on error.
 * Returns -EPIPE or -ECONNRESET if other end has hung up.
 */
int ustcomm_register_key(const struct ustcomm_sock *sock,
	int session_objd,		/* session descriptor */
	int map_objd,			/* map descriptor */
	uint32_t dimension,
	const uint64_t *dimension_indexes,
	const char *key_string,		/* key string (input) */
	uint64_t user_token,
	uint64_t *index)		/* (output) */
	__attribute__((visibility("hidden")));

/*
 * Returns 0 on success, negative error value on error.
 * Returns -EPIPE or -ECONNRESET if other end has hung up.
 */
int ustcomm_register_enum(const struct ustcomm_sock *sock,
	int session_objd,		/* session descriptor */
	const char *enum_name,		/* enum name (input) */
	size_t nr_entries,		/* entries */
	const struct lttng_ust_enum_entry * const *entries,
	uint64_t *id)			/* enum id (output) */
	__attribute__((visibility("hidden")));

/*
 * Returns 0 on success, negative error value on error.
 * Returns -EPIPE or -ECONNRESET if other end has hung up.
 */
int ustcomm_register_channel(const struct ustcomm_sock *sock,
	struct lttng_ust_session *session,
	int session_objd,		/* session descriptor */
	int channel_objd,		/* channel descriptor */
	size_t nr_ctx_fields,
	struct lttng_ust_ctx_field *ctx_fields,
	uint32_t *chan_id,		/* channel id (output) */
	int *header_type) 		/* header type (output) */
	__attribute__((visibility("hidden")));

int ustcomm_setsockopt_rcv_timeout(int sock, unsigned int msec)
	__attribute__((visibility("hidden")));

int ustcomm_setsockopt_snd_timeout(int sock, unsigned int msec)
	__attribute__((visibility("hidden")));

#endif	/* _UST_COMMON_UST_COMM_H */
