/*
 *  This file is part of ixemul.library for the Amiga.
 *  Copyright (C) 1991, 1992  Markus M. Wild
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#define _KERNEL
#include "ixemul.h"
#include "kprintf.h"
#include <hardware/intbits.h>
#include "multiuser.h"
  
void
__ix_close_muFS( struct user *ix_u )
{
  if (muBase)
    {
      if (ix_u->u_UserInfo)
        {
          muFreeUserInfo(ix_u->u_UserInfo);
          ix_u->u_UserInfo = NULL;
        }

      if (ix_u->u_fileUserInfo)
        {
          muFreeUserInfo(ix_u->u_fileUserInfo);
          ix_u->u_fileUserInfo = NULL;
        }

      if (ix_u->u_GroupInfo)
        {
          muFreeGroupInfo(ix_u->u_GroupInfo);
         ix_u->u_GroupInfo = NULL;
        }

      if (ix_u->u_fileGroupInfo)
        {
          muFreeGroupInfo(ix_u->u_fileGroupInfo);
          ix_u->u_fileGroupInfo = NULL;
        }

      /* log me out */
      while (ix_u->u_setuid--)
        muLogout(muT_Quiet, TRUE, TAG_DONE);
    }
}

void
ix_close (struct ixemul_base *ixbase)
{
  struct Task 		*me = SysBase->ThisTask;
  struct user 		*ix_u =	getuser(me);
  struct Process	*child;
  struct user		*cu;
  struct Node		*dm, *ndm;	/* really struct death_msg * */

  RemIntServer (INTB_VERTB, &ix_u->u_itimerint);

#ifndef NOTRAP
  /* already reset the trap vector here. It's better to get an alert than
   * to loop infinitely if one of the following functions should crash */
  me->tc_TrapCode = ix_u->u_otrap_code;
#endif
  
  freestack();

  /* had to move this block after the SYS_close's, since close() might have
     to wait for a packet, and then it's essential that our switch/launch
     handlers are still active */
  me->tc_Flags    = ix_u->u_otask_flags;
  me->tc_Launch	  = ix_u->u_olaunch;
  me->tc_Switch   = ix_u->u_oswitch;
  FreeSignal (ix_u->u_sleep_sig);
  FreeSignal (ix_u->u_pipe_sig);

  if (ix_u->u_ixnetbase)
    {
      CloseLibrary (ix_u->u_ixnetbase);
    }

  CloseDevice ((struct IORequest *) ix_u->u_time_req);
  syscall (SYS_DeleteExtIO, ix_u->u_time_req);
  
  if (ix_u->u_startup_cd != (BPTR) -1)
    {
      __unlock (CurrentDir (ix_u->u_startup_cd));
      set_dir_name_from_lock(ix_u->u_startup_cd);
    }

  ix_u->u_trace_flags = 1;
  DeletePort (ix_u->u_select_mp);
  DeletePort (ix_u->u_sync_mp);

  /* try to free it here */
  __ix_close_muFS(ix_u);

  for ((child = ix_u->p_cptr); child; (child = cu->p_osptr))
    {
      cu = getuser(child);
      cu->p_pptr = (struct Process *) 1;
    }

  ix_u->p_cptr = 0;

  /* decrease session count and possibly free the session structure */
  if (ix_u->u_session)
    {
      if (ix_u->u_session->pgrp == (int)me)
        ix_u->u_session->pgrp = 0;
      if (ix_u->u_session->s_count-- <= 1)
        kfree(ix_u->u_session);
    }
  
  for (dm  = (struct Node *) ix_u->p_zombies.mlh_Head;
       (ndm = dm->ln_Succ);
       dm  = ndm)
    {
      /* there might be children sleeping on this, so wake them up now.. */
      ix_wakeup ((u_int)dm);
      kfree (dm);
    }

  FreeSignal (ix_u->p_zombie_sig);

  all_free ();
  if (ix_u->p_flag & SFREEA4)
    kfree ((void *)(ix_u->u_a4 - 0x7ffe));

#ifndef NOTRAP
  /* delay this until here, since the above called functions need access
   * to the user area. */
  getuser(me) = ix_u->u_otrap_data;
#endif

  kfree (((char *)ix_u) - ix_u->u_a4_pointers_size * 4);
}
