Example #1
0
static void
enter(struct ctmp *c)
{
	unsigned i;
	int j;
	struct ctab **pt;

	if ((pctab = (struct ctab *)malloc(sizeof (struct ctab))) == NULL) {
		fprintf(stderr, "acctcon: malloc fail!\n");
		exit(2);
	}

	pctab->ct_uid = c->ct_uid;
	CPYN(pctab->ct_name, c->ct_name);
	pctab->ct_con[0] = c->ct_con[0];
	pctab->ct_con[1] = c->ct_con[1];
	pctab->ct_sess = 1;

	if (*(pt = (struct ctab **)tsearch((void *)pctab, (void **)&root,  \
		node_compare)) == NULL) {
		fprintf(stderr, "Not enough space available to build tree\n");
		exit(1);
	}

	if (*pt != pctab) {
		(*pt)->ct_con[0] += c->ct_con[0];
		(*pt)->ct_con[1] += c->ct_con[1];
		(*pt)->ct_sess++;
		free(pctab);
	}

}
Example #2
0
void
enter(struct ptmp *p)
{
        double memk;
        struct utab **pt;
         
	if ((ub = (struct utab *)malloc(sizeof (struct utab))) == NULL) {
		fprintf(stderr, "acctprc: malloc fail!\n");
		exit(2);
	}

        ub->ut_uid = p->pt_uid;
        CPYN(ub->ut_name, p->pt_name);
        ub->ut_cpu[0] = MINT(p->pt_cpu[0]);
        ub->ut_cpu[1] = MINT(p->pt_cpu[1]);
        memk = KCORE(pb.pt_mem);  
        ub->ut_kcore[0] = memk * MINT(p->pt_cpu[0]);
        ub->ut_kcore[1] = memk * MINT(p->pt_cpu[1]);
        ub->ut_pc = 1;
         
        if (*(pt = (struct utab **)tsearch((void *)ub, (void **)&root,  \
                node_compare)) == NULL) {
                fprintf(stderr, "Not enough space available to build tree\n");
                exit(1);
	}

	if (*pt != ub) {
        	(*pt)->ut_cpu[0] += MINT(p->pt_cpu[0]);
        	(*pt)->ut_cpu[1] += MINT(p->pt_cpu[1]);
        	(*pt)->ut_kcore[0] += memk * MINT(p->pt_cpu[0]);
        	(*pt)->ut_kcore[1] += memk * MINT(p->pt_cpu[1]);
		(*pt)->ut_pc++;
		free(ub);
        }
}
Example #3
0
static void
print_node(const void *node, VISIT order, int level)
{
	if (order == postorder || order == leaf) {
		tb.ta_uid = (*(struct ctab **)node)->ct_uid;
		CPYN(tb.ta_name, (*(struct ctab **)node)->ct_name);
		tb.ta_con[0] = ((*(struct ctab **)node)->ct_con[0]) / 60.0;
		tb.ta_con[1] = ((*(struct ctab **)node)->ct_con[1]) / 60.0;
		tb.ta_sc = (*(struct ctab **)node)->ct_sess;
		fwrite(&tb, sizeof (tb), 1, stdout);
	}
}
Example #4
0
void print_node(const void *node, VISIT order, int level) {

	if (order == postorder || order == leaf) {
		tb.ta_uid = (*(struct utab **)node)->ut_uid;
		CPYN(tb.ta_name, (char *)uidtonam((*(struct utab **)node)->ut_uid));
		tb.ta_cpu[0] = (*(struct utab **)node)->ut_cpu[0];
		tb.ta_cpu[1] = (*(struct utab **)node)->ut_cpu[1];
                tb.ta_kcore[0] = (*(struct utab **)node)->ut_kcore[0];
                tb.ta_kcore[1] = (*(struct utab **)node)->ut_kcore[1];
                tb.ta_pc = (*(struct utab **)node)->ut_pc;
                fwrite(&tb, sizeof(tb), 1, stdout);
	}
}
Example #5
0
/*
 * bootshut: record reboot (or shutdown)
 * bump count, looking up wb.ut_line in sy table
 */
