tests/server: make the signal handler signal-safe

Before this patch the signal handler called `logmsg()` which in turn
called `printf()` variants (internal implementations), and `FILE *`
functions, `localtime()`. Some of these called `malloc`/`free`, which
isn't supported in s signal handler. Replace them with `write` calls,
losing some logging functionality.

Also:
- De-dupe and move `STD*_FILENO` macros to `lib/curl_setup.h`. Revert
  the `src` definition to point to `stderr`, instead of `tool_stderr`.
  Follow-up to e5bb88b8f8 #11958

POSIX specs with list of functions allowed in a signal handler:
2004: https://pubs.opengroup.org/onlinepubs/009695399/functions/xsh_chap02_04.html#tag_02_04_03
2017: https://pubs.opengroup.org/onlinepubs/9699919799/functions/V2_chap02.html#tag_15_04_03
2024: https://pubs.opengroup.org/onlinepubs/9799919799/functions/V2_chap02.html#tag_16_04_03

Linux CI run with the thread sanitizer going crazy when
hitting the signal handler in test 1238 and 1242 (TFTP):
```
WARNING: ThreadSanitizer: signal-unsafe call inside of a signal (pid=12582)
    #0 malloc <null> (servers+0x5ed70)
    #1 _IO_file_doallocate <null> (libc.so.6+0x851b4)
    #2 formatf /home/runner/work/curl/curl/bld/tests/server/../../lib/../../lib/mprintf.c:886:9 (servers+0xdff77)
[...]
WARNING: ThreadSanitizer: signal-unsafe call inside of a signal (pid=12582)
    #0 free <null> (servers+0x5f453)
    #1 fclose <null> (libc.so.6+0x8532f)
    #2 logmsg /home/runner/work/curl/curl/bld/tests/server/../../../tests/server/util.c:134:5 (servers+0xe684d)
```
Ref: https://github.com/curl/curl/actions/runs/14118903372/job/39555309490?pr=16851

Closes #16852
This commit is contained in:
Viktor Szakats 2025-03-28 01:41:28 +01:00
parent 3b6c7142f6
commit e95f509c66
No known key found for this signature in database
GPG key ID: B5ABD165E2AEF201
5 changed files with 46 additions and 29 deletions

View file

@ -862,6 +862,8 @@
#define EINVAL 22
#define ENOSPC 28
#define strerror(x) "?"
#undef STDIN_FILENO
#define STDIN_FILENO 0
#else
#define CURL_SETERRNO(x) (errno = (x))
#endif
@ -910,6 +912,17 @@
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif
/* For MSVC (all versions as of VS2022) */
#ifndef STDIN_FILENO
#define STDIN_FILENO fileno(stdin)
#endif
#ifndef STDOUT_FILENO
#define STDOUT_FILENO fileno(stdout)
#endif
#ifndef STDERR_FILENO
#define STDERR_FILENO fileno(stderr)
#endif
/* Since O_BINARY is used in bitmasks, setting it to zero makes it usable in
source code but yet it does not ruin anything */
#ifdef O_BINARY