- return dp;
-}
-
-// Die with an error message if opendir() fails.
-DIR *xopendir(const char *path)
-{
- DIR *dp;
-
- if ((dp = opendir(path)) == NULL)
- bb_perror_msg_and_die("cannot open '%s'", path);
- return dp;
-}
-
-#ifndef BB_NOMMU
-// Die with an error message if we can't daemonize.
-void xdaemon(int nochdir, int noclose)
-{
- if (daemon(nochdir, noclose))
- bb_perror_msg_and_die("daemon");
-}
-#endif
-
-void bb_sanitize_stdio_maybe_daemonize(int daemonize)
-{
- int fd;
- /* Mega-paranoid */
- fd = xopen(bb_dev_null, O_RDWR);
- while (fd < 2)
- fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
- if (daemonize) {
- pid_t pid = fork();
- if (pid < 0) /* wtf? */
- bb_perror_msg_and_die("fork");
- if (pid) /* parent */
- exit(0);
- /* child */
- /* if daemonizing, make sure we detach from stdio */
- setsid();
- dup2(fd, 0);
- dup2(fd, 1);
- dup2(fd, 2);
- }
- while (fd > 2)
- close(fd--); /* close everything after fd#2 */
-}
-void bb_sanitize_stdio(void)
-{
- bb_sanitize_stdio_maybe_daemonize(0);
-}
-void bb_daemonize(void)
-{
- bb_sanitize_stdio_maybe_daemonize(1);
-}
-
-// 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) bb_perror_msg_and_die("socket");
-
- 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");
-}
-
-// xstat() - a stat() which dies on failure with meaningful error message
-void xstat(char *name, struct stat *stat_buf)
-{
- if (stat(name, stat_buf))
- bb_perror_msg_and_die("can't stat '%s'", name);