1

I'm playing around with sycalls, and have just written codes along the lines of

/* Clone the process and launch Something */
child_stack = (void *) malloc(max_memory);
args = malloc(2 * sizeof (int **));

args[0] = in_socket_fds;
args[1] = out_socket_fds;

clone(foo, child_stack + max_memory ,CLONE_FILES|CLONE_VM|CLONE_IO|CLONE_FS, (void *) args);

wait();

where the foo function is :

    int foo(int** args) {
     fprintf(stderr, "Hello world %d %d", args[0][0], ags[1][0]);
     _exit(0);
    }

What I'd really like to do is call a binary from the foo function.

    int foo(int** args) {
     system(start program foo)
     _exit(0);
    }

This binary should share the same file descriptor table (hence the use of clone previously). The code above is incorrect, since I don't think system allows to share the file descriptor table. ( I don't want copies of the fds, I really want to be able to read/write them) These fds are sockets.

    int foo(int** args) {
     system(start program foo)
     _exit(0);
    }

How can I make the clone command call a binary rather than a function? (is this even possible), or use the system call whilst sharing file descriptors?

Thanks

1 Answer 1

4

Go for fork() / execlp() instead of clone() / system(). A forked child inherits the file descriptors from the parent, and execlp() (or its relatives) allow you to load a different executable in the child.

You probably could exec*() in the function called by clone() (see comments), but in this case the fork() semantics are much simpler.

9
  • I was under the impression that fork inherited copies of the file descriptors, as opposed to clone who actively shared the table Jul 16, 2012 at 10:50
  • @user1018513 : After fork() call, your socket file descriptors are usable also in the child. So any table sharing is not needed.
    – SKi
    Jul 16, 2012 at 10:59
  • @user1018513: You could use clone() to create a process executing a function, which in turn calls execlp() (or one of its relatives). However, in this case fork() is the easier option. The documentation of clone() also leaves me scratching my head quite a bit about how it could / would cooperate with exec*()...
    – DevSolar
    Jul 16, 2012 at 12:15
  • Thanks, yes, I'd misunderstood how excelp() worked (I thought the fork call happened within the excel). I just used clone followed by exec as you suggested (as I need the child to close the fd I can't just use fork). Thank you. Jul 16, 2012 at 12:57
  • @user1018513: Erm... a child can close fd's inherited through the fork() alright...?!?
    – DevSolar
    Jul 16, 2012 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.