0

command line (works fine)

$ sudo chPermissions.sh

trying to do this from within a Qt program, using QProcess and have tried the following without success

code:

QString program = "/bin/sh /usr/bin/chPermissions.sh";
m_process->start(program);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 0 (the script ran but without sudo rights & didn't work!)

code:

QString program = "sudo /bin/sh /usr/bin/chPermissions.sh";
m_process->start(program);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 1

code:

QString program = "/usr/bin/sudo /bin/sh /usr/bin/chPermissions.sh";
m_process->start(program);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 1

code:

QString program = "/bin/sh";
QStringList arguments;
arguments <<  "/usr/bin/chPermissions.sh";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 0 (the script ran! no sudo rights)

code:

QString program = "/bin/sh";
QStringList arguments;
arguments << sudo <<  "/usr/bin/chPermissions.sh";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 127 (command not found)

code:

QString program = "/bin/sh";
QStringList arguments;
arguments << /usr/bin/sudo <<  "/usr/bin/chPermissions.sh";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 2 (Misuse of shell builtins)

code:

QString program = "/bin/bash";
QStringList arguments;
arguments <<  "-c" << "\"/usr/bin/sudo /usr/bin/chPermissions.sh"\";
m_process->start(program, arguments);
m_process->waitForFinished();
qDebug() << m_process->exitCode();

result: 127

code:

QString shellCommandLine = "/usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();

result: 0 (the script ran!)

code:

QString shellCommandLine = "sudo /usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();

result: 1

code:

QString shellCommandLine = "/usr/bin/sudo /usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();

result: 1

code:

QString shellCommandLine = "/usr/bin/chPermissions.sh";
QStringList arguments;
arguments << "-c" << shellCommandLine;
m_process->start("/bin/sh", arguments);
m_process->waitForFinished();
qDebug() << "Exit Code: " << m_process->exitCode();

result: 0 (script ran without sudo rights)

Anyone able to let me know the secret of how to do this? (or put me out of my misery if it's not possible)

TIA

Andy

  • You need to provide some means of supplying a password to `sudo`. Have a look at the `--askpass` option in the man page. – G.M. May 01 '20 at 14:08
  • I have the following line in sudoers `andy ALL=(root) NOPASSWD: /usr/bin/abPermissions.sh` . I thought this would stop the request being made? Also wouldn't expect the code to finish instantly (rather hang for 30 seconds until `waitForFinished()` timed out.) – OrangeReaper May 01 '20 at 14:54

1 Answers1

0

Not solved as such.

I got round this by

  1. using one of the options that actually runs the script (without su rights), and
  2. granting permission in sudoers, to user, to run the command in the script without password prompt.

hope that makes sense.

not happy, but it works.