2
0

e_os.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #ifndef HEADER_E_OS_H
  10. # define HEADER_E_OS_H
  11. # include <limits.h>
  12. # include <openssl/opensslconf.h>
  13. # include <openssl/e_os2.h>
  14. # include <openssl/crypto.h>
  15. # include "internal/nelem.h"
  16. /*
  17. * <openssl/e_os2.h> contains what we can justify to make visible to the
  18. * outside; this file e_os.h is not part of the exported interface.
  19. */
  20. # ifndef DEVRANDOM
  21. /*
  22. * set this to a comma-separated list of 'random' device files to try out. By
  23. * default, we will try to read at least one of these files
  24. */
  25. # define DEVRANDOM "/dev/urandom", "/dev/random", "/dev/hwrng", "/dev/srandom"
  26. # endif
  27. # if !defined(OPENSSL_NO_EGD) && !defined(DEVRANDOM_EGD)
  28. /*
  29. * set this to a comma-separated list of 'egd' sockets to try out. These
  30. * sockets will be tried in the order listed in case accessing the device
  31. * files listed in DEVRANDOM did not return enough randomness.
  32. */
  33. # define DEVRANDOM_EGD "/var/run/egd-pool", "/dev/egd-pool", "/etc/egd-pool", "/etc/entropy"
  34. # endif
  35. # if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
  36. # define NO_CHMOD
  37. # define NO_SYSLOG
  38. # endif
  39. # define get_last_sys_error() errno
  40. # define clear_sys_error() errno=0
  41. # define set_sys_error(e) errno=(e)
  42. /********************************************************************
  43. The Microsoft section
  44. ********************************************************************/
  45. # if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
  46. # define WIN32
  47. # endif
  48. # if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
  49. # define WINDOWS
  50. # endif
  51. # if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
  52. # define MSDOS
  53. # endif
  54. # ifdef WIN32
  55. # undef get_last_sys_error
  56. # undef clear_sys_error
  57. # undef set_sys_error
  58. # define get_last_sys_error() GetLastError()
  59. # define clear_sys_error() SetLastError(0)
  60. # define set_sys_error(e) SetLastError(e)
  61. # if !defined(WINNT)
  62. # define WIN_CONSOLE_BUG
  63. # endif
  64. # else
  65. # endif
  66. # if (defined(WINDOWS) || defined(MSDOS))
  67. # ifdef __DJGPP__
  68. # include <unistd.h>
  69. # include <sys/stat.h>
  70. # define _setmode setmode
  71. # define _O_TEXT O_TEXT
  72. # define _O_BINARY O_BINARY
  73. # define HAS_LFN_SUPPORT(name) (pathconf((name), _PC_NAME_MAX) > 12)
  74. # undef DEVRANDOM_EGD /* Neither MS-DOS nor FreeDOS provide 'egd' sockets. */
  75. # undef DEVRANDOM
  76. # define DEVRANDOM "/dev/urandom\x24"
  77. # endif /* __DJGPP__ */
  78. # ifndef S_IFDIR
  79. # define S_IFDIR _S_IFDIR
  80. # endif
  81. # ifndef S_IFMT
  82. # define S_IFMT _S_IFMT
  83. # endif
  84. # if !defined(WINNT) && !defined(__DJGPP__)
  85. # define NO_SYSLOG
  86. # endif
  87. # ifdef WINDOWS
  88. # if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
  89. /*
  90. * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
  91. * Most notably we ought to check for availability of each specific
  92. * routine that was introduced after denoted _WIN32_WINNT with
  93. * GetProcAddress(). Normally newer functions are masked with higher
  94. * _WIN32_WINNT in SDK headers. So that if you wish to use them in
  95. * some module, you'd need to override _WIN32_WINNT definition in
  96. * the target module in order to "reach for" prototypes, but replace
  97. * calls to new functions with indirect calls. Alternatively it
  98. * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
  99. * and check for current OS version instead.
  100. */
  101. # define _WIN32_WINNT 0x0501
  102. # endif
  103. # if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
  104. /*
  105. * Just like defining _WIN32_WINNT including winsock2.h implies
  106. * certain "discipline" for maintaining [broad] binary compatibility.
  107. * As long as structures are invariant among Winsock versions,
  108. * it's sufficient to check for specific Winsock2 API availability
  109. * at run-time [DSO_global_lookup is recommended]...
  110. */
  111. # include <winsock2.h>
  112. # include <ws2tcpip.h>
  113. /* yes, they have to be #included prior to <windows.h> */
  114. # endif
  115. # include <windows.h>
  116. # include <stdio.h>
  117. # include <stddef.h>
  118. # include <errno.h>
  119. # if defined(_WIN32_WCE) && !defined(EACCES)
  120. # define EACCES 13
  121. # endif
  122. # include <string.h>
  123. # ifdef _WIN64
  124. # define strlen(s) _strlen31(s)
  125. /* cut strings to 2GB */
  126. static __inline unsigned int _strlen31(const char *str)
  127. {
  128. unsigned int len = 0;
  129. while (*str && len < 0x80000000U)
  130. str++, len++;
  131. return len & 0x7FFFFFFF;
  132. }
  133. # endif
  134. # include <malloc.h>
  135. # if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
  136. # if _MSC_VER>=1300 && _MSC_VER<1600
  137. # undef stdin
  138. # undef stdout
  139. # undef stderr
  140. FILE *__iob_func();
  141. # define stdin (&__iob_func()[0])
  142. # define stdout (&__iob_func()[1])
  143. # define stderr (&__iob_func()[2])
  144. # elif _MSC_VER<1300 && defined(I_CAN_LIVE_WITH_LNK4049)
  145. # undef stdin
  146. # undef stdout
  147. # undef stderr
  148. /*
  149. * pre-1300 has __p__iob(), but it's available only in msvcrt.lib,
  150. * or in other words with /MD. Declaring implicit import, i.e. with
  151. * _imp_ prefix, works correctly with all compiler options, but
  152. * without /MD results in LINK warning LNK4049: 'locally defined
  153. * symbol "__iob" imported'.
  154. */
  155. extern FILE *_imp___iob;
  156. # define stdin (&_imp___iob[0])
  157. # define stdout (&_imp___iob[1])
  158. # define stderr (&_imp___iob[2])
  159. # endif
  160. # endif
  161. # endif
  162. # include <io.h>
  163. # include <fcntl.h>
  164. # ifdef OPENSSL_SYS_WINCE
  165. # define OPENSSL_NO_POSIX_IO
  166. # endif
  167. # define EXIT(n) exit(n)
  168. # define LIST_SEPARATOR_CHAR ';'
  169. # ifndef W_OK
  170. # define W_OK 2
  171. # endif
  172. # ifndef R_OK
  173. # define R_OK 4
  174. # endif
  175. # ifdef OPENSSL_SYS_WINCE
  176. # define DEFAULT_HOME ""
  177. # else
  178. # define DEFAULT_HOME "C:"
  179. # endif
  180. /* Avoid Visual Studio 13 GetVersion deprecated problems */
  181. # if defined(_MSC_VER) && _MSC_VER>=1800
  182. # define check_winnt() (1)
  183. # define check_win_minplat(x) (1)
  184. # else
  185. # define check_winnt() (GetVersion() < 0x80000000)
  186. # define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
  187. # endif
  188. # else /* The non-microsoft world */
  189. # if defined(OPENSSL_SYS_VXWORKS)
  190. # include <time.h>
  191. # else
  192. # include <sys/time.h>
  193. # endif
  194. # ifdef OPENSSL_SYS_VMS
  195. # define VMS 1
  196. /*
  197. * some programs don't include stdlib, so exit() and others give implicit
  198. * function warnings
  199. */
  200. # include <stdlib.h>
  201. # if defined(__DECC)
  202. # include <unistd.h>
  203. # else
  204. # include <unixlib.h>
  205. # endif
  206. # define LIST_SEPARATOR_CHAR ','
  207. /* We don't have any well-defined random devices on VMS, yet... */
  208. # undef DEVRANDOM
  209. /*-
  210. We need to do this since VMS has the following coding on status codes:
  211. Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
  212. The important thing to know is that odd numbers are considered
  213. good, while even ones are considered errors.
  214. Bits 3-15: actual status number
  215. Bits 16-27: facility number. 0 is considered "unknown"
  216. Bits 28-31: control bits. If bit 28 is set, the shell won't try to
  217. output the message (which, for random codes, just looks ugly)
  218. So, what we do here is to change 0 to 1 to get the default success status,
  219. and everything else is shifted up to fit into the status number field, and
  220. the status is tagged as an error, which is what is wanted here.
  221. Finally, we add the VMS C facility code 0x35a000, because there are some
  222. programs, such as Perl, that will reinterpret the code back to something
  223. POSIX. 'man perlvms' explains it further.
  224. NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
  225. codes (status type = 1). I couldn't disagree more. Fortunately, the
  226. status type doesn't seem to bother Perl.
  227. -- Richard Levitte
  228. */
  229. # define EXIT(n) exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
  230. # define DEFAULT_HOME "SYS$LOGIN:"
  231. # else
  232. /* !defined VMS */
  233. # ifdef OPENSSL_UNISTD
  234. # include OPENSSL_UNISTD
  235. # else
  236. # include <unistd.h>
  237. # endif
  238. # include <sys/types.h>
  239. # ifdef OPENSSL_SYS_WIN32_CYGWIN
  240. # include <io.h>
  241. # include <fcntl.h>
  242. # endif
  243. # define LIST_SEPARATOR_CHAR ':'
  244. # define EXIT(n) exit(n)
  245. # endif
  246. # endif
  247. /***********************************************/
  248. # if defined(OPENSSL_SYS_WINDOWS)
  249. # define strcasecmp _stricmp
  250. # define strncasecmp _strnicmp
  251. # if (_MSC_VER >= 1310)
  252. # define open _open
  253. # define fdopen _fdopen
  254. # define close _close
  255. # ifndef strdup
  256. # define strdup _strdup
  257. # endif
  258. # define unlink _unlink
  259. # define fileno _fileno
  260. # endif
  261. # else
  262. # include <strings.h>
  263. # endif
  264. /* vxworks */
  265. # if defined(OPENSSL_SYS_VXWORKS)
  266. # include <ioLib.h>
  267. # include <tickLib.h>
  268. # include <sysLib.h>
  269. # include <vxWorks.h>
  270. # include <sockLib.h>
  271. # include <taskLib.h>
  272. # define TTY_STRUCT int
  273. # define sleep(a) taskDelay((a) * sysClkRateGet())
  274. /*
  275. * NOTE: these are implemented by helpers in database app! if the database is
  276. * not linked, we need to implement them elsewhere
  277. */
  278. struct hostent *gethostbyname(const char *name);
  279. struct hostent *gethostbyaddr(const char *addr, int length, int type);
  280. struct servent *getservbyname(const char *name, const char *proto);
  281. # endif
  282. /* end vxworks */
  283. # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  284. # define CRYPTO_memcmp memcmp
  285. # endif
  286. /* unistd.h defines _POSIX_VERSION */
  287. # if !defined(OPENSSL_NO_SECURE_MEMORY) && defined(OPENSSL_SYS_UNIX) \
  288. && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
  289. || defined(__sun) || defined(__hpux) || defined(__sgi) \
  290. || defined(__osf__) )
  291. # define OPENSSL_SECURE_MEMORY /* secure memory is implemented */
  292. # endif
  293. #endif