Fix priority change feature 77/131477/13
authorHwankyu Jhun <h.jhun@samsung.com>
Mon, 29 May 2017 10:02:54 +0000 (19:02 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 31 May 2017 05:26:34 +0000 (14:26 +0900)
- Change prefix to "TIZEN_FEATURE".
- Change lowest priority value to 19. (range: -20 ~ 19)
- Set CAP_SYS_NICE if the feature is enabled
- Add a new API to adjust scheduling priority

Note:
 - To enable the feature, the user session systemd should have
 the cap_sys_nice capability(inheritable).

Change-Id: I86e3d30a26ecde5bf8a70ef1cb2660fa33cc5aef
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
CMakeLists.txt
inc/key.h
inc/launchpad.h
inc/launchpad_common.h
packaging/launchpad.spec
src/launchpad.c
src/launchpad_common.c
src/launchpad_lib.c
src/launchpad_loader.c

index 9c1c049..23a351c 100755 (executable)
@@ -62,9 +62,12 @@ SET(CMAKE_C_FLAGS_RELEASE "-O2")
 SET(CMAKE_EXE_LINKER_FLAGS "-Wl,--as-needed")
 
 ADD_DEFINITIONS("-DSHARE_PREFIX=\"/usr/share/aul\"")
-IF(_APPFW_FEATURE_PRIORITY_CHANGE)
-       ADD_DEFINITIONS("-D_APPFW_FEATURE_PRIORITY_CHANGE")
-ENDIF(_APPFW_FEATURE_PRIORITY_CHANGE)
+IF(_TIZEN_FEATURE_PRIORITY_CHANGE)
+       ADD_DEFINITIONS("-DTIZEN_FEATURE_PRIORITY_CHANGE")
+ENDIF(_TIZEN_FEATURE_PRIORITY_CHANGE)
+IF(_TIZEN_FEATURE_LOADER_PRIORITY)
+       ADD_DEFINITIONS("-DTIZEN_FEATURE_LOADER_PRIORITY")
+ENDIF(_TIZEN_FEATURE_LOADER_PRIORITY)
 
 ADD_DEFINITIONS("-DLAUNCHPAD_LOG")
 ADD_DEFINITIONS("-DPRELOAD_ACTIVATE")
index e83bb14..98647e0 100644 (file)
--- a/inc/key.h
+++ b/inc/key.h
@@ -45,6 +45,7 @@ extern "C" {
 #define AUL_K_LOADER_NAME              "__AUL_LOADER_NAME__"
 #define AUL_K_SDK                      "__AUL_SDK__"
 #define AUL_K_ORG_CALLER_PID           "__AUL_ORG_CALLER_PID__"
+#define AUL_K_HIGHPRIORITY             "__AUL_HIGHPRIORITY__"
 
 #ifdef __cplusplus
 }
index fb2cabf..7c2cae8 100644 (file)
@@ -60,6 +60,21 @@ int launchpad_loader_main(int argc, char **argv,
                loader_lifecycle_callback_s *callbacks,
                loader_adapter_s *adapter, void *user_data);
 
+/*
+ * @par Description
+ *      Set program scheduling priority.
+ *
+ * @param[in]   prio    The priority
+ * @return      @c 0 on success,
+ *              otherwise a negative error value
+ *
+ * @remarks
+ *      The prio is a value in the range -20 to 19.
+ *      If the prio is higher than current value, the process MUST have
+ *      CAP_SYS_NICE capability.
+ */
+int launchpad_loader_set_priority(int prio);
+
 #ifdef __cplusplus
 }
 #endif
index a2a95ad..7a8e323 100644 (file)
@@ -109,6 +109,7 @@ int _proc_get_attr_by_pid(int pid, char *buf, int size);
 int _close_all_fds(void);
 void _get_cpu_idle(unsigned long long *total, unsigned long long *idle);
 int _setup_stdio(const char *ident);
+int _set_priority(int prio);
 
 #endif /* __LAUNCHPAD_COMMON_H__ */
 
index 9c0baf2..e0f0e84 100644 (file)
@@ -29,7 +29,8 @@ Requires(postun): /sbin/ldconfig
 Requires(postun): /usr/bin/systemctl
 Requires(preun): /usr/bin/systemctl
 
-%define appfw_feature_priority_change 0
+%define tizen_feature_priority_change 0
+%define tizen_feature_loader_priority 0
 
 %description
 Launchpad for launching applications
@@ -58,14 +59,18 @@ export CFLAGS="$CFLAGS -DTIZEN_DEBUG_ENABLE"
 export CXXFLAGS="$CXXFLAGS -DTIZEN_DEBUG_ENABLE"
 export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE"
 %endif
-%if 0%{?appfw_feature_priority_change}
-_APPFW_FEATURE_PRIORITY_CHANGE=ON
+%if 0%{?tizen_feature_priority_change}
+_TIZEN_FEATURE_PRIORITY_CHANGE=ON
+%endif
+%if 0%{?tizen_feature_loader_priority}
+_TIZEN_FEATURE_LOADER_PRIORITY=ON
 %endif
 
 MAJORVER=`echo %{version} | awk 'BEGIN {FS="."}{print $1}'`
 %cmake -DVERSION=%{version} \
        -DMAJORVER=${MAJORVER} \
