Add short libminijail example.

Now that the filesystem on the emulators can be remounted RW,
having an executable that can be compiled and pushed to the system
to test functionality is very convenient.

Bug: None
Change-Id: I72f64ffe137cf5b24c1c74204986817a5929825d
diff --git a/Android.mk b/Android.mk
index 2df6eab..e14910d 100644
--- a/Android.mk
+++ b/Android.mk
@@ -107,3 +107,16 @@
 LOCAL_STATIC_LIBRARIES := libminijail_generated
 LOCAL_SHARED_LIBRARIES := $(minijailCommonSharedLibraries)
 include $(BUILD_NATIVE_TEST)
+
+# libminijail usage example.
+# ========================================================
+include $(CLEAR_VARS)
+LOCAL_MODULE := drop_privs
+LOCAL_MODULE_TAGS := optional
+LOCAL_CFLAGS := $(minijailCommonCFlags)
+LOCAL_CLANG := true
+LOCAL_SRC_FILES := \
+	examples/drop_privs.cpp
+
+LOCAL_SHARED_LIBRARIES := libbase libminijail
+include $(BUILD_EXECUTABLE)
diff --git a/examples/drop_privs.cpp b/examples/drop_privs.cpp
new file mode 100644
index 0000000..bfe24e5
--- /dev/null
+++ b/examples/drop_privs.cpp
@@ -0,0 +1,69 @@
+// Copyright (C) 2015 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <sys/types.h>
+#include <sys/capability.h>
+#include <unistd.h>
+
+#include <libminijail.h>
+
+#include <base/logging.h>
+
+gid_t groups[] = { 1001, 1002 };
+
+void log_resugid() {
+    uid_t ruid, euid, suid;
+    gid_t rgid, egid, sgid;
+    getresuid(&ruid, &euid, &suid);
+    getresgid(&rgid, &egid, &sgid);
+
+    LOG(INFO) << "ruid " << ruid << " euid " << euid << " suid " << suid;
+    LOG(INFO) << "rgid " << rgid << " egid " << egid << " sgid " << sgid;
+
+    int nsupp_groups = getgroups(0, NULL);
+    if (nsupp_groups < 0) {
+        PLOG(FATAL) << "getgroups(0)";
+    }
+    if (nsupp_groups == 0) {
+        LOG(INFO) << "no supplemental groups";
+        return;
+    }
+
+    gid_t *list = (gid_t*)calloc((size_t)nsupp_groups, sizeof(gid_t));
+    nsupp_groups = getgroups(nsupp_groups, list);
+    if (nsupp_groups < 0) {
+        PLOG(FATAL) << "getgroups(nsupp_groups)";
+    }
+    for (size_t i = 0; i < (size_t)nsupp_groups; i++) {
+        LOG(INFO) << "supp gid " << i + 1 << " " << list[i];
+    }
+}
+
+int main(void) {
+    log_resugid();
+    minijail *j = minijail_new();
+    minijail_change_user(j, "system");
+    minijail_change_group(j, "system");
+    minijail_set_supplementary_gids(j, sizeof(groups) / sizeof(groups[0]), groups);
+    // minijail_use_caps(j, CAP_TO_MASK(CAP_SETUID) | CAP_TO_MASK(CAP_SETGID));
+    minijail_enter(j);
+    log_resugid();
+    minijail_destroy(j);
+    // minijail *j2 = minijail_new();
+    // minijail_change_uid(j2, 5000);
+    // minijail_change_gid(j2, 5000);
+    // minijail_enter(j2);
+    // log_resugid();
+    return 0;
+}