unistd.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* Checking macros for unistd functions.
  2. Copyright (C) 2005-2021 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #ifndef _UNISTD_H
  16. # error "Never include <bits/unistd.h> directly; use <unistd.h> instead."
  17. #endif
  18. extern ssize_t __read_chk (int __fd, void *__buf, size_t __nbytes,
  19. size_t __buflen)
  20. __wur __attr_access ((__write_only__, 2, 3));
  21. extern ssize_t __REDIRECT (__read_alias, (int __fd, void *__buf,
  22. size_t __nbytes), read)
  23. __wur __attr_access ((__write_only__, 2, 3));
  24. extern ssize_t __REDIRECT (__read_chk_warn,
  25. (int __fd, void *__buf, size_t __nbytes,
  26. size_t __buflen), __read_chk)
  27. __wur __warnattr ("read called with bigger length than size of "
  28. "the destination buffer");
  29. __fortify_function __wur ssize_t
  30. read (int __fd, void *__buf, size_t __nbytes)
  31. {
  32. if (__glibc_objsize0 (__buf) != (size_t) -1)
  33. {
  34. if (!__builtin_constant_p (__nbytes))
  35. return __read_chk (__fd, __buf, __nbytes, __glibc_objsize0 (__buf));
  36. if (__nbytes > __glibc_objsize0 (__buf))
  37. return __read_chk_warn (__fd, __buf, __nbytes,
  38. __glibc_objsize0 (__buf));
  39. }
  40. return __read_alias (__fd, __buf, __nbytes);
  41. }
  42. #ifdef __USE_UNIX98
  43. extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
  44. __off_t __offset, size_t __bufsize)
  45. __wur __attr_access ((__write_only__, 2, 3));
  46. extern ssize_t __pread64_chk (int __fd, void *__buf, size_t __nbytes,
  47. __off64_t __offset, size_t __bufsize)
  48. __wur __attr_access ((__write_only__, 2, 3));
  49. extern ssize_t __REDIRECT (__pread_alias,
  50. (int __fd, void *__buf, size_t __nbytes,
  51. __off_t __offset), pread)
  52. __wur __attr_access ((__write_only__, 2, 3));
  53. extern ssize_t __REDIRECT (__pread64_alias,
  54. (int __fd, void *__buf, size_t __nbytes,
  55. __off64_t __offset), pread64)
  56. __wur __attr_access ((__write_only__, 2, 3));
  57. extern ssize_t __REDIRECT (__pread_chk_warn,
  58. (int __fd, void *__buf, size_t __nbytes,
  59. __off_t __offset, size_t __bufsize), __pread_chk)
  60. __wur __warnattr ("pread called with bigger length than size of "
  61. "the destination buffer");
  62. extern ssize_t __REDIRECT (__pread64_chk_warn,
  63. (int __fd, void *__buf, size_t __nbytes,
  64. __off64_t __offset, size_t __bufsize),
  65. __pread64_chk)
  66. __wur __warnattr ("pread64 called with bigger length than size of "
  67. "the destination buffer");
  68. # ifndef __USE_FILE_OFFSET64
  69. __fortify_function __wur ssize_t
  70. pread (int __fd, void *__buf, size_t __nbytes, __off_t __offset)
  71. {
  72. if (__glibc_objsize0 (__buf) != (size_t) -1)
  73. {
  74. if (!__builtin_constant_p (__nbytes))
  75. return __pread_chk (__fd, __buf, __nbytes, __offset,
  76. __glibc_objsize0 (__buf));
  77. if ( __nbytes > __glibc_objsize0 (__buf))
  78. return __pread_chk_warn (__fd, __buf, __nbytes, __offset,
  79. __glibc_objsize0 (__buf));
  80. }
  81. return __pread_alias (__fd, __buf, __nbytes, __offset);
  82. }
  83. # else
  84. __fortify_function __wur ssize_t
  85. pread (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
  86. {
  87. if (__glibc_objsize0 (__buf) != (size_t) -1)
  88. {
  89. if (!__builtin_constant_p (__nbytes))
  90. return __pread64_chk (__fd, __buf, __nbytes, __offset,
  91. __glibc_objsize0 (__buf));
  92. if ( __nbytes > __glibc_objsize0 (__buf))
  93. return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
  94. __glibc_objsize0 (__buf));
  95. }
  96. return __pread64_alias (__fd, __buf, __nbytes, __offset);
  97. }
  98. # endif
  99. # ifdef __USE_LARGEFILE64
  100. __fortify_function __wur ssize_t
  101. pread64 (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
  102. {
  103. if (__glibc_objsize0 (__buf) != (size_t) -1)
  104. {
  105. if (!__builtin_constant_p (__nbytes))
  106. return __pread64_chk (__fd, __buf, __nbytes, __offset,
  107. __glibc_objsize0 (__buf));
  108. if ( __nbytes > __glibc_objsize0 (__buf))
  109. return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
  110. __glibc_objsize0 (__buf));
  111. }
  112. return __pread64_alias (__fd, __buf, __nbytes, __offset);
  113. }
  114. # endif
  115. #endif
  116. #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K
  117. extern ssize_t __readlink_chk (const char *__restrict __path,
  118. char *__restrict __buf, size_t __len,
  119. size_t __buflen)
  120. __THROW __nonnull ((1, 2)) __wur __attr_access ((__write_only__, 2, 3));
  121. extern ssize_t __REDIRECT_NTH (__readlink_alias,
  122. (const char *__restrict __path,
  123. char *__restrict __buf, size_t __len), readlink)
  124. __nonnull ((1, 2)) __wur __attr_access ((__write_only__, 2, 3));
  125. extern ssize_t __REDIRECT_NTH (__readlink_chk_warn,
  126. (const char *__restrict __path,
  127. char *__restrict __buf, size_t __len,
  128. size_t __buflen), __readlink_chk)
  129. __nonnull ((1, 2)) __wur __warnattr ("readlink called with bigger length "
  130. "than size of destination buffer");
  131. __fortify_function __nonnull ((1, 2)) __wur ssize_t
  132. __NTH (readlink (const char *__restrict __path, char *__restrict __buf,
  133. size_t __len))
  134. {
  135. if (__glibc_objsize (__buf) != (size_t) -1)
  136. {
  137. if (!__builtin_constant_p (__len))
  138. return __readlink_chk (__path, __buf, __len, __glibc_objsize (__buf));
  139. if ( __len > __glibc_objsize (__buf))
  140. return __readlink_chk_warn (__path, __buf, __len,
  141. __glibc_objsize (__buf));
  142. }
  143. return __readlink_alias (__path, __buf, __len);
  144. }
  145. #endif
  146. #ifdef __USE_ATFILE
  147. extern ssize_t __readlinkat_chk (int __fd, const char *__restrict __path,
  148. char *__restrict __buf, size_t __len,
  149. size_t __buflen)
  150. __THROW __nonnull ((2, 3)) __wur __attr_access ((__write_only__, 3, 4));
  151. extern ssize_t __REDIRECT_NTH (__readlinkat_alias,
  152. (int __fd, const char *__restrict __path,
  153. char *__restrict __buf, size_t __len),
  154. readlinkat)
  155. __nonnull ((2, 3)) __wur __attr_access ((__write_only__, 3, 4));
  156. extern ssize_t __REDIRECT_NTH (__readlinkat_chk_warn,
  157. (int __fd, const char *__restrict __path,
  158. char *__restrict __buf, size_t __len,
  159. size_t __buflen), __readlinkat_chk)
  160. __nonnull ((2, 3)) __wur __warnattr ("readlinkat called with bigger "
  161. "length than size of destination "
  162. "buffer");
  163. __fortify_function __nonnull ((2, 3)) __wur ssize_t
  164. __NTH (readlinkat (int __fd, const char *__restrict __path,
  165. char *__restrict __buf, size_t __len))
  166. {
  167. if (__glibc_objsize (__buf) != (size_t) -1)
  168. {
  169. if (!__builtin_constant_p (__len))
  170. return __readlinkat_chk (__fd, __path, __buf, __len,
  171. __glibc_objsize (__buf));
  172. if (__len > __glibc_objsize (__buf))
  173. return __readlinkat_chk_warn (__fd, __path, __buf, __len,
  174. __glibc_objsize (__buf));
  175. }
  176. return __readlinkat_alias (__fd, __path, __buf, __len);
  177. }
  178. #endif
  179. extern char *__getcwd_chk (char *__buf, size_t __size, size_t __buflen)
  180. __THROW __wur __attr_access ((__write_only__, 1, 2));
  181. extern char *__REDIRECT_NTH (__getcwd_alias,
  182. (char *__buf, size_t __size), getcwd)
  183. __wur __attr_access ((__write_only__, 1, 2));
  184. extern char *__REDIRECT_NTH (__getcwd_chk_warn,
  185. (char *__buf, size_t __size, size_t __buflen),
  186. __getcwd_chk)
  187. __wur __warnattr ("getcwd caller with bigger length than size of "
  188. "destination buffer");
  189. __fortify_function __wur char *
  190. __NTH (getcwd (char *__buf, size_t __size))
  191. {
  192. if (__glibc_objsize (__buf) != (size_t) -1)
  193. {
  194. if (!__builtin_constant_p (__size))
  195. return __getcwd_chk (__buf, __size, __glibc_objsize (__buf));
  196. if (__size > __glibc_objsize (__buf))
  197. return __getcwd_chk_warn (__buf, __size, __glibc_objsize (__buf));
  198. }
  199. return __getcwd_alias (__buf, __size);
  200. }
  201. #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
  202. extern char *__getwd_chk (char *__buf, size_t buflen)
  203. __THROW __nonnull ((1)) __wur __attr_access ((__write_only__, 1, 2));
  204. extern char *__REDIRECT_NTH (__getwd_warn, (char *__buf), getwd)
  205. __nonnull ((1)) __wur __warnattr ("please use getcwd instead, as getwd "
  206. "doesn't specify buffer size");
  207. __fortify_function __nonnull ((1)) __attribute_deprecated__ __wur char *
  208. __NTH (getwd (char *__buf))
  209. {
  210. if (__glibc_objsize (__buf) != (size_t) -1)
  211. return __getwd_chk (__buf, __glibc_objsize (__buf));
  212. return __getwd_warn (__buf);
  213. }
  214. #endif
  215. extern size_t __confstr_chk (int __name, char *__buf, size_t __len,
  216. size_t __buflen) __THROW
  217. __attr_access ((__write_only__, 2, 3));
  218. extern size_t __REDIRECT_NTH (__confstr_alias, (int __name, char *__buf,
  219. size_t __len), confstr)
  220. __attr_access ((__write_only__, 2, 3));
  221. extern size_t __REDIRECT_NTH (__confstr_chk_warn,
  222. (int __name, char *__buf, size_t __len,
  223. size_t __buflen), __confstr_chk)
  224. __warnattr ("confstr called with bigger length than size of destination "
  225. "buffer");
  226. __fortify_function size_t
  227. __NTH (confstr (int __name, char *__buf, size_t __len))
  228. {
  229. if (__glibc_objsize (__buf) != (size_t) -1)
  230. {
  231. if (!__builtin_constant_p (__len))
  232. return __confstr_chk (__name, __buf, __len, __glibc_objsize (__buf));
  233. if (__glibc_objsize (__buf) < __len)
  234. return __confstr_chk_warn (__name, __buf, __len,
  235. __glibc_objsize (__buf));
  236. }
  237. return __confstr_alias (__name, __buf, __len);
  238. }
  239. extern int __getgroups_chk (int __size, __gid_t __list[], size_t __listlen)
  240. __THROW __wur __attr_access ((__write_only__, 2, 1));
  241. extern int __REDIRECT_NTH (__getgroups_alias, (int __size, __gid_t __list[]),
  242. getgroups) __wur __attr_access ((__write_only__, 2, 1));
  243. extern int __REDIRECT_NTH (__getgroups_chk_warn,
  244. (int __size, __gid_t __list[], size_t __listlen),
  245. __getgroups_chk)
  246. __wur __warnattr ("getgroups called with bigger group count than what "
  247. "can fit into destination buffer");
  248. __fortify_function int
  249. __NTH (getgroups (int __size, __gid_t __list[]))
  250. {
  251. if (__glibc_objsize (__list) != (size_t) -1)
  252. {
  253. if (!__builtin_constant_p (__size) || __size < 0)
  254. return __getgroups_chk (__size, __list, __glibc_objsize (__list));
  255. if (__size * sizeof (__gid_t) > __glibc_objsize (__list))
  256. return __getgroups_chk_warn (__size, __list, __glibc_objsize (__list));
  257. }
  258. return __getgroups_alias (__size, __list);
  259. }
  260. extern int __ttyname_r_chk (int __fd, char *__buf, size_t __buflen,
  261. size_t __nreal) __THROW __nonnull ((2))
  262. __attr_access ((__write_only__, 2, 3));
  263. extern int __REDIRECT_NTH (__ttyname_r_alias, (int __fd, char *__buf,
  264. size_t __buflen), ttyname_r)
  265. __nonnull ((2));
  266. extern int __REDIRECT_NTH (__ttyname_r_chk_warn,
  267. (int __fd, char *__buf, size_t __buflen,
  268. size_t __nreal), __ttyname_r_chk)
  269. __nonnull ((2)) __warnattr ("ttyname_r called with bigger buflen than "
  270. "size of destination buffer");
  271. __fortify_function int
  272. __NTH (ttyname_r (int __fd, char *__buf, size_t __buflen))
  273. {
  274. if (__glibc_objsize (__buf) != (size_t) -1)
  275. {
  276. if (!__builtin_constant_p (__buflen))
  277. return __ttyname_r_chk (__fd, __buf, __buflen,
  278. __glibc_objsize (__buf));
  279. if (__buflen > __glibc_objsize (__buf))
  280. return __ttyname_r_chk_warn (__fd, __buf, __buflen,
  281. __glibc_objsize (__buf));
  282. }
  283. return __ttyname_r_alias (__fd, __buf, __buflen);
  284. }
  285. #ifdef __USE_POSIX199506
  286. extern int __getlogin_r_chk (char *__buf, size_t __buflen, size_t __nreal)
  287. __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
  288. extern int __REDIRECT (__getlogin_r_alias, (char *__buf, size_t __buflen),
  289. getlogin_r) __nonnull ((1));
  290. extern int __REDIRECT (__getlogin_r_chk_warn,
  291. (char *__buf, size_t __buflen, size_t __nreal),
  292. __getlogin_r_chk)
  293. __nonnull ((1)) __warnattr ("getlogin_r called with bigger buflen than "
  294. "size of destination buffer");
  295. __fortify_function int
  296. getlogin_r (char *__buf, size_t __buflen)
  297. {
  298. if (__glibc_objsize (__buf) != (size_t) -1)
  299. {
  300. if (!__builtin_constant_p (__buflen))
  301. return __getlogin_r_chk (__buf, __buflen, __glibc_objsize (__buf));
  302. if (__buflen > __glibc_objsize (__buf))
  303. return __getlogin_r_chk_warn (__buf, __buflen,
  304. __glibc_objsize (__buf));
  305. }
  306. return __getlogin_r_alias (__buf, __buflen);
  307. }
  308. #endif
  309. #if defined __USE_MISC || defined __USE_UNIX98
  310. extern int __gethostname_chk (char *__buf, size_t __buflen, size_t __nreal)
  311. __THROW __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
  312. extern int __REDIRECT_NTH (__gethostname_alias, (char *__buf, size_t __buflen),
  313. gethostname)
  314. __nonnull ((1)) __attr_access ((__write_only__, 1, 2));
  315. extern int __REDIRECT_NTH (__gethostname_chk_warn,
  316. (char *__buf, size_t __buflen, size_t __nreal),
  317. __gethostname_chk)
  318. __nonnull ((1)) __warnattr ("gethostname called with bigger buflen than "
  319. "size of destination buffer");
  320. __fortify_function int
  321. __NTH (gethostname (char *__buf, size_t __buflen))
  322. {
  323. if (__glibc_objsize (__buf) != (size_t) -1)
  324. {
  325. if (!__builtin_constant_p (__buflen))
  326. return __gethostname_chk (__buf, __buflen, __glibc_objsize (__buf));
  327. if (__buflen > __glibc_objsize (__buf))
  328. return __gethostname_chk_warn (__buf, __buflen,
  329. __glibc_objsize (__buf));
  330. }
  331. return __gethostname_alias (__buf, __buflen);
  332. }
  333. #endif
  334. #if defined __USE_MISC || (defined __USE_XOPEN && !defined __USE_UNIX98)
  335. extern int __getdomainname_chk (char *__buf, size_t __buflen, size_t __nreal)
  336. __THROW __nonnull ((1)) __wur __attr_access ((__write_only__, 1, 2));
  337. extern int __REDIRECT_NTH (__getdomainname_alias, (char *__buf,
  338. size_t __buflen),
  339. getdomainname) __nonnull ((1))
  340. __wur __attr_access ((__write_only__, 1, 2));
  341. extern int __REDIRECT_NTH (__getdomainname_chk_warn,
  342. (char *__buf, size_t __buflen, size_t __nreal),
  343. __getdomainname_chk)
  344. __nonnull ((1)) __wur __warnattr ("getdomainname called with bigger "
  345. "buflen than size of destination "
  346. "buffer");
  347. __fortify_function int
  348. __NTH (getdomainname (char *__buf, size_t __buflen))
  349. {
  350. if (__glibc_objsize (__buf) != (size_t) -1)
  351. {
  352. if (!__builtin_constant_p (__buflen))
  353. return __getdomainname_chk (__buf, __buflen, __glibc_objsize (__buf));
  354. if (__buflen > __glibc_objsize (__buf))
  355. return __getdomainname_chk_warn (__buf, __buflen,
  356. __glibc_objsize (__buf));
  357. }
  358. return __getdomainname_alias (__buf, __buflen);
  359. }
  360. #endif