Blob Blame History Raw
#ifndef TOKSVC_DRIVER_IMPL_H
#define TOKSVC_DRIVER_IMPL_H

#include <psxtypes/psxtypes.h>
#include <ntapi/ntapi.h>

#include <stdint.h>
#include <stdio.h>

#include <toksvc/toksvc.h>
#include "argv/argv.h"

#define TOKS_OPTV_ELEMENTS 64
#define TOKS_MAX_WAITERS   4000

extern   const struct argv_option toks_default_options[];
extern   const ntapi_vtbl * toks_ntapi;

size_t   toks_strlen(const char * ch);
char *   toks_strcpy(char * dst, const char * src);

void *   toks_calloc(size_t n, size_t size);
void     toks_free(void *);

#define  ntapi toks_ntapi

enum app_tags {
	TAG_HELP,
	TAG_VERSION,
	TAG_DAEMON,
	TAG_SYSROOT,
	TAG_UUID,
	TAG_ROOTUUID,
	TAG_CONNECT,
	TAG_TOKENS,
	TAG_ABORT,
	TAG_ACQUIRE,
	TAG_RELEASE,
	TAG_TIMEOUT,
	TAG_REFSTR,
	TAG_PID,
	TAG_SYSPID,
	TAG_CTRLPID,
	TAG_CSYSPID,
	TAG_LOGFILE,
	TAG_LOGLEVEL,
	TAG_NTOKENSGET,
	TAG_NTOKENSSET,
	TAG_GETSVCINFO,
	TAG_LOGSVCINFO,
};

struct toks_ticks {
	nt_filetime	pcfreq;
	int32_t		(*qpc)(nt_filetime *);
};

struct toks_driver_ctx_impl {
	nt_rtdata *		rtdata;
	struct toks_common_ctx	cctx;
	struct toks_driver_ctx	ctx;
	struct toks_ticks	ticks;
	struct toks_token *	tokens;
	struct toks_waiter *	waiters;
	struct _nt_port_keys    keys;
	int64_t                 timeout;
	char *			refstr;
	void *			hevent;
	void *			hsvcdir;
	void *			hpiddir;
	void *			hsvclink;
	void *			hservice;
	void *			hserver;
	void *			hlog;
	void *			htmpfs;
	int			nwaiters;
	int			ntokens;
	int			atokens;
	int			tokpid;
	int			tsyspid;
	int			ctrlpid;
	int			csyspid;
	struct toks_uuid	uuid;
	struct _nt_guid		guid;
};

static inline void toks_uuid_to_guid(const struct toks_uuid * uuid, struct _nt_guid * guid)
{
	guid->data1    = uuid->data1;
	guid->data2    = uuid->data2;
	guid->data3    = uuid->data3;

	guid->data4[0] = uuid->data4[0];
	guid->data4[1] = uuid->data4[1];
	guid->data4[2] = uuid->data4[2];
	guid->data4[3] = uuid->data4[3];
	guid->data4[4] = uuid->data4[4];
	guid->data4[5] = uuid->data4[5];
	guid->data4[6] = uuid->data4[6];
	guid->data4[7] = uuid->data4[7];
}

static inline void toks_guid_to_uuid(const struct _nt_guid * guid, struct toks_uuid * uuid)
{
	uuid->data1    = guid->data1;
	uuid->data2    = guid->data2;
	uuid->data3    = guid->data3;

	uuid->data4[0] = guid->data4[0];
	uuid->data4[1] = guid->data4[1];
	uuid->data4[2] = guid->data4[2];
	uuid->data4[3] = guid->data4[3];
	uuid->data4[4] = guid->data4[4];
	uuid->data4[5] = guid->data4[5];
	uuid->data4[6] = guid->data4[6];
	uuid->data4[7] = guid->data4[7];
}

static inline struct toks_driver_ctx_impl * toks_get_driver_ictx(const struct toks_driver_ctx * dctx)
{
	uintptr_t addr;

	if (dctx) {
		addr = (uintptr_t)dctx - offsetof(struct toks_driver_ctx_impl,ctx);
		return (struct toks_driver_ctx_impl *)addr;
	}

	return 0;
}

