File: libkmod.patch

package info (click to toggle)
bumblebee 3.2.1-32
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,304 kB
  • sloc: ansic: 2,845; sh: 1,792; makefile: 187
file content (406 lines) | stat: -rw-r--r-- 11,864 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
Author: Ondřej Janošík <j.ondra14@gmail.com>
Description: module: use libkmod2 instead of modprobe
Origin: commit:cab32cd9994df183c696310d7a30291061d0039f
Applied-Upstream: commit:cab32cd9994df183c696310d7a30291061d0039f
--- a/Makefile.am
+++ b/Makefile.am
@@ -10,7 +10,7 @@
 		-DCONF_XORG='"$(bumblebeedconfdir)/xorg.conf.DRIVER"' \
 		-DCONF_XORG_DIR='"$(bumblebeedconfdir)/xorg.conf.d"'
 AM_CFLAGS = ${regular_CFLAGS} \
-		${x11_CFLAGS} ${libbsd_CFLAGS} ${glib_CFLAGS} \
+		${x11_CFLAGS} ${libbsd_CFLAGS} ${glib_CFLAGS} ${kmod_CFLAGS} \
 		-Wextra -funsigned-char -DGITVERSION='"${GITVERSION}"'
 
 noinst_SCRIPTS = scripts/systemd/bumblebeed.service \
@@ -49,13 +49,13 @@
 bin_PROGRAMS = bin/optirun
 
 bin_optirun_SOURCES = src/module.c src/bbconfig.c src/bblogger.c src/bbrun.c \
-	src/bbsocket.c src/driver.c src/optirun.c src/bbsocketclient.c
-bin_optirun_LDADD = ${glib_LIBS} -lrt
+	src/bbsocket.c src/optirun.c src/bbsocketclient.c
+bin_optirun_LDADD = ${glib_LIBS} ${kmod_LIBS} -lrt
 bin_bumblebeed_SOURCES = src/pci.c src/bbconfig.c src/bblogger.c src/bbrun.c \
 	src/bbsocket.c src/module.c src/bbsecondary.c src/switch/switching.c \
 	src/switch/sw_bbswitch.c src/switch/sw_switcheroo.c \
 	src/driver.c src/bumblebeed.c
-bin_bumblebeed_LDADD = ${x11_LIBS} ${libbsd_LIBS} ${glib_LIBS} -lrt
+bin_bumblebeed_LDADD = ${x11_LIBS} ${libbsd_LIBS} ${glib_LIBS} ${kmod_LIBS} -lrt
 
 dist_doc_DATA = $(relnotes) README.markdown
 bumblebeedconf_DATA = conf/bumblebee.conf conf/xorg.conf.nouveau conf/xorg.conf.nvidia
--- a/README.markdown
+++ b/README.markdown
@@ -19,6 +19,7 @@
 - pkg-config
 - glib-2.0 and development headers
 - libx11 and development headers
+- libkmod2 and development headers
 - libbsd and development headers (if pidfile support is enabled, default yes)
 - help2man (optional, it is needed for building manual pages)
 
--- a/configure.ac
+++ b/configure.ac
@@ -132,6 +132,7 @@
 # Checks for header files.
 PKG_CHECK_MODULES([x11], [x11])
 PKG_CHECK_MODULES([glib], [glib-2.0])
