OpenDNSSEC-enforcer  2.0.4
verbosity_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 <limits.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/verbosity_cmd.h"
41 
42 #define MAX_ARGS 2
43 
44 static const char *module_str = "verbosity_cmd";
45 
46 static void
47 usage(int sockfd)
48 {
49  client_printf(sockfd,
50  "verbosity <nr>\n"
51  );
52 }
53 
54 static void
55 help(int sockfd)
56 {
57  client_printf(sockfd, "Set verbosity.\n\n"
58  );
59 }
60 
61 static int
62 handles(const char *cmd, ssize_t n)
63 {
64  return ods_check_command(cmd, n, verbosity_funcblock()->cmdname)?1:0;
65 }
66 
67 static int
68 run(int sockfd, engine_type* engine, const char *cmd, ssize_t n,
69  db_connection_t *dbconn)
70 {
71  const int NARGV = MAX_ARGS;
72  const char *argv[MAX_ARGS];
73  char buf[ODS_SE_MAXLINE];
74  int argc;
75  long val;
76  char *endptr, *errorstr;
77  (void)n; (void)dbconn;
78 
79  strncpy(buf, cmd, sizeof(buf));
80  buf[sizeof(buf)-1] = '\0';
81  argc = ods_str_explode(buf, NARGV, argv);
82 
83  ods_log_debug("[%s] verbosity command", module_str);
84  if (argc == 1) {
85  client_printf(sockfd, "Current verbosity is set to %d.\n",
86  ods_log_verbosity());
87  client_printf(sockfd,
88  "Available modes:\n"
89  " 0 - Critical\n"
90  " 1 - Error\n"
91  " 2 - Warning\n"
92  " 3 - Notice\n"
93  " 4 - Info\n"
94  " 5 - Debug\n"
95  );
96  return 0;
97  } else if (argc == 2) {
98  errno = 0;
99  val = strtol(argv[1], &endptr, 10);
100  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
101  || (errno != 0 && val == 0)) {
102  errorstr = strerror(errno);
103  client_printf(sockfd, "Error parsing verbosity value: %s.\n", errorstr);
104  return -1;
105  }
106  if (endptr == argv[1]) {
107  client_printf(sockfd, "Error parsing verbosity value: No digits were found.\n");
108  return -1;
109  }
110  if ((int)val < 0) { /* also catches wrapped longs */
111  client_printf(sockfd, "Error parsing verbosity value: must be >= 0.\n");
112  return -1;
113  }
114  ods_log_assert(engine);
115  ods_log_assert(engine->config);
116  ods_log_init("ods-enforcerd", engine->config->use_syslog, engine->config->log_filename, val);
117  client_printf(sockfd, "Verbosity level set to %i.\n", val);
118  return 0;
119  } else {
120  client_printf(sockfd, "Too many arguments.\n");
121  return -1;
122  }
123 }
124 
125 
126 static struct cmd_func_block funcblock = {
127  "verbosity", &usage, &help, &handles, &run
128 };
129 
130 struct cmd_func_block*
132 {
133  return &funcblock;
134 }
#define NARGV
void(* help)(int sockfd)
Definition: cmdhandler.h:64
void ods_log_debug(const char *format,...)
Definition: log.c:41
struct cmd_func_block * verbosity_funcblock(void)
int(* run)(int sockfd, struct engine_struct *engine, const char *cmd, ssize_t n, db_connection_t *dbconn)
Definition: cmdhandler.h:79
const char * cmdname
Definition: cmdhandler.h:59
void(* usage)(int sockfd)
Definition: cmdhandler.h:61
const char * log_filename
Definition: cfg.h:59
engineconfig_type * config
Definition: engine.h:53
#define MAX_ARGS
Definition: verbosity_cmd.c:42
int use_syslog
Definition: cfg.h:72
int(* handles)(const char *cmd, ssize_t n)
Definition: cmdhandler.h:67