aboutsummaryrefslogtreecommitdiffstats
path: root/tests/ircd_util.c
blob: a7c2d1a2d372eb01c58dedc3faa7d5172fbd3c05 (about) (plain) (blame)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
/*
 *  ircd_util.c: Utility functions for making test ircds
 *  Copyright 2017 Simon Arlott
 *
 *  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 2 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, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 *  USA
 */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include "tap/basic.h"

#include "ircd_util.h"

#include "defaults.h"
#include "client.h"
#include "modules.h"

#define MSG "%s:%d (%s)", __FILE__, __LINE__, __FUNCTION__

extern int solanum_main(int argc, const char *argv[]);

static char argv0[BUFSIZE];
static char configfile[BUFSIZE];
static char logfile[BUFSIZE];
static char pidfile[BUFSIZE];

static const char *argv[] = {
	argv0,
	"-configfile", configfile,
	"-logfile", logfile,
	"-pidfile", pidfile,
	"-conftest",
	NULL,
};

void ircd_util_init(const char *name)
{
	rb_strlcpy(argv0, name, sizeof(argv0));
	snprintf(configfile, sizeof(configfile), "%sonf", name);
	snprintf(logfile, sizeof(logfile), "%s.log", name);
	snprintf(pidfile, sizeof(pidfile), "%s.pid", name);
	unlink(logfile);
	unlink(pidfile);

	rb_lib_init(NULL, NULL, NULL, 0, 1024, DNODE_HEAP_SIZE, FD_HEAP_SIZE);
	rb_linebuf_init(LINEBUF_HEAP_SIZE);

	char buf[BUFSIZE];
	ircd_paths[IRCD_PATH_IRCD_EXEC] = argv0;
	ircd_paths[IRCD_PATH_PREFIX] = ".";

	snprintf(buf, sizeof(buf), "runtime/modules");
	ircd_paths[IRCD_PATH_MODULES] = rb_strdup(buf);

	snprintf(buf, sizeof(buf), "runtime/modules/autoload");
	ircd_paths[IRCD_PATH_AUTOLOAD_MODULES] = rb_strdup(buf);

	ircd_paths[IRCD_PATH_ETC] = "runtime";
	ircd_paths[IRCD_PATH_LOG] = "runtime";

	snprintf(buf, sizeof(buf), "runtime/help/users");
	ircd_paths[IRCD_PATH_USERHELP] = rb_strdup(buf);

	snprintf(buf, sizeof(buf), "runtime/help/opers");
	ircd_paths[IRCD_PATH_OPERHELP] = rb_strdup(buf);

	snprintf(buf, sizeof(buf), "runtime/motd");
	ircd_paths[IRCD_PATH_IRCD_MOTD] = rb_strdup(buf);
	ircd_paths[IRCD_PATH_IRCD_OMOTD] = rb_strdup(buf);

	snprintf(buf, sizeof(buf), "%s.ban.db", name);
	ircd_paths[IRCD_PATH_BANDB] = rb_strdup(buf);
	snprintf(buf, sizeof(buf), "%s.ban.db-journal", name);
	unlink(buf);

	ircd_paths[IRCD_PATH_IRCD_PID] = rb_strdup(pidfile);
	ircd_paths[IRCD_PATH_IRCD_LOG] = rb_strdup(logfile);

	snprintf(buf, sizeof(buf), "runtime/bin");
	ircd_paths[IRCD_PATH_BIN] = rb_strdup(buf);
	ircd_paths[IRCD_PATH_LIBEXEC] = rb_strdup(buf);

	is_int(0, solanum_main(ARRAY_SIZE(argv) - 1, argv), MSG);
}

void ircd_util_reload_module(const char *name)
{
	struct module *mod = findmodule_byname(name);
	int origin, core;

	if (ok(mod != NULL, MSG)) {
		origin = mod->origin;
		core = mod->core;
		if (ok(unload_one_module(name, false), MSG)) {
			ok(load_one_module(name, origin, core), MSG);
		}
	}
}

void ircd_util_free(void)
{
}