123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691 |
- #include "libbb.h"
- #ifndef DMALLOC
- void* FAST_FUNC malloc_or_warn(size_t size)
- {
- void *ptr = malloc(size);
- if (ptr == NULL && size != 0)
- bb_error_msg(bb_msg_memory_exhausted);
- return ptr;
- }
- void* FAST_FUNC xmalloc(size_t size)
- {
- void *ptr = malloc(size);
- if (ptr == NULL && size != 0)
- bb_error_msg_and_die(bb_msg_memory_exhausted);
- return ptr;
- }
- void* FAST_FUNC xrealloc(void *ptr, size_t size)
- {
- ptr = realloc(ptr, size);
- if (ptr == NULL && size != 0)
- bb_error_msg_and_die(bb_msg_memory_exhausted);
- return ptr;
- }
- #endif
- void* FAST_FUNC xzalloc(size_t size)
- {
- void *ptr = xmalloc(size);
- memset(ptr, 0, size);
- return ptr;
- }
- char* FAST_FUNC xstrdup(const char *s)
- {
- char *t;
- if (s == NULL)
- return NULL;
- t = strdup(s);
- if (t == NULL)
- bb_error_msg_and_die(bb_msg_memory_exhausted);
- return t;
- }
- char* FAST_FUNC xstrndup(const char *s, int n)
- {
- int m;
- char *t;
- if (ENABLE_DEBUG && s == NULL)
- bb_error_msg_and_die("xstrndup bug");
-
-
- m = n;
- t = (char*) s;
- while (m) {
- if (!*t) break;
- m--;
- t++;
- }
- n -= m;
- t = xmalloc(n + 1);
- t[n] = '\0';
- return memcpy(t, s, n);
- }
- void* FAST_FUNC xmemdup(const void *s, int n)
- {
- return memcpy(xmalloc(n), s, n);
- }
- FILE* FAST_FUNC xfopen(const char *path, const char *mode)
- {
- FILE *fp = fopen(path, mode);
- if (fp == NULL)
- bb_perror_msg_and_die("can't open '%s'", path);
- return fp;
- }
- int FAST_FUNC xopen3(const char *pathname, int flags, int mode)
- {
- int ret;
- ret = open(pathname, flags, mode);
- if (ret < 0) {
- bb_perror_msg_and_die("can't open '%s'", pathname);
- }
- return ret;
- }
- int FAST_FUNC xopen(const char *pathname, int flags)
- {
- return xopen3(pathname, flags, 0666);
- }
- int FAST_FUNC open3_or_warn(const char *pathname, int flags, int mode)
- {
- int ret;
- ret = open(pathname, flags, mode);
- if (ret < 0) {
- bb_perror_msg("can't open '%s'", pathname);
- }
- return ret;
- }
- int FAST_FUNC open_or_warn(const char *pathname, int flags)
- {
- return open3_or_warn(pathname, flags, 0666);
- }
- int FAST_FUNC xopen_nonblocking(const char *pathname)
- {
- return xopen(pathname, O_RDONLY | O_NONBLOCK);
- }
- int FAST_FUNC xopen_as_uid_gid(const char *pathname, int flags, uid_t u, gid_t g)
- {
- int fd;
- uid_t old_euid = geteuid();
- gid_t old_egid = getegid();
- xsetegid(g);
- xseteuid(u);
- fd = xopen(pathname, flags);
- xseteuid(old_euid);
- xsetegid(old_egid);
- return fd;
- }
- void FAST_FUNC xunlink(const char *pathname)
- {
- if (unlink(pathname))
- bb_perror_msg_and_die("can't remove file '%s'", pathname);
- }
- void FAST_FUNC xrename(const char *oldpath, const char *newpath)
- {
- if (rename(oldpath, newpath))
- bb_perror_msg_and_die("can't move '%s' to '%s'", oldpath, newpath);
- }
- int FAST_FUNC rename_or_warn(const char *oldpath, const char *newpath)
- {
- int n = rename(oldpath, newpath);
- if (n)
- bb_perror_msg("can't move '%s' to '%s'", oldpath, newpath);
- return n;
- }
- void FAST_FUNC xpipe(int filedes[2])
- {
- if (pipe(filedes))
- bb_perror_msg_and_die("can't create pipe");
- }
- void FAST_FUNC xdup2(int from, int to)
- {
- if (dup2(from, to) != to)
- bb_perror_msg_and_die("can't duplicate file descriptor");
- }
- void FAST_FUNC xmove_fd(int from, int to)
- {
- if (from == to)
- return;
- xdup2(from, to);
- close(from);
- }
- void FAST_FUNC xwrite(int fd, const void *buf, size_t count)
- {
- if (count) {
- ssize_t size = full_write(fd, buf, count);
- if ((size_t)size != count) {
-
- bb_perror_msg_and_die(
- size >= 0 ? "short write" : "write error"
- );
- }
- }
- }
- void FAST_FUNC xwrite_str(int fd, const char *str)
- {
- xwrite(fd, str, strlen(str));
- }
- void FAST_FUNC xclose(int fd)
- {
- if (close(fd))
- bb_perror_msg_and_die("close failed");
- }
- off_t FAST_FUNC xlseek(int fd, off_t offset, int whence)
- {
- off_t off = lseek(fd, offset, whence);
- if (off == (off_t)-1) {
- if (whence == SEEK_SET)
- bb_perror_msg_and_die("lseek(%"OFF_FMT"u)", offset);
- bb_perror_msg_and_die("lseek");
- }
- return off;
- }
- int FAST_FUNC xmkstemp(char *template)
- {
- int fd = mkstemp(template);
- if (fd < 0)
- bb_perror_msg_and_die("can't create temp file '%s'", template);
- return fd;
- }
- void FAST_FUNC die_if_ferror(FILE *fp, const char *fn)
- {
- if (ferror(fp)) {
-
- bb_error_msg_and_die("%s: I/O error", fn);
- }
- }
- void FAST_FUNC die_if_ferror_stdout(void)
- {
- die_if_ferror(stdout, bb_msg_standard_output);
- }
- int FAST_FUNC fflush_all(void)
- {
- return fflush(NULL);
- }
- int FAST_FUNC bb_putchar(int ch)
- {
- return putchar(ch);
- }
- void FAST_FUNC xprint_and_close_file(FILE *file)
- {
- fflush_all();
-
- if (bb_copyfd_eof(fileno(file), STDOUT_FILENO) == -1)
- xfunc_die();
- fclose(file);
- }
- char* FAST_FUNC xasprintf(const char *format, ...)
- {
- va_list p;
- int r;
- char *string_ptr;
- va_start(p, format);
- r = vasprintf(&string_ptr, format, p);
- va_end(p);
- if (r < 0)
- bb_error_msg_and_die(bb_msg_memory_exhausted);
- return string_ptr;
- }
- void FAST_FUNC xsetenv(const char *key, const char *value)
- {
- if (setenv(key, value, 1))
- bb_error_msg_and_die(bb_msg_memory_exhausted);
- }
- void FAST_FUNC bb_unsetenv(const char *var)
- {
- char onstack[128 - 16];
- char *tp;
- tp = strchr(var, '=');
- if (tp) {
-
- unsigned sz = tp - var;
- if (sz < sizeof(onstack)) {
- ((char*)mempcpy(onstack, var, sz))[0] = '\0';
- tp = NULL;
- var = onstack;
- } else {
-
- var = tp = xstrndup(var, sz);
- }
- }
- unsetenv(var);
- free(tp);
- }
- void FAST_FUNC bb_unsetenv_and_free(char *var)
- {
- bb_unsetenv(var);
- free(var);
- }
- void FAST_FUNC xsetgid(gid_t gid)
- {
- if (setgid(gid)) bb_perror_msg_and_die("setgid");
- }
- void FAST_FUNC xsetuid(uid_t uid)
- {
- if (setuid(uid)) bb_perror_msg_and_die("setuid");
- }
- void FAST_FUNC xsetegid(gid_t egid)
- {
- if (setegid(egid)) bb_perror_msg_and_die("setegid");
- }
- void FAST_FUNC xseteuid(uid_t euid)
- {
- if (seteuid(euid)) bb_perror_msg_and_die("seteuid");
- }
- void FAST_FUNC xchdir(const char *path)
- {
- if (chdir(path))
- bb_perror_msg_and_die("can't change directory to '%s'", path);
- }
- void FAST_FUNC xfchdir(int fd)
- {
- if (fchdir(fd))
- bb_perror_msg_and_die("fchdir");
- }
- void FAST_FUNC xchroot(const char *path)
- {
- if (chroot(path))
- bb_perror_msg_and_die("can't change root directory to '%s'", path);
- xchdir("/");
- }
- DIR* FAST_FUNC warn_opendir(const char *path)
- {
- DIR *dp;
- dp = opendir(path);
- if (!dp)
- bb_perror_msg("can't open '%s'", path);
- return dp;
- }
- DIR* FAST_FUNC xopendir(const char *path)
- {
- DIR *dp;
- dp = opendir(path);
- if (!dp)
- bb_perror_msg_and_die("can't open '%s'", path);
- return dp;
- }
- int FAST_FUNC xsocket(int domain, int type, int protocol)
- {
- int r = socket(domain, type, protocol);
- if (r < 0) {
-
- #if ENABLE_VERBOSE_RESOLUTION_ERRORS
- const char *s = "INET";
- # ifdef AF_PACKET
- if (domain == AF_PACKET) s = "PACKET";
- # endif
- # ifdef AF_NETLINK
- if (domain == AF_NETLINK) s = "NETLINK";
- # endif
- IF_FEATURE_IPV6(if (domain == AF_INET6) s = "INET6";)
- bb_perror_msg_and_die("socket(AF_%s,%d,%d)", s, type, protocol);
- #else
- bb_perror_msg_and_die("socket");
- #endif
- }
- return r;
- }
- 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");
- }
- void FAST_FUNC xlisten(int s, int backlog)
- {
- if (listen(s, backlog)) bb_perror_msg_and_die("listen");
- }
- ssize_t FAST_FUNC 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;
- }
- void FAST_FUNC xstat(const char *name, struct stat *stat_buf)
- {
- if (stat(name, stat_buf))
- bb_perror_msg_and_die("can't stat '%s'", name);
- }
- void FAST_FUNC xfstat(int fd, struct stat *stat_buf, const char *errmsg)
- {
-
- if (fstat(fd, stat_buf))
- bb_simple_perror_msg_and_die(errmsg);
- }
- void FAST_FUNC 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
- }
- int FAST_FUNC ioctl_or_perror_and_die(int fd, unsigned request, void *argp, const char *fmt,...)
- {
- int ret;
- va_list p;
- ret = ioctl(fd, request, argp);
- if (ret < 0) {
- va_start(p, fmt);
- bb_verror_msg(fmt, p, strerror(errno));
-
- va_end(p);
- xfunc_die();
- }
- return ret;
- }
- int FAST_FUNC ioctl_or_perror(int fd, unsigned request, void *argp, const char *fmt,...)
- {
- va_list p;
- int ret = ioctl(fd, request, argp);
- if (ret < 0) {
- va_start(p, fmt);
- bb_verror_msg(fmt, p, strerror(errno));
- va_end(p);
- }
- return ret;
- }
- #if ENABLE_IOCTL_HEX2STR_ERROR
- int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp, const char *ioctl_name)
- {
- int ret;
- ret = ioctl(fd, request, argp);
- if (ret < 0)
- bb_simple_perror_msg(ioctl_name);
- return ret;
- }
- int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp, const char *ioctl_name)
- {
- int ret;
- ret = ioctl(fd, request, argp);
- if (ret < 0)
- bb_simple_perror_msg_and_die(ioctl_name);
- return ret;
- }
- #else
- int FAST_FUNC bb_ioctl_or_warn(int fd, unsigned request, void *argp)
- {
- int ret;
- ret = ioctl(fd, request, argp);
- if (ret < 0)
- bb_perror_msg("ioctl %#x failed", request);
- return ret;
- }
- int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp)
- {
- int ret;
- ret = ioctl(fd, request, argp);
- if (ret < 0)
- bb_perror_msg_and_die("ioctl %#x failed", request);
- return ret;
- }
- #endif
- char* FAST_FUNC xmalloc_ttyname(int fd)
- {
- char buf[128];
- int r = ttyname_r(fd, buf, sizeof(buf) - 1);
- if (r)
- return NULL;
- return xstrdup(buf);
- }
- void FAST_FUNC generate_uuid(uint8_t *buf)
- {
-
- pid_t pid;
- int i;
- i = open("/dev/urandom", O_RDONLY);
- if (i >= 0) {
- read(i, buf, 16);
- close(i);
- }
-
- srand(monotonic_us());
- pid = getpid();
- while (1) {
- for (i = 0; i < 16; i++)
- buf[i] ^= rand() >> 5;
- if (pid == 0)
- break;
- srand(pid);
- pid = 0;
- }
-
- buf[4 + 2 ] = (buf[4 + 2 ] & 0x0f) | 0x40;
-
- buf[4 + 2 + 2] = (buf[4 + 2 + 2] & 0x3f) | 0x80;
- }
- #if BB_MMU
- pid_t FAST_FUNC xfork(void)
- {
- pid_t pid;
- pid = fork();
- if (pid < 0)
- bb_perror_msg_and_die("vfork"+1);
- return pid;
- }
- #endif
- void FAST_FUNC xvfork_parent_waits_and_exits(void)
- {
- pid_t pid;
- fflush_all();
- pid = xvfork();
- if (pid > 0) {
-
- int exit_status = wait_for_exitstatus(pid);
- if (WIFSIGNALED(exit_status))
- kill_myself_with_sig(WTERMSIG(exit_status));
- _exit(WEXITSTATUS(exit_status));
- }
-
- }
|