#ident "$Header: session.c 1.4 89/08/07 $"

/*
 * $Copyright:	$
 * Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
 * Sequent Computer Systems, Inc.   All rights reserved.
 *  
 * This software is furnished under a license and may be used
 * only in accordance with the terms of that license and with the
 * inclusion of the above copyright notice.   This software may not
 * be provided or otherwise made available to, or used by, any
 * other person.  No title to or ownership of the software is
 * hereby transferred.
 */

/*
 * $Log:	session.c,v $
 */

#include "crash.h"

#include <sys/types.h>
#include <sys/param.h>
#include <sys/mutex.h>
#include <sys/systm.h>
#include <sys/map.h>
#include <sys/dir.h>
#include <sys/resource.h>	/* needed for user.h */
#include <signal.h>
#include <sys/user.h>
#include <sys/timer.h>		/* needed for proc.h */
#include <sys/session.h>	/* needed for proc.h */
#include <sys/proc.h>
#include <sys/vnode.h>

#define	INKERNEL
#include <sys/var.h>

struct	session *sess;
struct session *getsess();
struct session *readsess();
static	ast;

sess_init()
{
	extern struct var	V;

	readv(search("sess"), &sess, sizeof sess);
	if (V.v_proc == 0)
		proc_init();
}

Sess()
{
	struct session *s;
	int 		l=0, r=0, n;
	int 		i = 0;		/* for debug function only */
	char 		*arg;
	struct	proc	*p;

	if (live)
		sess_init();
	ast = 0;
	printf("Addr     Slot s_sid    s_pgrp      s_vp     v_rdev    s_ctty c_sid c_pgrp\n");
	/*      0x666666 4444 55555 0x7777777 0x7777777 0x88888888 0x7777777 55555 55555*/
	arg = token();
	if(arg == NULL) {
		for(s = sess; s < &sess[V.v_proc]; s++) {
			prsess(s, 0);
		}
	} else {
		do {
			if (p = getproc(arg)) {
				if (readv(&p->p_session, &s, sizeof (s)) != sizeof (s) ) 
					break;
				prsess(s, 1);
			}
		} while((arg = token()) != NULL);
	}
}

prsess(ss, all)
register struct session *ss;
int all;
{
	register struct session *s;
	struct	vnode	*v;
	extern	struct	vnode	*readvnode();
	struct ctty		ctty;
	pid_t		pgrp;
	pid_t		sid;

	if ((s = readsess(ss)) == NULL)
		return;
	if (!all && ((s->s_sid == 0) && (ss != sess)))
		return;

	printf("0x%6.6x %4.4d", ss, ss-sess);
	printf(" %5u", s->s_sid);
	printf(" 0x%7.7x ",s->s_pgrp);
	if ((s->s_vp) && (v = readvnode(s->s_vp)))
		printf(" 0x%7.7x 0x%8.8x", s->s_vp, v->v_rdev);
	else
		printf("         -          -");
		/*       0x7777777 0x88888888 */
	if (s->s_ctty) {
		readv(s->s_ctty, &ctty, sizeof (ctty) );
		printf(" 0x%7.7x %5u %5u", s->s_ctty, ctty.c_sid, ctty.c_pgrp);
	} else {
		printf("         -     -     -" );
		/*       0x7777777 55555 55555*/
	}
	printf("\n");
}

struct session *
getsess(arg)
	char *arg;
{
	int n = atoi(arg);
	if ( err_atoi ) {
		printf("'%s', %s\n", arg, err_atoi);
		return NULL;
	}
	if( n > (V.v_proc) ) {
		printf("%d out of range\n", n);
		return NULL;
	}
	return (sess+n);
}

struct session *
readsess(ss)
struct session *ss;
{
	static struct session sessbuf;

	if(readv(ss, &sessbuf, sizeof sessbuf) != sizeof sessbuf) {
		printf("%3d  read error on ession table\n", ss-sess);
		return NULL;
	}
	return &sessbuf;
}