+PKG_CHECK_MODULES([kmod], [libkmod])
 AS_IF([test "x$with_pidfile" != xno], [
 		PKG_CHECK_MODULES([libbsd], [libbsd >= 0.2.0])
 		PKG_CHECK_EXISTS([libbsd = 0.2.0], [AC_DEFINE(HAVE_LIBBSD_020)])
--- a/src/bbconfig.c
+++ b/src/bbconfig.c
@@ -251,12 +251,6 @@
  */
 static int bbconfig_parse_common(int opt, char *value) {
   switch (opt) {
-    case 'q'://quiet mode
-      bb_status.verbosity = VERB_NONE;
-      break;
-    case OPT_DEBUG://debug mode
-      bb_status.verbosity = VERB_ALL;
-      break;
     case 'd'://X display number
       set_string_value(&bb_config.x_display, value);
       break;
@@ -307,6 +301,12 @@
             bb_status.verbosity++;
           }
           break;
+        case 'q'://quiet mode
+          bb_status.verbosity = VERB_NONE;
+          break;
+        case OPT_DEBUG://debug mode
+          bb_status.verbosity = VERB_ALL;
+          break;
         case 's': /* Unix socket to use for communication */
           set_string_value(&bb_config.socket_path, optarg);
           break;
--- a/src/bbconfig.h
+++ b/src/bbconfig.h
@@ -26,6 +26,7 @@
 #include <unistd.h> //for pid_t
 #include <limits.h> //for CHAR_MAX
 #include <glib.h>
+#include <libkmod.h>
 
 /* Daemon states */
 #define BB_DAEMON 1
@@ -118,6 +119,7 @@
     int x_pipe[2];//pipes for reading/writing output from X's stdout/stderr
     gboolean use_syslog;
     char *program_name;
+    struct kmod_ctx *kmod_ctx;
 };
 
 /* Structure containing the configuration. */
--- a/src/bblogger.c
+++ b/src/bblogger.c
@@ -228,7 +228,7 @@
         /* line / buffer is full, process the remaining buffer the next round */
         repeat = 1;
       }
-    }else{
+    } else {
       if (r == 0 || (errno != EAGAIN && r == -1)){
         /* the pipe is closed/invalid. Clean up. */
         if (bb_status.x_pipe[0] != -1){close(bb_status.x_pipe[0]); bb_status.x_pipe[0] = -1;}
@@ -257,5 +257,5 @@
         memmove(x_output_buffer, next_part, x_buffer_pos);
       }
     }
-  }while(repeat);
+  }while (repeat);
 }/* check_xorg_pipe */
--- a/src/bumblebeed.c
+++ b/src/bumblebeed.c
@@ -34,6 +34,7 @@
 #include <string.h>
 #include <errno.h>
 #include <getopt.h>
+#include <libkmod.h>
 #ifdef WITH_PIDFILE
 #ifdef HAVE_LIBBSD_020
 #include <libutil.h>
@@ -488,6 +489,14 @@
 
   free(pci_id_igd);
 
+  // kmod context have to be available for driver detection
+  bb_status.kmod_ctx = kmod_new(NULL, NULL);
+  if (bb_status.kmod_ctx == NULL) {
+    bb_log(LOG_ERR, "kmod_new() failed!\n");
+    bb_closelog();
+    exit(EXIT_FAILURE);
+  }
+
   GKeyFile *bbcfg = bbconfig_parse_conf();
   bbconfig_parse_opts(argc, argv, PARSE_STAGE_DRIVER);
   driver_detect();
@@ -500,6 +509,7 @@
 
   /* dump the config after detecting the driver */
   config_dump();
+
   if (config_validate() != 0) {
     return (EXIT_FAILURE);
   }
@@ -572,5 +582,7 @@
   //close X pipe, if any parts of it are open still
   if (bb_status.x_pipe[0] != -1){close(bb_status.x_pipe[0]); bb_status.x_pipe[0] = -1;}
   if (bb_status.x_pipe[1] != -1){close(bb_status.x_pipe[1]); bb_status.x_pipe[1] = -1;}
+  //cleanup kmod context
+  kmod_unref(bb_status.kmod_ctx);
   return (EXIT_SUCCESS);
 }
--- a/src/module.c
+++ b/src/module.c
@@ -24,91 +24,152 @@
 #include <ctype.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include <libkmod.h>
+#include <errno.h>
 #include "module.h"
 #include "bblogger.h"
 #include "bbrun.h"
