main.c 7.37 KB
Newer Older
Bob Halley's avatar
add    
Bob Halley committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * Copyright (C) 1999  Internet Software Consortium.
 * 
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
 * SOFTWARE.
 */

#include <config.h>

#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stddef.h>

#include <isc/app.h>
26
#include <isc/assertions.h>
Bob Halley's avatar
add    
Bob Halley committed
27
28
#include <isc/error.h>
#include <isc/boolean.h>
29
#include <isc/commandline.h>
Bob Halley's avatar
add    
Bob Halley committed
30
31
32
33
#include <isc/task.h>
#include <isc/timer.h>

#include <dns/dbtable.h>
Bob Halley's avatar
Bob Halley committed
34
#include <dns/tsig.h>
Brian Wellington's avatar
Brian Wellington committed
35
#include <dns/tkey.h>
Bob Halley's avatar
add    
Bob Halley committed
36
37
38
39
40
41
42
43
#include <dns/result.h>

#include <dst/result.h>

#define NS_MAIN 1

#include <named/globals.h>
#include <named/interfacemgr.h>
44
#include <named/log.h>
Bob Halley's avatar
Bob Halley committed
45
#include <named/os.h>
Bob Halley's avatar
add    
Bob Halley committed
46
#include <named/server.h>
Bob Halley's avatar
Bob Halley committed
47
#include <named/main.h>
Bob Halley's avatar
add    
Bob Halley committed
48
49

static isc_boolean_t			want_stats = ISC_FALSE;
50
static char *				program_name = "named";
Bob Halley's avatar
add    
Bob Halley committed
51

Bob Halley's avatar
Bob Halley committed
52
53
void
ns_main_earlyfatal(const char *format, ...) {
Bob Halley's avatar
add    
Bob Halley committed
54
55
56
	va_list args;

	va_start(args, format);
57
58
59
60
	if (ns_g_lctx != NULL) {
		isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
			       NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
			       format, args);
Bob Halley's avatar
Bob Halley committed
61
62
63
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
			       NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
			       "exiting (due to early fatal error)");
64
	} else {
65
		fprintf(stderr, "%s: ", program_name);
66
67
		vfprintf(stderr, format, args);
		fprintf(stderr, "\n");
68
		fflush(stderr);
69
	}
Bob Halley's avatar
add    
Bob Halley committed
70
71
72
73
74
	va_end(args);

	exit(1);
}

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
static void
assertion_failed(char *file, int line, isc_assertiontype_t type, char *cond) {
	/*
	 * Handle assertion failures.
	 */

	if (ns_g_lctx != NULL) {
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
			      NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
			      "%s:%d: %s(%s) failed", file, line,
			      isc_assertion_typetotext(type), cond);
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
			       NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
			       "exiting (due assertion failure)");
	} else {
		fprintf(stderr, "%s:%d: %s(%s) failed\n",
			file, line, isc_assertion_typetotext(type), cond);
		fflush(stderr);
	}

	if (ns_g_coreok)
		abort();
	exit(1);
}

static void
library_fatal_error(char *file, int line, char *format, va_list args) {
	/*
	 * Handle isc_error_fatal() calls from our libraries.
	 */

	if (ns_g_lctx != NULL) {
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
			      NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
			      "%s:%d: fatal error", file, line);
		isc_log_vwrite(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
			       NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
			       format, args);
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
			      NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
			      "exiting (due to fatal error in library)");
	} else {
		fprintf(stderr, "%s:%d: fatal error: ", file, line);
		vfprintf(stderr, format, args);
		fprintf(stderr, "\n");
		fflush(stderr);
	}

	if (ns_g_coreok)
		abort();
	exit(1);
}

Bob Halley's avatar
add    
Bob Halley committed
128
129
130
131
132
static void
usage(void) {
	fprintf(stderr,
		"usage: named [[-c cachefile] ...] [[-z zonefile] ...]\n");
	fprintf(stderr,
Bob Halley's avatar
Bob Halley committed
133
		"             [-p port] [-s] [-N number of cpus]\n");
Bob Halley's avatar
add    
Bob Halley committed
134
135
136
137
138
139
}

static void 
parse_command_line(int argc, char *argv[]) {
	int ch;

140
141
	isc_commandline_errprint = ISC_FALSE;
	while ((ch = isc_commandline_parse(argc, argv, "b:c:d:N:p:sx:")) !=
Bob Halley's avatar
Bob Halley committed
142
	       -1) {
Bob Halley's avatar
add    
Bob Halley committed
143
		switch (ch) {
James Brister's avatar
James Brister committed
144
		case 'b':
Bob Halley's avatar
add    
Bob Halley committed
145
		case 'c':
146
			ns_g_conffile = isc_commandline_argument;
Bob Halley's avatar
add    
Bob Halley committed
147
			break;
Bob Halley's avatar
Bob Halley committed
148
149
150
		case 'd':
			ns_g_debuglevel = atoi(isc_commandline_argument);
			break;
Bob Halley's avatar
add    
Bob Halley committed
151
		case 'N':
152
			ns_g_cpus = atoi(isc_commandline_argument);
Bob Halley's avatar
add    
Bob Halley committed
153
154
155
			if (ns_g_cpus == 0)
				ns_g_cpus = 1;
			break;
Bob Halley's avatar
Bob Halley committed
156
		case 'p':
157
			ns_g_port = atoi(isc_commandline_argument);
Bob Halley's avatar
Bob Halley committed
158
			break;
Bob Halley's avatar
add    
Bob Halley committed
159
160
161
162
		case 's':
			/* XXXRTH temporary syntax */
			want_stats = ISC_TRUE;
			break;
163
164
165
		case 'x':
			/* XXXRTH temporary syntax */
			ns_g_cachefile = isc_commandline_argument;
Bob Halley's avatar
add    
Bob Halley committed
166
			break;
167
168
169
		case '?':
			ns_main_earlyfatal("unknown option `-%c'",
					   isc_commandline_option);
Bob Halley's avatar
add    
Bob Halley committed
170
		default:
Bob Halley's avatar
Bob Halley committed
171
			ns_main_earlyfatal("parsing options returned %d", ch);
Bob Halley's avatar
add    
Bob Halley committed
172
173
174
		}
	}

175
176
	argc -= isc_commandline_index;
	argv += isc_commandline_index;
Bob Halley's avatar
add    
Bob Halley committed
177
178
179

	if (argc > 1) {
		usage();
Bob Halley's avatar
Bob Halley committed
180
		ns_main_earlyfatal("extra command line arguments");
Bob Halley's avatar
add    
Bob Halley committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
	}
}

