execv failed :(

This is a discussion on execv failed :( within the Linux Programming forums, part of the Platform Specific Boards category; Hello guys, I almost shame to ask, but can you help me with this. When doing this: Code: char* av[] ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    37

    execv failed :(

    Hello guys,

    I almost shame to ask, but can you help me with this.
    When doing this:
    Code:
    char* av[] = {
      "/bin/ls", "-al"
    }
    
    if(execv(av[0], av) <0)
      printf("Failed to execute command.\n");
    else printf("Yeah! It works!\n");
    This works fine and the command is executing fine. And the output seems to be correct, this means it shows all directories/files in the current directory and also "-l" properties.

    But when I do the following, the execution keeps failing:
    Code:
    char* avlong = {
      "~/Development/Projects/somecmd",
      "-c 604 -n 1 -f 550"
    }
    
    if(execv(avlong[0], avlong) <0)
      printf("Failed to execute command.\n");
    else printf("Yeah! It works!\n");
    My goal is to call a function for executing a process with options that is determined during runtime. The called process is my other self written program.
    I hope you can help me out, thanks.
    Last edited by Andaluz; 09-24-2009 at 03:49 AM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Call perror() to print out a text version of the error execv encountered.

    However, I'm going out on a limb and say that it doesn't find the executable. The reason is that replacing ~ by the home directory is a shell substitution, whereas execv wants the real path of the executable. Try execvp instead.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Thanks for your answer CornedBeef,

    You are right , it has to do with that tilde '~'.
    So I modified it to: /home/userme/Development/Projects/somecmd
    Code:
    char* avlong = {
      "/home/userme/Development/Projects/somecmd",
      "-c 604 -n 1 -f 550"
    }
    
    if(execv(avlong[0], avlong) <0)
      printf("Failed to execute command.\n");
    else printf("Yeah! It works!\n");
    But than the arguments seems not be recognized somehow.

    So I tried several ways, like:
    Code:
    char* avlong = {
      "/home/userme/Development/Projects/somecmd",
      "somecmd",
      "-c 604",
      "-n 1",
      -f 550",
      "\0"
    }
    
    //even this:
    char* avlong = {
      "/home/userme/Development/Projects/somecmd",
      "somecmd",
      "-c",
      "604",
      "-n",
      "1",
      "-f",
      "550",
      (char*) 0
    }
    However, the called program respond with missing argument.
    I'm just one step from succes
    thank you...

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Ok guys,

    I finally got it, though it was very very frustrating man.
    To execute a program with crazy options, try this out:
    Code:
    char* program = "/home/userme/Development/Projects/somecmd";
    char* arguments[] = {
      "somecmd",
      "-c",
      "604",
      "-n",
      "1",
      "-f",
      "550",
      (char*) 0
    };
    
    if(execv(program, arguments) <0)    //it works also with execvp() call.
      printf("Failed to execute command.\n");
    else printf("Yeah! It works!\n");
    This should work, if I haven't made some mistypes.
    Ok, you might ask yourself how I solved it. Well, sometimes you want to get your code done quick for simple tasks and don't want to waste to much time on it. But with exec() calls, if you didn't read the man carefully, you'll end up with a broken keyboard. So I calmed down, opened the Advanced C programming - Linux ebook and read the chapter about process carefully. Tried out the example code and taraaa, it works!

    At your service.
    Last edited by Andaluz; 09-24-2009 at 08:28 AM. Reason: Forgot '[]' at arguments.

  5. #5
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    I'd make one change. . .
    Quote Originally Posted by man execv
    The execv() and execvp() functions provide an array of pointers to null-terminated strings that represent the argument list avail-
    able to the new program. The first argument, by convention, should point to the filename associated with the file being executed.
    The array of pointers must be terminated by a NULL pointer.
    I would change
    Code:
    char* arguments[] = {
      "somecmd",
      "-c",
      "604",
      "-n",
      "1",
      "-f",
      "550",
      (char*) 0
    };
    to
    Code:
    char* arguments[] = {
      "somecmd",
      "-c",
      "604",
      "-n",
      "1",
      "-f",
      "550",
     NULL
    };
    but that is because I don't believe in the use of magic numbers.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    32
    Quote Originally Posted by Andaluz View Post
    Ok guys,

    I finally got it, though it was very very frustrating man.
    To execute a program with crazy options, try this out:
    Code:
    char* program = "/home/userme/Development/Projects/somecmd";
    char* arguments[] = {
      "somecmd",
      "-c",
      "604",
      "-n",
      "1",
      "-f",
      "550",
      (char*) 0
    };
    
    if(execv(program, arguments) <0)    //it works also with execvp() call.
      printf("Failed to execute command.\n");
    else printf("Yeah! It works!\n");
    This should work, if I haven't made some mistypes.
    Ok, you might ask yourself how I solved it. Well, sometimes you want to get your code done quick for simple tasks and don't want to waste to much time on it. But with exec() calls, if you didn't read the man carefully, you'll end up with a broken keyboard. So I calmed down, opened the Advanced C programming - Linux ebook and read the chapter about process carefully. Tried out the example code and taraaa, it works!

    At your service.
    While reading man page is very useful thing to do, there is also something else that make life a lot easy - compile you program with warnings at least with -Wall and make sure you fixed all warnings.

    This way compiler will tell you where to look.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    37
    Hello guys,

    Thanks for your replies.

    Kennedy, it shouldn't make any difference.
    Yes, thanks for the tip. I just took it from the examples. Anyway, the main thing is, I think, is to make sure you have a null-pointer.

    Valery Reznic, that's a good tip. Normally I'm used to work on Visual Studio, but since I work with KDevelop, I had to learn a lot of new things. But I totally forgot the -Wall option. Cause I don't like to have warnings, I want it superclean

    Thank you!.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Find Injected DLLs In A Process?
    By pobri19 in forum Windows Programming
    Replies: 35
    Last Post: 02-06-2010, 08:53 AM
  2. Deleting an object if the constructor failed.
    By g4j31a5 in forum C++ Programming
    Replies: 31
    Last Post: 10-03-2009, 08:52 PM
  3. Thread Prog in C language (seg fault)
    By kumars in forum C Programming
    Replies: 22
    Last Post: 10-09-2008, 01:17 PM
  4. C++ text file
    By statquos in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2008, 01:42 PM
  5. execv and defunct process
    By groorj in forum C Programming
    Replies: 5
    Last Post: 08-05-2005, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21