I have this test code to use the smartctl command. It opens a pipe using popen
to get the output of this smartctl
command.
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
int main()
{
cout << "Hello ! " << endl;
FILE *fp;
char path[1035];
/* Open the command for reading. */
fp = popen("smartctl -A /dev/sda", "r");
if (fp == NULL) {
printf("Failed to run command\n" );
exit(1);
}
/* Read the output a line at a time - output it. */
while (fgets(path, sizeof(path), fp) != NULL) {
printf("%s", path);
}
/* close */
pclose(fp);
return 0;
}
Clearly running this program requires a root privilege and good thing for me to use is capabilities. What capability to use with this program and how to set it ?
Side question: I used another method here about changing ownership of the executable, it did not work. Can someone please explain why ? Same thing did not work if I used system
like this system("smartctl -A /dev/sda > test.txt");