-       -D_APPFW_FEATURE_PRIORITY_CHANGE:BOOL=${_APPFW_FEATURE_PRIORITY_CHANGE} \
+       -D_TIZEN_FEATURE_PRIORITY_CHANGE:BOOL=${_TIZEN_FEATURE_PRIORITY_CHANGE} \
+       -D_TIZEN_FEATURE_LOADER_PRIORITY:BOOL=${_TIZEN_FEATURE_LOADER_PRIORITY} \
        .
 %__make %{?_smp_mflags}
 
@@ -90,7 +95,11 @@ ln -sf ../launchpad-process-pool.service %{buildroot}%{_unitdir_user}/basic.targ
 %{_unitdir_user}/launchpad-process-pool.socket
 %{_unitdir_user}/sockets.target.wants/launchpad-process-pool.socket
 %{_unitdir_user}/basic.target.wants/launchpad-process-pool.service
+%if 0%{?tizen_feature_priority_change} || 0%{?tizen_feature_loader_priority}
+%caps(cap_mac_admin,cap_dac_override,cap_setgid,cap_sys_nice=ei) %{_bindir}/launchpad-process-pool
+%else
 %caps(cap_mac_admin,cap_dac_override,cap_setgid=ei) %{_bindir}/launchpad-process-pool
+%endif
 %attr(0644,root,root) %{_libdir}/liblaunchpad.so.*
 
 %files devel
@@ -102,4 +111,8 @@ ln -sf ../launchpad-process-pool.service %{buildroot}%{_unitdir_user}/basic.targ
 %manifest %{name}.manifest
 %license LICENSE
 %{_prefix}/share/aul/default.loader
+%if 0%{?tizen_feature_priority_change} || 0%{?tizen_feature_loader_priority}
+%caps(cap_setgid,cap_sys_nice=ei) %{_bindir}/launchpad-loader
+%else
 %caps(cap_setgid=ei) %{_bindir}/launchpad-loader
+%endif
index 5cf71de..ce1a77f 100755 (executable)
@@ -1695,21 +1695,6 @@ static void __after_loop(void)
                security_manager_app_labels_monitor_finish(label_monitor);
 }
 
-#ifdef _APPFW_FEATURE_PRIORITY_CHANGE
-static void __set_priority(void)
-{
-       char err_str[MAX_LOCAL_BUFSZ] = { 0, };
-       int res;
-
-       res = setpriority(PRIO_PROCESS, 0, -12);
-       if (res == -1) {
-               SECURE_LOGE("Setting process (%d) priority to -12 failed, " \
-                               "errno: %d (%s)", getpid(), errno,
-                               strerror_r(errno, err_str, sizeof(err_str)));
-       }
-}
-#endif
-
 int main(int argc, char **argv)
 {
        GMainLoop *mainloop = NULL;
@@ -1725,8 +1710,8 @@ int main(int argc, char **argv)
                return -1;
        }
 
-#ifdef _APPFW_FEATURE_PRIORITY_CHANGE
-       __set_priority();
+#ifdef TIZEN_FEATURE_PRIORITY_CHANGE
+       _set_priority(-12);
 #endif
        g_main_loop_run(mainloop);
 
index e3d727e..7f15619 100644 (file)
@@ -34,6 +34,8 @@
 #include <stdio.h>
 #include <stdbool.h>
 #include <systemd/sd-journal.h>
+#include <sys/time.h>
+#include <sys/resource.h>
 
 #include "launchpad_common.h"
 #include "key.h"
@@ -868,3 +870,16 @@ int _setup_stdio(const char *ident)
 
        return 0;
 }
+
+int _set_priority(int prio)
+{
+       int r;
+
+       r = setpriority(PRIO_PGRP, 0, prio);
+       if (r < 0) {
+               SECURE_LOGE("Failed to set process(%d) priority(%d) - err(%d)",
+                               getpid(), prio, errno);
+       }
+
+       return r;
+}
index 927046e..da0a357 100644 (file)
 #include <sys/types.h>
 #include <sys/prctl.h>
 #include <malloc.h>
-#ifdef _APPFW_FEATURE_LOADER_PRIORITY
-#include <sys/time.h>
-#include <sys/resource.h>
-#endif
 #include <bundle_internal.h>
 #include <aul.h>
 #include <security-manager.h>
@@ -40,7 +36,6 @@
 #endif
 
 #define AUL_PR_NAME 16
-#define LOWEST_PRIO 20
 
 static loader_lifecycle_callback_s *__loader_callbacks;
 static loader_adapter_s *__loader_adapter;
