X Tutup
Skip to content

Commit 7c24822

Browse files
poetteringkeszybz
authored andcommitted
tree-wide: use new RET_NERRNO() helper at various places
1 parent ef470ff commit 7c24822

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+151
-469
lines changed

src/basic/cgroup-util.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -629,10 +629,7 @@ int cg_set_xattr(const char *controller, const char *path, const char *name, con
629629
if (r < 0)
630630
return r;
631631

632-
if (setxattr(fs, name, value, size, flags) < 0)
633-
return -errno;
634-
635-
return 0;
632+
return RET_NERRNO(setxattr(fs, name, value, size, flags));
636633
}
637634

638635
int cg_get_xattr(const char *controller, const char *path, const char *name, void *value, size_t size) {
@@ -697,10 +694,7 @@ int cg_remove_xattr(const char *controller, const char *path, const char *name)
697694
if (r < 0)
698695
return r;
699696

700-
if (removexattr(fs, name) < 0)
701-
return -errno;
702-
703-
return 0;
697+
return RET_NERRNO(removexattr(fs, name));
704698
}
705699

706700
int cg_pid_get_path(const char *controller, pid_t pid, char **ret_path) {

src/basic/chattr-util.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,7 @@ int read_attr_fd(int fd, unsigned *ret) {
121121
if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode))
122122
return -ENOTTY;
123123

124-
if (ioctl(fd, FS_IOC_GETFLAGS, ret) < 0)
125-
return -errno;
126-
127-
return 0;
124+
return RET_NERRNO(ioctl(fd, FS_IOC_GETFLAGS, ret));
128125
}
129126

