Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop' into mandalg
Browse files Browse the repository at this point in the history
  • Loading branch information
Hoda Rohani committed Aug 2, 2016
2 parents 8f8ac26 + a057451 commit 2bd47e3
Show file tree
Hide file tree
Showing 52 changed files with 1,078 additions and 1,174 deletions.
13 changes: 11 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
* In the kasp file, KSK/ZSK section, the algorithm length must be set now.
UNRELEASED:

* Enforce and signconf tasks are now scheduled individually per zone. Resign
per policy.
* zone delete removes tasks associated with zone from queue.
* In the kasp file, KSK/ZSK section, the algorithm length must be set now.

OpenDNSSEC 2.0.1 - 2016-07-21

* Fixed crash and linking issue in ods-migrate.
* Fixed case where 2.0.0 could not read backup files from 1.4.10.
* Fixed bug in migration script where key state wasn't transformed properly.

OpenDNSSEC 2.0.0-1

* include db creation scripts in dist tarball needed for migration from 1.4.

OpenDNSSEC 2.0.0
OpenDNSSEC 2.0.0 - 2016-07-07

* OpenDNSSEC-99: Skip "are you sure" messages. Add --force and -f flag to
ods-enforcer-db-setup and hsmutil purge
Expand Down
2 changes: 0 additions & 2 deletions enforcer/src/daemon/engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ engine_alloc(void)
if (!engine) return NULL;

pthread_mutex_init(&engine->signal_lock, NULL);
pthread_mutex_init(&engine->enforce_lock, NULL);
pthread_cond_init(&engine->signal_cond, NULL);

engine->dbcfg_list = NULL;
Expand All @@ -96,7 +95,6 @@ void
engine_dealloc(engine_type* engine)
{
schedule_cleanup(engine->taskq);
pthread_mutex_destroy(&engine->enforce_lock);
pthread_mutex_destroy(&engine->signal_lock);
pthread_cond_destroy(&engine->signal_cond);
if (engine->dbcfg_list) {
Expand Down
2 changes: 0 additions & 2 deletions enforcer/src/daemon/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,6 @@ struct engine_struct {
/* Main thread blocks on this condition when there is nothing to do */
pthread_cond_t signal_cond;
pthread_mutex_t signal_lock;
/* To prevent having 2 enforce tasks running simultaneously. */
pthread_mutex_t enforce_lock;

db_configuration_list_t* dbcfg_list;
};
Expand Down
58 changes: 38 additions & 20 deletions enforcer/src/daemon/queue_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,30 @@ handles(const char *cmd, ssize_t n)
return ods_check_command(cmd, n, queue_funcblock()->cmdname)?1:0;
}

/**
* Convert task to string.
* buf must be at least ODS_SE_MAXLINE long.
*/
static void
task2str(task_t* task, char* buf, time_t now)
{
char ctimebuf[32]; /* at least 26 according to docs */
time_t time;
char* strtime = NULL;
char* strtask = NULL;

assert(task);

time = (task->due_date < now)?now:task->due_date;
strtime = ctime_r(&time, ctimebuf);
/* We need cut off the newline */
if (strtime && strlen(strtime) > 0) {
strtime[strlen(strtime)-1] = 0;
}
(void)snprintf(buf, ODS_SE_MAXLINE, "On %s I will [%s] %s\n",
strtime?strtime:"(null)", task->type, task->owner);
}

static int
run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
db_connection_t *dbconn)
Expand All @@ -80,8 +104,9 @@ run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
time_t now;
time_t nextFireTime;
ldns_rbnode_t* node = LDNS_RBTREE_NULL;
task_type* task = NULL;
task_t* task = NULL;
(void)cmd; (void)n; (void)dbconn;
int num_waiting;

ods_log_debug("[%s] list tasks command", module_str);

Expand All @@ -91,17 +116,10 @@ run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
return 0;
}

/* current work */
pthread_mutex_lock(&engine->taskq->schedule_lock);
for (i=0; i < (size_t) engine->config->num_worker_threads; i++) {
task = engine->workers[i]->task;
if (task) {
/* TODO: even holding that lock, this is not safe! */
client_printf(sockfd, "Working with [%s] %s\n",
task_what2str(task->what), task_who2str(task->who));
}
}
pthread_mutex_unlock(&engine->taskq->schedule_lock);
num_waiting = schedule_get_num_waiting(engine->taskq);
if (num_waiting == engine->config->num_worker_threads) {
client_printf(sockfd, "All worker threads idle.\n");
}

