OpenDNSSEC-enforcer  2.1.9
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 "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 run(int sockfd, cmdhandler_ctx_type* context, const char *cmd)
63 {
64  const int NARGV = MAX_ARGS;
65  const char *argv[MAX_ARGS];
66  char buf[ODS_SE_MAXLINE];
67  int argc;
68  long val;
69  char *endptr, *errorstr;
70 
71  strncpy(buf, cmd, sizeof(buf));
72  buf[sizeof(buf)-1] = '\0';
73  argc = ods_str_explode(buf, NARGV, argv);
74 
75  ods_log_debug("[%s] verbosity command", module_str);
76  if (argc == 1) {
77  client_printf(sockfd, "Current verbosity is set to %d.\n",
78  ods_log_verbosity());
79  client_printf(sockfd,
80  "Available modes:\n"
81  " 0 - Critical\n"
82  " 1 - Error\n"
83  " 2 - Warning\n"
84  " 3 - Notice\n"
85  " 4 - Info\n"
86  " 5 - Debug\n"
87  );
88  return 0;
89  } else if (argc == 2) {
90  errno = 0;
91  val = strtol(argv[1], &endptr, 10);
92  if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
93  || (errno != 0 && val == 0)) {
94  errorstr = strerror(errno);
95  client_printf(sockfd, "Error parsing verbosity value: %s.\n", errorstr);
96  return -1;
97  }
98  if (endptr == argv[1]) {
99  client_printf(sockfd, "Error parsing verbosity value: No digits were found.\n");
100  return -1;
101  }
102  if ((int)val < 0) { /* also catches wrapped longs */
103  client_printf(sockfd, "Error parsing verbosity value: must be >= 0.\n");
104  return -1;
105  }
106  ods_log_setverbosity(val);
107  client_printf(sockfd, "Verbosity level set to %li.\n", val);
108  return 0;
109  } else {
110  client_printf(sockfd, "Too many arguments.\n");
111  return -1;
112  }
113 }
114 
115 
116 struct cmd_func_block verbosity_funcblock = {
117  "verbosity", &usage, &help, NULL, &run
118 };
#define NARGV
#define MAX_ARGS
Definition: verbosity_cmd.c:42
struct cmd_func_block verbosity_funcblock