views:

625

answers:

4

I would like to prepare an old-school argument vector (argv) to use within the function

int execve(const char *filename, char *const argv[],char *const envp[]);

I tried it with the stl::vector class:

std::string arguments = std::string("arg1");    
std::vector<char*> argv; 
char argument[128];
strcpy(argument, arguments.c_str());
argv.push_back(argument); 
argv.push_back('\0'); // finish argv with zero

Finally I pass the vector to execve()

execve("bashscriptXY", &argv[0], NULL)

The code compiles but ArgV gets "ignored" by execve(). So it seems to be wrong, what I'm trying. How should I build an argV in a efficient way with c++?

+4  A: 

Be ware that argv[0] is always the name of the command itself. So if you want to pass 1 parameter to a program, you have to fill two elements:

argv[0] should be "bashscriptXY" (or whatever you want...) argv[1] = "your_argument"

rossoft
+1  A: 

Unless the arguments are hard-coded, you'll have to do some dynamic memory allocation. First, create an array of character pointers for the number of arguments, then allocate memory for each argument and copy the data in.

This question is very similar and includes some code solutions.

qid
"execve() does not return on success, and the text, data, bss, and stack of the calling process are overwritten by that of the program loaded. The program invoked inherits the calling process's PID, and any open file descriptors that are not set to close on exec." - I think dynamic allocation would leak, I don't think execve free()s the argv char*s
joshperry
@joshperry: I think that's OK, though. You expect a program's argv array to be "leaked", in the sense that the C runtime (in this case the OS) does it for you at program (process) exit. An OS which doesn't clean up leaked resources for you on exit probably isn't going to implement any of the exec functions to actually do anything.
Steve Jessop
+2  A: 

I think the char[128] is redundant as the string local will have the same lifetime, also, try adding the program as argv[0] like rossoft said in his answer:

std::string arguments = std::string("arg1");    
std::vector<char*> argv;

argv.push_back("bashscriptXY");
// The string will live as long as a locally allocated char*
argv.push_back(arguments.c_str()); 
argv.push_back(NULL); // finish argv with zero

execve(argv[0], &argv[0], NULL);
joshperry
For "finish argv with zero" I'd prefer "argv.push_back(0)" or "argv.push_back(NULL)". The "argv.push_back('\0')" leads to the correct result, but makes you think too much about what is happening here - and there is the danger of mis-writing it as "argv.push_back("\0")" which would be wrong...
hjhill
Agreed, I'm a fan of saying what you mean.
joshperry
"...as the string local will have the same lifetime..." It has indeed, but changes to the string after having taken its `c_str()` could be disastrous. So you might want to make it a `const` string.
sbi
+2  A: 

A few issues:

  • the first element of the argv array is supposed to be the program name
  • the argv vector can take char const* types as execve() doesn't modify the args (though the process that gets invoked might - but those will be copies in its own address space). So you can push c_str() strings onto it.

Try this out:

std::string program = std::string( "bashscriptXY");
std::string arg1("arg1");
std::string arg2("arg2");

std::vector<char const*> argv; 

argv.push_back( program.c_str());
argv.push_back( arg1.c_str()); 
argv.push_back( arg2.c_str()); 
argv.push_back( NULL); // finish argv with zero

intptr_t result = execve( program.c_str(), &argv[0], NULL);
Michael Burr
This fails to compile here without adding a const_cast. "invalid conversion from "const char**" to "char* const*".
Parker