]>
git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/unix_utility.cc
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.
30 #include "pdnsexception.hh"
35 #include <sys/types.h>
36 #include <sys/select.h>
38 #ifdef NEED_INET_NTOP_PROTO
40 const char *inet_ntop(int af
, const void *src
, char *dst
, size_t cnt
);
45 #include "namespaces.hh"
48 // Connects to socket with timeout
49 int Utility::timed_connect( Utility::sock_t sock
,
51 Utility::socklen_t sockaddr_size
,
56 struct timeval timeout
;
59 timeout
.tv_sec
= timeout_sec
;
60 timeout
.tv_usec
= timeout_usec
;
67 if ((ret
= connect (sock
, addr
, sockaddr_size
)) < 0) {
68 if (errno
!= EINPROGRESS
)
72 ret
= select(sock
+ 1, NULL
, &set
, NULL
, &timeout
);
80 void Utility::setBindAny(int af
, sock_t sock
)
84 (void) one
; // avoids 'unused var' warning on systems that have none of the defines checked below
86 if (setsockopt(sock
, IPPROTO_IP
, IP_FREEBIND
, &one
, sizeof(one
)) < 0)
87 g_log
<<Logger::Warning
<<"Warning: IP_FREEBIND setsockopt failed: "<<strerror(errno
)<<endl
;
92 if (setsockopt(sock
, IPPROTO_IP
, IP_BINDANY
, &one
, sizeof(one
)) < 0)
93 g_log
<<Logger::Warning
<<"Warning: IP_BINDANY setsockopt failed: "<<strerror(errno
)<<endl
;
97 if (setsockopt(sock
, IPPROTO_IPV6
, IPV6_BINDANY
, &one
, sizeof(one
)) < 0)
98 g_log
<<Logger::Warning
<<"Warning: IPV6_BINDANY setsockopt failed: "<<strerror(errno
)<<endl
;
101 if (setsockopt(sock
, SOL_SOCKET
, SO_BINDANY
, &one
, sizeof(one
)) < 0)
102 g_log
<<Logger::Warning
<<"Warning: SO_BINDANY setsockopt failed: "<<strerror(errno
)<<endl
;
106 const char *Utility::inet_ntop(int af
, const char *src
, char *dst
, size_t size
)
108 return ::inet_ntop(af
,src
,dst
,size
);
111 unsigned int Utility::sleep(unsigned int sec
)
116 void Utility::usleep(unsigned long usec
)
119 ts
.tv_sec
= usec
/ 1000000;
120 ts
.tv_nsec
= (usec
% 1000000) * 1000;
121 // POSIX.1 recommends using nanosleep instead of usleep
122 ::nanosleep(&ts
, NULL
);
126 // Drops the program's group privileges.
127 void Utility::dropGroupPrivs( uid_t uid
, gid_t gid
)
129 if(gid
&& gid
!= getegid()) {
131 g_log
<<Logger::Critical
<<"Unable to set effective group id to "<<gid
<<": "<<stringerror()<<endl
;
135 g_log
<<Logger::Info
<<"Set effective group id to "<<gid
<<endl
;
137 struct passwd
*pw
=getpwuid(uid
);
139 g_log
<<Logger::Warning
<<"Unable to determine user name for uid "<<uid
<<endl
;
140 if (setgroups(0, NULL
)<0) {
141 g_log
<<Logger::Critical
<<"Unable to drop supplementary gids: "<<stringerror()<<endl
;
145 if (initgroups(pw
->pw_name
, gid
)<0) {
146 g_log
<<Logger::Critical
<<"Unable to set supplementary groups: "<<stringerror()<<endl
;
154 // Drops the program's user privileges.
155 void Utility::dropUserPrivs( uid_t uid
)
157 if(uid
&& uid
!= geteuid()) {
159 g_log
<<Logger::Critical
<<"Unable to set effective user id to "<<uid
<<": "<<stringerror()<<endl
;
163 g_log
<<Logger::Info
<<"Set effective user id to "<<uid
<<endl
;
168 // Returns the current process id.
169 Utility::pid_t
Utility::getpid( void )
175 // Returns the current time.
176 int Utility::gettimeofday( struct timeval
*tv
, void *tz
)
178 return ::gettimeofday(tv
,0);
181 // Sets the random seed.
182 void Utility::srandom(void)
185 gettimeofday(&tv
, 0);
186 ::srandom(tv
.tv_sec
^ tv
.tv_usec
^ getpid());
190 int Utility::writev(int socket
, const iovec
*vector
, size_t count
)
192 return ::writev(socket
,vector
,count
);
195 /* this is cut and pasted from dietlibc, gratefully copied! */
196 static int isleap(int year
) {
197 /* every fourth year is a leap year except for century years that are
198 * not divisible by 400. */
199 return (!(year
%4) && ((year
%100) || !(year
%400)));
202 time_t Utility::timegm(struct tm
*const t
)
204 const static short spm
[13] = /* days per month -- nonleap! */
212 (31+28+31+30+31+30+31),
213 (31+28+31+30+31+30+31+31),
214 (31+28+31+30+31+30+31+31+30),
215 (31+28+31+30+31+30+31+31+30+31),
216 (31+28+31+30+31+30+31+31+30+31+30),
217 (31+28+31+30+31+30+31+31+30+31+30+31),
222 time_t years
= t
->tm_year
- 70;
224 if (t
->tm_sec
>60) { t
->tm_min
+= t
->tm_sec
/60; t
->tm_sec
%=60; }
225 if (t
->tm_min
>60) { t
->tm_hour
+= t
->tm_min
/60; t
->tm_min
%=60; }
226 if (t
->tm_hour
>60) { t
->tm_mday
+= t
->tm_hour
/60; t
->tm_hour
%=60; }
227 if (t
->tm_mon
>11) { t
->tm_year
+= t
->tm_mon
/12; t
->tm_mon
%=12; }
229 while (t
->tm_mday
>spm
[1+t
->tm_mon
]) {
230 if (t
->tm_mon
==1 && isleap(t
->tm_year
+1900)) {
231 if (t
->tm_mon
==31+29) break;
234 t
->tm_mday
-=spm
[t
->tm_mon
];
236 if (t
->tm_mon
>11) { t
->tm_mon
=0; ++t
->tm_year
; }
241 /* Days since 1970 is 365 * number of years + number of leap years since 1970 */
242 day
= years
* 365 + (years
+ 1) / 4;
244 /* After 2100 we have to subtract 3 leap years for every 400 years
245 This is not intuitive. Most mktime implementations do not support
246 dates after 2059, anyway, so we might leave this out for its
248 if ((years
-= 131) >= 0) {
250 day
-= (years
>> 2) * 3 + 1;
251 if ((years
&= 3) == 3) years
--;
255 day
+= t
->tm_yday
= spm
[t
->tm_mon
] + t
->tm_mday
-1 + ( isleap (t
->tm_year
+1900) & (t
->tm_mon
> 1) );
257 /* day is now the number of days since 'Jan 1 1970' */
259 t
->tm_wday
= (day
+ 4) % i
; /* Sunday=0, Monday=1, ..., Saturday=6 */
264 return ((day
+ t
->tm_hour
) * i
+ t
->tm_min
) * i
+ t
->tm_sec
;