e_os.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright 1995-2021 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 OSSL_E_OS_H
  10. # define OSSL_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. # if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_UEFI)
  21. # define NO_CHMOD
  22. # define NO_SYSLOG
  23. # endif
  24. # define get_last_sys_error() errno
  25. # define clear_sys_error() errno=0
  26. # define set_sys_error(e) errno=(e)
  27. /********************************************************************
  28. The Microsoft section
  29. ********************************************************************/
  30. # if defined(OPENSSL_SYS_WIN32) && !defined(WIN32)
  31. # define WIN32
  32. # endif
  33. # if defined(OPENSSL_SYS_WINDOWS) && !defined(WINDOWS)
  34. # define WINDOWS
  35. # endif
  36. # if defined(OPENSSL_SYS_MSDOS) && !defined(MSDOS)
  37. # define MSDOS
  38. # endif
  39. # ifdef WIN32
  40. # undef get_last_sys_error
  41. # undef clear_sys_error
  42. # undef set_sys_error
  43. # define get_last_sys_error() GetLastError()
  44. # define clear_sys_error() SetLastError(0)
  45. # define set_sys_error(e) SetLastError(e)
  46. # if !defined(WINNT)
  47. # define WIN_CONSOLE_BUG
  48. # endif
  49. # else
  50. # endif
  51. # if (defined(WINDOWS) || defined(MSDOS))
  52. # ifdef __DJGPP__
  53. # include <unistd.h>
  54. # include <sys/stat.h>
  55. # define _setmode setmode
  56. # define _O_TEXT O_TEXT
  57. # define _O_BINARY O_BINARY
  58. # undef DEVRANDOM_EGD /* Neither MS-DOS nor FreeDOS provide 'egd' sockets. */
  59. # undef DEVRANDOM
  60. # define DEVRANDOM "/dev/urandom\x24"
  61. # endif /* __DJGPP__ */
  62. # ifndef S_IFDIR
  63. # define S_IFDIR _S_IFDIR
  64. # endif
  65. # ifndef S_IFMT
  66. # define S_IFMT _S_IFMT
  67. # endif
  68. # if !defined(WINNT) && !defined(__DJGPP__)
  69. # define NO_SYSLOG
  70. # endif
  71. # ifdef WINDOWS
  72. # if !defined(_WIN32_WCE) && !defined(_WIN32_WINNT)
  73. /*
  74. * Defining _WIN32_WINNT here in e_os.h implies certain "discipline."
  75. * Most notably we ought to check for availability of each specific
  76. * routine that was introduced after denoted _WIN32_WINNT with
  77. * GetProcAddress(). Normally newer functions are masked with higher
  78. * _WIN32_WINNT in SDK headers. So that if you wish to use them in
  79. * some module, you'd need to override _WIN32_WINNT definition in
  80. * the target module in order to "reach for" prototypes, but replace
  81. * calls to new functions with indirect calls. Alternatively it
  82. * might be possible to achieve the goal by /DELAYLOAD-ing .DLLs
  83. * and check for current OS version instead.
  84. */
  85. # define _WIN32_WINNT 0x0501
  86. # endif
  87. # if defined(_WIN32_WINNT) || defined(_WIN32_WCE)
  88. /*
  89. * Just like defining _WIN32_WINNT including winsock2.h implies
  90. * certain "discipline" for maintaining [broad] binary compatibility.
  91. * As long as structures are invariant among Winsock versions,
  92. * it's sufficient to check for specific Winsock2 API availability
  93. * at run-time [DSO_global_lookup is recommended]...
  94. */
  95. # include <winsock2.h>
  96. # include <ws2tcpip.h>
  97. /*
  98. * Clang-based C++Builder 10.3.3 toolchains cannot find C inline
  99. * definitions at link-time. This header defines WspiapiLoad() as an
  100. * __inline function. https://quality.embarcadero.com/browse/RSP-33806
  101. */
  102. # if !defined(__BORLANDC__) || !defined(__clang__)
  103. # include <wspiapi.h>
  104. # endif
  105. /* yes, they have to be #included prior to <windows.h> */
  106. # endif
  107. # include <windows.h>
  108. # include <stdio.h>
  109. # include <stddef.h>
  110. # include <errno.h>
  111. # if defined(_WIN32_WCE) && !defined(EACCES)
  112. # define EACCES 13
  113. # endif
  114. # include <string.h>
  115. # ifdef _WIN64
  116. # define strlen(s) _strlen31(s)
  117. /* cut strings to 2GB */
  118. static __inline unsigned int _strlen31(const char *str)
  119. {
  120. unsigned int len = 0;
  121. while (*str && len < 0x80000000U)
  122. str++, len++;
  123. return len & 0x7FFFFFFF;
  124. }
  125. # endif
  126. # include <malloc.h>
  127. # if defined(_MSC_VER) && !defined(_WIN32_WCE) && !defined(_DLL) && defined(stdin)
  128. # if _MSC_VER>=1300 && _MSC_VER<1600
  129. # undef stdin
  130. # undef stdout
  131. # undef stderr
  132. FILE *__iob_func();
  133. # define stdin (&__iob_func()[0])
  134. # define stdout (&__iob_func()[1])
  135. # define stderr (&__iob_func()[2])
  136. # endif
  137. # endif
  138. # endif
  139. # include <io.h>
  140. # include <fcntl.h>
  141. # ifdef OPENSSL_SYS_WINCE
  142. # define OPENSSL_NO_POSIX_IO
  143. # endif
  144. # define EXIT(n) exit(n)
  145. # define LIST_SEPARATOR_CHAR ';'
  146. # ifndef W_OK
  147. # define W_OK 2
  148. # endif
  149. # ifndef R_OK
  150. # define R_OK 4
  151. # endif
  152. # ifdef OPENSSL_SYS_WINCE
  153. # define DEFAULT_HOME ""
  154. # else
  155. # define DEFAULT_HOME "C:"
  156. # endif
  157. /* Avoid Visual Studio 13 GetVersion deprecated problems */
  158. # if defined(_MSC_VER) && _MSC_VER>=1800
  159. # define check_winnt() (1)
  160. # define check_win_minplat(x) (1)
  161. # else
  162. # define check_winnt() (GetVersion() < 0x80000000)
  163. # define check_win_minplat(x) (LOBYTE(LOWORD(GetVersion())) >= (x))
  164. # endif
  165. # else /* The non-microsoft world */
  166. # if defined(OPENSSL_SYS_VXWORKS)
  167. # include <time.h>
  168. # else
  169. # include <sys/time.h>
  170. # endif
  171. # ifdef OPENSSL_SYS_VMS
  172. # define VMS 1
  173. /*
  174. * some programs don't include stdlib, so exit() and others give implicit
  175. * function warnings
  176. */
  177. # include <stdlib.h>
  178. # if defined(__DECC)
  179. # include <unistd.h>
  180. # else
  181. # include <unixlib.h>
  182. # endif
  183. # define LIST_SEPARATOR_CHAR ','
  184. /* We don't have any well-defined random devices on VMS, yet... */
  185. # undef DEVRANDOM
  186. /*-
  187. We need to do this since VMS has the following coding on status codes:
  188. Bits 0-2: status type: 0 = warning, 1 = success, 2 = error, 3 = info ...
  189. The important thing to know is that odd numbers are considered
  190. good, while even ones are considered errors.
  191. Bits 3-15: actual status number
  192. Bits 16-27: facility number. 0 is considered "unknown"
  193. Bits 28-31: control bits. If bit 28 is set, the shell won't try to
  194. output the message (which, for random codes, just looks ugly)
  195. So, what we do here is to change 0 to 1 to get the default success status,
  196. and everything else is shifted up to fit into the status number field, and
  197. the status is tagged as an error, which is what is wanted here.
  198. Finally, we add the VMS C facility code 0x35a000, because there are some
  199. programs, such as Perl, that will reinterpret the code back to something
  200. POSIX. 'man perlvms' explains it further.
  201. NOTE: the perlvms manual wants to turn all codes 2 to 255 into success
  202. codes (status type = 1). I couldn't disagree more. Fortunately, the
  203. status type doesn't seem to bother Perl.
  204. -- Richard Levitte
  205. */
  206. # define EXIT(n) exit((n) ? (((n) << 3) | 2 | 0x10000000 | 0x35a000) : 1)
  207. # define DEFAULT_HOME "SYS$LOGIN:"
  208. # else
  209. /* !defined VMS */
  210. # include <unistd.h>
  211. # include <sys/types.h>
  212. # ifdef OPENSSL_SYS_WIN32_CYGWIN
  213. # include <io.h>
  214. # include <fcntl.h>
  215. # endif
  216. # define LIST_SEPARATOR_CHAR ':'
  217. # define EXIT(n) exit(n)
  218. # endif
  219. # endif
  220. /***********************************************/
  221. # if defined(OPENSSL_SYS_WINDOWS)
  222. # define strcasecmp _stricmp
  223. # define strncasecmp _strnicmp
  224. # if (_MSC_VER >= 1310) && !defined(_WIN32_WCE)
  225. # define open _open
  226. # define fdopen _fdopen
  227. # define close _close
  228. # ifndef strdup
  229. # define strdup _strdup
  230. # endif
  231. # define unlink _unlink
  232. # define fileno _fileno
  233. # endif
  234. # else
  235. # include <strings.h>
  236. # endif
  237. /* vxworks */
  238. # if defined(OPENSSL_SYS_VXWORKS)
  239. # include <ioLib.h>
  240. # include <tickLib.h>
  241. # include <sysLib.h>
  242. # include <vxWorks.h>
  243. # include <sockLib.h>
  244. # include <taskLib.h>
  245. # define TTY_STRUCT int
  246. # define sleep(a) taskDelay((a) * sysClkRateGet())
  247. /*
  248. * NOTE: these are implemented by helpers in database app! if the database is
  249. * not linked, we need to implement them elsewhere
  250. */
  251. struct hostent *gethostbyname(const char *name);
  252. struct hostent *gethostbyaddr(const char *addr, int length, int type);
  253. struct servent *getservbyname(const char *name, const char *proto);
  254. # endif
  255. /* end vxworks */
  256. /* system-specific variants defining ossl_sleep() */
  257. #ifdef OPENSSL_SYS_UNIX
  258. # include <unistd.h>
  259. static ossl_inline void ossl_sleep(unsigned long millis)
  260. {
  261. # ifdef OPENSSL_SYS_VXWORKS
  262. struct timespec ts;
  263. ts.tv_sec = (long int) (millis / 1000);
  264. ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
  265. nanosleep(&ts, NULL);
  266. # elif defined(__TANDEM)
  267. # if !defined(_REENTRANT)
  268. # include <cextdecs.h(PROCESS_DELAY_)>
  269. /* HPNS does not support usleep for non threaded apps */
  270. PROCESS_DELAY_(millis * 1000);
  271. # elif defined(_SPT_MODEL_)
  272. # include <spthread.h>
  273. # include <spt_extensions.h>
  274. usleep(millis * 1000);
  275. # else
  276. usleep(millis * 1000);
  277. # endif
  278. # else
  279. usleep(millis * 1000);
  280. # endif
  281. }
  282. #elif defined(_WIN32)
  283. # include <windows.h>
  284. static ossl_inline void ossl_sleep(unsigned long millis)
  285. {
  286. Sleep(millis);
  287. }
  288. #else
  289. /* Fallback to a busy wait */
  290. static ossl_inline void ossl_sleep(unsigned long millis)
  291. {
  292. struct timeval start, now;
  293. unsigned long elapsedms;
  294. gettimeofday(&start, NULL);
  295. do {
  296. gettimeofday(&now, NULL);
  297. elapsedms = (((now.tv_sec - start.tv_sec) * 1000000)
  298. + now.tv_usec - start.tv_usec) / 1000;
  299. } while (elapsedms < millis);
  300. }
  301. #endif /* defined OPENSSL_SYS_UNIX */
  302. /* ----------------------------- HP NonStop -------------------------------- */
  303. /* Required to support platform variant without getpid() and pid_t. */
  304. # if defined(__TANDEM) && defined(_GUARDIAN_TARGET)
  305. # include <strings.h>
  306. # include <netdb.h>
  307. # define getservbyname(name,proto) getservbyname((char*)name,proto)
  308. # define gethostbyname(name) gethostbyname((char*)name)
  309. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  310. # ifdef NO_GETPID
  311. inline int nssgetpid();
  312. # ifndef NSSGETPID_MACRO
  313. # define NSSGETPID_MACRO
  314. # include <cextdecs.h(PROCESSHANDLE_GETMINE_)>
  315. # include <cextdecs.h(PROCESSHANDLE_DECOMPOSE_)>
  316. inline int nssgetpid()
  317. {
  318. short phandle[10]={0};
  319. union pseudo_pid {
  320. struct {
  321. short cpu;
  322. short pin;
  323. } cpu_pin ;
  324. int ppid;
  325. } ppid = { 0 };
  326. PROCESSHANDLE_GETMINE_(phandle);
  327. PROCESSHANDLE_DECOMPOSE_(phandle, &ppid.cpu_pin.cpu, &ppid.cpu_pin.pin);
  328. return ppid.ppid;
  329. }
  330. # define getpid(a) nssgetpid(a)
  331. # endif /* NSSGETPID_MACRO */
  332. # endif /* NO_GETPID */
  333. /*# define setsockopt(a,b,c,d,f) setsockopt(a,b,c,(char*)d,f)*/
  334. /*# define getsockopt(a,b,c,d,f) getsockopt(a,b,c,(char*)d,f)*/
  335. /*# define connect(a,b,c) connect(a,(struct sockaddr *)b,c)*/
  336. /*# define bind(a,b,c) bind(a,(struct sockaddr *)b,c)*/
  337. /*# define sendto(a,b,c,d,e,f) sendto(a,(char*)b,c,d,(struct sockaddr *)e,f)*/
  338. # if defined(OPENSSL_THREADS) && !defined(_PUT_MODEL_)
  339. /*
  340. * HPNS SPT threads
  341. */
  342. # define SPT_THREAD_SIGNAL 1
  343. # define SPT_THREAD_AWARE 1
  344. # include <spthread.h>
  345. # undef close
  346. # define close spt_close
  347. /*
  348. # define get_last_socket_error() errno
  349. # define clear_socket_error() errno=0
  350. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  351. # define closesocket(s) close(s)
  352. # define readsocket(s,b,n) read((s),(char*)(b),(n))
  353. # define writesocket(s,b,n) write((s),(char*)(b),(n)
  354. */
  355. # define accept(a,b,c) accept(a,(struct sockaddr *)b,c)
  356. # define recvfrom(a,b,c,d,e,f) recvfrom(a,b,(socklen_t)c,d,e,f)
  357. # endif
  358. # endif
  359. # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  360. # define CRYPTO_memcmp memcmp
  361. # endif
  362. # ifndef OPENSSL_NO_SECURE_MEMORY
  363. /* unistd.h defines _POSIX_VERSION */
  364. # if (defined(OPENSSL_SYS_UNIX) \
  365. && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
  366. || defined(__sun) || defined(__hpux) || defined(__sgi) \
  367. || defined(__osf__) )) \
  368. || defined(_WIN32)
  369. /* secure memory is implemented */
  370. # else
  371. # define OPENSSL_NO_SECURE_MEMORY
  372. # endif
  373. # endif
  374. #endif