Skip to content

Commit b6a0802

Browse files
[libc][NFC] Clean up utimes and setsid (#155495)
Simplify utims a bit and add proper error handling to setsid as described in the standard
1 parent defbd5b commit b6a0802

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

libc/src/sys/time/linux/utimes.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,21 @@
2121

2222
namespace LIBC_NAMESPACE_DECL {
2323

24-
#ifdef SYS_utimes
25-
constexpr auto UTIMES_SYSCALL_ID = SYS_utimes;
26-
#elif defined(SYS_utimensat)
27-
constexpr auto UTIMES_SYSCALL_ID = SYS_utimensat;
28-
#elif defined(SYS_utimensat_time64)
29-
constexpr auto UTIMES_SYSCALL_ID = SYS_utimensat_time64;
30-
#else
31-
#error "utimes, utimensat, utimensat_time64, syscalls not available."
32-
#endif
33-
3424
LLVM_LIBC_FUNCTION(int, utimes,
3525
(const char *path, const struct timeval times[2])) {
3626
int ret;
3727

3828
#ifdef SYS_utimes
3929
// No need to define a timespec struct, use the syscall directly.
40-
ret = LIBC_NAMESPACE::syscall_impl<int>(UTIMES_SYSCALL_ID, path, times);
30+
ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_utimes, path, times);
4131
#elif defined(SYS_utimensat) || defined(SYS_utimensat_time64)
32+
33+
#if defined(SYS_utimensat)
34+
constexpr auto UTIMES_SYSCALL_ID = SYS_utimensat;
35+
#elif defined(SYS_utimensat_time64)
36+
constexpr auto UTIMES_SYSCALL_ID = SYS_utimensat_time64;
37+
#endif
38+
4239
// the utimensat syscall requires a timespec struct, not timeval.
4340
struct timespec ts[2];
4441
struct timespec *ts_ptr = nullptr; // default value if times is nullptr
@@ -74,6 +71,9 @@ LLVM_LIBC_FUNCTION(int, utimes,
7471
// flags=0 means don't follow symlinks (like utimes)
7572
ret = LIBC_NAMESPACE::syscall_impl<int>(UTIMES_SYSCALL_ID, AT_FDCWD, path,
7673
ts_ptr, 0);
74+
75+
#else
76+
#error "utimes, utimensat, utimensat_time64, syscalls not available."
7777
#endif // SYS_utimensat
7878

7979
if (ret < 0) {

libc/src/unistd/linux/setsid.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@
1111
#include "hdr/types/pid_t.h"
1212
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1313
#include "src/__support/common.h"
14+
#include "src/__support/libc_errno.h"
1415
#include "src/__support/macros/config.h"
1516

1617
#include <sys/syscall.h> // For syscall numbers.
1718

1819
namespace LIBC_NAMESPACE_DECL {
1920

2021
LLVM_LIBC_FUNCTION(pid_t, setsid, ()) {
21-
return LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_setsid);
22+
pid_t ret = LIBC_NAMESPACE::syscall_impl<pid_t>(SYS_setsid);
23+
if (ret < 0) {
24+
libc_errno = static_cast<int>(-ret);
25+
return -1;
26+
}
27+
return ret;
2228
}
2329

2430
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)