2 * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2001-2003 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: control.c,v 1.7.2.2.2.14 2005/04/29 01:04:47 marka Exp $ */
25 #include <isc/event.h>
27 #include <isc/timer.h>
30 #include <dns/result.h>
32 #include <isccc/alist.h>
34 #include <isccc/result.h>
36 #include <named/control.h>
37 #include <named/log.h>
39 #include <named/server.h>
41 #include <named/ns_smf_globals.h>
45 command_compare(const char *text, const char *command) {
46 unsigned int commandlen = strlen(command);
47 if (strncasecmp(text, command, commandlen) == 0 &&
48 (text[commandlen] == '\0' ||
49 text[commandlen] == ' ' ||
50 text[commandlen] == '\t'))
56 * This function is called to process the incoming command
57 * when a control channel message is received.
60 ns_control_docommand(isccc_sexpr_t *message, isc_buffer_t *text) {
65 ns_smf_want_disable = 0;
68 data = isccc_alist_lookup(message, "_data");
73 return (ISC_R_FAILURE);
76 result = isccc_cc_lookupstring(data, "type", &command);
77 if (result != ISC_R_SUCCESS) {
79 * We have no idea what this is.
84 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
85 NS_LOGMODULE_CONTROL, ISC_LOG_DEBUG(1),
86 "received control channel command '%s'",
90 * Compare the 'command' parameter against all known control commands.
92 if (command_compare(command, NS_COMMAND_RELOAD)) {
93 result = ns_server_reloadcommand(ns_g_server, command, text);
94 } else if (command_compare(command, NS_COMMAND_RECONFIG)) {
95 result = ns_server_reconfigcommand(ns_g_server, command);
96 } else if (command_compare(command, NS_COMMAND_REFRESH)) {
97 result = ns_server_refreshcommand(ns_g_server, command, text);
98 } else if (command_compare(command, NS_COMMAND_RETRANSFER)) {
99 result = ns_server_retransfercommand(ns_g_server, command);
100 } else if (command_compare(command, NS_COMMAND_HALT)) {
103 * If we are managed by smf(5), AND in chroot, then
104 * we cannot connect to the smf repository, so just
105 * return with an appropriate message back to rndc.
107 if (ns_smf_got_instance == 1 && ns_smf_chroot == 1) {
108 result = ns_smf_add_message(text);
112 * If we are managed by smf(5) but not in chroot,
113 * try to disable ourselves the smf way.
115 if (ns_smf_got_instance == 1 && ns_smf_chroot == 0)
116 ns_smf_want_disable = 1;
118 * If ns_smf_got_instance = 0, ns_smf_chroot
119 * is not relevant and we fall through to
120 * isc_app_shutdown below.
123 ns_server_flushonshutdown(ns_g_server, ISC_FALSE);
124 ns_os_shutdownmsg(command, text);
126 result = ISC_R_SUCCESS;
127 } else if (command_compare(command, NS_COMMAND_STOP)) {
129 if (ns_smf_got_instance == 1 && ns_smf_chroot == 1) {
130 result = ns_smf_add_message(text);
133 if (ns_smf_got_instance == 1 && ns_smf_chroot == 0)
134 ns_smf_want_disable = 1;
136 ns_server_flushonshutdown(ns_g_server, ISC_TRUE);
137 ns_os_shutdownmsg(command, text);
139 result = ISC_R_SUCCESS;
140 } else if (command_compare(command, NS_COMMAND_DUMPSTATS)) {
141 result = ns_server_dumpstats(ns_g_server);
142 } else if (command_compare(command, NS_COMMAND_QUERYLOG)) {
143 result = ns_server_togglequerylog(ns_g_server);
144 } else if (command_compare(command, NS_COMMAND_DUMPDB)) {
145 ns_server_dumpdb(ns_g_server, command);
146 result = ISC_R_SUCCESS;
147 } else if (command_compare(command, NS_COMMAND_TRACE)) {
148 result = ns_server_setdebuglevel(ns_g_server, command);
149 } else if (command_compare(command, NS_COMMAND_NOTRACE)) {
151 isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
152 result = ISC_R_SUCCESS;
153 } else if (command_compare(command, NS_COMMAND_FLUSH)) {
154 result = ns_server_flushcache(ns_g_server, command);
155 } else if (command_compare(command, NS_COMMAND_FLUSHNAME)) {
156 result = ns_server_flushname(ns_g_server, command);
157 } else if (command_compare(command, NS_COMMAND_STATUS)) {
158 result = ns_server_status(ns_g_server, text);
159 } else if (command_compare(command, NS_COMMAND_FREEZE)) {
160 result = ns_server_freeze(ns_g_server, ISC_TRUE, command);
161 } else if (command_compare(command, NS_COMMAND_UNFREEZE) ||
162 command_compare(command, NS_COMMAND_THAW)) {
163 result = ns_server_freeze(ns_g_server, ISC_FALSE, command);
164 } else if (command_compare(command, NS_COMMAND_RECURSING)) {
165 result = ns_server_dumprecursing(ns_g_server);
166 } else if (command_compare(command, NS_COMMAND_NULL)) {
167 result = ISC_R_SUCCESS;
169 isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
170 NS_LOGMODULE_CONTROL, ISC_LOG_WARNING,
171 "unknown control channel command '%s'",
173 result = DNS_R_UNKNOWNCOMMAND;