diff --git a/services/BUILD.gn b/services/BUILD.gn index 567c24b768f59f8a1373ed7a2a49d5b04e8d71c0..cb4467b7b1bff7d80e9b54559b1b65b113e6342a 100644 --- a/services/BUILD.gn +++ b/services/BUILD.gn @@ -19,6 +19,7 @@ if (defined(ohos_lite)) { "OHOS_LITE", ] sources = [ + "log/init_log.c", "src/init_adapter.c", "src/init_capability.c", "src/init_cmds.c", @@ -31,7 +32,6 @@ if (defined(ohos_lite)) { "src/init_service_socket.c", "src/init_signal_handler.c", "src/init_utils.c", - "log/init_log.c", "src/main.c", ] @@ -48,11 +48,11 @@ if (defined(ohos_lite)) { cflags = [ "-Wall" ] deps = [ + "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", "//base/startup/init_lite/initsync:initsync", "//base/startup/syspara_lite/frameworks/parameter:parameter", "//build/lite/config/component/cJSON:cjson_shared", "//third_party/bounds_checking_function:libsec_shared", - "//base/hiviewdfx/hilog_lite/frameworks/featured:hilog_shared", ] ldflags = [] if (ohos_kernel_type == "liteos_a") { @@ -98,6 +98,11 @@ if (defined(ohos_lite)) { "//base/startup/init_lite/services/log:init_log", "//third_party/bounds_checking_function:libsec_static", ] + module_install_dir = "bin" + install_images = [ + "system", + "updater", + ] install_enable = true part_name = "init" } @@ -139,6 +144,10 @@ if (defined(ohos_lite)) { "//third_party/toybox:toybox", ] } + install_images = [ + "system", + "updater", + ] install_enable = true part_name = "init" } @@ -149,12 +158,12 @@ if (defined(ohos_lite)) { ":init_etc", ":updaterueventd", "//base/startup/init_lite/interfaces/innerkits/socket:libsocket", + "//base/startup/init_lite/services/cmds/reboot:reboot", + "//base/startup/init_lite/services/cmds/service_control:service_control", "//base/startup/init_lite/services/param:getparam", "//base/startup/init_lite/services/param:paramclient", "//base/startup/init_lite/services/param:paramservice", "//base/startup/init_lite/services/param:setparam", - "//base/startup/init_lite/services/cmds/reboot:reboot", - "//base/startup/init_lite/services/cmds/service_control:service_control", ] } @@ -202,13 +211,13 @@ if (defined(ohos_lite)) { group("init_etc") { deps = [ + ":group", ":init.Hi3516DV300.cfg", ":init.Hi3516DV300.usb.cfg", ":init.cfg", ":init.usb.cfg", ":init.usb.configfs.cfg", ":passwd", - ":group", ":ohos.para", ] } diff --git a/services/etc/group b/services/etc/group index 47bc75cf076aee5a0fccaa2fc38ba263b8f8ce57..59e29faa5c19c4f1e0f7f39ceb767928de5f9d41 100755 --- a/services/etc/group +++ b/services/etc/group @@ -1,115 +1,31 @@ -root:x:0 -daemon:x:1 -bin:x:2 -system:x:1000 -radio:x:1001 -bluetooth:x:1002 -graphics:x:1003 -input:x:1004 -audio:x:1005 -camera:x:1006 -log:x:1007 -compass:x:1008 -mount:x:1009 -wifi:x:1010 -adb:x:1011 -install:x:1012 -media:x:1013 -dhcp:x:1014 -sdcard_rw:x:1015 -vpn:x:1016 -keystore:x:1017 -usb:x:1018 -drm:x:1019 -mdnsr:x:1020 -gps:x:1021 -unused1:x:1022 -media_rw:x:1023 -mtp:x:1024 -unused2:x:1025 -drmrpc:x:1026 -nfc:x:1027 -sdcard_r:x:1028 -clat:x:1029 -loop_radio:x:1030 -media_drm:x:1031 -package_info:x:1032 -sdcard_pics:x:1033 -sdcard_av:x:1034 -sdcard_all:x:1035 -logd:x:1036 -shared_relro:x:1037 -dbus:x:1038 -tlsdate:x:1039 -media_ex:x:1040 -audioserver:x:1041 -metrics_coll:x:1042 -metricsd:x:1043 -webserv:x:1044 -debuggerd:x:1045 -media_codec:x:1046 -cameraserver:x:1047 -firewall:x:1048 -trunks:x:1049 -nvram:x:1050 -dns:x:1051 -dns_tether:x:1052 -webview_zygote:x:1053 -vehicle_network:x:1054 -media_audio:x:1055 -media_video:x:1056 -media_image:x:1057 -tombstoned:x:1058 -media_obb:x:1059 -ese:x:1060 -ota_update:x:1061 -automotive_evs:x:1062 -lowpan:x:1063 -hsm:x:1064 -reserved_disk:x:1065 -statsd:x:1066 -incidentd:x:1067 -secure_element:x:1068 -lmkd:x:1069 -llkd:x:1070 -iorapd:x:1071 -gpu_service:x:1072 -network_stack:x:1073 -gsid:x:1074 -shell:x:2000 -cache:x:2001 -diag:x:2002 -oem_reserved_start:x:2900 -oem_reserved_end:x:2999 -net_bt_admin:x:3001 -net_bt:x:3002 -inet:x:3003 -net_raw:x:3004 -net_admin:x:3005 -net_bw_stats:x:3006 -net_bw_acct:x:3007 -readproc:x:3009 -wakelock:x:3010 -uhid:x:3011 -oem_reserved_2_start:x:5000 -oem_reserved_2_end:x:5999 -everybody:x:9997 -misc:x:9998 -nobody:x:9999 -app:x:10000 -app_start:x:10000 -app_end:x:19999 -cache_gid_start:x:20000 -cache_gid_end:x:29999 -ext_gid_start:x:30000 -ext_gid_end:x:39999 -ext_cache_gid_start:x:40000 -ext_cache_gid_end:x:49999 -shared_gid_start:x:50000 -shared_gid_end:x:59999 -overflowuid:x:65534 -isolated_start:x:90000 -isolated_end:x:99999 -user:x:100000 -user_offset:x:100000 - +root:x:0: +bin:x:2: +system:x:1000: +radio:x:1001: +bluetooth:x:1002: +graphics:x:1003: +log:x:1007: +wifi:x:1010: +media:x:1013: +dhcp:x:1014: +vpn:x:1016: +keystore:x:1017: +usb:x:1018: +drm:x:1019: +media_rw:x:1023: +nfc:x:1027: +sdcard_r:x:1028: +package_info:x:1032: +logd:x:1036: +shared_relro:x:1037: +audioserver:x:1041: +cameraserver:x:1047: +shell:x:2000: +cache:x:2001: +net_bw_stats:x:3006: +net_bw_acct:x:3007: +readproc:x:3009: +wakelock:x:3010: +uhid:x:3011: +misc:x:9998: +app:x:10000: diff --git a/services/etc/init.cfg b/services/etc/init.cfg index 96afbbc23f67c922ecf2c3d24a705cdc8c6190c9..7774d27dcbc1f5cbad2024c05d63a4a2f1dfee86 100755 --- a/services/etc/init.cfg +++ b/services/etc/init.cfg @@ -178,7 +178,10 @@ "chmod 0444 /dev/cpu_variant:${ro.bionic.2nd_arch}", "chown system system /sys/power/state", "chown system system /sys/power/wakeup_count", - "chmod 0660 /sys/power/state" + "chmod 0660 /sys/power/state", + "ifup lo", + "hostname localhost", + "domainname localdomain" ] }, { "name" : "param:sys.boot_from_charger_mode=1", diff --git a/services/etc/passwd b/services/etc/passwd index f84c1e21ebc2e81ba435539ab3f379701c91d680..8c53db54da9794af591cb9265f68debb20681533 100755 --- a/services/etc/passwd +++ b/services/etc/passwd @@ -1,114 +1,31 @@ root:x:0:0:::/bin/false -daemon:x:1:1:::/bin/false bin:x:2:2:::/bin/false system:x:1000:1000:::/bin/false radio:x:1001:1001:::/bin/false bluetooth:x:1002:1002:::/bin/false graphics:x:1003:1003:::/bin/false -input:x:1004:1004:::/bin/false -audio:x:1005:1005:::/bin/false -camera:x:1006:1006:::/bin/false log:x:1007:1007:::/bin/false -compass:x:1008:1008:::/bin/false -mount:x:1009:1009:::/bin/false wifi:x:1010:1010:::/bin/false -adb:x:1011:1011:::/bin/false -install:x:1012:1012:::/bin/false media:x:1013:1013:::/bin/false dhcp:x:1014:1014:::/bin/false -sdcard_rw:x:1015:1015:::/bin/false vpn:x:1016:1016:::/bin/false keystore:x:1017:1017:::/bin/false usb:x:1018:1018:::/bin/false drm:x:1019:1019:::/bin/false -mdnsr:x:1020:1020:::/bin/false -gps:x:1021:1021:::/bin/false -unused1:x:1022:1022:::/bin/false media_rw:x:1023:1023:::/bin/false -mtp:x:1024:1024:::/bin/false -unused2:x:1025:1025:::/bin/false -drmrpc:x:1026:1026:::/bin/false nfc:x:1027:1027:::/bin/false sdcard_r:x:1028:1028:::/bin/false -clat:x:1029:1029:::/bin/false -loop_radio:x:1030:1030:::/bin/false -media_drm:x:1031:1031:::/bin/false package_info:x:1032:1032:::/bin/false -sdcard_pics:x:1033:1033:::/bin/false -sdcard_av:x:1034:1034:::/bin/false -sdcard_all:x:1035:1035:::/bin/false logd:x:1036:1036:::/bin/false shared_relro:x:1037:1037:::/bin/false -dbus:x:1038:1038:::/bin/false -tlsdate:x:1039:1039:::/bin/false -media_ex:x:1040:1040:::/bin/false audioserver:x:1041:1041:::/bin/false -metrics_coll:x:1042:1042:::/bin/false -metricsd:x:1043:1043:::/bin/false -webserv:x:1044:1044:::/bin/false -debuggerd:x:1045:1045:::/bin/false -media_codec:x:1046:1046:::/bin/false cameraserver:x:1047:1047:::/bin/false -firewall:x:1048:1048:::/bin/false -trunks:x:1049:1049:::/bin/false -nvram:x:1050:1050:::/bin/false -dns:x:1051:1051:::/bin/false -dns_tether:x:1052:1052:::/bin/false -webview_zygote:x:1053:1053:::/bin/false -vehicle_network:x:1054:1054:::/bin/false -media_audio:x:1055:1055:::/bin/false -media_video:x:1056:1056:::/bin/false -media_image:x:1057:1057:::/bin/false -tombstoned:x:1058:1058:::/bin/false -media_obb:x:1059:1059:::/bin/false -ese:x:1060:1060:::/bin/false -ota_update:x:1061:1061:::/bin/false -automotive_evs:x:1062:1062:::/bin/false -lowpan:x:1063:1063:::/bin/false -hsm:x:1064:1064:::/bin/false -reserved_disk:x:1065:1065:::/bin/false -statsd:x:1066:1066:::/bin/false -incidentd:x:1067:1067:::/bin/false -secure_element:x:1068:1068:::/bin/false -lmkd:x:1069:1069:::/bin/false -llkd:x:1070:1070:::/bin/false -iorapd:x:1071:1071:::/bin/false -gpu_service:x:1072:1072:::/bin/false -network_stack:x:1073:1073:::/bin/false -gsid:x:1074:1074:::/bin/false shell:x:2000:2000:::/bin/false cache:x:2001:2001:::/bin/false -diag:x:2002:2002:::/bin/false -oem_reserved_start:x:2900:2900:::/bin/false -oem_reserved_end:x:2999:2999:::/bin/false -net_bt_admin:x:3001:3001:::/bin/false -net_bt:x:3002:3002:::/bin/false -inet:x:3003:3003:::/bin/false -net_raw:x:3004:3004:::/bin/false -net_admin:x:3005:3005:::/bin/false net_bw_stats:x:3006:3006:::/bin/false net_bw_acct:x:3007:3007:::/bin/false readproc:x:3009:3009:::/bin/false wakelock:x:3010:3010:::/bin/false uhid:x:3011:3011:::/bin/false -oem_reserved_2_start:x:5000:5000:::/bin/false -oem_reserved_2_end:x:5999:5999:::/bin/false -everybody:x:9997:9997:::/bin/false misc:x:9998:9998:::/bin/false -nobody:x:9999:9999:::/bin/false app:x:10000:10000:::/bin/false -app_start:x:10000:10000:::/bin/false -app_end:x:19999:19999:::/bin/false -cache_gid_start:x:20000:20000:::/bin/false -cache_gid_end:x:29999:29999:::/bin/false -ext_gid_start:x:30000:30000:::/bin/false -ext_gid_end:x:39999:39999:::/bin/false -ext_cache_gid_start:x:40000:40000:::/bin/false -ext_cache_gid_end:x:49999:49999:::/bin/false -shared_gid_start:x:50000:50000:::/bin/false -shared_gid_end:x:59999:59999:::/bin/false -overflowuid:x:65534:65534:::/bin/false -isolated_start:x:90000:90000:::/bin/false -isolated_end:x:99999:99999:::/bin/false -user:x:100000:100000:::/bin/false -user_offset:x:100000:100000:::/bin/false diff --git a/services/src/init_cmds.c b/services/src/init_cmds.c index d881086c708c467210cc8cbe422db99888456be5..23d662184e1b6c868b1139f96053ca3540112e60 100644 --- a/services/src/init_cmds.c +++ b/services/src/init_cmds.c @@ -17,21 +17,23 @@ #include #include +#include +#ifndef OHOS_LITE +#include +#endif +#include #include #include #include #include +#include #include #include #include +#include #include #include #include -#include -#ifndef OHOS_LITE -#include -#endif -#include #include "init_jobs.h" #include "init_log.h" #ifndef OHOS_LITE @@ -217,6 +219,102 @@ void FreeCmd(struct CmdArgs **cmd) return; } +#define EXTRACT_ARGS(cmdname, cmdContent, args) \ + struct CmdArgs *ctx = GetCmd(cmdContent, " ", args); \ + if ((ctx == NULL) || (ctx->argv == NULL) || (ctx->argc != args)) { \ + INIT_LOGE("Command \"%s\" with invalid arguments: %s", #cmdname, cmdContent); \ + goto out; \ + } \ + +static void DoSetDomainname(const char *cmdContent, int maxArg) +{ + EXTRACT_ARGS(domainname, cmdContent, maxArg) + int fd = open("/proc/sys/kernel/domainname", O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, + S_IRUSR | S_IWUSR); + if (fd < 0) { + INIT_LOGE("DoSetDomainame failed to open \"/proc/sys/kernel/domainname\". err = %d", errno); + goto out; + } + + size_t size = strlen(ctx->argv[0]); + ssize_t n = write(fd, ctx->argv[0], size); + if (n != (ssize_t)size) { + INIT_LOGE("DoSetHostname failed to write %s to \"/proc/sys/kernel/domainname\". err = %d", errno); + } + + if (fd > 0) { + close(fd); + fd = -1; + } +out: + FreeCmd(&ctx); + return; +} + +static void DoSetHostname(const char *cmdContent, int maxArg) +{ + EXTRACT_ARGS(hostname, cmdContent, maxArg) + int fd = open("/proc/sys/kernel/hostname", O_WRONLY | O_CREAT | O_CLOEXEC | O_TRUNC, + S_IRUSR | S_IWUSR); + if (fd < 0) { + INIT_LOGE("DoSetHostname failed to open \"/proc/sys/kernel/hostname\". err = %d", errno); + goto out; + } + + size_t size = strlen(ctx->argv[0]); + ssize_t n = write(fd, ctx->argv[0], size); + if (n != (ssize_t)size) { + INIT_LOGE("DoSetHostname failed to write %s to \"/proc/sys/kernel/hostname\". err = %d", errno); + } + + if (fd > 0) { + close(fd); + fd = -1; + } +out: + FreeCmd(&ctx); + return; +} + +#ifndef OHOS_LITE +static void DoIfup(const char *cmdContent, int maxArg) +{ + EXTRACT_ARGS(ifup, cmdContent, maxArg) + struct ifreq interface; + if (strncpy_s(interface.ifr_name, IFNAMSIZ - 1, ctx->argv[0], strlen(ctx->argv[0])) != EOK) { + INIT_LOGE("DoIfup failed to copy interface name"); + goto out; + } + + INIT_LOGD("interface name: %s", interface.ifr_name); + int fd = socket(AF_INET, SOCK_DGRAM, 0); + if (fd < 0) { + INIT_LOGE("DoIfup failed to create socket, err = %d", errno); + goto out; + } + + if (ioctl(fd, SIOCGIFFLAGS, &interface) < 0) { + INIT_LOGE("DoIfup failed to do ioctl with command \"SIOCGIFFLAGS\", err = %d", errno); + close(fd); + fd = -1; + goto out; + } + interface.ifr_flags |= IFF_UP; + + if (ioctl(fd, SIOCSIFFLAGS, &interface) < 0) { + INIT_LOGE("DoIfup failed to do ioctl with command \"SIOCSIFFLAGS\", err = %d", errno); + } + + if (fd > 0) { + close(fd); + fd = -1; + } +out: + FreeCmd(&ctx); + return; +} +#endif + static void DoSleep(const char *cmdContent, int maxArg) { struct CmdArgs *ctx = GetCmd(cmdContent, " ", maxArg); @@ -1103,13 +1201,16 @@ static const struct CmdTable CMD_TABLE[] = { { "setparam ", 2, DoSetParam }, { "load_persist_params ", 1, DoLoadPersistParams }, { "load_param ", 1, DoLoadDefaultParams }, + { "ifup ", 1, DoIfup }, #endif { "stop ", 1, DoStop }, { "reset ", 1, DoReset }, { "copy ", 2, DoCopy }, { "reboot ", 1, DoRebootCmd }, { "setrlimit ", 3, DoSetrlimit }, - { "sleep ", 1, DoSleep } + { "sleep ", 1, DoSleep }, + { "hostname ", 1, DoSetHostname }, + { "domainname ", 1, DoSetDomainname } }; void DoCmdByName(const char *name, const char *cmdContent) diff --git a/services/src/init_service_manager.c b/services/src/init_service_manager.c index ffa2135f22c00c0c042e4e740dbd6c49104262c3..265be6e70ff05d131880c8679ce2c3f8fab1e2b7 100644 --- a/services/src/init_service_manager.c +++ b/services/src/init_service_manager.c @@ -129,6 +129,7 @@ static int GetServiceName(const cJSON* curArrItem, Service* curServ) return SERVICE_SUCCESS; } +#ifdef OHOS_LITE static int IsForbidden(const char* fieldStr) { size_t fieldLen = strlen(fieldStr); @@ -150,6 +151,12 @@ static int IsForbidden(const char* fieldStr) return 0; } } +#else +static int IsForbidden(const char* fieldStr) +{ + return 0; +} +#endif // TODO: move this function to common files static cJSON* GetArrItem(const cJSON* fileRoot, int* arrSize, const char* arrName)