Add a PREUPLOAD.cfg file to enable clang-format

This will save some time on reviews. This also preemptively formats some
stuff so that future developers don't have to fight against it.

Bug: None
Test: pre-upload.py complained when I introduced a badly formatted
      change.

Change-Id: I75b38cebc1298e481e62dbc9cea72f39ce33f88f
diff --git a/.clang-format b/.clang-format
index a3ef233..9320a7a 100644
--- a/.clang-format
+++ b/.clang-format
@@ -3,4 +3,5 @@
 UseTab: Always
 BreakBeforeBraces: Linux
 AllowShortIfStatementsOnASingleLine: false
+AllowShortFunctionsOnASingleLine: false
 IndentCaseLabels: false
diff --git a/PREUPLOAD.cfg b/PREUPLOAD.cfg
new file mode 100644
index 0000000..254a062
--- /dev/null
+++ b/PREUPLOAD.cfg
@@ -0,0 +1,5 @@
+[Builtin Hooks]
+clang_format = true
+
+[Builtin Hooks Options]
+clang_format = --commit ${PREUPLOAD_COMMIT} --style file --extensions c
diff --git a/libminijail.c b/libminijail.c
index a44c5d2..70577c6 100644
--- a/libminijail.c
+++ b/libminijail.c
@@ -25,7 +25,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <syscall.h>
 #include <sys/capability.h>
 #include <sys/mount.h>
 #include <sys/param.h>
@@ -34,6 +33,7 @@
 #include <sys/types.h>
 #include <sys/user.h>
 #include <sys/wait.h>
+#include <syscall.h>
 #include <unistd.h>
 
 #include "libminijail.h"
@@ -119,37 +119,37 @@
 	 * accounted for in minijail_pre{enter|exec}() below.
 	 */
 	struct {
-		int uid:1;
-		int gid:1;
-		int usergroups:1;
-		int suppl_gids:1;
-		int use_caps:1;
-		int capbset_drop:1;
-		int vfs:1;
-		int enter_vfs:1;
-		int skip_remount_private:1;
-		int pids:1;
-		int ipc:1;
-		int net:1;
-		int enter_net:1;
-		int ns_cgroups:1;
-		int userns:1;
-		int disable_setgroups:1;
-		int seccomp:1;
-		int remount_proc_ro:1;
-		int no_new_privs:1;
-		int seccomp_filter:1;
-		int seccomp_filter_tsync:1;
-		int seccomp_filter_logging:1;
-		int chroot:1;
-		int pivot_root:1;
-		int mount_tmp:1;
-		int do_init:1;
-		int pid_file:1;
-		int cgroups:1;
-		int alt_syscall:1;
-		int reset_signal_mask:1;
-		int close_open_fds:1;
+		int uid : 1;
+		int gid : 1;
+		int usergroups : 1;
+		int suppl_gids : 1;
+		int use_caps : 1;
+		int capbset_drop : 1;
+		int vfs : 1;
+		int enter_vfs : 1;
+		int skip_remount_private : 1;
+		int pids : 1;
+		int ipc : 1;
+		int net : 1;
+		int enter_net : 1;
+		int ns_cgroups : 1;
+		int userns : 1;
+		int disable_setgroups : 1;
+		int seccomp : 1;
+		int remount_proc_ro : 1;
+		int no_new_privs : 1;
+		int seccomp_filter : 1;
+		int seccomp_filter_tsync : 1;
+		int seccomp_filter_logging : 1;
+		int chroot : 1;
+		int pivot_root : 1;
+		int mount_tmp : 1;
+		int do_init : 1;
+		int pid_file : 1;
+		int cgroups : 1;
+		int alt_syscall : 1;
+		int reset_signal_mask : 1;
+		int close_open_fds : 1;
 	} flags;
 	uid_t uid;
 	gid_t gid;
diff --git a/libminijail_unittest.c b/libminijail_unittest.c
index c9c31d8..199b658 100644
--- a/libminijail_unittest.c
+++ b/libminijail_unittest.c
@@ -9,14 +9,14 @@
 #include <errno.h>
 
 #include <fcntl.h>
