OpenDNSSEC-enforcer 2.1.13
ctrl_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
33#include "file.h"
34#include "log.h"
35#include "str.h"
36#include "cmdhandler.h"
38#include "daemon/engine.h"
39#include "clientpipe.h"
40#include "longgetopt.h"
41
42#include "daemon/ctrl_cmd.h"
43
44static void
45usage(int sockfd)
46{
47 client_printf(sockfd,
48 "start \n"
49 "running\n"
50 "reload \n"
51 "stop \n"
52 );
53}
54
55static void
56help(int sockfd)
57{
58 client_printf(sockfd,
59 "start Starts the engine and the process. \n"
60 "running Returns acknowledgment that the engine is running.\n"
61 "reload Reload the engine.\n"
62 "stop Stop the engine and terminate the process.\n\n"
63 );
64}
65
66static int
67handles(const char *cmd)
68{
69 if (ods_check_command(cmd, "stop")) return 1;
70 if (ods_check_command(cmd, "reload")) return 1;
71 if (ods_check_command(cmd, "running")) return 1;
72 if (ods_check_command(cmd, "start")) return 1;
73 return 0;
74}
75
76
77static int
78run(cmdhandler_ctx_type* context, int argc, char* argv[])
79{
80 int sockfd = context->sockfd;
81 engine_type* engine = getglobalcontext(context);
82 if (ods_check_command(argv[0], "start")) {
83 ods_log_debug("[cmdhandler] start command");
84 client_printf(sockfd, "Engine already running.\n");
85 /* if you asked us to start, we are already started */
86 return 1; /* error */
87 } else if (ods_check_command(argv[0], "running")) {
88 ods_log_debug("[cmdhandler] running command");
89 client_printf(sockfd, "Engine running.\n");
90 return 0;
91 } else if (ods_check_command(argv[0], "reload")) {
92 ods_log_debug("[cmdhandler] reload command");
93 ods_log_assert(engine);
94 engine->need_to_reload = 1;
95 pthread_mutex_lock(&engine->signal_lock);
96 pthread_cond_signal(&engine->signal_cond);
97 pthread_mutex_unlock(&engine->signal_lock);
98 client_printf(sockfd, "Reloading engine.\n");
99 return 0;
100 } else if (ods_check_command(argv[0], "stop")) {
101 ods_log_debug("[cmdhandler] stop command");
102 ods_log_assert(engine);
103 engine->need_to_exit = 1;
104 pthread_mutex_lock(&engine->signal_lock);
105 pthread_cond_signal(&engine->signal_cond);
106 pthread_mutex_unlock(&engine->signal_lock);
107 client_printf(sockfd, "%s\n", ODS_SE_STOP_RESPONSE);
108 return 0;
109 } else {
110 return -1;
111 }
112}
113
114struct cmd_func_block ctrl_funcblock = {
115 "ctrl", &usage, &help, &handles, NULL, &run, NULL
116};
struct cmd_func_block ctrl_funcblock
Definition ctrl_cmd.c:114
engine_type * getglobalcontext(cmdhandler_ctx_type *context)
pthread_mutex_t signal_lock
Definition engine.h:65
pthread_cond_t signal_cond
Definition engine.h:64
int need_to_reload
Definition engine.h:56
int need_to_exit
Definition engine.h:55