Build init as C++.

This is just the minimal change to keep it building.

Change-Id: I245c5b8413a1db114576c81462eb5737f5ffcef2
diff --git a/init/Android.mk b/init/Android.mk
index 606c199..01ce4da 100644
--- a/init/Android.mk
+++ b/init/Android.mk
@@ -4,36 +4,35 @@
 include $(CLEAR_VARS)
 
 LOCAL_SRC_FILES:= \
-	builtins.c \
-	init.c \
-	devices.c \
-	property_service.c \
-	util.c \
-	parser.c \
-	keychords.c \
-	signal_handler.c \
-	init_parser.c \
-	ueventd.c \
-	ueventd_parser.c \
-	watchdogd.c
+    builtins.cpp \
+    devices.cpp \
+    init.cpp \
+    init_parser.cpp \
+    keychords.cpp \
+    parser.cpp \
+    property_service.cpp \
+    signal_handler.cpp \
+    ueventd.cpp \
+    ueventd_parser.cpp \
+    util.cpp \
+    watchdogd.cpp \
 
-LOCAL_CFLAGS += \
-    -std=gnu11 \
+LOCAL_CPPFLAGS += \
     -Wall \
     -Werror -Wno-error=deprecated-declarations \
     -Wno-unused-parameter \
 
 ifeq ($(strip $(INIT_BOOTCHART)),true)
-LOCAL_SRC_FILES += bootchart.c
-LOCAL_CFLAGS    += -DBOOTCHART=1
+LOCAL_SRC_FILES += bootchart.cpp
+LOCAL_CPPFLAGS  += -DBOOTCHART=1
 endif
 
 ifneq (,$(filter userdebug eng,$(TARGET_BUILD_VARIANT)))
-LOCAL_CFLAGS += -DALLOW_LOCAL_PROP_OVERRIDE=1 -DALLOW_DISABLE_SELINUX=1
+LOCAL_CPPFLAGS += -DALLOW_LOCAL_PROP_OVERRIDE=1 -DALLOW_DISABLE_SELINUX=1
 endif
 
 # Enable ueventd logging
-#LOCAL_CFLAGS += -DLOG_UEVENTS=1
+#LOCAL_CPPFLAGS += -DLOG_UEVENTS=1
 
 LOCAL_MODULE:= init
 
diff --git a/init/bootchart.c b/init/bootchart.cpp
similarity index 99%
rename from init/bootchart.c
rename to init/bootchart.cpp
index 27c7f65..44a2437 100644
--- a/init/bootchart.c
+++ b/init/bootchart.cpp
@@ -31,6 +31,7 @@
 #include <fcntl.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <string.h>
 #include <sys/stat.h>
 #include "bootchart.h"
 
diff --git a/init/builtins.c b/init/builtins.cpp
similarity index 98%
rename from init/builtins.c
rename to init/builtins.cpp
index 2521daf..6c4d9c4 100644
--- a/init/builtins.c
+++ b/init/builtins.cpp
@@ -53,7 +53,8 @@
 
 int add_environment(const char *name, const char *value);
 