-#include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/types.h>
 #include <sys/wait.h>
 
 #include "test_harness.h"
 
-#include "libminijail.h"
 #include "libminijail-private.h"
+#include "libminijail.h"
 #include "util.h"
 
 #if defined(__ANDROID__)
@@ -30,239 +30,255 @@
 char *consumestr(char **buf, size_t *buflength);
 
 /* Silence unused variable warnings. */
-TEST(silence_unused) {
-  EXPECT_STREQ(kLdPreloadEnvVar, kLdPreloadEnvVar);
-  EXPECT_STREQ(kFdEnvVar, kFdEnvVar);
-  EXPECT_STRNE(kFdEnvVar, kLdPreloadEnvVar);
+TEST(silence_unused)
+{
+	EXPECT_STREQ(kLdPreloadEnvVar, kLdPreloadEnvVar);
+	EXPECT_STREQ(kFdEnvVar, kFdEnvVar);
+	EXPECT_STRNE(kFdEnvVar, kLdPreloadEnvVar);
 }
 
-TEST(consumebytes_zero) {
-  char buf[1024];
-  size_t len = sizeof(buf);
-  char *pos = &buf[0];
-  EXPECT_NE(NULL, consumebytes(0, &pos, &len));
-  EXPECT_EQ(&buf[0], pos);
-  EXPECT_EQ(sizeof(buf), len);
+TEST(consumebytes_zero)
+{
+	char buf[1024];
+	size_t len = sizeof(buf);
+	char *pos = &buf[0];
+	EXPECT_NE(NULL, consumebytes(0, &pos, &len));
+	EXPECT_EQ(&buf[0], pos);
+	EXPECT_EQ(sizeof(buf), len);
 }
 
-TEST(consumebytes_exact) {
-  char buf[1024];
-  size_t len = sizeof(buf);
-  char *pos = &buf[0];
-  /* One past the end since it consumes the whole buffer. */
-  char *end = &buf[sizeof(buf)];
-  EXPECT_NE(NULL, consumebytes(len, &pos, &len));
-  EXPECT_EQ((size_t)0, len);
-  EXPECT_EQ(end, pos);
+TEST(consumebytes_exact)
+{
+	char buf[1024];
+	size_t len = sizeof(buf);
+	char *pos = &buf[0];
+	/* One past the end since it consumes the whole buffer. */
+	char *end = &buf[sizeof(buf)];
+	EXPECT_NE(NULL, consumebytes(len, &pos, &len));
+	EXPECT_EQ((size_t)0, len);
+	EXPECT_EQ(end, pos);
 }
 
-TEST(consumebytes_half) {
-  char buf[1024];
-  size_t len = sizeof(buf);
-  char *pos = &buf[0];
-  /* One past the end since it consumes the whole buffer. */
-  char *end = &buf[sizeof(buf) / 2];
-  EXPECT_NE(NULL, consumebytes(len / 2, &pos, &len));
-  EXPECT_EQ(sizeof(buf) / 2, len);
-  EXPECT_EQ(end, pos);
+TEST(consumebytes_half)
+{
+	char buf[1024];
+	size_t len = sizeof(buf);
+	char *pos = &buf[0];
+	/* One past the end since it consumes the whole buffer. */
+	char *end = &buf[sizeof(buf) / 2];
+	EXPECT_NE(NULL, consumebytes(len / 2, &pos, &len));
+	EXPECT_EQ(sizeof(buf) / 2, len);
+	EXPECT_EQ(end, pos);
 }
 
-TEST(consumebytes_toolong) {
-  char buf[1024];
-  size_t len = sizeof(buf);
-  char *pos = &buf[0];
-  /* One past the end since it consumes the whole buffer. */
-  EXPECT_EQ(NULL, consumebytes(len + 1, &pos, &len));
-  EXPECT_EQ(sizeof(buf), len);
-  EXPECT_EQ(&buf[0], pos);
+TEST(consumebytes_toolong)
+{
+	char buf[1024];
+	size_t len = sizeof(buf);
+	char *pos = &buf[0];
+	/* One past the end since it consumes the whole buffer. */
+	EXPECT_EQ(NULL, consumebytes(len + 1, &pos, &len));
+	EXPECT_EQ(sizeof(buf), len);
+	EXPECT_EQ(&buf[0], pos);
 }
 
-TEST(consumestr_zero) {
-  char buf[1024];
-  size_t len = 0;
-  char *pos = &buf[0];
-  memset(buf, 0xff, sizeof(buf));
-  EXPECT_EQ(NULL, consumestr(&pos, &len));
-  EXPECT_EQ((size_t)0, len);
-  EXPECT_EQ(&buf[0], pos);
+TEST(consumestr_zero)
+{
+	char buf[1024];
+	size_t len = 0;
+	char *pos = &buf[0];
+	memset(buf, 0xff, sizeof(buf));
+	EXPECT_EQ(NULL, consumestr(&pos, &len));
+	EXPECT_EQ((size_t)0, len);
+	EXPECT_EQ(&buf[0], pos);
 }
 
-TEST(consumestr_nonul) {
-  char buf[1024];
-  size_t len = sizeof(buf);
-  char *pos = &buf[0];
-  memset(buf, 0xff, sizeof(buf));
-  EXPECT_EQ(NULL, consumestr(&pos, &len));
-  EXPECT_EQ(sizeof(buf), len);
-  EXPECT_EQ(&buf[0], pos);
+TEST(consumestr_nonul)
+{
+	char buf[1024];
+	size_t len = sizeof(buf);
+	char *pos = &buf[0];
+	memset(buf, 0xff, sizeof(buf));
+	EXPECT_EQ(NULL, consumestr(&pos, &len));
+	EXPECT_EQ(sizeof(buf), len);
+	EXPECT_EQ(&buf[0], pos);
 }
 
-TEST(consumestr_full) {
-  char buf[1024];
-  size_t len = sizeof(buf);
-  char *pos = &buf[0];
-  memset(buf, 0xff, sizeof(buf));
-  buf[sizeof(buf)-1] = '\0';
-  EXPECT_EQ((void *)buf, consumestr(&pos, &len));
-  EXPECT_EQ((size_t)0, len);
-  EXPECT_EQ(&buf[sizeof(buf)], pos);
+TEST(consumestr_full)
+{
+	char buf[1024];
+	size_t len = sizeof(buf);
+	char *pos = &buf[0];
+	memset(buf, 0xff, sizeof(buf));
+	buf[sizeof(buf) - 1] = '\0';
+	EXPECT_EQ((void *)buf, consumestr(&pos, &len));
+	EXPECT_EQ((size_t)0, len);
+	EXPECT_EQ(&buf[sizeof(buf)], pos);
 }
 
-TEST(consumestr_trailing_nul) {
-  char buf[1024];
-  size_t len = sizeof(buf) - 1;
-  char *pos = &buf[0];
-  memset(buf, 0xff, sizeof(buf));
-  buf[sizeof(buf)-1] = '\0';
-  EXPECT_EQ(NULL, consumestr(&pos, &len));
-  EXPECT_EQ(sizeof(buf) - 1, len);
-  EXPECT_EQ(&buf[0], pos);
+TEST(consumestr_trailing_nul)
+{
+	char buf[1024];
+	size_t len = sizeof(buf) - 1;
+	char *pos = &buf[0];
+	memset(buf, 0xff, sizeof(buf));
+	buf[sizeof(buf) - 1] = '\0';
+	EXPECT_EQ(NULL, consumestr(&pos, &len));
+	EXPECT_EQ(sizeof(buf) - 1, len);
+	EXPECT_EQ(&buf[0], pos);
 }
 
-FIXTURE(marshal) {
-  char buf[4096];
-  struct minijail *m;
-  struct minijail *j;
-  size_t size;
+FIXTURE(marshal)
+{
+	char buf[4096];
+	struct minijail *m;
+	struct minijail *j;
+	size_t size;
 };
 
-FIXTURE_SETUP(marshal) {
-  self->m = minijail_new();
-  self->j = minijail_new();
-  ASSERT_TRUE(self->m && self->j) TH_LOG("allocation failed");
-  self->size = minijail_size(self->m);
-  ASSERT_GT(sizeof(self->buf), self->size) {
-    TH_LOG("static buffer too small for test");
-  }
+FIXTURE_SETUP(marshal)
+{
+	self->m = minijail_new();
+	self->j = minijail_new();
+	ASSERT_TRUE(self->m && self->j) TH_LOG("allocation failed");
+	self->size = minijail_size(self->m);
+	ASSERT_GT(sizeof(self->buf), self->size)
+	{
+		TH_LOG("static buffer too small for test");
+	}
 }
 
-FIXTURE_TEARDOWN(marshal) {
-  minijail_destroy(self->m);
-  minijail_destroy(self->j);
+FIXTURE_TEARDOWN(marshal)
+{
+	minijail_destroy(self->m);
+	minijail_destroy(self->j);
 }
 
-TEST_F(marshal, empty) {
-  ASSERT_EQ(0, minijail_marshal(self->m, self->buf, sizeof(self->buf)));
-  EXPECT_EQ(0, minijail_unmarshal(self->j, self->buf, self->size));
+TEST_F(marshal, empty)
+{
+	ASSERT_EQ(0, minijail_marshal(self->m, self->buf, sizeof(self->buf)));
+	EXPECT_EQ(0, minijail_unmarshal(self->j, self->buf, self->size));
 }
 
-TEST_F(marshal, 0xff) {
-  memset(self->buf, 0xff, sizeof(self->buf));
-  /* Should fail on the first consumestr since a NUL will never be found. */
-  EXPECT_EQ(-EINVAL, minijail_unmarshal(self->j, self->buf, sizeof(self->buf)));
+TEST_F(marshal, 0xff)
+{
+	memset(self->buf, 0xff, sizeof(self->buf));
+	/* Should fail on the first consumestr since a NUL will never be found.
+	 */
+	EXPECT_EQ(-EINVAL,
+		  minijail_unmarshal(self->j, self->buf, sizeof(self->buf)));
 }
 
-TEST(test_minijail_run_pid_pipes_no_preload) {
-  pid_t pid;
-  int child_stdin, child_stdout, child_stderr;
-  int mj_run_ret;
-  ssize_t write_ret, read_ret;
-  const size_t buf_len = 128;
-  char buf[buf_len];
-  int status;
+TEST(test_minijail_run_pid_pipes_no_preload)
+{
+	pid_t pid;
+	int child_stdin, child_stdout, child_stderr;
+	int mj_run_ret;
+	ssize_t write_ret, read_ret;
+	const size_t buf_len = 128;
+	char buf[buf_len];
+	int status;
 #if defined(__ANDROID__)
-  char filename[] = "/system/bin/cat";
+	char filename[] = "/system/bin/cat";
 #else
-  char filename[] = "/bin/cat";
+	char filename[] = "/bin/cat";
 #endif
-  char teststr[] = "test\n";
-  size_t teststr_len = strlen(teststr);
-  char *argv[4];
+	char teststr[] = "test\n";
+	size_t teststr_len = strlen(teststr);
+	char *argv[4];
 
-  struct minijail *j = minijail_new();
+	struct minijail *j = minijail_new();
 
-  argv[0] = filename;
-  argv[1] = NULL;
-  mj_run_ret = minijail_run_pid_pipes_no_preload(j, argv[0], argv,
-                                                 &pid,
-                                                 &child_stdin, &child_stdout,
-                                                 NULL);
-  EXPECT_EQ(mj_run_ret, 0);
+	argv[0] = filename;
+	argv[1] = NULL;
+	mj_run_ret = minijail_run_pid_pipes_no_preload(
+	    j, argv[0], argv, &pid, &child_stdin, &child_stdout, NULL);
+	EXPECT_EQ(mj_run_ret, 0);
 
-  write_ret = write(child_stdin, teststr, teststr_len);
-  EXPECT_EQ(write_ret, (int)teststr_len);
+	write_ret = write(child_stdin, teststr, teststr_len);
+	EXPECT_EQ(write_ret, (int)teststr_len);
 
-  read_ret = read(child_stdout, buf, 8);
-  EXPECT_EQ(read_ret, (int)teststr_len);
-  buf[teststr_len] = 0;
-  EXPECT_EQ(strcmp(buf, teststr), 0);
+	read_ret = read(child_stdout, buf, 8);
+	EXPECT_EQ(read_ret, (int)teststr_len);
+	buf[teststr_len] = 0;
+	EXPECT_EQ(strcmp(buf, teststr), 0);
 
-  EXPECT_EQ(kill(pid, SIGTERM), 0);
-  waitpid(pid, &status, 0);
-  ASSERT_TRUE(WIFSIGNALED(status));
-  EXPECT_EQ(WTERMSIG(status), SIGTERM);
+	EXPECT_EQ(kill(pid, SIGTERM), 0);
+	waitpid(pid, &status, 0);
+	ASSERT_TRUE(WIFSIGNALED(status));
+	EXPECT_EQ(WTERMSIG(status), SIGTERM);
 
-  argv[0] = (char *)kShellPath;
-  argv[1] = "-c";
-  argv[2] = "echo test >&2";
-  argv[3] = NULL;
-  mj_run_ret = minijail_run_pid_pipes_no_preload(j, argv[0], argv, &pid,
-                                                 &child_stdin, &child_stdout,
-                                                 &child_stderr);
-  EXPECT_EQ(mj_run_ret, 0);
+	argv[0] = (char *)kShellPath;
+	argv[1] = "-c";
+	argv[2] = "echo test >&2";
+	argv[3] = NULL;
+	mj_run_ret = minijail_run_pid_pipes_no_preload(
+	    j, argv[0], argv, &pid, &child_stdin, &child_stdout, &child_stderr);
+	EXPECT_EQ(mj_run_ret, 0);
 
-  read_ret = read(child_stderr, buf, buf_len);
-  EXPECT_GE(read_ret, (int)teststr_len);
+	read_ret = read(child_stderr, buf, buf_len);
+	EXPECT_GE(read_ret, (int)teststr_len);
 
-  waitpid(pid, &status, 0);
-  ASSERT_TRUE(WIFEXITED(status));
-  EXPECT_EQ(WEXITSTATUS(status), 0);
+	waitpid(pid, &status, 0);
+	ASSERT_TRUE(WIFEXITED(status));
+	EXPECT_EQ(WEXITSTATUS(status), 0);
 
-  minijail_destroy(j);
+	minijail_destroy(j);
 }
 
-TEST(test_minijail_no_fd_leaks) {
-  pid_t pid;
-  int child_stdout;
-  int mj_run_ret;
-  ssize_t read_ret;
-  const size_t buf_len = 128;
-  char buf[buf_len];
-  char script[buf_len];
-  int status;
-  char *argv[4];
+TEST(test_minijail_no_fd_leaks)
+{
+	pid_t pid;
+	int child_stdout;
+	int mj_run_ret;
+	ssize_t read_ret;
+	const size_t buf_len = 128;
+	char buf[buf_len];
+	char script[buf_len];
+	int status;
+	char *argv[4];
 
-  int dev_null = open("/dev/null", O_RDONLY);
-  ASSERT_NE(dev_null, -1);
-  snprintf(script, sizeof(script),
-           "[ -e /proc/self/fd/%d ] && echo yes || echo no", dev_null);
+	int dev_null = open("/dev/null", O_RDONLY);
+	ASSERT_NE(dev_null, -1);
+	snprintf(script, sizeof(script),
+		 "[ -e /proc/self/fd/%d ] && echo yes || echo no", dev_null);
 
-  struct minijail *j = minijail_new();
+	struct minijail *j = minijail_new();
 
-  argv[0] = (char *)kShellPath;
-  argv[1] = "-c";
-  argv[2] = script;
-  argv[3] = NULL;
-  mj_run_ret = minijail_run_pid_pipes_no_preload(j, argv[0], argv, &pid,
-                                                 NULL, &child_stdout, NULL);
-  EXPECT_EQ(mj_run_ret, 0);
+	argv[0] = (char *)kShellPath;
+	argv[1] = "-c";
+	argv[2] = script;
+	argv[3] = NULL;
+	mj_run_ret = minijail_run_pid_pipes_no_preload(
+	    j, argv[0], argv, &pid, NULL, &child_stdout, NULL);
+	EXPECT_EQ(mj_run_ret, 0);
 
-  read_ret = read(child_stdout, buf, buf_len);
-  EXPECT_GE(read_ret, 0);
-  buf[read_ret] = '\0';
-  EXPECT_STREQ(buf, "yes\n");
+	read_ret = read(child_stdout, buf, buf_len);
+	EXPECT_GE(read_ret, 0);
+	buf[read_ret] = '\0';
+	EXPECT_STREQ(buf, "yes\n");
 
-  waitpid(pid, &status, 0);
-  ASSERT_TRUE(WIFEXITED(status));
-  EXPECT_EQ(WEXITSTATUS(status), 0);
+	waitpid(pid, &status, 0);
+	ASSERT_TRUE(WIFEXITED(status));
+	EXPECT_EQ(WEXITSTATUS(status), 0);
 
-  minijail_close_open_fds(j);
-  mj_run_ret = minijail_run_pid_pipes_no_preload(j, argv[0], argv, &pid,
-                                                 NULL, &child_stdout, NULL);
-  EXPECT_EQ(mj_run_ret, 0);
+	minijail_close_open_fds(j);
+	mj_run_ret = minijail_run_pid_pipes_no_preload(
+	    j, argv[0], argv, &pid, NULL, &child_stdout, NULL);
+	EXPECT_EQ(mj_run_ret, 0);
 
-  read_ret = read(child_stdout, buf, buf_len);
-  EXPECT_GE(read_ret, 0);
-  buf[read_ret] = '\0';
-  EXPECT_STREQ(buf, "no\n");
+	read_ret = read(child_stdout, buf, buf_len);
+	EXPECT_GE(read_ret, 0);
+	buf[read_ret] = '\0';
+	EXPECT_STREQ(buf, "no\n");
 
-  waitpid(pid, &status, 0);
-  ASSERT_TRUE(WIFEXITED(status));
-  EXPECT_EQ(WEXITSTATUS(status), 0);
+	waitpid(pid, &status, 0);
+	ASSERT_TRUE(WIFEXITED(status));
+	EXPECT_EQ(WEXITSTATUS(status), 0);
 
-  minijail_destroy(j);
+	minijail_destroy(j);
 
-  close(dev_null);
+	close(dev_null);
 }
 
 TEST_HARNESS_MAIN
diff --git a/minijail0.c b/minijail0.c
index 48e1d07..e15c884 100644
--- a/minijail0.c
+++ b/minijail0.c
@@ -109,7 +109,7 @@
 static void usage(const char *progn)
 {
 	size_t i;
-
+	/* clang-format off */
 	printf("Usage: %s [-GhHiIKlLnNprstUvY]\n"
 	       "  [-a <table>]\n"
 	       "  [-b <src>,<dest>[,<writeable>]] [-k <src>,<dest>,<type>[,<flags>][,<data>]]\n"
@@ -142,9 +142,11 @@
 	       "  -L:         Report blocked syscalls to syslog when using seccomp filter.\n"
 	       "              Forces the following syscalls to be allowed:\n"
 	       "                  ", progn);
+	/* clang-format on */
 	for (i = 0; i < log_syscalls_len; i++)
 		printf("%s ", log_syscalls[i]);
 
+	/* clang-format off */
 	printf("\n"
 	       "  -m:         Set the uid map of a user namespace (implies -pU).\n"
 	       "              Same arguments as newuidmap(1), multiple mappings should be separated by ',' (comma).\n"
@@ -168,6 +170,7 @@
 	       "  -v:         Enter new mount namespace.\n"
 	       "  -V <file>:  Enter specified mount namespace.\n"
 	       "  -Y:         Synchronize seccomp filters across thread group.\n");
+	/* clang-format on */
 }
 
 static void seccomp_filter_usage(const char *progn)