]> git.proxmox.com Git - ceph.git/blob - ceph/src/common/module.c
import ceph quincy 17.2.4
[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 #ifndef _WIN32
24 /*
25 * TODO: Switch to libkmod when we abandon older platforms. The APIs
26 * we want are:
27 *
28 * - kmod_module_new_from_name() for obtaining handles;
29 * - kmod_module_probe_insert_module() for module_load();
30 * - kmod_module_get_info(), kmod_module_info_get_{key,value}() for
31 * module_has_param().
32 */
33
34 /*
35 * Return command's exit status or -1 on error.
36 */
37 static int run_command(const char *command)
38 {
39 int status;
40
41 status = system(command);
42 if (status >= 0 && WIFEXITED(status))
43 return WEXITSTATUS(status);
44
45 if (status < 0) {
46 char error_buf[80];
47 char* errp = ceph_strerror_r(errno, error_buf, sizeof(error_buf));
48 fprintf(stderr, "couldn't run '%s': %s\n", command,
49 errp);
50 } else if (WIFSIGNALED(status)) {
51 fprintf(stderr, "'%s' killed by signal %d\n", command,
52 WTERMSIG(status));
53 } else {
54 fprintf(stderr, "weird status from '%s': %d\n", command,
55 status);
56 }
57
58 return -1;
59 }
60
61 int module_has_param(const char *module, const char *param)
62 {
63 char command[128];
64
65 snprintf(command, sizeof(command),
66 "/sbin/modinfo -F parm %s | /bin/grep -q ^%s:",
67 module, param);
68
69 return run_command(command) == 0;
70 }
71
72 int module_load(const char *module, const char *options)
73 {
74 char command[128];
75
76 snprintf(command, sizeof(command), "/sbin/modprobe %s %s",
77 module, (options ? options : ""));
78
79 return run_command(command);
80 }
81
82 #else
83
84 // We're stubbing out those functions, for now.
85 int module_has_param(const char *module, const char *param)
86 {
87 return -1;
88 }
89
90 int module_load(const char *module, const char *options)
91 {
92 return -1;
93 }
94
95 #endif /* _WIN32 */