LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-17-2007, 01:50 AM   #1
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Rep: Reputation: 24
Lightbulb pseudo terminal - scp problem


hy frnds,

I am having a problem regarding pseudo terminal and scp command.

Let me explain the prob in brief...

I am developing a program which copies files to client pcs one by one.
For example I have one server and 5 client nodes.

and I have one "xmms-1.4.i386.rpm" in server, which i have to copy in all the 5 nodes. (for copying i am using "scp") without user interaction.

So I have implemented the pseudo terminal idea in my program.

Client IP and password id stored in a simple text file so I can directly read it, and can supply it in pseudo terminal.

but the problem is tht after copying the file the control is not returning back.

To call the "makeSecureCopy()" function u just need to read IP and passwd from text file one by one and then call the function.

I am using RHEL 4, 5, Redhat 9 and FC6 distro.

here is the code...

Code:
int makeSecureCopy (char *ip, char *pass, char *sSrcRPM )
{
        int ptm=0, i=0;
        char pts[MAX]="", buf[MAX*10]="";
        pid_t pid;

        makeCommand (sSrcRPM, ip /*HostIP*/,pass /*HostPass*/);
        fprintf(stderr," About to install in ip = %s \n",ip);

        if (((pid = forkpty (&ptm, pts, NULL, NULL))) == 0)     //child
        {
                execl ("/bin/bash", "/bin/bash", NULL);
                exit (1);
        }
        else
        {
                sleep (1);              //let the child run first

                fcntl (ptm, F_SETFL, fcntl (ptm, F_GETFL) | O_NONBLOCK);

                for (i = 0; i < TOT_CMD; ++i)
                {
                        bzero (buf, sizeof (buf));
                        while (!(read (ptm, buf, sizeof (buf)) == -1 && errno == EAGAIN))
                        {
                                sleep (3);
                                fprintf (stderr, "%s\n", buf);
                                bzero (buf, sizeof (buf));
                        }
                        write (ptm, cmd[i], strlen (cmd[i]));
                        write (STDOUT_FILENO, cmd[i], strlen (cmd[i]));
                        sleep (3);
                }

                bzero (buf, sizeof (buf));
                while (!(read (ptm, buf, sizeof (buf)) == -1 && errno == EAGAIN));
                        fprintf (stderr, "waiting...");
                wait (0);
        }
        fprintf(stdout,"Copying completed on %s client\n",ip);

return SUCCESS;
}

void makeCommand (char *rpm, char *ip, char *pass)
{
        strcpy(cmd[0],"");
        strcpy(cmd[1],"");
        strcpy(cmd[2],"");

        sprintf (cmd[0], "scp %s%s root@%s:%s\n", SERVER_PATCH_PATH, rpm, ip, CLIENT_PATCH_PATH);
        sprintf (cmd[1], "%s\n", pass);

        sprintf (cmd[3], "exit\n");

        return;
}
any kind of help will be appreciated.
plz help me.......
 
Old 08-17-2007, 08:59 PM   #2
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
I can't answer your question, but why do you do it that
way to begin with?
Just set-up a system user account with a login that's
disabled for ordinary logins; give him a password-less
login on the target machines (via public keypairs) and
do the job in a bash script ... no need to store passwords
in a flat-file.
Code:
for i in host1 host2 host3 host4 host5
do
  scp file $i:/path/to/target
done

Cheers,
Tink
 
Old 08-18-2007, 04:20 AM   #3
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Original Poster
Rep: Reputation: 24
thnx Tinkster

hi Tinkster

Thx 4 ur replay....and I am appreciate ur sudgetion, but I can not use shell scripting in my software so, If u have any other solution of my C program then plz replay me.

Thanking you once again.
 
Old 08-19-2007, 10:51 AM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
What control are you talking about? Do you mean control of the terminal or are you saying the function doesn't return? Are you wanting the terminal to go back to the session leader? If so, you can't block SIGTTIO/SIGTTOU in the session leader because it will fail. It's safe to leave them unblocked ONLY in the session leader since it can revoke the terminal from anyone else without blocking or stopping. If you aren't talking about this, please specify.
ta0kira

Last edited by ta0kira; 08-20-2007 at 08:18 AM.
 
Old 08-19-2007, 08:13 PM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,361

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Actually, using rsync would enable you to do no programming.
Also, remember that rsync can use the ssh protocol if you need on-the-fly encryption.
 
Old 08-20-2007, 01:22 AM   #6
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Original Poster
Rep: Reputation: 24
dear ta0kira,

Thnx 4 replay, Let me explain my prob in detail.

wen u fork a process, then in parent u can wait for the child using wait() or waitpid() syscall.

In my given code I am waiting for the forked child (the child part in which i m execl the pseudo terminal "/bin/bash") but the child is not returning back and we control encounters the wait() syscall, it pauses and waits for infinite times (in short my prog pauses at this line, and not returning back for the next iteration)

This is the actual problem i m currently facing....

and ya, control doesnt returning means child process does not returning back in parent process.
so i can not get the exit status of child
. If I got the exit status of child the problem is solved form me...

plz replay me...

btw thnk u very much 4 replay..

Last edited by vdx; 08-20-2007 at 01:34 AM.
 
Old 08-20-2007, 08:22 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
When the child process execs, it leaves the process group of the parent. To open a new controlling terminal it needs to be in its own session, so the problem is likely that the child process is no longer a child of the parent, so the parent can't waitpid it.
ta0kira
 
Old 08-20-2007, 09:17 AM   #8
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Original Poster
Rep: Reputation: 24
Wink

thnx ta0kira

thnk u very much 4 ur replay.

I got ur point very clear. And also finding solution for the same.

thnk u very much once again.
 
Old 08-22-2007, 12:13 AM   #9
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Original Poster
Rep: Reputation: 24
dear ta0kira

u said tht, "When the child process execs, it leaves the process group of the parent".

Thts ok, but I think tht If an execl process is setting sid bit for it, only then it can leave process group id of parent.

isn't it ??

and if it is or not then wt can be the solution ???
 
Old 08-22-2007, 03:37 AM   #10
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I think you are thinking of "session" in place of "group". For example, when you have a command line with piped output each part of the command line is a different process, but they are all in the same group. They can be put into and taken out of the foreground as a group. All of the processes the shell has control over are a session, which can consist of many groups. A process can't have a different session without using setsid (in one form or another,) but can be moved from group to group by its session leader. When a fork execs, it takes on its own group so that signals to the session leader don't go directly to it. The session leader can then link all of the piped commands together and give it a single process group so pausing/continuing/fg/etc. affect all of them at once.
ta0kira

Last edited by ta0kira; 08-22-2007 at 03:52 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simulating pseudo master-slave terminal sytem kshkid Programming 2 03-27-2006 07:58 AM
Regarding Pseudo tty, Pseudo terminals ? mqureshi Programming 0 07-30-2005 10:51 AM
Switch to pseudo terminal you didn't log out of? xnomad Linux - General 2 07-19-2005 12:35 AM
Questions about (Pseudo)Terminal devices beginner16 Programming 0 04-09-2004 10:03 AM
Pseudo Terminal Problems ailiez Programming 2 02-09-2004 01:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration