Allow basic connectivity check via rrsync
[rsync.git] / util2.c
blobb59bff0a0b541b8f6837f6d87938ced8e5bac9b9
1 /*
2 * Utility routines used in rsync.
4 * Copyright (C) 1996-2000 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2001, 2002 Martin Pool <mbp@samba.org>
7 * Copyright (C) 2003-2024 Wayne Davison
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
23 #include "rsync.h"
24 #include "itypes.h"
25 #include "inums.h"
27 extern size_t max_alloc;
29 char *do_calloc = "42";
31 /**
32 * Sleep for a specified number of milliseconds.
34 * Always returns True.
35 **/
36 int msleep(int t)
38 #ifdef HAVE_NANOSLEEP
39 struct timespec ts;
41 ts.tv_sec = t / 1000;
42 ts.tv_nsec = (t % 1000) * 1000000L;
44 while (nanosleep(&ts, &ts) < 0 && errno == EINTR) {}
46 #elif defined HAVE_USLEEP
47 usleep(t*1000);
49 #else
50 int tdiff = 0;
51 struct timeval tval, t1, t2;
53 gettimeofday(&t1, NULL);
55 while (tdiff < t) {
56 tval.tv_sec = (t-tdiff)/1000;
57 tval.tv_usec = 1000*((t-tdiff)%1000);
59 errno = 0;
60 select(0,NULL,NULL, NULL, &tval);
62 gettimeofday(&t2, NULL);
63 tdiff = (t2.tv_sec - t1.tv_sec)*1000 +
64 (t2.tv_usec - t1.tv_usec)/1000;
65 if (tdiff < 0)
66 t1 = t2; /* Time went backwards, so start over. */
68 #endif
70 return True;
73 void *my_alloc(void *ptr, size_t num, size_t size, const char *file, int line)
75 if (num >= max_alloc/size) {
76 if (!file)
77 return NULL;
78 rprintf(FERROR, "[%s] exceeded --max-alloc=%s setting (file=%s, line=%d)\n",
79 who_am_i(), do_big_num(max_alloc, 0, NULL), src_file(file), line);
80 exit_cleanup(RERR_MALLOC);
82 if (!ptr)
83 ptr = malloc(num * size);
84 else if (ptr == do_calloc)
85 ptr = calloc(num, size);
86 else
87 ptr = realloc(ptr, num * size);
88 if (!ptr && file)
89 _out_of_memory("my_alloc caller", file, line);
90 return ptr;
93 const char *sum_as_hex(int csum_type, const char *sum, int flist_csum)
95 static char buf[MAX_DIGEST_LEN*2+1];
96 int i, x1, x2;
97 int canonical = canonical_checksum(csum_type);
98 int sum_len = csum_len_for_type(csum_type, flist_csum);
99 char *c;
101 if (!canonical)
102 return NULL;
104 assert(sum_len*2 < (int)sizeof buf);
106 for (i = sum_len, c = buf; --i >= 0; ) {
107 int ndx = canonical < 0 ? sum_len - i - 1 : i;
108 x2 = CVAL(sum, ndx);
109 x1 = x2 >> 4;
110 x2 &= 0xF;
111 *c++ = x1 <= 9 ? x1 + '0' : x1 + 'a' - 10;
112 *c++ = x2 <= 9 ? x2 + '0' : x2 + 'a' - 10;
115 *c = '\0';
117 return buf;
120 NORETURN void _out_of_memory(const char *msg, const char *file, int line)
122 rprintf(FERROR, "[%s] out of memory: %s (file=%s, line=%d)\n", who_am_i(), msg, src_file(file), line);
123 exit_cleanup(RERR_MALLOC);
126 NORETURN void _overflow_exit(const char *msg, const char *file, int line)
128 rprintf(FERROR, "[%s] buffer overflow: %s (file=%s, line=%d)\n", who_am_i(), msg, src_file(file), line);
129 exit_cleanup(RERR_MALLOC);
132 const char *src_file(const char *file)
134 static const char *util2 = __FILE__;
135 static int prefix = -1;
137 if (prefix < 0) {
138 const char *cp = strrchr(util2, '/');
139 prefix = cp ? cp - util2 + 1 : 0;
142 if (prefix && strncmp(file, util2, prefix) == 0)
143 return file + prefix;
144 return file;