]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/module.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / common / module.c
1 /*
2 * Ceph - scalable distributed file system
3 *
4 * Copyright (C) 2014 Inktank Storage, Inc.
5 *
6 * This is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License version 2.1, as published by the Free Software
9 * Foundation. See file COPYING.
10 *
11 */
12
13 #include "acconfig.h"
14 #include "include/compat.h"
15 #include <errno.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #if defined(__FreeBSD__)
20 #include <sys/wait.h>
21 #endif
22
23 /*
24 * TODO: Switch to libkmod when we abandon older platforms. The APIs
25 * we want are:
26 *
27 * - kmod_module_new_from_name() for obtaining handles;
28 * - kmod_module_probe_insert_module() for module_load();
29 * - kmod_module_get_info(), kmod_module_info_get_{key,value}() for
30 * module_has_param().
31 */
32
33 /*
34 * Return command's exit status or -1 on error.
35 */
36 static int run_command(const char *command)
37 {
38 int status;
39
40 status = system(command);
41 if (status >= 0 && WIFEXITED(status))
42 return WEXITSTATUS(status);
43
44 if (status < 0) {
45 char error_buf[80];
46 char* errp = ceph_strerror_r(errno, error_buf, sizeof(error_buf));
47 fprintf(stderr, "couldn't run '%s': %s\n", command,
48 errp);
49 } else if (WIFSIGNALED(status)) {
50 fprintf(stderr, "'%s' killed by signal %d\n", command,
51 WTERMSIG(status));
52 } else {
53 fprintf(stderr, "weird status from '%s': %d\n", command,
54 status);
55 }
56
57 return -1;
58 }
59
60 int module_has_param(const char *module, const char *param)
61 {
62 char command[128];
63
64 snprintf(command, sizeof(command),
65 "/sbin/modinfo -F parm %s | /bin/grep -q ^%s:",
66 module, param);
67
68 return run_command(command) == 0;
69 }
70
71 int module_load(const char *module, const char *options)
72 {
73 char command[128];
74
75 snprintf(command, sizeof(command), "/sbin/modprobe %s %s",
76 module, (options ? options : ""));
77
78 return run_command(command);
79 }