You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

145 lines
4.0 KiB

  1. /*
  2. * This file is part of PowerDNS or dnsdist.
  3. * Copyright -- PowerDNS.COM B.V. and its contributors
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * In addition, for the avoidance of any doubt, permission is granted to
  10. * link this program with OpenSSL and to (re)distribute the binaries
  11. * produced as the result of such linking.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. // Utility class specification.
  23. #pragma once
  24. #ifdef NEED_POSIX_TYPEDEF
  25. typedef unsigned char uint8_t;
  26. typedef unsigned short int uint16_t;
  27. typedef unsigned int uint32_t;
  28. typedef unsigned long long uint64_t;
  29. #endif
  30. #include <arpa/inet.h>
  31. #include <netinet/in.h>
  32. #include <sys/socket.h>
  33. #include <sys/time.h>
  34. #include <sys/uio.h>
  35. #include <signal.h>
  36. #include <pthread.h>
  37. #include <semaphore.h>
  38. #include <signal.h>
  39. #include <errno.h>
  40. #include <unistd.h>
  41. #include <string>
  42. #include "namespaces.hh"
  43. //! A semaphore class.
  44. class Semaphore
  45. {
  46. private:
  47. typedef int sem_value_t;
  48. #if defined(_AIX) || defined(__APPLE__)
  49. uint32_t m_magic;
  50. pthread_mutex_t m_lock;
  51. pthread_cond_t m_gtzero;
  52. sem_value_t m_count;
  53. uint32_t m_nwaiters;
  54. #else
  55. std::unique_ptr<sem_t> m_pSemaphore;
  56. #endif
  57. protected:
  58. public:
  59. Semaphore(const Semaphore&) = delete;
  60. void operator=(const Semaphore&) = delete;
  61. //! Default constructor.
  62. Semaphore( unsigned int value = 0 );
  63. //! Destructor.
  64. ~Semaphore( void );
  65. //! Posts to a semaphore.
  66. int post( void );
  67. //! Waits for a semaphore.
  68. int wait( void );
  69. //! Tries to wait for a semaphore.
  70. int tryWait( void );
  71. //! Retrieves the semaphore value.
  72. int getValue( Semaphore::sem_value_t *sval );
  73. };
  74. //! This is a utility class used for platform independent abstraction.
  75. class Utility
  76. {
  77. public:
  78. typedef ::iovec iovec;
  79. typedef ::pid_t pid_t;
  80. typedef int sock_t;
  81. typedef ::socklen_t socklen_t;
  82. //! Connect with timeout
  83. // Returns:
  84. // > 0 on success
  85. // -1 on error
  86. // 0 on timeout
  87. static int timed_connect(sock_t sock,
  88. const sockaddr *addr,
  89. socklen_t sockaddr_size,
  90. int timeout_sec,
  91. int timeout_usec);
  92. //! Returns the process id of the current process.
  93. static pid_t getpid( void );
  94. //! Gets the current time.
  95. static int gettimeofday( struct timeval *tv, void *tz = NULL );
  96. //! Converts an address from dot and numbers format to binary data.
  97. static int inet_aton( const char *cp, struct in_addr *inp );
  98. //! Converts an address from presentation format to network format.
  99. static int inet_pton( int af, const char *src, void *dst );
  100. //! The inet_ntop() function converts an address from network format (usually a struct in_addr or some other binary form, in network byte order) to presentation format.
  101. static const char *inet_ntop( int af, const char *src, char *dst, size_t size );
  102. //! Writes a vector.
  103. static int writev( Utility::sock_t socket, const iovec *vector, size_t count );
  104. //! Sets the random seed.
  105. static void srandom(void);
  106. //! Drops the program's group privileges.
  107. static void dropGroupPrivs( uid_t uid, gid_t gid );
  108. //! Drops the program's user privileges.
  109. static void dropUserPrivs( uid_t uid );
  110. //! Sets the socket into Bind-any mode
  111. static void setBindAny ( int af, Utility::sock_t socket );
  112. //! Sleeps for a number of seconds.
  113. static unsigned int sleep( unsigned int seconds );
  114. //! Sleeps for a number of microseconds.
  115. static void usleep( unsigned long usec );
  116. static time_t timegm(struct tm *tm);
  117. };