Bump to version 1.22.1
[platform/upstream/busybox.git] / libbb / xfuncs_printf.c
index ba660a2..e4ac6a0 100644 (file)
@@ -140,15 +140,6 @@ 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.
@@ -532,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)