#ifndef TOKSVC_H
#define TOKSVC_H
#include <stdint.h>
#include <stddef.h>
#include "toksvc_api.h"
#ifdef __cplusplus
extern "C" {
#endif
/* pre-alpha */
#ifndef TOKS_APP
#ifndef TOKS_PRE_ALPHA
#error libtoks: pre-alpha: ABI is not final!
#error to use the library, compile with -DTOKS_PRE_ALPHA.
#endif
#endif
/* daemon */
#define TOKS_PORT_GUID_DAEMON {0x7d36f145, \
0xd8f0, \
0x440f, \
{0x92,0xbe,0xc3,0x53,0x61,0x7b,0xbc,0x96}}
#define TOKS_PORT_NAME_PREFIX {'t','o','k','s','v','c'}
/* status codes */
#define TOKS_OK 0x00
#define TOKS_USAGE 0x01
#define TOKS_ERROR 0x02
/* driver flags */
#define TOKS_DRIVER_VERBOSITY_NONE 0x0000
#define TOKS_DRIVER_VERBOSITY_ERRORS 0x0001
#define TOKS_DRIVER_VERBOSITY_STATUS 0x0002
#define TOKS_DRIVER_VERBOSITY_USAGE 0x0004
#define TOKS_DRIVER_CLONE_VECTOR 0x0008
#define TOKS_DRIVER_VERSION 0x0010
#define TOKS_DRIVER_DRY_RUN 0x0020
#define TOKS_DRIVER_DAEMON_ALWAYS 0x0040
#define TOKS_DRIVER_DAEMON_NEVER 0x0080
#define TOKS_DRIVER_ANNOTATE_ALWAYS 0x1000
#define TOKS_DRIVER_ANNOTATE_NEVER 0x2000
#define TOKS_DRIVER_ANNOTATE_FULL 0x4000
#define TOKS_DRIVER_MODE_SERVER 0X10000
#define TOKS_DRIVER_MODE_CLIENT 0X20000
#define TOKS_DRIVER_ROOT_UUID 0x40000
#define TOKS_DRIVER_ACTION_ACQUIRE 0X100000
#define TOKS_DRIVER_ACTION_RELEASE 0X200000
#define TOKS_DRIVER_ACTION_ABORT 0X400000
#define TOKS_DRIVER_ACTION_NTOKENS_GET 0X1000000
#define TOKS_DRIVER_ACTION_NTOKENS_SET 0X2000000
#define TOKS_DRIVER_ACTION_LOGLEVEL_GET 0X4000000
#define TOKS_DRIVER_ACTION_LOGLEVEL_SET 0X8000000
#define TOKS_DRIVER_ACTION_SVCINFO_GET 0X10000000
#define TOKS_DRIVER_ACTION_SVCINFO_LOG 0X20000000
/* message options */
#define TOKS_OPT_SYSTEM_PID 0x0000
#define TOKS_OPT_FRAMEWORK_PID 0x0001
/* error flags */
#define TOKS_ERROR_TOP_LEVEL 0x0001
#define TOKS_ERROR_NESTED 0x0002
#define TOKS_ERROR_CHILD 0x0004
#define TOKS_ERROR_CUSTOM 0x0008
/* ioctl */
#define TOKS_IOCTL_DEVICE_TOKEN_SERVICE (0xdaaf)
#define TOKS_IOCTL_OPCODE_BASE (0xaaf)
#define TOKS_IOCTL_METHOD_BUFFERED (0x0)
#define TOKS_IOCTL_METHOD_INDIRECT (0x1)
#define TOKS_IOCTL_METHOD_OUTDIRECT (0x2)
#define TOKS_IOCTL_METHOD_NEITHER (0x3)
#define TOKS_IOCTL_ACCESS_READ (0x1)
#define TOKS_IOCTL_ACCESS_WRITE (0x2)
#define TOKS_IOCTL_IDX(IDX) (TOKS_IOCTL_OPCODE_BASE + IDX)
#define TOKS_IOCTL_ANY(IDX,ACC,METHOD) ((TOKS_IOCTL_DEVICE_TOKEN_SERVICE << 16) \
| (TOKS_IOCTL_IDX(IDX) << 2) \
| (ACC << 14) | (METHOD))
#define TOKS_IOCTL_GET(IDX) TOKS_IOCTL_ANY(IDX,TOKS_IOCTL_ACCESS_READ, \
TOKS_IOCTL_METHOD_OUTDIRECT)
#define TOKS_IOCTL_SET(IDX) TOKS_IOCTL_ANY(IDX,TOKS_IOCTL_ACCESS_WRITE, \
TOKS_IOCTL_METHOD_INDIRECT)
#define TOKS_IOCTL_IDX_GET_TOKEN_COUNT 0
#define TOKS_IOCTL_IDX_SET_TOKEN_COUNT 1
#define TOKS_IOCTL_IDX_GET_SERVICE_INFO 2
#define TOKS_IOCTL_IDX_LOG_SERVICE_INFO 3
#define TOKS_IOCTL_IDX_GET_LOG_LEVEL 6
#define TOKS_IOCTL_IDX_SET_LOG_LEVEL 7
#define TOKS_IOCTL_GET_TOKEN_COUNT TOKS_IOCTL_GET(TOKS_IOCTL_IDX_GET_TOKEN_COUNT)
#define TOKS_IOCTL_SET_TOKEN_COUNT TOKS_IOCTL_SET(TOKS_IOCTL_IDX_SET_TOKEN_COUNT)
#define TOKS_IOCTL_GET_SERVICE_INFO TOKS_IOCTL_GET(TOKS_IOCTL_IDX_GET_SERVICE_INFO)
#define TOKS_IOCTL_LOG_SERVICE_INFO TOKS_IOCTL_SET(TOKS_IOCTL_IDX_LOG_SERVICE_INFO)
#define TOKS_IOCTL_GET_LOG_LEVEL TOKS_IOCTL_GET(TOKS_IOCTL_IDX_GET_LOG_LEVEL)
#define TOKS_IOCTL_SET_LOG_LEVEL TOKS_IOCTL_SET(TOKS_IOCTL_IDX_SET_LOG_LEVEL)
enum toks_custom_error {
TOKS_ERR_FLOW_ERROR,
TOKS_ERR_FLEE_ERROR,
TOKS_ERR_NULL_CONTEXT,
TOKS_ERR_BAD_DATA,
TOKS_ERR_CAP,
};
enum toks_warning_level {
TOKS_WARNING_LEVEL_UNKNOWN,
TOKS_WARNING_LEVEL_ALL,
TOKS_WARNING_LEVEL_ERROR,
TOKS_WARNING_LEVEL_NONE,
};
struct toks_uuid {
uint32_t data1;
uint16_t data2;
uint16_t data3;
unsigned char data4[8];
};
struct toks_source_version {
int major;
int minor;
int revision;
const char * commit;
};
struct toks_token_string{
char token[128];
};
struct toks_service_info {
struct toks_uuid uuid;
int32_t syspid;
int32_t systid;
int32_t ctrlpid;
int32_t csyspid;
int32_t allocated;
int32_t available;
int32_t tused;
int32_t tfree;
int32_t loglevel;
};
struct toks_error_info {
const struct toks_driver_ctx * edctx;
const struct toks_unit_ctx * euctx;
const char * eunit;
int esyscode;
int elibcode;
const char * efunction;
int eline;
unsigned eflags;
void * eany;
};
struct toks_common_ctx {
uint64_t drvflags;
uint64_t actflags;
uint64_t fmtflags;
int32_t loglevel;
const struct toks_uuid * uuid;
void * hroot;
const char * sysroot;
const char * logfile;
const char * refstr;
char ** eargv;
};
struct toks_driver_ctx {
const char ** units;
const char * program;
const char * module;
const struct toks_common_ctx * cctx;
struct toks_error_info ** errv;
void * any;
};
/* driver api */
toks_api int toks_get_driver_ctx (char ** argv, char ** envp, uint32_t flags,
struct toks_driver_ctx **);
toks_api void toks_free_driver_ctx (struct toks_driver_ctx *);
toks_api void toks_driver_set_timeout (struct toks_driver_ctx *, int64_t millisecs);
toks_api void toks_driver_unset_timeout (struct toks_driver_ctx *);
toks_api void toks_driver_set_refstr (struct toks_driver_ctx *, const char *);
toks_api void toks_driver_unset_refstr (struct toks_driver_ctx *);
/* core api */
toks_api int toks_service_abort (struct toks_driver_ctx *);
toks_api int toks_service_ioctl (struct toks_driver_ctx *, uint32_t ctlcode, uintptr_t * data, void * any);
toks_api int toks_service_start (struct toks_common_ctx *, int ntokens, int32_t ctrlpid, int32_t csyspid);
/* client api */
toks_api int toks_client_connect (struct toks_driver_ctx *);
toks_api int toks_client_acquire (struct toks_driver_ctx *);
toks_api int toks_client_release (struct toks_driver_ctx *);
toks_api int toks_client_token_to_str (struct toks_driver_ctx *, struct toks_token_string *);
toks_api int toks_client_str_to_token (struct toks_driver_ctx *, const struct toks_token_string *);
toks_api int toks_client_query_service (struct toks_driver_ctx *, struct toks_service_info *);
/* utility api */
toks_api int toks_main (char **, char **);
/* error trace api */
toks_api int toks_output_error_record (const struct toks_driver_ctx *, const struct toks_error_info *);
toks_api int toks_output_error_vector (const struct toks_driver_ctx *);
/* helper api */
toks_api int toks_uuid_from_path (const char *, struct toks_uuid *);
toks_api int toks_string_to_uuid (const char (*)[40], struct toks_uuid *);
toks_api void toks_uuid_to_string (const struct toks_uuid *, char (*)[40]);
/* package info */
toks_api const struct toks_source_version * toks_source_version(void);
#ifdef __cplusplus
}
#endif
#endif