xref: /illumos-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/async.c (revision bb9b6b3f59b8820022416cea99b49c50fef6e391)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <sys/types.h>
29 #include <dhcpmsg.h>
30 #include <libinetutil.h>
31 #include <dhcpagent_util.h>
32 
33 #include "async.h"
34 #include "util.h"
35 #include "interface.h"
36 #include "script_handler.h"
37 #include "states.h"
38 
39 /*
40  * async_start(): starts an asynchronous command on a state machine
41  *
42  *   input: dhcp_smach_t *: the state machine to start the async command on
43  *	    dhcp_ipc_type_t: the command to start
44  *	    boolean_t: B_TRUE if the command was started by a user
45  *  output: boolean: B_TRUE on success, B_FALSE on failure
46  */
47 
48 boolean_t
49 async_start(dhcp_smach_t *dsmp, dhcp_ipc_type_t cmd, boolean_t user)
50 {
51 	if (dsmp->dsm_async.as_present) {
52 		return (B_FALSE);
53 	} else {
54 		dsmp->dsm_async.as_cmd = cmd;
55 		dsmp->dsm_async.as_user = user;
56 		dsmp->dsm_async.as_present = B_TRUE;
57 		return (B_TRUE);
58 	}
59 }
60 
61 /*
62  * async_finish(): completes an asynchronous command
63  *
64  *   input: dhcp_smach_t *: the state machine with the pending async command
65  *  output: void
66  *    note: should only be used when the command has no residual state to
67  *	    clean up
68  */
69 
70 void
71 async_finish(dhcp_smach_t *dsmp)
72 {
73 	/*
74 	 * be defensive here. the script may still be running if
75 	 * the asynchronous action times out before it is killed by the
76 	 * script helper process.
77 	 */
78 
79 	if (dsmp->dsm_script_pid != -1)
80 		script_stop(dsmp);
81 	dsmp->dsm_async.as_present = B_FALSE;
82 }
83 
84 /*
85  * async_cancel(): cancels a pending asynchronous command
86  *
87  *   input: dhcp_smach_t *: the state machine with the pending async command
88  *  output: boolean: B_TRUE if cancellation was successful, B_FALSE on failure
89  */
90 
91 boolean_t
92 async_cancel(dhcp_smach_t *dsmp)
93 {
94 	if (!dsmp->dsm_async.as_present)
95 		return (B_TRUE);
96 	if (dsmp->dsm_async.as_user) {
97 		dhcpmsg(MSG_DEBUG,
98 		    "async_cancel: cannot abort command %d from user",
99 		    (int)dsmp->dsm_async.as_cmd);
100 		return (B_FALSE);
101 	} else {
102 		async_finish(dsmp);
103 		dhcpmsg(MSG_DEBUG, "async_cancel: command %d aborted",
104 		    (int)dsmp->dsm_async.as_cmd);
105 		return (B_TRUE);
106 	}
107 }
108