+#include "bbconfig.h"
+
+int module_unload_recursive(struct kmod_module *mod);
 
 /**
- * Checks in /proc/modules whether a kernel module is loaded
+ * Checks whether a kernel module is loaded
  *
  * @param driver The name of the driver (not a filename)
  * @return 1 if the module is loaded, 0 otherwise
  */
 int module_is_loaded(char *driver) {
-  // use the same buffer length as lsmod
-  char buffer[4096];
-  FILE * bbs = fopen("/proc/modules", "r");
-  int ret = 0;
-  /* assume mod_len <= sizeof(buffer) */
-  int mod_len = strlen(driver);
-
-  if (bbs == 0) {//error opening, return -1
-    bb_log(LOG_DEBUG, "Couldn't open /proc/modules");
-    return -1;
-  }
-  while (fgets(buffer, sizeof(buffer), bbs)) {
-    /* match "module" with "module " and not "module-blah" */
-    if (!strncmp(buffer, driver, mod_len) && isspace(buffer[mod_len])) {
-      /* module is found */
-      ret = 1;
-      break;
-    }
+  int err, state;
+  struct kmod_module *mod;
+
+  err = kmod_module_new_from_name(bb_status.kmod_ctx, driver, &mod);
+  if (err < 0) {
+    bb_log(LOG_DEBUG, "kmod_module_new_from_name(%s) failed (err: %d).\n",
+      driver, err);
+    return 0;
   }
-  fclose(bbs);
-  return ret;
+
+  state = kmod_module_get_initstate(mod);
+  kmod_module_unref(mod);
+
+  return state == KMOD_MODULE_LIVE;
 }
 
 /**
- * Attempts to load a module. If the module has not been loaded after ten
- * seconds, give up
+ * Attempts to load a module.
  *
  * @param module_name The filename of the module to be loaded
  * @param driver The name of the driver to be loaded
  * @return 1 if the driver is successfully loaded, 0 otherwise
  */
 int module_load(char *module_name, char *driver) {
+  int err = 0;
+  int flags = KMOD_PROBE_IGNORE_LOADED;
+  struct kmod_list *l, *list = NULL;
+
   if (module_is_loaded(driver) == 0) {
     /* the module has not loaded yet, try to load it */
-    bb_log(LOG_INFO, "Loading driver %s (module %s)\n", driver, module_name);
-    char *mod_argv[] = {
-      "modprobe",
-      module_name,
-      NULL
-    };
-    bb_run_fork_wait(mod_argv, 10);
-    if (module_is_loaded(driver) == 0) {
-      bb_log(LOG_ERR, "Module %s could not be loaded (timeout?)\n", module_name);
+
+    bb_log(LOG_INFO, "Loading driver '%s' (module '%s')\n", driver, module_name);
+    err = kmod_module_new_from_lookup(bb_status.kmod_ctx, module_name, &list);
+
+    if (err < 0) {
+      bb_log(LOG_DEBUG, "kmod_module_new_from_lookup(%s) failed (err: %d).\n",
+        module_name, err);
+      return 0;
+    }
+
+    if (list == NULL) {
+      bb_log(LOG_ERR, "Module '%s' not found.\n", module_name);
       return 0;
     }
+
+    kmod_list_foreach(l, list) {
+      struct kmod_module *mod = kmod_module_get_module(l);
+
+      bb_log(LOG_DEBUG, "Loading module '%s'.\n", kmod_module_get_name(mod));
+      err = kmod_module_probe_insert_module(mod, flags, NULL, NULL, NULL, 0);
+
+      if (err < 0) {
+        bb_log(LOG_DEBUG, "kmod_module_probe_insert_module(%s) failed (err: %d).\n",
+          kmod_module_get_name(mod), err);
+      }
+
+      kmod_module_unref(mod);
+
+      if (err < 0) {
+        break;
+      }
+    }
+
+    kmod_module_unref_list(list);
   }
-  return 1;
+
+  return err >= 0;
 }
 
 /**
- * Attempts to unload a module if loaded, for ten seconds before
- * giving up
+ * Unloads module and modules that are depending on this module.
+ *
+ * @param mod Reference to libkmod module
+ * @return 1 if the module is successfully unloaded, 0 otherwise
+ */
+int module_unload_recursive(struct kmod_module *mod) {
+  int err = 0, flags = 0, refcnt;
+  struct kmod_list *holders;
+
+  holders = kmod_module_get_holders(mod);
+  if (holders != NULL) {
+    struct kmod_list *itr;
+
+    kmod_list_foreach(itr, holders) {
+      struct kmod_module *hm = kmod_module_get_module(itr);
+      err = module_unload_recursive(hm);
+      kmod_module_unref(hm);
+
+      if (err < 0) {
+        break;
+      }
+    }
+    kmod_module_unref_list(holders);
+  }
+
+  refcnt = kmod_module_get_refcnt(mod);
+  if (refcnt == 0) {
+    bb_log(LOG_INFO, "Unloading module %s\n", kmod_module_get_name(mod));
+    err = kmod_module_remove_module(mod, flags);
+  } else {
+    bb_log(LOG_ERR, "Failed to unload module '%s' (ref count: %d).\n",
+      kmod_module_get_name(mod), refcnt);
+    err = 1;
+  }
+
+  return err == 0;
+}
+
+/**
+ * Attempts to unload a module if loaded.
  *
  * @param driver The name of the driver (not a filename)
  * @return 1 if the driver is successfully unloaded, 0 otherwise
  */
 int module_unload(char *driver) {
+  int err;
+  struct kmod_module *mod;
   if (module_is_loaded(driver) == 1) {
-    int retries = 30;
-    bb_log(LOG_INFO, "Unloading %s driver\n", driver);
-    char *mod_argv[] = {
-      "modprobe",
-      "-r",
-      driver,
-      NULL
-    };
-    bb_run_fork_wait(mod_argv, 10);
-    while (retries-- > 0 && module_is_loaded(driver) == 1) {
-      usleep(100000);
-    }
-    if (module_is_loaded(driver) == 1) {
-      bb_log(LOG_ERR, "Unloading %s driver timed out.\n", driver);
+    err = kmod_module_new_from_name(bb_status.kmod_ctx, driver, &mod);
+
+    if (err < 0) {
+      bb_log(LOG_DEBUG, "kmod_module_new_from_name(%s) failed (err: %d).\n",
+        driver, err);
       return 0;
     }
+
+    err = module_unload_recursive(mod);
+    kmod_module_unref(mod);
+
+    return err;
   }
   return 1;
 }
@@ -120,18 +181,20 @@
  * @return 1 if the module is available for loading, 0 otherwise
  */
 int module_is_available(char *module_name) {
-  /* HACK to support call from optirun */
-  char *modprobe_bin = "/sbin/modprobe";
-  if (access(modprobe_bin, X_OK)) {
-    /* if /sbin/modprobe is not found, pray that PATH contains it */
-    modprobe_bin = "modprobe";
-  }
-  char *mod_argv[] = {
-    modprobe_bin,
-    "--dry-run",
-    "--quiet",
-    module_name,
-    NULL
-  };
-  return bb_run_fork(mod_argv, 1) == EXIT_SUCCESS;
+  int err, available;
+  struct kmod_list *list = NULL;
+
+  err = kmod_module_new_from_lookup(bb_status.kmod_ctx, module_name, &list);
+
+  if (err < 0) {
+    bb_log(LOG_DEBUG, "kmod_module_new_from_lookup(%s) failed (err: %d).\n",
+      module_name, err);
+    return 0;
+  }
+
+  available = list != NULL;
+
+  kmod_module_unref_list(list);
+
+  return available;
 }
--- a/src/optirun.c
+++ b/src/optirun.c
@@ -37,7 +37,6 @@
 #include "bbsocketclient.h"
 #include "bblogger.h"
 #include "bbrun.h"
-#include "driver.h"
 
 
 /**