-extern int init_module(void *, unsigned long, const char *);
+// System call provided by bionic but not in any header file.
+extern "C" int init_module(void *, unsigned long, const char *);
 
 static int write_file(const char *path, const char *value)
 {
@@ -674,7 +675,7 @@
     int res;
     int len = 0;
     int cmd = 0;
-    char *reboot_target;
+    const char *reboot_target;
 
     res = expand_props(command, args[1], sizeof(command));
     if (res) {
@@ -776,7 +777,7 @@
     if ((fd2 = open(args[2], O_WRONLY|O_CREAT|O_TRUNC|O_CLOEXEC, 0660)) < 0)
         goto out_err;
 
-    if (!(buffer = malloc(info.st_size)))
+    if (!(buffer = (char*) malloc(info.st_size)))
         goto out_err;
 
     p = buffer;
diff --git a/init/devices.c b/init/devices.cpp
similarity index 98%
rename from init/devices.c
rename to init/devices.cpp
index 0de92f5..28f2ec0 100644
--- a/init/devices.c
+++ b/init/devices.cpp
@@ -101,7 +101,7 @@
                   mode_t perm, unsigned int uid, unsigned int gid,
                   unsigned short prefix,
                   unsigned short wildcard) {
-    struct perm_node *node = calloc(1, sizeof(*node));
+    struct perm_node *node = (perm_node*) calloc(1, sizeof(*node));
     if (!node)
         return -ENOMEM;
 
@@ -289,7 +289,7 @@
 
     INFO("adding platform device %s (%s)\n", name, path);
 
-    bus = calloc(1, sizeof(struct platform_node));
+    bus = (platform_node*) calloc(1, sizeof(struct platform_node));
     bus->path = strdup(path);
     bus->path_len = path_len;
     bus->name = bus->path + (name - path);
@@ -450,7 +450,7 @@
     if (!pdev)
         return NULL;
 
-    links = malloc(sizeof(char *) * 2);
+    links = (char**) malloc(sizeof(char *) * 2);
     if (!links)
         return NULL;
     memset(links, 0, sizeof(char *) * 2);
@@ -512,7 +512,7 @@
         return NULL;
     }
 
-    char **links = malloc(sizeof(char *) * 4);
+    char **links = (char**) malloc(sizeof(char *) * 4);
     if (!links)
         return NULL;
     memset(links, 0, sizeof(char *) * 4);
@@ -668,7 +668,7 @@
 
 static void handle_generic_device_event(struct uevent *uevent)
 {
-    char *base;
+    const char *base;
     const char *name;
     char devpath[DEVPATH_LEN] = {0};
     char **links = NULL;
diff --git a/init/devices.h b/init/devices.h
index 5d0fe88..6cb0a77 100644
--- a/init/devices.h
+++ b/init/devices.h
@@ -26,4 +26,5 @@
                          unsigned int gid, unsigned short prefix,
                          unsigned short wildcard);
 int get_device_fd();
+
 #endif	/* _INIT_DEVICES_H */
diff --git a/init/init.c b/init/init.cpp
similarity index 99%
rename from init/init.c
rename to init/init.cpp
index 8bb6da0..06a7326 100644
--- a/init/init.c
+++ b/init/init.cpp
@@ -113,9 +113,8 @@
 
         /* Add entry if a free slot is available */
         if (ENV[n] == NULL) {
-            size_t len = key_len + strlen(val) + 2;
-            char *entry = malloc(len);
-            snprintf(entry, len, "%s=%s", key, val);
+            char* entry;
+            asprintf(&entry, "%s=%s", key, val);
             ENV[n] = entry;
             return 0;
         }
diff --git a/init/init.h b/init/init.h
index 654a80b..eedec27 100644
--- a/init/init.h
+++ b/init/init.h
@@ -18,6 +18,7 @@
 #define _INIT_INIT_H
 
 #include <cutils/list.h>
+#include <cutils/iosched_policy.h>
 
 #include <sys/stat.h>
 
@@ -122,7 +123,7 @@
     int nkeycodes;
     int keychord_id;
 
-    int ioprio_class;
+    IoSchedClass ioprio_class;
     int ioprio_pri;
 
     int nargs;
diff --git a/init/init_parser.c b/init/init_parser.cpp
similarity index 95%
rename from init/init_parser.c
rename to init/init_parser.cpp
index 02cbf3d..c8f2ea7 100644
--- a/init/init_parser.c
+++ b/init/init_parser.cpp
@@ -290,8 +290,7 @@
 
 static void parse_import(struct parse_state *state, int nargs, char **args)
 {
-    struct listnode *import_list = state->priv;
-    struct import *import;
+    struct listnode *import_list = (listnode*) state->priv;
     char conf_file[PATH_MAX];
     int ret;
 
@@ -307,7 +306,7 @@
         return;
     }
 
-    import = calloc(1, sizeof(struct import));
+    struct import* import = (struct import*) calloc(1, sizeof(struct import));
     import->filename = strdup(conf_file);
     list_add_tail(import_list, &import->list);
     INFO("found import '%s', adding to import list", import->filename);
@@ -575,23 +574,19 @@
     queue_property_triggers(NULL, NULL);
 }
 
