X Tutup
Skip to content

Commit ddb6eea

Browse files
committed
tree-wide: port things over to FORMAT_PROC_FD_PATH()
1 parent 48a01cd commit ddb6eea

File tree

15 files changed

+52
-122
lines changed

15 files changed

+52
-122
lines changed

src/basic/fd-util.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -433,11 +433,9 @@ bool fdname_is_valid(const char *s) {
433433
}
434434

435435
int fd_get_path(int fd, char **ret) {
436-
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
437436
int r;
438437

439-
xsprintf(procfs_path, "/proc/self/fd/%i", fd);
440-
r = readlink_malloc(procfs_path, ret);
438+
r = readlink_malloc(FORMAT_PROC_FD_PATH(fd), ret);
441439
if (r == -ENOENT) {
442440
/* ENOENT can mean two things: that the fd does not exist or that /proc is not mounted. Let's make
443441
* things debuggable and distinguish the two. */
@@ -647,7 +645,6 @@ int rearrange_stdio(int original_input_fd, int original_output_fd, int original_
647645
}
648646

649647
int fd_reopen(int fd, int flags) {
650-
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
651648
int new_fd;
652649

653650
/* Reopens the specified fd with new flags. This is useful for convert an O_PATH fd into a regular one, or to
@@ -657,8 +654,7 @@ int fd_reopen(int fd, int flags) {
657654
*
658655
* This implicitly resets the file read index to 0. */
659656

660-
xsprintf(procfs_path, "/proc/self/fd/%i", fd);
661-
new_fd = open(procfs_path, flags);
657+
new_fd = open(FORMAT_PROC_FD_PATH(fd), flags);
662658
if (new_fd < 0) {
663659
if (errno != ENOENT)
664660
return -errno;

src/basic/fileio.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -721,8 +721,6 @@ int read_full_file_full(
721721
if (dir_fd == AT_FDCWD)
722722
r = sockaddr_un_set_path(&sa.un, filename);
723723
else {
724-
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
725-
726724
/* If we shall operate relative to some directory, then let's use O_PATH first to
727725
* open the socket inode, and then connect to it via /proc/self/fd/. We have to do
728726
* this since there's not connectat() that takes a directory fd as first arg. */
@@ -731,8 +729,7 @@ int read_full_file_full(
731729
if (dfd < 0)
732730
return -errno;
733731

734-
xsprintf(procfs_path, "/proc/self/fd/%i", dfd);
735-
r = sockaddr_un_set_path(&sa.un, procfs_path);
732+
r = sockaddr_un_set_path(&sa.un, FORMAT_PROC_FD_PATH(dfd));
736733
}
737734
if (r < 0)
738735
return r;

src/basic/fs-util.c

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,11 @@ int fchmod_umask(int fd, mode_t m) {
308308
}
309309

310310
int fchmod_opath(int fd, mode_t m) {
311-
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
312-
313311
/* This function operates also on fd that might have been opened with
314312
* O_PATH. Indeed fchmodat() doesn't have the AT_EMPTY_PATH flag like
315313
* fchownat() does. */
316314

317-
xsprintf(procfs_path, "/proc/self/fd/%i", fd);
318-
if (chmod(procfs_path, m) < 0) {
315+
if (chmod(FORMAT_PROC_FD_PATH(fd), m) < 0) {
319316
if (errno != ENOENT)
320317
return -errno;
321318

@@ -329,12 +326,9 @@ int fchmod_opath(int fd, mode_t m) {
329326
}
330327

331328
int futimens_opath(int fd, const struct timespec ts[2]) {
332-
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
333-
334329
/* Similar to fchmod_path() but for futimens() */
335330

336-
xsprintf(procfs_path, "/proc/self/fd/%i", fd);
337-
if (utimensat(AT_FDCWD, procfs_path, ts, 0) < 0) {
331+
if (utimensat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), ts, 0) < 0) {
338332
if (errno != ENOENT)
339333
return -errno;
340334

@@ -380,7 +374,6 @@ int fd_warn_permissions(const char *path, int fd) {
380374
}
381375

382376
int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gid, mode_t mode) {
383-
char fdpath[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
384377
_cleanup_close_ int fd = -1;
385378
int r, ret = 0;
386379

@@ -412,18 +405,16 @@ int touch_file(const char *path, bool parents, usec_t stamp, uid_t uid, gid_t gi
412405
/* Let's make a path from the fd, and operate on that. With this logic, we can adjust the access mode,
413406
* ownership and time of the file node in all cases, even if the fd refers to an O_PATH object — which is
414407
* something fchown(), fchmod(), futimensat() don't allow. */
415-
xsprintf(fdpath, "/proc/self/fd/%i", fd);
416-
417408
ret = fchmod_and_chown(fd, mode, uid, gid);
418409

419410
if (stamp != USEC_INFINITY) {
420411
struct timespec ts[2];
421412

422413
timespec_store(&ts[0], stamp);
423414
ts[1] = ts[0];
424-
r = utimensat(AT_FDCWD, fdpath, ts, 0);
415+
r = utimensat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), ts, 0);
425416
} else
426-
r = utimensat(AT_FDCWD, fdpath, NULL, 0);
417+
r = utimensat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), NULL, 0);
427418
if (r < 0 && ret >= 0)
428419
return -errno;
429420

@@ -703,13 +694,10 @@ int unlink_or_warn(const char *filename) {
703694
}
704695

705696
int inotify_add_watch_fd(int fd, int what, uint32_t mask) {
706-
char path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
707697
int wd;
708698

709699
/* This is like inotify_add_watch(), except that the file to watch is not referenced by a path, but by an fd */
710-
xsprintf(path, "/proc/self/fd/%i", what);
711-
712-
wd = inotify_add_watch(fd, path, mask);
700+
wd = inotify_add_watch(fd, FORMAT_PROC_FD_PATH(what), mask);
713701
if (wd < 0)
714702
return -errno;
715703

@@ -1156,7 +1144,6 @@ int chase_symlinks_and_opendir(
11561144
char **ret_path,
11571145
DIR **ret_dir) {
11581146

1159-
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
11601147
_cleanup_close_ int path_fd = -1;
11611148
_cleanup_free_ char *p = NULL;
11621149
DIR *d;
@@ -1182,8 +1169,7 @@ int chase_symlinks_and_opendir(
11821169
return r;
11831170
assert(path_fd >= 0);
11841171

1185-
xsprintf(procfs_path, "/proc/self/fd/%i", path_fd);
1186-
d = opendir(procfs_path);
1172+
d = opendir(FORMAT_PROC_FD_PATH(path_fd));
11871173
if (!d)
11881174
return -errno;
11891175

@@ -1237,12 +1223,9 @@ int chase_symlinks_and_stat(
12371223
}
12381224

12391225
int access_fd(int fd, int mode) {
1240-
char p[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1];
1241-
12421226
/* Like access() but operates on an already open fd */
12431227

1244-
xsprintf(p, "/proc/self/fd/%i", fd);
1245-
if (access(p, mode) < 0) {
1228+
if (access(FORMAT_PROC_FD_PATH(fd), mode) < 0) {
12461229
if (errno != ENOENT)
12471230
return -errno;
12481231

src/basic/namespace-util.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,7 @@ int namespace_enter(int pidns_fd, int mntns_fd, int netns_fd, int userns_fd, int
8989
/* Can't setns to your own userns, since then you could escalate from non-root to root in
9090
* your own namespace, so check if namespaces are equal before attempting to enter. */
9191

92-
char userns_fd_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
93-
xsprintf(userns_fd_path, "/proc/self/fd/%d", userns_fd);
94-
r = files_same(userns_fd_path, "/proc/self/ns/user", 0);
92+
r = files_same(FORMAT_PROC_FD_PATH(userns_fd), "/proc/self/ns/user", 0);
9593
if (r < 0)
9694
return r;
9795
if (r)

src/basic/tmpfile-util.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,7 @@ int link_tmpfile(int fd, const char *path, const char *target) {
300300
if (r < 0)
301301
return r;
302302
} else {
303-
char proc_fd_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(fd) + 1];
304-
305-
xsprintf(proc_fd_path, "/proc/self/fd/%i", fd);
306-
307-
if (linkat(AT_FDCWD, proc_fd_path, AT_FDCWD, target, AT_SYMLINK_FOLLOW) < 0)
303+
if (linkat(AT_FDCWD, FORMAT_PROC_FD_PATH(fd), AT_FDCWD, target, AT_SYMLINK_FOLLOW) < 0)
308304
return -errno;
309305
}
310306

src/basic/xattr-util.c

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,10 @@ static int getxattrat_fake_prepare(
108108
int dirfd,
109109
const char *filename,
110110
int flags,
111-
char ret_fn[static STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1],
111+
char ret_fn[static PROC_FD_PATH_MAX],
112112
int *ret_fd) {
113113

114114
_cleanup_close_ int fd = -1;
115-
116115
assert(ret_fn);
117116
assert(ret_fd);
118117

@@ -125,13 +124,15 @@ static int getxattrat_fake_prepare(
125124
if (!(flags & AT_EMPTY_PATH))
126125
return -EINVAL;
127126

128-
snprintf(ret_fn, STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1, "/proc/self/fd/%i", dirfd);
127+
assert(dirfd >= 0);
128+
129+
format_proc_fd_path(ret_fn, dirfd);
129130
} else {
130131
fd = openat(dirfd, filename, O_CLOEXEC|O_PATH|(flags & AT_SYMLINK_NOFOLLOW ? O_NOFOLLOW : 0));
131132
if (fd < 0)
132133
return -errno;
133134

134-
snprintf(ret_fn, STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1, "/proc/self/fd/%i", fd);
135+
format_proc_fd_path(ret_fn, fd);
135136
}
136137

137138
/* Pass the FD to the caller, since in case we do openat() the filename depends on it. */
@@ -148,8 +149,8 @@ int fgetxattrat_fake(
148149
int flags,
149150
size_t *ret_size) {
150151

151-
char fn[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
152152
_cleanup_close_ int fd = -1;
153+
char fn[PROC_FD_PATH_MAX];
153154
ssize_t l;
154155
int r;
155156

@@ -172,8 +173,8 @@ int fgetxattrat_fake_malloc(
172173
int flags,
173174
char **value) {
174175

175-
char fn[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
176176
_cleanup_close_ int fd = -1;
177+
char fn[PROC_FD_PATH_MAX];
177178
int r;
178179

179180
r = getxattrat_fake_prepare(dirfd, filename, flags, fn, &fd);

src/core/service.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,6 @@ static int service_is_suitable_main_pid(Service *s, pid_t pid, int prio) {
916916
}
917917

918918
static int service_load_pid_file(Service *s, bool may_warn) {
919-
char procfs[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
920919
bool questionable_pid_file = false;
921920
_cleanup_free_ char *k = NULL;
922921
_cleanup_close_ int fd = -1;
@@ -945,8 +944,7 @@ static int service_load_pid_file(Service *s, bool may_warn) {
945944

946945
/* Let's read the PID file now that we chased it down. But we need to convert the O_PATH fd
947946
* chase_symlinks() returned us into a proper fd first. */
948-
xsprintf(procfs, "/proc/self/fd/%i", fd);
949-
r = read_one_line_file(procfs, &k);
947+
r = read_one_line_file(FORMAT_PROC_FD_PATH(fd), &k);
950948
if (r < 0)
951949
return log_unit_error_errno(UNIT(s), r,
952950
"Can't convert PID files %s O_PATH file descriptor to proper file descriptor: %m",

src/nspawn/nspawn-patch-uid.c

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#if HAVE_ACL
2323

2424
static int get_acl(int fd, const char *name, acl_type_t type, acl_t *ret) {
25-
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
2625
acl_t acl;
2726

2827
assert(fd >= 0);
@@ -35,14 +34,11 @@ static int get_acl(int fd, const char *name, acl_type_t type, acl_t *ret) {
3534
if (child_fd < 0)
3635
return -errno;
3736

38-
xsprintf(procfs_path, "/proc/self/fd/%i", child_fd);
39-
acl = acl_get_file(procfs_path, type);
37+
acl = acl_get_file(FORMAT_PROC_FD_PATH(child_fd), type);
4038
} else if (type == ACL_TYPE_ACCESS)
4139
acl = acl_get_fd(fd);
42-
else {
43-
xsprintf(procfs_path, "/proc/self/fd/%i", fd);
44-
acl = acl_get_file(procfs_path, type);
45-
}
40+
else
41+
acl = acl_get_file(FORMAT_PROC_FD_PATH(fd), type);
4642
if (!acl)
4743
return -errno;
4844

@@ -51,7 +47,6 @@ static int get_acl(int fd, const char *name, acl_type_t type, acl_t *ret) {
5147
}
5248

5349
static int set_acl(int fd, const char *name, acl_type_t type, acl_t acl) {
54-
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
5550
int r;
5651

5752
assert(fd >= 0);
@@ -64,14 +59,11 @@ static int set_acl(int fd, const char *name, acl_type_t type, acl_t acl) {
6459
if (child_fd < 0)
6560
return -errno;
6661

67-
xsprintf(procfs_path, "/proc/self/fd/%i", child_fd);
68-
r = acl_set_file(procfs_path, type, acl);
62+
r = acl_set_file(FORMAT_PROC_FD_PATH(child_fd), type, acl);
6963
} else if (type == ACL_TYPE_ACCESS)
7064
r = acl_set_fd(fd, acl);
71-
else {
72-
xsprintf(procfs_path, "/proc/self/fd/%i", fd);
73-
r = acl_set_file(procfs_path, type, acl);
74-
}
65+
else
66+
r = acl_set_file(FORMAT_PROC_FD_PATH(fd), type, acl);
7567
if (r < 0)
7668
return -errno;
7769

src/partition/repart.c

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,12 +1581,8 @@ static int context_load_partition_table(
15811581
* /proc/self/fd/ magic path if we have an existing fd. Open the original file otherwise. */
15821582
if (*backing_fd < 0)
15831583
r = fdisk_assign_device(c, node, arg_dry_run);
1584-
else {
1585-
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
1586-
xsprintf(procfs_path, "/proc/self/fd/%i", *backing_fd);
1587-
1588-
r = fdisk_assign_device(c, procfs_path, arg_dry_run);
1589-
}
1584+
else
1585+
r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(*backing_fd), arg_dry_run);
15901586
if (r == -EINVAL && arg_size_auto) {
15911587
struct stat st;
15921588

@@ -4593,7 +4589,6 @@ static int find_root(char **ret, int *ret_fd) {
45934589
}
45944590

45954591
static int resize_pt(int fd) {
4596-
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
45974592
_cleanup_(fdisk_unref_contextp) struct fdisk_context *c = NULL;
45984593
int r;
45994594

@@ -4605,14 +4600,13 @@ static int resize_pt(int fd) {
46054600
if (!c)
46064601
return log_oom();
46074602

4608-
xsprintf(procfs_path, "/proc/self/fd/%i", fd);
4609-
r = fdisk_assign_device(c, procfs_path, 0);
4603+
r = fdisk_assign_device(c, FORMAT_PROC_FD_PATH(fd), 0);
46104604
if (r < 0)
4611-
return log_error_errno(r, "Failed to open device '%s': %m", procfs_path);
4605+
return log_error_errno(r, "Failed to open device '%s': %m", FORMAT_PROC_FD_PATH(fd));
46124606

46134607
r = fdisk_has_label(c);
46144608
if (r < 0)
4615-
return log_error_errno(r, "Failed to determine whether disk '%s' has a disk label: %m", procfs_path);
4609+
return log_error_errno(r, "Failed to determine whether disk '%s' has a disk label: %m", FORMAT_PROC_FD_PATH(fd));
46164610
if (r == 0) {
46174611
log_debug("Not resizing partition table, as there currently is none.");
46184612
return 0;

src/shared/chown-recursive.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ static int chown_one(
2121
gid_t gid,
2222
mode_t mask) {
2323

24-
char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int) + 1];
2524
const char *n;
2625
int r;
2726

@@ -30,11 +29,10 @@ static int chown_one(
3029

3130
/* We change ACLs through the /proc/self/fd/%i path, so that we have a stable reference that works
3231
* with O_PATH. */
33-
xsprintf(procfs_path, "/proc/self/fd/%i", fd);
3432

3533
/* Drop any ACL if there is one */
3634
FOREACH_STRING(n, "system.posix_acl_access", "system.posix_acl_default")
37-
if (removexattr(procfs_path, n) < 0)
35+
if (removexattr(FORMAT_PROC_FD_PATH(fd), n) < 0)
3836
if (!IN_SET(errno, ENODATA, EOPNOTSUPP, ENOSYS, ENOTTY))
3937
return -errno;
4038

0 commit comments

Comments
 (0)
X Tutup