Results 1 to 4 of 4
here is my working code :
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
int main(int argc, char** argv)
{
int ...
- 07-16-2010 #1Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
[SOLVED] open() syscall breaks execve()'s work
here is my working code :
Threre are no problems with it. But if I insert open() syscall in my code before execve(), like thisCode:#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <errno.h> int main(int argc, char** argv) { int fd; char *envp[] = { "DOCROOT=.", "HOME=/home/schmidt", "INVOKER=CGILauncher-0.01" }; char *newargv[] = { NULL }; execve(argv[1], newarg, envp); // int code = execle(argv[1], NULL, envp); /* execle works similar */ perror("execle() error"); printf("Int code: %d, errno is %d\n", code, errno); return 0; }
it breaks execve() with "Bad address" error. I need open() syscall because I want to redirect STDOUT to a file before starting execve(), but any manipulations with open()/dup2() also do not work because of open(). Can you give me any ideas what it could be?...Code:int fd; char *buf = "This is a test\n\0"; //create & write some data to a file umask(011); fd = open("out.tmp", O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); if(fd < 0) { perror("open() error"); return -1; } close(fd); fd = open("out.tmp", O_RDWR); printf("fd is %d\n", fd); if(fd < 0) { perror("open() error"); return -1; } ssize_t bytes = write(fd, (void*) buf , strlen(buf)); if(bytes < 0) { perror("write() error"); return -1; } close(fd);
- 07-16-2010 #2Just Joined!
- Join Date
- Feb 2009
- Location
- Southport, England
- Posts
- 31
You've written execle() in the top code, and it's not even in the bottom code. Not execve()?
- 07-17-2010 #3Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
Oh, I corrected the code
it doesn't matter, I tested it with execle and execve - they both fails after any open() call. Anyway, as written in manual "...execle, execlp are just front-ends for the execve..."
- 07-17-2010 #4Linux Newbie
- Join Date
- Apr 2010
- Location
- Novosibirsk, Russia
- Posts
- 136
Solved :)
I found where is my fault )
from execve manual:
thus, this was usual segfault )Code:... The last elements of passed arrays must be NULL pointers.


