#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2009
    Posts
    149
    Rep Power
    37

    Execvp fails with EFAULT


    I'm working with Linux, and I'm trying to fork and execvp another program. I've discovered that my execvp fails, and that errno gets set to EFAULT.

    Relevant declarations:

    Code:
    	char qArgs[5][100], mArgs[8][100];
    	char *argvs[8];
    The execvp:

    Code:
    	pid = fork();
    	if(pid == 0) //child
    	{
    		//exec q
    		sprintf(&qArgs[0][0], "q");
    		sprintf(&qArgs[1][0], "%d", shmID);
    		sprintf(&qArgs[2][0], "%d", 0);
    		sprintf(&qArgs[3][0], "%d", k - 1); //size
    		qArgs[4][0] = '\0';
    		for(i = 0; i < 5; i += 1)
    			argvs[i] = &qArgs[i][0];
    		execvp("q", argvs); // {"q", shmID, start, end}
    		fprintf(stderr, "q exec failed!\n");
    		Error();
    		exit(EXIT_FAILURE);
    	}
    According to the man page, errno of EFAULT means "filename points outside your accessible address space." I'm not entirely sure what the problem is here.

    In the directory I'm executing from, I have executables "main" and "q". I've tried adding a "./" at the beginning of the executable string but I get the same result.
    Last edited by jakotheshadows; February 27th, 2013 at 03:00 AM.
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    Your last argv must be
    argv[i] = NULL;

    The last thing you set up is (in effect)
    argv[i] = "";

    Then you leave the rest of your pointers containing garbage, one of which execv barfs on.

    Comments on this post

    • jakotheshadows agrees : Thanks!
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper

IMN logo majestic logo threadwatch logo seochat tools logo