1

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");

Community
  • 1
  • 1
3bdalla
  • 407
  • 1
  • 10
  • 28
  • 1
    this is not an issue with the the smartctl tool but rather with the permission of the underlying block device you are trying to acces, in your case "./dev/sda". either change the permissions to what you need or just run your tool with sudo in front of it. What is it you are trying to accomplish? – rowan.G Mar 03 '15 at 10:01
  • 1
    I'm trying to read the SMART values of the device as part of an application. No library helped us so we decided to use the command line. BTW part of the requirement is not running as root thats why I need to use capabilities. – 3bdalla Mar 03 '15 at 10:25
  • @3bdalla Then you probably need CAP_DAC_OVERRIDE (on the smartctl executable), although controlling access though the group permissions on the /dev/sda "file" would provide tighter security. – nos Mar 03 '15 at 12:03
  • Why on the smartctl executable ? Isn't there any cap. that I can apply to the application ? – 3bdalla Mar 03 '15 at 13:11
  • I tried `setcap 'CAP_DAC_OVERRIDE=+ep' /usr/sbin/smartctl` and it did not work. – 3bdalla Mar 03 '15 at 13:30
  • Where is /dev/sda file located ? – 3bdalla Mar 03 '15 at 13:31
  • that is the path. it is a device file. located in the "/dev" directory. – rowan.G Mar 03 '15 at 21:51
  • I changed the permissions to 666 and changed ownership also of /dev/sda and it did not work. It keeps saying "smartctl: command not found" when running it as non-root – 3bdalla Mar 04 '15 at 07:10

0 Answers0