xref: /illumos-gate/usr/src/cmd/cmd-inet/sbin/dhcpagent/async.c (revision 533affcbc7fc4d0c8132976ea454aaa715fe2307)
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 #include <sys/types.h>
27 #include <dhcpmsg.h>
28 #include <libinetutil.h>
29 #include <dhcpagent_util.h>
30 
31 #include "async.h"
32 #include "util.h"
33 #include "interface.h"
34 #include "script_handler.h"
35 #include "states.h"
36 
37 /*
38  * async_start(): starts an asynchronous command on a state machine
39  *
40  *   input: dhcp_smach_t *: the state machine to start the async command on
41  *	    dhcp_ipc_type_t: the command to start
42  *	    boolean_t: B_TRUE if the command was started by a user
43  *  output: boolean: B_TRUE on success, B_FALSE on failure
44  */
45 
46 boolean_t
47 async_start(dhcp_smach_t *dsmp, dhcp_ipc_type_t cmd, boolean_t user)
48 {
49 	if (dsmp->dsm_async.as_present) {
50 		return (B_FALSE);
51 	} else {
52 		dsmp->dsm_async.as_cmd = cmd;
53 		dsmp->dsm_async.as_user = user;
54 		dsmp->dsm_async.as_present = B_TRUE;
55 		return (B_TRUE);
56 	}
57 }
58 
59 /*
60  * async_finish(): completes an asynchronous command
61  *
62  *   input: dhcp_smach_t *: the state machine with the pending async command
63  *  output: void
64  *    note: should only be used when the command has no residual state to
65  *	    clean up
66  */
67 
68 void
69 async_finish(dhcp_smach_t *dsmp)
70 {
71 	/*
72 	 * be defensive here. the script may still be running if
73 	 * the asynchronous action times out before it is killed by the
74 	 * script helper process.
75 	 */
76 
77 	if (dsmp->dsm_script_pid != -1)
78 		script_stop(dsmp);
79 	dsmp->dsm_async.as_present = B_FALSE;
80 }
81 
82 /*
83  * async_cancel(): cancels a pending asynchronous command
84  *
85  *   input: dhcp_smach_t *: the state machine with the pending async command
86  *  output: boolean: B_TRUE if cancellation was successful, B_FALSE on failure
87  */
88 
89 boolean_t
90 async_cancel(dhcp_smach_t *dsmp)
91 {
92 	if (!dsmp->dsm_async.as_present)
93 		return (B_TRUE);
94 	if (dsmp->dsm_async.as_user) {
95 		dhcpmsg(MSG_DEBUG,
96 		    "async_cancel: cannot abort command %d from user",
97 		    (int)dsmp->dsm_async.as_cmd);
98 		return (B_FALSE);
99 	} else {
100 		async_finish(dsmp);
101 		dhcpmsg(MSG_DEBUG, "async_cancel: command %d aborted",
102 		    (int)dsmp->dsm_async.as_cmd);
103 		return (B_TRUE);
104 	}
105 }
106