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"
11 PIRModprobe::PIRModprobe()
12 : successfullyLoadedModule(false)
17 PIRModprobe::~PIRModprobe()
19 if (successfullyLoadedModule)
26 int PIRModprobe::loadRX51Module()
28 // First, has the module already been loaded? If /dev/lirc0 exists, it
29 // should already be there:
30 struct stat statBuffer;
31 if (stat("/dev/lirc0", &statBuffer) == 0)
33 // "/dev/lirc0" exists, so no need to do anything:
34 successfullyLoadedModule = false;
38 // Do the hard work: start by forking off a child process:
43 // The fork failed! Tell our user about the error:
45 ss << "Failed to fork a child process.\n";
46 ss << "Error returned was: " << strerror(errno) << "\n";
47 PIRException e(ss.str());
53 // We're inside the child process, so exec the load module script:
57 "/opt/pierogi/bin/loadRX51Module",
60 // The execl call should overwrite the child process. So, if we still
61 // exist at this point, an error has occurred:
63 ss << "Failed to successfully call execl().\n";
64 ss << "Error returned was: " << strerror(errno) << "\n";
65 PIRException e(ss.str());
70 // If we reach this point, we are inside the parent process. So, we'll wait
71 // for the child process to complete:
73 if (waitpid(pid, stat_loc, 0) == -1)
75 // The call to modprobe failed.
77 ss << "Call to modprobe failed.\n";
78 ss << "Error returned was: " << strerror(errno) << "\n";
79 PIRException e(ss.str());
86 if (WIFEXITED(*stat_loc) == 0)
88 // modprobe encountered an error of some sort.
90 ss << "Unable to load the lirc_rx51 module.\n";
91 // Need better details about the error here!
92 PIRException e(ss.str());
98 // By this point, we should have successfully loaded module.
99 successfullyLoadedModule = true;
105 int PIRModprobe::unloadRX51Module()
107 // start by forking off a child process:
112 // The fork failed! Tell our user about the error:
113 std::stringstream ss;
114 ss << "Failed to fork a child process.\n";
115 ss << "Error returned was: " << strerror(errno) << "\n";
116 PIRException e(ss.str());
122 // We're inside the child process, so exec the unload script:
126 "/opt/pierogi/bin/unloadRX51Module",
129 // The execl call should overwrite the child process. So, if we still
130 // exist at this point, an error has occurred:
131 std::stringstream ss;
132 ss << "Failed to successfully call execl().\n";
133 ss << "Error returned was: " << strerror(errno) << "\n";
134 PIRException e(ss.str());
139 // If we reach this point, we are inside the parent process. So, we'll wait
140 // for the child process to complete:
141 int *stat_loc = NULL;
142 if (waitpid(pid, stat_loc, 0) == -1)
144 // The call to modprobe failed.
145 std::stringstream ss;
146 ss << "Call to modprobe failed.\n";
147 ss << "Error returned was: " << strerror(errno) << "\n";
148 PIRException e(ss.str());
155 if (WIFEXITED(*stat_loc) == 0)
157 // modprobe encountered an error of some sort.
158 std::stringstream ss;
159 ss << "Unable to unload the lirc_rx51 module.\n";
160 // Need better details about the error here!
161 PIRException e(ss.str());
167 // If we reach this point, module has been unloaded successfully:
168 successfullyLoadedModule = false;