FreeBSD/Linux Kernel Cross Reference
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
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD: releng/5.2/sys/netncp/ncp_subr.c 116189 2003-06-11 05:37:42Z obrien $");
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/errno.h>
39 #include <sys/eventhandler.h>
40 #include <sys/kernel.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/proc.h>
44 #include <sys/sysctl.h>
45 #include <sys/time.h>
46
47 #include <netncp/ncp.h>
48 #include <netncp/ncp_conn.h>
49 #include <netncp/ncp_sock.h>
50 #include <netncp/ncp_subr.h>
51 #include <netncp/ncp_rq.h>
52 #include <netncp/ncp_ncp.h>
53 #include <netncp/nwerror.h>
54
55 int ncp_debuglevel = 0;
56
57 struct callout_handle ncp_timer_handle;
58 static eventhandler_tag ncp_exit_tag;
59
60 static void ncp_at_exit(void *arg, struct proc *p);
61 static void ncp_timer(void *arg);
62
63 /*
64 * duplicate string from user space. It should be very-very slow.
65 */
66 char *
67 ncp_str_dup(char *s) {
68 char *p, bt;
69 int len = 0;
70
71 for (p = s;;p++) {
72 if (copyin(p, &bt, 1)) return NULL;
73 len++;
74 if (bt == 0) break;
75 }
76 MALLOC(p, char*, len, M_NCPDATA, M_WAITOK);
77 copyin(s, p, len);
78 return p;
79 }
80
81
82 void
83 ncp_at_exit(void *arg, struct proc *p)
84 {
85 struct ncp_conn *ncp, *nncp;
86 struct thread *td;
87
88 FOREACH_THREAD_IN_PROC(p, td) {
89 if (ncp_conn_putprochandles(td) == 0)
90 continue;
91
92 ncp_conn_locklist(LK_EXCLUSIVE, td);
93 for (ncp = SLIST_FIRST(&conn_list); ncp; ncp = nncp) {
94 nncp = SLIST_NEXT(ncp, nc_next);
95 if (ncp_conn_lock(ncp, td, td->td_ucred,
96 NCPM_READ | NCPM_EXECUTE | NCPM_WRITE))
97 continue;
98 if (ncp_conn_free(ncp) != 0)
99 ncp_conn_unlock(ncp, td);
100 }
101 ncp_conn_unlocklist(td);
102 }
103 }
104
105 int
106 ncp_init(void)
107 {
108 ncp_conn_init();
109 ncp_exit_tag = EVENTHANDLER_REGISTER(process_exit, ncp_at_exit, NULL,
110 EVENTHANDLER_PRI_ANY);
111 ncp_timer_handle = timeout(ncp_timer, NULL, NCP_TIMER_TICK);
112 return 0;
113 }
114
115 int
116 ncp_done(void)
117 {
118 int error;
119
120 error = ncp_conn_destroy();
121 if (error)
122 return error;
123 untimeout(ncp_timer, NULL, ncp_timer_handle);
124 EVENTHANDLER_DEREGISTER(process_exit, ncp_exit_tag);
125 return 0;
126 }
127
128
129 /* tick every second and check for watch dog packets and lost connections */
130 static void
131 ncp_timer(void *arg)
132 {
133 struct ncp_conn *conn;
134
135 if(ncp_conn_locklist(LK_SHARED | LK_NOWAIT, NULL) == 0) {
136 SLIST_FOREACH(conn, &conn_list, nc_next)
137 ncp_check_conn(conn);
138 ncp_conn_unlocklist(NULL);
139 }
140 ncp_timer_handle = timeout(ncp_timer, NULL, NCP_TIMER_TICK);
141 }
Cache object: 63be5d3344e6b483e2d40fcee6610d32
|