130127
int read_attr_path(const char *p, unsigned *ret) {

src/basic/env-util.c

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "alloc-util.h"
1010
#include "env-util.h"
11+
#include "errno-util.h"
1112
#include "escape.h"
1213
#include "extract-word.h"
1314
#include "macro.h"
@@ -786,15 +787,12 @@ int getenv_bool_secure(const char *p) {
786787
}
787788

788789
int set_unset_env(const char *name, const char *value, bool overwrite) {
789-
int r;
790+
assert(name);
790791

791792
if (value)
792-
r = setenv(name, value, overwrite);
793-
else
794-
r = unsetenv(name);
795-
if (r < 0)
796-
return -errno;
797-
return 0;
793+
return RET_NERRNO(setenv(name, value, overwrite));
794+
795+
return RET_NERRNO(unsetenv(name));
798796
}
799797

800798
int putenv_dup(const char *assignment, bool override) {
@@ -807,9 +805,7 @@ int putenv_dup(const char *assignment, bool override) {
807805
n = strndupa_safe(assignment, e - assignment);
808806

809807
/* This is like putenv(), but uses setenv() so that our memory doesn't become part of environ[]. */
810-
if (setenv(n, e + 1, override) < 0)
811-
return -errno;
812-
return 0;
808+
return RET_NERRNO(setenv(n, e + 1, override));
813809
}
814810

815811
int setenv_systemd_exec_pid(bool update_only) {

src/basic/fd-util.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,7 @@ int fd_nonblock(int fd, bool nonblock) {
152152
if (nflags == flags)
153153
return 0;
154154

155-
if (fcntl(fd, F_SETFL, nflags) < 0)
156-
return -errno;
157-
158-
return 0;
155+
return RET_NERRNO(fcntl(fd, F_SETFL, nflags));
159156
}
160157

161158
int fd_cloexec(int fd, bool cloexec) {
@@ -171,10 +168,7 @@ int fd_cloexec(int fd, bool cloexec) {
171168
if (nflags == flags)
172169
return 0;
173170

174-
if (fcntl(fd, F_SETFD, nflags) < 0)
175-
return -errno;
176-
177-
return 0;
171+
return RET_NERRNO(fcntl(fd, F_SETFD, nflags));
178172
}
179173

180174
_pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) {
@@ -802,8 +796,5 @@ int btrfs_defrag_fd(int fd) {
802796
if (r < 0)
803797
return r;
804798

805-
if (ioctl(fd, BTRFS_IOC_DEFRAG, NULL) < 0)
806-
return -errno;
807-
808-
return 0;
799+
return RET_NERRNO(ioctl(fd, BTRFS_IOC_DEFRAG, NULL));
809800
}

src/basic/fs-util.c

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,13 @@
3030
#include "strv.h"
3131
#include "time-util.h"
3232
#include "tmpfile-util.h"
33+
#include "umask-util.h"
3334
#include "user-util.h"
3435
#include "util.h"
3536

3637
int unlink_noerrno(const char *path) {
3738
PROTECT_ERRNO;
38-
int r;
39-
40-
r = unlink(path);
41-
if (r < 0)
42-
return -errno;
43-
44-
return 0;
39+
return RET_NERRNO(unlink(path));
4540
}
4641

4742
int rmdir_parents(const char *path, const char *stop) {
@@ -97,8 +92,8 @@ int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char
9792
* want — though not atomic (i.e. for a short period both the new and the old filename will exist). */
9893
if (linkat(olddirfd, oldpath, newdirfd, newpath, 0) >= 0) {
9994

100-
if (unlinkat(olddirfd, oldpath, 0) < 0) {
101-
r = -errno; /* Backup errno before the following unlinkat() alters it */
95+
r = RET_NERRNO(unlinkat(olddirfd, oldpath, 0));
96+
if (r < 0) {
10297
(void) unlinkat(newdirfd, newpath, 0);
10398
return r;
10499
}
@@ -117,10 +112,7 @@ int rename_noreplace(int olddirfd, const char *oldpath, int newdirfd, const char
117112
if (errno != ENOENT)
118113
return -errno;
119114

120-
if (renameat(olddirfd, oldpath, newdirfd, newpath) < 0)
121-
return -errno;
122-
123-
return 0;
115+
return RET_NERRNO(renameat(olddirfd, oldpath, newdirfd, newpath));
124116
}
125117

126118
int readlinkat_malloc(int fd, const char *p, char **ret) {
@@ -286,14 +278,9 @@ int fchmod_and_chown_with_fallback(int fd, const char *path, mode_t mode, uid_t
286278
}
287279

288280
int fchmod_umask(int fd, mode_t m) {
289-
mode_t u;
290-
int r;
291-
292-
u = umask(0777);
293-
r = fchmod(fd, m & (~u)) < 0 ? -errno : 0;
294-
umask(u);
281+
_cleanup_umask_ mode_t u = umask(0777);
295282

296-
return r;
283+
return RET_NERRNO(fchmod(fd, m & (~u)));
297284
}
298285

299286
int fchmod_opath(int fd, mode_t m) {
@@ -822,7 +809,7 @@ int unlinkat_deallocate(int fd, const char *name, UnlinkDeallocateFlags flags) {
822809

823810
int open_parent(const char *path, int flags, mode_t mode) {
824811
_cleanup_free_ char *parent = NULL;
825-
int fd, r;
812+
int r;
826813

827814
r = path_extract_directory(path, &parent);
828815
if (r < 0)
@@ -836,11 +823,7 @@ int open_parent(const char *path, int flags, mode_t mode) {
836823
else if (!FLAGS_SET(flags, O_TMPFILE))
837824
flags |= O_DIRECTORY|O_RDONLY;
838825

839-
fd = open(parent, flags, mode);
840-
if (fd < 0)
841-
return -errno;
842-
843-
return fd;
826+
return RET_NERRNO(open(parent, flags, mode));
844827
}
845828

846829
int conservative_renameat(

src/basic/fs-util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int fd_warn_permissions(const char *path, int fd);
4747
int stat_warn_permissions(const char *path, const struct stat *st);
4848

4949
#define laccess(path, mode) \
50-
(faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW) < 0 ? -errno : 0)
50+
RET_NERRNO(faccessat(AT_FDCWD, (path), (mode), AT_SYMLINK_NOFOLLOW))
5151

5252
int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode);
5353
int touch(const char *path);

src/basic/memfd-util.c

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <sys/prctl.h>
1111

1212
#include "alloc-util.h"
13+
#include "errno-util.h"
1314
#include "fd-util.h"
1415
#include "macro.h"
1516
#include "memfd-util.h"
@@ -21,7 +22,6 @@
2122

2223
int memfd_new(const char *name) {
2324
_cleanup_free_ char *g = NULL;
24-
int fd;
2525

2626
if (!name) {
2727
char pr[17] = {};
@@ -49,11 +49,7 @@ int memfd_new(const char *name) {
4949
}
5050
}
5151

52-
fd = memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC);
53-
if (fd < 0)
54-
return -errno;
55-
56-
return fd;
52+
return RET_NERRNO(memfd_create(name, MFD_ALLOW_SEALING | MFD_CLOEXEC));
5753
}
5854

5955
int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
@@ -72,7 +68,6 @@ int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
7268
q = mmap(NULL, size, PROT_READ, MAP_PRIVATE, fd, offset);
7369
else
7470
q = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
75-
7671
if (q == MAP_FAILED)
7772
return -errno;
7873

@@ -81,15 +76,9 @@ int memfd_map(int fd, uint64_t offset, size_t size, void **p) {
8176
}
8277

8378
int memfd_set_sealed(int fd) {
84-
int r;
85-
8679
assert(fd >= 0);
8780

88-
r = fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL);
89-
if (r < 0)
90-
return -errno;
91-
92-
return 0;
81+
return RET_NERRNO(fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_WRITE | F_SEAL_SEAL));
9382
}
9483

9584
int memfd_get_sealed(int fd) {
@@ -106,29 +95,21 @@ int memfd_get_sealed(int fd) {
10695

10796
int memfd_get_size(int fd, uint64_t *sz) {
10897
struct stat stat;
109-
int r;
11098

11199
assert(fd >= 0);
112100
assert(sz);
113101

114-
r = fstat(fd, &stat);
115-
if (r < 0)
102+
if (fstat(fd, &stat) < 0)
116103
return -errno;
117104

118105
*sz = stat.st_size;
119106
return 0;
120107
}
121108

122109
int memfd_set_size(int fd, uint64_t sz) {
123-
int r;
124-
125110
assert(fd >= 0);
126111

127-
r = ftruncate(fd, sz);
128-
if (r < 0)
129-
return -errno;
130-
131-
return 0;
112+
return RET_NERRNO(ftruncate(fd, sz));
132113
}
133114

134115
int memfd_new_and_map(const char *name, size_t sz, void **p) {

src/basic/mkdir.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,11 @@ int mkdir_safe_internal(
8080
}
8181

8282
int mkdir_errno_wrapper(const char *pathname, mode_t mode) {
83-
if (mkdir(pathname, mode) < 0)
84-
return -errno;
85-
return 0;
83+
return RET_NERRNO(mkdir(pathname, mode));
8684
}
8785

8886
int mkdirat_errno_wrapper(int dirfd, const char *pathname, mode_t mode) {
89-
if (mkdirat(dirfd, pathname, mode) < 0)
90-
return -errno;
91-
return 0;
87+
return RET_NERRNO(mkdirat(dirfd, pathname, mode));
9288
}
9389

9490
int mkdir_safe(const char *path, mode_t mode, uid_t uid, gid_t gid, MkdirFlags flags) {

src/basic/namespace-util.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <sys/ioctl.h>
55
#include <sys/mount.h>
66

7+
#include "errno-util.h"
78
#include "fd-util.h"
89
#include "fileio.h"
910
#include "missing_fs.h"
@@ -177,10 +178,7 @@ int detach_mount_namespace(void) {
177178
if (unshare(CLONE_NEWNS) < 0)
178179
return -errno;
179180

180-
if (mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL) < 0)
181-
return -errno;
182-
183-
return 0;
181+
return RET_NERRNO(mount(NULL, "/", NULL, MS_SLAVE | MS_REC, NULL));
184182
}
185183

186184
int userns_acquire(const char *uid_map, const char *gid_map) {

src/basic/process-util.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,7 @@ int wait_for_terminate_with_timeout(pid_t pid, usec_t timeout) {
813813
if (n >= until)
814814
break;
815815

816-
r = sigtimedwait(&mask, NULL, timespec_store(&ts, until - n)) < 0 ? -errno : 0;
816+
r = RET_NERRNO(sigtimedwait(&mask, NULL, timespec_store(&ts, until - n)));
817817
/* Assuming we woke due to the child exiting. */
818818
if (waitid(P_PID, pid, &status, WEXITED|WNOHANG) == 0) {
819819
if (status.si_pid == pid) {
@@ -871,7 +871,7 @@ void sigterm_wait(pid_t pid) {
871871
int kill_and_sigcont(pid_t pid, int sig) {
872872
int r;
873873

874-
r = kill(pid, sig) < 0 ? -errno : 0;
874+
r = RET_NERRNO(kill(pid, sig));
875875

876876
/* If this worked, also send SIGCONT, unless we already just sent a SIGCONT, or SIGKILL was sent which isn't
877877
* affected by a process being suspended anyway. */

0 commit comments

Comments
 (0)
X Tutup