You are not logged in.

#1 2008-04-16 19:27:07

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Very Simple Question - How to Start a Programme with C?

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

#2 2008-04-16 19:33:32

jnengland77
Member
From: Black Hills, USA
Registered: 2005-05-06
Posts: 111

Re: Very Simple Question - How to Start a Programme with C?

Look at the system() function.  Should be able to do that with system("/etc/rc.d/moblock start").

Offline

#3 2008-04-16 19:49:34

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very Simple Question - How to Start a Programme with C?

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

#4 2008-04-16 19:52:39

neotuli
Lazy Developer
From: London, UK
Registered: 2004-07-06
Posts: 1,204
Website

Re: Very Simple Question - How to Start a Programme with C?

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

#5 2008-04-16 19:54:48

danielsoft
Member
From: Czech Republic
Registered: 2008-02-16
Posts: 102

Re: Very Simple Question - How to Start a Programme with C?

tony5429 wrote:

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

#6 2008-04-16 20:32:38

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very Simple Question - How to Start a Programme with C?

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

#7 2008-04-16 20:43:41

danielsoft
Member
From: Czech Republic
Registered: 2008-02-16
Posts: 102

Re: Very Simple Question - How to Start a Programme with C?

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

#8 2008-04-17 02:17:50

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very Simple Question - How to Start a Programme with C?

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

#9 2008-04-17 02:55:50

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Very Simple Question - How to Start a Programme with C?

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

#10 2008-04-17 05:50:12

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very Simple Question - How to Start a Programme with C?

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

#11 2008-04-17 11:47:28

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: Very Simple Question - How to Start a Programme with C?

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

#12 2008-04-17 15:40:28

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very Simple Question - How to Start a Programme with C?

Thank you very much for the information. I think I will just require that my program be run as root.

Offline

#13 2008-04-18 04:11:51

tony5429
Member
Registered: 2006-03-28
Posts: 1,017

Re: Very Simple Question - How to Start a Programme with C?

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

#14 2008-04-18 07:35:46

iphitus
Forum Fellow
From: Melbourne, Australia
Registered: 2004-10-09
Posts: 4,927

Re: Very Simple Question - How to Start a Programme with C?

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

#15 2009-03-22 17:12:08

quarkup
Member
From: Portugal
Registered: 2008-09-07
Posts: 497
Website

Re: Very Simple Question - How to Start a Programme with C?

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

#16 2009-03-22 17:13:59

bender02
Member
From: UK
Registered: 2007-02-04
Posts: 1,328

Re: Very Simple Question - How to Start a Programme with C?

bury the zombies...

Offline

Board footer

Powered by FluxBB