Fedora Linux Support Community & Resources Center
  #1  
Old 18th January 2013, 09:51 PM
abhics1 Offline
Registered User
 
Join Date: Oct 2010
Posts: 22
linuxchrome
Run bash as a child process in C having same effect as do ssh has

I want to run bash as child process of my C program.
Few conditions
1) My program will handle both STDIN and STDOUT of bash.
2) No care about practical use.

I have done it but it does not looks like bash.

Here is my code
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/wait.h>

// for tunneling the data
struct tnl {
    int in;
    int out;
};

// for tunneling the data
void *do_tnl(void *ptr);

int main(int argc,char **argv) {
    int p1[2];
    int p2[2];
    pipe(p1);
    pipe(p2);
    int c_pid = fork();
    if (c_pid == 0) {
        // child        
        dup2(p1[0], 0);
        close(p1[1]);
        dup2(p2[1], 1);
        close(p2[0]);
        char *argv1[2];
        argv1[0]=argv[1];
        argv1[1]=NULL;
        execvp(argv[1], argv1);
    } else {
        // parent
        close(p1[0]);
        close(p2[1]);
        struct tnl t1;
        t1.in = 0;
        t1.out = p1[1];
        struct tnl t2;
        t2.in = p2[0];
        t2.out = 1;

        pthread_t thread1, thread2;
        int iret1, iret2;
        iret1 = pthread_create(&thread1, NULL, do_tnl, (void*) &t1);
        iret2 = pthread_create(&thread2, NULL, do_tnl, (void*) &t2);
        pthread_join(thread1, NULL);
        pthread_join(thread2, NULL);
        printf("Thread 1 returns: %d\n", iret1);
        printf("Thread 2 returns: %d\n", iret2);
        int st;
        wait(&st);
    }
    return 0;
}

void *do_tnl(void *ptr) {
    struct tnl *t = ptr;
    while (1) {
        int buf[1];
        int r = read(t->in, buf, 1);
        if (r > 0) {
            write(t->out, buf, 1);
        } else {
            break;
        }
    }
}
// Run example
[u123@Sohani ~]$ ./myprog /bin/bash
pwd
/home/u123
who
u123 :0 2013-01-16 03:31 (:0)
u123 pts/0 2013-01-18 23:23 (:0)
u123 pts/1 2013-01-19 03:04 (:0)
u123 pts/3 2013-01-19 02:31 (:0)
exit

[u123@Sohani ~]$
// Run example end
So as you can see that it is working. I can now run any command(e.g. here I tun pwd, who, exit). But the problem is that
1) The output is not color full.
2) There is no "[u123@Sohani ~]$" part in bash.

So I want the same effect as it happen when I run bash normally in terminal.

I am sure that it is possible because I can run a ssh-server(sshd) and ssh-client(ssh) with normal privilege and there I have bash running like it run normally.

I have searched on net. And openssh code is too large.

I am using Fedora 17 64 bit.

So please help.
Thanks in advance.
Reply With Quote
  #2  
Old 18th January 2013, 11:09 PM
ocratato Online
Registered User
 
Join Date: Oct 2010
Location: Canberra
Posts: 540
linuxfirefox
Re: Run bash as a child process in C having same effect as do ssh has

It would appear that bash has dropped out of interactive mode, since its stdin and stderr are no longer ttys - see the man page for bash. Suggest that you pass -i to bash.
__________________
Don't tell me "The sky is the limit" when there are footprints on the moon.
Reply With Quote
Reply

Tags
bash, child, effect, pipe, process, run, ssh

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
There was an error creating the child process for this terminal Vector Using Fedora 6 15th December 2008 07:41 AM
-bash: child setpgid (22473 to 22473): No such process tarkhorani Using Fedora 0 14th July 2007 12:50 PM
There was an error creating the child process - xtterm voipfc Using Fedora 1 24th November 2006 12:50 AM


Current GMT-time: 17:14 (Sunday, 19-05-2013)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat