PDA

View Full Version : Executing linux command as root



stefan
15th August 2011, 07:37
I want to execute a linux command as a root user, from my C++/Qt code. Ultimately a a dialog requesting root pass should be implemented, but for no I can hard-code root password.

This is what I done so far:

QProcess p;
p.start( "dmidecode" );
p.waitForFinished(-1);

QString p_stdout = p.readAllStandardOutput();
QString p_stderr = p.readAllStandardError();
And it is working for commands that do not request root privileges. But I want to implement commands like "zypper up" or "dmidecode" which I can't execute without a root password.

Probably something can be done with void QProcess::setEnvironment ( const QStringList & environment )?

Thanks

NOTE: Also posted on stackoverflow (http://stackoverflow.com/questions/7036588/cqt-executing-linux-command-as-root)

wysota
15th August 2011, 15:07
It's best if you use PolicyKit (http://hal.freedesktop.org/docs/PolicyKit/) but you can also call kdesu (http://www.linfo.org/kdesu.html) or alike if it fits your purpose.