• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 14895667988

07 May 2025 08:57PM UTC coverage: 72.225% (-0.007%) from 72.232%
14895667988

push

github

yuwata
network: log_link_message_debug_errno() automatically append %m if necessary

Follow-up for d28746ef5.
Fixes CID#1609753.

0 of 1 new or added line in 1 file covered. (0.0%)

20297 existing lines in 338 files now uncovered.

297407 of 411780 relevant lines covered (72.22%)

695716.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

66.23
/src/basic/sync-util.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <fcntl.h>
4
#include <sys/stat.h>
5

6
#include "alloc-util.h"
7
#include "errno-util.h"
8
#include "fd-util.h"
9
#include "fs-util.h"
10
#include "log.h"
11
#include "path-util.h"
12
#include "string-util.h"
13
#include "sync-util.h"
14

15
int fsync_directory_of_file(int fd) {
1,704,476✔
16
        _cleanup_close_ int dfd = -EBADF;
1,704,476✔
17
        struct stat st;
1,704,476✔
18
        int r;
1,704,476✔
19

20
        assert(fd >= 0);
1,704,476✔
21

22
        /* We only reasonably can do this for regular files and directories, or for O_PATH fds, hence check
23
         * for the inode type first */
24
        if (fstat(fd, &st) < 0)
1,704,476✔
UNCOV
25
                return -errno;
×
26

27
        if (S_ISDIR(st.st_mode)) {
1,704,476✔
28
                dfd = openat(fd, "..", O_RDONLY|O_DIRECTORY|O_CLOEXEC, 0);
46✔
29
                if (dfd < 0)
46✔
UNCOV
30
                        return -errno;
×
31

32
        } else if (!S_ISREG(st.st_mode)) { /* Regular files are OK regardless if O_PATH or not, for all other
1,704,430✔
33
                                            * types check O_PATH flag */
34
                r = fd_is_opath(fd);
4✔
35
                if (r < 0)
4✔
36
                        return r;
37
                if (!r) /* If O_PATH this refers to the inode in the fs, in which case we can sensibly do
4✔
38
                         * what is requested. Otherwise this refers to a socket, fifo or device node, where
39
                         * the concept of a containing directory doesn't make too much sense. */
40
                        return -ENOTTY;
41
        }
42

43
        if (dfd < 0) {
44
                _cleanup_free_ char *path = NULL;
1,704,430✔
45

46
                r = fd_get_path(fd, &path);
1,704,430✔
47
                if (r < 0) {
1,704,430✔
UNCOV
48
                        log_debug_errno(r, "Failed to query /proc/self/fd/%d%s: %m",
×
49
                                        fd,
50
                                        r == -ENOSYS ? ", ignoring" : "");
51

UNCOV
52
                        if (r == -ENOSYS)
×
53
                                /* If /proc is not available, we're most likely running in some
54
                                 * chroot environment, and syncing the directory is not very
55
                                 * important in that case. Let's just silently do nothing. */
56
                                return 0;
57

UNCOV
58
                        return r;
×
59
                }
60

61
                if (!path_is_absolute(path))
1,704,430✔
62
                        return -EINVAL;
63

64
                dfd = open_parent(path, O_CLOEXEC|O_NOFOLLOW, 0);
1,704,430✔
65
                if (dfd < 0)
1,704,430✔
66
                        return dfd;
67
        }
68

69
        return RET_NERRNO(fsync(dfd));
1,704,476✔
70
}
71

72
int fsync_full(int fd) {
1,702,537✔
73
        int r, q;
1,702,537✔
74

75
        /* Sync both the file and the directory */
76

77
        r = RET_NERRNO(fsync(fd));
1,702,537✔
78

79
        q = fsync_directory_of_file(fd);
1,702,537✔
80
        if (r < 0) /* Return earlier error */
1,702,537✔
81
                return r;
82
        if (q == -ENOTTY) /* Ignore if the 'fd' refers to a block device or so which doesn't really have a
1,702,537✔
83
                           * parent dir */
UNCOV
84
                return 0;
×
85
        return q;
86
}
87

88
int fsync_path_at(int at_fd, const char *path) {
1✔
89
        _cleanup_close_ int opened_fd = -EBADF;
1✔
90
        int fd;
1✔
91

92
        if (isempty(path)) {
1✔
93
                if (at_fd == AT_FDCWD) {
×
UNCOV
94
                        opened_fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
×
UNCOV
95
                        if (opened_fd < 0)
×
UNCOV
96
                                return -errno;
×
97

98
                        fd = opened_fd;
99
                } else
100
                        fd = at_fd;
101
        } else {
102
                opened_fd = openat(at_fd, path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
1✔
103
                if (opened_fd < 0)
1✔
UNCOV
104
                        return -errno;
×
105

106
                fd = opened_fd;
107
        }
108

109
        return RET_NERRNO(fsync(fd));
1✔
110
}
111

112
int fsync_parent_at(int at_fd, const char *path) {
132✔
113
        _cleanup_close_ int opened_fd = -EBADF;
132✔
114

115
        if (isempty(path)) {
132✔
116
                if (at_fd != AT_FDCWD)
×
117
                        return fsync_directory_of_file(at_fd);
×
118

UNCOV
119
                opened_fd = open("..", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
×
120
                if (opened_fd < 0)
×
UNCOV
121
                        return -errno;
×
122

UNCOV
123
                return RET_NERRNO(fsync(opened_fd));
×
124
        }
125

126
        opened_fd = openat(at_fd, path, O_PATH|O_CLOEXEC|O_NOFOLLOW);
132✔
127
        if (opened_fd < 0)
132✔
UNCOV
128
                return -errno;
×
129

130
        return fsync_directory_of_file(opened_fd);
132✔
131
}
132

133
int fsync_path_and_parent_at(int at_fd, const char *path) {
32✔
134
        _cleanup_close_ int opened_fd = -EBADF;
32✔
135

136
        if (isempty(path)) {
32✔
137
                if (at_fd != AT_FDCWD)
×
UNCOV
138
                        return fsync_full(at_fd);
×
139

UNCOV
140
                opened_fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
×
141
        } else
142
                opened_fd = openat(at_fd, path, O_RDONLY|O_NOFOLLOW|O_NONBLOCK|O_CLOEXEC);
32✔
143
        if (opened_fd < 0)
32✔
UNCOV
144
                return -errno;
×
145

146
        return fsync_full(opened_fd);
32✔
147
}
148

149
int syncfs_path(int at_fd, const char *path) {
617✔
150
        _cleanup_close_ int fd = -EBADF;
617✔
151

152
        if (isempty(path)) {
617✔
153
                if (at_fd != AT_FDCWD)
×
UNCOV
154
                        return RET_NERRNO(syncfs(at_fd));
×
155

UNCOV
156
                fd = open(".", O_RDONLY|O_DIRECTORY|O_CLOEXEC);
×
157
        } else
158
                fd = openat(at_fd, path, O_RDONLY|O_CLOEXEC|O_NONBLOCK);
617✔
159
        if (fd < 0)
617✔
UNCOV
160
                return -errno;
×
161

162
        return RET_NERRNO(syncfs(fd));
617✔
163
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc