Debian Codegraph @

modules/operserv/modreload.c

raw
1 #include "atheme.h"
2 #include "conf.h"
3 #include "uplink.h" /* XXX: For sendq_flush and curr_uplink */
4 #include "datastream.h"
5
6 DECLARE_MODULE_V1
7 (
8 "operserv/modreload", false, _modinit, _moddeinit,
9 PACKAGE_STRING,
10 VENDOR_STRING
11 );
12
13 static void os_cmd_modreload(sourceinfo_t *si, int parc, char *parv[]);
14
15 command_t os_modreload = { "MODRELOAD", N_("Reloads a module."), PRIV_ADMIN, 20, os_cmd_modreload, { .path = "oservice/modreload" } };
16
17 void _modinit(module_t *m)
18 {
19 service_named_bind_command("operserv", &os_modreload);
20 }
21
22 void _moddeinit(module_unload_intent_t intent)
23 {
24 service_named_unbind_command("operserv", &os_modreload);
25 }
26
27 static void recurse_module_deplist(module_t *m, mowgli_list_t *deplist)
28 {
29 mowgli_node_t *n;
30 MOWGLI_LIST_FOREACH(n, m->dephost.head)
31 {
32 module_t *dm = (module_t *) n->data;
33
34 /* Skip duplicates */
35 bool found = false;
36 mowgli_node_t *n2;
37 MOWGLI_LIST_FOREACH(n2, deplist->head)
38 {
39 module_dependency_t *existing_dep = (module_dependency_t *) n2->data;
40 if (0 == strcasecmp(dm->name, existing_dep->name))
41 found = true;
42 }
43 if (found)
44 continue;
45
46 module_dependency_t *dep = malloc(sizeof(module_dependency_t));
47 dep->name = sstrdup(dm->name);
48 dep->can_unload = dm->can_unload;
49 mowgli_node_add(dep, mowgli_node_create(), deplist);
50
51 recurse_module_deplist(dm, deplist);
52 }
53 }
54
55 static void os_cmd_modreload(sourceinfo_t *si, int parc, char *parv[])
56 {
57 char *module = parv[0];
58 module_t *m;
59 mowgli_node_t *n;
60 module_dependency_t * reloading_semipermanent_module = NULL;
61
62 if (parc < 1)
63 {
64 command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, "MODRELOAD");
65 command_fail(si, fault_needmoreparams, _("Syntax: MODRELOAD <module...>"));
66 return;
67 }
68
69 if (!(m = module_find_published(module)))
70 {
71 command_fail(si, fault_nochange, _("\2%s\2 is not loaded."), module);
72 return;
73 }
74
75 /* Make sure these checks happen before we construct the list of dependent modules, so that
76 * we can return without having to clean up everything.
77 */
78
79 if (!strcmp(m->name, "operserv/main") || !strcmp(m->name, "operserv/modload") || !strcmp(m->name, "operserv/modunload") || !strcmp(m->name, "operserv/modreload"))
80 {
81 command_fail(si, fault_noprivs, _("Refusing to reload \2%s\2."), module);
82 return;
83 }
84
85 if (m->can_unload == MODULE_UNLOAD_CAPABILITY_NEVER)
86 {
87 command_fail(si, fault_noprivs, _("\2%s\2 is a permanent module; it cannot be reloaded."), module);
88 slog(LG_ERROR, "MODRELOAD:ERROR: \2%s\2 tried to reload permanent module \2%s\2", get_oper_name(si), module);
89 return;
90 }
91
92 mowgli_list_t *module_deplist = mowgli_list_create();
93 module_dependency_t *self_dep = malloc(sizeof(module_dependency_t));
94 self_dep->name = sstrdup(module);
95 self_dep->can_unload = m->can_unload;
96 mowgli_node_add(self_dep, mowgli_node_create(), module_deplist);
97 recurse_module_deplist(m, module_deplist);
98
99 MOWGLI_LIST_FOREACH(n, module_deplist->head)
100 {
101 module_dependency_t *dep = (module_dependency_t *) n->data;
102 if (dep->can_unload == MODULE_UNLOAD_CAPABILITY_NEVER)
103 {
104 command_fail(si, fault_noprivs, _("\2%s\2 is depended upon by \2%s\2, which is a permanent module and cannot be reloaded."), module, dep->name);
105 slog(LG_ERROR, "MODRELOAD:ERROR: \2%s\2 tried to reload \2%s\2, which is depended upon by permanent module \2%s\2", get_oper_name(si), module, dep->name);
106
107 /* We've constructed the dep list now, so don't return without cleaning up */
108 while (module_deplist->head != NULL)
109 {
110 dep = module_deplist->head->data;
111 free(dep->name);
112 free(dep);
113 mowgli_node_delete(module_deplist->head, module_deplist);
114 mowgli_list_free(module_deplist);
115 return;
116 }
117 }
118 else if (dep->can_unload == MODULE_UNLOAD_CAPABILITY_RELOAD_ONLY
119 && reloading_semipermanent_module == NULL)
120 {
121 reloading_semipermanent_module = dep;
122 }
123 }
124
125 if (reloading_semipermanent_module)
126 {
127 /* If we're reloading a semi-permanent module (reload only; no unload), then there's
128 * a chance that we'll abort if the module fails to load again. Save the DB beforehand
129 * just in case
130 */
131 slog(LG_INFO, "UPDATE (due to reload of module \2%s\2): \2%s\2",
132 reloading_semipermanent_module->name, get_oper_name(si));
133 wallops("Updating database by request of \2%s\2.", get_oper_name(si));
134 db_save(NULL);
135 }
136
137 module_unload(m, MODULE_UNLOAD_INTENT_RELOAD);
138
139 while (module_deplist->head != NULL)
140 {
141 module_t *t;
142 n = module_deplist->head;
143 module_dependency_t *dep = (module_dependency_t *) n->data;
144
145 t = module_load(dep->name);
146
147 if (t != NULL)
148 {
149 logcommand(si, CMDLOG_ADMIN, "MODRELOAD: \2%s\2 (from \2%s\2)", dep->name, module);
150 command_success_nodata(si, _("Module \2%s\2 reloaded (from \2%s\2)."), dep->name, module);
151 }
152 else
153 {
154 if (dep->can_unload != MODULE_UNLOAD_CAPABILITY_OK)
155 {
156 /* Failed to reload a module that can't be unloaded. Abort. */
157 command_fail(si, fault_nosuch_target, _(
158 "Module \2%s\2 failed to reload, and does not allow unloading. "
159 "Shutting down to avoid data loss."), dep->name);
160 slog(LG_ERROR, "MODRELOAD:ERROR: \2%s\2 failed to reload and does not allow unloading. "
161 "Shutting down to avoid data loss.", dep->name);
162 sendq_flush(curr_uplink->conn);
163
164 exit(EXIT_FAILURE);
165 }
166
167 command_fail(si, fault_nosuch_target, _("Module \2%s\2 failed to reload."), dep->name);
168 slog(LG_ERROR, "MODRELOAD:ERROR: \2%s\2 tried to reload \2%s\2 (from \2%s\2), operation failed.", get_oper_name(si), dep->name, module);
169 }
170
171 free(dep->name);
172 free(dep);
173 mowgli_node_delete(n, module_deplist);
174 }
175 mowgli_list_free(module_deplist);
176
177 if (conf_need_rehash)
178 {
179 logcommand(si, CMDLOG_ADMIN, "REHASH (MODRELOAD)");
180 wallops("Rehashing \2%s\2 to complete module reload by request of \2%s\2.", config_file, get_oper_name(si));
181 if (!conf_rehash())
182 command_fail(si, fault_nosuch_target, _("REHASH of \2%s\2 failed. Please correct any errors in the file and try again."), config_file);
183 }
184 }