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