0

Running a trivial program within gdb, that forks and execl a client. The execl line (while inside inferior 2 in gdb) gives the process ID "is executing new program" and then immediately seg faults.

Code to that point follows:

int main(int argc, char *argv[] ) {
/* Create socket pair for communication with server, and fork/exec the server code */
  int ret;
  int fd[2];
  ret = socketpair( AF_LOCAL, SOCK_STREAM | SOCK_NONBLOCK, 0, fd );
  if( ret < 0 ) {
    perror( "Unable to create initial socketpair for qrServer" );
    return -1;
  }
  pid_t pid = fork();
  if (pid == 0) {
    char arg[4];
    bzero( arg, 4 );
    close(fd[1]);
    sprintf( arg, "%d", fd[0] );
    char* filename = "child";
    ret = execl( filename, filename, arg, NULL );

Just for the sake of completeness, child program begins as follows:

int main(int argc, char *argv[] ) {
/* Create socket pair for communication with server, and fork/exec the server code */
  if( argc < 2 ) {
    perror("Usage: argv[0] <file descriptor>");

Any ideas? I get nothing, just the seg fault itself and I get it immediately.

Aenid37
  • 16
  • 2

1 Answers1

2

From man execl:

The list of arguments must be terminated by a null pointer, and, since these are variadic functions, this pointer must be cast (char *) NULL.

So this line

  ret = execl( filename, filename, arg, NULL );

should be

  ret = execl( filename, filename, arg, (char*) NULL );
alk
  • 69,737
  • 10
  • 105
  • 255
  • 1
    @Aenid37 If this helps, please let us know. If it helps, it suggests that (a) you're on a 64-bit platform, but (b) `NULL` is defined as plain `0`. (Which is perfectly legal, but leaves you vulnerable to this problem.) – Steve Summit May 03 '16 at 17:46