Problem with "system" and the linux "su" command

M

msigwald

I'm trying to write a little C program to run under linux, which must
gain root so it can then shutdown the computer. I can get the code to
execute the su command via system("su"), but once su comes into action,
my program it's halted and su takes over. Because of this, I can't get
my program to write the password su asks. I habe no idea on how to get
C to communicate with other running processes such as su, but I
understand it can be done. Any ideas on how I can achieve this?


Martin Sigwald
 
A

Artie Gold

I'm trying to write a little C program to run under linux, which must
gain root so it can then shutdown the computer. I can get the code to
execute the su command via system("su"), but once su comes into action,
my program it's halted and su takes over. Because of this, I can't get
my program to write the password su asks. I habe no idea on how to get
C to communicate with other running processes such as su, but I
understand it can be done. Any ideas on how I can achieve this?


Martin Sigwald
Three pieces of advice:

1) This question is off topic for Although system() is
a function in ISO standard C, its behavior depends on the particular
system upon which it is run.

2) As it's a Linux question, would
be the place to ask. Go there.

3) Even better, considering what you're trying to do, *don't*.

HTH,
--ag
 
F

Flash Gordon

I'm trying to write a little C program to run under linux, which must
gain root so it can then shutdown the computer. I can get the code to
execute the su command via system("su"), but once su comes into action,
my program it's halted and su takes over. Because of this, I can't get
my program to write the password su asks. I habe no idea on how to get
C to communicate with other running processes such as su, but I
understand it can be done. Any ideas on how I can achieve this?

This is highly system specific so you need to ask in a Linux or Unix
group, such as comp.unix.programmer, however I'm pretty sure you are
using completely the wrong approach.
 
K

Kleuskes & Moos

I'm trying to write a little C program to run under linux, which must
gain root so it can then shutdown the computer. I can get the code to
execute the su command via system("su"), but once su comes into action,
my program it's halted and su takes over. Because of this, I can't get
my program to write the password su asks. I habe no idea on how to get
C to communicate with other running processes such as su, but I
understand it can be done. Any ideas on how I can achieve this?

Your question is very linux specific and off topic to this newsgroup.
However, i think you should consider using 'sudo'. Spawning an 'su'
process (if I understand you correctly) will not get you where you want
to be.

Just as an aside, there are perfectly good system tools to shut your
system down ('shutdown -h now' for instance).

Check comp.os.linux.development.*. You'll get more answers there, i think.
 
G

Gordon Burditt

I'm trying to write a little C program to run under linux, which must
gain root so it can then shutdown the computer. I can get the code to
execute the su command via system("su"), but once su comes into action,
my program it's halted and su takes over.

I don't know of any implementation of system() which *DOESN'T*
suspend the program that called it and run the program it was
directed to, and not unsuspend the program that called it until the
called program finishes or aborts, unless the shell is specifically
asked to put the program in the background, in which case it's not
supposed to take input from stdin.
Because of this, I can't get
my program to write the password su asks.

popen() is not standard C, and I doubt it would work in this
case anyway.

Most implementations of su ask for the password from /dev/tty, not
stdin, so there's no place to write such a thing. The user is
supposed to TYPE it. Dictionary attacks are not encouraged.

Gordon L. Burditt
 
S

SM Ryan

# I'm trying to write a little C program to run under linux, which must
# gain root so it can then shutdown the computer. I can get the code to
# execute the su command via system("su"), but once su comes into action,

If you want to run interactive sub-processes, you might do better
using expect scripting. You can get more information from comp.lang.tcl.
You can run expect from system or popen in C program, if needs must.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top