xref: /onnv-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/ipc_action.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <sys/types.h>
30*0Sstevel@tonic-gate #include <sys/poll.h>
31*0Sstevel@tonic-gate #include <dhcpmsg.h>
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #include "interface.h"
34*0Sstevel@tonic-gate #include "ipc_action.h"
35*0Sstevel@tonic-gate #include "util.h"
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate static iu_tq_callback_t	ipc_action_timeout;
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * ipc_action_init(): initializes the ipc_action structure
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  *   input: struct ifslist *: the interface to initialize it for
43*0Sstevel@tonic-gate  *  output: void
44*0Sstevel@tonic-gate  */
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate void
47*0Sstevel@tonic-gate ipc_action_init(struct ifslist *ifsp)
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate 	ifsp->if_ia.ia_tid = -1;
50*0Sstevel@tonic-gate }
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate /*
53*0Sstevel@tonic-gate  * ipc_action_start(): starts an ipc_action request on an interface
54*0Sstevel@tonic-gate  *
55*0Sstevel@tonic-gate  *   input: struct ifslist *: the interface to start the action on
56*0Sstevel@tonic-gate  *	    dhcp_ipc_request_t *: the type of request
57*0Sstevel@tonic-gate  *	    int: the descriptor to contact the action requestor
58*0Sstevel@tonic-gate  *  output: int: 1 if the request is started successfully, 0 otherwise
59*0Sstevel@tonic-gate  */
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate int
62*0Sstevel@tonic-gate ipc_action_start(struct ifslist *ifsp, dhcp_ipc_request_t *request, int fd)
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate 	if (request->timeout == DHCP_IPC_WAIT_DEFAULT)
65*0Sstevel@tonic-gate 		request->timeout = DHCP_IPC_DEFAULT_WAIT;
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate 	ifsp->if_ia.ia_request	= request;
68*0Sstevel@tonic-gate 	ifsp->if_ia.ia_fd	= fd;
69*0Sstevel@tonic-gate 	ifsp->if_ia.ia_cmd	= DHCP_IPC_CMD(request->message_type);
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	if (request->timeout == DHCP_IPC_WAIT_FOREVER)
72*0Sstevel@tonic-gate 		ifsp->if_ia.ia_tid = -1;
73*0Sstevel@tonic-gate 	else {
74*0Sstevel@tonic-gate 		ifsp->if_ia.ia_tid = iu_schedule_timer(tq, request->timeout,
75*0Sstevel@tonic-gate 		    ipc_action_timeout, ifsp);
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 		if (ifsp->if_ia.ia_tid == -1)
78*0Sstevel@tonic-gate 			return (0);
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 		hold_ifs(ifsp);
81*0Sstevel@tonic-gate 	}
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 	return (1);
84*0Sstevel@tonic-gate }
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*
87*0Sstevel@tonic-gate  * ipc_action_pending(): checks if there is a pending ipc_action request on
88*0Sstevel@tonic-gate  *			 an interface
89*0Sstevel@tonic-gate  *
90*0Sstevel@tonic-gate  *   input: struct ifslist *: the interface to check for a pending ipc_action on
91*0Sstevel@tonic-gate  *  output: boolean_t: B_TRUE if there is a pending ipc_action request
92*0Sstevel@tonic-gate  */
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate boolean_t
95*0Sstevel@tonic-gate ipc_action_pending(struct ifslist *ifsp)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	struct pollfd		pollfd;
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	if (ifsp->if_ia.ia_fd > 0) {
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 		pollfd.events	= POLLIN;
102*0Sstevel@tonic-gate 		pollfd.fd	= ifsp->if_ia.ia_fd;
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 		if (poll(&pollfd, 1, 0) == 0)
105*0Sstevel@tonic-gate 			return (B_TRUE);
106*0Sstevel@tonic-gate 	}
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate 	return (B_FALSE);
109*0Sstevel@tonic-gate }
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate /*
112*0Sstevel@tonic-gate  * ipc_action_finish(): completes an ipc_action request on an interface
113*0Sstevel@tonic-gate  *
114*0Sstevel@tonic-gate  *   input: struct ifslist *: the interface to complete the action on
115*0Sstevel@tonic-gate  *	    int: the reason why the action finished (nonzero on error)
116*0Sstevel@tonic-gate  *  output: void
117*0Sstevel@tonic-gate  */
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate void
120*0Sstevel@tonic-gate ipc_action_finish(struct ifslist *ifsp, int reason)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate 	struct ipc_action *ia = &ifsp->if_ia;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 	if (ipc_action_pending(ifsp) == B_FALSE)
125*0Sstevel@tonic-gate 		return;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	if (reason == 0)
128*0Sstevel@tonic-gate 		send_ok_reply(ia->ia_request, &ia->ia_fd);
129*0Sstevel@tonic-gate 	else
130*0Sstevel@tonic-gate 		send_error_reply(ia->ia_request, reason, &ia->ia_fd);
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	ipc_action_cancel_timer(ifsp);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate /*
136*0Sstevel@tonic-gate  * ipc_action_timeout(): times out an ipc_action on an interface (the request
137*0Sstevel@tonic-gate  *			 continues asynchronously, however)
138*0Sstevel@tonic-gate  *
139*0Sstevel@tonic-gate  *   input: iu_tq_t *: unused
140*0Sstevel@tonic-gate  *	    void *: the struct ifslist * the ipc_action was pending on
141*0Sstevel@tonic-gate  *  output: void
142*0Sstevel@tonic-gate  */
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate /* ARGSUSED */
145*0Sstevel@tonic-gate static void
146*0Sstevel@tonic-gate ipc_action_timeout(iu_tq_t *tq, void *arg)
147*0Sstevel@tonic-gate {
148*0Sstevel@tonic-gate 	struct ifslist		*ifsp = (struct ifslist *)arg;
149*0Sstevel@tonic-gate 	struct ipc_action	*ia = &ifsp->if_ia;
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 	if (check_ifs(ifsp) == 0) {
152*0Sstevel@tonic-gate 		(void) release_ifs(ifsp);
153*0Sstevel@tonic-gate 		return;
154*0Sstevel@tonic-gate 	}
155*0Sstevel@tonic-gate 
156*0Sstevel@tonic-gate 	dhcpmsg(MSG_VERBOSE, "ipc timeout waiting for agent to complete "
157*0Sstevel@tonic-gate 	    "command %d for %s", ia->ia_cmd, ifsp->if_name);
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	send_error_reply(ia->ia_request, DHCP_IPC_E_TIMEOUT, &ia->ia_fd);
160*0Sstevel@tonic-gate 	ia->ia_tid = -1;
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate /*
164*0Sstevel@tonic-gate  * ipc_action_cancel_timer(): cancels the pending ipc_action timer for this
165*0Sstevel@tonic-gate  *			      request
166*0Sstevel@tonic-gate  *
167*0Sstevel@tonic-gate  *   input: struct ifslist *: the interface with a pending request to cancel
168*0Sstevel@tonic-gate  *  output: void
169*0Sstevel@tonic-gate  */
170*0Sstevel@tonic-gate 
171*0Sstevel@tonic-gate void
172*0Sstevel@tonic-gate ipc_action_cancel_timer(struct ifslist *ifsp)
173*0Sstevel@tonic-gate {
174*0Sstevel@tonic-gate 	if (ifsp->if_ia.ia_tid == -1)
175*0Sstevel@tonic-gate 		return;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	/*
178*0Sstevel@tonic-gate 	 * if we can't cancel this timer, we're really in the
179*0Sstevel@tonic-gate 	 * twilight zone.  however, as long as we don't drop the
180*0Sstevel@tonic-gate 	 * reference to the ifsp, it shouldn't hurt us
181*0Sstevel@tonic-gate 	 */
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	if (iu_cancel_timer(tq, ifsp->if_ia.ia_tid, NULL) == 1) {
184*0Sstevel@tonic-gate 		ifsp->if_ia.ia_tid = -1;
185*0Sstevel@tonic-gate 		(void) release_ifs(ifsp);
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate }
188