unistd: Improve fortify with clang
[glibc.git] / elf / dl-write.c
blob401391b16a50ef2e9fbe7d4cbc51027bea0f17ed
1 /* Implementation of the _dl_write function. Generic version.
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <errno.h>
20 #include <ldsodefs.h>
21 #include <libc-lock.h>
22 #include <sys/uio.h>
24 ssize_t
25 _dl_write (int fd, const void *buffer, size_t length)
27 struct iovec iov = { .iov_base = (void *) buffer, .iov_len = length };
28 ssize_t ret;
30 #if RTLD_PRIVATE_ERRNO
31 /* We have to take this lock just to be sure we don't clobber the private
32 errno when it's being used by another thread that cares about it.
33 Yet we must be sure not to try calling the lock functions before
34 the thread library is fully initialized. */
35 if (__glibc_unlikely (_dl_starting_up))
37 ret = __writev (fd, &iov, 1);
38 if (ret < 0)
39 ret = -errno;
41 else
43 __rtld_lock_lock_recursive (GL(dl_load_lock));
44 ret = __writev (fd, &iov, 1);
45 if (ret < 0)
46 ret = -errno;
47 __rtld_lock_unlock_recursive (GL(dl_load_lock));
49 #else
50 ret = __writev (fd, &iov, 1);
51 if (ret < 0)
52 ret = -errno;
53 #endif
55 return ret;