static isc_result_t
create_managers() {
	isc_result_t result;

	result = isc_taskmgr_create(ns_g_mctx, ns_g_cpus, 0, &ns_g_taskmgr);
	if (result != ISC_R_SUCCESS) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "ns_taskmgr_create() failed: %s\n",
				 isc_result_totext(result));
		return (ISC_R_UNEXPECTED);
	}

	result = isc_timermgr_create(ns_g_mctx, &ns_g_timermgr);
	if (result != ISC_R_SUCCESS) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "ns_timermgr_create() failed: %s\n",
				 isc_result_totext(result));
		return (ISC_R_UNEXPECTED);
	}

	result = isc_socketmgr_create(ns_g_mctx, &ns_g_socketmgr);
	if (result != ISC_R_SUCCESS) {
		UNEXPECTED_ERROR(__FILE__, __LINE__,
				 "isc_socketmgr_create() failed: %s\n",
				 isc_result_totext(result));
		return (ISC_R_UNEXPECTED);
	}

	return (ISC_R_SUCCESS);
}

static void
destroy_managers(void) {
	/*
	 * isc_taskmgr_destroy() will  block until all tasks have exited,
	 */
	isc_taskmgr_destroy(&ns_g_taskmgr);
	isc_timermgr_destroy(&ns_g_timermgr);
	isc_socketmgr_destroy(&ns_g_socketmgr);
}

static void
setup() {
	isc_result_t result;

229
230
	result = ns_log_init();
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
231
232
		ns_main_earlyfatal("ns_log_init() failed: %s",
				   isc_result_totext(result));
233
234

	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
Bob Halley's avatar
Bob Halley committed
235
		      ISC_LOG_NOTICE, "starting BIND %s", ns_g_version);
Bob Halley's avatar
add    
Bob Halley committed
236
237
238

	result = create_managers();
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
239
240
		ns_main_earlyfatal("create_managers() failed: %s",
				   isc_result_totext(result));
Bob Halley's avatar
add    
Bob Halley committed
241

242
	ns_server_create(ns_g_mctx, &ns_g_server);
Bob Halley's avatar
add    
Bob Halley committed
243
244
245
246
247
}

static void
cleanup() {
	destroy_managers();
248
	ns_server_destroy(&ns_g_server);
249
	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
Bob Halley's avatar
Bob Halley committed
250
		      ISC_LOG_NOTICE, "exiting");
251
	ns_log_shutdown();
Bob Halley's avatar
add    
Bob Halley committed
252
253
254
255
256
257
}

int
main(int argc, char *argv[]) {
	isc_result_t result;

258
	program_name = argv[0];
259
260
261
	isc_assertion_setcallback(assertion_failed);
	isc_error_setfatal(library_fatal_error);

Bob Halley's avatar
Bob Halley committed
262
263
264
265
266
	result = ns_os_init();
	if (result != ISC_R_SUCCESS)
		ns_main_earlyfatal("ns_os_init() failed: %s",
				   isc_result_totext(result));

Bob Halley's avatar
add    
Bob Halley committed
267
268
	result = isc_app_start();
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
269
270
		ns_main_earlyfatal("isc_app_start() failed: %s",
				   isc_result_totext(result));
Bob Halley's avatar
add    
Bob Halley committed
271
272
273

	result = isc_mem_create(0, 0, &ns_g_mctx);
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
274
275
		ns_main_earlyfatal("isc_mem_create() failed: %s",
				   isc_result_totext(result));
Bob Halley's avatar
add    
Bob Halley committed
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

	dns_result_register();
	dst_result_register();

	parse_command_line(argc, argv);

	setup();

	/*
	 * Start things running and then wait for a shutdown request.
	 */
	result = isc_app_run();
	if (result != ISC_R_SUCCESS)
		UNEXPECTED_ERROR(__FILE__, __LINE__, "isc_app_run(): %s",
				 isc_result_totext(result));

	cleanup();

	if (want_stats)
		isc_mem_stats(ns_g_mctx, stdout);
	isc_mem_destroy(&ns_g_mctx);

	isc_app_finish();

Bob Halley's avatar
Bob Halley committed
300
301
	ns_os_shutdown();

Bob Halley's avatar
add    
Bob Halley committed
302
303
	return (0);
}