Re: How to write a proper fork hook code?
On Sat, 2015-02-28 at 23:39 +0100, Samuel Thibault wrote:
> Svante Signell, le Sat 28 Feb 2015 15:32:47 +0100, a écrit :
> > * how come?
> > - code calling fcntl() works.
> > - code calling the (RPC) file_record_lock() does not!
>
> Which code? Without showing what you are doing, it's difficult for us to
> understand what you mean.
The code is attached. Compile with RPC_CALLS and FCNTL_CALLS
defined/undefined to get the two versions. with fcntl calls the hook is
triggered and unlocking works as expected, with rpc_calls it does not.
I've put print statements in sysdeps/mach/hurd/fork.c and RUN_HOOK()
is run. However, as there are more than one hook using
_hurd_atfork_child_hook. Is there any way to find out which ones is
called? Using_hurd_fork_child_hook printing does not work, due to??
BTW: All tests are made by LD_PRELOAD/LD_LIBRARY_PATH after building new
versions of libc.so (and libmachuser.so, libhurduser.so). I assume
testing can be done this way, not needing to install built packages?
> > * the code below iterates over the file descriptor table entries. I
> > would like to iterate over open file descriptors in the chile. How to do
> > that?
>
> It looks almost right.
> You should enter the critical section before that, by using
> HURD_CRITICAL_BEGIN and HURD_CRITICAL_END
Done that for _hurd_atfork_child_hook. Not needed for
_hurd_fork_child_hook, right? Main problem remains, whatever version is
used.
> I don't think you need to test for that, d == NULL should be enough.
OK!
> > err = HURD_FD_PORT_USE (d, __file_record_lock (port, F_SETLK64, &fl64));
>
> You can probably use _hurd_port_get directly instead, see ctty_new_pgrp
> for instance.
Works for the test code attached, not with tdbtorture.
db open failed: Interrupted system call
child 3411 exited with signal 11
child 3412 exited with signal 15
tdbtorture behaves (wrongly) as the rpc version with:
err = HURD_FD_PORT_USE (d, __file_record_lock (port, F_SETLK64, &fl64));
but does not get any interrupt signals.
>> //text_set_element (_hurd_fork_child_hook,fork_child_rlock);
>> text_set_element (_hurd_atfork_child_hook, fork_child_rlock);
> As I told you, the difference is that the former is run inside the
> critical section (and various locks), while the latter is run outside of
> it.
Got it, see above. I've tried both versions, same main problem as above.
Regarding file_record_lock etc functions, the code that can be used for
testing is the patches I already submitted some time ago.
/* Test if a process inherits locks after a fork.
Copyright (C) 2001, 2015 Free Software Foundation, Inc.
Written by Svante Signell <svante.signell@gmail.com>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
#define _GNU_SOURCE
//#define __USE_LARGEFILE64
//#define _FILE_OFFSET_BITS 64
#include <stdio.h>
#include <assert.h>
#include <error.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <hurd.h>
char *lock2str (int type)
{
if (type & LOCK_SH)
return "read";
if (type & LOCK_EX)
return "write";
if (type & LOCK_UN)
return "unlocked";
assert (! "Invalid");
return NULL;
}
char *fcntl2str (int type)
{
if (type == F_RDLCK)
return "read";
if (type == F_WRLCK)
return "write";
if (type == F_UNLCK)
return "unlocked";
assert (! "Invalid");
return NULL;
}
//#define RPC_CALLS
#undef RPC_CALLS
#define FCNTL_CALLS
//#undef FCNTL_CALLS
int main (int argc, char **argv)
{
struct flock64 lock;
pid_t pid;
int status, cmd;
#ifdef FCNTL_CALLS
int err;
int fd1;
#endif
#ifdef RPC_CALLS
error_t err;
int fd2;
int mine, others;
#endif
if (argc != 2)
error (1, 0, "Usage: %s file", argv[0]);
lock.l_type = F_WRLCK;
//lock.l_type = F_RDLCK;
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
lock.l_pid = -1; /* Can be any value */
//cmd = F_SETLK64;
cmd = F_SETLKW64;
#ifdef FCNTL_CALLS
fd1 = open (argv[1], O_RDONLY | O_WRONLY | O_CREAT);
if (fd1 == -1)
error (1, errno, "fopen");
err = fcntl (fd1, cmd, &lock);
if (err)
error (1, err, "fcntl write lock");
#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");
err = file_record_lock (fd2, cmd, &lock);
if (err)
error (1, err, "file_record_lock");
#endif
printf ("Parent pid = %d\n", getpid());
printf ("Lock type lock.l_type = '%s'\n", fcntl2str(lock.l_type));
if (lock.l_pid != -1)
printf ("PID holding lock lock.l_pid = %d\n", lock.l_pid);
pid = fork ();
if (pid == -1)
error (1, errno, "fork");
else if (pid == 0)
{
printf ("Child PID in the child = %d\n", pid);
#ifdef FCNTL_CALLS
cmd = F_GETLK64;
err = fcntl (fd1, cmd, &lock);
if (err)
error (1, err, "fcntl child");
printf ("Child lock status: '%s'\n", fcntl2str (lock.l_type));
#endif
#ifdef RPC_CALLS
err = file_lock_stat (fd2, &mine, &others);
if (err)
error (1, err, "file_lock_stat child");
printf ("Child has a '%s' lock; Others have a '%s' lock.\n",
lock2str (mine), lock2str (others));
#endif
exit(0);
}
else
{
printf ("Child PID in the parent = %d\n", pid);
#ifdef FCNTL_CALLS
cmd = F_GETLK64;
err = fcntl (fd1, cmd, &lock);
if (err)
error (1, err, "fcntl parent");
printf ("Parent lock status: '%s'\n", fcntl2str (lock.l_type));
#endif
#ifdef RPC_CALLS
err = file_lock_stat (fd2, &mine, &others);
if (err)
error (1, err, "file_lock_stat parent");
printf ("Parent has a '%s' lock; Others have a '%s' lock.\n",
lock2str (mine), lock2str (others));
#endif
waitpid(pid, &status, 0);
}
#ifdef FCNTL_CALLS
close (fd1);
#endif
#ifdef RPC_CALLS
mach_port_deallocate (mach_task_self (), fd2);
#endif
return 0;
}
Reply to: