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

Discussion in 'C Programming' started by msigwald@gmail.com, Dec 11, 2005.

  1. Guest

    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, 2005
    #1
    1. Advertising

  2. Artie Gold Guest

    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!"
     
    Artie Gold, Dec 11, 2005
    #2
    1. Advertising

  3. Flash Gordon Guest

    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.
     
    Flash Gordon, Dec 11, 2005
    #3
  4. [OT] Re: Problem with "system" and the linux "su" command

    On Sun, 11 Dec 2005 09:34:52 -0800, 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
     
    Kleuskes & Moos, Dec 11, 2005
    #4
  5. >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
     
    Gordon Burditt, Dec 11, 2005
    #5
  6. SM Ryan Guest

    "" <> 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.
     
    SM Ryan, Dec 12, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Irmen de Jong
    Replies:
    2
    Views:
    449
    =?ISO-8859-1?Q?Gerhard_H=E4ring?=
    Aug 12, 2003
  2. boriq
    Replies:
    4
    Views:
    625
    Tim Golden
    Jun 12, 2008
  3. madhav
    Replies:
    1
    Views:
    2,217
    birdsong
    Feb 24, 2009
  4. James Dinkel
    Replies:
    6
    Views:
    255
    James Dinkel
    May 1, 2008
  5. fmbright
    Replies:
    8
    Views:
    227
Loading...

Share This Page