Skip to content

Commit

Permalink
[libc] Implement fcntl() function (#89507)
Browse files Browse the repository at this point in the history
Fixes #84968. 

Implements the `fcntl()` function defined in the `fcntl.h` header.
  • Loading branch information
vinayakdsci committed May 1, 2024
1 parent d1b3648 commit aca5117
Show file tree
Hide file tree
Showing 22 changed files with 545 additions and 0 deletions.
1 change: 1 addition & 0 deletions libc/config/linux/aarch64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(TARGET_LIBC_ENTRYPOINTS

# fcntl.h entrypoints
libc.src.fcntl.creat
libc.src.fcntl.fcntl
libc.src.fcntl.open
libc.src.fcntl.openat

Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/riscv/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(TARGET_LIBC_ENTRYPOINTS

# fcntl.h entrypoints
libc.src.fcntl.creat
libc.src.fcntl.fcntl
libc.src.fcntl.open
libc.src.fcntl.openat

Expand Down
1 change: 1 addition & 0 deletions libc/config/linux/x86_64/entrypoints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ set(TARGET_LIBC_ENTRYPOINTS

# fcntl.h entrypoints
libc.src.fcntl.creat
libc.src.fcntl.fcntl
libc.src.fcntl.open
libc.src.fcntl.openat

Expand Down
9 changes: 9 additions & 0 deletions libc/hdr/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ add_proxy_header_library(
libc.include.math
)

add_proxy_header_library(
fcntl_macros
HDRS
fcntl_macros.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-macros.fcntl_macros
libc.include.fcntl
)

add_proxy_header_library(
fenv_macros
HDRS
Expand Down
22 changes: 22 additions & 0 deletions libc/hdr/fcntl_macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===-- Definition of macros from fcntl/fcntl.h ---------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_HDR_FCNTL_MACROS_H
#define LLVM_LIBC_HDR_FCNTL_MACROS_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-macros/fcntl-macros.h"

#else // Overlay mode

#include <fcntl.h>

#endif // LLVM_LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_FCNTL_MACROS_H
24 changes: 24 additions & 0 deletions libc/hdr/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,30 @@ add_proxy_header_library(
libc.include.llvm-libc-types.struct_epoll_event
)

add_proxy_header_library(
struct_flock
HDRS
struct_flock.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_flock
)

add_proxy_header_library(
struct_flock64
HDRS
struct_flock64.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_flock64
)

add_proxy_header_library(
struct_f_owner_ex
HDRS
struct_f_owner_ex.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.struct_f_owner_ex
)

add_proxy_header_library(
struct_timespec
HDRS
Expand Down
21 changes: 21 additions & 0 deletions libc/hdr/types/struct_f_owner_ex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Proxy for struct f_owner_ex --------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_F_OWNER_EX_H
#define LLVM_LIBC_HDR_TYPES_STRUCT_F_OWNER_EX_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/struct_f_owner_ex.h"

#else

#include <fcntl.h>

#endif // LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_STRUCT_F_OWNER_EX_H
21 changes: 21 additions & 0 deletions libc/hdr/types/struct_flock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Proxy for struct flock -------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK_H
#define LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/struct_flock.h"

#else

#include <fcntl.h>

#endif // LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK_H
21 changes: 21 additions & 0 deletions libc/hdr/types/struct_flock64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//===-- Proxy for struct flock64 -----------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK64_H
#define LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK64_H

#ifdef LIBC_FULL_BUILD

#include "include/llvm-libc-types/struct_flock64.h"

#else

#include <fcntl.h>

#endif // LIBC_FULL_BUILD

#endif // LLVM_LIBC_HDR_TYPES_STRUCT_FLOCK64_H
4 changes: 4 additions & 0 deletions libc/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ add_gen_header(
DEPENDS
.llvm-libc-macros.fcntl_macros
.llvm-libc-types.mode_t
.llvm-libc-types.struct_flock
.llvm-libc-types.struct_flock64
.llvm-libc-types.off64_t
.llvm-libc-types.pid_t
.llvm-libc-types.off_t
.llvm_libc_common_h
)
Expand Down
31 changes: 31 additions & 0 deletions libc/include/llvm-libc-macros/linux/fcntl-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,36 @@
#define F_SETFD 2
#define F_GETFL 3
#define F_SETFL 4
#define F_GETLK 5
#define F_SETLK 6
#define F_SETLKW 7
#define F_SETOWN 8
#define F_GETOWN 9
#define F_SETSIG 10
#define F_GETSIG 11
#define F_GETLK64 12
#define F_SETLK64 13
#define F_SETLKW64 14
#define F_SETOWN_EX 15
#define F_GETOWN_EX 16

// Open File Description Locks.
#define F_OFD_GETLK 36
#define F_OFD_SETLK 37
#define F_OFD_SETLKW 38

// Close on succesful
#define F_CLOEXEC 1

#define F_RDLCK 0
#define F_WRLCK 1
#define F_UNLCK 2

// For Large File Support
#if defined(_LARGEFILE64_SOURCE)
#define F_GETLK F_GETLK64
#define F_SETLK F_SETLK64
#define F_SETLKW F_SETLKW64
#endif

#endif // LLVM_LIBC_MACROS_LINUX_FCNTL_MACROS_H
3 changes: 3 additions & 0 deletions libc/include/llvm-libc-types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ add_header(rlim_t HDR rlim_t.h)
add_header(time_t HDR time_t.h)
add_header(stack_t HDR stack_t.h)
add_header(suseconds_t HDR suseconds_t.h)
add_header(struct_flock HDR struct_flock.h DEPENDS .off_t .pid_t)
add_header(struct_flock64 HDR struct_flock64.h DEPENDS .off64_t .pid_t)
add_header(struct_f_owner_ex HDR struct_f_owner_ex.h DEPENDS .pid_t)
add_header(struct_timeval HDR struct_timeval.h DEPENDS .suseconds_t .time_t)
add_header(struct_rlimit HDR struct_rlimit.h DEPENDS .rlim_t)
add_header(struct_rusage HDR struct_rusage.h DEPENDS .struct_timeval)
Expand Down
25 changes: 25 additions & 0 deletions libc/include/llvm-libc-types/struct_f_owner_ex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Definition of type struct f_owner_ex ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_TYPES_STRUCT_F_OWNER_EX_H
#define LLVM_LIBC_TYPES_STRUCT_F_OWNER_EX_H

#include "llvm-libc-types/pid_t.h"

enum pid_type {
F_OWNER_TID = 0,
F_OWNER_PID,
F_OWNER_PGRP,
};

struct f_owner_ex {
enum pid_type type;
pid_t pid;
};

#endif // LLVM_LIBC_TYPES_STRUCT_F_OWNER_EX_H
25 changes: 25 additions & 0 deletions libc/include/llvm-libc-types/struct_flock.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Definition of type struct flock64 ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_TYPES_STRUCT_FLOCK_H
#define LLVM_LIBC_TYPES_STRUCT_FLOCK_H

#include "llvm-libc-types/off_t.h"
#include "llvm-libc-types/pid_t.h"

#include <stdint.h>

struct flock {
int16_t l_type;
int16_t l_whence;
off_t l_start;
off_t l_len;
pid_t l_pid;
};

#endif // LLVM_LIBC_TYPES_STRUCT_FLOCK_H
25 changes: 25 additions & 0 deletions libc/include/llvm-libc-types/struct_flock64.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//===-- Definition of type struct flock64 ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_TYPES_STRUCT_FLOCK64_H
#define LLVM_LIBC_TYPES_STRUCT_FLOCK64_H

#include "llvm-libc-types/off64_t.h"
#include "llvm-libc-types/pid_t.h"

#include <stdint.h>

struct flock64 {
int16_t l_type;
int16_t l_whence;
off64_t l_start;
off64_t l_len;
pid_t l_pid;
};

#endif // LLVM_LIBC_TYPES_STRUCT_FLOCK64_H
5 changes: 5 additions & 0 deletions libc/spec/posix.td
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ def POSIX : StandardSpec<"POSIX"> {
RetValSpec<IntType>,
[ArgSpec<ConstCharPtr>, ArgSpec<ModeTType>]
>,
FunctionSpec<
"fcntl",
RetValSpec<IntType>,
[ArgSpec<IntType>, ArgSpec<IntType>, ArgSpec<VarArgType>]
>,
FunctionSpec<
"open",
RetValSpec<IntType>,
Expand Down
7 changes: 7 additions & 0 deletions libc/src/fcntl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ add_entrypoint_object(
.${LIBC_TARGET_OS}.creat
)

add_entrypoint_object(
fcntl
ALIAS
DEPENDS
.${LIBC_TARGET_OS}.fcntl
)

add_entrypoint_object(
open
ALIAS
Expand Down
18 changes: 18 additions & 0 deletions libc/src/fcntl/fcntl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===-- Implementation header of fcntl --------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef LLVM_LIBC_SRC_FCNTL_FCNTL_H
#define LLVM_LIBC_SRC_FCNTL_FCNTL_H

namespace LIBC_NAMESPACE {

int fcntl(int fd, int cmd, ...);

} // namespace LIBC_NAMESPACE

#endif // LLVM_LIBC_SRC_FCNTL_FCNTL_H
16 changes: 16 additions & 0 deletions libc/src/fcntl/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ add_entrypoint_object(
libc.src.errno.errno
)

add_entrypoint_object(
fcntl
SRCS
fcntl.cpp
HDRS
../fcntl.h
DEPENDS
libc.include.fcntl
libc.hdr.types.struct_flock
libc.hdr.types.struct_flock64
libc.hdr.types.struct_f_owner_ex
libc.hdr.fcntl_macros
libc.src.__support.OSUtil.osutil
libc.src.errno.errno
)

add_entrypoint_object(
open
SRCS
Expand Down

0 comments on commit aca5117

Please sign in to comment.