/* Test if a process inherits locks after a fork. Copyright (C) 2001, 2015 Free Software Foundation, Inc. Written by Svante Signell 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 . */ #define _GNU_SOURCE //#define __USE_LARGEFILE64 //#define _FILE_OFFSET_BITS 64 #include #include #include #include #include #include #include #include #include #include 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; }