From ac9b461f67af3fb4f669a4746d45478b99a9f588 Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Thu, 17 Apr 2014 19:03:24 +0400 Subject: [PATCH] common: add module_{load,has_parameter}() Add two kernel module helpers: module_{module,has_parameter}(). They are going to live in common/module.[ch]. Signed-off-by: Ilya Dryomov --- src/common/Makefile.am | 6 ++-- src/common/module.c | 75 ++++++++++++++++++++++++++++++++++++++++++ src/common/module.h | 27 +++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 src/common/module.c create mode 100644 src/common/module.h diff --git a/src/common/Makefile.am b/src/common/Makefile.am index ddeafb2057cc1..3a63dc604fd52 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -69,7 +69,8 @@ libcommon_la_SOURCES = \ common/addr_parsing.c \ common/hobject.cc \ common/bloom_filter.cc \ - common/linux_version.c + common/linux_version.c \ + common/module.c # these should go out of libcommon libcommon_la_SOURCES += \ @@ -197,7 +198,8 @@ noinst_HEADERS += \ common/sync_filesystem.h \ common/cmdparse.h \ common/hobject.h \ - common/linux_version.h + common/linux_version.h \ + common/module.h noinst_LTLIBRARIES += libcommon.la diff --git a/src/common/module.c b/src/common/module.c new file mode 100644 index 0000000000000..cdae181e8269a --- /dev/null +++ b/src/common/module.c @@ -0,0 +1,75 @@ +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2014 Inktank Storage, Inc. + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include +#include +#include +#include +#include +#include + +/* + * TODO: Switch to libkmod when we abandon older platforms. The APIs + * we want are: + * + * - kmod_module_new_from_name() for obtaining handles; + * - kmod_module_probe_insert_module() for module_load(); + * - kmod_module_get_info(), kmod_module_info_get_{key,value}() for + * module_has_param(). + */ + +/* + * Return command's exit status or -1 on error. + */ +static int run_command(const char *command) +{ + int status; + + status = system(command); + if (status >= 0 && WIFEXITED(status)) + return WEXITSTATUS(status); + + if (status < 0) { + char error_buf[80]; + fprintf(stderr, "couldn't run '%s': %s\n", command, + strerror_r(errno, error_buf, sizeof(error_buf))); + } else if (WIFSIGNALED(status)) { + fprintf(stderr, "'%s' killed by signal %d\n", command, + WTERMSIG(status)); + } else { + fprintf(stderr, "weird status from '%s': %d\n", command, + status); + } + + return -1; +} + +int module_has_param(const char *module, const char *param) +{ + char command[128]; + + snprintf(command, sizeof(command), + "/sbin/modinfo -F parm %s | /bin/grep -q ^%s:", + module, param); + + return run_command(command) == 0; +} + +int module_load(const char *module, const char *options) +{ + char command[128]; + + snprintf(command, sizeof(command), "/sbin/modprobe %s %s", + module, (options ? options : "")); + + return run_command(command); +} diff --git a/src/common/module.h b/src/common/module.h new file mode 100644 index 0000000000000..d5fa6a1a48778 --- /dev/null +++ b/src/common/module.h @@ -0,0 +1,27 @@ +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2014 Inktank Storage, Inc. + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#ifndef CEPH_MODULE_H +#define CEPH_MODULE_H + +#ifdef __cplusplus +extern "C" { +#endif + +int module_has_param(const char *module, const char *param); +int module_load(const char *module, const char *options); + +#ifdef __cplusplus +} +#endif + +#endif /* CEPH_MODULE_H */ -- 2.39.5