/* how many tasks */
count = schedule_taskcount(engine->taskq);
Expand All @@ -111,20 +129,20 @@ run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
strftime(strtime, sizeof(strtime), "%c", localtime_r(&now, &strtime_struct));
client_printf(sockfd, "It is now %s (%ld seconds since epoch)\n", (strtime[0]?strtime:"(null)"), (long)now);
nextFireTime = schedule_time_first(engine->taskq);
if (nextFireTime > 0) {
strftime(strtime, sizeof(strtime), "%c", localtime_r(&nextFireTime, &strtime_struct));
client_printf(sockfd, "Next task scheduled %s (%ld seconds since epoch)\n", strtime, (long)nextFireTime);
} else if (nextFireTime == 0) {
client_printf(sockfd, "Next task scheduled immediately\n");
}
if (nextFireTime > now) {
strftime(strtime, sizeof(strtime), "%c", localtime_r(&nextFireTime, &strtime_struct));
client_printf(sockfd, "Next task scheduled %s (%ld seconds since epoch)\n", strtime, (long)nextFireTime);
} else if (nextFireTime >= 0) {
client_printf(sockfd, "Next task scheduled immediately\n");
} /* else: no tasks scheduled at all. */

/* list tasks */
pthread_mutex_lock(&engine->taskq->schedule_lock);
node = ldns_rbtree_first(engine->taskq->tasks);
while (node && node != LDNS_RBTREE_NULL) {
task = (task_type*) node->data;
task = (task_t*) node->data;
memset(buf, 0, ODS_SE_MAXLINE);
(void)task2str(task, buf);
task2str(task, buf, now);
client_printf(sockfd, "%s", buf);
node = ldns_rbtree_next(node);
}
Expand Down
29 changes: 14 additions & 15 deletions enforcer/src/daemon/time_leap_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
const int NARGV = MAX_ARGS;
const char *argv[MAX_ARGS];
int argc, attach, cont;
task_type* task = NULL, *newtask;
(void)n; (void)dbconn;
task_t* task = NULL;
(void)n;

ods_log_debug("[%s] %s command", module_str, time_leap_funcblock()->cmdname);

Expand Down Expand Up @@ -136,12 +136,11 @@ run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
(int) schedule_taskcount(engine->taskq), strtime, (long)now);
cont = 1;
while (cont) {
if (! time)
if (!time)
time_leap = schedule_time_first(engine->taskq);
if (time_leap < 0) break;
if (now > time_leap) {
time_leap = now;
}

assert(time_leap >= 0); /* These tasks should not have end up
in the queue!*/

set_time_now(time_leap);
strftime(strtime, sizeof(strtime), "%c", localtime_r(&time_leap, &strtime_struct));
Expand All @@ -157,17 +156,17 @@ run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
break;
if (!(task = schedule_pop_first_task(engine->taskq)))
break;
client_printf(sockfd, "[timeleap] attaching to job %s\n", task_what2str(task->what));
if (strcmp(task_what2str(task->what), "enforce") == 0)
client_printf(sockfd, "[timeleap] attaching to job %s\n", task->type);
if (strcmp(task->type, TASK_TYPE_ENFORCE) == 0)
cont = 0;
task->dbconn = dbconn;
newtask = task_perform(task);
task->due_date = task_execute(task, dbconn);
ods_log_debug("[timeleap] finished working");
if (newtask) {
newtask->dbconn = NULL;
(void) schedule_task(engine->taskq, newtask); /* TODO unchecked error code */
if (task->due_date >= 0) {
(void) schedule_task(engine->taskq, task); /* TODO unchecked error code */
} else {
task_deepfree(task);
}
hsm_key_factory_generate_all(engine, dbconn, 0);
//~ hsm_key_factory_generate_all(engine, dbconn, 0); /* should be scheduled already */
}
return 0;
}
Expand Down
66 changes: 19 additions & 47 deletions enforcer/src/daemon/worker.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include "log.h"
#include "status.h"
#include "util.h"
#include "duration.h"
//~ #include "duration.h"

/**
* Create worker.
Expand All @@ -55,42 +55,11 @@ worker_create(int num)
ods_log_debug("create worker[%i]", num +1);
worker->thread_num = num +1;
worker->engine = NULL;
worker->task = NULL;
worker->need_to_exit = 0;
worker->jobs_appointed = 0;
worker->jobs_completed = 0;
worker->jobs_failed = 0;
worker->sleeping = 0;
worker->waiting = 0;
worker->dbconn = NULL;
return worker;
}

/**
* Perform task.
*
*/
static void
worker_perform_task(worker_type* worker)
{
task_type* task;

if (!worker || !worker->task || !worker->task->context || !worker->engine) {
return;
}

task = (task_type*) worker->task;
ods_log_debug("[worker[%i]]: perform task [%s] for %s",
worker->thread_num, task_what2str(task->what),
task_who2str(task->who));

/* We temporarily assign the database connection to the task so
* it is accessable from the task function */
task->dbconn = worker->dbconn;
worker->task = task_perform(task);
if (worker->task) task->dbconn = NULL;
}

