By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,376 Members | 1,559 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,376 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Dec 11 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
ms******@gmail.com wrote:
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 news:comp.lang.c. 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, news:comp.os.linux.development.apps would
be the place to ask. Go there.

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

HTH,
--ag

--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com (new post 8/5)
http://www.cafepress.com/goldsays
"If you have nothing to hide, you're not trying!"
Dec 11 '05 #2

P: n/a
On Sun, 11 Dec 2005 09:34:52 -0800, ms******@gmail.com wrote:
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.
Martin Sigwald


Dec 11 '05 #3

P: n/a
ms******@gmail.com wrote:
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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Dec 11 '05 #4

P: n/a
>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
Dec 11 '05 #5

P: n/a
"ms******@gmail.com" <ms******@gmail.com> wrote:
# 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.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Quit killing people. That's high profile.
Dec 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.