convert VOP_LOCK to vn_lock; add parameters to VOP_UNLOCK and vget
[unix-history] / usr / src / sys / netccitt / pk_acct.c
CommitLineData
94b7ce3b
KS
1/*
2 * Copyright (c) University of British Columbia, 1984
e7a3707f
KB
3 * Copyright (c) 1990, 1993
4 * The Regents of the University of California. All rights reserved.
94b7ce3b
KS
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 *
425b3035 12 * @(#)pk_acct.c 8.2 (Berkeley) %G%
94b7ce3b 13 */
5b7aaaef 14
5548a02f
KB
15#include <sys/param.h>
16#include <sys/systm.h>
17#include <sys/namei.h>
18#include <sys/proc.h>
19#include <sys/vnode.h>
20#include <sys/kernel.h>
21#include <sys/file.h>
22#include <sys/socket.h>
23#include <sys/socketvar.h>
5b7aaaef 24
5548a02f 25#include <net/if.h>
5b7aaaef 26
5548a02f
KB
27#include <netccitt/x25.h>
28#include <netccitt/pk.h>
29#include <netccitt/pk_var.h>
30#include <netccitt/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;
1ed56c80
KS
49 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
50 if (error = vn_open (&nd, FWRITE, 0644))
5b7aaaef 51 return (error);
494f64df 52 vp = nd.ni_vp;
425b3035 53 VOP_UNLOCK(vp, 0, p);
4507dea2
KS
54 if (vp -> v_type != VREG) {
55 vrele (vp);
5b7aaaef
KS
56 return (EACCES);
57 }
5b7aaaef 58 pkacctp = vp;
494f64df
KS
59 if (oacctp) {
60 close:
61 error = vn_close (oacctp, FWRITE, p -> p_ucred, p);
5b7aaaef 62 }
494f64df 63 return (error);
5b7aaaef
KS
64}
65
66/*
67 * Write a record on the accounting file.
68 */
69
70pk_acct (lcp)
71register struct pklcd *lcp;
72{
5b7aaaef 73 register struct vnode *vp;
5b7aaaef
KS
74 register struct sockaddr_x25 *sa;
75 register char *src, *dst;
76 register int len;
5b7aaaef 77 register long etime;
5b7aaaef
KS
78 static struct x25acct acbuf;
79
5b7aaaef 80 if ((vp = pkacctp) == 0)
5b7aaaef 81 return;
5b7aaaef
KS
82 bzero ((caddr_t)&acbuf, sizeof (acbuf));
83 if (lcp -> lcd_ceaddr != 0)
84 sa = lcp -> lcd_ceaddr;
85 else if (lcp -> lcd_craddr != 0) {
86 sa = lcp -> lcd_craddr;
87 acbuf.x25acct_callin = 1;
88 } else
89 return;
90
91 if (sa -> x25_opts.op_flags & X25_REVERSE_CHARGE)
92 acbuf.x25acct_revcharge = 1;
93 acbuf.x25acct_stime = lcp -> lcd_stime;
5b7aaaef 94 acbuf.x25acct_etime = time.tv_sec - acbuf.x25acct_stime;
494f64df 95 acbuf.x25acct_uid = curproc -> p_cred -> p_ruid;
5b7aaaef
KS
96 acbuf.x25acct_psize = sa -> x25_opts.op_psize;
97 acbuf.x25acct_net = sa -> x25_net;
98 /*
99 * Convert address to bcd
100 */
101 src = sa -> x25_addr;
102 dst = acbuf.x25acct_addr;
103 for (len = 0; *src; len++)
104 if (len & 01)
105 *dst++ |= *src++ & 0xf;
106 else
107 *dst = *src++ << 4;
108 acbuf.x25acct_addrlen = len;
109
110 bcopy (sa -> x25_udata, acbuf.x25acct_udata,
111 sizeof (acbuf.x25acct_udata));
112 acbuf.x25acct_txcnt = lcp -> lcd_txcnt;
113 acbuf.x25acct_rxcnt = lcp -> lcd_rxcnt;
114
5b7aaaef 115 (void) vn_rdwr(UIO_WRITE, vp, (caddr_t)&acbuf, sizeof (acbuf),
494f64df
KS
116 (off_t)0, UIO_SYSSPACE, IO_UNIT|IO_APPEND,
117 curproc -> p_ucred, (int *)0,
759cbc90 118 (struct proc *)0);
5b7aaaef 119}