You're not going to be able to cleanly do this on Linux** because the idea of escalating privileges doesn't really exist in Linux. sudo
is the de facto answer to privileged execution, but all that's really doing is running a single command as root, which is conceptually distinct from running a command as the same user but with escalated privileges.
Consider the fact that even baseline sudo
(not even considering gksudo
or its like) is just a userspace application and not a core component of Linux. Someone on Arch, for example, might have not ever installed it if they prefer to just do things with su
.
This philosophy contrasts sharply with OS X and Windows, which are desktop-focused and thus find it convenient to blur the line between administration and regular usage. Because Linux uses the same kernel for both desktop and server distributions, the heightened security requirements of the server environment tend to dominate policies around these kinds of things at the kernel level and so you're not going to get a solution that isn't specific to some class of desktop-oriented distributions.
There's also a cultural difference; Linux users tend to be the kind of people who want fine grained control over how to run things and most would probably view something that restarts itself with root privileges as a form of virus :P
Easy answer: throw out an error message saying the application needs root privileges, exit with a non-zero error code, call it a day. That's more or less the "expected" behavior in Linux-land.
Hack-y answer: if you're going to be running in a graphical environment then link gksudo
as a dependency along with gtk
(or kdesudo
if you're using qt
or something, etc etc) and then use it to prompt for escalation. Even if it's not installed by default, you'll get to leverage Linux distros' fancy package management to get it easily installed without any extra effort on your part of the user's. In a non-graphical environment you're just out of luck.
** At least, not easily enough for it to be worth it for just one application, especially considering your application can then become an attack vector for anyone trying to write a new rootkit.