main.c 8.5 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
44
#include <dns/result.h>

#include <dst/result.h>

#define NS_MAIN 1

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

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

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

	va_start(args, format);
58
59
60
61
	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
62
63
64
		isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL,
			       NS_LOGMODULE_MAIN, ISC_LOG_CRITICAL,
			       "exiting (due to early fatal error)");
65
	} else {
66
		fprintf(stderr, "%s: ", program_name);
67
68
		vfprintf(stderr, format, args);
		fprintf(stderr, "\n");
69
		fflush(stderr);
70
	}
Bob Halley's avatar
add    
Bob Halley committed
71
72
73
74
75
	va_end(args);

	exit(1);
}

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

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

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

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

	if (argc > 1) {
		usage();
Bob Halley's avatar
Bob Halley committed
181
		ns_main_earlyfatal("extra command line arguments");
Bob Halley's avatar
add    
Bob Halley committed
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
	}
}

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);
	}

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

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

	return (ISC_R_SUCCESS);
}

static void
destroy_managers(void) {
	/*
	 * The interface manager owns tasks, so we have to destroy it before
	 * we destroy the task manager.
	 */
	ns_interfacemgr_destroy(&ns_g_interfacemgr);
	/*
	 * 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);
	ns_clientmgr_destroy(&ns_g_clientmgr);
}

static void
setup() {
	isc_result_t result;

255
256
	result = ns_log_init();
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
257
258
		ns_main_earlyfatal("ns_log_init() failed: %s",
				   isc_result_totext(result));
259
260

	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
Bob Halley's avatar
Bob Halley committed
261
		      ISC_LOG_NOTICE, "starting BIND %s", ns_g_version);
Bob Halley's avatar
add    
Bob Halley committed
262

263
	ISC_LIST_INIT(ns_g_viewlist);
Bob Halley's avatar
add    
Bob Halley committed
264
265
	result = isc_rwlock_init(&ns_g_viewlock, 0, 0);
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
266
267
		ns_main_earlyfatal("isc_rwlock_init() failed: %s",
				   isc_result_totext(result));
Bob Halley's avatar
add    
Bob Halley committed
268
269
270

	result = create_managers();
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
271
272
		ns_main_earlyfatal("create_managers() failed: %s",
				   isc_result_totext(result));
Bob Halley's avatar
add    
Bob Halley committed
273
274
275

	result = ns_server_init();
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
276
277
		ns_main_earlyfatal("ns_server_init() failed: %s",
				   isc_result_totext(result));
Bob Halley's avatar
add    
Bob Halley committed
278
279
280
281
282
}

static void
cleanup() {
	destroy_managers();
283
	dns_tsig_destroy();
Brian Wellington's avatar
Brian Wellington committed
284
	dns_tkey_destroy();
Bob Halley's avatar
add    
Bob Halley committed
285
	isc_rwlock_destroy(&ns_g_viewlock);
286
	isc_log_write(ns_g_lctx, NS_LOGCATEGORY_GENERAL, NS_LOGMODULE_MAIN,
Bob Halley's avatar
Bob Halley committed
287
		      ISC_LOG_NOTICE, "exiting");
288
	ns_log_shutdown();
Bob Halley's avatar
add    
Bob Halley committed
289
290
291
292
293
294
}

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

295
	program_name = argv[0];
296
297
298
	isc_assertion_setcallback(assertion_failed);
	isc_error_setfatal(library_fatal_error);

Bob Halley's avatar
Bob Halley committed
299
300
301
302
303
	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
304
305
	result = isc_app_start();
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
306
307
		ns_main_earlyfatal("isc_app_start() failed: %s",
				   isc_result_totext(result));
Bob Halley's avatar
add    
Bob Halley committed
308
309
310

	result = isc_mem_create(0, 0, &ns_g_mctx);
	if (result != ISC_R_SUCCESS)
Bob Halley's avatar
Bob Halley committed
311
312
		ns_main_earlyfatal("isc_mem_create() failed: %s",
				   isc_result_totext(result));
Bob Halley's avatar
add    
Bob Halley committed
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336

	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
337
338
	ns_os_shutdown();

Bob Halley's avatar
add    
Bob Halley committed
339
340
	return (0);
}