OpenDNSSEC-enforcer 2.1.13
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#include "longgetopt.h"
40
42
43#define MAX_ARGS 2
44
45static const char *module_str = "verbosity_cmd";
46
47static void
48usage(int sockfd)
49{
50 client_printf(sockfd,
51 "verbosity <nr>\n"
52 );
53}
54
55static void
56help(int sockfd)
57{
58 client_printf(sockfd, "Set verbosity.\n\n"
59 );
60}
61
62static int
63run(cmdhandler_ctx_type* context, int argc, char* argv[])
64{
65 int sockfd = context->sockfd;
66 long val;
67 char *endptr, *errorstr;
68
69 ods_log_debug("[%s] verbosity command", module_str);
70 if (argc == 1) {
71 client_printf(sockfd, "Current verbosity is set to %d.\n",
72 ods_log_verbosity());
73 client_printf(sockfd,
74 "Available modes:\n"
75 " 0 - Critical\n"
76 " 1 - Error\n"
77 " 2 - Warning\n"
78 " 3 - Notice\n"
79 " 4 - Info\n"
80 " 5 - Debug\n"
81 );
82 return 0;
83 } else if (argc == 2) {
84 errno = 0;
85 val = strtol(argv[1], &endptr, 10);
86 if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN))
87 || (errno != 0 && val == 0)) {
88 errorstr = strerror(errno);
89 client_printf(sockfd, "Error parsing verbosity value: %s.\n", errorstr);
90 return -1;
91 }
92 if (endptr == argv[1]) {
93 client_printf(sockfd, "Error parsing verbosity value: No digits were found.\n");
94 return -1;
95 }
96 if ((int)val < 0) { /* also catches wrapped longs */
97 client_printf(sockfd, "Error parsing verbosity value: must be >= 0.\n");
98 return -1;
99 }
100 ods_log_setverbosity(val);
101 client_printf(sockfd, "Verbosity level set to %li.\n", val);
102 return 0;
103 } else {
104 client_printf(sockfd, "Too many arguments.\n");
105 return -1;
106 }
107}
108
109
110struct cmd_func_block verbosity_funcblock = {
111 "verbosity", &usage, &help, NULL, NULL, &run, NULL
112};
struct cmd_func_block verbosity_funcblock