Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Question 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
    #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];
        int c_pid = fork();
        if (c_pid == 0) {
            // child        
            dup2(p1[0], 0);
            dup2(p2[1], 1);
            char *argv1[2];
            execvp(argv[1], argv1);
        } else {
            // parent
            struct tnl t1;
   = 0;
            t1.out = p1[1];
            struct tnl t2;
   = 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;
        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 {
    // Run example
    [u123<at>Sohani ~]$ ./myprog /bin/bash
    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)

    [u123<at>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<at>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.

    <at>: It is actually ascii chracter having value 64 (decimal)
    It denied to have such character.

    So please help.
    Thanks in advance.

  2. #2
    Linux Engineer
    Join Date
    Jan 2005
    Saint Paul, MN
    When you ssh to a host, you create a login session on that computer (even if it is localhost) and it executes the application passed (defaults to the specified shell for the login on the remote host). In your c program, you are running a shell that is not a login shell and hence "$PS1" does not get set (also lack of interactive may also enter the picture as well).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts