Merge "system/core 64-bit cleanup."
diff --git a/adb/services.c b/adb/services.c
index 951048e..1c9db0d 100644
--- a/adb/services.c
+++ b/adb/services.c
@@ -86,7 +86,7 @@
 {
     char buf[100];
     char value[PROPERTY_VALUE_MAX];
-    int port = (int)cookie;
+    int port = (int) (uintptr_t) cookie;
 
     if (port <= 0) {
         snprintf(buf, sizeof(buf), "invalid port\n");
@@ -269,7 +269,7 @@
 #if !ADB_HOST
 static void subproc_waiter_service(int fd, void *cookie)
 {
-    pid_t pid = (pid_t)cookie;
+    pid_t pid = (pid_t) (uintptr_t) cookie;
 
     D("entered. fd=%d of pid=%d\n", fd, pid);
     for (;;) {
@@ -314,7 +314,7 @@
     sti = malloc(sizeof(stinfo));
     if(sti == 0) fatal("cannot allocate stinfo");
     sti->func = subproc_waiter_service;
-    sti->cookie = (void*)pid;
+    sti->cookie = (void*) (uintptr_t) pid;
     sti->fd = ret_fd;
 
     if(adb_thread_create( &t, service_bootstrap_func, sti)){
@@ -397,7 +397,7 @@
         if (sscanf(name + 6, "%d", &port) == 0) {
             port = 0;
         }
-        ret = create_service_thread(restart_tcp_service, (void *)port);
+        ret = create_service_thread(restart_tcp_service, (void *) (uintptr_t) port);
     } else if(!strncmp(name, "usb:", 4)) {
         ret = create_service_thread(restart_usb_service, NULL);
 #endif
diff --git a/adb/sockets.c b/adb/sockets.c
index 7f54ad9..de14a22 100644
--- a/adb/sockets.c
+++ b/adb/sockets.c
@@ -342,7 +342,7 @@
 
         while(avail > 0) {
             r = adb_read(fd, x, avail);
-            D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%d\n", s->id, s->fd, r, r<0?errno:0, avail);
+            D("LS(%d): post adb_read(fd=%d,...) r=%d (errno=%d) avail=%zu\n", s->id, s->fd, r, r<0?errno:0, avail);
             if(r > 0) {
                 avail -= r;
                 x += r;
diff --git a/adb/transport.c b/adb/transport.c
index 224fe55..6002530 100644
--- a/adb/transport.c
+++ b/adb/transport.c
@@ -1142,9 +1142,9 @@
     char *p = ptr;
     int r;
 #if ADB_TRACE
-    int  len0 = len;
+    size_t len0 = len;
 #endif
-    D("readx: fd=%d wanted=%d\n", fd, (int)len);
+    D("readx: fd=%d wanted=%zu\n", fd, len);
     while(len > 0) {
         r = adb_read(fd, p, len);
         if(r > 0) {
@@ -1163,7 +1163,7 @@
     }
 
 #if ADB_TRACE
-    D("readx: fd=%d wanted=%d got=%d\n", fd, len0, len0 - len);
+    D("readx: fd=%d wanted=%zu got=%zu\n", fd, len0, len0 - len);
     dump_hex( ptr, len0 );
 #endif
     return 0;
diff --git a/adb/transport_local.c b/adb/transport_local.c
index 1cfa24d..d2dbca6 100644
--- a/adb/transport_local.c
+++ b/adb/transport_local.c
@@ -159,7 +159,7 @@
     int serverfd, fd;
     struct sockaddr addr;
     socklen_t alen;
-    int port = (int)arg;
+    int port = (int) (uintptr_t) arg;
 
     D("transport: server_socket_thread() starting\n");
     serverfd = -1;
@@ -241,7 +241,7 @@
 /* 'ok' reply from the adb QEMUD service. */
 static const char _ok_resp[]    = "ok";
 
-    const int port = (int)arg;
+    const int port = (int) (uintptr_t) arg;
     int res, fd;
     char tmp[256];
     char con_name[32];
@@ -326,7 +326,7 @@
 
     D("transport: local %s init\n", HOST ? "client" : "server");
 
-    if(adb_thread_create(&thr, func, (void *)port)) {
+    if(adb_thread_create(&thr, func, (void *) (uintptr_t) port)) {
         fatal_errno("cannot create local socket %s thread",
                     HOST ? "client" : "server");
     }
diff --git a/adb/usb_linux.c b/adb/usb_linux.c
index 7bf2057..8ff753e 100644
--- a/adb/usb_linux.c
+++ b/adb/usb_linux.c
@@ -179,7 +179,7 @@
 
                 // should have device and configuration descriptors, and atleast two endpoints
             if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
-                D("desclength %d is too small\n", desclength);
+                D("desclength %zu is too small\n", desclength);
                 adb_close(fd);
                 continue;
             }
diff --git a/adb/usb_linux_client.c b/adb/usb_linux_client.c
index fb1dad0..69d8062 100644
--- a/adb/usb_linux_client.c
+++ b/adb/usb_linux_client.c
@@ -384,7 +384,7 @@
         ret = adb_read(bulk_out, buf + count, length - count);
         if (ret < 0) {
             if (errno != EINTR) {
-                D("[ bulk_read failed fd=%d length=%d count=%d ]\n",
+                D("[ bulk_read failed fd=%d length=%zu count=%zu ]\n",
                                            bulk_out, length, count);
                 return ret;
             }
diff --git a/fastbootd/commands.c b/fastbootd/commands.c
index d8a601f..063e1a6 100644
--- a/fastbootd/commands.c
+++ b/fastbootd/commands.c
@@ -29,6 +29,7 @@
  * SUCH DAMAGE.
  */
 
+#include <inttypes.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
@@ -319,7 +320,7 @@
         return;
     }
 
-    D(INFO, "writing %lld bytes to '%s'\n", sz, arg);
+    D(INFO, "writing %"PRId64" bytes to '%s'\n", sz, arg);
 
     if (flash_write(partition, phandle->download_fd, sz, header_sz)) {
         fastboot_fail(phandle, "flash write failure");
diff --git a/fastbootd/commands/flash.c b/fastbootd/commands/flash.c
index 0954217..1eb4d1b 100644
--- a/fastbootd/commands/flash.c
+++ b/fastbootd/commands/flash.c
@@ -31,6 +31,7 @@
 
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <inttypes.h>
 #include <sys/mman.h>
 
 #include "flash.h"
@@ -82,7 +83,7 @@
 {
     int64_t size;
     size = get_block_device_size(fd);
-    D(DEBUG, "erase %llu data from %d\n", size, fd);
+    D(DEBUG, "erase %"PRId64" data from %d\n", size, fd);
 
     return wipe_block_device(fd, size);
 }
@@ -97,7 +98,7 @@
         int current_size = MIN(size - written, BUFFER_SIZE);
 
         if (gpt_mmap(&input, written + skip, current_size, data_fd)) {
-            D(ERR, "Error in writing data, unable to map data file %d at %d size %d", size, skip, current_size);
+            D(ERR, "Error in writing data, unable to map data file %zd at %zd size %d", size, skip, current_size);
             return -1;
         }
         if (gpt_mmap(&output, written, current_size, partition_fd)) {
diff --git a/fastbootd/secure.c b/fastbootd/secure.c
index a657ad4..186e026 100644
--- a/fastbootd/secure.c
+++ b/fastbootd/secure.c
@@ -151,7 +151,7 @@
         char buf[256];
         unsigned long err = ERR_peek_last_error();
         D(ERR, "Verification failed with reason: %s, %s", ERR_lib_error_string(err),  ERR_error_string(err, buf));
-        D(ERR, "Data used: content %d", (int) content);
+        D(ERR, "Data used: content %p", content);
     }
 
     ERR_clear_error();
diff --git a/fastbootd/transport.c b/fastbootd/transport.c
index 19a705c..ce8f9d0 100644
--- a/fastbootd/transport.c
+++ b/fastbootd/transport.c
@@ -56,14 +56,14 @@
 
     buffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
     if (buffer == NULL) {
-        D(ERR, "mmap(%u) failed: %d %s", len, errno, strerror(errno));
+        D(ERR, "mmap(%zu) failed: %d %s", len, errno, strerror(errno));
         goto err;
     }
 
     while (n < len) {
         ret = thandle->transport->read(thandle, buffer + n, len - n);
         if (ret <= 0) {
-            D(WARN, "transport read failed, ret=%d %s", ret, strerror(-ret));
+            D(WARN, "transport read failed, ret=%zd %s", ret, strerror(-ret));
             break;
         }
         n += ret;
diff --git a/fastbootd/transport_socket.c b/fastbootd/transport_socket.c
index ff0f3bd..664d473 100644
--- a/fastbootd/transport_socket.c
+++ b/fastbootd/transport_socket.c
@@ -88,7 +88,7 @@
     ssize_t ret;
     struct socket_handle *handle = container_of(thandle, struct socket_handle, handle);
 
-    D(DEBUG, "about to write (fd=%d, len=%d)", handle->fd, len);
+    D(DEBUG, "about to write (fd=%d, len=%zu)", handle->fd, len);
     ret = bulk_write(handle->fd, data, len);
     if (ret < 0) {
         D(ERR, "ERROR: fd = %d, ret = %zd", handle->fd, ret);
@@ -103,7 +103,7 @@
     ssize_t ret;
     struct socket_handle *handle = container_of(thandle, struct socket_handle, handle);
 
-    D(DEBUG, "about to read (fd=%d, len=%d)", handle->fd, len);
+    D(DEBUG, "about to read (fd=%d, len=%zu)", handle->fd, len);
     ret = bulk_read(handle->fd, data, len);
     if (ret < 0) {
         D(ERR, "ERROR: fd = %d, ret = %zd", handle->fd, ret);
diff --git a/fastbootd/usb_linux_client.c b/fastbootd/usb_linux_client.c
index 7a8e46f..64420e9 100644
--- a/fastbootd/usb_linux_client.c
+++ b/fastbootd/usb_linux_client.c
@@ -217,7 +217,7 @@
     struct transport *t = thandle->transport;
     struct usb_transport *usb_transport = container_of(t, struct usb_transport, transport);
 
-    D(DEBUG, "about to write (fd=%d, len=%d)", usb_transport->bulk_in, len);
+    D(DEBUG, "about to write (fd=%d, len=%zu)", usb_transport->bulk_in, len);
     ret = bulk_write(usb_transport->bulk_in, data, len);
     if (ret < 0) {
         D(ERR, "ERROR: fd = %d, ret = %zd", usb_transport->bulk_in, ret);
@@ -233,7 +233,7 @@
     struct transport *t = thandle->transport;
     struct usb_transport *usb_transport = container_of(t, struct usb_transport, transport);
 
-    D(DEBUG, "about to read (fd=%d, len=%d)", usb_transport->bulk_out, len);
+    D(DEBUG, "about to read (fd=%d, len=%zu)", usb_transport->bulk_out, len);
     ret = bulk_read(usb_transport->bulk_out, data, len);
     if (ret < 0) {
         D(ERR, "ERROR: fd = %d, ret = %zd", usb_transport->bulk_out, ret);
diff --git a/fs_mgr/fs_mgr_verity.c b/fs_mgr/fs_mgr_verity.c
index 969eab2..1549316 100644
--- a/fs_mgr/fs_mgr_verity.c
+++ b/fs_mgr/fs_mgr_verity.c
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -178,7 +179,7 @@
         goto out;
     }
     if (magic_number != VERITY_METADATA_MAGIC_NUMBER) {
-        ERROR("Couldn't find verity metadata at offset %llu!\n", device_length);
+        ERROR("Couldn't find verity metadata at offset %"PRIu64"!\n", device_length);
         goto out;
     }
 
diff --git a/init/init.c b/init/init.c
index ab52749..4266a73 100644
--- a/init/init.c
+++ b/init/init.c
@@ -623,7 +623,7 @@
         total_bytes_written += chunk_size;
     }
 
-    INFO("Mixed %d bytes from /dev/hw_random into /dev/urandom",
+    INFO("Mixed %zu bytes from /dev/hw_random into /dev/urandom",
                 total_bytes_written);
     result = 0;
 
diff --git a/init/property_service.c b/init/property_service.c
index c370769..ac63377 100644
--- a/init/property_service.c
+++ b/init/property_service.c
@@ -168,7 +168,7 @@
     if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
         goto err;
 
-    if (selinux_check_access(sctx, tctx, class, perm, name) == 0)
+    if (selinux_check_access(sctx, tctx, class, perm, (void*) name) == 0)
         result = 1;
 
     freecon(tctx);
@@ -382,7 +382,7 @@
 
     r = TEMP_FAILURE_RETRY(recv(s, &msg, sizeof(msg), 0));
     if(r != sizeof(prop_msg)) {
-        ERROR("sys_prop: mis-match msg size received: %d expected: %d errno: %d\n",
+        ERROR("sys_prop: mis-match msg size received: %d expected: %zu errno: %d\n",
               r, sizeof(prop_msg), errno);
         close(s);
         return;
@@ -522,7 +522,7 @@
                     || (sb.st_uid != 0)
                     || (sb.st_gid != 0)
                     || (sb.st_nlink != 1)) {
-                ERROR("skipping insecure property file %s (uid=%lu gid=%lu nlink=%d mode=%o)\n",
+                ERROR("skipping insecure property file %s (uid=%u gid=%u nlink=%d mode=%o)\n",
                       entry->d_name, sb.st_uid, sb.st_gid, sb.st_nlink, sb.st_mode);
                 close(fd);
                 continue;
diff --git a/libdiskconfig/diskconfig.c b/libdiskconfig/diskconfig.c
index d5425de..6fd81b7 100644
--- a/libdiskconfig/diskconfig.c
+++ b/libdiskconfig/diskconfig.c
@@ -19,6 +19,7 @@
 
 #include <errno.h>
 #include <fcntl.h>
+#include <inttypes.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -80,7 +81,7 @@
         *plen *= multiple;
 
         if (*plen > 0xffffffffULL) {
-            ALOGE("Length specified is too large!: %llu KB", *plen);
+            ALOGE("Length specified is too large!: %"PRIu64" KB", *plen);
             return 1;
         }
     }
@@ -371,8 +372,8 @@
 
     /* only matters for disks, not files */
     if (S_ISBLK(stat.st_mode) && total_size > disk_size) {
-        ALOGE("Total requested size of partitions (%llu) is greater than disk "
-             "size (%llu).", total_size, disk_size);
+        ALOGE("Total requested size of partitions (%"PRIu64") is greater than disk "
+             "size (%"PRIu64").", total_size, disk_size);
         goto fail;
     }
 
diff --git a/libion/ion_test.c b/libion/ion_test.c
index 12163e9..8872282 100644
--- a/libion/ion_test.c
+++ b/libion/ion_test.c
@@ -63,7 +63,7 @@
 
     ret = ion_free(fd, handle);
     if (ret) {
-        printf("%s failed: %s %p\n", __func__, strerror(ret), handle);
+        printf("%s failed: %s %d\n", __func__, strerror(ret), handle);
         return;
     }
     ion_close(fd);
diff --git a/libmemtrack/memtrack_test.c b/libmemtrack/memtrack_test.c
index cd94bc5..eaadfa7 100644
--- a/libmemtrack/memtrack_test.c
+++ b/libmemtrack/memtrack_test.c
@@ -35,7 +35,7 @@
         return -1;
     }
 
-    if (asprintf(&filename, "/proc/%zd/cmdline", pid) < 0) {
+    if (asprintf(&filename, "/proc/%d/cmdline", pid) < 0) {
         rc = 1;
         goto exit;
     }
diff --git a/libsparse/output_file.c b/libsparse/output_file.c
index a28b0a5..e63c4a9 100644
--- a/libsparse/output_file.c
+++ b/libsparse/output_file.c
@@ -18,6 +18,7 @@
 #define _LARGEFILE64_SOURCE 1
 
 #include <fcntl.h>
+#include <inttypes.h>
 #include <limits.h>
 #include <stdbool.h>
 #include <stddef.h>
@@ -342,7 +343,7 @@
 	int ret, chunk;
 
 	if (skip_len % out->block_size) {
-		error("don't care size %llu is not a multiple of the block size %u",
+		error("don't care size %"PRIi64" is not a multiple of the block size %u",
 				skip_len, out->block_size);
 		return -1;
 	}
diff --git a/logcat/logcat.cpp b/logcat/logcat.cpp
index d44c679..4c6139c 100644
--- a/logcat/logcat.cpp
+++ b/logcat/logcat.cpp
@@ -298,7 +298,7 @@
                     }
                     else if (entry->entry.len != ret - sizeof(struct logger_entry)) {
                         fprintf(stderr, "read: unexpected length. Expected %d, got %d\n",
-                                entry->entry.len, ret - sizeof(struct logger_entry));
+                                entry->entry.len, ret - (int) sizeof(struct logger_entry));
                         exit(EXIT_FAILURE);
                     }
 
diff --git a/logwrapper/logwrapper.c b/logwrapper/logwrapper.c
index db6cb4c..9e0385d 100644
--- a/logwrapper/logwrapper.c
+++ b/logwrapper/logwrapper.c
@@ -90,7 +90,8 @@
     }
 
     if (seg_fault_on_exit) {
-        *(int *)status = 0;  // causes SIGSEGV with fault_address = status
+        uintptr_t fault_address = (uintptr_t) status;
+        *(int *) fault_address = 0;  // causes SIGSEGV with fault_address = status
     }
 
     return rc;
diff --git a/toolbox/Android.mk b/toolbox/Android.mk
index 75ce53f..436f13d 100644
--- a/toolbox/Android.mk
+++ b/toolbox/Android.mk
@@ -89,6 +89,8 @@
 
 LOCAL_C_INCLUDES := bionic/libc/bionic
 
+LOCAL_CFLAGS += -Wno-unused-parameter
+
 LOCAL_SHARED_LIBRARIES := \
 	libcutils \
 	liblog \
diff --git a/toolbox/dd.c b/toolbox/dd.c
index a8c12d2..6b61ffb 100644
--- a/toolbox/dd.c
+++ b/toolbox/dd.c
@@ -92,9 +92,6 @@
 extern int		progress;
 extern const u_char	*ctab;
 
-
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-#define MAX(a, b) ((a) > (b) ? (a) : (b))
 #define DEFFILEMODE (S_IRUSR | S_IWUSR)
 
 static void dd_close(void);
diff --git a/toolbox/getevent.c b/toolbox/getevent.c
index 5f5e16b..ed381f5 100644
--- a/toolbox/getevent.c
+++ b/toolbox/getevent.c
@@ -166,7 +166,7 @@
                     if(bit_labels && (print_flags & PRINT_LABELS)) {
                         bit_label = get_label(bit_labels, j * 8 + k);
                         if(bit_label)
-                            printf(" %.20s%c%*s", bit_label, down, 20 - strlen(bit_label), "");
+                            printf(" %.20s%c%*s", bit_label, down, (int) (20 - strlen(bit_label)), "");
                         else
                             printf(" %04x%c                ", j * 8 + k, down);
                     } else {
diff --git a/toolbox/insmod.c b/toolbox/insmod.c
index 756a64b..fb1448b 100644
--- a/toolbox/insmod.c
+++ b/toolbox/insmod.c
@@ -4,6 +4,7 @@
 #include <unistd.h>
 #include <malloc.h>
 #include <errno.h>
+#include <sys/param.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
@@ -45,7 +46,6 @@
 	return buffer;
 }
 
-#define min(x,y) ((x) < (y) ? (x) : (y))
 int insmod_main(int argc, char **argv)
 {
 	void *file;
@@ -73,7 +73,7 @@
 		char *ptr = opts;
 
 		for (i = 2; (i < argc) && (ptr < end); i++) {
-			len = min(strlen(argv[i]), end - ptr);
+			len = MIN(strlen(argv[i]), end - ptr);
 			memcpy(ptr, argv[i], len);
 			ptr += len;
 			*ptr++ = ' ';
diff --git a/toolbox/ls.c b/toolbox/ls.c
index c740f84..3cc5bb2 100644
--- a/toolbox/ls.c
+++ b/toolbox/ls.c
@@ -182,8 +182,8 @@
 
     mode2str(s->st_mode, mode);
     if (flags & LIST_LONG_NUMERIC) {
-        snprintf(user, sizeof(user), "%ld", s->st_uid);
-        snprintf(group, sizeof(group), "%ld", s->st_gid);
+        snprintf(user, sizeof(user), "%u", s->st_uid);
+        snprintf(group, sizeof(group), "%u", s->st_gid);
     } else {
         user2str(s->st_uid, user, sizeof(user));
         group2str(s->st_gid, group, sizeof(group));
diff --git a/toolbox/nandread.c b/toolbox/nandread.c
index 4666f26..d43b2fe 100644
--- a/toolbox/nandread.c
+++ b/toolbox/nandread.c
@@ -158,7 +158,7 @@
         printf("oobavail: %u\n", ecclayout.oobavail);
     }
     if (ecclayout.oobavail > spare_size)
-        printf("oobavail, %d > image spare size, %d\n", ecclayout.oobavail, spare_size);
+        printf("oobavail, %d > image spare size, %zu\n", ecclayout.oobavail, spare_size);
 
     ret = ioctl(fd, ECCGETSTATS, &initial_ecc);
     if (ret) {
diff --git a/toolbox/newfs_msdos.c b/toolbox/newfs_msdos.c
index 6d78eb6..27dca42 100644
--- a/toolbox/newfs_msdos.c
+++ b/toolbox/newfs_msdos.c
@@ -234,13 +234,6 @@
 static void setstr(u_int8_t *, const char *, size_t);
 static void usage(void);
 
-#ifdef ANDROID
-#define powerof2(x)     ((((x) - 1) & (x)) == 0)
-#define howmany(x, y)   (((x) + ((y) - 1)) / (y))
-#define MAX(x,y) ((x) > (y) ? (x) : (y))
-#define MIN(a, b) ((a) < (b) ? (a) : (b))
-
-#endif
 /*
  * Construct a FAT12, FAT16, or FAT32 file system.
  */
diff --git a/toolbox/schedtop.c b/toolbox/schedtop.c
index 6859b50..a76f968 100644
--- a/toolbox/schedtop.c
+++ b/toolbox/schedtop.c
@@ -212,7 +212,7 @@
     }
     if (!(flags & FLAG_BATCH))
         printf("\e[H\e[0J");
-    printf("Processes: %d, Threads %d\n", processes.active, threads.active);
+    printf("Processes: %zu, Threads %zu\n", processes.active, threads.active);
     switch (time_dp) {
     case 3:
         printf("   TID --- SINCE LAST ---- ---------- TOTAL ----------\n");
diff --git a/toolbox/setconsole.c b/toolbox/setconsole.c
index 0159c07..85ea7c2 100644
--- a/toolbox/setconsole.c
+++ b/toolbox/setconsole.c
@@ -12,12 +12,9 @@
 static int activate_thread_switch_vc;
 static void *activate_thread(void *arg)
 {
-    int res;
-    int fd = (int)arg;
+    int fd = (int) (uintptr_t) arg;
     while(activate_thread_switch_vc >= 0) {
-        do {
-            res = ioctl(fd, VT_ACTIVATE, (void*)activate_thread_switch_vc);
-        } while(res < 0 && errno == EINTR);
+        int res = TEMP_FAILURE_RETRY(ioctl(fd, VT_ACTIVATE, activate_thread_switch_vc));
         if (res < 0) {
             fprintf(stderr, "ioctl( vcfd, VT_ACTIVATE, vtnum) failed, %d %d %s for %d\n", res, errno, strerror(errno), activate_thread_switch_vc);
         }
@@ -131,11 +128,9 @@
         activate_thread_switch_vc = switch_vc;
         pthread_attr_init(&attr);
         pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
-        pthread_create(&thread, &attr, activate_thread, (void*)fd);
-        
-        do {
-            res = ioctl(fd, VT_WAITACTIVE, (void*)switch_vc);
-        } while(res < 0 && errno == EINTR);
+        pthread_create(&thread, &attr, activate_thread, (void*) (uintptr_t) fd);
+
+        res = TEMP_FAILURE_RETRY(ioctl(fd, VT_WAITACTIVE, switch_vc));
         activate_thread_switch_vc = -1;
         if (res < 0) {
             fprintf(stderr, "ioctl( vcfd, VT_WAITACTIVE, vtnum) failed, %d %d %s for %d\n", res, errno, strerror(errno), switch_vc);
@@ -157,7 +152,7 @@
         }
     }
     if (mode != -1) {
-        if (ioctl(fd, KDSETMODE, (void*)mode) < 0) {
+        if (ioctl(fd, KDSETMODE, mode) < 0) {
             fprintf(stderr, "KDSETMODE %d failed\n", mode);
             return -1;
         }