You are not logged in.
I am sure this is quite easy to do. I am trying to get an application I am writing in C to start a separate application, whenever it runs...
int main(void)
{
/etc/rc.d/moblock start
return 0;
}
Does anyone know how to do this?
Offline
Look at the system() function. Should be able to do that with system("/etc/rc.d/moblock start").
Offline
I figured it would be easy. Thanks! Now, if I run the programme I am writing as non-root, is therre a way for system("/etc/rc.d/moblock start") to work as root, or do I need to run the programme I am writing as root?
Offline
well you can run it as non-root and setuid it root (which means it will effectively run as root). However this is generally consider security-stupid (although there are plenty of programs that do it and drop root as soon as possible... for example mpd).
You could try using sudo...
The suggestion box only accepts patches.
Offline
I figured it would be easy. Thanks! Now, if I run the programme I am writing as non-root, is therre a way for system("/etc/rc.d/moblock start") to work as root, or do I need to run the programme I am writing as root?
normally the program will run as the user who executes the program. if you want, being non-root, to run a program as root, you can use the suid bit (http://wiki.linuxquestions.org/wiki/Suid) or the "sudo" mechanism. both is a security risk.
may the Source be with you
Offline
Okay; this is what I've got now. I'm away from my computer right now and cannot test it, but I have a hunch I am doing this completely wrong...
#include <unistd.h>
int main(void) {
setuid(pwd->pw_uid);
system("/etc/rc.d/moblock start");
return 0;
}
Offline
no... compile the source and run chmod +s to the resulting binary, which should be chowned to root
may the Source be with you
Offline
Hrm.... I am getting these errors...
bash-3.2# gcc -o /usr/bin/karamblock /home/karam/Desktop/karamblock.c
/home/karam/Desktop/karamblock.c: In function 'main':
/home/karam/Desktop/karamblock.c:4: error: 'pwd' undeclared (first use in this function)
/home/karam/Desktop/karamblock.c:4: error: (Each undeclared identifier is reported only once
/home/karam/Desktop/karamblock.c:4: error: for each function it appears in.)
with this code...
#include <unistd.h>
int main(void) {
setuid(pwd->pw_uid);
system("/etc/rc.d/moblock start");
return 0;
}
Offline
Why are you doing this:
setuid(pwd->pw_uid);
What are you trying to accomplish with that line?
Are you actually trying to learn code, or just make an easy way to run your rc script as root? If it's the latter, then just add
alias runmoblock="sudo /etc/rc.d/moblock start"
to your .bashrc and then use 'runmoblock' to run it.
Offline
I am sorry as I know I am missing something here. I was trying to set the user ID to root so that I can run that line as root even if I am running the application itself as non-root. I would prefer to use the setuid() if possible. I just do not know how... Can anyone help me?
Offline
You cannot do this. If it was that easy to just take root privilege, then the whole point of root would be lost - I could write a 5 line program that wiped out your entire system, no matter what user you ran it as.
Like other people have said, you have to sudo chmod u+s /path/to/compiled/application to get the desired result, or _just_use_sudo_ in the first place to run it.
Offline
Thank you very much for the information. I think I will just require that my program be run as root.
Offline
Is there a way to run something in the background? When I run system("/etc/rc.d/moblock start");, that process only ends if the user pushes CTRL+C. I would like to be able to execute that line in my own programme and then let the code continue while that system() line is running...
Offline
At a shell.... append '&' to the command
eg:
firefox &
xterm &
and... use sudo! not the C program. Life will be easier!
Last edited by iphitus (2008-04-18 07:36:09)
Offline
bash scripting would do the job easily.
If people do not believe that mathematics is simple, it is only because they do not realize how complicated life is.
Simplicity is the ultimate sophistication.
Offline
bury the zombies...
Offline