diff options
Diffstat (limited to 'libbb/xfuncs_printf.c')
-rw-r--r-- | libbb/xfuncs_printf.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/libbb/xfuncs_printf.c b/libbb/xfuncs_printf.c index 108e140..0bee40a 100644 --- a/libbb/xfuncs_printf.c +++ b/libbb/xfuncs_printf.c @@ -409,6 +409,21 @@ void FAST_FUNC xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind"); } +// Call a user supplied reporting function and die with an error message if we +// can't bind a socket to an address. +void FAST_FUNC xrbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen, + void (*reportf)(int err)) +{ + if (bind(sockfd, my_addr, addrlen)) { + if (reportf) { + int t_errno= errno; + reportf(t_errno); + errno= t_errno; + } + bb_perror_msg_and_die("bind"); + } +} + // Die with an error message if we can't listen for connections on a socket. void FAST_FUNC xlisten(int s, int backlog) { @@ -429,6 +444,47 @@ ssize_t FAST_FUNC xsendto(int s, const void *buf, size_t len, const struct sock return ret; } +/* Call a user supplied function and die with an error message if sendto failed. + * Return bytes sent otherwise */ +ssize_t FAST_FUNC xrsendto(int s, const void *buf, size_t len, + const struct sockaddr *to, socklen_t tolen, + void (*reportf)(int err)) +{ + ssize_t ret = sendto(s, buf, len, 0, to, tolen); + if (ret < 0) { + if (reportf) { + int t_errno= errno; + reportf(t_errno); + t_errno= errno; + } + if (ENABLE_FEATURE_CLEAN_UP) + close(s); + bb_perror_msg_and_die("sendto"); + } + return ret; +} + +/* Call a user supplied function with an error message if sendto failed. + * Return bytes sent otherwise */ +ssize_t FAST_FUNC rsendto(int s, const void *buf, size_t len, + const struct sockaddr *to, socklen_t tolen, + void (*reportf)(int err)) +{ + ssize_t ret = sendto(s, buf, len, 0, to, tolen); + if (ret < 0) { + int t_errno= errno; + if (reportf) { + reportf(t_errno); + } + if (ENABLE_FEATURE_CLEAN_UP) + close(s); + errno= t_errno; + bb_perror_msg("sendto"); + errno= t_errno; + } + return ret; +} + // xstat() - a stat() which dies on failure with meaningful error message void FAST_FUNC xstat(const char *name, struct stat *stat_buf) { |