-void queue_builtin_action(int (*func)(int nargs, char **args), char *name)
+void queue_builtin_action(int (*func)(int nargs, char **args), const char *name)
 {
-    struct action *act;
-    struct command *cmd;
-    struct trigger *cur_trigger;
-
-    act = calloc(1, sizeof(*act));
-    cur_trigger = calloc(1, sizeof(*cur_trigger));
+    action* act = (action*) calloc(1, sizeof(*act));
+    trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
     cur_trigger->name = name;
     list_init(&act->triggers);
     list_add_tail(&act->triggers, &cur_trigger->nlist);
     list_init(&act->commands);
     list_init(&act->qlist);
 
-    cmd = calloc(1, sizeof(*cmd));
+    command* cmd = (command*) calloc(1, sizeof(*cmd));
     cmd->func = func;
-    cmd->args[0] = name;
+    cmd->args[0] = const_cast<char*>(name);
     cmd->nargs = 1;
     list_add_tail(&act->commands, &cmd->clist);
 
@@ -626,8 +621,6 @@
 
 static void *parse_service(struct parse_state *state, int nargs, char **args)
 {
-    struct service *svc;
-    struct trigger *cur_trigger;
     if (nargs < 3) {
         parse_error(state, "services must have a name and a program\n");
         return 0;
@@ -637,14 +630,14 @@
         return 0;
     }
 
-    svc = service_find_by_name(args[1]);
+    service* svc = (service*) service_find_by_name(args[1]);
     if (svc) {
         parse_error(state, "ignored duplicate definition of service '%s'\n", args[1]);
         return 0;
     }
 
     nargs -= 2;
-    svc = calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
+    svc = (service*) calloc(1, sizeof(*svc) + sizeof(char*) * nargs);
     if (!svc) {
         parse_error(state, "out of memory\n");
         return 0;
@@ -652,7 +645,7 @@
     svc->name = args[1];
     svc->classname = "default";
     memcpy(svc->args, args + 2, sizeof(char*) * nargs);
-    cur_trigger = calloc(1, sizeof(*cur_trigger));
+    trigger* cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
     svc->args[nargs] = 0;
     svc->nargs = nargs;
     list_init(&svc->onrestart.triggers);
@@ -665,7 +658,7 @@
 
 static void parse_line_service(struct parse_state *state, int nargs, char **args)
 {
-    struct service *svc = state->context;
+    struct service *svc = (service*) state->context;
     struct command *cmd;
     int i, kw, kw_nargs;
 
@@ -734,7 +727,7 @@
         if (nargs < 2) {
             parse_error(state, "keycodes option requires atleast one keycode\n");
         } else {
-            svc->keycodes = malloc((nargs - 1) * sizeof(svc->keycodes[0]));
+            svc->keycodes = (int*) malloc((nargs - 1) * sizeof(svc->keycodes[0]));
             if (!svc->keycodes) {
                 parse_error(state, "could not allocate keycodes\n");
             } else {
@@ -763,7 +756,7 @@
             break;
         }
 
-        cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
+        cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
         cmd->func = kw_func(kw);
         cmd->nargs = nargs;
         memcpy(cmd->args, args, sizeof(char*) * nargs);
@@ -773,12 +766,11 @@
         svc->flags |= SVC_CRITICAL;
         break;
     case K_setenv: { /* name value */
-        struct svcenvinfo *ei;
         if (nargs < 3) {
             parse_error(state, "setenv option requires name and value arguments\n");
             break;
         }
-        ei = calloc(1, sizeof(*ei));
+        svcenvinfo* ei = (svcenvinfo*) calloc(1, sizeof(*ei));
         if (!ei) {
             parse_error(state, "out of memory\n");
             break;
@@ -790,7 +782,6 @@
         break;
     }
     case K_socket: {/* name type perm [ uid gid context ] */
-        struct socketinfo *si;
         if (nargs < 4) {
             parse_error(state, "socket option requires name, type, perm arguments\n");
             break;
@@ -800,7 +791,7 @@
             parse_error(state, "socket type must be 'dgram', 'stream' or 'seqpacket'\n");
             break;
         }
-        si = calloc(1, sizeof(*si));
+        socketinfo* si = (socketinfo*) calloc(1, sizeof(*si));
         if (!si) {
             parse_error(state, "out of memory\n");
             break;
@@ -840,7 +831,6 @@
 
 static void *parse_action(struct parse_state *state, int nargs, char **args)
 {
-    struct action *act;
     struct trigger *cur_trigger;
     int i;
     if (nargs < 2) {
@@ -848,7 +838,7 @@
         return 0;
     }
 
-    act = calloc(1, sizeof(*act));
+    action* act = (action*) calloc(1, sizeof(*act));
     list_init(&act->triggers);
 
     for (i = 1; i < nargs; i++) {
@@ -859,7 +849,7 @@
             } else
                 continue;
         }
-        cur_trigger = calloc(1, sizeof(*cur_trigger));
+        cur_trigger = (trigger*) calloc(1, sizeof(*cur_trigger));
         cur_trigger->name = args[i];
         list_add_tail(&act->triggers, &cur_trigger->nlist);
     }
@@ -873,8 +863,7 @@
 
 static void parse_line_action(struct parse_state* state, int nargs, char **args)
 {
-    struct command *cmd;
-    struct action *act = state->context;
+    struct action *act = (action*) state->context;
     int kw, n;
 
     if (nargs == 0) {
@@ -893,7 +882,7 @@
             n > 2 ? "arguments" : "argument");
         return;
     }
-    cmd = malloc(sizeof(*cmd) + sizeof(char*) * nargs);
+    command* cmd = (command*) malloc(sizeof(*cmd) + sizeof(char*) * nargs);
     cmd->func = kw_func(kw);
     cmd->line = state->line;
     cmd->filename = state->filename;
diff --git a/init/init_parser.h b/init/init_parser.h
index b078cad..0047da7 100644
--- a/init/init_parser.h
+++ b/init/init_parser.h
@@ -28,7 +28,7 @@
 int action_queue_empty(void);
 void queue_property_triggers(const char *name, const char *value);
 void queue_all_property_triggers();
-void queue_builtin_action(int (*func)(int nargs, char **args), char *name);
+void queue_builtin_action(int (*func)(int nargs, char **args), const char *name);
 
 int init_parse_config_file(const char *fn);
 int expand_props(char *dst, const char *src, int len);
diff --git a/init/keychords.c b/init/keychords.cpp
similarity index 96%
rename from init/keychords.c
rename to init/keychords.cpp
index 5a9e45f..d6464bd 100644
--- a/init/keychords.c
+++ b/init/keychords.cpp
@@ -40,7 +40,7 @@
     if (svc->keycodes) {
         /* add a new keychord to the list */
         size = sizeof(*keychord) + svc->nkeycodes * sizeof(keychord->keycodes[0]);
-        keychords = realloc(keychords, keychords_length + size);
+        keychords = (input_keychord*) realloc(keychords, keychords_length + size);
         if (!keychords) {
             ERROR("could not allocate keychords\n");
             keychords_length = 0;
diff --git a/init/keywords.h b/init/keywords.h
index 7473586..0805cdd 100644
--- a/init/keywords.h
+++ b/init/keywords.h
@@ -1,4 +1,3 @@
-
 #ifndef KEYWORD
 int do_chroot(int nargs, char **args);
 int do_chdir(int nargs, char **args);
@@ -112,4 +111,3 @@
 #undef __MAKE_KEYWORD_ENUM__
 #undef KEYWORD
 #endif
-
diff --git a/init/parser.c b/init/parser.cpp
similarity index 100%
rename from init/parser.c
rename to init/parser.cpp
diff --git a/init/property_service.c b/init/property_service.cpp
similarity index 98%
rename from init/property_service.c
rename to init/property_service.cpp
index 7dd9ef9..4f0a46c 100644
--- a/init/property_service.c
+++ b/init/property_service.cpp
@@ -97,8 +97,6 @@
         return 1;
 
     char *tctx = NULL;
-    const char *class = "property_service";
-    const char *perm = "set";
     int result = 0;
 
     if (!sctx)
@@ -110,7 +108,7 @@
     if (selabel_lookup(sehandle_prop, &tctx, name, 1) != 0)
         goto err;
 
-    if (selinux_check_access(sctx, tctx, class, perm, (void*) name) == 0)
+    if (selinux_check_access(sctx, tctx, "property_service", "set", (void*) name) == 0)
         result = 1;
 
     freecon(tctx);
diff --git a/init/signal_handler.c b/init/signal_handler.cpp
similarity index 100%
rename from init/signal_handler.c
rename to init/signal_handler.cpp
diff --git a/init/ueventd.c b/init/ueventd.cpp
similarity index 100%
rename from init/ueventd.c
rename to init/ueventd.cpp
diff --git a/init/ueventd.h b/init/ueventd.h
index 0a454c5..d12d7fe 100644
--- a/init/ueventd.h
+++ b/init/ueventd.h
@@ -20,16 +20,18 @@
 #include <cutils/list.h>
 #include <sys/types.h>
 
+enum devname_src_t {
+    DEVNAME_UNKNOWN = 0,
+    DEVNAME_UEVENT_DEVNAME,
+    DEVNAME_UEVENT_DEVPATH,
+};
+
 struct ueventd_subsystem {
     struct listnode slist;
 
     const char *name;
-    enum {
-        DEVNAME_UNKNOWN = 0,
-        DEVNAME_UEVENT_DEVNAME,
-        DEVNAME_UEVENT_DEVPATH,
-    } devname_src;
     const char *dirname;
+    devname_src_t devname_src;
 };
 
 int ueventd_main(int argc, char **argv);
diff --git a/init/ueventd_parser.c b/init/ueventd_parser.cpp
similarity index 96%
rename from init/ueventd_parser.c
rename to init/ueventd_parser.cpp
index e447006..f54a90b 100644
--- a/init/ueventd_parser.c
+++ b/init/ueventd_parser.cpp
@@ -100,21 +100,19 @@
 static void *parse_subsystem(struct parse_state *state,
         int nargs __attribute__((unused)), char **args)
 {
-    struct ueventd_subsystem *s;
-
     if (!valid_name(args[1])) {
         parse_error(state, "invalid subsystem name '%s'\n", args[1]);
         return 0;
     }
 
-    s = ueventd_subsystem_find_by_name(args[1]);
+    ueventd_subsystem* s = ueventd_subsystem_find_by_name(args[1]);
     if (s) {
         parse_error(state, "ignored duplicate definition of subsystem '%s'\n",
                 args[1]);
         return 0;
     }
 
-    s = calloc(1, sizeof(*s));
+    s = (ueventd_subsystem*) calloc(1, sizeof(*s));
     if (!s) {
         parse_error(state, "out of memory\n");
         return 0;
@@ -128,7 +126,7 @@
 static void parse_line_subsystem(struct parse_state *state, int nargs,
         char **args)
 {
-    struct ueventd_subsystem *s = state->context;
+    struct ueventd_subsystem *s = (ueventd_subsystem*) state->context;
     int kw;
 
     if (nargs == 0) {
diff --git a/init/util.c b/init/util.cpp
similarity index 87%
rename from init/util.c
rename to init/util.cpp
index 8f27ee9..c484168 100644
--- a/init/util.c
+++ b/init/util.cpp
@@ -147,9 +147,9 @@
 }
 
 /* reads a file, making sure it is terminated with \n \0 */
-void *read_file(const char *fn, unsigned *_sz)
+char *read_file(const char *fn, unsigned *_sz)
 {
-    char *data;
+    char *data = NULL;
     int sz;
     int fd;
     struct stat sb;
@@ -186,7 +186,7 @@
 
 oops:
     close(fd);
-    if(data != 0) free(data);
+    free(data);
     return 0;
 }
 
@@ -404,72 +404,40 @@
     exit(1);
 }
 
-void get_hardware_name(char *hardware, unsigned int *revision)
-{
-    const char *cpuinfo = "/proc/cpuinfo";
-    char *data = NULL;
-    size_t len = 0, limit = 1024;
-    int fd, n;
-    char *x, *hw, *rev;
+void get_hardware_name(char *hardware, unsigned int *revision) {
+  // Hardware string was provided on kernel command line.
+  if (hardware[0]) {
+    return;
+  }
 
-    /* Hardware string was provided on kernel command line */
-    if (hardware[0])
-        return;
-
-    fd = open(cpuinfo, O_RDONLY | O_CLOEXEC);
-    if (fd < 0) return;
-
-    for (;;) {
-        x = realloc(data, limit);
-        if (!x) {
-            ERROR("Failed to allocate memory to read %s\n", cpuinfo);
-            goto done;
+  FILE* fp = fopen("/proc/cpuinfo", "re");
+  if (fp == NULL) {
+    return;
+  }
+  char buf[1024];
+  while (fgets(buf, sizeof(buf), fp) != NULL) {
+    if (strncmp(buf, "Hardware", 8) == 0) {
+      const char* hw = strstr(buf, ": ");
+      if (hw) {
+        hw += 2;
+        size_t n = 0;
+        while (*hw) {
+          if (!isspace(*hw)) {
+            hardware[n++] = tolower(*hw);
+          }
+          hw++;
+          if (n == 31) break;
         }
-        data = x;
-
-        n = read(fd, data + len, limit - len);
-        if (n < 0) {
-            ERROR("Failed reading %s: %s (%d)\n", cpuinfo, strerror(errno), errno);
-            goto done;
-        }
-        len += n;
-
-        if (len < limit)
-            break;
-
-        /* We filled the buffer, so increase size and loop to read more */
-        limit *= 2;
+        hardware[n] = 0;
+      }
+    } else if (strncmp(buf, "Revision", 8) == 0) {
+      const char* rev = strstr(buf, ": ");
+      if (rev) {
+        *revision = strtoul(rev + 2, 0, 16);
+      }
     }
-
-    data[len] = 0;
-    hw = strstr(data, "\nHardware");
-    rev = strstr(data, "\nRevision");
-
-    if (hw) {
-        x = strstr(hw, ": ");
-        if (x) {
-            x += 2;
-            n = 0;
-            while (*x && *x != '\n') {
-                if (!isspace(*x))
-                    hardware[n++] = tolower(*x);
-                x++;
-                if (n == 31) break;
-            }
-            hardware[n] = 0;
-        }
-    }
-
-    if (rev) {
-        x = strstr(rev, ": ");
-        if (x) {
-            *revision = strtoul(x + 2, 0, 16);
-        }
-    }
-
-done:
-    close(fd);
-    free(data);
+  }
+  fclose(fp);
 }
 
 void import_kernel_cmdline(int in_qemu,
diff --git a/init/util.h b/init/util.h
index 4cfe99d..1f9ecbe 100644
--- a/init/util.h
+++ b/init/util.h
@@ -27,7 +27,7 @@
 int mtd_name_to_number(const char *name);
 int create_socket(const char *name, int type, mode_t perm,
                   uid_t uid, gid_t gid, const char *socketcon);
-void *read_file(const char *fn, unsigned *_sz);
+char *read_file(const char *fn, unsigned *_sz);
 time_t gettime(void);
 unsigned int decode_uid(const char *s);
 
diff --git a/init/watchdogd.c b/init/watchdogd.cpp
similarity index 100%
rename from init/watchdogd.c
rename to init/watchdogd.cpp