[Top][All Lists]

[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>

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

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

  int fd1;

#ifdef RPC_CALLS
  int fd2;

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

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

#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");

  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);
      printf ("Child PID in the parent = %d\n", pid);
      waitpid(pid, &status, 0);

  close (fd1);

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

  return 0;

reply via email to

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