1

I have this C code that runs smartctl command and takes its output:

#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;
}

The machine is openSUSE 13.2. My problem is it requires root privilege, but I don't want to switch to root.

What I tried and did not work:

  1. Added CAP_SYS_ADMIN capability to the program executable.
  2. Added CAP_DAC_OVERRIDE to the smartctl executable (as suggested here) and I also added it to the program executable.
  3. I used setcap 'cap_dac_override=+ep' cpptest to add the capability (also for the CAP_SYS_Admin) but when I use getcap I get only one of them, not both. (Can you help me in this ?)
  4. I changed the permission of /dev/sda to 755, 777, 766 and changed ownership of the device to my group using chown root:users /dev/sda.

I wrote a program that performs some network operations using sockets and it required root privilege but I added CAP_NET_ADMIN and it worked. I just don't know why capabilities does not work.

This is really important issue. Any help is really appreciated.

Please note: I provided a C code but this is an issue with Linux not a programming problem.

  • I wonder would it help if you changed your executable owner to root:users and set SUID bit? – UVV Mar 4 '15 at 8:01
  • Using chown root:users cpptest ? – 3bdalla Mar 4 '15 at 8:02
  • Yes, and then chmod u+s cpptest – UVV Mar 4 '15 at 8:03
  • No it did not work. I get sh: smartctl: command not found – 3bdalla Mar 4 '15 at 8:04
  • 1
    How is it supposed to find your smartctl if it's not in your current directory? – UVV Mar 4 '15 at 8:06
1

According to this discussion CAP_SYS_RAWIO capability needs to be applied to smartctl executable.

  • You have my respect! Thanks for the help! :D – 3bdalla Mar 4 '15 at 10:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.