17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5c2934490Sdh155122 * Common Development and Distribution License (the "License").
6c2934490Sdh155122 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*d04ccbb3Scarlsonj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
237c478bd9Sstevel@tonic-gate * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
277c478bd9Sstevel@tonic-gate
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <dhcpmsg.h>
307c478bd9Sstevel@tonic-gate #include <libinetutil.h>
31*d04ccbb3Scarlsonj #include <dhcpagent_util.h>
327c478bd9Sstevel@tonic-gate
337c478bd9Sstevel@tonic-gate #include "async.h"
347c478bd9Sstevel@tonic-gate #include "util.h"
357c478bd9Sstevel@tonic-gate #include "interface.h"
367c478bd9Sstevel@tonic-gate #include "script_handler.h"
37df7aad50Sdh155122 #include "states.h"
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate /*
40*d04ccbb3Scarlsonj * async_start(): starts an asynchronous command on a state machine
417c478bd9Sstevel@tonic-gate *
42*d04ccbb3Scarlsonj * input: dhcp_smach_t *: the state machine to start the async command on
43*d04ccbb3Scarlsonj * dhcp_ipc_type_t: the command to start
44*d04ccbb3Scarlsonj * boolean_t: B_TRUE if the command was started by a user
45*d04ccbb3Scarlsonj * output: boolean: B_TRUE on success, B_FALSE on failure
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate boolean_t
async_start(dhcp_smach_t * dsmp,dhcp_ipc_type_t cmd,boolean_t user)49*d04ccbb3Scarlsonj async_start(dhcp_smach_t *dsmp, dhcp_ipc_type_t cmd, boolean_t user)
507c478bd9Sstevel@tonic-gate {
51*d04ccbb3Scarlsonj if (dsmp->dsm_async.as_present) {
527c478bd9Sstevel@tonic-gate return (B_FALSE);
53*d04ccbb3Scarlsonj } else {
54*d04ccbb3Scarlsonj dsmp->dsm_async.as_cmd = cmd;
55*d04ccbb3Scarlsonj dsmp->dsm_async.as_user = user;
56*d04ccbb3Scarlsonj dsmp->dsm_async.as_present = B_TRUE;
577c478bd9Sstevel@tonic-gate return (B_TRUE);
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate /*
627c478bd9Sstevel@tonic-gate * async_finish(): completes an asynchronous command
637c478bd9Sstevel@tonic-gate *
64*d04ccbb3Scarlsonj * input: dhcp_smach_t *: the state machine with the pending async command
657c478bd9Sstevel@tonic-gate * output: void
667c478bd9Sstevel@tonic-gate * note: should only be used when the command has no residual state to
677c478bd9Sstevel@tonic-gate * clean up
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate void
async_finish(dhcp_smach_t * dsmp)71*d04ccbb3Scarlsonj async_finish(dhcp_smach_t *dsmp)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate * be defensive here. the script may still be running if
757c478bd9Sstevel@tonic-gate * the asynchronous action times out before it is killed by the
767c478bd9Sstevel@tonic-gate * script helper process.
777c478bd9Sstevel@tonic-gate */
787c478bd9Sstevel@tonic-gate
79*d04ccbb3Scarlsonj if (dsmp->dsm_script_pid != -1)
80*d04ccbb3Scarlsonj script_stop(dsmp);
81*d04ccbb3Scarlsonj dsmp->dsm_async.as_present = B_FALSE;
827c478bd9Sstevel@tonic-gate }
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /*
857c478bd9Sstevel@tonic-gate * async_cancel(): cancels a pending asynchronous command
867c478bd9Sstevel@tonic-gate *
87*d04ccbb3Scarlsonj * input: dhcp_smach_t *: the state machine with the pending async command
88*d04ccbb3Scarlsonj * output: boolean: B_TRUE if cancellation was successful, B_FALSE on failure
897c478bd9Sstevel@tonic-gate */
907c478bd9Sstevel@tonic-gate
91*d04ccbb3Scarlsonj boolean_t
async_cancel(dhcp_smach_t * dsmp)92*d04ccbb3Scarlsonj async_cancel(dhcp_smach_t *dsmp)
937c478bd9Sstevel@tonic-gate {
94*d04ccbb3Scarlsonj if (!dsmp->dsm_async.as_present)
95*d04ccbb3Scarlsonj return (B_TRUE);
96*d04ccbb3Scarlsonj if (dsmp->dsm_async.as_user) {
97*d04ccbb3Scarlsonj dhcpmsg(MSG_DEBUG,
98*d04ccbb3Scarlsonj "async_cancel: cannot abort command %d from user",
99*d04ccbb3Scarlsonj (int)dsmp->dsm_async.as_cmd);
100*d04ccbb3Scarlsonj return (B_FALSE);
101*d04ccbb3Scarlsonj } else {
102*d04ccbb3Scarlsonj async_finish(dsmp);
103*d04ccbb3Scarlsonj dhcpmsg(MSG_DEBUG, "async_cancel: command %d aborted",
104*d04ccbb3Scarlsonj (int)dsmp->dsm_async.as_cmd);
105*d04ccbb3Scarlsonj return (B_TRUE);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate }
108