]>
Commit | Line | Data |
---|---|---|
12c86877 | 1 | /* |
12471842 PL |
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 | */ | |
12c86877 | 22 | // Utility class specification. |
e8c59f2d | 23 | #pragma once |
12c86877 | 24 | |
1258abe0 | 25 | #ifdef NEED_POSIX_TYPEDEF |
092f210a BH |
26 | typedef unsigned char uint8_t; |
27 | typedef unsigned short int uint16_t; | |
28 | typedef unsigned int uint32_t; | |
705f31ae | 29 | typedef unsigned long long uint64_t; |
1258abe0 BH |
30 | #endif |
31 | ||
76473b92 KM |
32 | #include <arpa/inet.h> |
33 | #include <netinet/in.h> | |
34 | #include <sys/socket.h> | |
35 | #include <sys/time.h> | |
36 | #include <sys/uio.h> | |
dc6aa7f5 | 37 | #include <csignal> |
76473b92 KM |
38 | #include <pthread.h> |
39 | #include <semaphore.h> | |
dc6aa7f5 AV |
40 | #include <csignal> |
41 | #include <cerrno> | |
76473b92 | 42 | #include <unistd.h> |
12c86877 BH |
43 | #include <string> |
44 | ||
10f4eea8 | 45 | #include "namespaces.hh" |
12c86877 | 46 | |
bbce5bf0 BH |
47 | //! A semaphore class. |
48 | class Semaphore | |
49 | { | |
50 | private: | |
bbce5bf0 BH |
51 | typedef int sem_value_t; |
52 | ||
82442a51 | 53 | #if defined(_AIX) || defined(__APPLE__) |
cefd8904 PD |
54 | uint32_t m_magic; |
55 | pthread_mutex_t m_lock; | |
56 | pthread_cond_t m_gtzero; | |
57 | sem_value_t m_count; | |
58 | uint32_t m_nwaiters; | |
2ce1b817 | 59 | #else |
c2826d2e | 60 | std::unique_ptr<sem_t> m_pSemaphore; |
2ce1b817 | 61 | #endif |
bbce5bf0 BH |
62 | |
63 | protected: | |
64 | public: | |
f6c19c4f CHB |
65 | Semaphore(const Semaphore&) = delete; |
66 | void operator=(const Semaphore&) = delete; | |
bbce5bf0 BH |
67 | //! Default constructor. |
68 | Semaphore( unsigned int value = 0 ); | |
69 | ||
70 | //! Destructor. | |
df8248ca | 71 | ~Semaphore(); |
bbce5bf0 BH |
72 | |
73 | //! Posts to a semaphore. | |
df8248ca | 74 | int post(); |
bbce5bf0 BH |
75 | |
76 | //! Waits for a semaphore. | |
df8248ca | 77 | int wait(); |
bbce5bf0 BH |
78 | |
79 | //! Tries to wait for a semaphore. | |
df8248ca | 80 | int tryWait(); |
bbce5bf0 BH |
81 | |
82 | //! Retrieves the semaphore value. | |
83 | int getValue( Semaphore::sem_value_t *sval ); | |
bbce5bf0 | 84 | }; |
12c86877 | 85 | |
7c696097 | 86 | //! This is a utility class used for platform independent abstraction. |
12c86877 BH |
87 | class Utility |
88 | { | |
12c86877 | 89 | public: |
12c86877 | 90 | typedef ::iovec iovec; |
76473b92 KM |
91 | typedef ::pid_t pid_t; |
92 | typedef int sock_t; | |
93 | typedef ::socklen_t socklen_t; | |
12c86877 | 94 | |
c7c7edb5 IM |
95 | //! Connect with timeout |
96 | // Returns: | |
97 | // > 0 on success | |
98 | // -1 on error | |
99 | // 0 on timeout | |
100 | static int timed_connect(sock_t sock, | |
101 | const sockaddr *addr, | |
102 | socklen_t sockaddr_size, | |
103 | int timeout_sec, | |
104 | int timeout_usec); | |
105 | ||
12c86877 | 106 | //! Returns the process id of the current process. |
df8248ca | 107 | static pid_t getpid(); |
12c86877 BH |
108 | |
109 | //! Gets the current time. | |
110 | static int gettimeofday( struct timeval *tv, void *tz = NULL ); | |
111 | ||
112 | //! Converts an address from dot and numbers format to binary data. | |
113 | static int inet_aton( const char *cp, struct in_addr *inp ); | |
114 | ||
115 | //! Converts an address from presentation format to network format. | |
116 | static int inet_pton( int af, const char *src, void *dst ); | |
117 | ||
118 | //! 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. | |
119 | static const char *inet_ntop( int af, const char *src, char *dst, size_t size ); | |
120 | ||
12c86877 BH |
121 | //! Writes a vector. |
122 | static int writev( Utility::sock_t socket, const iovec *vector, size_t count ); | |
12c86877 | 123 | |
f1d6a7ce | 124 | //! Drops the program's group privileges. |
76ba685e | 125 | static void dropGroupPrivs( uid_t uid, gid_t gid ); |
f1d6a7ce KM |
126 | |
127 | //! Drops the program's user privileges. | |
76ba685e | 128 | static void dropUserPrivs( uid_t uid ); |
12c86877 | 129 | |
fec7dd5a SS |
130 | //! Sets the socket into Bind-any mode |
131 | static void setBindAny ( int af, Utility::sock_t socket ); | |
42c235e5 | 132 | |
12c86877 BH |
133 | //! Sleeps for a number of seconds. |
134 | static unsigned int sleep( unsigned int seconds ); | |
135 | ||
136 | //! Sleeps for a number of microseconds. | |
137 | static void usleep( unsigned long usec ); | |
49f72da1 BH |
138 | |
139 | static time_t timegm(struct tm *tm); | |
c96765da | 140 | |
12c86877 | 141 | }; |