Bump to version 1.22.1
[platform/upstream/busybox.git] / libbb / xfuncs_printf.c
index 91f7ba2..e4ac6a0 100644 (file)
@@ -6,7 +6,7 @@
  * Copyright (C) 2006 Rob Landley
  * Copyright (C) 2006 Denys Vlasenko
  *
- * Licensed under GPL version 2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
 /* We need to have separate xfuncs.c and xfuncs_printf.c because
@@ -134,21 +134,12 @@ int FAST_FUNC xopen3(const char *pathname, int flags, int mode)
        return ret;
 }
 
-// Die if we can't open an existing file and return a fd.
+// Die if we can't open a file and return a fd.
 int FAST_FUNC xopen(const char *pathname, int flags)
 {
        return xopen3(pathname, flags, 0666);
 }
 
-/* Die if we can't open an existing file readonly with O_NONBLOCK
- * and return the fd.
- * Note that for ioctl O_RDONLY is sufficient.
- */
-int FAST_FUNC xopen_nonblocking(const char *pathname)
-{
-       return xopen(pathname, O_RDONLY | O_NONBLOCK);
-}
-
 // Warn if we can't open a file and return a fd.
 int FAST_FUNC open3_or_warn(const char *pathname, int flags, int mode)
 {
@@ -167,6 +158,32 @@ int FAST_FUNC open_or_warn(const char *pathname, int flags)
        return open3_or_warn(pathname, flags, 0666);
 }
 
+/* Die if we can't open an existing file readonly with O_NONBLOCK
+ * and return the fd.
+ * Note that for ioctl O_RDONLY is sufficient.
+ */
+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))
@@ -240,6 +257,14 @@ off_t FAST_FUNC xlseek(int fd, off_t offset, int whence)
        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;
+}
+
 // Die with supplied filename if this FILE* has ferror set.
 void FAST_FUNC die_if_ferror(FILE *fp, const char *fn)
 {
@@ -343,17 +368,28 @@ 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");
+}
+
 // Die if we can't chdir to a new path.
 void FAST_FUNC xchdir(const char *path)
 {
        if (chdir(path))
-               bb_perror_msg_and_die("chdir(%s)", path);
+               bb_perror_msg_and_die("can't change directory to '%s'", path);
 }
 
 void FAST_FUNC xchroot(const char *path)
 {
        if (chroot(path))
-               bb_perror_msg_and_die("can't change root directory to %s", path);
+               bb_perror_msg_and_die("can't change root directory to '%s'", path);
+       xchdir("/");
 }
 
 // Print a warning message if opendir() fails, but don't die.
@@ -436,6 +472,16 @@ void FAST_FUNC xstat(const char *name, struct stat *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)
+{
+       /* errmsg is usually a file name, but not always:
+        * xfstat may be called in a spot where file name is no longer
+        * available, and caller may give e.g. "can't stat input file" string.
+        */
+       if (fstat(fd, stat_buf))
+               bb_simple_perror_msg_and_die(errmsg);
+}
+
 // selinux_or_die() - die if SELinux is disabled.
 void FAST_FUNC selinux_or_die(void)
 {
@@ -522,13 +568,11 @@ int FAST_FUNC bb_xioctl(int fd, unsigned request, void *argp)
 
 char* FAST_FUNC xmalloc_ttyname(int fd)
 {
-       char *buf = xzalloc(128);
-       int r = ttyname_r(fd, buf, 127);
-       if (r) {
-               free(buf);
-               buf = NULL;
-       }
-       return buf;
+       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)