Add kernel module version validation
[lttng-tools.git] / src / bin / lttng-sessiond / modprobe.c
CommitLineData
096102bd
DG
1/*
2 * Copyright (C) 2011 - David Goulet <dgoulet@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the Free
6 * Software Foundation; only version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307, USA.
16 */
17
18#define _GNU_SOURCE
19#include <stdio.h>
20#include <stdlib.h>
21#include <sys/wait.h>
22
23#include <common/common.h>
24
25#include "modprobe.h"
26#include "kern-modules.h"
27
28/* MUST be loaded first */
29const struct kern_modules_param kern_modules_control[] = {
30 { "ltt-relay", 1 },
31};
32
33/* LTTng kernel tracer modules list */
34const struct kern_modules_param kern_modules_list[] = {
35 { "lttng-ftrace", 0 },
36 { "lttng-kprobes", 0 },
37 { "lttng-kretprobes", 0 },
38 { "lib-ring-buffer", 1 },
39 { "ltt-ring-buffer-client-discard", 1 },
40 { "ltt-ring-buffer-client-overwrite", 1 },
41 { "ltt-ring-buffer-metadata-client", 1 },
42 { "ltt-ring-buffer-client-mmap-discard", 1 },
43 { "ltt-ring-buffer-client-mmap-overwrite", 1 },
44 { "ltt-ring-buffer-metadata-mmap-client", 1 },
45 { "lttng-probe-lttng", 1 },
46 { "lttng-types", 0 },
47 { "lttng-probe-block", 0 },
48 { "lttng-probe-irq", 0 },
49 { "lttng-probe-kvm", 0 },
50 { "lttng-probe-sched", 0 },
51};
52
53/*
54 * Remove control kernel module(s) in reverse load order.
55 */
56void modprobe_remove_lttng_control(void)
57{
58 int ret = 0, i;
59 char modprobe[256];
60
61 for (i = ARRAY_SIZE(kern_modules_control) - 1; i >= 0; i--) {
62 ret = snprintf(modprobe, sizeof(modprobe),
63 "/sbin/modprobe -r -q %s",
64 kern_modules_control[i].name);
65 if (ret < 0) {
66 PERROR("snprintf modprobe -r");
67 goto error;
68 }
69 modprobe[sizeof(modprobe) - 1] = '\0';
70 ret = system(modprobe);
71 if (ret == -1) {
72 ERR("Unable to launch modprobe -r for module %s",
73 kern_modules_control[i].name);
74 } else if (kern_modules_control[i].required
75 && WEXITSTATUS(ret) != 0) {
76 ERR("Unable to remove module %s",
77 kern_modules_control[i].name);
78 } else {
79 DBG("Modprobe removal successful %s",
80 kern_modules_control[i].name);
81 }
82 }
83
84error:
85 return;
86}
87
88/*
89 * Remove data kernel modules in reverse load order.
90 */
91void modprobe_remove_lttng_data(void)
92{
93 int ret = 0, i;
94 char modprobe[256];
95
96 for (i = ARRAY_SIZE(kern_modules_list) - 1; i >= 0; i--) {
97 ret = snprintf(modprobe, sizeof(modprobe),
98 "/sbin/modprobe -r -q %s",
99 kern_modules_list[i].name);
100 if (ret < 0) {
101 perror("snprintf modprobe -r");
102 goto error;
103 }
104 modprobe[sizeof(modprobe) - 1] = '\0';
105 ret = system(modprobe);
106 if (ret == -1) {
107 ERR("Unable to launch modprobe -r for module %s",
108 kern_modules_list[i].name);
109 } else if (kern_modules_list[i].required
110 && WEXITSTATUS(ret) != 0) {
111 ERR("Unable to remove module %s",
112 kern_modules_list[i].name);
113 } else {
114 DBG("Modprobe removal successful %s",
115 kern_modules_list[i].name);
116 }
117 }
118
119error:
120 return;
121}
122
123/*
124 * Remove all kernel modules in reverse order.
125 */
126void modprobe_remove_lttng_all(void)
127{
128 modprobe_remove_lttng_data();
129 modprobe_remove_lttng_control();
130}
131
132/*
133 * Load control kernel module(s).
134 */
135int modprobe_lttng_control(void)
136{
137 int ret = 0, i;
138 char modprobe[256];
139
140 for (i = 0; i < ARRAY_SIZE(kern_modules_control); i++) {
141 ret = snprintf(modprobe, sizeof(modprobe),
142 "/sbin/modprobe %s%s",
143 kern_modules_control[i].required ? "" : "-q ",
144 kern_modules_control[i].name);
145 if (ret < 0) {
146 PERROR("snprintf modprobe");
147 goto error;
148 }
149 modprobe[sizeof(modprobe) - 1] = '\0';
150 ret = system(modprobe);
151 if (ret == -1) {
152 ERR("Unable to launch modprobe for module %s",
153 kern_modules_control[i].name);
154 } else if (kern_modules_control[i].required
155 && WEXITSTATUS(ret) != 0) {
156 ERR("Unable to load module %s",
157 kern_modules_control[i].name);
158 } else {
159 DBG("Modprobe successfully %s",
160 kern_modules_control[i].name);
161 }
162 }
163
164error:
165 return ret;
166}
167
168/*
169 * Load data kernel module(s).
170 */
171int modprobe_lttng_data(void)
172{
173 int ret = 0, i;
174 char modprobe[256];
175
176 for (i = 0; i < ARRAY_SIZE(kern_modules_list); i++) {
177 ret = snprintf(modprobe, sizeof(modprobe),
178 "/sbin/modprobe %s%s",
179 kern_modules_list[i].required ? "" : "-q ",
180 kern_modules_list[i].name);
181 if (ret < 0) {
182 perror("snprintf modprobe");
183 goto error;
184 }
185 modprobe[sizeof(modprobe) - 1] = '\0';
186 ret = system(modprobe);
187 if (ret == -1) {
188 ERR("Unable to launch modprobe for module %s",
189 kern_modules_list[i].name);
190 } else if (kern_modules_list[i].required
191 && WEXITSTATUS(ret) != 0) {
192 ERR("Unable to load module %s",
193 kern_modules_list[i].name);
194 } else {
195 DBG("Modprobe successfully %s",
196 kern_modules_list[i].name);
197 }
198 }
199
200error:
201 return ret;
202}
203
204/*
205 * Load all lttng kernel modules.
206 */
207int modprobe_lttng_all(void)
208{
209 int ret;
210
211 ret = modprobe_lttng_control();
212 if (ret < 0) {
213 goto error;
214 }
215
216 ret = modprobe_lttng_data();
217 if (ret < 0) {
218 goto error;
219 }
220
221error:
222 return ret;
223}
This page took 0.045176 seconds and 4 git commands to generate.