- if (r >= 0) {
- full_write(fd, string_ptr, r);
- free(string_ptr);
- }
- return r;
-}
-#endif
-
-// Die with an error message if we can't copy an entire FILE * to stdout, then
-// close that file.
-void xprint_and_close_file(FILE *file)
-{
- fflush(stdout);
- // copyfd outputs error messages for us.
- if (bb_copyfd_eof(fileno(file), 1) == -1)
- xfunc_die();
-
- fclose(file);
-}
-
-// Die if we can't chdir to a new path.
-void xchdir(const char *path)
-{
- if (chdir(path))
- bb_perror_msg_and_die("chdir(%s)", path);
-}
-
-// Print a warning message if opendir() fails, but don't die.
-DIR *warn_opendir(const char *path)
-{
- DIR *dp;
-
- dp = opendir(path);
- if (!dp)
- bb_perror_msg("can't open '%s'", path);
- return dp;
-}
-
-// Die with an error message if opendir() fails.
-DIR *xopendir(const char *path)
-{
- DIR *dp;
-
- dp = opendir(path);
- if (!dp)
- bb_perror_msg_and_die("can't open '%s'", path);
- return dp;
-}
-
-// Die with an error message if we can't open a new socket.
-int xsocket(int domain, int type, int protocol)
-{
- int r = socket(domain, type, protocol);
-
- if (r < 0) {
- /* Hijack vaguely related config option */
-#if ENABLE_VERBOSE_RESOLUTION_ERRORS
- const char *s = "INET";
- if (domain == AF_PACKET) s = "PACKET";
- if (domain == AF_NETLINK) s = "NETLINK";
-USE_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";)
- bb_perror_msg_and_die("socket(AF_%s)", s);
-#else
- bb_perror_msg_and_die("socket");
-#endif
- }
-
- return r;
-}
-
-// Die with an error message if we can't bind a socket to an address.
-void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
-{
- if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind");
-}
-
-// Die with an error message if we can't listen for connections on a socket.
-void xlisten(int s, int backlog)
-{
- if (listen(s, backlog)) bb_perror_msg_and_die("listen");
-}
-
-/* Die with an error message if sendto failed.
- * Return bytes sent otherwise */
-ssize_t xsendto(int s, const void *buf, size_t len, const struct sockaddr *to,
- socklen_t tolen)
-{
- ssize_t ret = sendto(s, buf, len, 0, to, tolen);
- if (ret < 0) {
- if (ENABLE_FEATURE_CLEAN_UP)
- close(s);
- bb_perror_msg_and_die("sendto");
- }
- return ret;
-}
-
-// xstat() - a stat() which dies on failure with meaningful error message
-void xstat(const char *name, struct stat *stat_buf)
-{
- if (stat(name, stat_buf))
- bb_perror_msg_and_die("can't stat '%s'", name);
-}
-
-// selinux_or_die() - die if SELinux is disabled.
-void selinux_or_die(void)
-{
-#if ENABLE_SELINUX
- int rc = is_selinux_enabled();
- if (rc == 0) {
- bb_error_msg_and_die("SELinux is disabled");
- } else if (rc < 0) {
- bb_error_msg_and_die("is_selinux_enabled() failed");
- }
-#else
- bb_error_msg_and_die("SELinux support is disabled");
-#endif
-}
-