summaryrefslogtreecommitdiff
path: root/src/common/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/util.c')
-rw-r--r--src/common/util.c111
1 files changed, 58 insertions, 53 deletions
diff --git a/src/common/util.c b/src/common/util.c
index f0c9346ddd..1f94345fc0 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -130,7 +130,7 @@ _tor_malloc(size_t size DMALLOC_PARAMS)
result = dmalloc_malloc(file, line, size, DMALLOC_FUNC_MALLOC, 0, 0);
if (!result) {
- err(LD_MM,"Out of memory. Dying.");
+ log_err(LD_MM,"Out of memory. Dying.");
/* XXX if these functions die within a worker process, they won't
* call spawn_exit */
exit(1);
@@ -162,7 +162,7 @@ _tor_realloc(void *ptr, size_t size DMALLOC_PARAMS)
result = dmalloc_realloc(file, line, ptr, size, DMALLOC_FUNC_REALLOC, 0);
if (!result) {
- err(LD_MM,"Out of memory. Dying.");
+ log_err(LD_MM,"Out of memory. Dying.");
exit(1);
}
return result;
@@ -180,7 +180,7 @@ _tor_strdup(const char *s DMALLOC_PARAMS)
dup = dmalloc_strdup(file, line, s, 0);
if (!dup) {
- err(LD_MM,"Out of memory. Dying.");
+ log_err(LD_MM,"Out of memory. Dying.");
exit(1);
}
return dup;
@@ -577,7 +577,7 @@ tv_udiff(struct timeval *start, struct timeval *end)
long secdiff = end->tv_sec - start->tv_sec;
if (labs(secdiff+1) > LONG_MAX/1000000) {
- warn(LD_GENERAL, "comparing times too far apart.");
+ log_warn(LD_GENERAL, "comparing times too far apart.");
return LONG_MAX;
}
@@ -647,7 +647,7 @@ tor_timegm(struct tm *tm)
int i;
year = tm->tm_year + 1900;
if (year < 1970 || tm->tm_mon < 0 || tm->tm_mon > 11) {
- warn(LD_BUG, "Out-of-range argument to tor_timegm");
+ log_warn(LD_BUG, "Out-of-range argument to tor_timegm");
return -1;
}
days = 365 * (year-1970) + n_leapdays(1970,year);
@@ -700,7 +700,7 @@ parse_rfc1123_time(const char *buf, time_t *t)
if (sscanf(buf, "%3s, %d %3s %d %d:%d:%d GMT", weekday,
&tm.tm_mday, month, &tm.tm_year, &tm.tm_hour,
&tm.tm_min, &tm.tm_sec) < 7) {
- warn(LD_GENERAL, "Got invalid RFC1123 time \"%s\"", buf);
+ log_warn(LD_GENERAL, "Got invalid RFC1123 time \"%s\"", buf);
return -1;
}
@@ -712,13 +712,14 @@ parse_rfc1123_time(const char *buf, time_t *t)
}
}
if (m<0) {
- warn(LD_GENERAL, "Got invalid RFC1123 time \"%s\"", buf);
+ log_warn(LD_GENERAL, "Got invalid RFC1123 time \"%s\"", buf);
return -1;
}
tm.tm_mon = m;
if (tm.tm_year < 1970) {
- warn(LD_GENERAL, "Got invalid RFC1123 time \"%s\". (Before 1970)", buf);
+ log_warn(LD_GENERAL,
+ "Got invalid RFC1123 time \"%s\". (Before 1970)", buf);
return -1;
}
tm.tm_year -= 1900;
@@ -747,17 +748,17 @@ parse_iso_time(const char *cp, time_t *t)
struct tm st_tm;
#ifdef HAVE_STRPTIME
if (!strptime(cp, "%Y-%m-%d %H:%M:%S", &st_tm)) {
- warn(LD_GENERAL, "Published time was unparseable"); return -1;
+ log_warn(LD_GENERAL, "Published time was unparseable"); return -1;
}
#else
unsigned int year=0, month=0, day=0, hour=100, minute=100, second=100;
if (sscanf(cp, "%u-%u-%u %u:%u:%u", &year, &month,
&day, &hour, &minute, &second) < 6) {
- warn(LD_GENERAL, "Published time was unparseable"); return -1;
+ log_warn(LD_GENERAL, "Published time was unparseable"); return -1;
}
if (year < 1970 || month < 1 || month > 12 || day < 1 || day > 31 ||
hour > 23 || minute > 59 || second > 61) {
- warn(LD_GENERAL, "Published time was nonsensical"); return -1;
+ log_warn(LD_GENERAL, "Published time was nonsensical"); return -1;
}
st_tm.tm_year = year-1900;
st_tm.tm_mon = month-1;
@@ -767,7 +768,7 @@ parse_iso_time(const char *cp, time_t *t)
st_tm.tm_sec = second;
#endif
if (st_tm.tm_year < 70) {
- warn(LD_GENERAL, "Got invalid ISO time \"%s\". (Before 1970)", cp);
+ log_warn(LD_GENERAL, "Got invalid ISO time \"%s\". (Before 1970)", cp);
return -1;
}
*t = tor_timegm(&st_tm);
@@ -903,7 +904,7 @@ check_private_dir(const char *dirname, cpd_check_t check)
log(LOG_WARN, LD_FS, "Directory %s does not exist.", dirname);
return -1;
} else if (check == CPD_CREATE) {
- info(LD_GENERAL, "Creating directory %s", dirname);
+ log_info(LD_GENERAL, "Creating directory %s", dirname);
#ifdef MS_WINDOWS
r = mkdir(dirname);
#else
@@ -965,8 +966,8 @@ write_str_to_file(const char *fname, const char *str, int bin)
{
#ifdef MS_WINDOWS
if (!bin && strchr(str, '\r')) {
- warn(LD_BUG,
- "Bug: we're writing a text string that already contains a CR.");
+ log_warn(LD_BUG,
+ "Bug: we're writing a text string that already contains a CR.");
}
#endif
return write_bytes_to_file(fname, str, strlen(str), bin);
@@ -1091,13 +1092,13 @@ read_file_to_str(const char *filename, int bin)
r = stat(f, &statbuf);
tor_free(f);
if (r < 0) {
- info(LD_FS,"Could not stat \"%s\".",filename);
+ log_info(LD_FS,"Could not stat \"%s\".",filename);
return NULL;
}
fd = open(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
if (fd<0) {
- warn(LD_FS,"Could not open \"%s\".",filename);
+ log_warn(LD_FS,"Could not open \"%s\".",filename);
return NULL;
}
@@ -1105,8 +1106,8 @@ read_file_to_str(const char *filename, int bin)
r = read_all(fd,string,statbuf.st_size,0);
if (r<0) {
- warn(LD_FS,"Error reading from file \"%s\": %s", filename,
- strerror(errno));
+ log_warn(LD_FS,"Error reading from file \"%s\": %s", filename,
+ strerror(errno));
tor_free(string);
close(fd);
return NULL;
@@ -1116,17 +1117,17 @@ read_file_to_str(const char *filename, int bin)
if (bin && r != statbuf.st_size) {
/* If we're in binary mode, then we'd better have an exact match for
* size. Otherwise, win32 encoding may throw us off, and that's okay. */
- warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
- r, (long)statbuf.st_size,filename);
+ log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
+ r, (long)statbuf.st_size,filename);
tor_free(string);
close(fd);
return NULL;
}
#ifdef MS_WINDOWS
if (!bin && strchr(string, '\r')) {
- debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
- "when reading %s. Coping.",
- filename);
+ log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
+ "when reading %s. Coping.",
+ filename);
tor_strstrip(string, "\r");
}
#endif
@@ -1220,8 +1221,8 @@ expand_filename(const char *filename)
if (filename[1] == '/' || filename[1] == '\0') {
home = getenv("HOME");
if (!home) {
- warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
- "expanding %s", filename);
+ log_warn(LD_CONFIG, "Couldn't find $HOME environment variable while "
+ "expanding %s", filename);
return NULL;
}
home = tor_strdup(home);
@@ -1235,14 +1236,14 @@ expand_filename(const char *filename)
else
username = tor_strdup(filename+1);
if (!(home = get_user_homedir(username))) {
- warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
+ log_warn(LD_CONFIG,"Couldn't get homedir for \"%s\"",username);
tor_free(username);
return NULL;
}
tor_free(username);
rest = slash ? (slash+1) : NULL;
#else
- warn(LD_CONFIG, "Couldn't expend homedir on system without pwd.h");
+ log_warn(LD_CONFIG, "Couldn't expend homedir on system without pwd.h");
return tor_strdup(filename);
#endif
}
@@ -1289,7 +1290,7 @@ tor_listdir(const char *dirname)
}
if (!FindNextFile(handle, &findData)) {
if (GetLastError() != ERROR_NO_MORE_FILES) {
- warn(LD_FS, "Error reading directory.");
+ log_warn(LD_FS, "Error reading directory.");
}
break;
}
@@ -1384,12 +1385,13 @@ parse_addr_port(const char *addrport, char **address, uint32_t *addr,
_address = tor_strndup(addrport, colon-addrport);
_port = (int) tor_parse_long(colon+1,10,1,65535,NULL,NULL);
if (!_port) {
- warn(LD_GENERAL, "Port '%s' out of range", colon+1);
+ log_warn(LD_GENERAL, "Port '%s' out of range", colon+1);
ok = 0;
}
if (!port_out) {
- warn(LD_GENERAL, "Port '%s' given on '%s' when not required", colon+1,
- addrport);
+ log_warn(LD_GENERAL,
+ "Port '%s' given on '%s' when not required",
+ colon+1, addrport);
ok = 0;
}
} else {
@@ -1400,7 +1402,7 @@ parse_addr_port(const char *addrport, char **address, uint32_t *addr,
if (addr) {
/* There's an addr pointer, so we need to resolve the hostname. */
if (tor_lookup_hostname(_address,addr)) {
- warn(LD_NET, "Couldn't look up '%s'", _address);
+ log_warn(LD_NET, "Couldn't look up '%s'", _address);
ok = 0;
*addr = 0;
}
@@ -1477,8 +1479,8 @@ parse_addr_and_port_range(const char *s, uint32_t *addr_out,
} else if (tor_inet_aton(address, &in) != 0) {
*addr_out = ntohl(in.s_addr);
} else {
- warn(LD_GENERAL, "Malformed IP \"%s\" in address pattern; rejecting.",
- address);
+ log_warn(LD_GENERAL, "Malformed IP \"%s\" in address pattern; rejecting.",
+ address);
goto err;
}
@@ -1493,16 +1495,17 @@ parse_addr_and_port_range(const char *s, uint32_t *addr_out,
if (!*endptr) {
/* strtol handled the whole mask. */
if (bits < 0 || bits > 32) {
- warn(LD_GENERAL,
- "Bad number of mask bits on address range; rejecting.");
+ log_warn(LD_GENERAL,
+ "Bad number of mask bits on address range; rejecting.");
goto err;
}
*mask_out = ~((1u<<(32-bits))-1);
} else if (tor_inet_aton(mask, &in) != 0) {
*mask_out = ntohl(in.s_addr);
} else {
- warn(LD_GENERAL, "Malformed mask \"%s\" on address range; rejecting.",
- mask);
+ log_warn(LD_GENERAL,
+ "Malformed mask \"%s\" on address range; rejecting.",
+ mask);
goto err;
}
}
@@ -1520,18 +1523,20 @@ parse_addr_and_port_range(const char *s, uint32_t *addr_out,
*port_max_out = (uint16_t) tor_parse_long(port, 10, 1, 65535, NULL,
&endptr);
if (*endptr || !*port_max_out) {
- warn(LD_GENERAL, "Malformed port \"%s\" on address range rejecting.",
- port);
+ log_warn(LD_GENERAL,
+ "Malformed port \"%s\" on address range rejecting.",
+ port);
}
} else if (*endptr || !*port_min_out) {
- warn(LD_GENERAL, "Malformed port \"%s\" on address range; rejecting.",
- port);
+ log_warn(LD_GENERAL,
+ "Malformed port \"%s\" on address range; rejecting.",
+ port);
goto err;
} else {
*port_max_out = *port_min_out;
}
if (*port_min_out > *port_max_out) {
- warn(LD_GENERAL, "Insane port range on address policy; rejecting.");
+ log_warn(LD_GENERAL, "Insane port range on address policy; rejecting.");
goto err;
}
}
@@ -1607,7 +1612,7 @@ get_interface_address(uint32_t *addr)
sock = socket(PF_INET,SOCK_DGRAM,IPPROTO_UDP);
if (sock < 0) {
int e = tor_socket_errno(-1);
- warn(LD_NET, "unable to create socket: %s", tor_socket_strerror(e));
+ log_warn(LD_NET, "unable to create socket: %s", tor_socket_strerror(e));
goto err;
}
@@ -1621,14 +1626,14 @@ get_interface_address(uint32_t *addr)
if (connect(sock,(struct sockaddr *)&target_addr,sizeof(target_addr))<0) {
int e = tor_socket_errno(sock);
- warn(LD_NET, "connnect() failed: %s", tor_socket_strerror(e));
+ log_warn(LD_NET, "connnect() failed: %s", tor_socket_strerror(e));
goto err;
}
/* XXXX Can this be right on IPv6 clients? */
if (getsockname(sock, (struct sockaddr*)&my_addr, &my_addr_len)) {
int e = tor_socket_errno(sock);
- warn(LD_NET, "getsockname() failed: %s", tor_socket_strerror(e));
+ log_warn(LD_NET, "getsockname() failed: %s", tor_socket_strerror(e));
goto err;
}
@@ -1667,7 +1672,7 @@ start_daemon(void)
pipe(daemon_filedes);
pid = fork();
if (pid < 0) {
- err(LD_GENERAL,"fork failed. Exiting.");
+ log_err(LD_GENERAL,"fork failed. Exiting.");
exit(1);
}
if (pid) { /* Parent */
@@ -1722,14 +1727,14 @@ finish_daemon(const char *desired_cwd)
desired_cwd = "/";
/* Don't hold the wrong FS mounted */
if (chdir(desired_cwd) < 0) {
- err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
+ log_err(LD_GENERAL,"chdir to \"%s\" failed. Exiting.",desired_cwd);
exit(1);
}
nullfd = open("/dev/null",
O_CREAT | O_RDWR | O_APPEND);
if (nullfd < 0) {
- err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
+ log_err(LD_GENERAL,"/dev/null can't be opened. Exiting.");
exit(1);
}
/* close fds linking to invoking terminal, but
@@ -1739,7 +1744,7 @@ finish_daemon(const char *desired_cwd)
if (dup2(nullfd,0) < 0 ||
dup2(nullfd,1) < 0 ||
dup2(nullfd,2) < 0) {
- err(LD_GENERAL,"dup2 failed. Exiting.");
+ log_err(LD_GENERAL,"dup2 failed. Exiting.");
exit(1);
}
if (nullfd > 2)
@@ -1768,8 +1773,8 @@ write_pidfile(char *filename)
FILE *pidfile;
if ((pidfile = fopen(filename, "w")) == NULL) {
- warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
- strerror(errno));
+ log_warn(LD_FS, "Unable to open \"%s\" for writing: %s", filename,
+ strerror(errno));
} else {
fprintf(pidfile, "%d\n", (int)getpid());
fclose(pidfile);