Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions libc/hdr/types/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,6 @@ add_proxy_header_library(
libc.include.locale
)

add_proxy_header_library(
sighandler_t
HDRS
sighandler_t.h
FULL_BUILD_DEPENDS
libc.include.llvm-libc-types.__sighandler_t
libc.include.signal
)

add_proxy_header_library(
stack_t
HDRS
Expand Down
24 changes: 0 additions & 24 deletions libc/hdr/types/sighandler_t.h

This file was deleted.

6 changes: 3 additions & 3 deletions libc/include/llvm-libc-macros/gpu/signal-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
#define SIGSEGV 11
#define SIGTERM 15

#define SIG_DFL ((__sighandler_t)(0))
#define SIG_IGN ((__sighandler_t)(1))
#define SIG_ERR ((__sighandler_t)(-1))
#define SIG_DFL ((void (*)(int))(0))
#define SIG_IGN ((void (*)(int))(1))
#define SIG_ERR ((void (*)(int))(-1))

// Max signal number
#define NSIG 64
Expand Down
6 changes: 3 additions & 3 deletions libc/include/llvm-libc-macros/linux/signal-macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@
#error "Signal stack sizes not defined for your platform."
#endif

#define SIG_DFL ((__sighandler_t)0)
#define SIG_IGN ((__sighandler_t)1)
#define SIG_ERR ((__sighandler_t)-1)
#define SIG_DFL ((void (*)(int))0)
#define SIG_IGN ((void (*)(int))1)
#define SIG_ERR ((void (*)(int))-1)

// SIGCHLD si_codes
#define CLD_EXITED 1 // child has exited
Expand Down
5 changes: 5 additions & 0 deletions libc/include/llvm-libc-types/__sighandler_t.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@
#ifndef LLVM_LIBC_TYPES___SIGHANDLER_T_H
#define LLVM_LIBC_TYPES___SIGHANDLER_T_H

#ifndef __linux__
#error "sighandler_t only available on linux"
#endif

typedef void (*__sighandler_t)(int);
typedef __sighandler_t sighandler_t;

#endif // LLVM_LIBC_TYPES___SIGHANDLER_T_H
2 changes: 0 additions & 2 deletions libc/include/llvm-libc-types/struct_sigaction.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@ struct sigaction {
#endif
};

typedef void (*__sighandler_t)(int);

#endif // LLVM_LIBC_TYPES_STRUCT_SIGACTION_H
4 changes: 4 additions & 0 deletions libc/include/signal.h.def
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@

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

#ifdef __linux__
#include "llvm-libc-types/__sighandler_t.h"
#endif

%%public_api()

#endif // LLVM_LIBC_SIGNAL_H
15 changes: 10 additions & 5 deletions libc/include/signal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ header_template: signal.h.def
macros: []
types:
- type_name: pid_t
- type_name: stack_t
- type_name: sig_atomic_t
- type_name: siginfo_t
- type_name: struct_sigaction
- type_name: sigset_t
- type_name: stack_t
- type_name: struct_sigaction
- type_name: union_sigval
- type_name: sig_atomic_t
enums: []
objects: []
functions:
Expand Down Expand Up @@ -69,10 +69,15 @@ functions:
- name: signal
standards:
- stdc
return_type: __sighandler_t
# May the Geneva Convention have mercy on my soul... Why this insanity?
# Well: signal returns a function pointer to a function with no return
# value and which accepts an int. The parameter list appears on the far
# right of the declaration. i.e.
# void (*signal(int, void (*)(int)))(int);
return_type: void (*
arguments:
- type: int
- type: __sighandler_t
- type: void (*)(int)))(int
- name: sigprocmask
standards:
- POSIX
Expand Down
1 change: 0 additions & 1 deletion libc/src/signal/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ add_entrypoint_object(
DEPENDS
.sigaction
libc.hdr.signal_macros
libc.hdr.types.sighandler_t
)

add_entrypoint_object(
Expand Down
6 changes: 4 additions & 2 deletions libc/src/signal/linux/signal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@

#include "src/signal/signal.h"
#include "hdr/signal_macros.h"
#include "hdr/types/sighandler_t.h"
#include "src/__support/common.h"
#include "src/__support/macros/config.h"
#include "src/signal/sigaction.h"

namespace LIBC_NAMESPACE_DECL {

LLVM_LIBC_FUNCTION(sighandler_t, signal, (int signum, sighandler_t handler)) {
// Our LLVM_LIBC_FUNCTION macro doesn't handle function pointer return types.
using signal_handler = void (*)(int);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps decltype would be better here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm...no nevermind. We'd only be able to do decltype(signal), which would not be the type of the parameter or return value.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps auto would work...

Copy link
Member Author

@nickdesaulniers nickdesaulniers Feb 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, no, I get an error related to overload resolution. Ok will leave for now.
error: functions that differ only in their return type cannot be overloaded


LLVM_LIBC_FUNCTION(signal_handler, signal, (int signum, signal_handler handler)) {
struct sigaction action, old;
action.sa_handler = handler;
action.sa_flags = SA_RESTART;
Expand Down
3 changes: 1 addition & 2 deletions libc/src/signal/signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@
#ifndef LLVM_LIBC_SRC_SIGNAL_SIGNAL_H
#define LLVM_LIBC_SRC_SIGNAL_SIGNAL_H

#include "hdr/types/sighandler_t.h"
#include "src/__support/macros/config.h"

namespace LIBC_NAMESPACE_DECL {

sighandler_t signal(int signum, sighandler_t handler);
void (*signal(int signum, void (*handler)(int)))(int);

} // namespace LIBC_NAMESPACE_DECL

Expand Down
2 changes: 1 addition & 1 deletion libc/test/UnitTest/FPExceptMatcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static void sigfpeHandler(int sig) {
}

FPExceptMatcher::FPExceptMatcher(FunctionCaller *func) {
sighandler_t oldSIGFPEHandler = signal(SIGFPE, &sigfpeHandler);
auto *oldSIGFPEHandler = signal(SIGFPE, &sigfpeHandler);

caughtExcept = false;
fenv_t oldEnv;
Expand Down
1 change: 0 additions & 1 deletion libc/test/src/signal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ add_libc_unittest(
SRCS
signal_test.cpp
DEPENDS
libc.hdr.types.sighandler_t
libc.src.errno.errno
libc.src.signal.raise
libc.src.signal.signal
Expand Down
4 changes: 1 addition & 3 deletions libc/test/src/signal/signal_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@
#include "test/UnitTest/ErrnoSetterMatcher.h"
#include "test/UnitTest/Test.h"

#include "hdr/types/sighandler_t.h"

using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Fails;
using LIBC_NAMESPACE::testing::ErrnoSetterMatcher::Succeeds;

TEST(LlvmLibcSignal, Invalid) {
LIBC_NAMESPACE::libc_errno = 0;
sighandler_t valid = +[](int) {};
auto *valid = +[](int) {};
EXPECT_THAT((void *)LIBC_NAMESPACE::signal(0, valid),
Fails(EINVAL, (void *)SIG_ERR));
EXPECT_THAT((void *)LIBC_NAMESPACE::signal(65, valid),
Expand Down
Loading