+3150. [func] Improved startup and reconfiguration time by
+ enabling zones to load in multiple threads. [RT #25333]
+
+3149. [placeholder]
+
3148. [bug] Processing of normal queries could be stalled when
forwarding a UPDATE message. [RT #24711]
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: server.c,v 1.617 2011/08/30 05:16:10 marka Exp $ */
+/* $Id: server.c,v 1.618 2011/09/02 21:15:30 each Exp $ */
/*! \file */
#include <isc/parseint.h>
#include <isc/portset.h>
#include <isc/print.h>
+#include <isc/refcount.h>
#include <isc/resource.h>
#include <isc/sha2.h>
#include <isc/socket.h>
cfg_aclconfctx_t * actx;
};
+/*%
+ * Holds state information for the initial zone loading process.
+ * Uses the isc_refcount structure to count the number of views
+ * with pending zone loads, dereferencing as each view finishes.
+ */
+typedef struct {
+ ns_server_t *server;
+ isc_refcount_t refs;
+} ns_zoneload_t;
+
/*
* These zones should not leak onto the Internet.
*/
}
static isc_result_t
-load_zones(ns_server_t *server, isc_boolean_t stop) {
+view_loaded(void *arg) {
+ isc_result_t result;
+ ns_zoneload_t *zl = (ns_zoneload_t *) arg;
+ ns_server_t *server = zl->server;
+ unsigned int refs;
+
+
+ /*
+ * Force zone maintenance. Do this after loading
+ * so that we know when we need to force AXFR of
+ * slave zones whose master files are missing.
+ *
+ * We use the zoneload reference counter to let us
+ * know when all views are finished.
+ */
+ isc_refcount_decrement(&zl->refs, &refs);
+ if (refs != 0)
+ return (ISC_R_SUCCESS);
+
+ isc_refcount_destroy(&zl->refs);
+ isc_mem_put(server->mctx, zl, sizeof (*zl));
+
+ isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
+ ISC_LOG_NOTICE, "all zones loaded");
+ CHECKFATAL(dns_zonemgr_forcemaint(server->zonemgr),
+ "forcing zone maintenance");
+
+ ns_os_started();
+ isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
+ ISC_LOG_NOTICE, "running");
+
+ return (ISC_R_SUCCESS);
+}
+
+static isc_result_t
+load_zones(ns_server_t *server) {
isc_result_t result;
dns_view_t *view;
+ ns_zoneload_t *zl;
+ unsigned int refs = 0;
+
+ zl = isc_mem_get(server->mctx, sizeof (*zl));
+ if (zl == NULL)
+ return (ISC_R_NOMEMORY);
+ zl->server = server;
result = isc_task_beginexclusive(server->task);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
+ isc_refcount_init(&zl->refs, 1);
+
/*
- * Load zone data from disk.
+ * Schedule zones to be loaded from disk.
*/
for (view = ISC_LIST_HEAD(server->viewlist);
view != NULL;
view = ISC_LIST_NEXT(view, link))
{
- CHECK(dns_view_load(view, stop));
if (view->managed_keys != NULL)
CHECK(dns_zone_load(view->managed_keys));
if (view->redirect != NULL)
CHECK(dns_zone_load(view->redirect));
+ isc_refcount_increment(&zl->refs, NULL);
+ CHECK(dns_view_asyncload(view, view_loaded, zl));
}
- /*
- * Force zone maintenance. Do this after loading
- * so that we know when we need to force AXFR of
- * slave zones whose master files are missing.
- */
- CHECK(dns_zonemgr_forcemaint(server->zonemgr));
cleanup:
+ isc_refcount_decrement(&zl->refs, &refs);
+ if (result != ISC_R_SUCCESS || refs == 0) {
+ isc_refcount_destroy(&zl->refs);
+ isc_mem_put(server->mctx, zl, sizeof (*zl));
+ } else {
+ /*
+ * Place the task manager into privileged mode. This
+ * ensures that after we leave task-exclusive mode, no
+ * other tasks will be able to run except for the ones
+ * that are loading zones.
+ */
+ isc_taskmgr_setmode(ns_g_taskmgr, isc_taskmgrmode_privileged);
+ }
+
isc_task_endexclusive(server->task);
return (result);
}
isc_hash_init();
- CHECKFATAL(load_zones(server, ISC_FALSE), "loading zones");
-
- ns_os_started();
- isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_SERVER,
- ISC_LOG_NOTICE, "running");
+ CHECKFATAL(load_zones(server), "loading zones");
}
void
isc_result_t result;
CHECK(loadconfig(server));
- result = load_zones(server, ISC_FALSE);
+ result = load_zones(server);
if (result == ISC_R_SUCCESS)
isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
NS_LOGMODULE_SERVER, ISC_LOG_INFO,
These scripts generate a named.conf file with an arbitrary number of
small zones, for testing startup performance.
-To generate a test server with 1000 zones, run:
+To generate a test server with 1000 zones each of which contains 5 A
+records, run:
- $ sh setup.sh 1000 > named.conf
+ $ sh setup.sh 1000 5 > named.conf
-Zones are generated with random names, and all of them load from the same
-file: smallzone.db.
+Zones are generated with random names, and the zone files are created
+in the subdirectory "zones".
+
+Or, to generate a test server with 100 zones which all load from the same
+generic file (smallzone.db):
+
+ $ sh setup.sh -s 100 > named.conf
+
+The "number of records" argument is ignored if -s is used.
--- /dev/null
+#!/bin/sh
+rm -rf zones
+rm -f named.conf
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
-# $Id: makenames.pl,v 1.2 2011/07/06 05:05:51 each Exp $
+# $Id: makenames.pl,v 1.3 2011/09/02 21:15:35 each Exp $
use strict;
-die "Usage: makenames.pl <num>" if (@ARGV == 0);
+die "Usage: makenames.pl <num> [<len>]" if (@ARGV == 0 || @ARGV > 2);
+my $len = 10;
+$len = @ARGV[1] if (@ARGV == 2);
my @chars = split("", "abcdefghijklmnopqrstuvwxyz123456789");
srand;
for (my $i = 0; $i < @ARGV[0]; $i++) {
my $name = "";
- for (my $j = 0; $j < 10; $j++) {
+ for (my $j = 0; $j < $len; $j++) {
my $r = rand 35;
$name .= $chars[$r];
}
--- /dev/null
+#!/usr/bin/perl
+#
+# Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+#
+# Permission to use, copy, modify, and/or distribute this software for any
+# purpose with or without fee is hereby granted, provided that the above
+# copyright notice and this permission notice appear in all copies.
+#
+# THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+# AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+# PERFORMANCE OF THIS SOFTWARE.
+
+# $Id: mkzonefile.pl,v 1.2 2011/09/02 21:15:35 each Exp $
+use strict;
+
+die "Usage: makenames.pl zonename num_records" if (@ARGV != 2);
+my $zname = @ARGV[0];
+my $nrecords = @ARGV[1];
+
+my @chars = split("", "abcdefghijklmnopqrstuvwxyz");
+
+print"\$TTL 300 ; 5 minutes
+\$ORIGIN $zname.
+@ IN SOA mname1. . (
+ 2011080201 ; serial
+ 20 ; refresh (20 seconds)
+ 20 ; retry (20 seconds)
+ 1814400 ; expire (3 weeks)
+ 600 ; minimum (1 hour)
+ )
+ NS ns
+ns A 10.53.0.3\n";
+
+srand;
+for (my $i = 0; $i < $nrecords; $i++) {
+ my $name = "";
+ for (my $j = 0; $j < 8; $j++) {
+ my $r = rand 25;
+ $name .= $chars[$r];
+ }
+ print "$name" . "\tIN\tA\t";
+ my $x = int rand 254;
+ my $y = int rand 254;
+ my $z = int rand 254;
+ print "10.$x.$y.$z\n";
+}
+
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
-# $Id: setup.sh,v 1.3 2011/07/07 23:47:49 tbox Exp $
+# $Id: setup.sh,v 1.4 2011/09/02 21:15:35 each Exp $
-if [ "$#" -ne 1 ]; then
- echo "Usage: $0 <number of zones>"
+usage () {
+ echo "Usage: $0 [-s] <number of zones> [<records per zone>]"
+ echo " -s: use the same zone file all zones"
exit 1
+}
+
+if [ "$#" -lt 1 -o "$#" -gt 3 ]; then
+ usage
+fi
+
+single_file=""
+if [ $1 = "-s" ]; then
+ single_file=yes
+ shift
fi
+nzones=$1
+shift
+
+nrecords=5
+[ "$#" -eq 1 ] && nrecords=$1
+
. ../system/conf.sh
cat << EOF
EOF
-$PERL makenames.pl $1 | while read zonename; do
+$PERL makenames.pl $nzones | while read zonename; do
+ if [ $single_file ]; then
echo "zone $zonename { type master; file \"smallzone.db\"; };"
+ else
+ [ -d zones ] || mkdir zones
+ $PERL mkzonefile.pl $zonename $nrecords > zones/$zonename.db
+ echo "zone $zonename { type master; file \"zones/$zonename.db\"; };"
+ fi
done
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: events.h,v 1.58 2011/08/30 23:46:53 tbox Exp $ */
+/* $Id: events.h,v 1.59 2011/09/02 21:15:36 each Exp $ */
#ifndef DNS_EVENTS_H
#define DNS_EVENTS_H 1
#define DNS_EVENT_ADBGROWNAMES (ISC_EVENTCLASS_DNS + 46)
#define DNS_EVENT_ZONESECURESERIAL (ISC_EVENTCLASS_DNS + 47)
#define DNS_EVENT_ZONESECUREDB (ISC_EVENTCLASS_DNS + 48)
+#define DNS_EVENT_ZONELOAD (ISC_EVENTCLASS_DNS + 49)
#define DNS_EVENT_FIRSTEVENT (ISC_EVENTCLASS_DNS + 0)
#define DNS_EVENT_LASTEVENT (ISC_EVENTCLASS_DNS + 65535)
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: view.h,v 1.134 2011/08/02 20:36:13 each Exp $ */
+/* $Id: view.h,v 1.135 2011/09/02 21:15:36 each Exp $ */
#ifndef DNS_VIEW_H
#define DNS_VIEW_H 1
#include <dns/rdatastruct.h>
#include <dns/rpz.h>
#include <dns/types.h>
+#include <dns/zt.h>
ISC_LANG_BEGINDECLS
isc_result_t
dns_view_loadnew(dns_view_t *view, isc_boolean_t stop);
+
+isc_result_t
+dns_view_asyncload(dns_view_t *view, dns_zt_allloaded_t callback, void *arg);
/*%<
* Load zones attached to this view. dns_view_load() loads
* all zones whose master file has changed since the last
* load; dns_view_loadnew() loads only zones that have never
* been loaded.
*
+ * dns_view_asyncload() loads zones asynchronously. When all zones
+ * in the view have finished loading, 'callback' is called with argument
+ * 'arg' to inform the caller.
+ *
* If 'stop' is ISC_TRUE, stop on the first error and return it.
- * If 'stop' is ISC_FALSE, ignore errors.
+ * If 'stop' is ISC_FALSE (or we are loading asynchronously), ignore errors.
*
* Requires:
*
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: zone.h,v 1.192 2011/08/30 05:16:15 marka Exp $ */
+/* $Id: zone.h,v 1.193 2011/09/02 21:15:36 each Exp $ */
#ifndef DNS_ZONE_H
#define DNS_ZONE_H 1
#include <dns/masterdump.h>
#include <dns/rdatastruct.h>
#include <dns/types.h>
+#include <dns/zt.h>
typedef enum {
dns_zone_none,
isc_result_t
dns_zone_loadandthaw(dns_zone_t *zone);
+
/*%<
* Cause the database to be loaded from its backing store.
* Confirm that the minimum requirements for the zone type are
*\li Any result value from dns_db_load().
*/
+isc_result_t
+dns_zone_asyncload(dns_zone_t *zone, dns_zt_zoneloaded_t done, void *arg);
+/*%<
+ * Cause the database to be loaded from its backing store asynchronously.
+ * Other zone maintenance functions are suspended until this is complete.
+ * When finished, 'done' is called to inform the caller, with 'arg' as
+ * its first argument and 'zone' as its second. (Normally, 'arg' is
+ * expected to point to the zone table but is left undefined for testing
+ * purposes.)
+ */
+
+isc_boolean_t
+dns__zone_loadpending(dns_zone_t *zone);
+/*%<
+ * Indicates whether the zone is waiting to be loaded asynchronously.
+ * (Not currently intended for use outside of this module and associated
+ * tests.)
+ */
+
void
dns_zone_attach(dns_zone_t *source, dns_zone_t **target);
/*%<
* earliest convenience.
*/
+void
+dns__zonemgr_run(isc_task_t *task, isc_event_t *event);
+/*%<
+ * Event handler to call dns_zonemgr_forcemaint(); used to start
+ * zone operations from a unit test. Not intended for use outside
+ * libdns or related tests.
+ */
+
void
dns_zonemgr_resumexfrs(dns_zonemgr_t *zmgr);
/*%<
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: zt.h,v 1.38 2007/06/19 23:47:17 tbox Exp $ */
+/* $Id: zt.h,v 1.39 2011/09/02 21:15:37 each Exp $ */
#ifndef DNS_ZT_H
#define DNS_ZT_H 1
ISC_LANG_BEGINDECLS
+typedef isc_result_t
+(*dns_zt_allloaded_t)(void *arg);
+/*%<
+ * Method prototype: when all pending zone loads are complete,
+ * the zone table can inform the caller via a callback function with
+ * this signature.
+ */
+
+typedef isc_result_t
+(*dns_zt_zoneloaded_t)(dns_zt_t *zt, dns_zone_t *zone, isc_task_t *task);
+/*%<
+ * Method prototype: when a zone finishes loading, the zt object
+ * can be informed via a callback function with this signature.
+ */
+
isc_result_t
dns_zt_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, dns_zt_t **zt);
/*%<
isc_result_t
dns_zt_loadnew(dns_zt_t *zt, isc_boolean_t stop);
+
+isc_result_t
+dns_zt_asyncload(dns_zt_t *zt, dns_zt_allloaded_t alldone, void *arg);
/*%<
* Load all zones in the table. If 'stop' is ISC_TRUE,
* stop on the first error and return it. If 'stop'
* dns_zt_loadnew() only loads zones that are not yet loaded.
* dns_zt_load() also loads zones that are already loaded and
* and whose master file has changed since the last load.
+ * dns_zt_asyncload() loads zones asynchronously; when all
+ * zones in the zone table have finished loaded (or failed due
+ * to errors), the caller is informed by calling 'alldone'
+ * with an argument of 'arg'.
*
* Requires:
* \li 'zt' to be valid
* any error code from 'action'.
*/
+isc_boolean_t
+dns_zt_loadspending(dns_zt_t *zt);
+/*%<
+ * Returns ISC_TRUE if and only if there are zones still waiting to
+ * be loaded in zone table 'zt'.
+ *
+ * Requires:
+ * \li 'zt' to be valid.
+ */
+
ISC_LANG_ENDDECLS
#endif /* DNS_ZT_H */
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
-# $Id: Makefile.in,v 1.7 2011/08/23 01:29:38 each Exp $
+# $Id: Makefile.in,v 1.8 2011/09/02 21:15:37 each Exp $
srcdir = @srcdir@
VPATH = @srcdir@
LIBS = @LIBS@ @ATFLIBS@
OBJS = dnstest.@O@
-SRCS = dnstest.c master_test.c time_test.c update_test.c \
- zonemgr_test.c dbiterator_test.c
+SRCS = dnstest.c master_test.c dbiterator_test.c time_test.c \
+ update_test.c zonemgr_test.c zt_test.c
SUBDIRS =
-TARGETS = master_test@EXEEXT@ time_test@EXEEXT@ update_test@EXEEXT@ \
- zonemgr_test@EXEEXT@ dbiterator_test@EXEEXT@
+TARGETS = master_test@EXEEXT@ dbiterator_test@EXEEXT@ time_test@EXEEXT@ \
+ update_test@EXEEXT@ zonemgr_test@EXEEXT@ zt_test@EXEEXT@
@BIND9_MAKE_RULES@
dbiterator_test.@O@ dnstest.@O@ ${DNSLIBS} \
${ISCLIBS} ${LIBS}
+zt_test@EXEEXT@: zt_test.@O@ dnstest.@O@ ${ISCDEPLIBS} ${DNSDEPLIBS}
+ ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
+ zt_test.@O@ dnstest.@O@ ${DNSLIBS} \
+ ${ISCLIBS} ${LIBS}
+
unit::
sh ${top_srcdir}/unit/unittest.sh
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: dnstest.c,v 1.4 2011/07/06 01:36:32 each Exp $ */
+/* $Id: dnstest.c,v 1.5 2011/09/02 21:15:37 each Exp $ */
/*! \file */
#include <config.h>
+#include <unistd.h>
+
#include <isc/app.h>
#include <isc/buffer.h>
#include <isc/entropy.h>
#include <isc/timer.h>
#include <isc/util.h>
+#include <dns/fixedname.h>
#include <dns/log.h>
#include <dns/name.h>
#include <dns/result.h>
+#include <dns/view.h>
+#include <dns/zone.h>
#include <dst/dst.h>
isc_entropy_t *ectx = NULL;
isc_log_t *lctx = NULL;
isc_taskmgr_t *taskmgr = NULL;
+isc_task_t *maintask = NULL;
isc_timermgr_t *timermgr = NULL;
isc_socketmgr_t *socketmgr = NULL;
+dns_zonemgr_t *zonemgr = NULL;
+isc_boolean_t app_running = ISC_FALSE;
int ncpus;
static isc_boolean_t hash_active = ISC_FALSE, dst_active = ISC_FALSE;
static void
cleanup_managers() {
+ if (app_running)
+ isc_app_finish();
if (socketmgr != NULL)
isc_socketmgr_destroy(&socketmgr);
+ if (maintask != NULL)
+ isc_task_destroy(&maintask);
if (taskmgr != NULL)
isc_taskmgr_destroy(&taskmgr);
if (timermgr != NULL)
CHECK(isc_taskmgr_create(mctx, ncpus, 0, &taskmgr));
CHECK(isc_timermgr_create(mctx, &timermgr));
CHECK(isc_socketmgr_create(mctx, &socketmgr));
+ CHECK(isc_task_create(taskmgr, 0, &maintask));
+ CHECK(isc_app_start());
return (ISC_R_SUCCESS);
cleanup:
if (start_managers)
CHECK(create_managers());
+ /*
+ * atf-run changes us to a /tmp directory, so tests
+ * that access test data files must first chdir to the proper
+ * location.
+ */
+ if (chdir(TESTS) == -1)
+ CHECK(ISC_R_FAILURE);
+
return (ISC_R_SUCCESS);
cleanup:
isc_mem_destroy(&mctx);
}
+/*
+ * Create a zone with origin 'name', return a pointer to the zone object in
+ * 'zonep'. If 'view' is set, add the zone to that view; otherwise, create
+ * a new view for the purpose.
+ *
+ * If the created view is going to be needed by the caller subsequently,
+ * then 'keepview' should be set to true; this will prevent the view
+ * from being detached. In this case, the caller is responsible for
+ * detaching the view.
+ */
+isc_result_t
+dns_test_makezone(const char *name, dns_zone_t **zonep, dns_view_t *view,
+ isc_boolean_t keepview)
+{
+ isc_result_t result;
+ dns_zone_t *zone = NULL;
+ isc_buffer_t buffer;
+ dns_fixedname_t fixorigin;
+ dns_name_t *origin;
+
+ if (view == NULL)
+ CHECK(dns_view_create(mctx, dns_rdataclass_in, "view", &view));
+ else if (!keepview)
+ keepview = ISC_TRUE;
+
+ CHECK(dns_zone_create(&zone, mctx));
+
+ isc_buffer_init(&buffer, name, strlen(name));
+ isc_buffer_add(&buffer, strlen(name));
+ dns_fixedname_init(&fixorigin);
+ origin = dns_fixedname_name(&fixorigin);
+ CHECK(dns_name_fromtext(origin, &buffer, dns_rootname, 0, NULL));
+ CHECK(dns_zone_setorigin(zone, origin));
+ dns_zone_setview(zone, view);
+ dns_zone_settype(zone, dns_zone_master);
+ dns_zone_setclass(zone, view->rdclass);
+ dns_view_addzone(view, zone);
+
+ if (!keepview)
+ dns_view_detach(&view);
+
+ *zonep = zone;
+
+ return (ISC_R_SUCCESS);
+
+ cleanup:
+ if (zone != NULL)
+ dns_zone_detach(&zone);
+ if (view != NULL)
+ dns_view_detach(&view);
+ return (result);
+}
+
+isc_result_t
+dns_test_setupzonemgr() {
+ isc_result_t result;
+ REQUIRE(zonemgr == NULL);
+
+ result = dns_zonemgr_create(mctx, taskmgr, timermgr, socketmgr,
+ &zonemgr);
+ return (result);
+}
+
+isc_result_t
+dns_test_managezone(dns_zone_t *zone) {
+ isc_result_t result;
+ REQUIRE(zonemgr != NULL);
+
+ result = dns_zonemgr_setsize(zonemgr, 1);
+ if (result != ISC_R_SUCCESS)
+ return (result);
+
+ result = dns_zonemgr_managezone(zonemgr, zone);
+ return (result);
+}
+
+void
+dns_test_releasezone(dns_zone_t *zone) {
+ REQUIRE(zonemgr != NULL);
+ dns_zonemgr_releasezone(zonemgr, zone);
+}
+
+void
+dns_test_closezonemgr() {
+ REQUIRE(zonemgr != NULL);
+
+ dns_zonemgr_shutdown(zonemgr);
+ dns_zonemgr_detach(&zonemgr);
+}
+
+/*
+ * Sleep for 'usec' microseconds.
+ */
+void
+dns_test_nap(isc_uint32_t usec) {
+#ifdef HAVE_NANOSLEEP
+ struct timespec ts;
+
+ ts.tv_sec = usec / 1000000;
+ ts.tv_nsec = (usec % 1000000) * 1000;
+ nanosleep(&ts, NULL);
+#elif HAVE_USLEEP
+ usleep(usec);
+#else
+ /*
+ * No fractional-second sleep function is available, so we
+ * round up to the nearest second and sleep instead
+ */
+ sleep((usec / 1000000) + 1);
+#endif
+}
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: dnstest.h,v 1.3 2011/07/06 01:36:32 each Exp $ */
+/* $Id: dnstest.h,v 1.4 2011/09/02 21:15:37 each Exp $ */
/*! \file */
#include <isc/util.h>
#include <dns/result.h>
+#include <dns/zone.h>
#define CHECK(r) \
do { \
extern isc_mem_t *mctx;
extern isc_entropy_t *ectx;
extern isc_log_t *lctx;
-isc_taskmgr_t *taskmgr;
-isc_timermgr_t *timermgr;
-isc_socketmgr_t *socketmgr;
-int ncpus;
-
+extern isc_taskmgr_t *taskmgr;
+extern isc_task_t *maintask;
+extern isc_timermgr_t *timermgr;
+extern isc_socketmgr_t *socketmgr;
+extern dns_zonemgr_t *zonemgr;
+extern isc_boolean_t app_running;
+extern int ncpus;
isc_result_t
dns_test_begin(FILE *logfile, isc_boolean_t create_managers);
void
dns_test_end(void);
+isc_result_t
+dns_test_makezone(const char *name, dns_zone_t **zonep, dns_view_t *view,
+ isc_boolean_t keepview);
+
+isc_result_t
+dns_test_setupzonemgr(void);
+
+isc_result_t
+dns_test_managezone(dns_zone_t *zone);
+
+void
+dns_test_releasezone(dns_zone_t *zone);
+
+void
+dns_test_closezonemgr(void);
+
+void
+dns_test_nap(isc_uint32_t usec);
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: master_test.c,v 1.4 2011/07/06 01:36:32 each Exp $ */
+/* $Id: master_test.c,v 1.5 2011/09/02 21:15:37 each Exp $ */
/*! \file */
dns_rdatacallbacks_init_stdio(&callbacks);
callbacks.add = add_callback;
- /*
- * atf-run changes us to a /tmp directory, so tests
- * that access test data files must first chdir to the proper
- * location.
- */
- if (chdir(TESTS) == -1)
- return (ISC_R_FAILURE);
-
result = dns_master_loadfile(testfile, &dns_origin, &dns_origin,
dns_rdataclass_in, ISC_TRUE,
&callbacks, mctx);
--- /dev/null
+$TTL 1000
+@ in soa localhost. postmaster.localhost. (
+ 1993050801 ;serial
+ 3600 ;refresh
+ 1800 ;retry
+ 604800 ;expiration
+ 3600 ) ;minimum
+ in ns ns.vix.com.
+ in ns ns2.vix.com.
+ in ns ns3.vix.com.
+a in a 1.2.3.4
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: zonemgr_test.c,v 1.2 2011/07/06 05:05:51 each Exp $ */
+/* $Id: zonemgr_test.c,v 1.3 2011/09/02 21:15:37 each Exp $ */
/*! \file */
#include "dnstest.h"
-static isc_result_t
-make_zone(const char *name, dns_zone_t **zonep) {
- isc_result_t result;
- dns_view_t *view = NULL;
- dns_zone_t *zone = NULL;
- isc_buffer_t buffer;
- dns_fixedname_t fixorigin;
- dns_name_t *origin;
-
- CHECK(dns_view_create(mctx, dns_rdataclass_in, "view", &view));
- CHECK(dns_zone_create(&zone, mctx));
-
- isc_buffer_init(&buffer, name, strlen(name));
- isc_buffer_add(&buffer, strlen(name));
- dns_fixedname_init(&fixorigin);
- origin = dns_fixedname_name(&fixorigin);
- CHECK(dns_name_fromtext(origin, &buffer, dns_rootname, 0, NULL));
- CHECK(dns_zone_setorigin(zone, origin));
- dns_zone_setview(zone, view);
- dns_zone_settype(zone, dns_zone_master);
- dns_zone_setclass(zone, view->rdclass);
-
- dns_view_detach(&view);
- *zonep = zone;
-
- return (ISC_R_SUCCESS);
-
- cleanup:
- if (zone != NULL)
- dns_zone_detach(&zone);
- if (view != NULL)
- dns_view_detach(&view);
- return (result);
-}
-
/*
* Individual unit tests
*/
&zonemgr);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
- result = make_zone("foo", &zone);
+ result = dns_test_makezone("foo", &zone, NULL, ISC_FALSE);
ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
/* This should not succeed until the dns_zonemgr_setsize() is run */
--- /dev/null
+/*
+ * Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+ * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/* $Id: zt_test.c,v 1.2 2011/09/02 21:15:37 each Exp $ */
+
+/*! \file */
+
+#include <config.h>
+
+#include <atf-c.h>
+
+#include <unistd.h>
+
+#include <isc/app.h>
+#include <isc/buffer.h>
+#include <isc/task.h>
+#include <isc/timer.h>
+
+#include <dns/db.h>
+#include <dns/name.h>
+#include <dns/view.h>
+#include <dns/zone.h>
+#include <dns/zt.h>
+
+#include "dnstest.h"
+
+/*
+ * Helper functions
+ */
+static isc_result_t
+count_zone(dns_zone_t *zone, void *uap) {
+ int *nzones = (int *)uap;
+
+ UNUSED(zone);
+
+ *nzones += 1;
+ return (ISC_R_SUCCESS);
+}
+
+static isc_result_t
+load_done(dns_zt_t *zt, dns_zone_t *zone) {
+ /* We treat zt as a pointer to a boolean for testing purposes */
+ isc_boolean_t *done = (isc_boolean_t *) zt;
+
+ UNUSED(zone);
+
+ *done = ISC_TRUE;
+ isc_app_shutdown();
+ return (ISC_R_SUCCESS);
+}
+
+static isc_result_t
+all_done(void *arg) {
+ isc_boolean_t *done = (isc_boolean_t *) arg;
+
+ *done = ISC_TRUE;
+ isc_app_shutdown();
+ return (ISC_R_SUCCESS);
+}
+
+/*
+ * Individual unit tests
+ */
+ATF_TC(apply);
+ATF_TC_HEAD(apply, tc) {
+ atf_tc_set_md_var(tc, "descr", "apply a function to a zone table");
+}
+ATF_TC_BODY(apply, tc) {
+ isc_result_t result;
+ dns_zone_t *zone = NULL;
+ dns_view_t *view = NULL;
+ int nzones = 0;
+
+ UNUSED(tc);
+
+ result = dns_test_begin(NULL, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ result = dns_test_makezone("foo", &zone, NULL, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ view = dns_zone_getview(zone);
+ ATF_REQUIRE(view->zonetable != NULL);
+
+ ATF_CHECK_EQ(0, nzones);
+ result = dns_zt_apply(view->zonetable, ISC_FALSE, count_zone, &nzones);
+ ATF_CHECK_EQ(1, nzones);
+
+ /* These steps are necessary so the zone can be detached properly */
+ result = dns_test_setupzonemgr();
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ result = dns_test_managezone(zone);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ dns_test_releasezone(zone);
+ dns_test_closezonemgr();
+
+ /* The view was left attached in dns_test_makezone() */
+ dns_view_detach(&view);
+ dns_zone_detach(&zone);
+
+ dns_test_end();
+}
+
+ATF_TC(asyncload_zone);
+ATF_TC_HEAD(asyncload_zone, tc) {
+ atf_tc_set_md_var(tc, "descr", "asynchronous zone load");
+}
+ATF_TC_BODY(asyncload_zone, tc) {
+ isc_result_t result;
+ dns_zone_t *zone = NULL;
+ dns_view_t *view = NULL;
+ dns_db_t *db = NULL;
+ isc_boolean_t done = ISC_FALSE;
+ int i = 0;
+
+ UNUSED(tc);
+
+ result = dns_test_begin(NULL, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ result = dns_test_makezone("foo", &zone, NULL, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ result = dns_test_setupzonemgr();
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ result = dns_test_managezone(zone);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ view = dns_zone_getview(zone);
+ ATF_REQUIRE(view->zonetable != NULL);
+
+ ATF_CHECK(!dns__zone_loadpending(zone));
+ ATF_CHECK(!done);
+ dns_zone_setfile(zone, "testdata/zt/zone1.db");
+ dns_zone_asyncload(zone, load_done, (void *) &done);
+ ATF_CHECK(dns__zone_loadpending(zone));
+
+ isc_app_run();
+ while (dns__zone_loadpending(zone) && i++ < 5000)
+ dns_test_nap(1000);
+ ATF_CHECK(done);
+
+ /* The zone should now be loaded; test it */
+ result = dns_zone_getdb(zone, &db);
+ ATF_CHECK_EQ(result, ISC_R_SUCCESS);
+ ATF_CHECK(db != NULL);
+ if (db != NULL)
+ dns_db_detach(&db);
+
+ dns_test_releasezone(zone);
+ dns_test_closezonemgr();
+
+ dns_zone_detach(&zone);
+ dns_view_detach(&view);
+
+ dns_test_end();
+}
+
+ATF_TC(asyncload_zt);
+ATF_TC_HEAD(asyncload_zt, tc) {
+ atf_tc_set_md_var(tc, "descr", "asynchronous zone table load");
+}
+ATF_TC_BODY(asyncload_zt, tc) {
+ isc_result_t result;
+ dns_zone_t *zone1 = NULL, *zone2 = NULL, *zone3 = NULL;
+ dns_view_t *view;
+ dns_zt_t *zt;
+ dns_db_t *db = NULL;
+ isc_boolean_t done = ISC_FALSE;
+ int i = 0;
+
+ UNUSED(tc);
+
+ result = dns_test_begin(NULL, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ result = dns_test_makezone("foo", &zone1, NULL, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ dns_zone_setfile(zone1, "testdata/zt/zone1.db");
+ view = dns_zone_getview(zone1);
+
+ result = dns_test_makezone("bar", &zone2, view, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ dns_zone_setfile(zone2, "testdata/zt/zone1.db");
+
+ /* This one will fail to load */
+ result = dns_test_makezone("fake", &zone3, view, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ dns_zone_setfile(zone3, "testdata/zt/nonexistent.db");
+
+ zt = view->zonetable;
+ ATF_REQUIRE(zt != NULL);
+
+ result = dns_test_setupzonemgr();
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ result = dns_test_managezone(zone1);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ result = dns_test_managezone(zone2);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ result = dns_test_managezone(zone3);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ ATF_CHECK(!dns__zone_loadpending(zone1));
+ ATF_CHECK(!dns__zone_loadpending(zone2));
+ ATF_CHECK(!done);
+ dns_zt_asyncload(zt, all_done, (void *) &done);
+
+ isc_app_run();
+ while (!done && i++ < 5000)
+ dns_test_nap(1000);
+ ATF_CHECK(done);
+
+ /* Both zones should now be loaded; test them */
+ result = dns_zone_getdb(zone1, &db);
+ ATF_CHECK_EQ(result, ISC_R_SUCCESS);
+ ATF_CHECK(db != NULL);
+ if (db != NULL)
+ dns_db_detach(&db);
+
+ result = dns_zone_getdb(zone2, &db);
+ ATF_CHECK_EQ(result, ISC_R_SUCCESS);
+ ATF_CHECK(db != NULL);
+ if (db != NULL)
+ dns_db_detach(&db);
+
+ dns_test_releasezone(zone3);
+ dns_test_releasezone(zone2);
+ dns_test_releasezone(zone1);
+ dns_test_closezonemgr();
+
+ dns_zone_detach(&zone1);
+ dns_zone_detach(&zone2);
+ dns_zone_detach(&zone3);
+ dns_view_detach(&view);
+
+ dns_test_end();
+}
+
+/*
+ * Main
+ */
+ATF_TP_ADD_TCS(tp) {
+ ATF_TP_ADD_TC(tp, apply);
+ ATF_TP_ADD_TC(tp, asyncload_zone);
+ ATF_TP_ADD_TC(tp, asyncload_zt);
+ return (atf_no_error());
+}
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: view.c,v 1.182 2011/08/30 21:14:50 marka Exp $ */
+/* $Id: view.c,v 1.183 2011/09/02 21:15:35 each Exp $ */
/*! \file */
dns_view_load(dns_view_t *view, isc_boolean_t stop) {
REQUIRE(DNS_VIEW_VALID(view));
+ REQUIRE(view->zonetable != NULL);
return (dns_zt_load(view->zonetable, stop));
}
dns_view_loadnew(dns_view_t *view, isc_boolean_t stop) {
REQUIRE(DNS_VIEW_VALID(view));
+ REQUIRE(view->zonetable != NULL);
return (dns_zt_loadnew(view->zonetable, stop));
}
+
+isc_result_t
+dns_view_asyncload(dns_view_t *view, dns_zt_allloaded_t callback, void *arg) {
+ REQUIRE(DNS_VIEW_VALID(view));
+ REQUIRE(view->zonetable != NULL);
+
+ return (dns_zt_asyncload(view->zonetable, callback, arg));
+}
+
+
#endif /* BIND9 */
isc_result_t
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: zone.c,v 1.628 2011/08/31 06:49:10 marka Exp $ */
+/* $Id: zone.c,v 1.629 2011/09/02 21:15:36 each Exp $ */
/*! \file */
#include <dns/update.h>
#include <dns/xfrin.h>
#include <dns/zone.h>
+#include <dns/zt.h>
#include <dst/dst.h>
typedef struct dns_nsec3chain dns_nsec3chain_t;
typedef ISC_LIST(dns_nsec3chain_t) dns_nsec3chainlist_t;
typedef struct dns_keyfetch dns_keyfetch_t;
+typedef struct dns_asyncload dns_asyncload_t;
#define DNS_ZONE_CHECKLOCK
#ifdef DNS_ZONE_CHECKLOCK
unsigned int notifycnt;
isc_sockaddr_t notifyfrom;
isc_task_t *task;
+ isc_task_t *loadtask;
isc_sockaddr_t notifysrc4;
isc_sockaddr_t notifysrc6;
isc_sockaddr_t xfrsource4;
#define DNS_ZONEFLG_NEEDCOMPACT 0x02000000U
#define DNS_ZONEFLG_REFRESHING 0x04000000U /*%< Refreshing keydata */
#define DNS_ZONEFLG_THAW 0x08000000U
-/* #define DNS_ZONEFLG_XXXXX 0x10000000U XXXMPA unused. */
+#define DNS_ZONEFLG_LOADPENDING 0x10000000U /*%< Loading scheduled */
#define DNS_ZONEFLG_NODELAY 0x20000000U
#define DNS_ZONE_OPTION(z,o) (((z)->options & (o)) != 0)
isc_timermgr_t * timermgr;
isc_socketmgr_t * socketmgr;
isc_taskpool_t * zonetasks;
+ isc_taskpool_t * loadtasks;
isc_task_t * task;
isc_ratelimiter_t * rl;
isc_rwlock_t rwlock;
dns_fetch_t *fetch;
};
+/*%
+ * Hold state for an asynchronous load
+ */
+struct dns_asyncload {
+ dns_zone_t *zone;
+ dns_zt_zoneloaded_t loaded;
+ void *loaded_arg;
+};
+
#define HOUR 3600
#define DAY (24*HOUR)
#define MONTH (30*DAY)
zone->notifytype = dns_notifytype_yes;
zone->notifycnt = 0;
zone->task = NULL;
+ zone->loadtask = NULL;
zone->update_acl = NULL;
zone->forward_acl = NULL;
zone->notify_acl = NULL;
if (zone->task != NULL)
isc_task_detach(&zone->task);
+ if (zone->loadtask != NULL)
+ isc_task_detach(&zone->loadtask);
if (zone->zmgr != NULL)
dns_zonemgr_releasezone(zone->zmgr, zone);
goto cleanup;
}
-
INSIST(zone->db_argc >= 1);
rbt = strcmp(zone->db_argv[0], "rbt") == 0 ||
return (zone_load(zone, DNS_ZONELOADFLAG_NOSTAT));
}
+static void
+zone_asyncload(isc_task_t *task, isc_event_t *event) {
+ dns_asyncload_t *asl = event->ev_arg;
+ dns_zone_t *zone = asl->zone;
+ isc_result_t result = ISC_R_SUCCESS;
+
+ UNUSED(task);
+
+ REQUIRE(DNS_ZONE_VALID(zone));
+
+ if ((event->ev_attributes & ISC_EVENTATTR_CANCELED) != 0)
+ result = ISC_R_CANCELED;
+ isc_event_free(&event);
+ if (result == ISC_R_CANCELED ||
+ !DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING))
+ return;
+
+ result = zone_load(zone, 0);
+
+ LOCK_ZONE(zone);
+ DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADPENDING);
+ UNLOCK_ZONE(zone);
+
+ /* Inform the zone table we've finished loading */
+ if (asl->loaded != NULL)
+ (asl->loaded)(asl->loaded_arg, zone, task);
+
+ isc_mem_put(zone->mctx, asl, sizeof (*asl));
+}
+
+isc_result_t
+dns_zone_asyncload(dns_zone_t *zone, dns_zt_zoneloaded_t done, void *arg) {
+ isc_event_t *e;
+ dns_asyncload_t *asl = NULL;
+ isc_result_t result = ISC_R_SUCCESS;
+
+ REQUIRE(DNS_ZONE_VALID(zone));
+
+ if (zone->zmgr == NULL)
+ return (ISC_R_FAILURE);
+
+ asl = isc_mem_get(zone->mctx, sizeof (*asl));
+ if (asl == NULL)
+ CHECK(ISC_R_NOMEMORY);
+
+ asl->zone = zone;
+ asl->loaded = done;
+ asl->loaded_arg = arg;
+
+ e = isc_event_allocate(zone->zmgr->mctx, zone->zmgr,
+ DNS_EVENT_ZONELOAD,
+ zone_asyncload, asl,
+ sizeof(isc_event_t));
+ if (e == NULL)
+ CHECK(ISC_R_NOMEMORY);
+
+ LOCK_ZONE(zone);
+ DNS_ZONE_SETFLAG(zone, DNS_ZONEFLG_LOADPENDING);
+ isc_task_send(zone->loadtask, &e);
+ UNLOCK_ZONE(zone);
+
+ return (ISC_R_SUCCESS);
+
+ failure:
+ if (asl != NULL)
+ isc_mem_put(zone->mctx, asl, sizeof (*asl));
+ return (result);
+}
+
+isc_boolean_t
+dns__zone_loadpending(dns_zone_t *zone) {
+ REQUIRE(DNS_ZONE_VALID(zone));
+
+ return (ISC_TF(DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING)));
+}
+
isc_result_t
dns_zone_loadandthaw(dns_zone_t *zone) {
isc_result_t result;
if (DNS_ZONE_OPTION(zone, DNS_ZONEOPT_MANYERRORS))
options |= DNS_MASTER_MANYERRORS;
- if (zone->zmgr != NULL && zone->db != NULL && zone->task != NULL) {
+ if (zone->zmgr != NULL && zone->db != NULL && zone->loadtask != NULL) {
load = isc_mem_get(zone->mctx, sizeof(*load));
if (load == NULL)
return (ISC_R_NOMEMORY);
&load->callbacks.add_private);
if (result != ISC_R_SUCCESS)
goto cleanup;
- result = zonemgr_getio(zone->zmgr, ISC_TRUE, zone->task,
+ result = zonemgr_getio(zone->zmgr, ISC_TRUE, zone->loadtask,
zone_gotreadhandle, load,
&zone->readio);
if (result != ISC_R_SUCCESS) {
result = DNS_R_BADZONE;
goto cleanup;
}
-
if (zone->type == dns_zone_master &&
DNS_ZONE_OPTION(zone, DNS_ZONEOPT_CHECKDUPRR) &&
!zone_check_dup(zone, db)) {
dns_zone_log(zone, ISC_LOG_INFO, "loaded serial %u%s", serial,
dns_db_issecure(db) ? " (DNSSEC signed)" : "");
+ DNS_ZONE_CLRFLAG(zone, DNS_ZONEFLG_LOADPENDING);
return (result);
cleanup:
} else if (zone->type == dns_zone_master ||
zone->type == dns_zone_redirect)
dns_zone_log(zone, ISC_LOG_ERROR, "not loaded due to errors.");
+
return (result);
}
REQUIRE(DNS_ZONE_VALID(zone));
ENTER;
+ /*
+ * Are we pending load/reload?
+ */
+ if (DNS_ZONE_FLAG(zone, DNS_ZONEFLG_LOADPENDING))
+ return;
+
/*
* Configuring the view of this zone may have
* failed, for example because the config file
* had a syntax error. In that case, the view
- * adb or resolver, and we had better not try
- * to do maintenance on it.
+ * adb or resolver will be NULL, and we had better not try
+ * to do further maintenance on it.
*/
if (zone->view == NULL || zone->view->adb == NULL)
return;
set_key_expiry_warning(zone, zone->key_expiry,
isc_time_seconds(&now));
break;
+
default:
break;
}
zmgr->timermgr = timermgr;
zmgr->socketmgr = socketmgr;
zmgr->zonetasks = NULL;
+ zmgr->loadtasks = NULL;
zmgr->task = NULL;
zmgr->rl = NULL;
ISC_LIST_INIT(zmgr->zones);
REQUIRE(zone->zmgr == NULL);
isc_taskpool_gettask(zmgr->zonetasks, &zone->task);
+ isc_taskpool_gettask(zmgr->loadtasks, &zone->loadtask);
/*
* Set the task name. The tag will arbitrarily point to one
* to be managed last).
*/
isc_task_setname(zone->task, "zone", zone);
+ isc_task_setname(zone->loadtask, "loadzone", zone);
result = isc_timer_create(zmgr->timermgr, isc_timertype_inactive,
NULL, NULL,
&zone->timer);
if (result != ISC_R_SUCCESS)
- goto cleanup_task;
+ goto cleanup_tasks;
/*
* The timer "holds" a iref.
goto unlock;
- cleanup_task:
+ cleanup_tasks:
+ isc_task_detach(&zone->loadtask);
isc_task_detach(&zone->task);
unlock:
isc_task_destroy(&zmgr->task);
if (zmgr->zonetasks != NULL)
isc_taskpool_destroy(&zmgr->zonetasks);
+ if (zmgr->loadtasks != NULL)
+ isc_taskpool_destroy(&zmgr->loadtasks);
RWLOCK(&zmgr->rwlock, isc_rwlocktype_read);
for (zone = ISC_LIST_HEAD(zmgr->zones);
/*
* For anything fewer than 1000 zones we use 10 tasks in
- * the task pool. More than that, and we'll scale at one
+ * the task pools. More than that, and we'll scale at one
* task per 100 zones.
*/
if (ntasks < 10)
ntasks = 10;
- /* Create or resize the zone task pool. */
+ /* Create or resize the zone task pools. */
if (zmgr->zonetasks == NULL)
result = isc_taskpool_create(zmgr->taskmgr, zmgr->mctx,
ntasks, 2, &pool);
if (result == ISC_R_SUCCESS)
zmgr->zonetasks = pool;
+ pool = NULL;
+ if (zmgr->loadtasks == NULL)
+ result = isc_taskpool_create(zmgr->taskmgr, zmgr->mctx,
+ ntasks, 2, &pool);
+ else
+ result = isc_taskpool_expand(&zmgr->loadtasks, ntasks, &pool);
+
+ if (result == ISC_R_SUCCESS)
+ zmgr->loadtasks = pool;
+
+#ifdef BIND9
+ /*
+ * We always set all tasks in the zone-load task pool to
+ * privileged. This prevents other tasks in the system from
+ * running while the server task manager is in privileged
+ * mode.
+ *
+ * NOTE: If we start using task privileges for any other
+ * part of the system than zone tasks, then this will need to be
+ * revisted. In that case we'd want to turn on privileges for
+ * zone tasks only when we were loading, and turn them off the
+ * rest of the time. For now, however, it's okay to just
+ * set it and forget it.
+ */
+ isc_taskpool_setprivilege(zmgr->loadtasks, ISC_TRUE);
+#endif
+
return (result);
}
io = isc_mem_get(zmgr->mctx, sizeof(*io));
if (io == NULL)
return (ISC_R_NOMEMORY);
+
io->event = isc_event_allocate(zmgr->mctx, task, DNS_EVENT_IOREADY,
action, arg, sizeof(*io->event));
if (io->event == NULL) {
isc_mem_put(zmgr->mctx, io, sizeof(*io));
return (ISC_R_NOMEMORY);
}
+
io->zmgr = zmgr;
io->high = high;
io->task = NULL;
UNLOCK(&zmgr->iolock);
*iop = io;
- if (!queue) {
+ if (!queue)
isc_task_send(io->task, &io->event);
- }
return (ISC_R_SUCCESS);
}
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: zt.c,v 1.50 2011/03/21 23:47:21 tbox Exp $ */
+/* $Id: zt.c,v 1.51 2011/09/02 21:15:36 each Exp $ */
/*! \file */
isc_mem_t *mctx;
dns_rdataclass_t rdclass;
isc_rwlock_t rwlock;
+ dns_zt_allloaded_t loaddone;
+ void * loaddone_arg;
/* Locked by lock. */
isc_uint32_t references;
+ unsigned int loads_pending;
dns_rbt_t *table;
};
static isc_result_t
load(dns_zone_t *zone, void *uap);
+static isc_result_t
+asyncload(dns_zone_t *zone, void *callback);
+
static isc_result_t
loadnew(dns_zone_t *zone, void *uap);
static isc_result_t
freezezones(dns_zone_t *zone, void *uap);
+void
+startloading(dns_zt_t *zt, dns_zone_t *zone);
+
+void
+doneloading(dns_zt_t *zt, dns_zone_t *zone);
+
isc_result_t
dns_zt_create(isc_mem_t *mctx, dns_rdataclass_t rdclass, dns_zt_t **ztp)
{
zt->references = 1;
zt->rdclass = rdclass;
zt->magic = ZTMAGIC;
+ zt->loaddone = NULL;
+ zt->loaddone_arg = NULL;
+ zt->loads_pending = 0;
*ztp = zt;
return (ISC_R_SUCCESS);
load(dns_zone_t *zone, void *uap) {
isc_result_t result;
UNUSED(uap);
+
result = dns_zone_load(zone);
if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE)
result = ISC_R_SUCCESS;
+
+ return (result);
+}
+
+isc_result_t
+dns_zt_asyncload(dns_zt_t *zt, dns_zt_allloaded_t alldone, void *arg) {
+ isc_result_t result, tresult;
+ int pending;
+
+ REQUIRE(VALID_ZT(zt));
+
+ RWLOCK(&zt->rwlock, isc_rwlocktype_read);
+
+ INSIST(zt->loads_pending == 0);
+
+ result = dns_zt_apply2(zt, ISC_FALSE, &tresult, asyncload, doneloading);
+
+ pending = zt->loads_pending;
+ if (pending != 0) {
+ zt->loaddone = alldone;
+ zt->loaddone_arg = arg;
+ }
+
+ RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
+
+ if (pending == 0)
+ alldone(arg);
+
return (result);
}
+/*
+ * Initiates asynchronous loading of zone 'zone'. 'callback' is a
+ * pointer to a function which will be used to inform the caller when
+ * the zone loading is complete.
+ */
+static isc_result_t
+asyncload(dns_zone_t *zone, void *callback) {
+ isc_result_t result;
+ dns_zt_t *zt;
+
+ REQUIRE(zone != NULL);
+ zt = dns_zone_getview(zone)->zonetable;
+
+ result = dns_zone_asyncload(zone, (dns_zt_zoneloaded_t) callback, zt);
+ if (result == ISC_R_SUCCESS)
+ zt->loads_pending++;
+ return (ISC_R_SUCCESS);
+}
+
isc_result_t
dns_zt_loadnew(dns_zt_t *zt, isc_boolean_t stop) {
isc_result_t result;
loadnew(dns_zone_t *zone, void *uap) {
isc_result_t result;
UNUSED(uap);
+
result = dns_zone_loadnew(zone);
if (result == DNS_R_CONTINUE || result == DNS_R_UPTODATE ||
result == DNS_R_DYNAMIC)
RWLOCK(&zt->rwlock, isc_rwlocktype_read);
result = dns_zt_apply2(zt, ISC_FALSE, &tresult, freezezones, &freeze);
RWUNLOCK(&zt->rwlock, isc_rwlocktype_read);
+ if (tresult == ISC_R_NOTFOUND)
+ tresult = ISC_R_SUCCESS;
return ((result == ISC_R_SUCCESS) ? tresult : result);
}
/*
* The tree is empty.
*/
+ tresult = result;
result = ISC_R_NOMORE;
}
while (result == DNS_R_NEWORIGIN || result == ISC_R_SUCCESS) {
return (result);
}
+/*
+ * Decrement the loads_pending counter; when counter reaches
+ * zero, call the loaddone callback that was initially set by
+ * dns_zt_asyncload().
+ */
+void
+doneloading(dns_zt_t *zt, dns_zone_t *zone) {
+ dns_zt_allloaded_t alldone = NULL;
+ void *arg = NULL;
+
+ UNUSED(zone);
+
+ REQUIRE(VALID_ZT(zt));
+
+ RWLOCK(&zt->rwlock, isc_rwlocktype_write);
+ INSIST(zt->loads_pending != 0);
+ zt->loads_pending--;
+ if (zt->loads_pending == 0) {
+ alldone = zt->loaddone;
+ arg = zt->loaddone_arg;
+ zt->loaddone = NULL;
+ zt->loaddone_arg = NULL;
+ }
+ RWUNLOCK(&zt->rwlock, isc_rwlocktype_write);
+
+ if (alldone != NULL)
+ alldone(arg);
+}
+
/***
*** Private
***/
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: list.h,v 1.24 2007/06/19 23:47:18 tbox Exp $ */
+/* $Id: list.h,v 1.25 2011/09/02 21:15:38 each Exp $ */
#ifndef ISC_LIST_H
#define ISC_LIST_H 1
(list2).tail = NULL; \
} while (0)
+#define ISC_LIST_PREPENDLIST(list1, list2, link) \
+ do { \
+ if (ISC_LIST_EMPTY(list1)) \
+ (list1) = (list2); \
+ else if (!ISC_LIST_EMPTY(list2)) { \
+ (list2).tail->link.next = (list1).head; \
+ (list1).head->link.prev = (list2).tail; \
+ (list1).head = (list2).head; \
+ } \
+ (list2).head = NULL; \
+ (list2).tail = NULL; \
+ } while (0)
+
#define ISC_LIST_ENQUEUE(list, elt, link) ISC_LIST_APPEND(list, elt, link)
#define __ISC_LIST_ENQUEUEUNSAFE(list, elt, link) \
__ISC_LIST_APPENDUNSAFE(list, elt, link)
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: namespace.h,v 1.12 2011/08/23 17:02:53 each Exp $ */
+/* $Id: namespace.h,v 1.13 2011/09/02 21:15:38 each Exp $ */
#ifndef ISCAPI_NAMESPACE_H
#define ISCAPI_NAMESPACE_H 1
#define isc_app_run isc__app_run
#define isc_app_ctxrun isc__app_ctxrun
#define isc_app_shutdown isc__app_shutdown
+#define isc_app_ctxfinish isc__app_ctxfinish
#define isc_app_ctxshutdown isc__app_ctxshutdown
#define isc_app_ctxsuspend isc__app_ctxsuspend
#define isc_app_reload isc__app_reload
#define isc_task_gettag isc__task_gettag
#define isc_task_getcurrenttime isc__task_getcurrenttime
#define isc_taskmgr_create isc__taskmgr_create
+#define isc_taskmgr_setmode isc__taskmgr_setmode
+#define isc_taskmgr_mode isc__taskmgr_mode
#define isc_taskmgr_destroy isc__taskmgr_destroy
#define isc_task_beginexclusive isc__task_beginexclusive
#define isc_task_endexclusive isc__task_endexclusive
+#define isc_task_setprivilege isc__task_setprivilege
+#define isc_task_privilege isc__task_privilege
#define isc_timer_create isc__timer_create
#define isc_timer_reset isc__timer_reset
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: task.h,v 1.71 2011/02/03 12:18:12 tbox Exp $ */
+/* $Id: task.h,v 1.72 2011/09/02 21:15:38 each Exp $ */
#ifndef ISC_TASK_H
#define ISC_TASK_H 1
#define ISC_TASKEVENT_FIRSTEVENT (ISC_EVENTCLASS_TASK + 0)
#define ISC_TASKEVENT_SHUTDOWN (ISC_EVENTCLASS_TASK + 1)
+#define ISC_TASKEVENT_TEST (ISC_EVENTCLASS_TASK + 1)
#define ISC_TASKEVENT_LASTEVENT (ISC_EVENTCLASS_TASK + 65535)
/*****
*** Types
***/
+typedef enum {
+ isc_taskmgrmode_normal = 0,
+ isc_taskmgrmode_privileged
+} isc_taskmgrmode_t;
+
/*% Task and task manager methods */
typedef struct isc_taskmgrmethods {
void (*destroy)(isc_taskmgr_t **managerp);
+ void (*setmode)(isc_taskmgr_t *manager,
+ isc_taskmgrmode_t mode);
+ isc_taskmgrmode_t (*mode)(isc_taskmgr_t *manager);
isc_result_t (*taskcreate)(isc_taskmgr_t *manager,
unsigned int quantum,
isc_task_t **taskp);
void *tag);
isc_result_t (*beginexclusive)(isc_task_t *task);
void (*endexclusive)(isc_task_t *task);
+ void (*setprivilege)(isc_task_t *task, isc_boolean_t priv);
+ isc_boolean_t (*privilege)(isc_task_t *task);
} isc_taskmethods_t;
/*%
*\li 'task' is a valid task.
*/
+void
+isc_task_setprivilege(isc_task_t *task, isc_boolean_t priv);
+/*%<
+ * Set or unset the task's "privileged" flag depending on the value of
+ * 'priv'.
+ *
+ * Under normal circumstances this flag has no effect on the task behavior,
+ * but when the task manager has been set to privileged exeuction mode via
+ * isc_taskmgr_setmode(), only tasks with the flag set will be executed,
+ * and all other tasks will wait until they're done. Once all privileged
+ * tasks have finished executing, the task manager will automatically
+ * return to normal execution mode and nonprivileged task can resume.
+ *
+ * Requires:
+ *\li 'task' is a valid task.
+ */
+
+isc_boolean_t
+isc_task_privilege(isc_task_t *task);
+/*%<
+ * Returns the current value of the task's privilege flag.
+ *
+ * Requires:
+ *\li 'task' is a valid task.
+ */
+
/*****
***** Task Manager.
*****/
* manager shutting down.
*/
+void
+isc_taskmgr_setmode(isc_taskmgr_t *manager, isc_taskmgrmode_t mode);
+
+isc_taskmgrmode_t
+isc_taskmgr_mode(isc_taskmgr_t *manager);
+/*%<
+ * Set/get the current operating mode of the task manager. Valid modes are:
+ *
+ *\li isc_taskmgrmode_normal
+ *\li isc_taskmgrmode_privileged
+ *
+ * In privileged execution mode, only tasks that have had the "privilege"
+ * flag set via isc_task_setprivilege() can be executed. When all such
+ * tasks are complete, the manager automatically returns to normal mode
+ * and proceeds with running non-privileged ready tasks. This means it is
+ * necessary to have at least one privileged task waiting on the ready
+ * queue *before* setting the manager into privileged execution mode,
+ * which in turn means the task which calls this function should be in
+ * task-exclusive mode when it does so.
+ *
+ * Requires:
+ *
+ *\li 'manager' is a valid task manager.
+ */
+
void
isc_taskmgr_destroy(isc_taskmgr_t **managerp);
/*%<
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: taskpool.h,v 1.17 2011/07/07 23:47:50 tbox Exp $ */
+/* $Id: taskpool.h,v 1.18 2011/09/02 21:15:38 each Exp $ */
#ifndef ISC_TASKPOOL_H
#define ISC_TASKPOOL_H 1
* \li '*poolp' is a valid task pool.
*/
+void
+isc_taskpool_setprivilege(isc_taskpool_t *pool, isc_boolean_t priv);
+/*%<
+ * Set the privilege flag on all tasks in 'pool' to 'priv'. If 'priv' is
+ * true, then when the task manager is set into privileged mode, only
+ * tasks wihin this pool will be able to execute. (Note: It is important
+ * to turn the pool tasks' privilege back off before the last task finishes
+ * executing.)
+ *
+ * Requires:
+ * \li 'pool' is a valid task pool.
+ */
+
ISC_LANG_ENDDECLS
#endif /* ISC_TASKPOOL_H */
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: task.c,v 1.117 2011/02/03 12:18:12 tbox Exp $ */
+/* $Id: task.c,v 1.118 2011/09/02 21:15:37 each Exp $ */
/*! \file
* \author Principal Author: Bob Halley
#endif /* ISC_PLATFORM_USETHREADS */
#endif /* BIND9 */
-#ifndef USE_WORKER_THREADS
#include "task_p.h"
-#endif /* USE_WORKER_THREADS */
#ifdef ISC_TASK_TRACE
#define XTRACE(m) fprintf(stderr, "task %p thread %lu: %s\n", \
/* Locked by task manager lock. */
LINK(isc__task_t) link;
LINK(isc__task_t) ready_link;
+ LINK(isc__task_t) ready_priority_link;
};
#define TASK_F_SHUTTINGDOWN 0x01
+#define TASK_F_PRIVILEGED 0x02
#define TASK_SHUTTINGDOWN(t) (((t)->flags & TASK_F_SHUTTINGDOWN) \
!= 0)
unsigned int default_quantum;
LIST(isc__task_t) tasks;
isc__tasklist_t ready_tasks;
+ isc__tasklist_t ready_priority_tasks;
+ isc_taskmgrmode_t mode;
#ifdef ISC_PLATFORM_USETHREADS
isc_condition_t work_available;
isc_condition_t exclusive_granted;
+ isc_condition_t paused;
#endif /* ISC_PLATFORM_USETHREADS */
unsigned int tasks_running;
+ isc_boolean_t pause_requested;
isc_boolean_t exclusive_requested;
isc_boolean_t exiting;
#ifdef USE_SHARED_MANAGER
isc__task_beginexclusive(isc_task_t *task);
ISC_TASKFUNC_SCOPE void
isc__task_endexclusive(isc_task_t *task0);
+ISC_TASKFUNC_SCOPE void
+isc__task_setprivilege(isc_task_t *task0, isc_boolean_t priv);
+ISC_TASKFUNC_SCOPE isc_boolean_t
+isc__task_privilege(isc_task_t *task0);
+ISC_TASKFUNC_SCOPE void
+isc__taskmgr_setmode(isc_taskmgr_t *manager0, isc_taskmgrmode_t mode);
+ISC_TASKFUNC_SCOPE isc_taskmgrmode_t
+isc__taskmgr_mode(isc_taskmgr_t *manager0);
+
+static isc_boolean_t
+empty_readyq(isc__taskmgr_t *manager);
+
+static isc__task_t *
+pop_readyq(isc__taskmgr_t *manager);
+
+static void
+push_readyq(isc__taskmgr_t *manager, isc__task_t *task);
static struct isc__taskmethods {
isc_taskmethods_t methods;
isc__task_purge,
isc__task_purgerange,
isc__task_beginexclusive,
- isc__task_endexclusive
+ isc__task_endexclusive,
+ isc__task_setprivilege,
+ isc__task_privilege
}
#ifndef BIND9
,
static isc_taskmgrmethods_t taskmgrmethods = {
isc__taskmgr_destroy,
+ isc__taskmgr_setmode,
+ isc__taskmgr_mode,
isc__task_create
};
task->tag = NULL;
INIT_LINK(task, link);
INIT_LINK(task, ready_link);
+ INIT_LINK(task, ready_priority_link);
exiting = ISC_FALSE;
LOCK(&manager->lock);
}
INSIST(task->state == task_state_ready ||
task->state == task_state_running);
+
/*
* Note that we post shutdown events LIFO.
*/
return (was_idle);
}
+/*
+ * Moves a task onto the appropriate run queue.
+ *
+ * Caller must NOT hold manager lock.
+ */
static inline void
task_ready(isc__task_t *task) {
isc__taskmgr_t *manager = task->manager;
+#ifdef USE_WORKER_THREADS
+ isc_boolean_t has_privilege = isc__task_privilege((isc_task_t *) task);
+#endif /* USE_WORKER_THREADS */
REQUIRE(VALID_MANAGER(manager));
REQUIRE(task->state == task_state_ready);
XTRACE("task_ready");
LOCK(&manager->lock);
-
- ENQUEUE(manager->ready_tasks, task, ready_link);
+ push_readyq(manager, task);
#ifdef USE_WORKER_THREADS
- SIGNAL(&manager->work_available);
+ if (manager->mode == isc_taskmgrmode_normal || has_privilege)
+ SIGNAL(&manager->work_available);
#endif /* USE_WORKER_THREADS */
-
UNLOCK(&manager->lock);
}
REQUIRE(t != NULL);
LOCK(&task->lock);
-
*t = task->now;
-
UNLOCK(&task->lock);
}
/***
*** Task Manager.
***/
+
+/*
+ * Return ISC_TRUE if the current ready list for the manager, which is
+ * either ready_tasks or the ready_priority_tasks, depending on whether
+ * the manager is currently in normal or privileged execution mode.
+ *
+ * Caller must hold the task manager lock.
+ */
+static inline isc_boolean_t
+empty_readyq(isc__taskmgr_t *manager) {
+ isc__tasklist_t queue;
+
+ if (manager->mode == isc_taskmgrmode_normal)
+ queue = manager->ready_tasks;
+ else
+ queue = manager->ready_priority_tasks;
+
+ return (ISC_TF(EMPTY(queue)));
+}
+
+/*
+ * Dequeue and return a pointer to the first task on the current ready
+ * list for the manager.
+ * If the task is privileged, dequeue it from the other ready list
+ * as well.
+ *
+ * Caller must hold the task manager lock.
+ */
+static inline isc__task_t *
+pop_readyq(isc__taskmgr_t *manager) {
+ isc__task_t *task;
+
+ if (manager->mode == isc_taskmgrmode_normal)
+ task = HEAD(manager->ready_tasks);
+ else
+ task = HEAD(manager->ready_priority_tasks);
+
+ if (task != NULL) {
+ DEQUEUE(manager->ready_tasks, task, ready_link);
+ if (ISC_LINK_LINKED(task, ready_priority_link))
+ DEQUEUE(manager->ready_priority_tasks, task,
+ ready_priority_link);
+ }
+
+ return (task);
+}
+
+/*
+ * Push 'task' onto the ready_tasks queue. If 'task' has the privilege
+ * flag set, then also push it onto the ready_priority_tasks queue.
+ *
+ * Caller must hold the task manager lock.
+ */
+static inline void
+push_readyq(isc__taskmgr_t *manager, isc__task_t *task) {
+ ENQUEUE(manager->ready_tasks, task, ready_link);
+ if ((task->flags & TASK_F_PRIVILEGED) != 0)
+ ENQUEUE(manager->ready_priority_tasks, task,
+ ready_priority_link);
+}
+
static void
dispatch(isc__taskmgr_t *manager) {
isc__task_t *task;
#ifndef USE_WORKER_THREADS
unsigned int total_dispatch_count = 0;
- isc__tasklist_t ready_tasks;
+ isc__tasklist_t new_ready_tasks;
+ isc__tasklist_t new_priority_tasks;
#endif /* USE_WORKER_THREADS */
REQUIRE(VALID_MANAGER(manager));
*/
#ifndef USE_WORKER_THREADS
- ISC_LIST_INIT(ready_tasks);
+ ISC_LIST_INIT(new_ready_tasks);
+ ISC_LIST_INIT(new_priority_tasks);
#endif
LOCK(&manager->lock);
+
while (!FINISHED(manager)) {
#ifdef USE_WORKER_THREADS
/*
* the task while only holding the manager lock, and then
* change the task to running state while only holding the
* task lock.
+ *
+ * If a pause has been requested, don't do any work
+ * until it's been released.
*/
- while ((EMPTY(manager->ready_tasks) ||
- manager->exclusive_requested) &&
- !FINISHED(manager))
+ while ((empty_readyq(manager) || manager->pause_requested ||
+ manager->exclusive_requested) && !FINISHED(manager))
{
XTHREADTRACE(isc_msgcat_get(isc_msgcat,
ISC_MSGSET_GENERAL,
}
#else /* USE_WORKER_THREADS */
if (total_dispatch_count >= DEFAULT_TASKMGR_QUANTUM ||
- EMPTY(manager->ready_tasks))
+ empty_readyq(manager))
break;
#endif /* USE_WORKER_THREADS */
XTHREADTRACE(isc_msgcat_get(isc_msgcat, ISC_MSGSET_TASK,
ISC_MSG_WORKING, "working"));
- task = HEAD(manager->ready_tasks);
+ task = pop_readyq(manager);
if (task != NULL) {
unsigned int dispatch_count = 0;
isc_boolean_t done = ISC_FALSE;
* have a task to do. We must reacquire the manager
* lock before exiting the 'if (task != NULL)' block.
*/
- DEQUEUE(manager->ready_tasks, task, ready_link);
manager->tasks_running++;
UNLOCK(&manager->lock);
if (manager->exclusive_requested &&
manager->tasks_running == 1) {
SIGNAL(&manager->exclusive_granted);
+ } else if (manager->pause_requested &&
+ manager->tasks_running == 0) {
+ SIGNAL(&manager->paused);
}
#endif /* USE_WORKER_THREADS */
if (requeue) {
* might even hurt rather than help.
*/
#ifdef USE_WORKER_THREADS
- ENQUEUE(manager->ready_tasks, task,
- ready_link);
+ push_readyq(manager, task);
#else
- ENQUEUE(ready_tasks, task, ready_link);
+ ENQUEUE(new_ready_tasks, task, ready_link);
+ if ((task->flags & TASK_F_PRIVILEGED) != 0)
+ ENQUEUE(new_priority_tasks, task,
+ ready_priority_link);
#endif
}
}
+
+#ifdef USE_WORKER_THREADS
+ /*
+ * If we are in privileged execution mode and there are no
+ * tasks remaining on the current ready queue, then
+ * we're stuck. Automatically drop privileges at that
+ * point and continue with the regular ready queue.
+ */
+ if (manager->tasks_running == 0 && empty_readyq(manager)) {
+ manager->mode = isc_taskmgrmode_normal;
+ if (!empty_readyq(manager))
+ BROADCAST(&manager->work_available);
+ }
+#endif
}
+
#ifndef USE_WORKER_THREADS
- ISC_LIST_APPENDLIST(manager->ready_tasks, ready_tasks, ready_link);
+ ISC_LIST_APPENDLIST(&manager->ready_tasks, new_ready_tasks, ready_link);
+ ISC_LIST_APPENDLIST(&manager->ready_priority_tasks, new_priority_tasks,
+ ready_priority_link);
+ if (empty_readyq(manager))
+ manager->mode == isc_taskmgrmode_normal;
#endif
+
UNLOCK(&manager->lock);
}
#ifdef USE_WORKER_THREADS
(void)isc_condition_destroy(&manager->exclusive_granted);
(void)isc_condition_destroy(&manager->work_available);
+ (void)isc_condition_destroy(&manager->paused);
isc_mem_free(manager->mctx, manager->threads);
#endif /* USE_WORKER_THREADS */
DESTROYLOCK(&manager->lock);
manager->common.methods = &taskmgrmethods;
manager->common.impmagic = TASK_MANAGER_MAGIC;
manager->common.magic = ISCAPI_TASKMGR_MAGIC;
+ manager->mode = isc_taskmgrmode_normal;
manager->mctx = NULL;
result = isc_mutex_init(&manager->lock);
if (result != ISC_R_SUCCESS)
result = ISC_R_UNEXPECTED;
goto cleanup_workavailable;
}
+ if (isc_condition_init(&manager->paused) != ISC_R_SUCCESS) {
+ UNEXPECTED_ERROR(__FILE__, __LINE__,
+ "isc_condition_init() %s",
+ isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
+ ISC_MSG_FAILED, "failed"));
+ result = ISC_R_UNEXPECTED;
+ goto cleanup_exclusivegranted;
+ }
#endif /* USE_WORKER_THREADS */
if (default_quantum == 0)
default_quantum = DEFAULT_DEFAULT_QUANTUM;
manager->default_quantum = default_quantum;
INIT_LIST(manager->tasks);
INIT_LIST(manager->ready_tasks);
+ INIT_LIST(manager->ready_priority_tasks);
manager->tasks_running = 0;
manager->exclusive_requested = ISC_FALSE;
+ manager->pause_requested = ISC_FALSE;
manager->exiting = ISC_FALSE;
isc_mem_attach(mctx, &manager->mctx);
return (ISC_R_SUCCESS);
#ifdef USE_WORKER_THREADS
+ cleanup_exclusivegranted:
+ (void)isc_condition_destroy(&manager->exclusive_granted);
cleanup_workavailable:
(void)isc_condition_destroy(&manager->work_available);
cleanup_threads:
INSIST(!manager->exiting);
manager->exiting = ISC_TRUE;
+ /*
+ * If privileged mode was on, turn it off.
+ */
+ manager->mode = isc_taskmgrmode_normal;
+
/*
* Post shutdown event(s) to every task (if they haven't already been
* posted).
task = NEXT(task, link)) {
LOCK(&task->lock);
if (task_shutdown(task))
- ENQUEUE(manager->ready_tasks, task, ready_link);
+ push_readyq(manager, task);
UNLOCK(&task->lock);
}
#ifdef USE_WORKER_THREADS
*managerp = NULL;
}
+ISC_TASKFUNC_SCOPE void
+isc__taskmgr_setmode(isc_taskmgr_t *manager0, isc_taskmgrmode_t mode) {
+ isc__taskmgr_t *manager = (isc__taskmgr_t *)manager0;
+
+ LOCK(&manager->lock);
+ manager->mode = mode;
+ UNLOCK(&manager->lock);
+}
+
+ISC_TASKFUNC_SCOPE isc_taskmgrmode_t
+isc__taskmgr_mode(isc_taskmgr_t *manager0) {
+ isc__taskmgr_t *manager = (isc__taskmgr_t *)manager0;
+ isc_taskmgrmode_t mode;
+ LOCK(&manager->lock);
+ mode = manager->mode;
+ UNLOCK(&manager->lock);
+ return (mode);
+}
+
#ifndef USE_WORKER_THREADS
isc_boolean_t
isc__taskmgr_ready(isc_taskmgr_t *manager0) {
isc__taskmgr_t *manager = (isc__taskmgr_t *)manager0;
+ isc_boolean_t is_ready;
#ifdef USE_SHARED_MANAGER
if (manager == NULL)
#endif
if (manager == NULL)
return (ISC_FALSE);
- return (ISC_TF(!ISC_LIST_EMPTY(manager->ready_tasks)));
+
+ LOCK(&manager->lock);
+ is_ready = !empty_readyq(manager);
+ UNLOCK(&manager->lock);
+
+ return (is_ready);
}
isc_result_t
return (ISC_R_SUCCESS);
}
+#else
+ISC_TASKFUNC_SCOPE void
+isc__taskmgr_pause(isc_taskmgr_t *manager0) {
+ isc__taskmgr_t *manager = (isc__taskmgr_t *)manager0;
+ LOCK(&manager->lock);
+ while (manager->tasks_running > 0) {
+ WAIT(&manager->paused, &manager->lock);
+ }
+ manager->pause_requested = ISC_TRUE;
+ UNLOCK(&manager->lock);
+}
+
+ISC_TASKFUNC_SCOPE void
+isc__taskmgr_resume(isc_taskmgr_t *manager0) {
+ isc__taskmgr_t *manager = (isc__taskmgr_t *)manager0;
+
+ LOCK(&manager->lock);
+ if (manager->pause_requested) {
+ manager->pause_requested = ISC_FALSE;
+ BROADCAST(&manager->work_available);
+ }
+ UNLOCK(&manager->lock);
+}
#endif /* USE_WORKER_THREADS */
ISC_TASKFUNC_SCOPE isc_result_t
#endif
}
+ISC_TASKFUNC_SCOPE void
+isc__task_setprivilege(isc_task_t *task0, isc_boolean_t priv) {
+ isc__task_t *task = (isc__task_t *)task0;
+ isc__taskmgr_t *manager = task->manager;
+ isc_boolean_t oldpriv;
+
+ LOCK(&task->lock);
+ oldpriv = ISC_TF((task->flags & TASK_F_PRIVILEGED) != 0);
+ if (priv)
+ task->flags |= TASK_F_PRIVILEGED;
+ else
+ task->flags &= ~TASK_F_PRIVILEGED;
+ UNLOCK(&task->lock);
+
+ if (priv == oldpriv)
+ return;
+
+ LOCK(&manager->lock);
+ if (priv && ISC_LINK_LINKED(task, ready_link))
+ ENQUEUE(manager->ready_priority_tasks, task,
+ ready_priority_link);
+ else if (!priv && ISC_LINK_LINKED(task, ready_priority_link))
+ DEQUEUE(manager->ready_priority_tasks, task,
+ ready_priority_link);
+ UNLOCK(&manager->lock);
+}
+
+ISC_TASKFUNC_SCOPE isc_boolean_t
+isc__task_privilege(isc_task_t *task0) {
+ isc__task_t *task = (isc__task_t *)task0;
+ isc_boolean_t priv;
+
+ LOCK(&task->lock);
+ priv = ISC_TF((task->flags & TASK_F_PRIVILEGED) != 0);
+ UNLOCK(&task->lock);
+ return (priv);
+}
+
#ifdef USE_SOCKETIMPREGISTER
isc_result_t
isc__task_register() {
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: task_api.c,v 1.7 2010/12/22 23:46:59 tbox Exp $ */
+/* $Id: task_api.c,v 1.8 2011/09/02 21:15:38 each Exp $ */
#include <config.h>
ENSURE(*managerp == NULL);
}
+void
+isc_taskmgr_setmode(isc_taskmgr_t *manager, isc_taskmgrmode_t mode) {
+ REQUIRE(ISCAPI_TASKMGR_VALID(manager));
+
+ manager->methods->setmode(manager, mode);
+}
+
+isc_taskmgrmode_t
+isc_taskmgr_mode(isc_taskmgr_t *manager) {
+ REQUIRE(ISCAPI_TASKMGR_VALID(manager));
+
+ return (manager->methods->mode(manager));
+}
+
isc_result_t
isc_task_create(isc_taskmgr_t *manager, unsigned int quantum,
isc_task_t **taskp)
task->methods->endexclusive(task);
}
+void
+isc_task_setprivilege(isc_task_t *task, isc_boolean_t priv) {
+ REQUIRE(ISCAPI_TASK_VALID(task));
+
+ task->methods->setprivilege(task, priv);
+}
+
+isc_boolean_t
+isc_task_privilege(isc_task_t *task) {
+ REQUIRE(ISCAPI_TASK_VALID(task));
+
+ return (task->methods->privilege(task));
+}
+
/*%
* This is necessary for libisc's internal timer implementation. Other
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: task_p.h,v 1.13 2009/09/02 23:48:02 tbox Exp $ */
+/* $Id: task_p.h,v 1.14 2011/09/02 21:15:38 each Exp $ */
#ifndef ISC_TASK_P_H
#define ISC_TASK_P_H
/*! \file */
+#if defined(BIND9) && defined(ISC_PLATFORM_USETHREADS)
+void
+isc__taskmgr_pause(isc_taskmgr_t *taskmgr);
+
+void
+isc__taskmgr_resume(isc_taskmgr_t *taskmgr);
+#else
isc_boolean_t
isc__taskmgr_ready(isc_taskmgr_t *taskmgr);
isc_result_t
isc__taskmgr_dispatch(isc_taskmgr_t *taskmgr);
+#endif /* !BIND9 || !ISC_PLATFORM_USETHREADS */
#endif /* ISC_TASK_P_H */
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: taskpool.c,v 1.20 2011/07/07 23:47:49 tbox Exp $ */
+/* $Id: taskpool.c,v 1.21 2011/09/02 21:15:38 each Exp $ */
/*! \file */
unsigned int i;
isc_taskpool_t *pool = *poolp;
for (i = 0; i < pool->ntasks; i++) {
- if (pool->tasks[i] != NULL) {
+ if (pool->tasks[i] != NULL)
isc_task_detach(&pool->tasks[i]);
- }
}
isc_mem_put(pool->mctx, pool->tasks,
pool->ntasks * sizeof(isc_task_t *));
*poolp = NULL;
}
+void
+isc_taskpool_setprivilege(isc_taskpool_t *pool, isc_boolean_t priv) {
+ unsigned int i;
+
+ REQUIRE(pool != NULL);
+ for (i = 0; i < pool->ntasks; i++) {
+ if (pool->tasks[i] != NULL)
+ isc_task_setprivilege(pool->tasks[i], priv);
+ }
+}
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
-# $Id: Makefile.in,v 1.6 2011/08/23 01:29:38 each Exp $
+# $Id: Makefile.in,v 1.7 2011/09/02 21:15:38 each Exp $
srcdir = @srcdir@
VPATH = @srcdir@
LIBS = @LIBS@ @ATFLIBS@
OBJS = isctest.@O@
-SRCS = isctest.c taskpool_test.c socket_test.c hash_test.c
+SRCS = isctest.c taskpool_test.c socket_test.c hash_test.c \
+ task_test.c
SUBDIRS =
-TARGETS = taskpool_test@EXEEXT@ socket_test@EXEEXT@ hash_test@EXEEXT@
+TARGETS = taskpool_test@EXEEXT@ socket_test@EXEEXT@ hash_test@EXEEXT@ \
+ task_test@EXEEXT@
@BIND9_MAKE_RULES@
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
taskpool_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS}
+task_test@EXEEXT@: task_test.@O@ isctest.@O@ ${ISCDEPLIBS}
+ ${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
+ task_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS}
+
socket_test@EXEEXT@: socket_test.@O@ isctest.@O@ ${ISCDEPLIBS}
${LIBTOOL_MODE_LINK} ${PURIFY} ${CC} ${CFLAGS} ${LDFLAGS} -o $@ \
socket_test.@O@ isctest.@O@ ${ISCLIBS} ${LIBS}
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: isctest.c,v 1.3 2011/07/28 04:04:37 each Exp $ */
+/* $Id: isctest.c,v 1.4 2011/09/02 21:15:38 each Exp $ */
/*! \file */
#include <config.h>
+#include <time.h>
+
#include <isc/app.h>
#include <isc/buffer.h>
#include <isc/entropy.h>
isc_mem_destroy(&mctx);
}
+/*
+ * Sleep for 'usec' microseconds.
+ */
+void
+isc_test_nap(isc_uint32_t usec) {
+#ifdef HAVE_NANOSLEEP
+ struct timespec ts;
+
+ ts.tv_sec = usec / 1000000;
+ ts.tv_nsec = (usec % 1000000) * 1000;
+ nanosleep(&ts, NULL);
+#elif HAVE_USLEEP
+ usleep(usec);
+#else
+ /*
+ * No fractional-second sleep function is available, so we
+ * round up to the nearest second and sleep instead
+ */
+ sleep((usec / 1000000) + 1);
+#endif
+}
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: isctest.h,v 1.3 2011/07/28 04:04:37 each Exp $ */
+/* $Id: isctest.h,v 1.4 2011/09/02 21:15:38 each Exp $ */
/*! \file */
void
isc_test_end(void);
+void
+isc_test_nap(isc_uint32_t usec);
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: socket_test.c,v 1.3 2011/07/28 23:47:59 tbox Exp $ */
+/* $Id: socket_test.c,v 1.4 2011/09/02 21:15:38 each Exp $ */
/*! \file */
isc_event_free(&event);
}
-static void
-nap(isc_uint32_t usec) {
-#ifdef HAVE_NANOSLEEP
- struct timespec ts;
-
- ts.tv_sec = usec / 1000000;
- ts.tv_nsec = (usec % 1000000) * 1000;
- nanosleep(&ts, NULL);
-#elif HAVE_USLEEP
- usleep(usec);
-#else
- /* Round up to the nearest second and sleep, instead */
- sleep((usec / 1000000) + 1);
-#endif
-}
-
static isc_result_t
waitfor(completion_t *completion) {
int i = 0;
while (isc__taskmgr_ready(taskmgr))
isc__taskmgr_dispatch(taskmgr);
#endif
- nap(1000);
+ isc_test_nap(1000);
}
if (completion->done)
return (ISC_R_SUCCESS);
--- /dev/null
+/*
+ * Copyright (C) 2011 Internet Systems Consortium, Inc. ("ISC")
+ *
+ * Permission to use, copy, modify, and/or distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+ * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+ * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+ * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+ * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+ * PERFORMANCE OF THIS SOFTWARE.
+ */
+
+/* $Id: task_test.c,v 1.2 2011/09/02 21:15:38 each Exp $ */
+
+/*! \file */
+
+#include <config.h>
+
+#include <atf-c.h>
+
+#include <unistd.h>
+
+#include <isc/task.h>
+#include <isc/util.h>
+
+#include "../task_p.h"
+#include "isctest.h"
+
+/*
+ * Helper functions
+ */
+
+/* task event handler, sets a boolean to true */
+int counter = 0;
+isc_mutex_t set_lock;
+
+static void
+set(isc_task_t *task, isc_event_t *event) {
+ int *value = (int *) event->ev_arg;
+
+ UNUSED(task);
+
+ isc_event_free(&event);
+ LOCK(&set_lock);
+ *value = counter++;
+ UNLOCK(&set_lock);
+}
+
+static void
+set_and_drop(isc_task_t *task, isc_event_t *event) {
+ int *value = (int *) event->ev_arg;
+
+ UNUSED(task);
+
+ isc_event_free(&event);
+ LOCK(&set_lock);
+ *value = (int) isc_taskmgr_mode(taskmgr);
+ counter++;
+ UNLOCK(&set_lock);
+ isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_normal);
+}
+
+/*
+ * Individual unit tests
+ */
+
+/* Create a task */
+ATF_TC(create_task);
+ATF_TC_HEAD(create_task, tc) {
+ atf_tc_set_md_var(tc, "descr", "create and destroy a task");
+}
+ATF_TC_BODY(create_task, tc) {
+ isc_result_t result;
+ isc_task_t *task = NULL;
+
+ UNUSED(tc);
+
+ result = isc_test_begin(NULL, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ result = isc_task_create(taskmgr, 0, &task);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ isc_task_destroy(&task);
+ ATF_REQUIRE_EQ(task, NULL);
+
+ isc_test_end();
+}
+
+/* Process events */
+ATF_TC(all_events);
+ATF_TC_HEAD(all_events, tc) {
+ atf_tc_set_md_var(tc, "descr", "process task events");
+}
+ATF_TC_BODY(all_events, tc) {
+ isc_result_t result;
+ isc_task_t *task = NULL;
+ isc_event_t *event;
+ int a = 0, b = 0;
+ int i = 0;
+
+ UNUSED(tc);
+
+ counter = 1;
+
+ result = isc_test_begin(NULL, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ result = isc_task_create(taskmgr, 0, &task);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ /* First event */
+ event = isc_event_allocate(mctx, task, ISC_TASKEVENT_TEST,
+ set, &a, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(a, 0);
+ isc_task_send(task, &event);
+
+ event = isc_event_allocate(mctx, task, ISC_TASKEVENT_TEST,
+ set, &b, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(b, 0);
+ isc_task_send(task, &event);
+
+ while (a == 0 && b == 0 && i++ < 5000) {
+#ifndef ISC_PLATFORM_USETHREADS
+ while (isc__taskmgr_ready(taskmgr))
+ isc__taskmgr_dispatch(taskmgr);
+#endif
+ isc_test_nap(1000);
+ }
+
+ ATF_CHECK(a != 0);
+ ATF_CHECK(b != 0);
+
+ isc_task_destroy(&task);
+ ATF_REQUIRE_EQ(task, NULL);
+
+ isc_test_end();
+}
+
+/* Privileged events */
+ATF_TC(privileged_events);
+ATF_TC_HEAD(privileged_events, tc) {
+ atf_tc_set_md_var(tc, "descr", "process privileged events");
+}
+ATF_TC_BODY(privileged_events, tc) {
+ isc_result_t result;
+ isc_task_t *task1 = NULL, *task2 = NULL;
+ isc_event_t *event;
+ int a = 0, b = 0, c = 0, d = 0, e = 0;
+ int i = 0;
+
+ UNUSED(tc);
+
+ counter = 1;
+ result = isc_mutex_init(&set_lock);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ result = isc_test_begin(NULL, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+#ifdef ISC_PLATFORM_USETHREADS
+ /*
+ * Pause the task manager so we can fill up the work queue
+ * without things happening while we do it.
+ */
+ isc__taskmgr_pause(taskmgr);
+#endif
+
+ result = isc_task_create(taskmgr, 0, &task1);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ isc_task_setname(task1, "privileged", NULL);
+ ATF_CHECK(!isc_task_privilege(task1));
+ isc_task_setprivilege(task1, ISC_TRUE);
+ ATF_CHECK(isc_task_privilege(task1));
+
+ result = isc_task_create(taskmgr, 0, &task2);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ isc_task_setname(task2, "normal", NULL);
+ ATF_CHECK(!isc_task_privilege(task2));
+
+ /* First event: privileged */
+ event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
+ set, &a, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(a, 0);
+ isc_task_send(task1, &event);
+
+ /* Second event: not privileged */
+ event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
+ set, &b, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(b, 0);
+ isc_task_send(task2, &event);
+
+ /* Third event: privileged */
+ event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
+ set, &c, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(c, 0);
+ isc_task_send(task1, &event);
+
+ /* Fourth event: privileged */
+ event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
+ set, &d, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(d, 0);
+ isc_task_send(task1, &event);
+
+ /* Fifth event: not privileged */
+ event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
+ set, &e, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(e, 0);
+ isc_task_send(task2, &event);
+
+ ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
+ isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_privileged);
+ ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_privileged);
+
+#ifdef ISC_PLATFORM_USETHREADS
+ isc__taskmgr_resume(taskmgr);
+#endif
+
+ /* We're waiting for *all* variables to be set */
+ while ((a == 0 || b == 0 || c == 0 || d == 0 || e == 0) && i++ < 5000) {
+#ifndef ISC_PLATFORM_USETHREADS
+ while (isc__taskmgr_ready(taskmgr))
+ isc__taskmgr_dispatch(taskmgr);
+#endif
+ isc_test_nap(1000);
+ }
+
+ /*
+ * We can't guarantee what order the events fire, but
+ * we do know the privileged tasks that set a, c, and d
+ * would have fired first.
+ */
+ ATF_CHECK(a <= 3);
+ ATF_CHECK(c <= 3);
+ ATF_CHECK(d <= 3);
+
+ /* ...and the non-privileged tasks that set b and e, last */
+ ATF_CHECK(b >= 4);
+ ATF_CHECK(e >= 4);
+
+ ATF_CHECK_EQ(counter, 6);
+
+ isc_task_setprivilege(task1, ISC_FALSE);
+ ATF_CHECK(!isc_task_privilege(task1));
+
+ ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
+
+ isc_task_destroy(&task1);
+ ATF_REQUIRE_EQ(task1, NULL);
+ isc_task_destroy(&task2);
+ ATF_REQUIRE_EQ(task2, NULL);
+
+ isc_test_end();
+}
+
+/*
+ * Edge case: this tests that the task manager behaves as expected when
+ * we explicitly set it into normal mode *while* running privileged.
+ */
+ATF_TC(privilege_drop);
+ATF_TC_HEAD(privilege_drop, tc) {
+ atf_tc_set_md_var(tc, "descr", "process privileged events");
+}
+ATF_TC_BODY(privilege_drop, tc) {
+ isc_result_t result;
+ isc_task_t *task1 = NULL, *task2 = NULL;
+ isc_event_t *event;
+ int a = 0, b = 0, c = 0, d = 0, e = 0;
+ int i = 0;
+
+ UNUSED(tc);
+
+ counter = 1;
+ result = isc_mutex_init(&set_lock);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ result = isc_test_begin(NULL, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+#ifdef ISC_PLATFORM_USETHREADS
+ /*
+ * Pause the task manager so we can fill up the work queue
+ * without things happening while we do it.
+ */
+ isc__taskmgr_pause(taskmgr);
+#endif
+
+ result = isc_task_create(taskmgr, 0, &task1);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ isc_task_setname(task1, "privileged", NULL);
+ ATF_CHECK(!isc_task_privilege(task1));
+ isc_task_setprivilege(task1, ISC_TRUE);
+ ATF_CHECK(isc_task_privilege(task1));
+
+ result = isc_task_create(taskmgr, 0, &task2);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ isc_task_setname(task2, "normal", NULL);
+ ATF_CHECK(!isc_task_privilege(task2));
+
+ /* First event: privileged */
+ event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
+ set_and_drop, &a, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(a, 0);
+ isc_task_send(task1, &event);
+
+ /* Second event: not privileged */
+ event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
+ set_and_drop, &b, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(b, 0);
+ isc_task_send(task2, &event);
+
+ /* Third event: privileged */
+ event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
+ set_and_drop, &c, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(c, 0);
+ isc_task_send(task1, &event);
+
+ /* Fourth event: privileged */
+ event = isc_event_allocate(mctx, task1, ISC_TASKEVENT_TEST,
+ set_and_drop, &d, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(d, 0);
+ isc_task_send(task1, &event);
+
+ /* Fifth event: not privileged */
+ event = isc_event_allocate(mctx, task2, ISC_TASKEVENT_TEST,
+ set_and_drop, &e, sizeof (isc_event_t));
+ ATF_REQUIRE(event != NULL);
+
+ ATF_CHECK_EQ(e, 0);
+ isc_task_send(task2, &event);
+
+ ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
+ isc_taskmgr_setmode(taskmgr, isc_taskmgrmode_privileged);
+ ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_privileged);
+
+#ifdef ISC_PLATFORM_USETHREADS
+ isc__taskmgr_resume(taskmgr);
+#endif
+
+ /* We're waiting for *any* variable to be set */
+ while ((a == 0 && b == 0 && c == 0 && d == 0 && e == 0) && i++ < 5000) {
+#ifndef ISC_PLATFORM_USETHREADS
+ while (isc__taskmgr_ready(taskmgr))
+ isc__taskmgr_dispatch(taskmgr);
+#endif
+ isc_test_nap(1000);
+ }
+
+ /*
+ * We can't guarantee what order the events fire, but
+ * we do know *exactly one* of the privileged tasks will
+ * have run in privileged mode...
+ */
+ ATF_CHECK(a == isc_taskmgrmode_privileged ||
+ c == isc_taskmgrmode_privileged ||
+ d == isc_taskmgrmode_privileged);
+ ATF_CHECK(a + c + d == isc_taskmgrmode_privileged);
+
+ /* ...and neither of the non-privileged tasks did... */
+ ATF_CHECK(b == isc_taskmgrmode_normal || e == isc_taskmgrmode_normal);
+
+ /* ...but all five of them did run. */
+ ATF_CHECK_EQ(counter, 6);
+
+ ATF_CHECK_EQ(isc_taskmgr_mode(taskmgr), isc_taskmgrmode_normal);
+
+ isc_task_destroy(&task1);
+ ATF_REQUIRE_EQ(task1, NULL);
+ isc_task_destroy(&task2);
+ ATF_REQUIRE_EQ(task2, NULL);
+
+ isc_test_end();
+}
+/*
+ * Main
+ */
+ATF_TP_ADD_TCS(tp) {
+ ATF_TP_ADD_TC(tp, create_task);
+ ATF_TP_ADD_TC(tp, all_events);
+ ATF_TP_ADD_TC(tp, privileged_events);
+ ATF_TP_ADD_TC(tp, privilege_drop);
+
+ return (atf_no_error());
+}
+
* PERFORMANCE OF THIS SOFTWARE.
*/
-/* $Id: taskpool_test.c,v 1.3 2011/07/28 04:04:37 each Exp $ */
+/* $Id: taskpool_test.c,v 1.4 2011/09/02 21:15:38 each Exp $ */
/*! \file */
isc_test_end();
}
+/* Get tasks */
+ATF_TC(set_privilege);
+ATF_TC_HEAD(set_privilege, tc) {
+ atf_tc_set_md_var(tc, "descr", "create a taskpool");
+}
+ATF_TC_BODY(set_privilege, tc) {
+ isc_result_t result;
+ isc_taskpool_t *pool = NULL;
+ isc_task_t *task1 = NULL, *task2 = NULL, *task3 = NULL;
+
+ UNUSED(tc);
+
+ result = isc_test_begin(NULL, ISC_TRUE);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+
+ result = isc_taskpool_create(taskmgr, mctx, 2, 2, &pool);
+ ATF_REQUIRE_EQ(result, ISC_R_SUCCESS);
+ ATF_REQUIRE_EQ(isc_taskpool_size(pool), 2);
+
+ isc_taskpool_setprivilege(pool, ISC_TRUE);
+
+ isc_taskpool_gettask(pool, &task1);
+ isc_taskpool_gettask(pool, &task2);
+ isc_taskpool_gettask(pool, &task3);
+
+ ATF_CHECK(ISCAPI_TASK_VALID(task1));
+ ATF_CHECK(ISCAPI_TASK_VALID(task2));
+ ATF_CHECK(ISCAPI_TASK_VALID(task3));
+
+ ATF_CHECK(isc_task_privilege(task1));
+ ATF_CHECK(isc_task_privilege(task2));
+ ATF_CHECK(isc_task_privilege(task3));
+
+ isc_taskpool_setprivilege(pool, ISC_FALSE);
+
+ ATF_CHECK(!isc_task_privilege(task1));
+ ATF_CHECK(!isc_task_privilege(task2));
+ ATF_CHECK(!isc_task_privilege(task3));
+
+ isc_task_destroy(&task1);
+ isc_task_destroy(&task2);
+ isc_task_destroy(&task3);
+
+ isc_taskpool_destroy(&pool);
+ ATF_REQUIRE_EQ(pool, NULL);
+
+ isc_test_end();
+}
+
/*
* Main
*/
ATF_TP_ADD_TC(tp, create_pool);
ATF_TP_ADD_TC(tp, expand_pool);
ATF_TP_ADD_TC(tp, get_tasks);
+ ATF_TP_ADD_TC(tp, set_privilege);
return (atf_no_error());
}
isc__task_getname
isc__task_gettag
isc__task_onshutdown
+isc__task_privilege
isc__task_purge
isc__task_purgeevent
isc__task_purgerange
isc__task_send
isc__task_sendanddetach
isc__task_setname
+isc__task_setprivilege
isc__task_shutdown
isc__task_unsend
isc__task_unsendrange
isc__taskmgr_create
isc__taskmgr_destroy
+isc__taskmgr_mode
+isc__taskmgr_setmode
isc__timer_attach
isc__timer_create
isc__timer_detach
isc_taskpool_destroy
isc_taskpool_expand
isc_taskpool_gettask
+isc_taskpool_setprivilege
isc_taskpool_size
isc_thread_create
isc_thread_join
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
# PERFORMANCE OF THIS SOFTWARE.
-# $Id: rules.in,v 1.70 2011/03/01 23:48:07 tbox Exp $
+# $Id: rules.in,v 1.71 2011/09/02 21:15:39 each Exp $
###
### Common Makefile rules for BIND 9.
install:: all
install clean distclean maintainer-clean doc docclean man manclean::
- @for i in ${ALL_SUBDIRS}; do \
+ @for i in ${ALL_SUBDIRS} ${ALL_TESTDIRS}; do \
if [ "$$i" != "nulldir" -a -d $$i ]; then \
echo "making $@ in `pwd`/$$i"; \
(cd $$i; ${MAKE} ${MAKEDEFS} DESTDIR="${DESTDIR}" $@) || exit 1; \