Find the answer to your Linux question:
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 ...
  1. #1
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    136

    Arrow [SOLVED] open() syscall breaks execve()'s work

    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 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;
    }
    Threre are no problems with it. But if I insert open() syscall in my code before execve(), like this

    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);
    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?...

  2. #2
    Just 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()?

  3. #3
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    136

    Post

    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..."

  4. #4
    Linux Newbie
    Join Date
    Apr 2010
    Location
    Novosibirsk, Russia
    Posts
    136

    Smile Solved :)

    I found where is my fault )

    from execve manual:
    Code:
    ... The last elements of passed arrays must be NULL pointers.
    thus, this was usual segfault )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...