comparison dbutil.c @ 989:73ea0dce9a57 pam

Merge up to date
author Matt Johnston <matt@ucc.asn.au>
date Fri, 23 Jan 2015 21:38:47 +0800
parents db9fa5971d24
children 6fb4c010c448
comparison
equal deleted inserted replaced
925:bae0b34bc059 989:73ea0dce9a57
46 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 46 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
47 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 47 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
48 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 48 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
49 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 49 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
50 50
51 #include "config.h"
52
53 #ifdef __linux__
54 #define _GNU_SOURCE
55 /* To call clock_gettime() directly */
56 #include <sys/syscall.h>
57 #endif /* __linux */
58
59 #ifdef HAVE_MACH_MACH_TIME_H
60 #include <mach/mach_time.h>
61 #include <mach/mach.h>
62 #endif
63
51 #include "includes.h" 64 #include "includes.h"
52 #include "dbutil.h" 65 #include "dbutil.h"
53 #include "buffer.h" 66 #include "buffer.h"
54 #include "session.h" 67 #include "session.h"
55 #include "atomicio.h" 68 #include "atomicio.h"
146 } 159 }
147 160
148 gettimeofday(&tv, NULL); 161 gettimeofday(&tv, NULL);
149 162
150 va_start(param, format); 163 va_start(param, format);
151 fprintf(stderr, "TRACE (%d) %d.%d: ", getpid(), tv.tv_sec, tv.tv_usec); 164 fprintf(stderr, "TRACE (%d) %d.%d: ", getpid(), (int)tv.tv_sec, (int)tv.tv_usec);
152 vfprintf(stderr, format, param); 165 vfprintf(stderr, format, param);
153 fprintf(stderr, "\n"); 166 fprintf(stderr, "\n");
154 va_end(param); 167 va_end(param);
155 } 168 }
156 169
168 } 181 }
169 182
170 gettimeofday(&tv, NULL); 183 gettimeofday(&tv, NULL);
171 184
172 va_start(param, format); 185 va_start(param, format);
173 fprintf(stderr, "TRACE2 (%d) %d.%d: ", getpid(), tv.tv_sec, tv.tv_usec); 186 fprintf(stderr, "TRACE2 (%d) %d.%d: ", getpid(), (int)tv.tv_sec, (int)tv.tv_usec);
174 vfprintf(stderr, format, param); 187 vfprintf(stderr, format, param);
175 fprintf(stderr, "\n"); 188 fprintf(stderr, "\n");
176 va_end(param); 189 va_end(param);
177 } 190 }
178 #endif /* DEBUG_TRACE */ 191 #endif /* DEBUG_TRACE */
186 } 199 }
187 200
188 void set_sock_priority(int sock, enum dropbear_prio prio) { 201 void set_sock_priority(int sock, enum dropbear_prio prio) {
189 202
190 int iptos_val = 0, so_prio_val = 0, rc; 203 int iptos_val = 0, so_prio_val = 0, rc;
204
205 /* Don't log ENOTSOCK errors so that this can harmlessly be called
206 * on a client '-J' proxy pipe */
191 207
192 /* set the TOS bit for either ipv4 or ipv6 */ 208 /* set the TOS bit for either ipv4 or ipv6 */
193 #ifdef IPTOS_LOWDELAY 209 #ifdef IPTOS_LOWDELAY
194 if (prio == DROPBEAR_PRIO_LOWDELAY) { 210 if (prio == DROPBEAR_PRIO_LOWDELAY) {
195 iptos_val = IPTOS_LOWDELAY; 211 iptos_val = IPTOS_LOWDELAY;
196 } else if (prio == DROPBEAR_PRIO_BULK) { 212 } else if (prio == DROPBEAR_PRIO_BULK) {
197 iptos_val = IPTOS_THROUGHPUT; 213 iptos_val = IPTOS_THROUGHPUT;
198 } 214 }
199 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS) 215 #if defined(IPPROTO_IPV6) && defined(IPV6_TCLASS)
200 rc = setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, (void*)&iptos_val, sizeof(iptos_val)); 216 rc = setsockopt(sock, IPPROTO_IPV6, IPV6_TCLASS, (void*)&iptos_val, sizeof(iptos_val));
201 if (rc < 0) { 217 if (rc < 0 && errno != ENOTSOCK) {
202 TRACE(("Couldn't set IPV6_TCLASS (%s)", strerror(errno))); 218 TRACE(("Couldn't set IPV6_TCLASS (%s)", strerror(errno)));
203 } 219 }
204 #endif 220 #endif
205 rc = setsockopt(sock, IPPROTO_IP, IP_TOS, (void*)&iptos_val, sizeof(iptos_val)); 221 rc = setsockopt(sock, IPPROTO_IP, IP_TOS, (void*)&iptos_val, sizeof(iptos_val));
206 if (rc < 0) { 222 if (rc < 0 && errno != ENOTSOCK) {
207 TRACE(("Couldn't set IP_TOS (%s)", strerror(errno))); 223 TRACE(("Couldn't set IP_TOS (%s)", strerror(errno)));
208 } 224 }
209 #endif 225 #endif
210 226
211 #ifdef SO_PRIORITY 227 #ifdef SO_PRIORITY
214 } else if (prio == DROPBEAR_PRIO_BULK) { 230 } else if (prio == DROPBEAR_PRIO_BULK) {
215 so_prio_val = TC_PRIO_BULK; 231 so_prio_val = TC_PRIO_BULK;
216 } 232 }
217 /* linux specific, sets QoS class. see tc-prio(8) */ 233 /* linux specific, sets QoS class. see tc-prio(8) */
218 rc = setsockopt(sock, SOL_SOCKET, SO_PRIORITY, (void*) &so_prio_val, sizeof(so_prio_val)); 234 rc = setsockopt(sock, SOL_SOCKET, SO_PRIORITY, (void*) &so_prio_val, sizeof(so_prio_val));
219 if (rc < 0) 235 if (rc < 0 && errno != ENOTSOCK)
220 dropbear_log(LOG_WARNING, "Couldn't set SO_PRIORITY (%s)", 236 dropbear_log(LOG_WARNING, "Couldn't set SO_PRIORITY (%s)",
221 strerror(errno)); 237 strerror(errno));
222 #endif 238 #endif
223 239
224 } 240 }
317 close(sock); 333 close(sock);
318 TRACE(("bind(%s) failed", port)) 334 TRACE(("bind(%s) failed", port))
319 continue; 335 continue;
320 } 336 }
321 337
322 if (listen(sock, 20) < 0) { 338 if (listen(sock, DROPBEAR_LISTEN_BACKLOG) < 0) {
323 err = errno; 339 err = errno;
324 close(sock); 340 close(sock);
325 TRACE(("listen() failed")) 341 TRACE(("listen() failed"))
326 continue; 342 continue;
327 } 343 }
930 c |= (xa[i] ^ xb[i]); 946 c |= (xa[i] ^ xb[i]);
931 } 947 }
932 return c; 948 return c;
933 } 949 }
934 950
951 #if defined(__linux__) && defined(SYS_clock_gettime)
952 /* CLOCK_MONOTONIC_COARSE was added in Linux 2.6.32 but took a while to
953 reach userspace include headers */
954 #ifndef CLOCK_MONOTONIC_COARSE
955 #define CLOCK_MONOTONIC_COARSE 6
956 #endif
957 static clockid_t get_linux_clock_source() {
958 struct timespec ts;
959 if (syscall(SYS_clock_gettime, CLOCK_MONOTONIC_COARSE, &ts) == 0) {
960 return CLOCK_MONOTONIC_COARSE;
961 }
962
963 if (syscall(SYS_clock_gettime, CLOCK_MONOTONIC, &ts) == 0) {
964 return CLOCK_MONOTONIC;
965 }
966 return -1;
967 }
968 #endif
969
970 time_t monotonic_now() {
971 #if defined(__linux__) && defined(SYS_clock_gettime)
972 static clockid_t clock_source = -2;
973
974 if (clock_source == -2) {
975 /* First run, find out which one works.
976 -1 will fall back to time() */
977 clock_source = get_linux_clock_source();
978 }
979
980 if (clock_source >= 0) {
981 struct timespec ts;
982 if (syscall(SYS_clock_gettime, clock_source, &ts) != 0) {
983 /* Intermittent clock failures should not happen */
984 dropbear_exit("Clock broke");
985 }
986 return ts.tv_sec;
987 }
988 #endif /* linux clock_gettime */
989
990 #if defined(HAVE_MACH_ABSOLUTE_TIME)
991 /* OS X, see https://developer.apple.com/library/mac/qa/qa1398/_index.html */
992 static mach_timebase_info_data_t timebase_info;
993 if (timebase_info.denom == 0) {
994 mach_timebase_info(&timebase_info);
995 }
996 return mach_absolute_time() * timebase_info.numer / timebase_info.denom
997 / 1e9;
998 #endif /* osx mach_absolute_time */
999
1000 /* Fallback for everything else - this will sometimes go backwards */
1001 return time(NULL);
1002 }
1003