/**
* Work.
*
Expand All @@ -99,26 +68,29 @@ void
worker_start(worker_type* worker)
{
ods_log_assert(worker);
task_t *task;
time_t time;

while (worker->need_to_exit == 0) {
ods_log_debug("[worker[%i]]: report for duty", worker->thread_num);

/* When no task available this call blocks and waits for event.
* Then it will return NULL; */
worker->task = schedule_pop_task(worker->engine->taskq);
if (worker->task) {
ods_log_debug("[worker[%i]] start working", worker->thread_num);
worker_perform_task(worker);
ods_log_debug("[worker[%i]] finished working", worker->thread_num);
if (worker->task) {
if (schedule_task(worker->engine->taskq, worker->task) !=
ODS_STATUS_OK)
{
ods_log_error("[worker[%i]] unable to schedule task",
worker->thread_num);
}
worker->task = NULL;
}
task = schedule_pop_task(worker->engine->taskq);
if (!task) continue;

ods_log_debug("[worker[%i]] start working", worker->thread_num);
time = task_execute(task, worker->dbconn);
ods_log_debug("[worker[%i]] finished working", worker->thread_num);

if (time < 0) {
task_deepfree(task);
continue;
}
task->due_date = time;
if (schedule_task(worker->engine->taskq, task) != ODS_STATUS_OK) {
ods_log_error("[worker[%i]] unable to schedule task",
worker->thread_num);
}
}
}
Expand All @@ -130,6 +102,6 @@ worker_start(worker_type* worker)
void
worker_cleanup(worker_type* worker)
{
if (!worker) return;
//What about its db connection?
free(worker);
}
8 changes: 1 addition & 7 deletions enforcer/src/daemon/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#include "scheduler/task.h"
#include "db/db_connection.h"

#include <time.h>
//~ #include <time.h>

struct engine_struct;

Expand All @@ -44,12 +44,6 @@ struct worker_struct {
int thread_num;
pthread_t thread_id;
struct engine_struct* engine;
task_type* task;
size_t jobs_appointed;
size_t jobs_completed;
size_t jobs_failed;
int sleeping;
int waiting;
int need_to_exit;
db_connection_t* dbconn;
};
Expand Down
18 changes: 18 additions & 0 deletions enforcer/src/db/zone_db_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@

#include <string.h>

char *
zone_db_ext_zonename_from_id(const db_connection_t* connection,
const db_value_t* id)
{
zone_db_t *zone;
char *zonename = NULL;

if (!connection || !id) {
return NULL;
}

if ((zone = zone_db_new(connection)) && !zone_db_get_by_id(zone, id)) {
zonename = strdup(zone_db_name(zone));
}
zone_db_free(zone);
return zonename;
}

key_data_list_t* zone_db_get_keys(const zone_db_t* zone) {
if (!zone) {
return NULL;
Expand Down
8 changes: 8 additions & 0 deletions enforcer/src/db/zone_db_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@

#include <libxml/tree.h>

/**
* Convert zone ID to name, caller must free resulting string.
* return NULL on error
*/
char *
zone_db_ext_zonename_from_id(const db_connection_t* connection,
const db_value_t* id);

/**
* Get a list of keys for an enforcer zone object.
* \param[in] zone an zone_db_t pointer.
Expand Down
14 changes: 9 additions & 5 deletions enforcer/src/enforcer/autostart_cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "daemon/engine.h"
#include "enforcer/enforce_task.h"
#include "policy/policy_resalt_task.h"
#include "duration.h"
#include "status.h"
#include "log.h"
#include "hsmkey/hsm_key_factory.h"
Expand All @@ -44,17 +45,20 @@ void
autostart(engine_type* engine)
{
ods_status status;
db_connection_t* dbconn;

ods_log_debug("[%s] autostart", module_str);
dbconn = get_database_connection(engine->dbcfg_list);

schedule_purge(engine->taskq); /* Remove old tasks in queue */

hsm_key_factory_schedule_generate_all(engine, 0);
status = schedule_task(engine->taskq, policy_resalt_task(engine));
status = flush_resalt_task_all(engine, dbconn);

if (status != ODS_STATUS_OK)
ods_log_crit("[%s] failed to create resalt task", module_str);
status = schedule_task(engine->taskq, enforce_task(engine, 1));
if (status != ODS_STATUS_OK)
ods_fatal_exit("[%s] failed to create enforce task", module_str);

enforce_task_flush_all(engine, dbconn);
db_connection_free(dbconn);

}
Loading

0 comments on commit 2bd47e3

Please sign in to comment.