]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netncp/ncp_subr.c
Define misc structs outside of struct firewire_comm.
[FreeBSD/FreeBSD.git] / sys / netncp / ncp_subr.c
1 /*
2  * Copyright (c) 1999, 2000, 2001 Boris Popov
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *    This product includes software developed by Boris Popov.
16  * 4. Neither the name of the author nor the names of any co-contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $FreeBSD$
33  */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/errno.h>
37 #include <sys/kernel.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/proc.h>
41 #include <sys/sysctl.h>
42 #include <sys/time.h>
43
44 #include <netncp/ncp.h>
45 #include <netncp/ncp_conn.h>
46 #include <netncp/ncp_sock.h>
47 #include <netncp/ncp_subr.h>
48 #include <netncp/ncp_rq.h>
49 #include <netncp/ncp_ncp.h>
50 #include <netncp/nwerror.h>
51
52 int ncp_debuglevel = 0;
53
54 struct callout_handle ncp_timer_handle;
55
56 static void ncp_at_exit(struct proc *p);
57 static void ncp_timer(void *arg);
58
59 /*
60  * duplicate string from user space. It should be very-very slow.
61  */
62 char *
63 ncp_str_dup(char *s) {
64         char *p, bt;
65         int len = 0;
66
67         for (p = s;;p++) {
68                 if (copyin(p, &bt, 1)) return NULL;
69                 len++;
70                 if (bt == 0) break;
71         }
72         MALLOC(p, char*, len, M_NCPDATA, 0);
73         copyin(s, p, len);
74         return p;
75 }
76
77
78 void
79 ncp_at_exit(struct proc *p)
80 {
81         struct ncp_conn *ncp, *nncp;
82
83         if (ncp_conn_putprochandles(p) == 0) return;
84
85         ncp_conn_locklist(LK_EXCLUSIVE, p);
86         for (ncp = SLIST_FIRST(&conn_list); ncp; ncp = nncp) {
87                 nncp = SLIST_NEXT(ncp, nc_next);
88                 if (ncp_conn_lock(ncp, p, p->p_ucred,NCPM_READ|NCPM_EXECUTE|NCPM_WRITE))
89                         continue;
90                 if (ncp_conn_free(ncp) != 0)
91                         ncp_conn_unlock(ncp,p);
92         }
93         ncp_conn_unlocklist(p);
94         return;
95 }
96
97 int
98 ncp_init(void)
99 {
100         ncp_conn_init();
101         if (at_exit(ncp_at_exit)) {
102                 NCPFATAL("can't register at_exit handler\n");
103                 return ENOMEM;
104         }
105         ncp_timer_handle = timeout(ncp_timer,NULL,NCP_TIMER_TICK);
106         return 0;
107 }
108
109 int
110 ncp_done(void)
111 {
112         int error;
113
114         error = ncp_conn_destroy();
115         if (error)
116                 return error;
117         untimeout(ncp_timer,NULL,ncp_timer_handle);
118         rm_at_exit(ncp_at_exit);
119         return 0;
120 }
121
122
123 /* tick every second and check for watch dog packets and lost connections */
124 static void
125 ncp_timer(void *arg)
126 {
127         struct ncp_conn *conn;
128
129         if(ncp_conn_locklist(LK_SHARED | LK_NOWAIT, NULL) == 0) {
130                 SLIST_FOREACH(conn, &conn_list, nc_next)
131                         ncp_check_conn(conn);
132                 ncp_conn_unlocklist(NULL);
133         }
134         ncp_timer_handle = timeout(ncp_timer,NULL,NCP_TIMER_TICK);
135 }