unistd.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* Checking macros for unistd functions.
  2. Copyright (C) 2005-2016 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. <http://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) __wur;
  20. extern ssize_t __REDIRECT (__read_alias, (int __fd, void *__buf,
  21. size_t __nbytes), read) __wur;
  22. extern ssize_t __REDIRECT (__read_chk_warn,
  23. (int __fd, void *__buf, size_t __nbytes,
  24. size_t __buflen), __read_chk)
  25. __wur __warnattr ("read called with bigger length than size of "
  26. "the destination buffer");
  27. __fortify_function __wur ssize_t
  28. read (int __fd, void *__buf, size_t __nbytes)
  29. {
  30. if (__bos0 (__buf) != (size_t) -1)
  31. {
  32. if (!__builtin_constant_p (__nbytes))
  33. return __read_chk (__fd, __buf, __nbytes, __bos0 (__buf));
  34. if (__nbytes > __bos0 (__buf))
  35. return __read_chk_warn (__fd, __buf, __nbytes, __bos0 (__buf));
  36. }
  37. return __read_alias (__fd, __buf, __nbytes);
  38. }
  39. #ifdef __USE_UNIX98
  40. extern ssize_t __pread_chk (int __fd, void *__buf, size_t __nbytes,
  41. __off_t __offset, size_t __bufsize) __wur;
  42. extern ssize_t __pread64_chk (int __fd, void *__buf, size_t __nbytes,
  43. __off64_t __offset, size_t __bufsize) __wur;
  44. extern ssize_t __REDIRECT (__pread_alias,
  45. (int __fd, void *__buf, size_t __nbytes,
  46. __off_t __offset), pread) __wur;
  47. extern ssize_t __REDIRECT (__pread64_alias,
  48. (int __fd, void *__buf, size_t __nbytes,
  49. __off64_t __offset), pread64) __wur;
  50. extern ssize_t __REDIRECT (__pread_chk_warn,
  51. (int __fd, void *__buf, size_t __nbytes,
  52. __off_t __offset, size_t __bufsize), __pread_chk)
  53. __wur __warnattr ("pread called with bigger length than size of "
  54. "the destination buffer");
  55. extern ssize_t __REDIRECT (__pread64_chk_warn,
  56. (int __fd, void *__buf, size_t __nbytes,
  57. __off64_t __offset, size_t __bufsize),
  58. __pread64_chk)
  59. __wur __warnattr ("pread64 called with bigger length than size of "
  60. "the destination buffer");
  61. # ifndef __USE_FILE_OFFSET64
  62. __fortify_function __wur ssize_t
  63. pread (int __fd, void *__buf, size_t __nbytes, __off_t __offset)
  64. {
  65. if (__bos0 (__buf) != (size_t) -1)
  66. {
  67. if (!__builtin_constant_p (__nbytes))
  68. return __pread_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
  69. if ( __nbytes > __bos0 (__buf))
  70. return __pread_chk_warn (__fd, __buf, __nbytes, __offset,
  71. __bos0 (__buf));
  72. }
  73. return __pread_alias (__fd, __buf, __nbytes, __offset);
  74. }
  75. # else
  76. __fortify_function __wur ssize_t
  77. pread (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
  78. {
  79. if (__bos0 (__buf) != (size_t) -1)
  80. {
  81. if (!__builtin_constant_p (__nbytes))
  82. return __pread64_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
  83. if ( __nbytes > __bos0 (__buf))
  84. return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
  85. __bos0 (__buf));
  86. }
  87. return __pread64_alias (__fd, __buf, __nbytes, __offset);
  88. }
  89. # endif
  90. # ifdef __USE_LARGEFILE64
  91. __fortify_function __wur ssize_t
  92. pread64 (int __fd, void *__buf, size_t __nbytes, __off64_t __offset)
  93. {
  94. if (__bos0 (__buf) != (size_t) -1)
  95. {
  96. if (!__builtin_constant_p (__nbytes))
  97. return __pread64_chk (__fd, __buf, __nbytes, __offset, __bos0 (__buf));
  98. if ( __nbytes > __bos0 (__buf))
  99. return __pread64_chk_warn (__fd, __buf, __nbytes, __offset,
  100. __bos0 (__buf));
  101. }
  102. return __pread64_alias (__fd, __buf, __nbytes, __offset);
  103. }
  104. # endif
  105. #endif
  106. #if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K
  107. extern ssize_t __readlink_chk (const char *__restrict __path,
  108. char *__restrict __buf, size_t __len,
  109. size_t __buflen)
  110. __THROW __nonnull ((1, 2)) __wur;
  111. extern ssize_t __REDIRECT_NTH (__readlink_alias,
  112. (const char *__restrict __path,
  113. char *__restrict __buf, size_t __len), readlink)
  114. __nonnull ((1, 2)) __wur;
  115. extern ssize_t __REDIRECT_NTH (__readlink_chk_warn,
  116. (const char *__restrict __path,
  117. char *__restrict __buf, size_t __len,
  118. size_t __buflen), __readlink_chk)
  119. __nonnull ((1, 2)) __wur __warnattr ("readlink called with bigger length "
  120. "than size of destination buffer");
  121. __fortify_function __nonnull ((1, 2)) __wur ssize_t
  122. __NTH (readlink (const char *__restrict __path, char *__restrict __buf,
  123. size_t __len))
  124. {
  125. if (__bos (__buf) != (size_t) -1)
  126. {
  127. if (!__builtin_constant_p (__len))
  128. return __readlink_chk (__path, __buf, __len, __bos (__buf));
  129. if ( __len > __bos (__buf))
  130. return __readlink_chk_warn (__path, __buf, __len, __bos (__buf));
  131. }
  132. return __readlink_alias (__path, __buf, __len);
  133. }
  134. #endif
  135. #ifdef __USE_ATFILE
  136. extern ssize_t __readlinkat_chk (int __fd, const char *__restrict __path,
  137. char *__restrict __buf, size_t __len,
  138. size_t __buflen)
  139. __THROW __nonnull ((2, 3)) __wur;
  140. extern ssize_t __REDIRECT_NTH (__readlinkat_alias,
  141. (int __fd, const char *__restrict __path,
  142. char *__restrict __buf, size_t __len),
  143. readlinkat)
  144. __nonnull ((2, 3)) __wur;
  145. extern ssize_t __REDIRECT_NTH (__readlinkat_chk_warn,
  146. (int __fd, const char *__restrict __path,
  147. char *__restrict __buf, size_t __len,
  148. size_t __buflen), __readlinkat_chk)
  149. __nonnull ((2, 3)) __wur __warnattr ("readlinkat called with bigger "
  150. "length than size of destination "
  151. "buffer");
  152. __fortify_function __nonnull ((2, 3)) __wur ssize_t
  153. __NTH (readlinkat (int __fd, const char *__restrict __path,
  154. char *__restrict __buf, size_t __len))
  155. {
  156. if (__bos (__buf) != (size_t) -1)
  157. {
  158. if (!__builtin_constant_p (__len))
  159. return __readlinkat_chk (__fd, __path, __buf, __len, __bos (__buf));
  160. if (__len > __bos (__buf))
  161. return __readlinkat_chk_warn (__fd, __path, __buf, __len,
  162. __bos (__buf));
  163. }
  164. return __readlinkat_alias (__fd, __path, __buf, __len);
  165. }
  166. #endif
  167. extern char *__getcwd_chk (char *__buf, size_t __size, size_t __buflen)
  168. __THROW __wur;
  169. extern char *__REDIRECT_NTH (__getcwd_alias,
  170. (char *__buf, size_t __size), getcwd) __wur;
  171. extern char *__REDIRECT_NTH (__getcwd_chk_warn,
  172. (char *__buf, size_t __size, size_t __buflen),
  173. __getcwd_chk)
  174. __wur __warnattr ("getcwd caller with bigger length than size of "
  175. "destination buffer");
  176. __fortify_function __wur char *
  177. __NTH (getcwd (char *__buf, size_t __size))
  178. {
  179. if (__bos (__buf) != (size_t) -1)
  180. {
  181. if (!__builtin_constant_p (__size))
  182. return __getcwd_chk (__buf, __size, __bos (__buf));
  183. if (__size > __bos (__buf))
  184. return __getcwd_chk_warn (__buf, __size, __bos (__buf));
  185. }
  186. return __getcwd_alias (__buf, __size);
  187. }
  188. #if defined __USE_MISC || defined __USE_XOPEN_EXTENDED
  189. extern char *__getwd_chk (char *__buf, size_t buflen)
  190. __THROW __nonnull ((1)) __wur;
  191. extern char *__REDIRECT_NTH (__getwd_warn, (char *__buf), getwd)
  192. __nonnull ((1)) __wur __warnattr ("please use getcwd instead, as getwd "
  193. "doesn't specify buffer size");
  194. __fortify_function __nonnull ((1)) __attribute_deprecated__ __wur char *
  195. __NTH (getwd (char *__buf))
  196. {
  197. if (__bos (__buf) != (size_t) -1)
  198. return __getwd_chk (__buf, __bos (__buf));
  199. return __getwd_warn (__buf);
  200. }
  201. #endif
  202. extern size_t __confstr_chk (int __name, char *__buf, size_t __len,
  203. size_t __buflen) __THROW;
  204. extern size_t __REDIRECT_NTH (__confstr_alias, (int __name, char *__buf,
  205. size_t __len), confstr);
  206. extern size_t __REDIRECT_NTH (__confstr_chk_warn,
  207. (int __name, char *__buf, size_t __len,
  208. size_t __buflen), __confstr_chk)
  209. __warnattr ("confstr called with bigger length than size of destination "
  210. "buffer");
  211. __fortify_function size_t
  212. __NTH (confstr (int __name, char *__buf, size_t __len))
  213. {
  214. if (__bos (__buf) != (size_t) -1)
  215. {
  216. if (!__builtin_constant_p (__len))
  217. return __confstr_chk (__name, __buf, __len, __bos (__buf));
  218. if (__bos (__buf) < __len)
  219. return __confstr_chk_warn (__name, __buf, __len, __bos (__buf));
  220. }
  221. return __confstr_alias (__name, __buf, __len);
  222. }
  223. extern int __getgroups_chk (int __size, __gid_t __list[], size_t __listlen)
  224. __THROW __wur;
  225. extern int __REDIRECT_NTH (__getgroups_alias, (int __size, __gid_t __list[]),
  226. getgroups) __wur;
  227. extern int __REDIRECT_NTH (__getgroups_chk_warn,
  228. (int __size, __gid_t __list[], size_t __listlen),
  229. __getgroups_chk)
  230. __wur __warnattr ("getgroups called with bigger group count than what "
  231. "can fit into destination buffer");
  232. __fortify_function int
  233. __NTH (getgroups (int __size, __gid_t __list[]))
  234. {
  235. if (__bos (__list) != (size_t) -1)
  236. {
  237. if (!__builtin_constant_p (__size) || __size < 0)
  238. return __getgroups_chk (__size, __list, __bos (__list));
  239. if (__size * sizeof (__gid_t) > __bos (__list))
  240. return __getgroups_chk_warn (__size, __list, __bos (__list));
  241. }
  242. return __getgroups_alias (__size, __list);
  243. }
  244. extern int __ttyname_r_chk (int __fd, char *__buf, size_t __buflen,
  245. size_t __nreal) __THROW __nonnull ((2));
  246. extern int __REDIRECT_NTH (__ttyname_r_alias, (int __fd, char *__buf,
  247. size_t __buflen), ttyname_r)
  248. __nonnull ((2));
  249. extern int __REDIRECT_NTH (__ttyname_r_chk_warn,
  250. (int __fd, char *__buf, size_t __buflen,
  251. size_t __nreal), __ttyname_r_chk)
  252. __nonnull ((2)) __warnattr ("ttyname_r called with bigger buflen than "
  253. "size of destination buffer");
  254. __fortify_function int
  255. __NTH (ttyname_r (int __fd, char *__buf, size_t __buflen))
  256. {
  257. if (__bos (__buf) != (size_t) -1)
  258. {
  259. if (!__builtin_constant_p (__buflen))
  260. return __ttyname_r_chk (__fd, __buf, __buflen, __bos (__buf));
  261. if (__buflen > __bos (__buf))
  262. return __ttyname_r_chk_warn (__fd, __buf, __buflen, __bos (__buf));
  263. }
  264. return __ttyname_r_alias (__fd, __buf, __buflen);
  265. }
  266. #if defined __USE_REENTRANT || defined __USE_POSIX199506
  267. extern int __getlogin_r_chk (char *__buf, size_t __buflen, size_t __nreal)
  268. __nonnull ((1));
  269. extern int __REDIRECT (__getlogin_r_alias, (char *__buf, size_t __buflen),
  270. getlogin_r) __nonnull ((1));
  271. extern int __REDIRECT (__getlogin_r_chk_warn,
  272. (char *__buf, size_t __buflen, size_t __nreal),
  273. __getlogin_r_chk)
  274. __nonnull ((1)) __warnattr ("getlogin_r called with bigger buflen than "
  275. "size of destination buffer");
  276. __fortify_function int
  277. getlogin_r (char *__buf, size_t __buflen)
  278. {
  279. if (__bos (__buf) != (size_t) -1)
  280. {
  281. if (!__builtin_constant_p (__buflen))
  282. return __getlogin_r_chk (__buf, __buflen, __bos (__buf));
  283. if (__buflen > __bos (__buf))
  284. return __getlogin_r_chk_warn (__buf, __buflen, __bos (__buf));
  285. }
  286. return __getlogin_r_alias (__buf, __buflen);
  287. }
  288. #endif
  289. #if defined __USE_MISC || defined __USE_UNIX98
  290. extern int __gethostname_chk (char *__buf, size_t __buflen, size_t __nreal)
  291. __THROW __nonnull ((1));
  292. extern int __REDIRECT_NTH (__gethostname_alias, (char *__buf, size_t __buflen),
  293. gethostname) __nonnull ((1));
  294. extern int __REDIRECT_NTH (__gethostname_chk_warn,
  295. (char *__buf, size_t __buflen, size_t __nreal),
  296. __gethostname_chk)
  297. __nonnull ((1)) __warnattr ("gethostname called with bigger buflen than "
  298. "size of destination buffer");
  299. __fortify_function int
  300. __NTH (gethostname (char *__buf, size_t __buflen))
  301. {
  302. if (__bos (__buf) != (size_t) -1)
  303. {
  304. if (!__builtin_constant_p (__buflen))
  305. return __gethostname_chk (__buf, __buflen, __bos (__buf));
  306. if (__buflen > __bos (__buf))
  307. return __gethostname_chk_warn (__buf, __buflen, __bos (__buf));
  308. }
  309. return __gethostname_alias (__buf, __buflen);
  310. }
  311. #endif
  312. #if defined __USE_MISC || (defined __USE_XOPEN && !defined __USE_UNIX98)
  313. extern int __getdomainname_chk (char *__buf, size_t __buflen, size_t __nreal)
  314. __THROW __nonnull ((1)) __wur;
  315. extern int __REDIRECT_NTH (__getdomainname_alias, (char *__buf,
  316. size_t __buflen),
  317. getdomainname) __nonnull ((1)) __wur;
  318. extern int __REDIRECT_NTH (__getdomainname_chk_warn,
  319. (char *__buf, size_t __buflen, size_t __nreal),
  320. __getdomainname_chk)
  321. __nonnull ((1)) __wur __warnattr ("getdomainname called with bigger "
  322. "buflen than size of destination "
  323. "buffer");
  324. __fortify_function int
  325. __NTH (getdomainname (char *__buf, size_t __buflen))
  326. {
  327. if (__bos (__buf) != (size_t) -1)
  328. {
  329. if (!__builtin_constant_p (__buflen))
  330. return __getdomainname_chk (__buf, __buflen, __bos (__buf));
  331. if (__buflen > __bos (__buf))
  332. return __getdomainname_chk_warn (__buf, __buflen, __bos (__buf));
  333. }
  334. return __getdomainname_alias (__buf, __buflen);
  335. }
  336. #endif