1 #include "pirmodprobe.h"
3 #include <unistd.h> // for fork()
4 #include <sys/types.h> // for pid_t
5 #include <sys/wait.h> // for waitpid()
6 #include "pirexception.h"
12 PIRModprobe::PIRModprobe()
13 : successfullyLoadedModule(false)
18 PIRModprobe::~PIRModprobe()
20 if (successfullyLoadedModule)
27 int PIRModprobe::modprobeRX51Module(
30 // First, has the module already been loaded? If /dev/lirc0 exists, it
31 // should already be there:
32 struct stat statBuffer;
33 if (stat("/dev/lirc0", &statBuffer) == 0)
35 // "/dev/lirc0" exists, so no need to do anything:
39 // Do the hard work: start by forking off a child process:
44 // The fork failed! Tell our user about the error:
46 ss << "Failed to fork a child process.\n";
47 ss << "Error returned was: " << strerror(errno) << "\n";
48 PIRException e(ss.str());
54 // We're inside the child process, so exec a modprobe:
60 "/opt/pierogi/bin/unloadRX51Module",
68 "/opt/pierogi/bin/loadRX51Module",
71 // The execl call should overwrite the child process. So, if we still
72 // exist at this point, an error has occurred:
74 ss << "Failed to successfully call execl().\n";
75 ss << "Error returned was: " << strerror(errno) << "\n";
76 PIRException e(ss.str());
81 // If we reach this point, we are inside the parent process. So, we'll wait
82 // for the child process to complete:
84 if (waitpid(pid, stat_loc, 0) == -1)
86 // The call to modprobe failed.
88 ss << "Call to modprobe failed.\n";
89 ss << "Error returned was: " << strerror(errno) << "\n";
90 PIRException e(ss.str());
97 if (WIFEXITED(*stat_loc) == 0)
99 // modprobe encountered an error of some sort.
100 std::stringstream ss;
101 ss << "Unable to load the lirc_rx51 module.\n";
102 // Need better details about the error here!
103 PIRException e(ss.str());
109 // By this point, we should have successfully ensured the module is loaded.
110 successfullyLoadedModule = true;