| 1 |
/* |
| 2 |
* Copyright (C) 2017 Red Hat, Inc. |
| 3 |
* |
| 4 |
* This library is free software; you can redistribute it and/or |
| 5 |
* modify it under the terms of the GNU Lesser General Public |
| 6 |
* License as published by the Free Software Foundation; either |
| 7 |
* version 2.1 of the License, or (at your option) any later version. |
| 8 |
* |
| 9 |
* This library is distributed in the hope that it will be useful, |
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* Lesser General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU Lesser General Public |
| 15 |
* License along with this library; if not, see <http://www.gnu.org/licenses/>. |
| 16 |
* |
| 17 |
* Author: Vojtech Trefny <[email protected]> |
| 18 |
*/ |
| 19 |
|
| 20 |
#include <glib.h> |
| 21 |
#include <glib/gprintf.h> |
| 22 |
#include <libkmod.h> |
| 23 |
#include <string.h> |
| 24 |
#include <syslog.h> |
| 25 |
#include <locale.h> |
| 26 |
#include <sys/utsname.h> |
| 27 |
|
| 28 |
#include "module.h" |
| 29 |
#include "exec.h" |
| 30 |
#include "logging.h" |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* bd_utils_module_error_quark: (skip) |
| 35 |
*/ |
| 36 |
GQuark bd_utils_module_error_quark (void) |
| 37 |
{ |
| 38 |
return g_quark_from_static_string ("g-bd-utils-module-error-quark"); |
| 39 |
} |
| 40 |
|
| 41 |
static void utils_kmod_log_redirect (void *log_data G_GNUC_UNUSED, int priority, |
| 42 |
const char *file G_GNUC_UNUSED, int line G_GNUC_UNUSED, |
| 43 |
const char *fn G_GNUC_UNUSED, const char *format, |
| 44 |
va_list args) { |
| 45 |
gchar *kmod_msg = NULL; |
| 46 |
gchar *message = NULL; |
| 47 |
gint ret = 0; |
| 48 |
|
| 49 |
ret = g_vasprintf (&kmod_msg, format, args); |
| 50 |
if (ret < 0) { |
| 51 |
g_free (kmod_msg); |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
#ifdef DEBUG |
| 56 |
message = g_strdup_printf ("[libmkod] %s:%d %s() %s", file, line, fn, kmod_msg); |
| 57 |
#else |
| 58 |
message = g_strdup_printf ("[libmkod] %s", kmod_msg); |
| 59 |
#endif |
| 60 |
bd_utils_log (priority, message); |
| 61 |
|
| 62 |
g_free (kmod_msg); |
| 63 |
g_free (message); |
| 64 |
|
| 65 |
} |
| 66 |
|
| 67 |
static void set_kmod_logging (struct kmod_ctx *ctx) { |
| 68 |
#ifdef DEBUG |
| 69 |
kmod_set_log_priority (ctx, LOG_DEBUG); |
| 70 |
#else |
| 71 |
kmod_set_log_priority (ctx, LOG_INFO); |
| 72 |
#endif |
| 73 |
kmod_set_log_fn (ctx, utils_kmod_log_redirect, NULL); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* bd_utils_have_kernel_module: |
| 78 |
* @module_name: name of the kernel module to check |
| 79 |
* @error: (out) (optional): place to store error (if any) |
| 80 |
* |
| 81 |
* Returns: whether the @module_name was found in the system, either as a module |
| 82 |
* or built-in in the kernel |
| 83 |
*/ |
| 84 |
gboolean bd_utils_have_kernel_module (const gchar *module_name, GError **error) { |
| 85 |
gint ret = 0; |
| 86 |
struct kmod_ctx *ctx = NULL; |
| 87 |
struct kmod_module *mod = NULL; |
| 88 |
struct kmod_list *list = NULL; |
| 89 |
gchar *null_config = NULL; |
| 90 |
const gchar *path = NULL; |
| 91 |
gboolean have_path = FALSE; |
| 92 |
gboolean builtin = FALSE; |
| 93 |
locale_t c_locale = newlocale (LC_ALL_MASK, "C", (locale_t) 0); |
| 94 |
|
| 95 |
ctx = kmod_new (NULL, (const gchar * const*) &null_config); |
| 96 |
if (!ctx) { |
| 97 |
g_set_error_literal (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_KMOD_INIT_FAIL, |
| 98 |
"Failed to initialize kmod context"); |
| 99 |
freelocale (c_locale); |
| 100 |
return FALSE; |
| 101 |
} |
| 102 |
set_kmod_logging (ctx); |
| 103 |
|
| 104 |
ret = kmod_module_new_from_lookup (ctx, module_name, &list); |
| 105 |
if (ret < 0) { |
| 106 |
g_set_error (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_FAIL, |
| 107 |
"Failed to get the module: %s", strerror_l (-ret, c_locale)); |
| 108 |
kmod_unref (ctx); |
| 109 |
kmod_module_unref_list (list); |
| 110 |
freelocale (c_locale); |
| 111 |
return FALSE; |
| 112 |
} |
| 113 |
|
| 114 |
if (list == NULL) { |
| 115 |
kmod_unref (ctx); |
| 116 |
freelocale (c_locale); |
| 117 |
return FALSE; |
| 118 |
} |
| 119 |
|
| 120 |
mod = kmod_module_get_module (list); |
| 121 |
path = kmod_module_get_path (mod); |
| 122 |
have_path = (path != NULL) && (g_strcmp0 (path, "") != 0); |
| 123 |
if (!have_path) { |
| 124 |
builtin = kmod_module_get_initstate (mod) == KMOD_MODULE_BUILTIN; |
| 125 |
} |
| 126 |
kmod_module_unref (mod); |
| 127 |
kmod_module_unref_list (list); |
| 128 |
kmod_unref (ctx); |
| 129 |
freelocale (c_locale); |
| 130 |
|
| 131 |
return have_path || builtin; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* bd_utils_load_kernel_module: |
| 136 |
* @module_name: name of the kernel module to load |
| 137 |
* @options: (nullable): module options |
| 138 |
* @error: (out) (optional): place to store error (if any) |
| 139 |
* |
| 140 |
* Returns: whether the @module_name was successfully loaded or not |
| 141 |
*/ |
| 142 |
gboolean bd_utils_load_kernel_module (const gchar *module_name, const gchar *options, GError **error) { |
| 143 |
gint ret = 0; |
| 144 |
struct kmod_ctx *ctx = NULL; |
| 145 |
struct kmod_module *mod = NULL; |
| 146 |
gchar *null_config = NULL; |
| 147 |
locale_t c_locale = newlocale (LC_ALL_MASK, "C", (locale_t) 0); |
| 148 |
|
| 149 |
ctx = kmod_new (NULL, (const gchar * const*) &null_config); |
| 150 |
if (!ctx) { |
| 151 |
g_set_error_literal (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_KMOD_INIT_FAIL, |
| 152 |
"Failed to initialize kmod context"); |
| 153 |
freelocale (c_locale); |
| 154 |
return FALSE; |
| 155 |
} |
| 156 |
set_kmod_logging (ctx); |
| 157 |
|
| 158 |
ret = kmod_module_new_from_name (ctx, module_name, &mod); |
| 159 |
if (ret < 0) { |
| 160 |
g_set_error (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_FAIL, |
| 161 |
"Failed to get the module: %s", strerror_l (-ret, c_locale)); |
| 162 |
kmod_unref (ctx); |
| 163 |
freelocale (c_locale); |
| 164 |
return FALSE; |
| 165 |
} |
| 166 |
|
| 167 |
if (!kmod_module_get_path (mod)) { |
| 168 |
g_set_error (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_NOEXIST, |
| 169 |
"Module '%s' doesn't exist", module_name); |
| 170 |
kmod_module_unref (mod); |
| 171 |
kmod_unref (ctx); |
| 172 |
freelocale (c_locale); |
| 173 |
return FALSE; |
| 174 |
} |
| 175 |
|
| 176 |
/* module, flags, options, run_install, data, print_action |
| 177 |
flag KMOD_PROBE_FAIL_ON_LOADED is used for backwards compatibility */ |
| 178 |
ret = kmod_module_probe_insert_module (mod, KMOD_PROBE_FAIL_ON_LOADED, |
| 179 |
options, NULL, NULL, NULL); |
| 180 |
if (ret < 0) { |
| 181 |
if (options) |
| 182 |
g_set_error (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_FAIL, |
| 183 |
"Failed to load the module '%s' with options '%s': %s", |
| 184 |
module_name, options, strerror_l (-ret, c_locale)); |
| 185 |
else |
| 186 |
g_set_error (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_FAIL, |
| 187 |
"Failed to load the module '%s': %s", |
| 188 |
module_name, strerror_l (-ret, c_locale)); |
| 189 |
kmod_module_unref (mod); |
| 190 |
kmod_unref (ctx); |
| 191 |
freelocale (c_locale); |
| 192 |
return FALSE; |
| 193 |
} |
| 194 |
|
| 195 |
kmod_module_unref (mod); |
| 196 |
kmod_unref (ctx); |
| 197 |
freelocale (c_locale); |
| 198 |
return TRUE; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* bd_utils_unload_kernel_module: |
| 203 |
* @module_name: name of the kernel module to unload |
| 204 |
* @error: (out) (optional): place to store error (if any) |
| 205 |
* |
| 206 |
* Returns: whether the @module_name was successfully unloaded or not |
| 207 |
*/ |
| 208 |
gboolean bd_utils_unload_kernel_module (const gchar *module_name, GError **error) { |
| 209 |
gint ret = 0; |
| 210 |
struct kmod_ctx *ctx = NULL; |
| 211 |
struct kmod_module *mod = NULL; |
| 212 |
struct kmod_list *list = NULL; |
| 213 |
struct kmod_list *cur = NULL; |
| 214 |
gchar *null_config = NULL; |
| 215 |
gboolean found = FALSE; |
| 216 |
locale_t c_locale = newlocale (LC_ALL_MASK, "C", (locale_t) 0); |
| 217 |
|
| 218 |
ctx = kmod_new (NULL, (const gchar * const*) &null_config); |
| 219 |
if (!ctx) { |
| 220 |
g_set_error_literal (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_KMOD_INIT_FAIL, |
| 221 |
"Failed to initialize kmod context"); |
| 222 |
freelocale (c_locale); |
| 223 |
return FALSE; |
| 224 |
} |
| 225 |
set_kmod_logging (ctx); |
| 226 |
|
| 227 |
ret = kmod_module_new_from_loaded (ctx, &list); |
| 228 |
if (ret < 0) { |
| 229 |
g_set_error (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_FAIL, |
| 230 |
"Failed to get the module: %s", strerror_l (-ret, c_locale)); |
| 231 |
kmod_unref (ctx); |
| 232 |
freelocale (c_locale); |
| 233 |
return FALSE; |
| 234 |
} |
| 235 |
|
| 236 |
for (cur=list; !found && cur != NULL; cur = kmod_list_next(list, cur)) { |
| 237 |
mod = kmod_module_get_module (cur); |
| 238 |
if (g_strcmp0 (kmod_module_get_name (mod), module_name) == 0) |
| 239 |
found = TRUE; |
| 240 |
else |
| 241 |
kmod_module_unref (mod); |
| 242 |
} |
| 243 |
kmod_module_unref_list (list); |
| 244 |
|
| 245 |
if (!found) { |
| 246 |
g_set_error (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_NOEXIST, |
| 247 |
"Module '%s' is not loaded", module_name); |
| 248 |
kmod_unref (ctx); |
| 249 |
freelocale (c_locale); |
| 250 |
return FALSE; |
| 251 |
} |
| 252 |
|
| 253 |
/* module, flags */ |
| 254 |
ret = kmod_module_remove_module (mod, 0); |
| 255 |
if (ret < 0) { |
| 256 |
g_set_error (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_FAIL, |
| 257 |
"Failed to unload the module '%s': %s", |
| 258 |
module_name, strerror_l (-ret, c_locale)); |
| 259 |
kmod_module_unref (mod); |
| 260 |
kmod_unref (ctx); |
| 261 |
freelocale (c_locale); |
| 262 |
return FALSE; |
| 263 |
} |
| 264 |
|
| 265 |
kmod_module_unref (mod); |
| 266 |
kmod_unref (ctx); |
| 267 |
freelocale (c_locale); |
| 268 |
return TRUE; |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
static BDUtilsLinuxVersion detected_linux_ver; |
| 273 |
static gboolean have_linux_ver = FALSE; |
| 274 |
|
| 275 |
G_LOCK_DEFINE_STATIC (detected_linux_ver); |
| 276 |
|
| 277 |
static BDUtilsLinuxVersion * _get_linux_version (gboolean lock, GError **error) { |
| 278 |
struct utsname buf; |
| 279 |
|
| 280 |
if (lock) |
| 281 |
G_LOCK (detected_linux_ver); |
| 282 |
|
| 283 |
/* return cached value if available */ |
| 284 |
if (have_linux_ver) { |
| 285 |
if (lock) |
| 286 |
G_UNLOCK (detected_linux_ver); |
| 287 |
return &detected_linux_ver; |
| 288 |
} |
| 289 |
|
| 290 |
memset (&detected_linux_ver, 0, sizeof (BDUtilsLinuxVersion)); |
| 291 |
|
| 292 |
if (uname (&buf)) { |
| 293 |
g_set_error (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_FAIL, |
| 294 |
"Failed to get linux kernel version: %m"); |
| 295 |
if (lock) |
| 296 |
G_UNLOCK (detected_linux_ver); |
| 297 |
return NULL; |
| 298 |
} |
| 299 |
|
| 300 |
if (g_ascii_strncasecmp (buf.sysname, "Linux", sizeof buf.sysname) != 0) { |
| 301 |
g_set_error (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_INVALID_PLATFORM, |
| 302 |
"Failed to get kernel version: spurious sysname '%s' detected", buf.sysname); |
| 303 |
if (lock) |
| 304 |
G_UNLOCK (detected_linux_ver); |
| 305 |
return NULL; |
| 306 |
} |
| 307 |
|
| 308 |
if (sscanf (buf.release, "%d.%d.%d", |
| 309 |
&detected_linux_ver.major, |
| 310 |
&detected_linux_ver.minor, |
| 311 |
&detected_linux_ver.micro) < 1) { |
| 312 |
g_set_error (error, BD_UTILS_MODULE_ERROR, BD_UTILS_MODULE_ERROR_FAIL, |
| 313 |
"Failed to parse kernel version: malformed release string '%s'", buf.release); |
| 314 |
if (lock) |
| 315 |
G_UNLOCK (detected_linux_ver); |
| 316 |
return NULL; |
| 317 |
} |
| 318 |
|
| 319 |
have_linux_ver = TRUE; |
| 320 |
if (lock) |
| 321 |
G_UNLOCK (detected_linux_ver); |
| 322 |
return &detected_linux_ver; |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* bd_utils_get_linux_version: |
| 327 |
* @error: (out) (optional): place to store error (if any) |
| 328 |
* |
| 329 |
* Retrieves version of currently running Linux kernel. Acts also as an initializer for statically cached data. |
| 330 |
* |
| 331 |
* Returns: (transfer none): Detected Linux kernel version or %NULL in case of an error. The returned value belongs to the library, do not free. |
| 332 |
*/ |
| 333 |
BDUtilsLinuxVersion * bd_utils_get_linux_version (GError **error) { |
| 334 |
return _get_linux_version (TRUE, error); |
| 335 |
} |
| 336 |
|
| 337 |
/** |
| 338 |
* bd_utils_check_linux_version: |
| 339 |
* @major: Minimal major linux kernel version. |
| 340 |
* @minor: Minimal minor linux kernel version. |
| 341 |
* @micro: Minimal micro linux kernel version. |
| 342 |
* |
| 343 |
* Checks whether the currently running linux kernel version is equal or higher |
| 344 |
* than the specified required @major.@minor.@micro version. |
| 345 |
* |
| 346 |
* Returns: an integer less than, equal to, or greater than zero, if detected version is <, == or > than the specified @major.@minor.@micro version. |
| 347 |
*/ |
| 348 |
gint bd_utils_check_linux_version (guint major, guint minor, guint micro) { |
| 349 |
gint ret; |
| 350 |
|
| 351 |
G_LOCK (detected_linux_ver); |
| 352 |
if (!have_linux_ver) |
| 353 |
_get_linux_version (FALSE, NULL); |
| 354 |
|
| 355 |
ret = detected_linux_ver.major - major; |
| 356 |
if (ret == 0) |
| 357 |
ret = detected_linux_ver.minor - minor; |
| 358 |
if (ret == 0) |
| 359 |
ret = detected_linux_ver.micro - micro; |
| 360 |
|
| 361 |
G_UNLOCK (detected_linux_ver); |
| 362 |
|
| 363 |
return ret; |
| 364 |
} |