From 621089c73c7819e6ba57fce0cc3f72201c9d59b7 Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Thu, 10 Jan 2019 00:17:22 +0000 Subject: [PATCH 01/16] power: Add deviced-shutdown This commit provides special binary that is going to handle Tizen system shutdown. This utility is called by systemd, not deviced. The final shutdown scheme is going to look like following: 1. deviced receivies request to shutdown/reboot the system, - preparation is performed (send signals, etc) - operation start is marked in /run/ - hardcoded actions are started - depending on shutdown type following is performed: + direct shutdown: system is rebooted immidietly (end of flow) + other: dbus request to systemd is sent 2. systemd receives the "poweroff" request - supported requests are Reboot, PowerOff & Halt - systemd (pid1) replaces it's own process with shutdown binary (execve("/usr/lib/systemd/systemd-shutdown")) - shutdown binary is provided by deviced package by alternatives mechanism # ls -l systemd-shutdown systemd-shutdown -> /etc/alternatives/systemd-shutdown # ls -l /etc/alternatives/systemd-shutdown /etc/alternatives/systemd-shutdown -> /usr/lib/systemd/deviced-shutdown (alternatives allow both systemd and deviced to "provide" same binary, with deviced having higher priority being selected as the one to use) 3. /usr/lib/systemd/systemd-shutdown becomes pid1 - original systemd1 manager is no longer available, meaning no restart, watchdog, socket/path/etc activation is handled. - depending on systemd-provided params it performs reboot or poweroff Change-Id: I46b4d0f375a1a6e7fc3e1d74b0530d7ec73ccb12 Signed-off-by: Hyotaek Shim --- CMakeLists.txt | 7 ++ packaging/deviced.spec | 15 ++++- src/power-shutdown/shutdown.c | 150 ++++++++++++++++++++++++++++++++++++++++++ src/power/power-handler.c | 80 +++++++++------------- src/power/power-handler.h | 2 + 5 files changed, 204 insertions(+), 50 deletions(-) create mode 100644 src/power-shutdown/shutdown.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 013b1f2..66ebf37 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -227,6 +227,13 @@ SET(deviced_LDFLAGS ${pkgs_LDFLAGS}) TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} "-lrt -ldl -lm" shared) INSTALL(TARGETS ${PROJECT_NAME} DESTINATION bin) +IF(POWER_MODULE STREQUAL on) + ADD_EXECUTABLE(deviced-shutdown src/power-shutdown/shutdown.c src/core/common.c src/core/execute.c) + SET(deviced-shutdown_LDFLAGS ${pkgs_LDFLAGS}) + TARGET_LINK_LIBRARIES(deviced-shutdown ${pkgs_LDFLAGS} "-lrt -ldl -lm" shared) + INSTALL(TARGETS deviced-shutdown DESTINATION lib/systemd) +ENDIF() + INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/deviced/ DESTINATION include/${PROJECT_NAME} FILES_MATCHING PATTERN "*_doc.h" EXCLUDE diff --git a/packaging/deviced.spec b/packaging/deviced.spec index b3713e1..6a7b35f 100644 --- a/packaging/deviced.spec +++ b/packaging/deviced.spec @@ -41,9 +41,11 @@ BuildRequires: pkgconfig(capi-system-device) Requires: %{name}-tools = %{version}-%{release} %{?systemd_requires} Requires(post): /usr/bin/vconftool +Requires(post): %{_sbindir}/update-alternatives +Requires(preun): %{_sbindir}/update-alternatives %description -deviced +Deviced %package -n libdeviced Summary: Deviced library @@ -178,6 +180,9 @@ mkdir -p %{buildroot}%{TZ_SYS_DUMPGEN} install -m 775 scripts/dump_pmstate_log.sh %{buildroot}%{TZ_SYS_DUMPGEN}/dump_pmstate_log.sh %post +# Assume power module is on (-DPOWER_MODULE=on) +update-alternatives --install %{_libdir}/systemd/systemd-shutdown systemd-shutdown %{_libdir}/systemd/deviced-shutdown 500 + #memory type vconf key init users_gid=$(getent group %{TZ_SYS_USER_GROUP} | cut -f3 -d':') @@ -186,6 +191,10 @@ if [ "$1" == "1" ]; then systemctl restart deviced.service fi +%preun +# Assume power module is on (-DPOWER_MODULE=on) +update-alternatives --remove %{_libdir}/systemd/systemd-shutdown systemd-shutdown %{_libdir}/systemd/deviced-shutdown + %postun systemctl daemon-reload if [ "$1" == "0" ]; then @@ -238,6 +247,10 @@ mv %{_libdir}/iot-display.so %{_libdir}/deviced/display.so %{_sysconfdir}/deviced/usb-host-test/descs %{_sysconfdir}/deviced/usb-host-test/strs +# Assume power module is on (-DPOWER_MODULE=on) +%{_libdir}/systemd/deviced-shutdown + + %files -n libdeviced %manifest deviced.manifest %license LICENSE.Apache-2.0 diff --git a/src/power-shutdown/shutdown.c b/src/power-shutdown/shutdown.c new file mode 100644 index 0000000..0ac97c5 --- /dev/null +++ b/src/power-shutdown/shutdown.c @@ -0,0 +1,150 @@ +/* + * deviced-shutdown: deviced's replacement for systemd-shutdown binary + * + * This binary is supposed to be called on Tizen system by systemd + * manager during shutdown, as result of eg. dbus call to Reboot + * method, test via: + * + * # dbus-send --print-reply --system --dest=org.freedesktop.systemd1 --type=method_call /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager.(Reboot|PowerOff) + * + * + * Copyright (c) 2019 Samsung Electronics Co., Ltd. + * + * 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "dd-deviced.h" +#include "core/log.h" +#include "core/common.h" + +#define UMOUNT_RW_PATH_USER "/opt/usr" +#define UMOUNT_RW_PATH_SYSTEM "/opt" +#define MAX_UMOUNT_KILL_RETRY 4 + +char *progname; + +static int usage(void) +{ + printf("%s: Invalid command specified (supported are: reboot, halt|poweroff, exit)\n", progname); + return 0; +} + +static void umount_partitions(void) +{ + umount_partition_by_kill(UMOUNT_RW_PATH_USER, MAX_UMOUNT_KILL_RETRY); + umount_partition_by_kill(UMOUNT_RW_PATH_SYSTEM, MAX_UMOUNT_KILL_RETRY); +} + +static void sigchld(int signo) +{ + int wstatus; + int r; + + do + r = waitpid(-1, &wstatus, WNOHANG); + while (r > 0); +} + +static void autoboot(void) +{ + static char buf[4096] = {0,}; + int fd = open("/run/reboot", O_RDONLY); // XXX POWER_FLAG_REBOOT + if (fd >= 0) { + (void)read(fd, buf, sizeof(buf)); + close(fd); + } + + printf("%s: reboot param is <%s>\n", progname, buf); + syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, + buf[0] != 0 ? LINUX_REBOOT_CMD_RESTART2 : LINUX_REBOOT_CMD_RESTART, + &buf); +} + +int main(int ac, char *av[]) +{ + enum { + ARG_LOGLEVEL, + ARG_LOGTARGET, + ARG_LOGCOLOR, + ARG_LOGLOCATION, + ARG_EXITCODE + }; + static struct option opts[] = { + {"log-level", required_argument, NULL, ARG_LOGLEVEL}, + {"log-target", required_argument, NULL, ARG_LOGTARGET}, + {"log-color", optional_argument, NULL, ARG_LOGCOLOR}, + {"log-location", optional_argument, NULL, ARG_LOGLOCATION}, + {"exit-code", required_argument, NULL, ARG_EXITCODE}, + {}, + }; + + progname = av[0]; + + int c; + while ((c = getopt_long(ac, av, "", opts, NULL)) >= 0) + ; // not supported for now + + int cmd; + char *cmd_name = av[optind]; + if (optind >= ac || !cmd_name) + return usage(); + + if (strcmp(cmd_name, "reboot") == 0) + cmd = RB_AUTOBOOT; + else if (strcmp(cmd_name, "halt") == 0 || strcmp(cmd_name, "poweroff") == 0) + cmd = RB_POWER_OFF; + else if (strcmp(cmd_name, "exit") == 0) + exit(0); + else + // XXX this is critical failure - we are pid1 already + // and can't perform requested action - exit below + // will cause system to panic() and, hopefully, reboot + // (depends on sysctls) + return usage(); + + printf("%s: executing %s action\n", progname, cmd_name); + + (void)signal(SIGCHLD, sigchld); + (void)signal(SIGHUP, SIG_IGN); + (void)signal(SIGTERM, SIG_IGN); + + if (!is_emulator()) { + printf("%s: unmounting rw partitions\n", progname); + umount_partitions(); + } + + printf("%s: flushing filesystems\n", progname); + + sync(); + + printf("%s: performing %s\n", progname, cmd_name); + + if (cmd == RB_AUTOBOOT) + autoboot(); + else + reboot(cmd); + + return 1; +} diff --git a/src/power/power-handler.c b/src/power/power-handler.c index d63a29e..a084cc7 100644 --- a/src/power/power-handler.c +++ b/src/power/power-handler.c @@ -61,14 +61,9 @@ #define POWEROFF_WAIT_RESOURCED (0.5*1000) /* 0.5 seconds */ #define POWEROFF_WAIT_MAX 10 /* 10 seconds */ -#define SIGNAL_POWEROFF_STATE "ChangeState" - -#define UMOUNT_RW_PATH_USER "/opt/usr" -#define UMOUNT_RW_PATH_SYSTEM "/opt" -#define MAX_UMOUNT_KILL_RETRY 4 +#define POWEROFF_WAIT_SYSTEMD_MS (30 * 1000/*ms*/) /* 30 seconds */ -#define POWER_FLAG_POWEROFF "/run/"POWER_POWEROFF -#define POWER_FLAG_REBOOT "/run/"POWER_REBOOT +#define SIGNAL_POWEROFF_STATE "ChangeState" #define POWER_CONF_FILE "/etc/deviced/power.conf" @@ -194,14 +189,6 @@ static bool disable_coredump_handler(void) return is_ok; } -/* umount usr data partition */ -static void unmount_rw_partition(void) -{ - umount_partition_by_kill(UMOUNT_RW_PATH_USER, MAX_UMOUNT_KILL_RETRY); - umount_partition_by_kill(UMOUNT_RW_PATH_SYSTEM, MAX_UMOUNT_KILL_RETRY); - sync(); -} - static void powerdown(void) { static int wait; @@ -236,52 +223,49 @@ static void powerdown(void) } watchdog_notify(); - - if (!is_emulator()) - unmount_rw_partition(); } -static void reboot_with_option(int cmd, char *option) +static void poweroff_request_shutdown() { - if (!cmd) + _I("Requested power off via systemd"); + + char *method; + + if (poweroff_opt.type == POWER_OFF_REBOOT) + method = "Reboot"; + else if (poweroff_opt.type == POWER_OFF_POWEROFF) + method = "PowerOff"; + else { + _E("poweroff invalid type (%d)", poweroff_opt.type); return; - if (syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, - cmd, (void *)option) < 0) - _E("Failed to reboot with option (%d)", errno); -} + } -static void poweroff(void) -{ - _I("Power off !!!"); - reboot_with_option(LINUX_REBOOT_CMD_POWER_OFF, NULL); + dbus_handle_method_sync_timeout(SYSTEMD_DBUS_DEST, + SYSTEMD_DBUS_PATH, + SYSTEMD_DBUS_IFACE_MANAGER, + method, + NULL, + NULL, + POWEROFF_WAIT_SYSTEMD_MS); } -static void restart(char *option) +static void poweroff_direct(void) { - if (option) { - _I("Restart with option %s !!!", option); - reboot_with_option(LINUX_REBOOT_CMD_RESTART2, option); - } else { - _I("Restart !!!"); - reboot_with_option(LINUX_REBOOT_CMD_RESTART, NULL); - } + _I("Requested power off without systemd (direct call)"); + if (syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, + LINUX_REBOOT_CMD_POWER_OFF, NULL) < 0) + _E("Failed to poweroff (%d)", errno); } int poweroff_procedure(void) { - powerdown(); - - if (poweroff_opt.type == POWER_OFF_DIRECT) { - poweroff(); - return 0; - } - if (poweroff_opt.type == POWER_OFF_RESTART) { - restart(poweroff_opt.option); - return 0; - } + powerdown(); - _E("poweroff invalid type (%d)", poweroff_opt.type); + if (poweroff_opt.type == POWER_OFF_DIRECT) + poweroff_direct(); + else + poweroff_request_shutdown(); return 0; } @@ -508,8 +492,6 @@ static void poweroff_send_broadcast(int status) static int power_execute_pid(char *type, char *option, pid_t pid) { - int ret; - if (during_poweroff) { _E("during poweroff"); return -EINVAL; diff --git a/src/power/power-handler.h b/src/power/power-handler.h index 1406b3f..968e17a 100644 --- a/src/power/power-handler.h +++ b/src/power/power-handler.h @@ -28,6 +28,8 @@ #define PWROFF_POPUP "pwroff-popup" #define PWROFF_POPUP_LEN 12 +#define POWER_FLAG_POWEROFF "/run/"POWER_POWEROFF +#define POWER_FLAG_REBOOT "/run/"POWER_REBOOT enum poweroff_type { POWER_OFF_INVALID = 0, -- 2.7.4 From c4546512bb84cf1f6f6cd4120ecf644afc83f32b Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Thu, 31 Jan 2019 11:58:47 +0900 Subject: [PATCH 02/16] Version 5.5.0 Change-Id: I5bfa2097c864342fccd20c0660524a0c35ac5580 Signed-off-by: Hyotaek Shim --- packaging/deviced.spec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packaging/deviced.spec b/packaging/deviced.spec index 6a7b35f..db995fd 100644 --- a/packaging/deviced.spec +++ b/packaging/deviced.spec @@ -5,7 +5,7 @@ Name: deviced Summary: Deviced -Version: 5.0.0 +Version: 5.5.0 Release: 1 Group: System/Management License: Apache-2.0 -- 2.7.4 From 29b553d5134ad23966c1686558e7cfd50071fe2e Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Thu, 31 Jan 2019 17:01:24 +0900 Subject: [PATCH 03/16] Fix 64-bit build errors, /lib64/systemd/ Change-Id: I7170c14e46cdd8ccbbcfcede18232bb10a62c8e9 Signed-off-by: Hyotaek Shim --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 66ebf37..daba64d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -231,7 +231,7 @@ IF(POWER_MODULE STREQUAL on) ADD_EXECUTABLE(deviced-shutdown src/power-shutdown/shutdown.c src/core/common.c src/core/execute.c) SET(deviced-shutdown_LDFLAGS ${pkgs_LDFLAGS}) TARGET_LINK_LIBRARIES(deviced-shutdown ${pkgs_LDFLAGS} "-lrt -ldl -lm" shared) - INSTALL(TARGETS deviced-shutdown DESTINATION lib/systemd) + INSTALL(TARGETS deviced-shutdown DESTINATION ${LIB_INSTALL_DIR}/systemd) ENDIF() INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/deviced/ DESTINATION include/${PROJECT_NAME} -- 2.7.4 From 6b9a6abc130ba77a4371f9f3aeda648cc098b1b0 Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Thu, 31 Jan 2019 20:21:30 +0900 Subject: [PATCH 04/16] Remove unused edbus-handler Change-Id: I5a95dc4c181ca2c22793264f98198340cd0cfb40 Signed-off-by: Hyotaek Shim --- src/core/edbus-handler.c | 893 ----------------------------------------------- src/core/edbus-handler.h | 67 ---- 2 files changed, 960 deletions(-) delete mode 100644 src/core/edbus-handler.c delete mode 100644 src/core/edbus-handler.h diff --git a/src/core/edbus-handler.c b/src/core/edbus-handler.c deleted file mode 100644 index ddc5944..0000000 --- a/src/core/edbus-handler.c +++ /dev/null @@ -1,893 +0,0 @@ -/* - * deviced - * - * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. - * - * 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 - -#include "core/log.h" -#include "core/common.h" -#include "core/device-idler.h" -#include "core/device-notifier.h" -#include "core/list.h" - -#define EDBUS_INIT_RETRY_COUNT 5 -#define NAME_OWNER_CHANGED "NameOwnerChanged" -#define NAME_OWNER_MATCH "type='signal',sender='org.freedesktop.DBus'," \ - "path='/org/freedesktop/DBus',interface='org.freedesktop.DBus'," \ - "member='NameOwnerChanged',arg0='%s'" - -/* -1 is a default timeout value, it's converted to 25*1000 internally. */ -#define DBUS_REPLY_TIMEOUT (-1) -#define RETRY_MAX 5 - -struct edbus_list { - char *signal_name; - E_DBus_Signal_Handler *handler; -}; - -static struct edbus_object { - char *path; - char *interface; - E_DBus_Object *obj; - E_DBus_Interface *iface; -} edbus_objects[] = { - { DEVICED_PATH_CORE , DEVICED_INTERFACE_CORE , NULL, NULL }, - { DEVICED_PATH_DISPLAY, DEVICED_INTERFACE_DISPLAY, NULL, NULL }, - { DEVICED_PATH_POWER , DEVICED_INTERFACE_POWER , NULL, NULL }, - { DEVICED_PATH_LOWPOWER, DEVICED_INTERFACE_LOWPOWER, NULL, NULL }, - { DEVICED_PATH_REBOOT , DEVICED_INTERFACE_REBOOT , NULL, NULL }, - { DEVICED_PATH_STORAGE, DEVICED_INTERFACE_STORAGE, NULL, NULL }, - { DEVICED_PATH_PROCESS, DEVICED_INTERFACE_PROCESS, NULL, NULL }, - { DEVICED_PATH_KEY , DEVICED_INTERFACE_KEY , NULL, NULL }, - { DEVICED_PATH_SYSNOTI, DEVICED_INTERFACE_SYSNOTI, NULL, NULL }, - { DEVICED_PATH_USB , DEVICED_INTERFACE_USB , NULL, NULL }, - /* Add new object & interface here*/ -}; - -struct watch_func_info { - bool deleted; - void (*func)(const char *sender, void *data); - void *data; -}; - -struct watch_info { - bool deleted; - char *sender; - dd_list *func_list; -}; - -static dd_list *edbus_object_list; -static dd_list *edbus_owner_list; -static dd_list *edbus_handler_list; -static dd_list *edbus_watch_list; -static int edbus_init_val; -static DBusConnection *conn; -static E_DBus_Connection *edbus_conn; -static DBusPendingCall *edbus_request_name; - -static DBusHandlerResult message_filter(DBusConnection *connection, - DBusMessage *message, void *data); - -DBusConnection *get_dbus_connection(void) -{ - return conn; -} - -E_DBus_Object *register_edbus_object(const char *object_path, void *data) -{ - E_DBus_Object *object; - - if (!object_path) { - _E("invalid parameter"); - return NULL; - } - - object = e_dbus_object_add(edbus_conn, object_path, data); - if (!object) { - _E("fail to add object for %s", object_path); - return NULL; - } - - return object; -} - -void unregister_edbus_object(E_DBus_Object *object) -{ - if (!object) - return; - - e_dbus_object_free(object); -} - -static int register_edbus_interface(struct edbus_object *object) -{ - if (!object) { - _E("object is invalid value!"); - return -1; - } - - object->obj = e_dbus_object_add(edbus_conn, object->path, NULL); - if (!object->obj) { - _E("fail to add edbus obj"); - return -1; - } - - object->iface = e_dbus_interface_new(object->interface); - if (!object->iface) { - _E("fail to add edbus interface"); - return -1; - } - - e_dbus_object_interface_attach(object->obj, object->iface); - - return 0; -} - -E_DBus_Interface *get_edbus_interface(const char *path) -{ - int i; - - for (i = 0; i < ARRAY_SIZE(edbus_objects); i++) - if (!strcmp(path, edbus_objects[i].path)) - return edbus_objects[i].iface; - - return NULL; -} - -pid_t get_edbus_sender_pid(DBusMessage *msg) -{ - const char *sender; - DBusMessage *send_msg; - DBusPendingCall *pending; - DBusMessageIter iter; - int ret; - pid_t pid; - - if (!msg) { - _E("invalid argument!"); - return -1; - } - - sender = dbus_message_get_sender(msg); - if (!sender) { - _E("invalid sender!"); - return -1; - } - - send_msg = dbus_message_new_method_call(DBUS_SERVICE_DBUS, - DBUS_PATH_DBUS, - DBUS_INTERFACE_DBUS, - "GetConnectionUnixProcessID"); - if (!send_msg) { - _E("invalid send msg!"); - return -1; - } - - ret = dbus_message_append_args(send_msg, DBUS_TYPE_STRING, - &sender, DBUS_TYPE_INVALID); - if (!ret) { - _E("fail to append args!"); - dbus_message_unref(send_msg); - return -1; - } - - pending = e_dbus_message_send(edbus_conn, send_msg, NULL, -1, NULL); - if (!pending) { - _E("pending is null!"); - dbus_message_unref(send_msg); - return -1; - } - - dbus_message_unref(send_msg); - - /* block until reply is received */ - dbus_pending_call_block(pending); - - msg = dbus_pending_call_steal_reply(pending); - dbus_pending_call_unref(pending); - if (!msg) { - _E("reply msg is null!"); - return -1; - } - - dbus_message_iter_init(msg, &iter); - dbus_message_iter_get_basic(&iter, &pid); - dbus_message_unref(msg); - - return pid; -} - -static void unregister_edbus_signal_handle(void) -{ - dd_list *tmp, *next; - struct edbus_list *entry; - - DD_LIST_FOREACH_SAFE(edbus_handler_list, tmp, next, entry) { - if (!entry->handler) - continue; - e_dbus_signal_handler_del(edbus_conn, entry->handler); - DD_LIST_REMOVE(edbus_handler_list, entry); - free(entry->signal_name); - free(entry); - } -} - -int register_edbus_signal_handler(const char *path, const char *interface, - const char *name, E_DBus_Signal_Cb cb) -{ - dd_list *tmp; - struct edbus_list *entry; - E_DBus_Signal_Handler *handler; - - DD_LIST_FOREACH(edbus_handler_list, tmp, entry) { - if (strncmp(entry->signal_name, name, strlen(name)) == 0) - return -EEXIST; - } - - handler = e_dbus_signal_handler_add(edbus_conn, NULL, path, - interface, name, cb, NULL); - - if (!handler) { - _E("fail to add edbus handler"); - return -ENOMEM; - } - - entry = malloc(sizeof(struct edbus_list)); - - if (!entry) { - e_dbus_signal_handler_del(edbus_conn, handler); - _E("Malloc failed"); - return -ENOMEM; - } - - entry->signal_name = strndup(name, strlen(name)); - - if (!entry->signal_name) { - _E("Malloc failed"); - e_dbus_signal_handler_del(edbus_conn, handler); - free(entry); - return -ENOMEM; - } - - entry->handler = handler; - DD_LIST_PREPEND(edbus_handler_list, entry); - if (!edbus_handler_list) { - _E("dd_list_prepend failed"); - e_dbus_signal_handler_del(edbus_conn, handler); - free(entry->signal_name); - free(entry); - return -ENOMEM; - } - return 0; -} - -int unregister_edbus_signal_handler(const char *path, const char *interface, - const char *name) -{ - dd_list *tmp, *next; - struct edbus_list *entry; - - DD_LIST_FOREACH_SAFE(edbus_handler_list, tmp, next, entry) { - if (strncmp(entry->signal_name, name, strlen(name) + 1) == 0) { - e_dbus_signal_handler_del(edbus_conn, entry->handler); - DD_LIST_REMOVE(edbus_handler_list, entry); - free(entry->signal_name); - free(entry); - return 0; - } - } - - return -1; -} - -int broadcast_edbus_signal(const char *path, const char *interface, - const char *name, const char *sig, char *param[]) -{ - DBusMessage *msg; - DBusMessageIter iter; - int r; - - msg = dbus_message_new_signal(path, interface, name); - if (!msg) { - _E("fail to allocate new %s.%s signal", interface, name); - return -EPERM; - } - - dbus_message_iter_init_append(msg, &iter); - /*r = append_variant(&iter, sig, param); - if (r < 0) { - _E("append_variant error(%d)", r); - return -EPERM; - }*/ - - r = dbus_connection_send(conn, msg, NULL); - dbus_message_unref(msg); - - if (r != TRUE) { - _E("dbus_connection_send error(%s:%s-%s)", - path, interface, name); - return -ECOMM; - } - - return 0; -} - -static void print_watch_item(void) -{ - struct watch_info *watch; - struct watch_func_info *finfo; - dd_list *n; - dd_list *e; - - DD_LIST_FOREACH(edbus_watch_list, n, watch) { - _D("watch sender : %s, deleted : %d", - watch->sender, watch->deleted); - DD_LIST_FOREACH(watch->func_list, e, finfo) - _D("\tfunc : %p, deleted : %d", - finfo->func, finfo->deleted); - } -} - -static bool get_valid_watch_item(void) -{ - struct watch_info *watch; - dd_list *elem; - - DD_LIST_FOREACH(edbus_watch_list, elem, watch) { - if (!watch->deleted) - return true; - } - - return false; -} - -static void watch_idler_cb(void *data) -{ - struct watch_info *watch; - struct watch_func_info *finfo; - dd_list *n; - dd_list *next; - dd_list *elem; - dd_list *enext; - char match[256]; - - DD_LIST_FOREACH_SAFE(edbus_watch_list, n, next, watch) { - if (!watch->deleted) - continue; - - /* remove dbus match */ - snprintf(match, sizeof(match), NAME_OWNER_MATCH, watch->sender); - dbus_bus_remove_match(conn, match, NULL); - - _I("%s is not watched by dbus!", watch->sender); - - /* remove watch func list */ - DD_LIST_FOREACH_SAFE(watch->func_list, elem, enext, finfo) - free(finfo); - - /* remove watch item */ - DD_LIST_FREE_LIST(watch->func_list); - DD_LIST_REMOVE_LIST(edbus_watch_list, n); - free(watch->sender); - free(watch); - } - - /* if the last request, remove message filter */ - if (!get_valid_watch_item()) - dbus_connection_remove_filter(conn, message_filter, NULL); -} - -static DBusHandlerResult message_filter(DBusConnection *connection, - DBusMessage *message, void *data) -{ - int ret; - const char *iface, *member; - const char *sender; - struct watch_info *watch; - struct watch_func_info *finfo; - dd_list *n; - int len; - - if (dbus_message_get_type(message) != DBUS_MESSAGE_TYPE_SIGNAL) - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - - iface = dbus_message_get_interface(message); - member = dbus_message_get_member(message); - - if (strncmp(iface, DBUS_INTERFACE_DBUS, - sizeof(DBUS_INTERFACE_DBUS))) - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - - if (strncmp(member, NAME_OWNER_CHANGED, - sizeof(NAME_OWNER_CHANGED))) - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - - ret = dbus_message_get_args(message, NULL, - DBUS_TYPE_STRING, &sender, - DBUS_TYPE_INVALID); - if (!ret) { - _E("no message"); - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - } - - len = strlen(sender) + 1; - DD_LIST_FOREACH(edbus_watch_list, n, watch) { - if (!watch->deleted && - !strncmp(watch->sender, sender, len)) - break; - } - - if (!watch) - return DBUS_HANDLER_RESULT_NOT_YET_HANDLED; - - DD_LIST_FOREACH(watch->func_list, n, finfo) { - if (!finfo->deleted && - finfo->func) - finfo->func(watch->sender, finfo->data); - } - - /* no interest in this item anymore */ - watch->deleted = true; - - print_watch_item(); - add_idle_request(watch_idler_cb, NULL); - return DBUS_HANDLER_RESULT_HANDLED; -} - -static struct watch_info *get_matched_watch_item(const char *sender) -{ - int len; - dd_list *n; - struct watch_info *watch; - - if (!sender) - return NULL; - - len = strlen(sender) + 1; - /* check the sender&type is already registered */ - DD_LIST_FOREACH(edbus_watch_list, n, watch) { - if (!watch->deleted && - !strncmp(watch->sender, sender, len)) - return watch; - } - - return NULL; -} - -static struct watch_info *add_watch_item(const char *sender) -{ - DBusError err; - struct watch_info *watch; - char match[256]; - int ret; - - if (!sender) - return NULL; - - watch = calloc(1, sizeof(struct watch_info)); - if (!watch) - return NULL; - - watch->sender = strdup(sender); - if (!watch->sender) - goto out; - - dbus_error_init(&err); - /* add name owner changed match string */ - snprintf(match, sizeof(match), NAME_OWNER_MATCH, watch->sender); - dbus_bus_add_match(conn, match, &err); - - if (dbus_error_is_set(&err)) { - _E("fail to add match for %s [%s:%s]", - sender, err.name, err.message); - dbus_error_free(&err); - goto out; - } - - /* if the first request, add message filter */ - if (!get_valid_watch_item()) { - ret = dbus_connection_add_filter(conn, - message_filter, NULL, NULL); - if (!ret) { - _E("fail to add message filter!"); - dbus_bus_remove_match(conn, match, NULL); - goto out; - } - _I("success to add message filter!"); - } - - /* Add watch to watch list */ - DD_LIST_APPEND(edbus_watch_list, watch); - _I("%s is watched by dbus!", sender); - return watch; - -out: - if (watch) { - free(watch->sender); - free(watch); - } - - return NULL; -} - -int register_edbus_watch(const char *sender, - void (*func)(const char *sender, void *data), void *data) -{ - struct watch_info *watch; - struct watch_func_info *finfo; - dd_list *elem; - bool isnew = false; - - if (!sender || !func) { - _E("invalid argument : sender(NULL) || func(NULL)"); - return -EINVAL; - } - - watch = get_matched_watch_item(sender); - if (!watch) { - /* create new watch item */ - watch = add_watch_item(sender); - if (!watch) { - _E("fail to add watch item"); - return -EPERM; - } - isnew = true; - } - - /* find the same callback */ - DD_LIST_FOREACH(watch->func_list, elem, finfo) { - if (finfo->func == func) { - _E("there is already the same callback"); - goto out; - } - } - - finfo = calloc(1, sizeof(struct watch_func_info)); - if (!finfo) { - _E("fail to allocate watch func info"); - goto out; - } - - finfo->func = func; - finfo->data = data; - - /* add callback function to the watch list */ - DD_LIST_APPEND(watch->func_list, finfo); - - _I("register watch func(%p) of %s", func, sender); - return 0; -out: - if (isnew) - watch->deleted = true; - - return -EPERM; -} - -int unregister_edbus_watch(const char *sender, - void (*func)(const char *sender, void *data)) -{ - struct watch_info *watch; - struct watch_func_info *finfo; - dd_list *elem; - bool matched = false; - - if (!sender || !func) { - _E("invalid argument : sender(NULL) || func(NULL)"); - return -EINVAL; - } - - watch = get_matched_watch_item(sender); - if (!watch) { - _E("fail to get matched watch item"); - return -ENODEV; - } - - /* check the no interest function */ - DD_LIST_FOREACH(watch->func_list, elem, finfo) { - if (finfo->func == func) - finfo->deleted = true; - if (!finfo->deleted) - matched = true; - } - - /* if it is the last item */ - if (!matched) - watch->deleted = true; - - _I("unregister watch func(%p) of %s", func, sender); - return 0; -} - -static void unregister_edbus_watch_all(void) -{ - dd_list *n, *next; - struct watch_info *watch; - - DD_LIST_FOREACH_SAFE(edbus_watch_list, n, next, watch) - watch->deleted = true; - - add_idle_request(watch_idler_cb, NULL); -} - -static int register_method(E_DBus_Interface *iface, - const struct edbus_method *edbus_methods, int size) -{ - int ret; - int i; - - assert(iface); - assert(edbus_methods); - - for (i = 0; i < size; i++) { - ret = e_dbus_interface_method_add(iface, - edbus_methods[i].member, - edbus_methods[i].signature, - edbus_methods[i].reply_signature, - edbus_methods[i].func); - if (!ret) { - _E("fail to add method %s!", edbus_methods[i].member); - return -EINVAL; - } - } - - return 0; -} - -int register_edbus_interface_and_method(const char *path, - const char *interface, - const struct edbus_method *edbus_methods, int size) -{ - struct edbus_object *obj; - dd_list *elem; - int ret; - - if (!path || !interface || !edbus_methods || size < 1) { - _E("invalid parameter"); - return -EINVAL; - } - - /* find matched obj */ - DD_LIST_FOREACH(edbus_object_list, elem, obj) { - if (strncmp(obj->path, path, strlen(obj->path)) == 0 && - strncmp(obj->interface, interface, strlen(obj->interface)) == 0) { - _I("found matched item : obj(%p)", obj); - break; - } - } - - /* if there is no matched obj */ - if (!obj) { - obj = malloc(sizeof(struct edbus_object)); - if (!obj) { - _E("fail to allocate %s interface", path); - return -ENOMEM; - } - - obj->path = strdup(path); - obj->interface = strdup(interface); - - ret = register_edbus_interface(obj); - if (ret < 0) { - _E("fail to register %s interface(%d)", obj->path, ret); - free(obj->path); - free(obj->interface); - free(obj); - return ret; - } - - DD_LIST_APPEND(edbus_object_list, obj); - } - - ret = register_method(obj->iface, edbus_methods, size); - if (ret < 0) { - _E("fail to register %s method(%d)", obj->path, ret); - return ret; - } - - return 0; -} - -int unregister_edbus_interface_all(void) -{ - struct edbus_object *obj; - dd_list *elem, *n; - - DD_LIST_FOREACH_SAFE(edbus_object_list, elem, n, obj) { - DD_LIST_REMOVE(edbus_object_list, obj); - free(obj->path); - free(obj->interface); - free(obj); - } - - return 0; -} - -int register_edbus_method(const char *path, const struct edbus_method *edbus_methods, int size) -{ - E_DBus_Interface *iface; - int ret; - - if (!path || !edbus_methods || size < 1) { - _E("invalid parameter"); - return -EINVAL; - } - - iface = get_edbus_interface(path); - if (!iface) { - _E("fail to get edbus interface!"); - return -ENODEV; - } - - ret = register_method(iface, edbus_methods, size); - if (ret < 0) { - _E("fail to register %s method(%d)", path, ret); - return ret; - } - - return 0; -} - -static void request_name_cb(GVariant *var, void *user_data, GError *err) -{ - unsigned int val; - - if (!var) { - _D("invalid DBusMessage!"); - return; - } - - if (!dh_get_param_from_var(var, "(u)", &val)) { - _E("no message [%s]", g_variant_get_type_string(var)); - goto out; - } - - _I("Request Name reply : %d", val); - -out: - g_variant_unref(var); -} - -static void check_owner_name(void) -{ - GVariant *msg; - char exe_name[PATH_MAX]; - char *entry; - dd_list *n; - int pid; - - DD_LIST_FOREACH(edbus_owner_list, n, entry) { - msg = dbus_method_sync_with_reply_var(E_DBUS_FDO_BUS, - E_DBUS_FDO_PATH, - E_DBUS_FDO_INTERFACE, - "GetConnectionUnixProcessID", g_variant_new("(s)", entry)); - - if (!msg) { - _E("invalid DBusMessage!"); - return; - } - - if (!dh_get_param_from_var(msg, "(i)", &pid)) { - _E("fail (%s): no message", "GetConnectionUnixProcessID"); - goto out; - } - - if (get_cmdline_name(pid, exe_name, PATH_MAX) != 0) - goto out; - _I("%s(%d)", exe_name, pid); - -out: - g_variant_unref(msg); - } -} - -static void check_owner_list(void) -{ - DBusError err; - DBusMessage *msg; - DBusMessageIter array, item; - char *pa[1]; - char *name; - char *entry; - - pa[0] = DEVICED_BUS_NAME; - msg = dbus_method_sync_with_reply(E_DBUS_FDO_BUS, - E_DBUS_FDO_PATH, - E_DBUS_FDO_INTERFACE, - "ListQueuedOwners", "s", pa); - - if (!msg) { - _E("invalid DBusMessage!"); - return; - } - - dbus_error_init(&err); - dbus_message_iter_init(msg, &array); - - if (dbus_message_iter_get_arg_type(&array) != DBUS_TYPE_ARRAY) - goto out; - dbus_message_iter_recurse(&array, &item); - while (dbus_message_iter_get_arg_type(&item) == DBUS_TYPE_STRING) { - dbus_message_iter_get_basic(&item, &name); - entry = strndup(name, strlen(name)); - DD_LIST_APPEND(edbus_owner_list, entry); - if (!edbus_owner_list) { - _E("append failed"); - free(entry); - goto out; - } - dbus_message_iter_next(&item); - } - -out: - dbus_message_unref(msg); - dbus_error_free(&err); -} - -void edbus_init(void *data) -{ - int retry = 0; - int i, ret; - - dbus_handle_h handle; - - for (int i = 0 ; i < EDBUS_INIT_RETRY_COUNT; ++i) { - handle = dbus_handle_get_connection(G_BUS_TYPE_SYSTEM, FALSE); - if (handle) - break; - _E("fail to get dbus:retry %d", i); - } - if (!handle) { - _E("fail to get dbus"); - goto out1; - } - - for (int i = 0 ; i < EDBUS_INIT_RETRY_COUNT; ++i) { - ret = dbus_handle_request_bus_name(handle, DEVICED_BUS_NAME, NULL, NULL); - if (ret > 0) - break; - _E("failed to request bus name:retry %d", i); - } - if (ret <= 0) - goto out1; - - //check_owner_list(); - //check_owner_name(); - - return; - -out3: - //e_dbus_connection_close(edbus_conn); - return ; - -out2: - //dbus_connection_set_exit_on_disconnect(conn, FALSE); - return ; - -out1: - //e_dbus_shutdown(); - return ; -} - -void edbus_exit(void *data) -{ - unregister_edbus_signal_handle(); - unregister_edbus_watch_all(); - unregister_edbus_interface_all(); - e_dbus_connection_close(edbus_conn); - e_dbus_shutdown(); -} diff --git a/src/core/edbus-handler.h b/src/core/edbus-handler.h deleted file mode 100644 index 9dfa21b..0000000 --- a/src/core/edbus-handler.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * deviced - * - * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. - * - * 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. - */ - - -#ifndef __EDBUS_HANDLE_H__ -#define __EDBUS_HANDLE_H__ - -#include -#include -#include - -struct edbus_method { - const char *member; - const char *signature; - const char *reply_signature; - E_DBus_Method_Cb func; -}; - -//static inline DBusMessage *make_reply_message(DBusMessage *msg, int ret) -//{ -// DBusMessageIter iter; -// DBusMessage *reply; -// reply = dbus_message_new_method_return(msg); -// dbus_message_iter_init_append(reply, &iter); -// dbus_message_iter_append_basic(&iter, DBUS_TYPE_INT32, &ret); -// return reply; -//} - -DBusConnection *get_dbus_connection(void); -E_DBus_Object *register_edbus_object(const char *object_path, void *data); -void unregister_edbus_object(E_DBus_Object *object); -int register_edbus_interface_and_method(const char *path, - const char *interface, - const struct edbus_method *edbus_methods, int size); -int register_edbus_method(const char *path, const struct edbus_method *edbus_methods, int size); -int register_edbus_signal_handler(const char *path, const char *interface, - const char *name, E_DBus_Signal_Cb cb); -int unregister_edbus_signal_handler(const char *path, const char *interface, - const char *name); -E_DBus_Interface *get_edbus_interface(const char *path); -pid_t get_edbus_sender_pid(DBusMessage *msg); -int broadcast_edbus_signal(const char *path, const char *interface, - const char *name, const char *sig, char *param[]); -int register_edbus_watch(const char *sender, - void (*func)(const char *sender, void *data), void *data); -int unregister_edbus_watch(const char *sender, - void (*func)(const char *sender, void *data)); - -void edbus_init(void *data); -void edbus_exit(void *data); - -#endif /* __EDBUS_HANDLE_H__ */ -- 2.7.4 From e7b237f6a720a632bf1b5b2bb00311ccd3b4bc03 Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Fri, 1 Feb 2019 11:15:50 +0900 Subject: [PATCH 05/16] Coverity: 'handle' going out of scope Change-Id: Ib055cb0f12fd23d6c9750ef977b76c3d166203f8 Signed-off-by: Hyotaek Shim --- src/display/display-ops.h | 1 + src/shared/plugin.c | 13 +++++++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/display/display-ops.h b/src/display/display-ops.h index 0477e71..93f8abe 100644 --- a/src/display/display-ops.h +++ b/src/display/display-ops.h @@ -47,6 +47,7 @@ void remove_display(const struct display_ops *disp); const struct display_ops *find_display_feature(const char *name); struct display_plugin { + void *handle; int (*pm_lock_internal) (pid_t pid, int s_bits, int flag, int timeout); int (*pm_unlock_internal) (pid_t pid, int s_bits, int flag); int (*pm_change_internal) (pid_t pid, int s_bits); diff --git a/src/shared/plugin.c b/src/shared/plugin.c index ba96073..72f291d 100644 --- a/src/shared/plugin.c +++ b/src/shared/plugin.c @@ -46,25 +46,26 @@ int load_plugin(const char *id, void **h) char path[PATH_MAX]; void *handle; - if (!id) + if (!id || !h) { + _E("Wrong parameters"); return -EINVAL; + } /* Find matched module path */ snprintf(path, sizeof(path), "%s/%s.so", MODULE_PATH, id); if (access(path, R_OK) != 0) { - _E("there is no %s device", id); + _E("There is no %s device", id); return -ENODEV; } /* Load module */ handle = dlopen(path, RTLD_NOW|RTLD_GLOBAL); if (!handle) { - _E("fail to open module : %s", dlerror()); + _E("Failed to open module: %s", dlerror()); return -ENOENT; } - if (h) - *h = handle; + *h = handle; return 0; } @@ -79,7 +80,7 @@ int unload_plugin(void *h) void load_plugins() { - load_plugin("display", NULL); + load_plugin("display", &disp_plgn.handle); return; } -- 2.7.4 From 220157bf923401b5eae46f43fb0c4d33fb5669f7 Mon Sep 17 00:00:00 2001 From: lokilee73 Date: Fri, 1 Feb 2019 13:54:32 +0900 Subject: [PATCH 06/16] Add dump module to get debug infomation for devices When dump signal is received, dump logs are saved under /var/log/deviced.log Change-Id: Idde9ce3bb02430e5373dbd953134e8e4acf46616 Signed-off-by: lokilee73 --- CMakeLists.txt | 4 ++ packaging/deviced.spec | 1 + src/core/devices.h | 2 + src/dump/dump.c | 127 ++++++++++++++++++++++++++++++++++++++++++ src/shared/log-macro.h | 3 + src/touchscreen/touchscreen.c | 21 +++++++ 6 files changed, 158 insertions(+) create mode 100644 src/dump/dump.c diff --git a/CMakeLists.txt b/CMakeLists.txt index daba64d..929c273 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -112,6 +112,10 @@ IF(TOUCH_SENSITIVITY_MODULE STREQUAL on) SET(SRCS ${SRCS} src/touchscreen/sensitivity.c) ENDIF() +IF(DUMP_MODULE STREQUAL on) + SET(SRCS ${SRCS} src/dump/dump.c) +ENDIF() + IF(TZIP_MODULE STREQUAL on) ADD_SOURCE(src/tzip TZIP_SRCS) SET(SRCS ${SRCS} ${TZIP_SRCS}) diff --git a/packaging/deviced.spec b/packaging/deviced.spec index db995fd..893cdd9 100644 --- a/packaging/deviced.spec +++ b/packaging/deviced.spec @@ -158,6 +158,7 @@ Plugin libraries for IoT devices -DTIZEN_FEATURE_USBHOST_TEST=on \ -DTIZEN_FEATURE_BATTERY_OVER_TEMPERATURE=on \ -DTOUCH_SENSITIVITY_MODULE=on \ + -DDUMP_MODULE=on \ #eol %build diff --git a/src/core/devices.h b/src/core/devices.h index 8b7de5a..9e90422 100644 --- a/src/core/devices.h +++ b/src/core/devices.h @@ -58,6 +58,8 @@ struct device_ops { int (*execute) (void *data); void (*suspend) (void); void (*resume) (void); + int (*dump) (FILE *fp, int mode, void *dump_data); + void *dump_data; }; enum device_ops_status { diff --git a/src/dump/dump.c b/src/dump/dump.c new file mode 100644 index 0000000..f16e617 --- /dev/null +++ b/src/dump/dump.c @@ -0,0 +1,127 @@ +/* + * deviced + * + * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * + * 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 +#include + +#include "core/log.h" +#include "core/list.h" +#include "core/common.h" +#include "core/devices.h" +#include + +#define SERVICE_NAME "deviced" +#define DUMP_SIGNAL "Dump" +#define DUMP_START_SIGNAL "Start" +#define DUMP_FINISH_SIGNAL "Finish" +#define DEFAULT_DUMP_PATH "/var/log" + +static void send_dump_signal(char *signal) +{ + int ret; + pid_t pid; + + pid = getpid(); + + ret = dbus_handle_broadcast_dbus_signal_var(DUMP_SERVICE_OBJECT_PATH, + DUMP_SERVICE_INTERFACE_NAME, signal, + g_variant_new("(i)", pid)); + if (ret < 0) + _E("Failed to send dbus signal(%s)", signal); +} + +static void dump_all_devices(int mode, char *path) +{ + dd_list *head = get_device_list_head(); + dd_list *elem; + FILE *fp = NULL; + char fname[PATH_MAX]; + const struct device_ops *dev; + + /* send start signal to dump service */ + send_dump_signal(DUMP_START_SIGNAL); + + /* open saved file */ + snprintf(fname, PATH_MAX, "%s/%s.log", path, SERVICE_NAME); + + if (path) + fp = fopen(fname, "w+"); + if (!fp) + _I("Failed to open %s, print to DLOG", fname); + + /* save dump each device ops */ + DD_LIST_FOREACH(head, elem, dev) { + if (dev->dump) { + _D("[%s] get dump", dev->name); + LOG_DUMP(fp, "\n==== %s\n\n", dev->name); + dev->dump(fp, mode, dev->dump_data); + } + } + + /* close file */ + if (fp) + fclose(fp); + + /* send finish signal to dump service */ + send_dump_signal(DUMP_FINISH_SIGNAL); + + _D("%s(%d) dump is saved!", fname, mode); +} + +static void dump_signal_handler(GDBusConnection *conn, + const gchar *sender, + const gchar *path, + const gchar *iface, + const gchar *name, + GVariant *param, + gpointer data) + +{ + int mode = 0; + char *log_path = NULL; + + g_variant_get(param, "(is)", &mode, &log_path); + _D("mode(%d) path(%s)", mode, log_path); + + dump_all_devices(mode, log_path); + g_free(log_path); +} + +static void dump_init(void *data) +{ + int ret; + + /* register dump signal */ + ret = subscribe_dbus_signal(NULL, + DUMP_SERVICE_OBJECT_PATH, + DUMP_SERVICE_INTERFACE_NAME, + DUMP_SIGNAL, + dump_signal_handler, + NULL, NULL); + if (ret <= 0) + _E("Failed to register signal handler! %d", ret); +} + +static const struct device_ops dump_device_ops = { + .name = "dump", + .init = dump_init, +}; + +DEVICE_OPS_REGISTER(&dump_device_ops) + diff --git a/src/shared/log-macro.h b/src/shared/log-macro.h index 8532b37..cb309af 100644 --- a/src/shared/log-macro.h +++ b/src/shared/log-macro.h @@ -48,4 +48,7 @@ #define _SW(...) do { } while (0) #define _SE(...) do { } while (0) #endif + +#define LOG_DUMP(fp, fmt, arg...) \ + do { if (fp) fprintf(fp, fmt, ##arg); else _E(fmt, ##arg); } while(0) #endif diff --git a/src/touchscreen/touchscreen.c b/src/touchscreen/touchscreen.c index 64b62ca..1621c00 100644 --- a/src/touchscreen/touchscreen.c +++ b/src/touchscreen/touchscreen.c @@ -112,12 +112,33 @@ static int touchscreen_stop(enum device_flags flags) return touchscreen_set_state(TOUCHSCREEN_OFF); } +static int touchscreen_dump(FILE *fp, int mode, void *dump_data) +{ + int ret; + enum touchscreen_state state; + + if (!touchscreen_dev) + return 0; + + ret = touchscreen_dev->get_state(&state); + if (ret < 0) + _E("Failed to get touchscreen state : %d", ret); + + if (state == TOUCHSCREEN_ON) + LOG_DUMP(fp, "Touchscreen is enabled\n"); + else + LOG_DUMP(fp, "Touchscreen is disabled\n"); + + return 0; +} + static const struct device_ops touchscreen_device_ops = { .name = "touchscreen", .probe = touchscreen_probe, .exit = touchscreen_exit, .start = touchscreen_start, .stop = touchscreen_stop, + .dump = touchscreen_dump, }; DEVICE_OPS_REGISTER(&touchscreen_device_ops) -- 2.7.4 From a647a64ba1db2504683712aed7c5ec3f12185d18 Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Fri, 1 Feb 2019 17:13:56 +0900 Subject: [PATCH 07/16] Plugin Architecture - for device module adaptation Change-Id: I5aef481d44b13c9cd3f8529cc04596d56a683f1c Signed-off-by: Hyotaek Shim --- src/core/devices.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/core/devices.c b/src/core/devices.c index 6dba16c..c196d14 100644 --- a/src/core/devices.c +++ b/src/core/devices.c @@ -36,19 +36,25 @@ dd_list *get_device_list_head(void) return dev_head; } +void remove_device(const struct device_ops *dev) +{ + DD_LIST_REMOVE(dev_head, dev); +} + void add_device(const struct device_ops *dev) { + const struct device_ops *ops = NULL; + + ops = find_device(dev->name); + if (ops != &default_ops) + remove_device(dev); + if (dev->priority == DEVICE_PRIORITY_HIGH) DD_LIST_PREPEND(dev_head, dev); else DD_LIST_APPEND(dev_head, dev); } -void remove_device(const struct device_ops *dev) -{ - DD_LIST_REMOVE(dev_head, dev); -} - const struct device_ops *find_device(const char *name) { dd_list *elem; -- 2.7.4 From c38d08fe9b8af07a063802da311982ae58d56a8e Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Fri, 1 Feb 2019 17:32:24 +0900 Subject: [PATCH 08/16] Fix deviced-shutdown installation The location should be /usr/lib/systemd/deviced-shutdown (for both 32-bit and 64-bit build) Change-Id: Ic31de51f4b2f848d2602f1dbc42d34283cc0f624 Signed-off-by: Hyotaek Shim --- CMakeLists.txt | 2 +- packaging/deviced.spec | 5 ++--- src/core/devices.c | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 929c273..4e5afc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -235,7 +235,7 @@ IF(POWER_MODULE STREQUAL on) ADD_EXECUTABLE(deviced-shutdown src/power-shutdown/shutdown.c src/core/common.c src/core/execute.c) SET(deviced-shutdown_LDFLAGS ${pkgs_LDFLAGS}) TARGET_LINK_LIBRARIES(deviced-shutdown ${pkgs_LDFLAGS} "-lrt -ldl -lm" shared) - INSTALL(TARGETS deviced-shutdown DESTINATION ${LIB_INSTALL_DIR}/systemd) + INSTALL(TARGETS deviced-shutdown DESTINATION /usr/lib/systemd) ENDIF() INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/src/deviced/ DESTINATION include/${PROJECT_NAME} diff --git a/packaging/deviced.spec b/packaging/deviced.spec index 893cdd9..91a1bc8 100644 --- a/packaging/deviced.spec +++ b/packaging/deviced.spec @@ -194,7 +194,7 @@ fi %preun # Assume power module is on (-DPOWER_MODULE=on) -update-alternatives --remove %{_libdir}/systemd/systemd-shutdown systemd-shutdown %{_libdir}/systemd/deviced-shutdown +update-alternatives --remove %{_prefix}/lib/systemd/systemd-shutdown systemd-shutdown %{_prefix}/lib/systemd/deviced-shutdown %postun systemctl daemon-reload @@ -249,8 +249,7 @@ mv %{_libdir}/iot-display.so %{_libdir}/deviced/display.so %{_sysconfdir}/deviced/usb-host-test/strs # Assume power module is on (-DPOWER_MODULE=on) -%{_libdir}/systemd/deviced-shutdown - +%{_prefix}/lib/systemd/deviced-shutdown %files -n libdeviced %manifest deviced.manifest diff --git a/src/core/devices.c b/src/core/devices.c index c196d14..4785111 100644 --- a/src/core/devices.c +++ b/src/core/devices.c @@ -43,7 +43,7 @@ void remove_device(const struct device_ops *dev) void add_device(const struct device_ops *dev) { - const struct device_ops *ops = NULL; + const struct device_ops *ops; ops = find_device(dev->name); if (ops != &default_ops) -- 2.7.4 From fc0dad67162affbd10c7806961a19193015df9a0 Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Thu, 7 Feb 2019 18:18:15 +0900 Subject: [PATCH 09/16] Add device_notify_once() to avoid multiple invocation Change-Id: I355dbe2d937e5ebdc5ffa8c59228763900e34568 Signed-off-by: Hyotaek Shim --- src/core/device-notifier.c | 18 ++++++++++++++++++ src/power/boot.c | 16 +++++++--------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/core/device-notifier.c b/src/core/device-notifier.c index 0a50f87..a88554c 100644 --- a/src/core/device-notifier.c +++ b/src/core/device-notifier.c @@ -117,3 +117,21 @@ void device_notify(enum device_notifier_type status, void *data) if (!idl) idl = g_idle_add(delete_unused_notifier_cb, NULL); } + +void device_notify_once(enum device_notifier_type status, void *data) +{ + dd_list *n; + struct device_notifier *notifier; + + DD_LIST_FOREACH(device_notifier_list, n, notifier) { + if (!notifier->deleted && status == notifier->status) { + if (notifier->func) + notifier->func(data); + + notifier->deleted = true; + } + } + + if (!idl) + idl = g_idle_add(delete_unused_notifier_cb, NULL); +} diff --git a/src/power/boot.c b/src/power/boot.c index e0a5ed4..89bc808 100644 --- a/src/power/boot.c +++ b/src/power/boot.c @@ -29,13 +29,12 @@ #include "display/display-ops.h" #include "shared/deviced-systemd.h" -#define SIGNAL_BOOTING_DONE "BootingDone" - -#define SYSTEMD_DBUS_SIGNAL_SYSTEM_STARTUP_FINISHED "StartupFinished" -#define SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED "UserSessionStartupFinished" -#define SYSTEMD_DBUS_METHOD_SYSTEM_STATE "SystemState" -#define SYSTEMD_STATE_RUNNING "running" -#define SYSTEMD_STATE_DEGRADED "degraded" +#define SIGNAL_BOOTING_DONE "BootingDone" +#define SYSTEMD_DBUS_SIGNAL_SYSTEM_STARTUP_FINISHED "StartupFinished" +#define SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED "UserSessionStartupFinished" +#define SYSTEMD_DBUS_METHOD_SYSTEM_STATE "SystemState" +#define SYSTEMD_STATE_RUNNING "running" +#define SYSTEMD_STATE_DEGRADED "degraded" int booting_finished(void) { @@ -86,7 +85,6 @@ static void booting_done_received(GDBusConnection *conn, const gchar *name, GVariant *param, gpointer data) - { static int system_done = 0; static int user_done = 0; @@ -101,7 +99,7 @@ static void booting_done_received(GDBusConnection *conn, return; } _I("System Session is Ready"); - device_notify(DEVICE_NOTIFIER_BOOTING_DONE, &system_done); + device_notify_once(DEVICE_NOTIFIER_BOOTING_DONE, &system_done); } else if (strcmp(name, SYSTEMD_DBUS_SIGNAL_USER_STARTUP_FINISHED) == 0) { if (user_done) -- 2.7.4 From 53256c665e90251b688e277d6ea8673476d317cf Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Thu, 7 Feb 2019 18:40:59 +0900 Subject: [PATCH 10/16] Do not unnecessarily check booting-done Change-Id: I5680777ac0b907a6a8e394d1659e0e5b48d1192f Signed-off-by: Hyotaek Shim --- src/core/main.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index 64d44c8..a68db93 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -68,13 +68,6 @@ void watchdog_notify(void) static void deviced_dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data) { - int ret; - ret = booting_finished(); - if (ret == 1) { - _I("notify relaunch"); - device_notify(DEVICE_NOTIFIER_BOOTING_DONE, &ret); - } - _I("sd_notify(READY=1)"); sd_notify(0, "READY=1"); } -- 2.7.4 From a4906c41a99f9beda77989384f71f4ec0ddffdbb Mon Sep 17 00:00:00 2001 From: lokilee73 Date: Thu, 7 Feb 2019 18:51:25 +0900 Subject: [PATCH 11/16] Add notifier_device_ops for late or no booting done signal Systemd can't confirm booting done signal in valid time. So, add notifier_device_ops to broadcast booting done siganl after 30 seconds, if there is no booting done signal in it. Change-Id: I535f8a848239b75fcd8e92f1ff13cd8ce895868a Signed-off-by: lokilee73 --- src/core/device-notifier.c | 75 ++++++++++++++++++++++++++++++++++++++++++++++ src/core/device-notifier.h | 1 + 2 files changed, 76 insertions(+) diff --git a/src/core/device-notifier.c b/src/core/device-notifier.c index a88554c..369ac55 100644 --- a/src/core/device-notifier.c +++ b/src/core/device-notifier.c @@ -21,6 +21,8 @@ #include "device-notifier.h" #include "list.h" #include "common.h" +#include "core/devices.h" +#include struct device_notifier { bool deleted; @@ -30,7 +32,9 @@ struct device_notifier { static dd_list *device_notifier_list; static guint idl; +static guint late_init_timer; +#define LATE_INIT_TIME 30 #define FIND_NOTIFIER(a, b, d, e, f) \ DD_LIST_FOREACH(a, b, d) \ if (e == d->e && f == (d->f)) @@ -135,3 +139,74 @@ void device_notify_once(enum device_notifier_type status, void *data) if (!idl) idl = g_idle_add(delete_unused_notifier_cb, NULL); } + +static void late_init_stop(void) +{ + if (!late_init_timer) + return; + + g_source_remove(late_init_timer); + late_init_timer = 0; +} + +static int booting_done(void *data) +{ + static int done; + + if (data == NULL) + goto out; + + done = *(int *)data; + if (!late_init_timer) + return done; + late_init_stop(); + +out: + return done; +} + +static gboolean late_init_timer_cb(void *data) +{ + int done; + + late_init_stop(); + done = booting_done(NULL); + if (done) + return G_SOURCE_REMOVE; + _I("late booting done"); + done = TRUE; + device_notify_once(DEVICE_NOTIFIER_BOOTING_DONE, (void *)&done); + + return G_SOURCE_REMOVE; +} + +static void device_notifier_init(void *data) +{ + int ret; + + ret = check_systemd_active(); + if (ret == TRUE) { + _I("restart booting done"); + return; + } + register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done); + + late_init_timer = g_timeout_add_seconds(LATE_INIT_TIME, + late_init_timer_cb, NULL); + if (!late_init_timer) + _E("Failed to set late_init_timer"); +} + +static void device_notifier_exit(void *data) +{ + +} + +static const struct device_ops notifier_device_ops = { + .name = "notifier", + .init = device_notifier_init, + .exit = device_notifier_exit, +}; + +DEVICE_OPS_REGISTER(¬ifier_device_ops) + diff --git a/src/core/device-notifier.h b/src/core/device-notifier.h index 087edb3..cb0ce1f 100644 --- a/src/core/device-notifier.h +++ b/src/core/device-notifier.h @@ -85,5 +85,6 @@ typedef enum _device_notifier_state { int register_notifier(enum device_notifier_type status, int (*func)(void *data)); int unregister_notifier(enum device_notifier_type status, int (*func)(void *data)); void device_notify(enum device_notifier_type status, void *value); +void device_notify_once(enum device_notifier_type status, void *data); #endif /* __DEVICE_NOTIFIER_H__ */ -- 2.7.4 From 0cc6f7907522b916a9b99e1f11d61dd23114408e Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Fri, 8 Feb 2019 01:55:58 +0000 Subject: [PATCH 12/16] Revert "Do not unnecessarily check booting-done" This reverts commit 53256c665e90251b688e277d6ea8673476d317cf. Change-Id: Ic9ddcec6a9b7a2e7832901d296e3eb6d4a9b99d3 --- src/core/main.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/core/main.c b/src/core/main.c index a68db93..64d44c8 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -68,6 +68,13 @@ void watchdog_notify(void) static void deviced_dbus_name_acquired(GDBusConnection *connection, const gchar *name, gpointer user_data) { + int ret; + ret = booting_finished(); + if (ret == 1) { + _I("notify relaunch"); + device_notify(DEVICE_NOTIFIER_BOOTING_DONE, &ret); + } + _I("sd_notify(READY=1)"); sd_notify(0, "READY=1"); } -- 2.7.4 From 1f75d0dda57f0c4aaa52f12ed2662afc67886a5f Mon Sep 17 00:00:00 2001 From: Hyotaek Shim Date: Fri, 8 Feb 2019 11:02:25 +0900 Subject: [PATCH 13/16] Apply device_notify_once() Change-Id: Ic56422e699bd3b1bae9174d31b271c40c7c5e907 Signed-off-by: Hyotaek Shim --- src/core/main.c | 8 ++++---- src/power/power-handler.c | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index 64d44c8..a2ece6b 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -71,8 +71,9 @@ static void deviced_dbus_name_acquired(GDBusConnection *connection, const gchar int ret; ret = booting_finished(); if (ret == 1) { + /* Restarted: deviced was terminated */ _I("notify relaunch"); - device_notify(DEVICE_NOTIFIER_BOOTING_DONE, &ret); + device_notify_once(DEVICE_NOTIFIER_BOOTING_DONE, &ret); } _I("sd_notify(READY=1)"); @@ -94,9 +95,8 @@ static int deviced_main(int argc, char **argv) ret = check_power_flag(); if (ret) { - /* assume reboot was requested and deviced was - * killed/terminated in middle of it - resume - * procedure + /* Restarted: deviced was terminated + * in middle of reboot/poweroff - resume procedure */ poweroff_procedure(); return 0; diff --git a/src/power/power-handler.c b/src/power/power-handler.c index a084cc7..9b82c50 100644 --- a/src/power/power-handler.c +++ b/src/power/power-handler.c @@ -259,7 +259,6 @@ static void poweroff_direct(void) int poweroff_procedure(void) { - powerdown(); if (poweroff_opt.type == POWER_OFF_DIRECT) -- 2.7.4 From 7f68002e284654b16cb74a90942f6ce220630324 Mon Sep 17 00:00:00 2001 From: "sanghyeok.oh" Date: Tue, 15 Jan 2019 08:35:12 +0900 Subject: [PATCH 14/16] dbus: systemd: change api - start/stop systemd unit move systemd related dbus api to libgdbus(libsyscommon) Change-Id: I6ae0c125dc5293fbc38df7ca3a0ba7cde12a8f38 Signed-off-by: sanghyeok.oh --- src/power/boot.c | 4 ++-- src/power/power-handler.c | 8 ++++---- src/shared/CMakeLists.txt | 1 - src/usb-host-test/usb-host-test.c | 8 ++++---- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/power/boot.c b/src/power/boot.c index 89bc808..413a968 100644 --- a/src/power/boot.c +++ b/src/power/boot.c @@ -21,13 +21,13 @@ #include #include #include +#include #include "core/log.h" #include "core/device-notifier.h" #include "core/common.h" #include "display/poll.h" #include "display/display-ops.h" -#include "shared/deviced-systemd.h" #define SIGNAL_BOOTING_DONE "BootingDone" #define SYSTEMD_DBUS_SIGNAL_SYSTEM_STARTUP_FINISHED "StartupFinished" @@ -43,7 +43,7 @@ int booting_finished(void) size_t len; GVariant *reply = NULL; - reply = deviced_systemd_get_manager_property(SYSTEMD_DBUS_METHOD_SYSTEM_STATE); + reply = systemd_get_manager_property(SYSTEMD_DBUS_METHOD_SYSTEM_STATE); if (!reply) { _E("Failed to get System State: no reply"); goto err; diff --git a/src/power/power-handler.c b/src/power/power-handler.c index 9b82c50..589dd02 100644 --- a/src/power/power-handler.c +++ b/src/power/power-handler.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "dd-deviced.h" #include "core/log.h" @@ -53,7 +54,6 @@ #include "display/display-ops.h" #include "power-handler.h" #include "apps/apps.h" -#include "shared/deviced-systemd.h" #include "boot.h" #define POWEROFF_DURATION 2 @@ -109,7 +109,7 @@ static void poweroff_start_animation(void) { int ret; - ret = deviced_systemd_start_unit("shutdown-animation.service"); + ret = systemd_start_unit_async("shutdown-animation.service", NULL); if (ret < 0) _E("Failed to start shutdown animation"); @@ -154,13 +154,13 @@ static int disable_systemd_journald(void) { int ret; - ret = deviced_systemd_stop_unit("systemd-journald.socket"); + ret = systemd_stop_unit_async("systemd-journald.socket", NULL); if (ret < 0) { _E("failed to stop 'systemd-journald.socket'"); return ret; } - ret |= deviced_systemd_stop_unit("systemd-journald.service"); + ret = systemd_stop_unit_async("systemd-journald.service", NULL); if (ret < 0) { _E("failed to stop 'systemd-journald.service'"); return ret; diff --git a/src/shared/CMakeLists.txt b/src/shared/CMakeLists.txt index 2ad9dc1..67290f9 100644 --- a/src/shared/CMakeLists.txt +++ b/src/shared/CMakeLists.txt @@ -1,7 +1,6 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) SET(SHARED_SRCS - deviced-systemd.c plugin.c ) diff --git a/src/usb-host-test/usb-host-test.c b/src/usb-host-test/usb-host-test.c index e83811b..7ea07c8 100644 --- a/src/usb-host-test/usb-host-test.c +++ b/src/usb-host-test/usb-host-test.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "core/log.h" #include "core/config-parser.h" @@ -30,7 +31,6 @@ #include "core/device-notifier.h" #include "core/devices.h" #include "core/list.h" -#include "shared/deviced-systemd.h" #define FFS_PATH "/run/usb-host-test-ffs" #define GADGET_SCHEME_PATH "/etc/deviced/usb-host-test/test_gadget.gs" @@ -409,7 +409,7 @@ int start() _E("fail to init dbus signal(%d)", ret); sigid_start = ret; - ret = deviced_systemd_start_unit(SYSTEMD_UNIT_NAME); + ret = systemd_start_unit_sync(SYSTEMD_UNIT_NAME, NULL, -1); if (ret < 0) { _E("Error starting daemon"); /*unregister_dbus_signal_handler(SYSTEMD_DBUS_PATH, @@ -455,13 +455,13 @@ static int stop() sigid_stop = ret; - ret = deviced_systemd_stop_unit(SYSTEMD_UNIT_NAME); + ret = systemd_stop_unit_sync(SYSTEMD_UNIT_NAME, NULL, -1); if (ret < 0) { _E("could not stop socket unit"); goto out; } - ret = deviced_systemd_stop_unit(SYSTEMD_SERVICE_NAME); + ret = systemd_stop_unit_sync(SYSTEMD_SERVICE_NAME, NULL, -1); if (ret < 0) { _E("could not stop service unit"); goto out; -- 2.7.4 From 386706aea1c051d9b19f5d4b3eb93e99482928f5 Mon Sep 17 00:00:00 2001 From: lokilee73 Date: Thu, 27 Dec 2018 20:42:01 +0900 Subject: [PATCH 15/16] Add dbus_get_temperature for method, GetTemperature Change-Id: I732241d0d3c57deba03e0beaf6caf949ddf7009f Signed-off-by: lokilee73 --- src/thermal/thermal.c | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/thermal/thermal.c b/src/thermal/thermal.c index 794b186..d9a5e9f 100644 --- a/src/thermal/thermal.c +++ b/src/thermal/thermal.c @@ -21,6 +21,7 @@ #include #include +#include #include "apps/apps.h" #include "core/devices.h" @@ -343,6 +344,40 @@ static int thermal_service_stop(void) return ret; } +static GVariant *dbus_get_temperature(GDBusConnection *conn, + const gchar *sender, const gchar *path, const gchar *iface, const gchar *name, + GVariant *param, GDBusMethodInvocation *invocation, gpointer user_data) +{ + struct thermal_info info; + int index, ret = 0; + + g_variant_get(param, "(i)", &index); + + if (!thermal_dev || !thermal_dev->get_info) { + ret = -ENODEV; + goto exit; + } + + ret = thermal_dev->get_info((device_thermal_e)index, &info); + if (!ret) + ret = info.temp; + +exit: + return g_variant_new("(i)", ret); +} + +static const dbus_method_s dbus_methods[] = { + { "GetTemperature", "i", "i", dbus_get_temperature}, + /* Add methods here */ +}; + +static const dbus_interface_u dbus_interface = { + .oh = NULL, + .name = DEVICED_INTERFACE_TEMPERATURE, + .methods = dbus_methods, + .nr_methods = ARRAY_SIZE(dbus_methods), +}; + static int booting_done(void *data) { static int done; @@ -402,6 +437,10 @@ static void thermal_init(void *data) ret = register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done); if (ret < 0) _E("Fail to register booting done Notifier"); + + ret = dbus_handle_add_dbus_object(NULL, DEVICED_PATH_TEMPERATURE, &dbus_interface); + if (ret < 0) + _E("fail to init dbus method(%d)", ret); } static void thermal_exit(void *data) -- 2.7.4 From 90cc6072381e4e7fcffb43c1f0cda4069f913818 Mon Sep 17 00:00:00 2001 From: Karol Lewandowski Date: Thu, 14 Feb 2019 16:47:00 +0100 Subject: [PATCH 16/16] power: Remove erronous DIRECT poweroff type Commit 51849e1cad ("power: Rework handling of repeated poweroff/reboot request") mistakenly introduced POWEROFF type. Before that commit "DIRECT" type was used to mean POWEROFF. This commit reverts to original behaviour, dropping DIRECT type for all new uses but retains constant for compatiblity. Change-Id: I4ddd5fa7fe45dd54bd9a597ed36b989b93a2f3f8 --- src/core/main.c | 2 +- src/power/power-handler.c | 83 +++++++++++++++++------------------------------ src/power/power-handler.h | 4 +-- 3 files changed, 32 insertions(+), 57 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index a2ece6b..9ff0cd1 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -98,7 +98,7 @@ static int deviced_main(int argc, char **argv) /* Restarted: deviced was terminated * in middle of reboot/poweroff - resume procedure */ - poweroff_procedure(); + powerdown(); return 0; } diff --git a/src/power/power-handler.c b/src/power/power-handler.c index 589dd02..f0fa00f 100644 --- a/src/power/power-handler.c +++ b/src/power/power-handler.c @@ -74,14 +74,13 @@ static bool during_poweroff; static dd_list *poweroff_handles; static char *poweroff_type_flagpaths[] = { // index denotes type - [POWER_OFF_DIRECT] = POWER_FLAG_POWEROFF, + [POWER_OFF_POWEROFF] = POWER_FLAG_POWEROFF, [POWER_OFF_REBOOT] = POWER_FLAG_REBOOT, }; static char *poweroff_type_names[] = { // index denotes type [POWER_OFF_POWEROFF] = POWER_POWEROFF, [POWER_OFF_POPUP] = PWROFF_POPUP, [POWER_OFF_REBOOT] = POWER_REBOOT, - [POWER_OFF_DIRECT] = "", }; static char *poweroff_type_to_name(enum poweroff_type type) @@ -189,7 +188,31 @@ static bool disable_coredump_handler(void) return is_ok; } -static void powerdown(void) +static void poweroff_request_shutdown() +{ + _I("Requested power off via systemd"); + + char *method; + + if (poweroff_opt.type == POWER_OFF_REBOOT) + method = "Reboot"; + else if (poweroff_opt.type == POWER_OFF_POWEROFF) + method = "PowerOff"; + else { + _E("poweroff invalid type (%d)", poweroff_opt.type); + return; + } + + dbus_handle_method_sync_timeout(SYSTEMD_DBUS_DEST, + SYSTEMD_DBUS_PATH, + SYSTEMD_DBUS_IFACE_MANAGER, + method, + NULL, + NULL, + POWEROFF_WAIT_SYSTEMD_MS); +} + +void powerdown(void) { static int wait; struct timeval now; @@ -223,50 +246,8 @@ static void powerdown(void) } watchdog_notify(); -} - -static void poweroff_request_shutdown() -{ - _I("Requested power off via systemd"); - - char *method; - if (poweroff_opt.type == POWER_OFF_REBOOT) - method = "Reboot"; - else if (poweroff_opt.type == POWER_OFF_POWEROFF) - method = "PowerOff"; - else { - _E("poweroff invalid type (%d)", poweroff_opt.type); - return; - } - - dbus_handle_method_sync_timeout(SYSTEMD_DBUS_DEST, - SYSTEMD_DBUS_PATH, - SYSTEMD_DBUS_IFACE_MANAGER, - method, - NULL, - NULL, - POWEROFF_WAIT_SYSTEMD_MS); -} - -static void poweroff_direct(void) -{ - _I("Requested power off without systemd (direct call)"); - if (syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, - LINUX_REBOOT_CMD_POWER_OFF, NULL) < 0) - _E("Failed to poweroff (%d)", errno); -} - -int poweroff_procedure(void) -{ - powerdown(); - - if (poweroff_opt.type == POWER_OFF_DIRECT) - poweroff_direct(); - else - poweroff_request_shutdown(); - - return 0; + poweroff_request_shutdown(); } int check_power_flag(void) @@ -336,10 +317,8 @@ static void poweroff_remove_handle(pid_t pid) static gboolean poweroff_timeout_cb(void *data) { - int ret; - pid_t pid; + pid_t pid = (pid_t)((intptr_t)data); - pid = (pid_t)((intptr_t)data); poweroff_remove_handle(pid); if (DD_LIST_LENGTH(poweroff_handles)) { @@ -368,9 +347,7 @@ static gboolean poweroff_timeout_cb(void *data) /* TODO for notify. will be removed asap. */ vconf_set_int(VCONFKEY_SYSMAN_POWER_OFF_STATUS, poweroff_opt.type); - ret = poweroff_procedure(); - if (ret < 0) - _E("Failed to %s (%d)", name, ret); + powerdown(); out: if (disp_plgn.update_pm_setting) @@ -488,7 +465,6 @@ static void poweroff_send_broadcast(int status) g_variant_new("(i)", status)); } - static int power_execute_pid(char *type, char *option, pid_t pid) { if (during_poweroff) { @@ -790,7 +766,6 @@ static void power_init(void *data) register_notifier(DEVICE_NOTIFIER_BOOTING_DONE, booting_done); add_poweroff_option(POWER_OFF_POWEROFF, NULL); - add_poweroff_option(POWER_OFF_DIRECT, NULL); add_poweroff_option(POWER_OFF_RESTART, NULL); add_poweroff_option(POWER_OFF_POPUP, NULL); diff --git a/src/power/power-handler.h b/src/power/power-handler.h index 968e17a..551356c 100644 --- a/src/power/power-handler.h +++ b/src/power/power-handler.h @@ -36,9 +36,9 @@ enum poweroff_type { POWER_OFF_NONE = POWER_OFF_INVALID, // compat only POWER_OFF_POPUP, POWER_OFF_POWEROFF, /* replaces POWER_OFF_DIRECT */ + POWER_OFF_DIRECT = POWER_OFF_POWEROFF, // compat only POWER_OFF_REBOOT, POWER_OFF_RESTART = POWER_OFF_REBOOT, // compat only - POWER_OFF_DIRECT, }; struct power_option { @@ -53,6 +53,6 @@ struct poweroff_handle { }; int check_power_flag(void); -int poweroff_procedure(void); +void powerdown(void); #endif /* __POWER_HANDLE_H__ */ -- 2.7.4