1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @brief Synchronize Data on Disk with Memory |
---|
7 | * @ingroup libcsupport |
---|
8 | */ |
---|
9 | |
---|
10 | /* |
---|
11 | * COPYRIGHT (c) 1989-2008. |
---|
12 | * On-Line Applications Research Corporation (OAR). |
---|
13 | * |
---|
14 | * Redistribution and use in source and binary forms, with or without |
---|
15 | * modification, are permitted provided that the following conditions |
---|
16 | * are met: |
---|
17 | * 1. Redistributions of source code must retain the above copyright |
---|
18 | * notice, this list of conditions and the following disclaimer. |
---|
19 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
20 | * notice, this list of conditions and the following disclaimer in the |
---|
21 | * documentation and/or other materials provided with the distribution. |
---|
22 | * |
---|
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
24 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
25 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
26 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
27 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
28 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
29 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
30 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
31 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
32 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
33 | * POSSIBILITY OF SUCH DAMAGE. |
---|
34 | */ |
---|
35 | |
---|
36 | #ifdef HAVE_CONFIG_H |
---|
37 | #include "config.h" |
---|
38 | #endif |
---|
39 | |
---|
40 | #include <sys/reent.h> |
---|
41 | #include <unistd.h> |
---|
42 | #include <stdio.h> |
---|
43 | |
---|
44 | #include <rtems.h> |
---|
45 | #include <rtems/score/thread.h> |
---|
46 | #include <rtems/score/percpu.h> |
---|
47 | |
---|
48 | /* In Newlib this function is declared in a private header file */ |
---|
49 | int _fwalk_reent (struct _reent *, int (*)(struct _reent *, FILE *)); |
---|
50 | |
---|
51 | static int sync_wrapper(struct _reent *reent, FILE *f) |
---|
52 | { |
---|
53 | int fn = fileno(f); |
---|
54 | |
---|
55 | (void) reent; |
---|
56 | |
---|
57 | /* |
---|
58 | * There is no way to report errors here. So this is a best-effort approach. |
---|
59 | */ |
---|
60 | (void) fsync(fn); |
---|
61 | (void) fdatasync(fn); |
---|
62 | |
---|
63 | return 0; |
---|
64 | } |
---|
65 | |
---|
66 | /* iterate over all FILE *'s for this thread */ |
---|
67 | static bool sync_per_thread(Thread_Control *t, void *arg) |
---|
68 | { |
---|
69 | struct _reent *current_reent; |
---|
70 | struct _reent *this_reent; |
---|
71 | |
---|
72 | /* |
---|
73 | * The sync_wrapper() function will operate on the current thread's |
---|
74 | * reent structure so we will temporarily use that. |
---|
75 | */ |
---|
76 | this_reent = t->libc_reent; |
---|
77 | if ( this_reent ) { |
---|
78 | Thread_Control *executing = _Thread_Get_executing(); |
---|
79 | current_reent = executing->libc_reent; |
---|
80 | executing->libc_reent = this_reent; |
---|
81 | _fwalk_reent (this_reent, sync_wrapper); |
---|
82 | executing->libc_reent = current_reent; |
---|
83 | } |
---|
84 | |
---|
85 | return false; |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * This function operates by as follows: |
---|
90 | * for all threads |
---|
91 | * for all FILE * |
---|
92 | * fsync() |
---|
93 | * fdatasync() |
---|
94 | */ |
---|
95 | void sync(void) |
---|
96 | { |
---|
97 | |
---|
98 | /* |
---|
99 | * Walk the one used initially by RTEMS. |
---|
100 | */ |
---|
101 | _fwalk_reent(_GLOBAL_REENT, sync_wrapper); |
---|
102 | |
---|
103 | /* |
---|
104 | * XXX Do we walk the one used globally by newlib? |
---|
105 | * XXX Do we need the RTEMS global one? |
---|
106 | */ |
---|
107 | |
---|
108 | /* |
---|
109 | * Now walk all the per-thread reentrancy structures. |
---|
110 | */ |
---|
111 | rtems_task_iterate(sync_per_thread, NULL); |
---|
112 | } |
---|