xref: /onnv-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/async.c (revision 3431:9f2d277dcffa)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52157Sdh155122  * Common Development and Distribution License (the "License").
62157Sdh155122  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*3431Scarlsonj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <dhcpmsg.h>
300Sstevel@tonic-gate #include <libinetutil.h>
31*3431Scarlsonj #include <dhcpagent_util.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #include "async.h"
340Sstevel@tonic-gate #include "util.h"
350Sstevel@tonic-gate #include "interface.h"
360Sstevel@tonic-gate #include "script_handler.h"
372159Sdh155122 #include "states.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate /*
40*3431Scarlsonj  * async_start(): starts an asynchronous command on a state machine
410Sstevel@tonic-gate  *
42*3431Scarlsonj  *   input: dhcp_smach_t *: the state machine to start the async command on
43*3431Scarlsonj  *	    dhcp_ipc_type_t: the command to start
44*3431Scarlsonj  *	    boolean_t: B_TRUE if the command was started by a user
45*3431Scarlsonj  *  output: boolean: B_TRUE on success, B_FALSE on failure
460Sstevel@tonic-gate  */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate boolean_t
async_start(dhcp_smach_t * dsmp,dhcp_ipc_type_t cmd,boolean_t user)49*3431Scarlsonj async_start(dhcp_smach_t *dsmp, dhcp_ipc_type_t cmd, boolean_t user)
500Sstevel@tonic-gate {
51*3431Scarlsonj 	if (dsmp->dsm_async.as_present) {
520Sstevel@tonic-gate 		return (B_FALSE);
53*3431Scarlsonj 	} else {
54*3431Scarlsonj 		dsmp->dsm_async.as_cmd = cmd;
55*3431Scarlsonj 		dsmp->dsm_async.as_user = user;
56*3431Scarlsonj 		dsmp->dsm_async.as_present = B_TRUE;
570Sstevel@tonic-gate 		return (B_TRUE);
58*3431Scarlsonj 	}
590Sstevel@tonic-gate }
600Sstevel@tonic-gate 
610Sstevel@tonic-gate /*
620Sstevel@tonic-gate  * async_finish(): completes an asynchronous command
630Sstevel@tonic-gate  *
64*3431Scarlsonj  *   input: dhcp_smach_t *: the state machine with the pending async command
650Sstevel@tonic-gate  *  output: void
660Sstevel@tonic-gate  *    note: should only be used when the command has no residual state to
670Sstevel@tonic-gate  *	    clean up
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate void
async_finish(dhcp_smach_t * dsmp)71*3431Scarlsonj async_finish(dhcp_smach_t *dsmp)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 	/*
740Sstevel@tonic-gate 	 * be defensive here. the script may still be running if
750Sstevel@tonic-gate 	 * the asynchronous action times out before it is killed by the
760Sstevel@tonic-gate 	 * script helper process.
770Sstevel@tonic-gate 	 */
780Sstevel@tonic-gate 
79*3431Scarlsonj 	if (dsmp->dsm_script_pid != -1)
80*3431Scarlsonj 		script_stop(dsmp);
81*3431Scarlsonj 	dsmp->dsm_async.as_present = B_FALSE;
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * async_cancel(): cancels a pending asynchronous command
860Sstevel@tonic-gate  *
87*3431Scarlsonj  *   input: dhcp_smach_t *: the state machine with the pending async command
88*3431Scarlsonj  *  output: boolean: B_TRUE if cancellation was successful, B_FALSE on failure
890Sstevel@tonic-gate  */
900Sstevel@tonic-gate 
91*3431Scarlsonj boolean_t
async_cancel(dhcp_smach_t * dsmp)92*3431Scarlsonj async_cancel(dhcp_smach_t *dsmp)
930Sstevel@tonic-gate {
94*3431Scarlsonj 	if (!dsmp->dsm_async.as_present)
95*3431Scarlsonj 		return (B_TRUE);
96*3431Scarlsonj 	if (dsmp->dsm_async.as_user) {
97*3431Scarlsonj 		dhcpmsg(MSG_DEBUG,
98*3431Scarlsonj 		    "async_cancel: cannot abort command %d from user",
99*3431Scarlsonj 		    (int)dsmp->dsm_async.as_cmd);
100*3431Scarlsonj 		return (B_FALSE);
101*3431Scarlsonj 	} else {
102*3431Scarlsonj 		async_finish(dsmp);
103*3431Scarlsonj 		dhcpmsg(MSG_DEBUG, "async_cancel: command %d aborted",
104*3431Scarlsonj 		    (int)dsmp->dsm_async.as_cmd);
105*3431Scarlsonj 		return (B_TRUE);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate }
108