more-or-less working with new proc & user structs
[unix-history] / usr / src / sys / netccitt / pk_acct.c
CommitLineData
94b7ce3b
KS
1/*
2 * Copyright (c) University of British Columbia, 1984
3 * Copyright (c) 1990 The Regents of the University of California.
4 * All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * the Laboratory for Computation Vision and the Computer Science Department
8 * of the University of British Columbia.
9 *
10 * %sccs.include.redist.c%
11 *
c5c1ab43 12 * @(#)pk_acct.c 7.4 (Berkeley) %G%
94b7ce3b 13 */
5b7aaaef 14
c5c1ab43 15#include "param.h"
5b7aaaef 16#include "systm.h"
4507dea2
KS
17#include "time.h"
18#include "proc.h"
5b7aaaef
KS
19#include "user.h"
20#include "vnode.h"
5b7aaaef 21#include "kernel.h"
4507dea2
KS
22#include "file.h"
23#include "acct.h"
5b7aaaef 24#include "uio.h"
4507dea2
KS
25#include "socket.h"
26#include "socketvar.h"
5b7aaaef 27
4507dea2 28#include "../net/if.h"
5b7aaaef 29
4507dea2
KS
30#include "x25.h"
31#include "pk.h"
32#include "pk_var.h"
33#include "x25acct.h"
5b7aaaef 34
5b7aaaef 35
4507dea2 36struct vnode *pkacctp;
5b7aaaef
KS
37/*
38 * Turn on packet accounting
39 */
40
41pk_accton (path)
42 char *path;
43{
4507dea2
KS
44 register struct vnode *vp = NULL;
45 register struct nameidata *ndp = &u.u_nd;
46 struct vnode *oacctp = pkacctp;
47 int error;
48
49 ndp -> ni_segflg = UIO_USERSPACE;
50 ndp -> ni_dirp = path;
51 if (error = vn_open (ndp, FREAD|FWRITE, 0))
5b7aaaef 52 return (error);
4507dea2
KS
53 vp = ndp -> ni_vp;
54 if (vp -> v_type != VREG) {
55 vrele (vp);
5b7aaaef
KS
56 return (EACCES);
57 }
5b7aaaef 58 pkacctp = vp;
4507dea2
KS
59 if (oacctp)
60 vrele (oacctp);
5b7aaaef
KS
61 return (0);
62}
63
64/*
65 * Turn off packet accounting
66 */
67
68pk_acctoff ()
69{
70 if (pkacctp) {
4507dea2 71 vrele (pkacctp);
5b7aaaef
KS
72 pkacctp = 0;
73 }
74}
75
76/*
77 * Write a record on the accounting file.
78 */
79
80pk_acct (lcp)
81register struct pklcd *lcp;
82{
5b7aaaef 83 register struct vnode *vp;
5b7aaaef
KS
84 register struct sockaddr_x25 *sa;
85 register char *src, *dst;
86 register int len;
5b7aaaef 87 register long etime;
5b7aaaef
KS
88 static struct x25acct acbuf;
89
5b7aaaef 90 if ((vp = pkacctp) == 0)
5b7aaaef 91 return;
5b7aaaef
KS
92 bzero ((caddr_t)&acbuf, sizeof (acbuf));
93 if (lcp -> lcd_ceaddr != 0)
94 sa = lcp -> lcd_ceaddr;
95 else if (lcp -> lcd_craddr != 0) {
96 sa = lcp -> lcd_craddr;
97 acbuf.x25acct_callin = 1;
98 } else
99 return;
100
101 if (sa -> x25_opts.op_flags & X25_REVERSE_CHARGE)
102 acbuf.x25acct_revcharge = 1;
103 acbuf.x25acct_stime = lcp -> lcd_stime;
5b7aaaef 104 acbuf.x25acct_etime = time.tv_sec - acbuf.x25acct_stime;
5b7aaaef
KS
105 acbuf.x25acct_uid = u.u_uid;
106 acbuf.x25acct_psize = sa -> x25_opts.op_psize;
107 acbuf.x25acct_net = sa -> x25_net;
108 /*
109 * Convert address to bcd
110 */
111 src = sa -> x25_addr;
112 dst = acbuf.x25acct_addr;
113 for (len = 0; *src; len++)
114 if (len & 01)
115 *dst++ |= *src++ & 0xf;
116 else
117 *dst = *src++ << 4;
118 acbuf.x25acct_addrlen = len;
119
120 bcopy (sa -> x25_udata, acbuf.x25acct_udata,
121 sizeof (acbuf.x25acct_udata));
122 acbuf.x25acct_txcnt = lcp -> lcd_txcnt;
123 acbuf.x25acct_rxcnt = lcp -> lcd_rxcnt;
124
5b7aaaef 125 (void) vn_rdwr(UIO_WRITE, vp, (caddr_t)&acbuf, sizeof (acbuf),
4507dea2 126 (off_t)0, UIO_SYSSPACE, IO_UNIT|IO_APPEND, u.u_cred, (int *)0);
5b7aaaef 127}