]> git.ipfire.org Git - thirdparty/glibc.git/blob - sysdeps/mach/hurd/msync.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / sysdeps / mach / hurd / msync.c
1 /* msync -- Synchronize mapped memory to external storage. Mach version.
2 Copyright (C) 2002-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #include <sys/types.h>
20 #include <sys/mman.h>
21 #include <errno.h>
22 #include <sysdep-cancel.h>
23
24 #include <hurd/hurd.h>
25
26 /* Synchronize the region starting at ADDR and extending LEN bytes with the
27 file it maps. Filesystem operations on a file being mapped are
28 unpredictable before this is done. */
29
30 int
31 msync (void *addr, size_t length, int flags)
32 {
33 boolean_t should_flush = flags & MS_INVALIDATE ? 1 : 0;
34 boolean_t should_iosync = flags & MS_ASYNC ? 0 : 1;
35
36 vm_address_t cur = (vm_address_t) addr;
37 vm_address_t target = cur + length;
38
39 vm_size_t len;
40 vm_prot_t prot;
41 vm_prot_t max_prot;
42 vm_inherit_t inherit;
43 boolean_t shared;
44 memory_object_name_t obj;
45 vm_offset_t offset;
46
47 kern_return_t err;
48 int cancel_oldtype;
49
50 if (flags & (MS_SYNC | MS_ASYNC) == (MS_SYNC | MS_ASYNC))
51 return __hurd_fail (EINVAL);
52
53 while (cur < target)
54 {
55 vm_address_t begin = cur;
56
57 err = __vm_region (__mach_task_self (),
58 &begin, &len, &prot, &max_prot, &inherit,
59 &shared, &obj, &offset);
60
61 if (err != KERN_SUCCESS)
62 return __hurd_fail (err);
63
64 if (begin > cur)
65 /* We were given an address before the first region,
66 or we found a hole. */
67 cur = begin;
68
69 if (cur >= target)
70 /* We were given an ending address within a hole. */
71 break;
72
73 if (MACH_PORT_VALID (obj))
74 {
75 vm_size_t sync_len;
76
77 if (begin + len > target)
78 sync_len = target - begin;
79 else
80 sync_len = len;
81
82 cancel_oldtype = LIBC_CANCEL_ASYNC();
83 err = __vm_object_sync (obj, cur - begin + offset, sync_len,
84 should_flush, 1, should_iosync);
85 LIBC_CANCEL_RESET (cancel_oldtype);
86 __mach_port_deallocate (__mach_task_self (), obj);
87
88 if (err)
89 return __hurd_fail (err);
90
91 }
92
93 cur = begin + len;
94 }
95
96 return 0;
97 }