Skip to content

Commit b80e6d9

Browse files
committed
chore: Use new "at" syscalls when possible
For many IO syscalls such as `open` and `mkdir`, use the new `at` version that takes a FD/path pair instead. On newer architectures like `aarch64`, the traditional path-only version is not available and causes compile errors.
1 parent f265b90 commit b80e6d9

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

monika/linux/io.cpp

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,13 @@ int _moni_read_link(int fd, const char* path, char* buffer, size_t *_bufferSize)
260260
}
261261

262262
struct stat linuxStat;
263-
status = LINUX_SYSCALL2(__NR_lstat, hostPath, &linuxStat);
263+
status = LINUX_SYSCALL4(__NR_newfstatat, AT_FDCWD, hostPath, &linuxStat, AT_SYMLINK_NOFOLLOW);
264264
if (status < 0)
265265
{
266266
return LinuxToB(-status);
267267
}
268268

269-
status = LINUX_SYSCALL3(__NR_readlink, hostPath, buffer, *_bufferSize);
269+
status = LINUX_SYSCALL4(__NR_readlinkat, AT_FDCWD, hostPath, buffer, *_bufferSize);
270270

271271
if (status < 0)
272272
{
@@ -292,7 +292,7 @@ status_t _moni_create_symlink(int fd, const char* path, const char* toPath, int
292292
return status;
293293
}
294294

295-
status = LINUX_SYSCALL2(__NR_symlink, toPath, hostPath);
295+
status = LINUX_SYSCALL3(__NR_symlinkat, toPath, AT_FDCWD, hostPath);
296296

297297
if (status < 0)
298298
{
@@ -524,11 +524,11 @@ int _moni_open(int fd, const char* path, int openMode, int perms)
524524
linuxFlags |= O_NOFOLLOW;
525525
}
526526

527-
int result = LINUX_SYSCALL3(__NR_open, hostPath, linuxFlags, linuxMode);
527+
int result = LINUX_SYSCALL4(__NR_openat, AT_FDCWD, hostPath, linuxFlags, linuxMode);
528528

529529
if (result == -ELOOP && !shouldFailWithEloop)
530530
{
531-
result = LINUX_SYSCALL3(__NR_open, hostPath, linuxFlags | O_PATH, linuxMode);
531+
result = LINUX_SYSCALL4(__NR_openat, AT_FDCWD, hostPath, linuxFlags | O_PATH, linuxMode);
532532
}
533533

534534
if (result < 0)
@@ -694,7 +694,7 @@ int _moni_normalize_path(const char* userPath, bool traverseLink, char* buffer)
694694

695695
int _moni_create_pipe(int *fds)
696696
{
697-
long result = LINUX_SYSCALL1(__NR_pipe, fds);
697+
long result = LINUX_SYSCALL2(__NR_pipe2, fds, 0);
698698

699699
if (result < 0)
700700
{
@@ -728,7 +728,7 @@ status_t _moni_create_fifo(int fd, const char* path, mode_t perms)
728728
return status;
729729
}
730730

731-
status = LINUX_SYSCALL3(__NR_mknod, hostPath, perms | S_IFIFO, 0);
731+
status = LINUX_SYSCALL4(__NR_mknodat, AT_FDCWD, hostPath, perms | S_IFIFO, 0);
732732
if (status < 0)
733733
{
734734
return LinuxToB(-status);
@@ -759,7 +759,7 @@ int _moni_rename(int oldDir, const char* oldpath, int newDir, const char* newpat
759759
return status;
760760
}
761761

762-
status = LINUX_SYSCALL2(__NR_rename, oldHostPath, newHostPath);
762+
status = LINUX_SYSCALL4(__NR_renameat, AT_FDCWD, oldHostPath, AT_FDCWD, newHostPath);
763763

764764
if (status < 0)
765765
{
@@ -809,7 +809,7 @@ int _moni_open_dir(int fd, const char* path)
809809
return expandStatus;
810810
}
811811

812-
int result = LINUX_SYSCALL2(__NR_open, hostPath, O_DIRECTORY);
812+
int result = LINUX_SYSCALL4(__NR_openat, AT_FDCWD, hostPath, O_DIRECTORY, 0);
813813

814814
if (result < 0)
815815
{
@@ -828,7 +828,7 @@ int _moni_open_dir(int fd, const char* path)
828828

829829
status_t _moni_open_parent_dir(int fd, char* name, size_t nameLength)
830830
{
831-
long result = LINUX_SYSCALL3(__NR_openat, fd, "..", O_DIRECTORY);
831+
long result = LINUX_SYSCALL4(__NR_openat, fd, "..", O_DIRECTORY, 0);
832832

833833
if (result < 0)
834834
{
@@ -906,7 +906,7 @@ int _moni_dup2(int ofd, int nfd)
906906
return HAIKU_POSIX_EBADF;
907907
}
908908

909-
int result = LINUX_SYSCALL2(__NR_dup2, ofd, nfd);
909+
int result = LINUX_SYSCALL3(__NR_dup3, ofd, nfd, 0);
910910

911911
if (result < 0)
912912
{
@@ -1133,7 +1133,7 @@ status_t _moni_create_dir(int fd, const char* path, int perms)
11331133
return status;
11341134
}
11351135

1136-
status = LINUX_SYSCALL2(__NR_mkdir, hostPath, ModeBToLinux(perms));
1136+
status = LINUX_SYSCALL3(__NR_mkdirat, AT_FDCWD, hostPath, ModeBToLinux(perms));
11371137

11381138
if (status < 0)
11391139
{
@@ -1151,7 +1151,11 @@ status_t _moni_remove_dir(int fd, const char* path)
11511151
long status = GET_SERVERCALLS()->vchroot_expandat(fd, path, strlen(path),
11521152
false, hostPath, sizeof(hostPath));
11531153

1154+
#ifdef __NR_rmdir
11541155
status = LINUX_SYSCALL1(__NR_rmdir, hostPath);
1156+
#else
1157+
status = LINUX_SYSCALL3(__NR_unlinkat, AT_FDCWD, hostPath, AT_REMOVEDIR);
1158+
#endif
11551159

11561160
if (status < 0)
11571161
{
@@ -1216,7 +1220,7 @@ status_t _moni_open_dir_entry_ref(haiku_dev_t device, haiku_ino_t inode, const c
12161220
return status;
12171221
}
12181222

1219-
status = LINUX_SYSCALL2(__NR_open, hostPath, O_DIRECTORY);
1223+
status = LINUX_SYSCALL4(__NR_openat, AT_FDCWD, hostPath, O_DIRECTORY, 0);
12201224
if (status < 0)
12211225
{
12221226
return LinuxToB(-status);
@@ -1269,11 +1273,11 @@ status_t _moni_open_entry_ref(haiku_dev_t device, haiku_ino_t inode, const char*
12691273
linuxFlags |= O_NOFOLLOW;
12701274
}
12711275

1272-
status = LINUX_SYSCALL3(__NR_open, hostPath, linuxFlags, linuxMode);
1276+
status = LINUX_SYSCALL4(__NR_openat, AT_FDCWD, hostPath, linuxFlags, linuxMode);
12731277

12741278
if (status == -ELOOP && !shouldFailWithEloop)
12751279
{
1276-
status = LINUX_SYSCALL3(__NR_open, hostPath, linuxFlags | O_PATH, linuxMode);
1280+
status = LINUX_SYSCALL4(__NR_openat, AT_FDCWD, hostPath, linuxFlags | O_PATH, linuxMode);
12771281
}
12781282

12791283
if (status < 0)
@@ -1346,7 +1350,7 @@ int _moni_open_attr(int fd, const char* path, const char *name,
13461350
int linuxMode = OFlagsBToLinux(openMode & ~HAIKU_O_NOTRAVERSE);
13471351
int linuxPerms = 0777;
13481352

1349-
status = LINUX_SYSCALL3(__NR_open, hostPath, linuxMode, linuxPerms);
1353+
status = LINUX_SYSCALL4(__NR_openat, AT_FDCWD, hostPath, linuxMode, linuxPerms);
13501354

13511355
if (status < 0)
13521356
{
@@ -1375,7 +1379,7 @@ int _moni_open_attr_dir(int fd, const char* path, bool traverseLeafLink)
13751379
return status;
13761380
}
13771381

1378-
status = LINUX_SYSCALL2(__NR_open, hostPath, O_DIRECTORY);
1382+
status = LINUX_SYSCALL4(__NR_openat, AT_FDCWD, hostPath, O_DIRECTORY, 0);
13791383

13801384
if (status < 0)
13811385
{
@@ -1490,4 +1494,4 @@ int FlockFlagsBToLinux(int flockFlags)
14901494
linuxFlags |= LOCK_UN;
14911495
}
14921496
return linuxFlags;
1493-
}
1497+
}

monika/linux/mman.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ area_id MONIKA_EXPORT _moni_clone_area(const char *name, void **address,
185185
mmap_flags |= MAP_SHARED;
186186
mmap_flags &= ~MAP_PRIVATE;
187187

188-
int fd = LINUX_SYSCALL3(__NR_open, hostPath, open_flags, 0);
188+
int fd = LINUX_SYSCALL4(__NR_openat, AT_FDCWD, hostPath, open_flags, 0);
189189
if (fd < 0)
190190
{
191191
return LinuxToB(-fd);
@@ -1152,7 +1152,7 @@ int ShareArea(void* mappedAddr, size_t size, int areaId,
11521152
bool ownsFd = false;
11531153
if (hostPath[0] != '\0')
11541154
{
1155-
fd = LINUX_SYSCALL2(__NR_open, hostPath, open_flags);
1155+
fd = LINUX_SYSCALL4(__NR_openat, AT_FDCWD, hostPath, open_flags, 0);
11561156
offset = 0;
11571157
if (fd < 0)
11581158
{
@@ -1197,4 +1197,4 @@ int LockArea(void* mappedAddr, size_t size, uint32 lock)
11971197
}
11981198

11991199
return LinuxToB(-status);
1200-
}
1200+
}

monika/linux/realpath.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <sys/stat.h>
3131

3232
#include <errno.h>
33+
#include <fcntl.h>
3334
#include <stdlib.h>
3435
#include <string.h>
3536
#include <unistd.h>
@@ -139,7 +140,7 @@ long realpath(const char *path, char resolved[PATH_MAX], bool resolveSymlinks)
139140
{
140141
return -ENAMETOOLONG;
141142
}
142-
status = LINUX_SYSCALL2(__NR_lstat, resolved, &sb);
143+
status = LINUX_SYSCALL4(__NR_newfstatat, AT_FDCWD, resolved, &sb, AT_SYMLINK_NOFOLLOW);
143144
if (status < 0)
144145
{
145146
if (status == -ENOENT && p == NULL)
@@ -154,7 +155,11 @@ long realpath(const char *path, char resolved[PATH_MAX], bool resolveSymlinks)
154155
{
155156
return -ELOOP;
156157
}
158+
#ifdef __NR_readlink
157159
slen = LINUX_SYSCALL3(__NR_readlink, resolved, symlink, sizeof(symlink) - 1);
160+
#else
161+
slen = LINUX_SYSCALL4(__NR_readlinkat, AT_FDCWD, resolved, symlink, sizeof(symlink) - 1);
162+
#endif
158163
if (slen < 0)
159164
{
160165
return slen;
@@ -207,4 +212,4 @@ long realpath(const char *path, char resolved[PATH_MAX], bool resolveSymlinks)
207212
if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
208213
resolved[resolved_len - 1] = '\0';
209214
return (long)(resolved);
210-
}
215+
}

0 commit comments

Comments
 (0)