static inline nt_rtdata * toks_get_driver_rtdata(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->rtdata;
}

static inline int64_t toks_get_driver_timeout(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->timeout;
}

static inline void toks_set_driver_timeout(const struct toks_driver_ctx * dctx, int64_t timeout)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->timeout = timeout;
}

static inline void * toks_get_driver_hevent(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hevent;
}

static inline void * toks_get_driver_hsvcdir(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hsvcdir;
}

static inline void toks_set_driver_hsvcdir(const struct toks_driver_ctx * dctx, void * hsvcdir)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->hsvcdir = hsvcdir;
}

static inline void * toks_get_driver_hpiddir(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hpiddir;
}

static inline void toks_set_driver_hpiddir(const struct toks_driver_ctx * dctx, void * hpiddir)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->hpiddir = hpiddir;
}

static inline void * toks_get_driver_hsvclink(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hsvclink;
}

static inline void toks_set_driver_hsvclink(const struct toks_driver_ctx * dctx, void * hsvclink)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->hsvclink = hsvclink;
}

static inline void * toks_get_driver_hservice(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hservice;
}

static inline void toks_set_driver_hservice(const struct toks_driver_ctx * dctx, void * hservice)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->hservice = hservice;
}

static inline void * toks_get_driver_hserver(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->hserver;
}

static inline void toks_set_driver_hserver(const struct toks_driver_ctx * dctx, void * hserver)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->hserver = hserver;
}

static inline struct _nt_port_keys * toks_get_driver_keys(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return &ictx->keys;
}

static inline const struct toks_uuid * toks_get_driver_uuid(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return &ictx->uuid;
}

static inline const struct _nt_guid * toks_get_driver_guid(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return &ictx->guid;
}

static inline int toks_get_driver_nwaiters(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->nwaiters;
}

static inline void toks_set_driver_nwaiters(const struct toks_driver_ctx * dctx, int nwaiters)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->nwaiters = nwaiters;
}

static inline struct toks_token * toks_get_driver_tokens(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->tokens;
}

static inline int toks_get_driver_ntokens(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->ntokens;
}

static inline void toks_set_driver_ntokens(const struct toks_driver_ctx * dctx, int ntokens)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->ntokens = ntokens;
}

static inline int toks_get_driver_atokens(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->atokens;
}

static inline void toks_set_driver_log_level(const struct toks_driver_ctx * dctx, int loglevel)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	ictx->cctx.loglevel = loglevel;
}

static inline int toks_get_driver_tokpid(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->tokpid;
}

static inline int toks_get_driver_tsyspid(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->tsyspid;
}

static inline int toks_get_driver_ctrlpid(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->ctrlpid;
}

static inline int toks_get_driver_csyspid(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->csyspid;
}

static inline char * toks_get_driver_refstr(const struct toks_driver_ctx * dctx)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);
	return ictx->refstr;
}

static inline void toks_set_driver_refstr(const struct toks_driver_ctx * dctx, const char * refstr)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);

	if (ictx->refstr) {
		toks_free(ictx->refstr);
		ictx->refstr = 0;
	}

	if (refstr && (ictx->refstr = toks_calloc(1,toks_strlen(refstr)+1)))
		toks_strcpy(ictx->refstr,refstr);
}

static inline void toks_query_performance_counters(const struct toks_driver_ctx * dctx, nt_filetime * ticks)
{
	struct toks_driver_ctx_impl * ictx;
	ictx = toks_get_driver_ictx(dctx);

	if (!(ictx->ticks.qpc(ticks)))
		ticks->quad = 0;
}

int32_t toks_open_log_file(void ** hfile, void * hat, const char * arg, bool fprivate);
int32_t toks_open_file(void ** hfile, void * hat, const char * arg, bool fprivate);
int32_t toks_open_dir(void ** hfile, void * hat, const char * arg, bool fprivate);

#endif