2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
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.
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.
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.
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.
22 // Utility class specification.
27 #ifdef NEED_POSIX_TYPEDEF
28 typedef unsigned char uint8_t;
29 typedef unsigned short int uint16_t;
30 typedef unsigned int uint32_t;
31 typedef unsigned long long uint64_t;
34 #include <arpa/inet.h>
35 #include <netinet/in.h>
36 #include <sys/socket.h>
41 #include <semaphore.h>
47 #include "namespaces.hh"
49 //! A semaphore class.
53 typedef int sem_value_t;
55 #if defined(_AIX) || defined(__APPLE__)
57 pthread_mutex_t m_lock;
58 pthread_cond_t m_gtzero;
67 Semaphore(const Semaphore&) = delete;
68 void operator=(const Semaphore&) = delete;
69 //! Default constructor.
70 Semaphore( unsigned int value = 0 );
75 //! Posts to a semaphore.
78 //! Waits for a semaphore.
81 //! Tries to wait for a semaphore.
84 //! Retrieves the semaphore value.
85 int getValue( Semaphore::sem_value_t *sval );
88 //! This is a utility class used for platform independent abstraction.
92 typedef ::iovec iovec;
93 typedef ::pid_t pid_t;
95 typedef ::socklen_t socklen_t;
97 //! Connect with timeout
102 static int timed_connect(sock_t sock,
103 const sockaddr *addr,
104 socklen_t sockaddr_size,
108 //! Returns the process id of the current process.
109 static pid_t getpid( void );
111 //! Gets the current time.
112 static int gettimeofday( struct timeval *tv, void *tz = NULL );
114 //! Converts an address from dot and numbers format to binary data.
115 static int inet_aton( const char *cp, struct in_addr *inp );
117 //! Converts an address from presentation format to network format.
118 static int inet_pton( int af, const char *src, void *dst );
120 //! 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.
121 static const char *inet_ntop( int af, const char *src, char *dst, size_t size );
123 //! Retrieves a gid using a groupname.
124 static gid_t makeGidNumeric( const string & group );
126 //! Retrieves an uid using an username.
127 static uid_t makeUidNumeric( const string & username );
130 static int writev( Utility::sock_t socket, const iovec *vector, size_t count );
132 //! Sets the random seed.
133 static void srandom(void);
135 //! Drops the program's group privileges.
136 static void dropGroupPrivs( uid_t uid, gid_t gid );
138 //! Drops the program's user privileges.
139 static void dropUserPrivs( uid_t uid );
141 //! Sets the socket into Bind-any mode
142 static void setBindAny ( int af, Utility::sock_t socket );
144 //! Sleeps for a number of seconds.
145 static unsigned int sleep( unsigned int seconds );
147 //! Sleeps for a number of microseconds.
148 static void usleep( unsigned long usec );
150 static time_t timegm(struct tm *tm);