e_os.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. * Copyright 1995-2022 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. # if (_MSC_VER >= 1310) && !defined(_WIN32_WCE)
  223. # define open _open
  224. # define fdopen _fdopen
  225. # define close _close
  226. # ifndef strdup
  227. # define strdup _strdup
  228. # endif
  229. # define unlink _unlink
  230. # define fileno _fileno
  231. # endif
  232. # else
  233. # include <strings.h>
  234. # endif
  235. /* vxworks */
  236. # if defined(OPENSSL_SYS_VXWORKS)
  237. # include <ioLib.h>
  238. # include <tickLib.h>
  239. # include <sysLib.h>
  240. # include <vxWorks.h>
  241. # include <sockLib.h>
  242. # include <taskLib.h>
  243. # define TTY_STRUCT int
  244. # define sleep(a) taskDelay((a) * sysClkRateGet())
  245. /*
  246. * NOTE: these are implemented by helpers in database app! if the database is
  247. * not linked, we need to implement them elsewhere
  248. */
  249. struct hostent *gethostbyname(const char *name);
  250. struct hostent *gethostbyaddr(const char *addr, int length, int type);
  251. struct servent *getservbyname(const char *name, const char *proto);
  252. # endif
  253. /* end vxworks */
  254. /* system-specific variants defining ossl_sleep() */
  255. #if defined(OPENSSL_SYS_UNIX) || defined(__DJGPP__)
  256. # include <unistd.h>
  257. static ossl_inline void ossl_sleep(unsigned long millis)
  258. {
  259. # ifdef OPENSSL_SYS_VXWORKS
  260. struct timespec ts;
  261. ts.tv_sec = (long int) (millis / 1000);
  262. ts.tv_nsec = (long int) (millis % 1000) * 1000000ul;
  263. nanosleep(&ts, NULL);
  264. # elif defined(__TANDEM) && !defined(_REENTRANT)
  265. # include <cextdecs.h(PROCESS_DELAY_)>
  266. /* HPNS does not support usleep for non threaded apps */
  267. PROCESS_DELAY_(millis * 1000);
  268. # else
  269. unsigned int s = (unsigned int)(millis / 1000);
  270. unsigned int us = (unsigned int)((millis % 1000) * 1000);
  271. if (s > 0)
  272. sleep(s);
  273. usleep(us);
  274. # endif
  275. }
  276. #elif defined(_WIN32)
  277. # include <windows.h>
  278. static ossl_inline void ossl_sleep(unsigned long millis)
  279. {
  280. Sleep(millis);
  281. }
  282. #else
  283. /* Fallback to a busy wait */
  284. static ossl_inline void ossl_sleep(unsigned long millis)
  285. {
  286. struct timeval start, now;
  287. unsigned long elapsedms;
  288. gettimeofday(&start, NULL);
  289. do {
  290. gettimeofday(&now, NULL);
  291. elapsedms = (((now.tv_sec - start.tv_sec) * 1000000)
  292. + now.tv_usec - start.tv_usec) / 1000;
  293. } while (elapsedms < millis);
  294. }
  295. #endif /* defined OPENSSL_SYS_UNIX */
  296. /* ----------------------------- HP NonStop -------------------------------- */
  297. /* Required to support platform variant without getpid() and pid_t. */
  298. # if defined(__TANDEM) && defined(_GUARDIAN_TARGET)
  299. # include <strings.h>
  300. # include <netdb.h>
  301. # define getservbyname(name,proto) getservbyname((char*)name,proto)
  302. # define gethostbyname(name) gethostbyname((char*)name)
  303. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  304. # ifdef NO_GETPID
  305. inline int nssgetpid();
  306. # ifndef NSSGETPID_MACRO
  307. # define NSSGETPID_MACRO
  308. # include <cextdecs.h(PROCESSHANDLE_GETMINE_)>
  309. # include <cextdecs.h(PROCESSHANDLE_DECOMPOSE_)>
  310. inline int nssgetpid()
  311. {
  312. short phandle[10]={0};
  313. union pseudo_pid {
  314. struct {
  315. short cpu;
  316. short pin;
  317. } cpu_pin ;
  318. int ppid;
  319. } ppid = { 0 };
  320. PROCESSHANDLE_GETMINE_(phandle);
  321. PROCESSHANDLE_DECOMPOSE_(phandle, &ppid.cpu_pin.cpu, &ppid.cpu_pin.pin);
  322. return ppid.ppid;
  323. }
  324. # define getpid(a) nssgetpid(a)
  325. # endif /* NSSGETPID_MACRO */
  326. # endif /* NO_GETPID */
  327. /*# define setsockopt(a,b,c,d,f) setsockopt(a,b,c,(char*)d,f)*/
  328. /*# define getsockopt(a,b,c,d,f) getsockopt(a,b,c,(char*)d,f)*/
  329. /*# define connect(a,b,c) connect(a,(struct sockaddr *)b,c)*/
  330. /*# define bind(a,b,c) bind(a,(struct sockaddr *)b,c)*/
  331. /*# define sendto(a,b,c,d,e,f) sendto(a,(char*)b,c,d,(struct sockaddr *)e,f)*/
  332. # if defined(OPENSSL_THREADS) && !defined(_PUT_MODEL_)
  333. /*
  334. * HPNS SPT threads
  335. */
  336. # define SPT_THREAD_SIGNAL 1
  337. # define SPT_THREAD_AWARE 1
  338. # include <spthread.h>
  339. # undef close
  340. # define close spt_close
  341. /*
  342. # define get_last_socket_error() errno
  343. # define clear_socket_error() errno=0
  344. # define ioctlsocket(a,b,c) ioctl(a,b,c)
  345. # define closesocket(s) close(s)
  346. # define readsocket(s,b,n) read((s),(char*)(b),(n))
  347. # define writesocket(s,b,n) write((s),(char*)(b),(n)
  348. */
  349. # define accept(a,b,c) accept(a,(struct sockaddr *)b,c)
  350. # define recvfrom(a,b,c,d,e,f) recvfrom(a,b,(socklen_t)c,d,e,f)
  351. # endif
  352. # endif
  353. # ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
  354. # define CRYPTO_memcmp memcmp
  355. # endif
  356. # ifndef OPENSSL_NO_SECURE_MEMORY
  357. /* unistd.h defines _POSIX_VERSION */
  358. # if (defined(OPENSSL_SYS_UNIX) \
  359. && ( (defined(_POSIX_VERSION) && _POSIX_VERSION >= 200112L) \
  360. || defined(__sun) || defined(__hpux) || defined(__sgi) \
  361. || defined(__osf__) )) \
  362. || defined(_WIN32)
  363. /* secure memory is implemented */
  364. # else
  365. # define OPENSSL_NO_SECURE_MEMORY
  366. # endif
  367. # endif
  368. /*
  369. * str[n]casecmp_l is defined in POSIX 2008-01. Value is taken accordingly
  370. * https://www.gnu.org/software/libc/manual/html_node/Feature-Test-Macros.html
  371. * There are also equivalent functions on Windows.
  372. * There is no locale_t on NONSTOP.
  373. */
  374. # if defined(OPENSSL_SYS_WINDOWS)
  375. # define locale_t _locale_t
  376. # define freelocale _free_locale
  377. # define strcasecmp_l _stricmp_l
  378. # define strncasecmp_l _strnicmp_l
  379. # define strcasecmp _stricmp
  380. # define strncasecmp _strnicmp
  381. # elif !defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L \
  382. || defined(OPENSSL_SYS_TANDEM)
  383. # ifndef OPENSSL_NO_LOCALE
  384. # define OPENSSL_NO_LOCALE
  385. # endif
  386. # endif
  387. #endif