useful debugging changes
[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 *
494f64df 12 * @(#)pk_acct.c 7.6 (Berkeley) %G%
94b7ce3b 13 */
5b7aaaef 14
c5c1ab43 15#include "param.h"
5b7aaaef 16#include "systm.h"
494f64df 17#include "namei.h"
4507dea2 18#include "proc.h"
5b7aaaef 19#include "vnode.h"
5b7aaaef 20#include "kernel.h"
4507dea2 21#include "file.h"
4507dea2
KS
22#include "socket.h"
23#include "socketvar.h"
5b7aaaef 24
4507dea2 25#include "../net/if.h"
5b7aaaef 26
4507dea2
KS
27#include "x25.h"
28#include "pk.h"
29#include "pk_var.h"
30#include "x25acct.h"
5b7aaaef 31
5b7aaaef 32
4507dea2 33struct vnode *pkacctp;
5b7aaaef
KS
34/*
35 * Turn on packet accounting
36 */
37
38pk_accton (path)
39 char *path;
40{
4507dea2 41 register struct vnode *vp = NULL;
494f64df 42 struct nameidata nd;
4507dea2 43 struct vnode *oacctp = pkacctp;
494f64df 44 struct proc *p = curproc;
4507dea2
KS
45 int error;
46
494f64df
KS
47 if (path == 0)
48 goto close;
49 nd.ni_segflg = UIO_USERSPACE;
50 nd.ni_dirp = path;
51 if (error = vn_open (&nd, p, FWRITE, 0644))
5b7aaaef 52 return (error);
494f64df
KS
53 vp = nd.ni_vp;
54 VOP_UNLOCK(vp);
4507dea2
KS
55 if (vp -> v_type != VREG) {
56 vrele (vp);
5b7aaaef
KS
57 return (EACCES);
58 }
5b7aaaef 59 pkacctp = vp;
494f64df
KS
60 if (oacctp) {
61 close:
62 error = vn_close (oacctp, FWRITE, p -> p_ucred, p);
5b7aaaef 63 }
494f64df 64 return (error);
5b7aaaef
KS
65}
66
67/*
68 * Write a record on the accounting file.
69 */
70
71pk_acct (lcp)
72register struct pklcd *lcp;
73{
5b7aaaef 74 register struct vnode *vp;
5b7aaaef
KS
75 register struct sockaddr_x25 *sa;
76 register char *src, *dst;
77 register int len;
5b7aaaef 78 register long etime;
5b7aaaef
KS
79 static struct x25acct acbuf;
80
5b7aaaef 81 if ((vp = pkacctp) == 0)
5b7aaaef 82 return;
5b7aaaef
KS
83 bzero ((caddr_t)&acbuf, sizeof (acbuf));
84 if (lcp -> lcd_ceaddr != 0)
85 sa = lcp -> lcd_ceaddr;
86 else if (lcp -> lcd_craddr != 0) {
87 sa = lcp -> lcd_craddr;
88 acbuf.x25acct_callin = 1;
89 } else
90 return;
91
92 if (sa -> x25_opts.op_flags & X25_REVERSE_CHARGE)
93 acbuf.x25acct_revcharge = 1;
94 acbuf.x25acct_stime = lcp -> lcd_stime;
5b7aaaef 95 acbuf.x25acct_etime = time.tv_sec - acbuf.x25acct_stime;
494f64df 96 acbuf.x25acct_uid = curproc -> p_cred -> p_ruid;
5b7aaaef
KS
97 acbuf.x25acct_psize = sa -> x25_opts.op_psize;
98 acbuf.x25acct_net = sa -> x25_net;
99 /*
100 * Convert address to bcd
101 */
102 src = sa -> x25_addr;
103 dst = acbuf.x25acct_addr;
104 for (len = 0; *src; len++)
105 if (len & 01)
106 *dst++ |= *src++ & 0xf;
107 else
108 *dst = *src++ << 4;
109 acbuf.x25acct_addrlen = len;
110
111 bcopy (sa -> x25_udata, acbuf.x25acct_udata,
112 sizeof (acbuf.x25acct_udata));
113 acbuf.x25acct_txcnt = lcp -> lcd_txcnt;
114 acbuf.x25acct_rxcnt = lcp -> lcd_rxcnt;
115
5b7aaaef 116 (void) vn_rdwr(UIO_WRITE, vp, (caddr_t)&acbuf, sizeof (acbuf),
494f64df
KS
117 (off_t)0, UIO_SYSSPACE, IO_UNIT|IO_APPEND,
118 curproc -> p_ucred, (int *)0,
759cbc90 119 (struct proc *)0);
5b7aaaef 120}