0

The aim of the program is to fork a new child process and execute a process which also has command line arguments. If I enter /bin/ls --help, I get the error:

shadyabhi@shadyabhi-desktop:~/lab/200801076_lab3$ ./a.out
Enter the name of the executable(with full path)/bin/ls --help
Starting the executable as a new child process...
Binary file to be executed: /bin/ls
/bin/ls: unrecognized option '--help
'
Try `/bin/ls --help' for more information.
Status returned by Child process: 2
shadyabhi@shadyabhi-desktop:~/lab/200801076_lab3$

What would be the right argument to execve()?

#include<stdio.h>
#include<string.h>      //strcpy() used
#include<malloc.h>      //malloc() used
#include<unistd.h>      //fork() used
#include<stdlib.h>      //exit() function used
#include<sys/wait.h>    //waitpid() used

int main(int argc, char **argv)
{
    char command[256];
    char **args=NULL;
    char *arg;
    int count=0;
    char *binary;
    pid_t pid;
    printf("Enter the name of the executable(with full path)");
    fgets(command,256,stdin);
    binary=strtok(command," ");
    args=malloc(sizeof(char*)*10);
    args[0]=malloc(strlen(binary)+1);
    strcpy(args[0],binary);
    while ((arg=strtok(NULL," "))!=NULL)
    {
        if ( count%10 == 0) args=realloc(args,sizeof(char*)*10);
        count++;
        args[count]=malloc(strlen(arg));
        strcpy(args[count],arg);
    }
    args[++count]=NULL;
    if ((pid = fork()) == -1)
    {
        perror("Error forking...\n");
        exit(1);
    }
    if (pid == 0)
    {
        printf("Starting the executable as a new child process...\n");
        printf("Binary file to be executed: %s\n",binary);
        execve(args[0],args,NULL);
    }
    else
    {
        int status;
        waitpid(-1, &status, 0);
        printf("Status returned by Child process: %d\n",WEXITSTATUS(status));
    }
    return 0;
}
Smi
  • 13,850
  • 9
  • 56
  • 64
shadyabhi
  • 16,675
  • 26
  • 80
  • 131
  • If the program lies in the same directory, ie the directory of a.out, the arguments are taken.... guyz pls help fast.. i have an assignment to submit.. – shadyabhi Feb 02 '10 at 17:45
  • 2
    That is not warranted...asking someone else to complete the assignment without even debugging it yourself! sigh...I have retagged your question as 'homework'...grrr – t0mm13b Feb 02 '10 at 17:55
  • i did the coding myself.. i was just unable to remove the bug... So, actually i tried doing it.. – shadyabhi Feb 02 '10 at 18:15
  • 1
    The error message from 'ls' gives you a hint - it includes a newline after the 'p' of '--help'. That's because the input string included the newline (fgets()). The shell normally removes newlines (and tabs, subject to the control of $IFS); your code did not, so 'ls' got an unrecognized option. – Jonathan Leffler Feb 02 '10 at 20:16

3 Answers3

3

The first entry in the args array should be the program name again. Your code calls /bin/ls with --help as the process name.

Geoff Reedy
  • 34,891
  • 3
  • 56
  • 79
  • What should be the value of execve arguments to get the code working properly.. is it .. binary="/bin/ls" & args[0]="/bin/ls" and args[1]="--help"... and args[0] and args[1] should be null terminated.?? – shadyabhi Feb 02 '10 at 18:17
1

Please check to make sure args is not getting clobbered by the realloc call. See here on SO regarding realloc

Edit: Also the loop looks funny.... You called strtok like this:

binary=strtok(command," ");

Change the loop construct to use binary instead as shown...

char *tmpPtr;
while (binary != NULL){
   if ( count%10 == 0) tmpPtr=realloc(args,sizeof(char)*10);
   if (tmpPtr != NULL) args = tmpPtr;
   count++;
   args[count-1]=malloc(strlen(binary)+1);
   strcpy(args[count-1],binary);  
   binary = strtok(command, " ");
}

And use the binary for copying the string....

Hope this helps, Best regards, Tom.

Community
  • 1
  • 1
t0mm13b
  • 34,087
  • 8
  • 78
  • 110
1

Your program has some obvious errors. For instance, declaring char **args=NULL; and then args=realloc(args,sizeof(char)*10); (since it's char**, you should be alloc-ing to char*, no?..).

Since sizeof(char*) is usually 4 while sizeof(char) is usually 1, you end up with some serious memory management problems around there (you alloc less than you use, and you end up writing where you shouldn't). From there on, all hell breaks loose and you can't expect your program's behavior to make any sense.

I'd suggest that you run your program through an util such as Valgrind to figure out memory leaks and correct the program appropriately. Probably your execve problems will disappear as soon as the memory problems are corrected.

Miguel Ventura
  • 10,344
  • 2
  • 31
  • 41
  • ok .. i changed the code a bit... now pls tell.. i think there is some problem in giving argument to execve() – shadyabhi Feb 02 '10 at 18:31