bug-hurd
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to write a proper fork hook code?


From: Svante Signell
Subject: Re: How to write a proper fork hook code?
Date: Mon, 02 Mar 2015 00:15:14 +0100

On Sun, 2015-03-01 at 23:08 +0100, Svante Signell wrote:
> On Sun, 2015-03-01 at 22:06 +0100, Samuel Thibault wrote:

Much simpler code: It only opens a file and calls fork!
FCNTL_CALLS version triggers the hook
RPC_CALLS version does not

#define _GNU_SOURCE
#include <stdio.h>
#include <error.h>
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <hurd.h>

#define FCNTL_CALLS
//#undef FCNTL_CALLS
//#undef RPC_CALLS
#define RPC_CALLS

int main (int argc, char **argv)
{
  pid_t pid;
  int status;

#ifdef FCNTL_CALLS
  int fd1;
#endif

#ifdef RPC_CALLS
  int fd2;
#endif

  if (argc != 2)
    error (1, 0, "Usage: %s file", argv[0]);

#ifdef FCNTL_CALLS
  fd1 = open (argv[1], O_RDONLY | O_WRONLY | O_CREAT);
  if (fd1 == -1)
    error (1, errno, "fopen");
#endif

#ifdef RPC_CALLS
  fd2 = file_name_lookup (argv[1], O_READ | O_WRITE | O_CREAT, 0666);
  if (fd2 == MACH_PORT_NULL)
    error (1, errno, "file_name_lookup");
#endif

  printf ("Parent pid = %d\n", getpid());

  pid = fork ();
  if (pid == -1)
    error (1, errno, "fork");
  else if (pid == 0)
    {
      printf ("Child PID in the child = %d\n", pid);
      exit(0);
    }
  else
    {
      printf ("Child PID in the parent = %d\n", pid);
      waitpid(pid, &status, 0);
    }

#ifdef FCNTL_CALLS
  close (fd1);
#endif

#ifdef RPC_CALLS
  mach_port_deallocate (mach_task_self (), fd2);
#endif

  return 0;
}





reply via email to

[Prev in Thread] Current Thread [Next in Thread]