#ifdef HAVE_XORG_CONFIG_H
#include <xorg-config.h>
#endif
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#include <signal.h>
#include "xf86_OSlib.h"
#include "xf86.h"
#define MODPROBE_PATH_FILE "/proc/sys/kernel/modprobe"
#define MAX_PATH 1024
#if 0
#ifdef execl
#undef execl
#endif
#endif
_X_EXPORT int
xf86LoadKernelModule(const char *modName)
{
char mpPath[MAX_PATH] = "";
int fd = -1, status, n;
pid_t pid;
fd = open(MODPROBE_PATH_FILE, O_RDONLY);
if (fd >= 0) {
int count = read(fd, mpPath, MAX_PATH - 1);
if (count <= 0) {
mpPath[0] = 0;
}
else if (mpPath[count - 1] == '\n') {
mpPath[count - 1] = 0;
}
close(fd);
}
if (mpPath[0] == 0) {
strcpy(mpPath, "/sbin/modprobe");
}
switch (pid = fork()) {
case 0:
if (setreuid(0,0)) {
xf86Msg(X_WARNING,"LoadKernelModule: "
"Setting of real/effective user Id to 0/0 failed");
}
setenv("PATH","/sbin",1);
n = execl(mpPath, "modprobe", modName, NULL);
xf86Msg(X_WARNING,"LoadKernelModule %s\n",strerror(errno));
exit(EXIT_FAILURE);
break;
case -1:
return 0;
default:
{
int count = 0, p;
do {
p = waitpid(pid, &status, 0);
} while (p == -1 && count++ < 4);
if (p == -1) {
return 0;
}
if (WIFEXITED(status) && WEXITSTATUS(status) == 0) {
return 1;
}
else {
return 0;
}
}
}
return 0;
}