Build init as C++.

This is just the minimal change to keep it building.

Change-Id: I245c5b8413a1db114576c81462eb5737f5ffcef2
This commit is contained in:
Elliott Hughes 2015-02-03 17:12:07 -08:00
parent 5204b1580e
commit f3cf438714
20 changed files with 99 additions and 144 deletions

View File

@ -4,36 +4,35 @@ LOCAL_PATH:= $(call my-dir)
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

View File

@ -31,6 +31,7 @@
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "bootchart.h"

View File

@ -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 do_powerctl(int nargs, char **args)
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 @@ int do_copy(int nargs, char **args)
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;

View File

@ -101,7 +101,7 @@ int add_dev_perms(const char *name, const char *attr,
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 @@ static void add_platform_device(const char *path)
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 @@ static char **get_character_device_symlinks(struct uevent *uevent)
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 @@ static char **get_block_device_symlinks(struct uevent *uevent)
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 inline void __attribute__((__deprecated__)) kernel_logger()
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;

View File

@ -26,4 +26,5 @@ extern int add_dev_perms(const char *name, const char *attr,
unsigned int gid, unsigned short prefix,
unsigned short wildcard);
int get_device_fd();
#endif /* _INIT_DEVICES_H */

View File

@ -113,9 +113,8 @@ int add_environment(const char *key, const char *val)
/* 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;
}

View File

@ -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 @@ struct service {
int nkeycodes;
int keychord_id;
int ioprio_class;
IoSchedClass ioprio_class;
int ioprio_pri;
int nargs;

View File

@ -290,8 +290,7 @@ err:
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 @@ static void parse_import(struct parse_state *state, int nargs, char **args)
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 @@ void queue_all_property_triggers()
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 @@ int action_queue_empty()
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 @@ static void *parse_service(struct parse_state *state, int nargs, char **args)
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 @@ static void *parse_service(struct parse_state *state, int nargs, char **args)
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_service(struct parse_state *state, int nargs, char **args)
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 @@ static void parse_line_service(struct parse_state *state, int nargs, char **args
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 @@ static void parse_line_service(struct parse_state *state, int nargs, char **args
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 @@ static void parse_line_service(struct parse_state *state, int nargs, char **args
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 @@ static void parse_line_service(struct parse_state *state, int nargs, char **args
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 @@ static void parse_line_service(struct parse_state *state, int nargs, char **args
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_line_service(struct parse_state *state, int nargs, char **args
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 @@ static void *parse_action(struct parse_state *state, int nargs, char **args)
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 @@ static void *parse_action(struct parse_state *state, int nargs, char **args)
} 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_action(struct parse_state *state, int nargs, char **args)
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 @@ static void parse_line_action(struct parse_state* state, int nargs, char **args)
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;

View File

@ -28,7 +28,7 @@ void action_for_each_trigger(const char *trigger,
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);

View File

@ -40,7 +40,7 @@ void add_service_keycodes(struct service *svc)
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;

View File

@ -1,4 +1,3 @@
#ifndef KEYWORD
int do_chroot(int nargs, char **args);
int do_chdir(int nargs, char **args);
@ -112,4 +111,3 @@ enum {
#undef __MAKE_KEYWORD_ENUM__
#undef KEYWORD
#endif

View File

@ -97,8 +97,6 @@ static int check_mac_perms(const char *name, char *sctx)
return 1;
char *tctx = NULL;
const char *class = "property_service";
const char *perm = "set";
int result = 0;
if (!sctx)
@ -110,7 +108,7 @@ static int check_mac_perms(const char *name, char *sctx)
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);

View File

@ -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);

View File

@ -100,21 +100,19 @@ struct ueventd_subsystem *ueventd_subsystem_find_by_name(const char *name)
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_subsystem(struct parse_state *state,
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) {

View File

@ -147,9 +147,9 @@ out_close:
}
/* 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 @@ void *read_file(const char *fn, unsigned *_sz)
oops:
close(fd);
if(data != 0) free(data);
free(data);
return 0;
}
@ -404,72 +404,40 @@ void open_devnull_stdio(void)
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,

View File

@ -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);