static void
bootshut()
{
	int i;

	for (i = 0; i < nsys && !EQN(wb.ut_line, sy[i].sname); i++)
		;
	if (i >= nsys) {
		if (++nsys > NSYS) {
			fprintf(stderr,
				"acctcon: recompile with larger NSYS\n");
			nsys = NSYS;
			return;
		}
		CPYN(sy[i].sname, wb.ut_line);
	}
	sy[i].snum++;
}
Example #6
0
int
main(int argc, char **argv)
{
	tb.ta_sc = 1;
	while (scanf("%lu\t%ld\t%s\t%lu\t%lu\t%lu\t%*[^\n]",
		&cb.ct_tty,
		&cb.ct_uid,
		cb.ct_name,
		&cb.ct_con[0],
		&cb.ct_con[1],
		&cb.ct_start) != EOF) {

		tb.ta_uid = cb.ct_uid;
		CPYN(tb.ta_name, cb.ct_name);
		tb.ta_con[0] = MINS(cb.ct_con[0]);
		tb.ta_con[1] = MINS(cb.ct_con[1]);
		fwrite(&tb, sizeof (tb), 1, stdout);
	}
	exit(0);
}
Example #7
0
int
main(int argc, char **argv)
{
	long		elaps[2];
	ulong_t		etime, stime;
	unsigned long	mem;
#ifdef uts
	float   expand();
#else
	ulong_t expand();
#endif

        while (fread(&ab, sizeof(ab), 1, stdin) == 1) {
                if (!MYKIND(ab.ac_flag))
                        continue;
                pb.pt_uid = ab.ac_uid;
                CPYN(pb.pt_name, NULL);
                /*
                 * approximate cpu P/NP split as same as elapsed time
                 */
                if ((etime = SECS(expand(ab.ac_etime))) == 0)
                        etime = 1;
                stime = expand(ab.ac_stime) + expand(ab.ac_utime);
                mem = expand(ab.ac_mem);
                if(pnpsplit(ab.ac_btime, etime, elaps) == 0) {
			fprintf(stderr, "acctprc: could not calculate prime/non-prime hours\n");

			exit(1);
		}
                pb.pt_cpu[0] = (double)stime * (double)elaps[0] / etime;
                pb.pt_cpu[1] = (stime > pb.pt_cpu[0])? stime - pb.pt_cpu[0] : 0;
                pb.pt_cpu[1] = stime - pb.pt_cpu[0];
                if (stime)
                        pb.pt_mem = (mem + stime - 1) / stime;
                else
                        pb.pt_mem = 0;  /* unlikely */
                enter(&pb);
        }
        output();
	exit(0);
}
Example #8
0
/*
 * iline: look up/enter current line name in tbuf, return index
 * (used to avoid system dependencies on naming)
 */
static int
iline()
{
	int i;

	for (i = 0; i <= tsize; i++)
		if (EQN(wb.ut_line, tbuf[i].tline))
			return (i);
	if (++tsize >= a_tsize) {
		a_tsize = a_tsize + A_TSIZE;
		if ((tbuf = (struct tbuf *)realloc(tbuf, a_tsize *
			sizeof (struct tbuf))) == NULL) {
			fprintf(stderr, "acctcon: Cannot reallocate memory\n");
			exit(2);
		}
	}

	CPYN(tbuf[tsize].tline, wb.ut_line);
	tbuf[tsize].tdev = lintodev(wb.ut_line);
	return (tsize);
}
Example #9
0
int
main(int argc, char **argv)
{
	int rc;

	tb.ta_dc = 1;
	while ((rc = scanf("%ld\t%s\t%f",
		&tb.ta_uid,
		ntmp,
		&tb.ta_du)) == 3) {

		CPYN(tb.ta_name, ntmp);
		fwrite(&tb, sizeof (tb), 1, stdout);
	}

	if (rc != EOF) {
		fprintf(stderr, "\nacctdisk: incorrect input format.\n");
		exit(1);
	} else {
		exit(0);
	}
}
Example #10
0
int
main(int argc, char **argv)
{
	long		elaps[2];
	ulong_t		etime, stime;
	unsigned long	mem;
	ulong_t		expand();
	int 		ver;	/* version of acct struct */
	int 		aread();

	if ((ur = (struct urec *) calloc(a_usize,
		sizeof (struct urec))) == NULL) {
		fprintf(stderr, "acctpr1: Cannot allocate memory\n");
		exit(3);
	}

	urlast = ur;
	if ((sr = (struct srec *) calloc(a_ssize,
		sizeof (struct srec))) == NULL) {
		fprintf(stderr, "acctpr1: Cannot allocate memory\n");
		exit(3);
	}

	while (--argc > 0) {
		if (**++argv == '-')
			switch(*++*argv) {
			}
		else {
			readctmp(*argv);
		}
	}


	if (fread((char *)&ab, sizeof(struct acct), 1, stdin) != 1)
		exit(1);
	else if (ab.ac_flag & AEXPND)
		ver = 2;	/* 4.0 acct structure */
	else 
		ver = 1;	/* 3.x acct structure */

	rewind(stdin);

	while (aread(ver) == 1) {
		if (!MYKIND(ab.ac_flag))
			continue;
		pb.pt_uid = ab.ac_uid;
		CPYN(pb.pt_name, getname(ab.ac_uid, ab.ac_tty, ab.ac_btime));
		/*
		 * approximate cpu P/NP split as same as elapsed time
		 */
		if ((etime = SECS(expand(ab.ac_etime))) == 0)
			etime = 1;
		stime = expand(ab.ac_stime) + expand(ab.ac_utime);
		mem = expand(ab.ac_mem);
		if(pnpsplit(ab.ac_btime, etime, elaps) == 0) {
			fprintf(stderr, "acctprc1: could not calculate prime/non-prime hours\n");

			exit(1);
		}
		pb.pt_cpu[0] = (double)stime * (double)elaps[0] / etime;
		pb.pt_cpu[1] = (stime > pb.pt_cpu[0])? stime - pb.pt_cpu[0] : 0;
		pb.pt_cpu[1] = stime - pb.pt_cpu[0];
		if (stime)
			pb.pt_mem = (mem + stime - 1) / stime;
		else
			pb.pt_mem = 0;	/* unlikely */
		printf("%ld\t%.*s\t%lu\t%lu\t%u\n",
		    pb.pt_uid,
		    OUTPUT_NSZ,
		    pb.pt_name,
		    pb.pt_cpu[0], pb.pt_cpu[1],
		    pb.pt_mem);
	}
	
	exit(0);
}
Example #11
0
/*
 *	read ctmp file, build up urec-srec data structures for
 *	later use by getnamc
 */
