1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
/* Test is a process inherits locks after a fork.
Copyright (C) 2001 Free Software Foundation, Inc.
Written by Neal H Walfield <neal@cs.uml.edu>
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/>. */
#include <assert.h>
#include <stdio.h>
#include <error.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include "fs_U.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;
}
int main (int argc, char **argv)
{
error_t err;
struct flock64 lock;
mach_port_t rendezvous = MACH_PORT_NULL;
int fd;
pid_t pid;
int mine, others;
if (argc != 2)
error (1, 0, "Usage: %s file", argv[0]);
lock.l_whence = SEEK_SET;
lock.l_start = 0;
lock.l_len = 0;
lock.l_type = F_WRLCK;
fd = file_name_lookup (argv[1], O_READ | O_WRITE | O_CREAT, 0666);
if (fd == MACH_PORT_NULL)
error (1, errno, "file_name_lookup");
err = file_record_lock (fd, F_SETLK64, &lock, rendezvous, MACH_MSG_TYPE_MAKE_SEND);
if (err)
error (1, err, "file_record_lock");
pid = fork ();
if (pid == -1)
error (1, errno, "fork");
err = file_lock_stat (fd, &mine, &others);
if (err)
error (1, err, "file_lock_stat");
printf ("%s has a %s lock; Others have a %s lock.\n",
pid ? "Parent" : "Child", lock2str (mine), lock2str (others));
mach_port_deallocate (mach_task_self (), fd);
return 0;
}
|