main.c 3.97 KB
Newer Older
1
/*  Copyright (C) 2016 CZ.NIC, z.s.p.o. <knot-dns@labs.nic.cz>
2 3 4 5 6 7 8 9 10 11 12 13 14

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
15
*/
16

Marek Vavrusa's avatar
Marek Vavrusa committed
17
#include <getopt.h>
18
#include <stdio.h>
Marek Vavrusa's avatar
Marek Vavrusa committed
19

20
#include "contrib/strtonum.h"
21
#include "knot/common/log.h"
22
#include "utils/common/params.h"
23 24 25
#include "utils/knotc/commands.h"
#include "utils/knotc/interactive.h"
#include "utils/knotc/process.h"
26

27 28
#define PROGRAM_NAME		"knotc"
#define SPACE			"                  "
29 30
#define DEFAULT_CTL_TIMEOUT	5

31
static void print_help(void)
32
{
33
	printf("Usage: %s [parameters] <action> [action_args]\n"
34 35
	       "\n"
	       "Parameters:\n"
36 37 38 39 40 41 42 43 44 45 46 47
	       " -c, --config <file>"SPACE"Use a textual configuration file.\n"
	       "                    "SPACE" (default %s)\n"
	       " -C, --confdb <dir> "SPACE"Use a binary configuration database directory.\n"
	       "                    "SPACE" (default %s)\n"
	       " -s, --socket <path>"SPACE"Use a control UNIX socket path.\n"
	       "                    "SPACE" (default %s)\n"
	       " -t, --timeout <sec>"SPACE"Use a control socket timeout in seconds.\n"
	       "                    "SPACE" (default %u seconds)\n"
	       " -f, --force        "SPACE"Forced operation. Overrides some checks.\n"
	       " -v, --verbose      "SPACE"Enable debug output.\n"
	       " -h, --help         "SPACE"Print the program help.\n"
	       " -V, --version      "SPACE"Print the program version.\n",
48 49
	       PROGRAM_NAME, CONF_DEFAULT_FILE, CONF_DEFAULT_DBDIR,
	       RUN_DIR "/knot.sock", DEFAULT_CTL_TIMEOUT);
50

51
	print_commands();
52 53
}

54 55 56 57
params_t params = {
	.flags = CMD_FNONE,
	.timeout = DEFAULT_CTL_TIMEOUT * 1000
};
58

Marek Vavrusa's avatar
Marek Vavrusa committed
59 60
int main(int argc, char **argv)
{
Marek Vavrusa's avatar
Marek Vavrusa committed
61 62
	/* Long options. */
	struct option opts[] = {
63 64 65
		{ "config",  required_argument, NULL, 'c' },
		{ "confdb",  required_argument, NULL, 'C' },
		{ "socket",  required_argument, NULL, 's' },
66
		{ "timeout", required_argument, NULL, 't' },
67 68 69 70
		{ "force",   no_argument,       NULL, 'f' },
		{ "verbose", no_argument,       NULL, 'v' },
		{ "help",    no_argument,       NULL, 'h' },
		{ "version", no_argument,       NULL, 'V' },
71
		{ NULL }
Marek Vavrusa's avatar
Marek Vavrusa committed
72
	};
73

74 75
	/* Parse command line arguments */
	int opt = 0, li = 0;
76
	while ((opt = getopt_long(argc, argv, "c:C:s:t:fvhV", opts, &li)) != -1) {
77
		switch (opt) {
78
		case 'c':
79
			params.config = optarg;
80
			break;
Daniel Salzman's avatar
Daniel Salzman committed
81
		case 'C':
82
			params.confdb = optarg;
Daniel Salzman's avatar
Daniel Salzman committed
83
			break;
84
		case 's':
85
			params.socket = optarg;
86
			break;
87
		case 't':
88
			if (str_to_int(optarg, &params.timeout) != KNOT_EOK) {
89 90 91
				print_help();
				return EXIT_FAILURE;
			}
92 93
			/* Convert to milliseconds. */
			params.timeout *= 1000;
94
			break;
95
		case 'f':
96
			params.flags |= CMD_FFORCE;
97 98
			break;
		case 'v':
99
			params.verbose = true;
100
			break;
Marek Vavrusa's avatar
Marek Vavrusa committed
101
		case 'h':
102 103 104 105 106
			print_help();
			return EXIT_SUCCESS;
		case 'V':
			print_version(PROGRAM_NAME);
			return EXIT_SUCCESS;
Marek Vavrusa's avatar
Marek Vavrusa committed
107
		default:
108 109
			print_help();
			return EXIT_FAILURE;
Marek Vavrusa's avatar
Marek Vavrusa committed
110 111 112
		}
	}

113 114 115 116 117 118
	/* Set up simplified logging just to stdout/stderr. */
	log_init();
	log_levels_set(LOGT_STDOUT, LOG_ANY, LOG_MASK(LOG_INFO) | LOG_MASK(LOG_NOTICE));
	log_levels_set(LOGT_STDERR, LOG_ANY, LOG_UPTO(LOG_WARNING));
	log_levels_set(LOGT_SYSLOG, LOG_ANY, 0);
	log_flag_set(LOG_FNO_TIMESTAMP | LOG_FNO_INFO);
119
	if (params.verbose) {
120
		log_levels_add(LOGT_STDOUT, LOG_ANY, LOG_MASK(LOG_DEBUG));
121 122
	}

123 124 125 126 127
	int ret;
	if (argc - optind < 1) {
		ret = interactive_loop(&params);
	} else {
		ret = process_cmd(argc - optind, (const char **)argv + optind, &params);
128
	}
129 130

	log_close();
131

132
	return (ret == KNOT_EOK) ? EXIT_SUCCESS : EXIT_FAILURE;
133
}