unix-misc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010 - 2019 Andy Green <[email protected]>
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to
  8. * deal in the Software without restriction, including without limitation the
  9. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  10. * sell copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  21. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  22. * IN THE SOFTWARE.
  23. */
  24. #if !defined(_GNU_SOURCE)
  25. #define _GNU_SOURCE
  26. #endif
  27. #include "private-lib-core.h"
  28. /*
  29. * Normally you don't want this, use lws_sul instead inside the event loop.
  30. * But sometimes for drivers it makes sense, so there's an internal-only
  31. * crossplatform api for it.
  32. */
  33. void
  34. lws_msleep(unsigned int ms)
  35. {
  36. usleep(ms * LWS_US_PER_MS);
  37. }
  38. lws_usec_t
  39. lws_now_usecs(void)
  40. {
  41. #if defined(LWS_HAVE_CLOCK_GETTIME)
  42. struct timespec ts;
  43. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  44. return 0;
  45. return (((lws_usec_t)ts.tv_sec) * LWS_US_PER_SEC) +
  46. ((lws_usec_t)ts.tv_nsec / LWS_NS_PER_US);
  47. #else
  48. struct timeval now;
  49. gettimeofday(&now, NULL);
  50. return (((lws_usec_t)now.tv_sec) * LWS_US_PER_SEC) +
  51. (lws_usec_t)now.tv_usec;
  52. #endif
  53. }
  54. size_t
  55. lws_get_random(struct lws_context *context, void *buf, size_t len)
  56. {
  57. #if defined(__COVERITY__)
  58. memset(buf, 0, len);
  59. return len;
  60. #else
  61. /* coverity[tainted_scalar] */
  62. return (size_t)read(context->fd_random, (char *)buf, len);
  63. #endif
  64. }
  65. void lwsl_emit_syslog(int level, const char *line)
  66. {
  67. int syslog_level = LOG_DEBUG;
  68. switch (level) {
  69. case LLL_ERR:
  70. syslog_level = LOG_ERR;
  71. break;
  72. case LLL_WARN:
  73. syslog_level = LOG_WARNING;
  74. break;
  75. case LLL_NOTICE:
  76. syslog_level = LOG_NOTICE;
  77. break;
  78. case LLL_INFO:
  79. syslog_level = LOG_INFO;
  80. break;
  81. }
  82. syslog(syslog_level, "%s", line);
  83. }
  84. int
  85. lws_plat_write_cert(struct lws_vhost *vhost, int is_key, int fd, void *buf,
  86. int len)
  87. {
  88. int n;
  89. n = write(fd, buf, len);
  90. if (fsync(fd))
  91. return 1;
  92. if (lseek(fd, 0, SEEK_SET) < 0)
  93. return 1;
  94. return n != len;
  95. }
  96. int
  97. lws_plat_recommended_rsa_bits(void)
  98. {
  99. return 4096;
  100. }