From 1c02cbdcb58e95b4f6d0f757c5d933094dc31142 Mon Sep 17 00:00:00 2001 From: Taeyoung Kim Date: Fri, 15 Nov 2013 13:56:28 +0900 Subject: [PATCH] Initial commit for Resourced Signed-off-by: Taeyoung Kim Change-Id: I9c06df974efabf248bc78e0292f33eb0cd193fc1 --- CMakeLists.txt | 8 + CMakeLists/init-cmake-projects.txt | 4 + CMakeLists/init-needed-environment.txt | 73 ++++++ CMakeLists/resourced.txt | 61 +++++ LICENSE | 204 +++++++++++++++ include/resourced.h | 90 +++++++ packaging/resourced.service | 14 + packaging/resourced.spec | 91 +++++++ resourced.conf | 10 + resourced.manifest | 5 + resourced.pc.in | 10 + src/common/cgroup.h | 58 +++++ src/common/config-parser.c | 127 +++++++++ src/common/config-parser.h | 40 +++ src/common/config.h.in | 14 + src/common/edbus-handler.c | 461 +++++++++++++++++++++++++++++++++ src/common/edbus-handler.h | 101 ++++++++ src/common/file-helper.c | 116 +++++++++ src/common/file-helper.h | 48 ++++ src/common/macro.h | 122 +++++++++ src/common/module.c | 93 +++++++ src/common/module.h | 50 ++++ src/common/trace.h | 82 ++++++ src/resourced/init.c | 132 ++++++++++ src/resourced/init.h | 41 +++ src/resourced/main.c | 55 ++++ 26 files changed, 2110 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 CMakeLists/init-cmake-projects.txt create mode 100644 CMakeLists/init-needed-environment.txt create mode 100644 CMakeLists/resourced.txt create mode 100644 LICENSE create mode 100644 include/resourced.h create mode 100644 packaging/resourced.service create mode 100644 packaging/resourced.spec create mode 100644 resourced.conf create mode 100644 resourced.manifest create mode 100644 resourced.pc.in create mode 100644 src/common/cgroup.h create mode 100644 src/common/config-parser.c create mode 100644 src/common/config-parser.h create mode 100644 src/common/config.h.in create mode 100644 src/common/edbus-handler.c create mode 100644 src/common/edbus-handler.h create mode 100644 src/common/file-helper.c create mode 100644 src/common/file-helper.h create mode 100644 src/common/macro.h create mode 100644 src/common/module.c create mode 100644 src/common/module.h create mode 100644 src/common/trace.h create mode 100644 src/resourced/init.c create mode 100644 src/resourced/init.h create mode 100644 src/resourced/main.c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..c1bf9b2 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,8 @@ +CMAKE_MINIMUM_REQUIRED(VERSION 2.6) + +INCLUDE(${CMAKE_SOURCE_DIR}/CMakeLists/init-needed-environment.txt) + +INSTALL(FILES ${CMAKE_SOURCE_DIR}/${fw_name}.pc DESTINATION lib/pkgconfig) +INSTALL(FILES ${CMAKE_SOURCE_DIR}/resourced.conf DESTINATION /etc/dbus-1/system.d) + +INCLUDE(${CMAKE_SOURCE_DIR}/CMakeLists/init-cmake-projects.txt) diff --git a/CMakeLists/init-cmake-projects.txt b/CMakeLists/init-cmake-projects.txt new file mode 100644 index 0000000..2cb19e0 --- /dev/null +++ b/CMakeLists/init-cmake-projects.txt @@ -0,0 +1,4 @@ +FILE(MAKE_DIRECTORY ${RESOURCED}-cmake) +CONFIGURE_FILE(${CMAKELISTS_DIR}/${RESOURCED}.txt ${RESOURCED}-cmake/CMakeLists.txt COPYONLY) + +ADD_SUBDIRECTORY(${CMAKE_SOURCE_DIR}/${RESOURCED}-cmake) diff --git a/CMakeLists/init-needed-environment.txt b/CMakeLists/init-needed-environment.txt new file mode 100644 index 0000000..ff584bb --- /dev/null +++ b/CMakeLists/init-needed-environment.txt @@ -0,0 +1,73 @@ +SET(fw_name "resourced") +SET(RESOURCED resourced) +SET(LIBRESOURCED lib${RESOURCED}) +SET(LIBS libs) +SET(PROC-STAT proc-stat) +PROJECT(${fw_name}) + +#Set modules, all modules are enabled by default +IF(NOT DEFINED MEMORY_MODULE) + SET(MEMORY_MODULE "ON") +ENDIF() + +#Set visibility +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Werror -fvisibility=hidden") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror -fvisibility=hidden") + +#Set as-needed +SET(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--as-needed") +SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--as-needed") + +#Set debug-mode if needed +IF("${CMAKE_BUILD_TYPE}" STREQUAL "DEBUG") + STRING(REGEX REPLACE "-O2" "" CMAKE_C_FLAGS ${CMAKE_C_FLAGS}) + SET(DEBUG_ENABLED 1) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall -O0 -g -pg") + SET(CMAKE_VERBOSE_MAKEFILE ON) + SET(VERBOSE 1) +ELSE() +#set compile size optimization option in case of none DEBUG + SET(ADDITIONAL_OFLAGS "-fdata-sections -ffunction-sections -Wl,--gc-sections -fno-exceptions") + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${ADDITIONAL_OFLAGS}") +SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${ADDITIONAL_OFLAGS}") +ENDIF() + +SET(PREFIX ${CMAKE_INSTALL_PREFIX}) + +#following section is needed for pkg-config *.in file +SET(LIBDIR ${PREFIX}/lib) +SET(INCLUDEDIR ${PREFIX}/include) +SET(PC_NAME ${fw_name}) +SET(PC_REQUIRED "glib-2.0 + vconf + vconf-internal-keys + sqlite3 + dlog + edbus") + +SET(PC_PROVIDED_LIBS "-l${PROC-STAT} -l${RESOURCED}") + +SET(PC_CFLAGS -I\${includedir}/system) +SET(VERSION ${FULLVER}) + +CONFIGURE_FILE( + ${fw_name}.pc.in + ${CMAKE_SOURCE_DIR}/${fw_name}.pc + @ONLY +) + +#init variables with sources +SET(DATA_DIR ${CMAKE_SOURCE_DIR}/data) +SET(CMAKELISTS_DIR ${CMAKE_SOURCE_DIR}/CMakeLists) +SET(INCLUDE_COMMON_DIR ${CMAKE_SOURCE_DIR}/src/common) +SET(INCLUDE_PUBLIC_DIR ${CMAKE_SOURCE_DIR}/include) +SET(RESOURCED_INCLUDEDIR ${INCLUDE_COMMON_DIR} ${INCLUDE_PUBLIC_DIR}) + +SET(SOURCE_DIR ${CMAKE_SOURCE_DIR}/src) +SET(RESOURCED_SOURCE_DIR ${SOURCE_DIR}/resourced) +SET(PROC-STAT_SOURCE_DIR ${SOURCE_DIR}/proc-stat) +SET(MEMORY_SOURCE_DIR ${SOURCE_DIR}/memory) +SET(MODULES_SOURCE_DIR ${SOURCE_DIR}/modules) +SET(CGROUP_SOURCE_DIR ${SOURCE_DIR}/cgroup) +SET(APP-STAT_SOURCE_DIR ${SOURCE_DIR}/app-stat) +SET(COMMON_SOURCE_DIR ${SOURCE_DIR}/common) diff --git a/CMakeLists/resourced.txt b/CMakeLists/resourced.txt new file mode 100644 index 0000000..0bb1caa --- /dev/null +++ b/CMakeLists/resourced.txt @@ -0,0 +1,61 @@ +CMAKE_MINIMUM_REQUIRED (VERSION 2.6) + +PROJECT(resourced) + +SET(CMAKE_EXTRA_INCLUDE_FILES unistd.h) + +INCLUDE_DIRECTORIES(${RESOURCED_INCLUDEDIR} + ${RESOURCED_SOURCE_DIR} +) + +CONFIGURE_FILE(${INCLUDE_COMMON_DIR}/config.h.in + ${INCLUDE_COMMON_DIR}/config.h) + +SET (HEADERS + ${INCLUDE_COMMON_DIR}/config.h + ${INCLUDE_COMMON_DIR}/config-parser.h + ${INCLUDE_COMMON_DIR}/macro.h + ${INCLUDE_COMMON_DIR}/module.h +) + +SET (SOURCES + ${COMMON_SOURCE_DIR}/config-parser.c + ${COMMON_SOURCE_DIR}/edbus-handler.c + ${COMMON_SOURCE_DIR}/file-helper.c + ${COMMON_SOURCE_DIR}/module.c + ${RESOURCED_SOURCE_DIR}/init.c + ${RESOURCED_SOURCE_DIR}/main.c + ) + +SET (REQUIRES_LIST ${REQUIRES_LIST} + ecore + dlog + glib-2.0 + sqlite3 + vconf + vconf-internal-keys + x11 + ecore-x + ecore-input + ecore-file + evas + edbus) + +INCLUDE(FindPkgConfig) +pkg_check_modules(daemon_pkgs REQUIRED ${REQUIRES_LIST}) + +FOREACH(flag ${daemon_pkgs_CFLAGS}) + SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} ${flag} -pthread") +ENDFOREACH(flag) + +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") + +ADD_EXECUTABLE (${PROJECT_NAME} ${HEADERS} ${SOURCES}) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} + ${daemon_pkgs_LDFLAGS}) + +SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES SKIP_BUILD_RPATH true) +INSTALL(FILES ${PROJECT_NAME} + DESTINATION ${MAKE_INSTALL_PREFIX}/usr/bin RENAME resourced + PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE WORLD_EXECUTE) +INSTALL(FILES ${INCLUDE_PUBLIC_DIR}/resourced.h DESTINATION include/system) diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5554685 --- /dev/null +++ b/LICENSE @@ -0,0 +1,204 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + 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. + + + diff --git a/include/resourced.h b/include/resourced.h new file mode 100644 index 0000000..fbb8754 --- /dev/null +++ b/include/resourced.h @@ -0,0 +1,90 @@ +/* + * resourced + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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 _RESOURCED_H_ +#define _RESOURCED_H_ + +#include +#include + +#define RESOURCED_ALL_APP "RESOURCED_ALL_APPLICATION_IDENTIFIER" +#define TETHERING_APP_NAME "RESOURCED_TETHERING_APPLICATION_IDENTIFIER" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifndef API +#define API __attribute__ ((visibility("default"))) +#endif + +/** + * @brief State of the monitored process + */ +typedef enum { + RESOURCED_STATE_UNKNOWN = 0, + RESOURCED_STATE_FOREGROUND = 1 << 1, /** < foreground state */ + RESOURCED_STATE_BACKGROUND = 1 << 2, /** < background state */ + RESOURCED_STATE_LAST_ELEM = 1 << 3 +} resourced_state_t; + +/** + * @brief return code of the rsml's function + */ +typedef enum { + RESOURCED_ERROR_NONMONITOR = -9, /** < Process don't show watchdog popup */ + RESOURCED_ERROR_NOTIMPL = -7, /**< Not implemented yet error */ + RESOURCED_ERROR_UNINITIALIZED = -6, /**< Cgroup doen't + mounted or daemon not started */ + RESOURCED_ERROR_NO_DATA = -5, /**< Success, but no data */ + RESOURCED_ERROR_INVALID_PARAMETER = -4,/**< Invalid parameter */ + RESOURCED_ERROR_OUT_OF_MEMORY = -3, /**< Out of memory */ + RESOURCED_ERROR_DB_FAILED = -2, /**< Database error */ + RESOURCED_ERROR_FAIL = -1, /**< General error */ + RESOURCED_ERROR_OK = 0 /**< General success */ +} resourced_ret_c; + +#define RESOURCED_ERROR_NONE RESOURCED_ERROR_OK + +/** + * @desc After invoking this function, application will be in + * the monitored scope. + * @details It creates an appropriate cgroup, + * It creates a unit file for the systemd. + * @param app_id[in] - application identifier, it's package name now + * @param pid - pid to put in to cgroup, or self pid of 0 + * @return 0 if success or error code + */ +resourced_ret_c join_app_performance(const char *app_id, const pid_t pid); + +/** + * @desc Description of the boolean option for enabling/disabling + * some behaviar + */ +typedef enum { + RESOURCED_OPTION_UNDEF, + RESOURCED_OPTION_ENABLE, + RESOURCED_OPTION_DISABLE +} resourced_option_state; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* _RESOURCED_H_ */ diff --git a/packaging/resourced.service b/packaging/resourced.service new file mode 100644 index 0000000..9c6ee5c --- /dev/null +++ b/packaging/resourced.service @@ -0,0 +1,14 @@ +[Unit] +Description=Resource management daemon +After=tizen-system.target +Wants=tizen-system.target + +[Service] +Type=simple +EnvironmentFile=/run/tizen-mobile-env +ExecStart=/usr/bin/resourced -s 1 -u 60 -c swapon +Restart=always +RestartSec=0 + +[Install] +WantedBy=multi-user.target diff --git a/packaging/resourced.spec b/packaging/resourced.spec new file mode 100644 index 0000000..a6c451e --- /dev/null +++ b/packaging/resourced.spec @@ -0,0 +1,91 @@ +Name: resourced +Summary: System Resource Daemon +Version: 0.0.1 +Release: 1 +Group: System/Management +License: Apache-2.0 +Source0: %{name}-%{version}.tar.gz +Source1: resourced.service + +BuildRequires: cmake +BuildRequires: pkgconfig(glib-2.0) +BuildRequires: pkgconfig(dlog) +BuildRequires: pkgconfig(sqlite3) +BuildRequires: pkgconfig(vconf) +BuildRequires: pkgconfig(vconf-internal-keys) +BuildRequires: pkgconfig(ecore) +BuildRequires: pkgconfig(x11) +BuildRequires: pkgconfig(ecore-x) +BuildRequires: pkgconfig(utilX) +BuildRequires: pkgconfig(ecore-input) +BuildRequires: pkgconfig(ecore-file) +BuildRequires: pkgconfig(edbus) + +Requires(post): /sbin/ldconfig +Requires(postun): /sbin/ldconfig + +%description +System Resource Daemon to manage memory and process state + +%package devel +Summary: System Resource Information (Development) +Group: System/Development +Requires: %{name} = %{version}-%{release} + +%description devel +Development package for Resourced Daemon +to manage memory and process state + +%prep +%setup -q + +%build +MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'` +MINORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $2}'` +PATCHVER=`echo %{version} | awk 'BEGIN {FS="."}{print $3}'` +echo "\ +/* That file was generated automaticaly. Don't edit it */ +#define MINOR_VERSION ${MINORVER} +#define MAJOR_VERSION ${MAJORVER} +#define PATCH_VERSION ${PATCHVER}" > src/common/version.h + +%if 0%{?tizen_build_binary_release_type_eng} + CFLAGS+=" -DTIZEN_ENGINEER_MODE" +%endif + +cmake . -DCMAKE_INSTALL_PREFIX=/usr -DFULLVER=%{version} -DCMAKE_BUILD_TYPE=Release + +make %{?jobs:-j%jobs} + +%install +rm -rf %{buildroot} +mkdir -p %{buildroot}/usr/share/license +cp -f LICENSE %{buildroot}/usr/share/license/%{name} + +%make_install + +mkdir -p %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants +install -m 0644 %SOURCE1 %{buildroot}%{_libdir}/systemd/system/resourced.service +ln -s ../resourced.service %{buildroot}%{_libdir}/systemd/system/multi-user.target.wants/resourced.service + +%post -p /sbin/ldconfig + +mkdir -p /opt/usr/etc + +if [ "$1" = "2" ]; then # upgrade begins + systemctl start resourced.service +fi + +%postun -p /sbin/ldconfig + +%files +%manifest resourced.manifest +/usr/share/license/%{name} +%attr(-,root, root) %{_bindir}/resourced +%config %{_sysconfdir}/dbus-1/system.d/resourced.conf +%{_libdir}/systemd/system/resourced.service +%{_libdir}/systemd/system/multi-user.target.wants/resourced.service + +%files devel +%{_libdir}/pkgconfig/*.pc +%{_includedir}/system/resourced.h diff --git a/resourced.conf b/resourced.conf new file mode 100644 index 0000000..12b9307 --- /dev/null +++ b/resourced.conf @@ -0,0 +1,10 @@ + + + + + + + + + diff --git a/resourced.manifest b/resourced.manifest new file mode 100644 index 0000000..f5a44ec --- /dev/null +++ b/resourced.manifest @@ -0,0 +1,5 @@ + + + + + diff --git a/resourced.pc.in b/resourced.pc.in new file mode 100644 index 0000000..44fb005 --- /dev/null +++ b/resourced.pc.in @@ -0,0 +1,10 @@ +libdir=@LIBDIR@ +includedir=@INCLUDEDIR@ + +Name: @PC_NAME@ +Description: @PACKAGE_DESCRIPTION@ +Version: @VERSION@ +Requires: @PC_REQUIRED@ +Libs: -L${libdir} @PC_PROVIDED_LIBS@ +Cflags: @PC_CFLAGS@ + diff --git a/src/common/cgroup.h b/src/common/cgroup.h new file mode 100644 index 0000000..4883133 --- /dev/null +++ b/src/common/cgroup.h @@ -0,0 +1,58 @@ +/* + * resourced + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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 + +/* + * Cgroup creation interface + */ + +#ifndef _CGROUP_LIBRARY_CGROUP_H_ +#define _CGROUP_LIBRARY_CGROUP_H_ + +/** + * @desc Make net_cls cgroup and put in it the given pid and + * generated classid. + * If cgroup alreay exists function just put pid in it. + * @param pid - process, that will be added to cgroup pkg_name, + * @param pkg_name - package name. + */ +resourced_ret_c make_net_cls_cgroup_with_pid(const int pid, + const char *pkg_name); + +/** + * @desc take classid from net_cls cgroup by appid + * This function converts appid to pkgname. + * @param pkg_name - name of the cgroup + * @param create - in case of true - create cgroup if it's not exists + * @return classid + */ +u_int32_t get_classid_by_app_id(const char *app_id, int create); + + +/** + * @desc take classid from net_cls cgroup with name pkg_name + * @param pkg_name - name of the cgroup + * @param create - in case of true - create cgroup if it's not exists + * @return classid + */ +u_int32_t get_classid_by_pkg_name(const char *pkg_name, int create); + +#endif /*_CGROUP_LIBRARY_CGROUP_H_*/ diff --git a/src/common/config-parser.c b/src/common/config-parser.c new file mode 100644 index 0000000..e184a5b --- /dev/null +++ b/src/common/config-parser.c @@ -0,0 +1,127 @@ +/* + * resourced + * + * Copyright (c) 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 +#include +#include "trace.h" +#include "config-parser.h" + +#define MAX_LINE 128 +#define MAX_SECTION 64 +#define WHITESPACE " \t" +#define NEWLINE "\n\r" +#define COMMENT '#' + +static inline char *trim_str(char *s) +{ + char *t; + /* left trim */ + s += strspn(s, WHITESPACE); + + /* right trim */ + for (t = strchr(s, 0); t > s; t--) + if (!strchr(WHITESPACE, t[-1])) + break; + *t = 0; + return s; +} + +int config_parse(const char *file_name, int cb(struct parse_result *result, + void *user_data), void *user_data) +{ + FILE *f = NULL; + struct parse_result result; + /* use stack for parsing */ + char line[MAX_LINE]; + char section[MAX_SECTION]; + char *start, *end, *name, *value; + int lineno = 0, ret = 0; + + if (!file_name || !cb) { + ret = -EINVAL; + goto error; + } + + /* open conf file */ + f = fopen(file_name, "r"); + if (!f) { + _E("Failed to open file %s", file_name); + ret = -EIO; + goto error; + } + + /* parsing line by line */ + while (fgets(line, MAX_LINE, f) != NULL) { + lineno++; + + start = line; + start[strcspn(start, NEWLINE)] = '\0'; + start = trim_str(start); + + if (*start == COMMENT) { + continue; + } else if (*start == '[') { + /* parse section */ + end = strchr(start, ']'); + if (!end || *end != ']') { + ret = -EBADMSG; + goto error; + } + + *end = '\0'; + strncpy(section, start + 1, sizeof(section)); + section[MAX_SECTION-1] = '\0'; + } else if (*start) { + /* parse name & value */ + end = strchr(start, '='); + if (!end || *end != '=') { + ret = -EBADMSG; + goto error; + } + *end = '\0'; + name = trim_str(start); + value = trim_str(end + 1); + end = strchr(value, COMMENT); + if (end && *end == COMMENT) { + *end = '\0'; + value = trim_str(value); + } + + result.section = section; + result.name = name; + result.value = value; + /* callback with parse result */ + ret = cb(&result, user_data); + if (ret < 0) { + ret = -EBADMSG; + goto error; + } + } + } + _D("Success to load %s", file_name); + fclose(f); + return 0; + +error: + if (f) + fclose(f); + _E("Failed to read %s:%d!", file_name, lineno); + return ret; +} + diff --git a/src/common/config-parser.h b/src/common/config-parser.h new file mode 100644 index 0000000..f277fa9 --- /dev/null +++ b/src/common/config-parser.h @@ -0,0 +1,40 @@ +/* + * resourced + * + * Copyright (c) 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 __CONFIG_PARSER_H__ +#define __CONFIG_PARSER_H__ + +struct parse_result { + char *section; + char *name; + char *value; +}; + +/** + * @brief Parse config file and call callback\n + * @param[in] file_name conf file. + * @param[in] cb cb is called when conf file is parsed line by line. + * @param[in] user_data user data is passed to cb. + * @return 0 on success, negative if failed + */ +int config_parse(const char *file_name, int cb(struct parse_result *result, + void *user_data), void *user_data); + +#endif + diff --git a/src/common/config.h.in b/src/common/config.h.in new file mode 100644 index 0000000..f1d55a4 --- /dev/null +++ b/src/common/config.h.in @@ -0,0 +1,14 @@ +#ifndef _CONFIG_H_GENERATED +#define _CONFIG_H_GENERATED + +#cmakedefine DEBUG_ENABLED @DEBUG_ENABLED@ +#cmakedefine EXCLUDE_LIST_FULL_PATH "@EXCLUDE_LIST_FULL_PATH@" +#cmakedefine EXCLUDE_LIST_OPT_FULL_PATH "@EXCLUDE_LIST_OPT_FULL_PATH@" +/* It's command line arguments*/ + +#cmakedefine DATABASE_FULL_PATH "@DATABASE_FULL_PATH@" + +#cmakedefine MINOR_VERSION ${MINOR_VERSION} +#cmakedefine MAJOR_VERSION ${MAJOR_VERSION} + +#endif /* _CONFIG_H_GENERATED*/ diff --git a/src/common/edbus-handler.c b/src/common/edbus-handler.c new file mode 100644 index 0000000..35d10a6 --- /dev/null +++ b/src/common/edbus-handler.c @@ -0,0 +1,461 @@ +/* + * resourced + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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. + * + */ + +/* + * @file edbus-handler.c + * + * @desc dbus handler using edbus interface + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + */ + +#include "trace.h" +#include "edbus-handler.h" +#include "macro.h" +#include "resourced.h" + +#define EDBUS_INIT_RETRY_COUNT 5 + +struct edbus_list{ + char *signal_name; + E_DBus_Signal_Handler *handler; +}; + +static struct edbus_object edbus_objects[] = { + { RESOURCED_PATH_OOM, RESOURCED_INTERFACE_OOM, NULL, NULL }, + /* Add new object & interface here*/ +}; + +static Eina_List *edbus_handler_list; +static int edbus_init_val; +static E_DBus_Connection *edbus_conn; +static DBusPendingCall *edbus_request_name; + +static int append_variant(DBusMessageIter *iter, + const char *sig, char *param[]) +{ + char *ch; + int i; + int int_type; + uint64_t int64_type; + DBusMessageIter arr; + struct dbus_byte *byte; + + if (!sig || !param) + return 0; + + for (ch = (char*)sig, i = 0; *ch != '\0'; ++i, ++ch) { + switch (*ch) { + case 'i': + int_type = atoi(param[i]); + dbus_message_iter_append_basic(iter, + DBUS_TYPE_INT32, &int_type); + break; + case 'u': + int_type = strtoul(param[i], NULL, 10); + dbus_message_iter_append_basic(iter, + DBUS_TYPE_UINT32, &int_type); + break; + case 't': + int64_type = atoll(param[i]); + dbus_message_iter_append_basic(iter, + DBUS_TYPE_UINT64, &int64_type); + break; + case 's': + dbus_message_iter_append_basic(iter, + DBUS_TYPE_STRING, ¶m[i]); + break; + case 'a': + ++i, ++ch; + switch (*ch) { + case 'y': + dbus_message_iter_open_container(iter, + DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &arr); + byte = (struct dbus_byte*)param[i]; + dbus_message_iter_append_fixed_array(&arr, + DBUS_TYPE_BYTE, &(byte->data), byte->size); + dbus_message_iter_close_container(iter, &arr); + break; + default: + break; + } + break; + case 'd': + dbus_message_iter_append_basic(iter, + DBUS_TYPE_INT32, ¶m[i]); + break; + + default: + return -EINVAL; + } + } + + return 0; +} + +DBusMessage *dbus_method_sync(const char *dest, const char *path, + const char *interface, const char *method, + const char *sig, char *param[]) +{ + DBusConnection *conn; + DBusMessage *msg; + DBusMessageIter iter; + DBusMessage *reply; + DBusError err; + int r; + + conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL); + if (!conn) { + _E("dbus_bus_get error"); + return NULL; + } + + msg = dbus_message_new_method_call(dest, path, interface, method); + if (!msg) { + _E("dbus_message_new_method_call(%s:%s-%s)", path, interface, method); + return NULL; + } + + dbus_message_iter_init_append(msg, &iter); + r = append_variant(&iter, sig, param); + if (r < 0) { + _E("append_variant error(%d)", r); + dbus_message_unref(msg); + return NULL; + } + + dbus_error_init(&err); + + reply = dbus_connection_send_with_reply_and_block(conn, msg, + DBUS_REPLY_TIMEOUT, &err); + if (!reply) { + _E("dbus_connection_send error(No reply)"); + } + + if (dbus_error_is_set(&err)) { + _E("dbus_connection_send error(%s:%s)", err.name, err.message); + dbus_error_free(&err); + reply = NULL; + } + + dbus_message_unref(msg); + return reply; +} + + +int register_edbus_interface(struct edbus_object *object) +{ + int ret = RESOURCED_ERROR_FAIL; + + if (!object) { + _E("object is invalid value!"); + return ret; + } + + object->obj = e_dbus_object_add(edbus_conn, object->path, NULL); + if (!object->obj) { + _E("fail to add edbus obj"); + return ret; + } + + object->iface = e_dbus_interface_new(object->interface); + if (!object->iface) { + _E("fail to add edbus interface"); + return ret; + } + + 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 RESOURCED_ERROR_FAIL; + } + + sender = dbus_message_get_sender(msg); + if (!sender) { + _E("invalid sender!"); + return RESOURCED_ERROR_FAIL; + } + + 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 RESOURCED_ERROR_FAIL; + } + + 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 RESOURCED_ERROR_FAIL; + } + + pending = e_dbus_message_send(edbus_conn, send_msg, NULL, -1, NULL); + if (!pending) { + _E("pending is null!"); + dbus_message_unref(send_msg); + return RESOURCED_ERROR_FAIL; + } + + 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 RESOURCED_ERROR_FAIL; + } + + 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) +{ + Eina_List *search; + Eina_List *serach_next; + struct edbus_list *entry; + + EINA_LIST_FOREACH_SAFE(edbus_handler_list, search, serach_next, entry) { + if (entry != NULL) { + e_dbus_signal_handler_del(edbus_conn, entry->handler); + edbus_handler_list = eina_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) +{ + Eina_List *search; + struct edbus_list *entry; + E_DBus_Signal_Handler *handler; + + EINA_LIST_FOREACH(edbus_handler_list, search, entry) { + if (entry != NULL && strncmp(entry->signal_name, name, strlen(name)) == 0) + return RESOURCED_ERROR_FAIL; + } + + handler = e_dbus_signal_handler_add(edbus_conn, NULL, path, + interface, name, cb, NULL); + + if (!handler) { + _E("fail to add edbus handler"); + return RESOURCED_ERROR_FAIL; + } + + entry = malloc(sizeof(struct edbus_list)); + + if (!entry) { + _E("Malloc failed"); + return RESOURCED_ERROR_FAIL; + } + + entry->signal_name = strndup(name, strlen(name)); + + if (!entry->signal_name) { + _E("Malloc failed"); + free(entry); + return -1; + } + + entry->handler = handler; + edbus_handler_list = eina_list_prepend(edbus_handler_list, entry); + if (!edbus_handler_list) { + _E("eina_list_prepend failed"); + free(entry->signal_name); + free(entry); + return RESOURCED_ERROR_FAIL; + } + return 0; +} + +int broadcast_edbus_signal_str(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; + } + + e_dbus_message_send(edbus_conn, msg, NULL, -1, NULL); + + dbus_message_unref(msg); + return 0; +} + +int broadcast_edbus_signal(const char *path, const char *interface, + const char *name, int type, void *value) +{ + DBusMessage *signal = dbus_message_new_signal(path, interface, name); + + if (!signal) { + _E("fail to allocate new %s.%s signal", interface, name); + return RESOURCED_ERROR_FAIL; + } + + dbus_message_append_args(signal, type, value, DBUS_TYPE_INVALID); + + e_dbus_message_send(edbus_conn, signal, NULL, -1, NULL); + + dbus_message_unref(signal); + return RESOURCED_ERROR_OK; +} + +resourced_ret_c edbus_add_methods(const char *path, + const struct edbus_method *const edbus_methods, + const size_t size) +{ + E_DBus_Interface *iface; + int i; + int ret; + + iface = get_edbus_interface(path); + + if (!iface) { + _E("Fail to get edbus interface! Path = %s\n", path); + return RESOURCED_ERROR_FAIL; + } + + 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!\n", + edbus_methods[i].member); + return RESOURCED_ERROR_FAIL; + } + } + + return RESOURCED_ERROR_NONE; +} + +void edbus_init(void) +{ + int retry = RESOURCED_ERROR_OK; + int i; +retry_init: + edbus_init_val = e_dbus_init(); + if (edbus_init_val) + goto retry_bus_get; + if (retry == EDBUS_INIT_RETRY_COUNT) { + _E("fail to init edbus"); + return; + } + retry++; + goto retry_init; + +retry_bus_get: + retry = 0; + edbus_conn = e_dbus_bus_get(DBUS_BUS_SYSTEM); + if (edbus_conn) + goto retry_bus_request; + if (retry == EDBUS_INIT_RETRY_COUNT) { + _E("fail to get edbus"); + return; + } + retry++; + goto retry_bus_get; + +retry_bus_request: + retry = 0; + edbus_request_name = e_dbus_request_name(edbus_conn, BUS_NAME, 0, NULL, NULL); + if (edbus_request_name) + goto register_objects; + if (retry == EDBUS_INIT_RETRY_COUNT) { + _E("fail to request edbus name"); + return; + } + retry++; + goto retry_bus_request; + +register_objects: + for (i = 0; i < ARRAY_SIZE(edbus_objects); i++) { + int ret; + + ret = register_edbus_interface(&edbus_objects[i]); + if (ret < 0) { + _E("fail to add obj & interface for %s", + edbus_objects[i].interface); + return; + } + + _I("add new obj for %s", edbus_objects[i].interface); + } + + _I("start edbus service"); +} + +void edbus_exit(void) +{ + unregister_edbus_signal_handle(); + e_dbus_connection_close(edbus_conn); + e_dbus_shutdown(); +} diff --git a/src/common/edbus-handler.h b/src/common/edbus-handler.h new file mode 100644 index 0000000..4460596 --- /dev/null +++ b/src/common/edbus-handler.h @@ -0,0 +1,101 @@ +/* + * resourced + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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. + * + */ + +/** + * @file edbus-handler.h + * @desc dbus handler using edbus interface + **/ + +#ifndef __EDBUS_HANDLE_H__ +#define __EDBUS_HANDLE_H__ + +#include +#include + +struct edbus_method { + const char *member; + const char *signature; + const char *reply_signature; + E_DBus_Method_Cb func; +}; + +struct edbus_object { + const char *path; + const char *interface; + E_DBus_Object *obj; + E_DBus_Interface *iface; +}; + +#define DBUS_REPLY_TIMEOUT (120 * 1000) + +#define BUS_NAME "org.tizen.resourced" +#define OBJECT_PATH "/Org/Tizen/ResourceD" +#define INTERFACE_NAME BUS_NAME + +#define RESOURCED_PATH_OOM OBJECT_PATH"/Oom" +#define RESOURCED_INTERFACE_OOM INTERFACE_NAME".oom" + +#define RESOURCED_PATH_PROCESS OBJECT_PATH"/Process" +#define RESOURCED_INTERFACE_PROCESS INTERFACE_NAME".process" + +/* + * System popup + */ +#define SYSTEM_POPUP_BUS_NAME "org.tizen.system.popup" +#define SYSTEM_POPUP_PATH_NAME "/Org/Tizen/System/Popup" +#define SYSTEM_POPUP_IFACE_NAME SYSTEM_POPUP_BUS_NAME + +#define SYSTEM_POPUP_PATH_WATCHDOG SYSTEM_POPUP_PATH_NAME"/System" +#define SYSTEM_POPUP_IFACE_WATCHDOG SYSTEM_POPUP_BUS_NAME".System" + +/* + * Deviced + */ +#define DEVICED_BUS_NAME "org.tizen.system.deviced" +#define DEVICED_PATH_PROCESS "/Org/Tizen/System/DeviceD/Process" +#define DEVICED_INTERFACE_PROCESS DEVICED_BUS_NAME".Process" + +struct dbus_byte { + char *data; + int size; +}; + +#define RETRY_MAX 5 + +DBusMessage *dbus_method_sync(const char *dest, const char *path, + const char *interface, const char *method, + const char *sig, char *param[]); + +int register_edbus_signal_handler(const char *path, const char *interface, + const char *name, E_DBus_Signal_Cb cb); +E_DBus_Interface *get_edbus_interface(const char *path); +pid_t get_edbus_sender_pid(DBusMessage *msg); +int broadcast_edbus_signal_str(const char *path, const char *interface, + const char *name, const char *sig, char *param[]); +int broadcast_edbus_signal(const char *path, const char *interface, + const char *name, int type, void *value); +resourced_ret_c edbus_add_methods(const char *path, + const struct edbus_method *const edbus_methods, + const size_t size); +int register_edbus_interface(struct edbus_object *object); + +void edbus_init(void); +void edbus_exit(void); + +#endif /* __EDBUS_HANDLE_H__ */ diff --git a/src/common/file-helper.c b/src/common/file-helper.c new file mode 100644 index 0000000..6e40b48 --- /dev/null +++ b/src/common/file-helper.c @@ -0,0 +1,116 @@ +/* + * resourced + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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. + * + */ + + +/* + * @file file-helper.c + * @desc Helper functions for working with files + */ + +#include +#include + +#include "file-helper.h" +#include "trace.h" +#include "macro.h" + +resourced_ret_c fwrite_str(const char *path, const char *str) +{ + FILE *f; + int ret; + + ret_value_msg_if(!path, RESOURCED_ERROR_INVALID_PARAMETER, + "please provide valid name file!\n"); + ret_value_msg_if(!str, RESOURCED_ERROR_INVALID_PARAMETER, + "please provide valid string!\n"); + + f = fopen(path, "w"); + ret_value_errno_msg_if(!f, RESOURCED_ERROR_FAIL, "Fail to file open"); + + ret = fputs(str, f); + fclose(f); + ret_value_errno_msg_if(ret == EOF, RESOURCED_ERROR_FAIL, + "Fail to write file\n"); + + return RESOURCED_ERROR_OK; +} + +resourced_ret_c fwrite_int(const char *path, const int number) +{ + char digit_buf[MAX_DEC_SIZE(int)]; + int ret; + + ret = sprintf(digit_buf, "%d", number); + ret_value_errno_msg_if(ret < 0, RESOURCED_ERROR_FAIL, + "sprintf failed\n"); + + return fwrite_str(path, digit_buf); +} + +resourced_ret_c fwrite_uint(const char *path, const u_int32_t number) +{ + char digit_buf[MAX_DEC_SIZE(u_int32_t)]; + int ret; + + ret = sprintf(digit_buf, "%u", number); + ret_value_errno_msg_if(ret < 0, RESOURCED_ERROR_FAIL, + "sprintf failed\n"); + + return fwrite_str(path, digit_buf); +} + +resourced_ret_c fread_int(const char *path, u_int32_t *number) +{ + FILE *f; + int ret; + + f = fopen(path, "r"); + + ret_value_errno_msg_if(!f, RESOURCED_ERROR_FAIL, "Fail to open file"); + + ret = fscanf(f, "%u", number); + fclose(f); + ret_value_errno_msg_if(ret == EOF, RESOURCED_ERROR_FAIL, + "Fail to read file\n"); + + return RESOURCED_ERROR_OK; +} + +resourced_ret_c fwrite_array(const char *path, const void *array, + const size_t size_of_elem, + const size_t numb_of_elem) +{ + FILE *f; + int ret; + + ret_value_msg_if(!array, RESOURCED_ERROR_INVALID_PARAMETER, + "please provide valid array of elements!\n"); + + f = fopen(path, "w"); + + ret_value_errno_msg_if(!f, RESOURCED_ERROR_FAIL, + "Failed open %s file\n", path); + + ret = fwrite(array, size_of_elem, numb_of_elem, f); + fclose(f); + ret_value_errno_msg_if(ret != numb_of_elem, RESOURCED_ERROR_FAIL, + "Failed write array into %s file\n"); + + return RESOURCED_ERROR_OK; +} diff --git a/src/common/file-helper.h b/src/common/file-helper.h new file mode 100644 index 0000000..6930a7c --- /dev/null +++ b/src/common/file-helper.h @@ -0,0 +1,48 @@ +/* + * resourced + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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. + * + */ + + +/* + * @file file-helper.h + * @desc Helper functions for working with files + */ + +#ifndef _RESOURCED_FILE_HELPER_H_ +#define _RESOURCED_FILE_HELPER_H_ + +#include "resourced.h" + +/** + * @desc write string to the file + * @param path - path to the file, str - string is written to the file + * @return negative value if error + */ +resourced_ret_c fwrite_str(const char *path, const char *str); + +resourced_ret_c fwrite_int(const char *path, const int number); + +resourced_ret_c fwrite_uint(const char *path, const u_int32_t number); + +resourced_ret_c fread_int(const char *path, u_int32_t *number); + +resourced_ret_c fwrite_array(const char *path, const void *array, + const size_t size_of_elem, + const size_t numb_of_elem); + +#endif /*_RESOURCED_FILE_HELPER_H_*/ diff --git a/src/common/macro.h b/src/common/macro.h new file mode 100644 index 0000000..886071c --- /dev/null +++ b/src/common/macro.h @@ -0,0 +1,122 @@ +#ifndef _PERF_CONTROL_MACRO_H +#define _PERF_CONTROL_MACRO_H + +#define execute_once \ + static int __func__##guardian; \ + for (; \ + __func__##guardian == 0; \ + __func__##guardian = 1) + +#include +#include +#include + +#define ARRAY_SIZE(x) (sizeof(x)/sizeof(x[0])) + +#define DECLARE_WRAPPER(fn_name, inner_fn) \ +static void fn_name(void *data, void __attribute__((__unused__)) *not_used) \ +{\ + return inner_fn(data); \ +} + +#define UNUSED __attribute__((__unused__)) + +#define MAX_SIZE2(a, b) sizeof(a) + sizeof(b) - 1 +#define MAX_SIZE3(a, b, c) MAX_SIZE2(a, b) + sizeof(c) - 1 + +/* + * One byte digit has 3 position in decimal representation + * 2 - 5 + * 4 - 10 + * 8 - 20 + * >8 - compile time error + * plus 1 null termination byte + * plus 1 for negative prefix + */ +#define MAX_DEC_SIZE(type) \ + (2 + (sizeof(type) <= 1 ? 3 : \ + sizeof(type) <= 2 ? 5 : \ + sizeof(type) <= 4 ? 10 : \ + sizeof(type) <= 8 ? 20 : \ + sizeof(int[-2*(sizeof(type) > 8)]))) + +#define ret_value_if(expr, val) do { \ + if (expr) { \ + _E("(%s) -> %s():%d return", #expr, __FUNCTION__, __LINE__); \ + return (val); \ + } \ +} while (0) + +#define ret_value_msg_if(expr, val, fmt, arg...) do { \ + if (expr) { \ + _E(fmt, ##arg); \ + return val; \ + } \ +} while (0) + +#define ret_value_secure_msg_if(expr, val, fmt, arg...) do { \ + if (expr) { \ + _SE(fmt, ##arg); \ + return val; \ + } \ + } while (0) + +#define ret_value_errno_msg_if(expr, val, fmt, arg...) do { \ + if (expr) { \ + ETRACE_ERRNO_MSG(fmt, ##arg); \ + return val; \ + } \ +} while (0) + +/* + * @brief Copy from source to destination + * destination should not be on heap. + * Destination will be null terminated + */ +#define STRING_SAVE_COPY(destination, source) \ + do { \ + size_t null_pos = strlen(source); \ + strncpy(destination, source, sizeof(destination)); \ + null_pos = sizeof(destination) - 1 < null_pos ? \ + sizeof(destination) - 1 : null_pos; \ + destination[null_pos] = '\0'; \ + } while(0) + +/* FIXME: Do we really need pointers? */ +#define array_foreach(key, type, array) \ + guint _array_foreach_index; \ + type *key; \ + for (_array_foreach_index = 0; \ + array && _array_foreach_index < array->len && \ + (key = &g_array_index(array, type, _array_foreach_index)); \ + ++_array_foreach_index) + +#define slist_foreach(key, type, list) \ + type *key; \ + GSList *_slist_foreach_copy_list = list; \ + for (; \ + _slist_foreach_copy_list && \ + ((key = _slist_foreach_copy_list->data) || 1); \ + _slist_foreach_copy_list = _slist_foreach_copy_list->next) + +#define gslist_for_each_item(item, list) \ + for(item = list; item != NULL; item = g_slist_next(item)) + +#define DB_ACTION(command) do { \ + if ((command) != SQLITE_OK) { \ + error_code = RESOURCED_ERROR_DB_FAILED; \ + goto handle_error; \ + } \ +} while (0) + +#define MODULE_REGISTER(module) \ + static void __attribute__ ((constructor)) module_init(void) \ + { \ + add_module(module); \ + } \ + static void __attribute__ ((destructor)) module_exit(void) \ + { \ + remove_module(module); \ + } + +#endif /* _PERF_CONTROL_MACRO_H */ diff --git a/src/common/module.c b/src/common/module.c new file mode 100644 index 0000000..eb7e08d --- /dev/null +++ b/src/common/module.c @@ -0,0 +1,93 @@ +/* + * resourced + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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. + * + */ + +/** + * @file module.c + * @desc Module helper functions + **/ + +#include "macro.h" +#include "module.h" +#include "resourced.h" +#include "trace.h" + +#include + +static GSList *modules_list; + +void add_module(struct module_ops *module) +{ + ret_value_msg_if(!module, , "Invalid module handler\n"); + if (module->priority == MODULE_PRIORITY_HIGH) + modules_list = g_slist_prepend(modules_list, module); + else + modules_list = g_slist_append(modules_list, module); +} + +void remove_module(struct module_ops *module) +{ + modules_list = g_slist_remove(modules_list, module); +} + +const struct module_ops *find_module(const char *name) +{ + GSList *iter; + const struct module_ops *module; + + gslist_for_each_item(iter, modules_list) { + module = (struct module_ops *)iter->data; + if (!strcmp(module->name, name)) + return module; + } + return NULL; +} + +void modules_init(void *data) +{ + GSList *iter; + struct module_ops *module; + int ret_code = RESOURCED_ERROR_NONE; + + gslist_for_each_item(iter, modules_list) { + module = (struct module_ops *)iter->data; + _D("Initialize [%s] module\n", module->name); + if (module->init) + ret_code = module->init(data); + if (ret_code < 0) + _E("Fail to initialize [%s] module\n", module->name); + } +} + +void modules_exit(void *data) +{ + GSList *iter; + /* Deinitialize in reverse order */ + GSList *reverse_list = g_slist_reverse(modules_list); + struct module_ops *module; + int ret_code = RESOURCED_ERROR_NONE; + + gslist_for_each_item(iter, reverse_list) { + module = (struct module_ops *)iter->data; + _D("Deinitialize [%s] module\n", module->name); + if (module->exit) + ret_code = module->exit(data); + if (ret_code < 0) + _E("Fail to deinitialize [%s] module\n", module->name); + } +} diff --git a/src/common/module.h b/src/common/module.h new file mode 100644 index 0000000..4135134 --- /dev/null +++ b/src/common/module.h @@ -0,0 +1,50 @@ +/* + * resourced + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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. + * + */ + +/** + * @file module.h + * @desc Module helper functions + **/ + +#ifndef __MODULE_HANDLE_H__ +#define __MODULE_HANDLE_H__ + +enum module_priority { + MODULE_PRIORITY_NORMAL, + MODULE_PRIORITY_HIGH, +}; + +struct module_ops { + enum module_priority priority; + const char *name; + int (*init) (void *data); + int (*exit) (void *data); + int (*control) (void *data); + int (*status) (void *data); +}; + +void add_module(struct module_ops *module); +void remove_module(struct module_ops *module); + +void modules_init(void *data); +void modules_exit(void *data); + +const struct module_ops *find_module(const char *name); + +#endif /* __MODULE_HANDLE_H__ */ diff --git a/src/common/trace.h b/src/common/trace.h new file mode 100644 index 0000000..049ff9a --- /dev/null +++ b/src/common/trace.h @@ -0,0 +1,82 @@ +/* + * resourced + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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. + * + */ + + +/* + * @file trace.h + * Common macros for tracing + */ + +#ifndef _SYSTEM_RESOURCE_TRACE_H_ +#define _SYSTEM_RESOURCE_TRACE_H_ + +#include +#include +#include +#include +#include + +#undef LOG_TAG +#define LOG_TAG "RESOURCED" + +#define WALK_TREE(list, func) g_tree_foreach((GTree *)list, func, NULL) + +#define _E(fmt, arg...) LOGE("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg) +#define _D(fmt, arg...) LOGD("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg) +#define _I(fmt, arg...) LOGI("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg) + +#define _SE(fmt, arg...) SECURE_LOGE("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg) +#define _SD(fmt, arg...) SECURE_LOGD("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg) +#define _SI(fmt, arg...) SECURE_LOGI("[%s,%d] "fmt, __FUNCTION__, __LINE__, ##arg) + +#define TRACE_DB_ERR(a) if (a != NULL) { \ + _D("%s\n", a); \ + sqlite3_free(a); \ +} + +#define TRACE_RET_ERRCODE(type, error_code) do { \ + _##type("errno %d, errmsg %s", error_code, strerror(-error_code)); \ +} while (0) + +#define DTRACE_RET_ERRCODE(error_code) TRACE_RET_ERRCODE(D, error_code) + +#define ETRACE_RET_ERRCODE(error_code) TRACE_RET_ERRCODE(E, error_code) + +#define TRACE_RET_ERRCODE_MSG(type, error_code, fmt, arg...) do { \ + _##type(fmt, ##arg); \ + _##type("errno %d, errmsg %s", error_code, strerror(-error_code)); \ +} while (0) + +#define DTRACE_RET_ERRCODE_MSG(error_code, fmt, arg...) \ + TRACE_RET_ERRCODE_MSG(D, error_code, fmt, ##arg) + +#define ETRACE_RET_ERRCODE_MSG(error_code, fmt, arg...) \ + TRACE_RET_ERRCODE_MSG(E, error_code, fmt, ##arg) + +#define DTRACE_ERRNO() TRACE_RET_ERRCODE(D, -errno) + +#define ETRACE_ERRNO() TRACE_RET_ERRCODE(E, -errno) + +#define DTRACE_ERRNO_MSG(fmt, arg...) \ + TRACE_RET_ERRCODE_MSG(D, -errno, fmt, ##arg) + +#define ETRACE_ERRNO_MSG(fmt, arg...) \ + TRACE_RET_ERRCODE_MSG(E, -errno, fmt, ##arg) + +#endif /* _SYSTEM_RESOURCE_TRACE_H_ */ diff --git a/src/resourced/init.c b/src/resourced/init.c new file mode 100644 index 0000000..102970d --- /dev/null +++ b/src/resourced/init.c @@ -0,0 +1,132 @@ +/* + * resourced + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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. + * + */ + +/** + * @file init.c + * @desc Resourced initialization + * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + **/ + +#include "edbus-handler.h" +#include "cgroup.h" +#include "init.h" +#include "macro.h" +#include "trace.h" +#include "version.h" + +#include +#include +#include + +static void print_root_usage() +{ + puts("You must be root to start it."); +} + +static void print_usage() +{ + puts("resmand [Options]"); + puts(" Application options:"); + puts("-v [--version] - program version"); + puts("-h [--help] - application help"); +} + +static void print_version() +{ + printf("Version number: %d.%d.%d\n", + MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION); +} + +static int parse_cmd(int argc, char **argv) +{ + const char *optstring = ":hvu:s:f:e:c:ow"; + const struct option options[] = { + {"help", no_argument, 0, 'h'}, + {"version", no_argument, 0, 'v'}, + {"enable-watchodg", no_argument, 0, 'w'}, + {0, 0, 0, 0} + }; + int longindex, retval; + + while ((retval = + getopt_long(argc, argv, optstring, options, &longindex)) != -1) + switch (retval) { + case 'h': + case '?': + print_usage(); + return RESOURCED_ERROR_FAIL; + case 'v': + print_version(); + return RESOURCED_ERROR_FAIL; + case 'w': + break; + default: + printf("Unknown option %c\n", (char)retval); + print_usage(); + return RESOURCED_ERROR_FAIL; + } + return RESOURCED_ERROR_OK; +} + +static int assert_root(void) +{ + if (getuid() != 0) { + print_root_usage(); + return RESOURCED_ERROR_FAIL; + } + return RESOURCED_ERROR_OK; +} + +static void sig_term_handler(int sig) +{ + ecore_main_loop_quit(); +} + +static void add_signal_handler(void) +{ + signal(SIGTERM, sig_term_handler); + signal(SIGINT, sig_term_handler); +} + +int resourced_init(struct daemon_arg *darg) +{ + int ret_code; + + ret_value_msg_if(darg == NULL, RESOURCED_ERROR_INVALID_PARAMETER, + "Invalid daemon argument\n"); + ret_code = assert_root(); + ret_value_if(ret_code < 0, RESOURCED_ERROR_FAIL); + ecore_init(); + ret_code = parse_cmd(darg->argc, darg->argv); + ret_value_msg_if(ret_code < 0, RESOURCED_ERROR_FAIL, + "Error parse cmd arguments\n"); + add_signal_handler(); + edbus_init(); + return RESOURCED_ERROR_NONE; +} + +int resourced_deinit(struct daemon_arg *darg) +{ + ecore_shutdown(); + edbus_exit(); + ret_value_msg_if(darg == NULL, RESOURCED_ERROR_INVALID_PARAMETER, + "Invalid daemon argument\n"); + return RESOURCED_ERROR_NONE; +} diff --git a/src/resourced/init.h b/src/resourced/init.h new file mode 100644 index 0000000..b57d769 --- /dev/null +++ b/src/resourced/init.h @@ -0,0 +1,41 @@ +/* + * resourced + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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. + * + */ + +/** + * @file init.h + * @desc Resourced initialization + * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + **/ + +#ifndef _RESOURCED_INIT_H +#define _RESOURCED_INIT_H + +#include "resourced.h" + +struct daemon_arg { + int argc; + char **argv; +}; + +int resourced_init(struct daemon_arg *darg); + +int resourced_deinit(struct daemon_arg *darg); + +#endif /* _RESOURCED_INIT_H */ diff --git a/src/resourced/main.c b/src/resourced/main.c new file mode 100644 index 0000000..c5266a2 --- /dev/null +++ b/src/resourced/main.c @@ -0,0 +1,55 @@ +/* + * resourced + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * 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. + * + */ + +/* + * @file main.c + * + * Copyright (c) 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + */ + +#include "init.h" +#include "macro.h" +#include "module.h" +#include "trace.h" +#include "version.h" + +#include +#include + +int main(int argc, char **argv) +{ + int ret_code = 0; + struct daemon_arg darg = { argc, argv }; + +#ifdef DEBUG_ENABLED + mtrace(); + mcheck(0); +#endif + ret_code = resourced_init(&darg); + ret_value_msg_if(ret_code < 0, ret_code, + "Resourced initialization failed\n"); + modules_init(NULL); + if (ret_code < 0) + _E("Proc init failed"); + ecore_main_loop_begin(); + modules_exit(NULL); + resourced_deinit(&darg); + return ret_code; +} -- 2.7.4