OpenDNSSEC-enforcer 2.1.13
queue_cmd.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2014 NLNet Labs
3 * Copyright (c) 2014 OpenDNSSEC AB (svb)
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 *
27 */
28
29#include "config.h"
30
31#include <pthread.h>
32#include <time.h>
33
34#include "file.h"
35#include "log.h"
36#include "str.h"
37#include "duration.h"
38#include "scheduler/schedule.h"
39#include "cmdhandler.h"
41#include "daemon/engine.h"
42#include "clientpipe.h"
43#include "longgetopt.h"
44
45#include "daemon/queue_cmd.h"
46#include "scheduler/task.h"
47
48static const char *module_str = "queue_cmd";
49
50static void
51usage(int sockfd)
52{
53 client_printf(sockfd,
54 "queue\n"
55 );
56}
57
58static void
59help(int sockfd)
60{
61 client_printf(sockfd,
62 "queue shows all scheduled tasks with their time of earliest executions,\n"
63 "as well as all tasks currently being processed."
64 "\n\n"
65 );
66}
67
68static int
69run(cmdhandler_ctx_type* context, int argc, char* argv[])
70{
71 int sockfd = context->sockfd;
72 struct tm strtime_struct;
73 char strtime[64]; /* at least 26 according to docs plus a long integer */
74 char* taskdescription;
75 int count;
76 time_t now;
77 time_t nextFireTime;
78 ldns_rbnode_t* node = LDNS_RBTREE_NULL;
79 task_type* task = NULL;
80 int num_waiting;
81 engine_type* engine = getglobalcontext(context);
82
83 ods_log_debug("[%s] list tasks command", module_str);
84
85 ods_log_assert(engine);
86 if (!engine->taskq || !engine->taskq->tasks) {
87 client_printf(sockfd, "There are no tasks scheduled.\n");
88 return 0;
89 }
90
91 schedule_info(engine->taskq, &nextFireTime, &num_waiting, &count);
92 if (num_waiting == engine->config->num_worker_threads) {
93 client_printf(sockfd, "All worker threads idle.\n");
94 }
95
96 /* how many tasks */
97 client_printf(sockfd, "There %s %i %s scheduled.\n",
98 (count==1)?"is":"are", (int) count, (count==1)?"task":"tasks");
99 now = time_now();
100 strftime(strtime, sizeof(strtime), "%c", localtime_r(&now, &strtime_struct));
101 client_printf(sockfd, "It is now %s (%ld seconds since epoch)\n", (strtime[0]?strtime:"(null)"), (long)now);
102 if (nextFireTime > now) {
103 strftime(strtime, sizeof(strtime), "%c", localtime_r(&nextFireTime, &strtime_struct));
104 client_printf(sockfd, "Next task scheduled %s (%ld seconds since epoch)\n", strtime, (long)nextFireTime);
105 } else if (nextFireTime >= 0) {
106 client_printf(sockfd, "Next task scheduled immediately\n");
107 } /* else: no tasks scheduled at all. */
108
109 /* list tasks */
110 pthread_mutex_lock(&engine->taskq->schedule_lock);
111 node = ldns_rbtree_first(engine->taskq->tasks);
112 while (node && node != LDNS_RBTREE_NULL) {
113 task = (task_type*) node->data;
114 taskdescription = schedule_describetask(task);
115 client_printf(sockfd, "%s", taskdescription);
116 free(taskdescription);
117 node = ldns_rbtree_next(node);
118 }
119 pthread_mutex_unlock(&engine->taskq->schedule_lock);
120 return 0;
121}
122
123struct cmd_func_block queue_funcblock = {
124 "queue", &usage, &help, NULL, NULL, &run, NULL
125};
126
127static void
128usage_flush(int sockfd)
129{
130 client_printf(sockfd,
131 "flush\n"
132 );
133}
134
135static void
136help_flush(int sockfd)
137{
138 client_printf(sockfd,
139 "Execute all scheduled tasks immediately.\n\n");
140}
141
142static int
143run_flush(cmdhandler_ctx_type* context, int argc, char* argv[])
144{
145 int sockfd = context->sockfd;
146 engine_type* engine = getglobalcontext(context);
147 ods_log_debug("[%s] flush tasks command", module_str);
148 ods_log_assert(engine);
149 ods_log_assert(engine->taskq);
150
151 schedule_flush(engine->taskq);
152
153 client_printf(sockfd, "All tasks scheduled immediately.\n");
154 ods_log_verbose("[cmdhandler] all tasks scheduled immediately");
155 return 0;
156}
157
158struct cmd_func_block flush_funcblock = {
159 "flush", &usage_flush, &help_flush, NULL, NULL, &run_flush, NULL
160};
engine_type * getglobalcontext(cmdhandler_ctx_type *context)
struct cmd_func_block queue_funcblock
Definition queue_cmd.c:123
struct cmd_func_block flush_funcblock
Definition queue_cmd.c:158
schedule_type * taskq
Definition engine.h:60
engineconfig_type * config
Definition engine.h:48
int num_worker_threads
Definition cfg.h:73