@@ -130,22 +125,6 @@ static int __default_launch_cb(bundle *kb, const char *appid,
                const char *pkgid, const char *pkg_type, int loader_type)
 {
        char err_str[MAX_LOCAL_BUFSZ] = { 0, };
-#ifdef _APPFW_FEATURE_PRIORITY_CHANGE
-       int res;
-       const char *high_priority = bundle_get_val(kb, AUL_K_HIGHPRIORITY);
-
-       _D("high_priority: %s", high_priority);
-       if (strncmp(high_priority, "true", 4) == 0) {
-               res = setpriority(PRIO_PROCESS, 0, -10);
-               if (res == -1) {
-                       SECURE_LOGE("Setting process (%d) priority " \
-                               "to -10 failed, errno: %d (%s)",
-                               getpid(), errno,
-                               strerror_r(errno, err_str, sizeof(err_str)));
-               }
-       }
-       bundle_del(kb, AUL_K_HIGHPRIORITY);
-#endif
 
        if (__prepare_exec(appid, app_path, pkg_type, loader_type) < 0) {
                _E("__candidate_process_prepare_exec() failed");
@@ -296,16 +275,7 @@ static int __before_loop(int argc, char **argv)
        int client_fd;
        int ret = -1;
        bundle *extra = NULL;
-#ifdef _APPFW_FEATURE_LOADER_PRIORITY
-       char err_str[MAX_LOCAL_BUFSZ] = { 0, };
-       int res = setpriority(PRIO_PROCESS, 0, LOWEST_PRIO);
 
-       if (res == -1) {
-               SECURE_LOGE("Setting process (%d) priority to %d failed, " \
-                               "errno: %d (%s)", getpid(), LOWEST_PRIO, errno,
-                               strerror_r(errno, err_str, sizeof(err_str)));
-       }
-#endif
        __preexec_init(argc, argv);
 
        /* Set new session ID & new process group ID*/
@@ -332,14 +302,7 @@ static int __before_loop(int argc, char **argv)
                bundle_free(extra);
 
        malloc_trim(0);
-#ifdef _APPFW_FEATURE_LOADER_PRIORITY
-       res = setpriority(PRIO_PGRP, 0, 0);
-       if (res == -1) {
-               SECURE_LOGE("Setting process (%d) priority to 0 failed, " \
-                               "errno: %d (%s)", getpid(), errno,
-                               strerror_r(errno, err_str, sizeof(err_str)));
-       }
-#endif
+
        client_fd = _connect_to_launchpad(__loader_type, __loader_id);
        if (client_fd == -1) {
                _D("Connecting to candidate process was failed.");
@@ -414,3 +377,7 @@ API int launchpad_loader_main(int argc, char **argv,
        return __after_loop();
 }
 
+API int launchpad_loader_set_priority(int prio)
+{
+       return _set_priority(prio);
+}
index fdc60a5..01487de 100644 (file)
@@ -36,7 +36,6 @@
 #define LOADER_TYPE_HW         "hw-loader"
 #define LOADER_TYPE_SW         "sw-loader"
 
-
 extern bundle *launchpad_loader_get_bundle(void);
 
 static Ecore_Fd_Handler *__fd_handler;
@@ -181,6 +180,10 @@ static void __loader_create_cb(bundle *extra, int type, void *user_data)
                return;
        }
 
+#ifdef TIZEN_FEATURE_LOADER_PRIORITY
+       launchpad_loader_set_priority(19);
+#endif
+
        if (!strcmp(LOADER_TYPE_COMMON, ltype))
                __type = TYPE_COMMON;
        else if (!strcmp(LOADER_TYPE_SW, ltype))
@@ -224,6 +227,9 @@ static void __loader_create_cb(bundle *extra, int type, void *user_data)
                _E("Failed to register callback for %s",
                                VCONFKEY_SETAPPL_APP_HW_ACCELERATION);
        }
+#ifdef TIZEN_FEATURE_LOADER_PRIORITY
+       launchpad_loader_set_priority(0);
+#endif
 }
 
 static loader_convertible __converter_table[MAX_LOADER_TYPE][MAX_ACC_TYPE] = {
@@ -243,12 +249,24 @@ static int __loader_launch_cb(int argc, char **argv, const char *app_path,
        bundle *kb = launchpad_loader_get_bundle();
        int acc = SW_ACC;
        loader_convertible convert;
+#ifdef TIZEN_FEATURE_PRIORITY_CHANGE
+       const char *high_priority;
+#endif
 
        vconf_ignore_key_changed(VCONFKEY_SETAPPL_APP_HW_ACCELERATION,
                        __vconf_cb);
        if (kb == NULL)
                return 0;
 
+#ifdef TIZEN_FEATURE_PRIORITY_CHANGE
+       high_priority = bundle_get_val(kb, AUL_K_HIGHPRIORITY);
+       if (high_priority) {
+               if (!strcmp(high_priority, "true"))
+                       launchpad_loader_set_priority(-12);
+               bundle_del(kb, AUL_K_HIGHPRIORITY);
+       }
+#endif
+
        hwacc = bundle_get_val(kb, AUL_K_HWACC);
 
        if (!hwacc)