/*
 * Server side token handle stuff
 *
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#include "winbase.h"

#include "handle.h"
#include "request.h"
#include "thread.h"
#include "process.h"

struct token 
{
    struct object       obj;         /* object header */
    struct object*      owning_obj;  /* What owns it */
};

static void token_dump( struct object *obj, int verbose );
static void token_destroy( struct object *obj);

static const struct object_ops token_ops =
{
    sizeof(struct token),         /* size */
    token_dump,                   /* dump */
    no_add_queue,                 /* add_queue */
    NULL,                         /* remove_queue */
    NULL,                         /* signaled */
    NULL,                         /* satisfied */
    NULL,                         /* get_poll_events */
    NULL,                         /* poll_event */
    no_get_fd,                    /* get_fd */
#if defined( CONTEXTUAL_FD )
    no_get_ctx_fd,                /* get_ctx_fd */
#endif
    no_flush,                     /* flush */
    no_get_file_info,             /* get_file_info */
    NULL,                         /* queue_async */
    token_destroy                 /* destroy */
};

const struct object_ops* get_this_process_token_ops(void) { return &token_ops; }

static void token_dump( struct object *obj, int verbose )
{
    struct token *token = (struct token *)obj;

    assert( obj->ops == token_ops_tag );

    fprintf( stderr, "Token owned by %p\n", token->owning_obj );
}

static void token_destroy( struct object *obj )
{
    struct token *token = (struct token *)obj;

    assert( obj->ops == token_ops_tag );

    token->owning_obj = NULL;
}

struct token *create_token( struct object *obj )
{
    struct token *token;

    if ((token = alloc_object(token_ops_tag, -1)))
    {
        token->owning_obj = obj;
    }

    return token;
}

DECL_HANDLER(open_token)
{
    unsigned int access = req->access;

    reply->handle = 0;

    if (!req->thread)
    {
        struct process *process = get_process_from_handle(req->handle, PROCESS_ALL_ACCESS /* ?? */);
        if (!process) return;
        reply->handle = alloc_handle( current->process, process->token, access, 0 );
        release_object(process);
    }
    else
    {
        fprintf(stderr, "need to support thread tokens\n");
    }
}