void
readctmp(char *fname)
{
	FILE *fp;
	struct urec *up;
	struct srec *sp;
	int i = 0, j = 0, k=0;

	if ((fp = fopen(fname, "r")) == NULL) {
		fprintf(stderr, "acctprc1: can't open %s\n", fname);
		return;
	}

	up = NULL;
	sp = sr;

	while (fscanf(fp, "%hd\t%ld\t%s\t%lu\t%lu\t%lu\t%*[^\n]",
		&cb.ct_tty,
		&cb.ct_uid,
		cb.ct_name,
		&cb.ct_con[0],
		&cb.ct_con[1],
		&cb.ct_start) != EOF) {
		if (up == NULL || cb.ct_uid != up->ur_uid ||
			!EQN(cb.ct_name, up->ur_name)) {
			if (up == NULL)
				up = ur;
			if (++up >= &ur[a_usize]) {
				a_usize = a_usize + A_USIZE;
                		if ((ur = (struct urec *) realloc(ur, a_usize *
					sizeof (struct urec))) == NULL) {
                        		fprintf(stderr, "acctprc1: 1 Cannot reallocate memory\n");
					exit(2);
				}
				up = &ur[a_usize - A_USIZE];
			}
			up->ur_uid = cb.ct_uid;
			CPYN(up->ur_name, cb.ct_name);
			up->ur_srec = sp;
			up->ur_cnt = 0;
		}
		
		if (sp >= &sr[a_ssize-1]) { 
			a_ssize = a_ssize + A_SSIZE;
			if ((sr = (struct srec *) realloc(sr, a_ssize *
				sizeof (struct srec))) == NULL) {
				fprintf(stderr, "acctprc1: 2 Cannot reallocate memory\n");
				printf("errno=%d\n", errno);
				exit(2);
			}
			sp = &sr[a_ssize - A_SSIZE];
		}

		sp->sr_tty = cb.ct_tty;
		sp->sr_start = cb.ct_start;
		sp->sr_end = cb.ct_start + cb.ct_con[0] + cb.ct_con[1];
		sp++;
		up->ur_cnt++;
	}
	if (up != NULL)
		urlast = ++up;
	fclose(fp);
}
Example #12
0
/*
 * update tbuf with new time, write ctmp record for end of session
 */
static void
update(struct tbuf *tp)
{
	time_t	told,	/* last time for tbuf record */
		tnew;	/* time of this record */
			/* Difference is connect time */

	told = tp->ttime;
	tnew = wb.ut_xtime;
	if (told > tnew) {
		cftime(time_buf, DATE_FMT, &told);
		fprintf(stderr, "acctcon: bad times: old: %s", time_buf);
		cftime(time_buf, DATE_FMT, &tnew);
		fprintf(stderr, "new: %s", time_buf);
		exitcode = 1;
		tp->ttime = tnew;
		return;
	}
	tp->ttime = tnew;
	switch (wb.ut_type) {
	case USER_PROCESS:
		tp->tlsess++;
		/*
		 * Someone logged in without logging off. Put out record.
		 */
		if (tp->tname[0] != '\0') {
			cb.ct_tty = tp->tdev;
			CPYN(cb.ct_name, tp->tname);
			cb.ct_uid = namtouid(cb.ct_name);
			cb.ct_start = told;
			if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
			    cb.ct_con) == 0) {
				fprintf(stderr, "acctcon: could not calculate "
				    "prime/non-prime hours\n");
				exit(1);
			}
			enter(&cb);
			tp->ttotal += tnew-told;
		} else	/* Someone just logged in */
			tp->tlon++;
		CPYN(tp->tname, wb.ut_name);
		break;
	case DEAD_PROCESS:
		tp->tloff++;
		if (tp->tname[0] != '\0') { /* Someone logged off */
			/* Set up and print ctmp record */
			cb.ct_tty = tp->tdev;
			CPYN(cb.ct_name, tp->tname);
			cb.ct_uid = namtouid(cb.ct_name);
			cb.ct_start = told;
			if (pnpsplit(cb.ct_start, (ulong_t)(tnew-told),
			    cb.ct_con) == 0) {
				fprintf(stderr, "acctcon: could not calculate "
				    "prime/non-prime hours\n");
				exit(1);
			}
			enter(&cb);
			tp->ttotal += tnew-told;
			tp->tname[0] = '\0';
		}
	}
}