Change return values 31/214131/3
authorInkyun Kil <inkyun.kil@samsung.com>
Wed, 18 Sep 2019 04:16:33 +0000 (13:16 +0900)
committerInkyun Kil <inkyun.kil@samsung.com>
Wed, 18 Sep 2019 09:35:53 +0000 (18:35 +0900)
- When registering once alarm with wrong time, return false.

Change-Id: I016fc294f40a6825e228c4d9193e9e2596233092
Signed-off-by: Inkyun Kil <inkyun.kil@samsung.com>
server/alarm-manager.c

index a5c5469..d3c43b2 100755 (executable)
@@ -115,6 +115,7 @@ static void __on_system_time_external_changed(keynode_t *node, void *data);
 static void __initialize_alarm_list();
 static void __initialize_scheduled_alarm_list();
 static void __initialize_noti();
+static gboolean __alarm_expired_directly(gpointer user_data);
 
 void _release_alarm_info_t(__alarm_info_t *entry);
 static notification_h __get_notification(guchar *data, int datalen);
@@ -796,12 +797,100 @@ static void __set_caller_info(bundle *b, uid_t uid,
        }
 }
 
-static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
-                          long requested_interval, uid_t uid, int pid, char *bundle_data, int *error_code)
+static bool __alarm_add_and_set(__alarm_info_t *alarm_info, pid_t pid)
 {
        time_t current_time;
        struct tm ts_ret;
        char due_time_r[100] = { 0 };
+
+       time(&current_time);
+
+       SECURE_LOGW("[alarm-server]:pid=%d, app_unique_name=%s, \
+                       app_service_name=%s,dst_service_name=%s, c_due_time=%ld",
+                       pid, alarm_info->app_unique_name,
+                       alarm_info->app_service_name,
+                       alarm_info->dst_service_name,
+                       alarm_context.c_due_time);
+
+       if (alarm_info->alarm_info.mode.repeat == ALARM_REPEAT_MODE_ONCE)
+               alarm_info->alarm_info.reserved_info = current_time;
+
+       if (alarm_context.c_due_time < current_time) {
+               LOGW("Caution!! alarm_context.c_due_time (%ld) is less than current time(%ld)",
+                               alarm_context.c_due_time, current_time);
+               alarm_context.c_due_time = -1;
+       }
+
+        _alarm_set_next_duetime(alarm_info);
+
+       if (alarm_info->due_time == 0) {
+               LOGW("[alarm-server]:Create a new alarm: due_time is 0. [alarm(%d):repeat_type(%d)]",
+                               alarm_info->alarm_id, alarm_info->alarm_info.mode.repeat);
+
+               if (alarm_info->alarm_info.mode.repeat == ALARM_REPEAT_MODE_ONCE)
+                       return false;
+
+               __alarm_add_to_list(alarm_info);
+               return true;
+       } else if (current_time == alarm_info->due_time) {
+               LOGW("[alarm-server]:Create alarm: current_time(%ld) is same as due_time(%ld) [alarm(%d):repeat_type(%d)]",
+                               current_time, alarm_info->due_time, alarm_info->alarm_id, alarm_info->alarm_info.mode.repeat);
+
+               __alarm_add_to_list(alarm_info);
+               _clear_scheduled_alarm_list();
+               _add_to_scheduled_alarm_list(alarm_info);
+               alarm_context.c_due_time = alarm_info->due_time;
+
+               time_t *_dt = malloc(sizeof(time_t));
+               if (_dt) {
+                       *_dt = alarm_info->due_time;
+               } else {
+                       LOGE("Out of memory");
+                       return false;
+               }
+
+               g_idle_add(__alarm_expired_directly, (gpointer)_dt);
+
+               return true;
+       } else if (difftime(alarm_info->due_time, current_time) < 0) {
+               LOGW("[alarm-server]: Expired Due Time. \
+                               [Due time=%ld, Current Time=%ld]!!!Do not add to schedule list. \
+                               [alarm(%d):repeat_type(%d)]",
+                               alarm_info->due_time, current_time, alarm_info->alarm_id, alarm_info->alarm_info.mode.repeat);
+
+               if (alarm_info->alarm_info.mode.repeat == ALARM_REPEAT_MODE_ONCE)
+                       return false;
+
+               __alarm_add_to_list(alarm_info);
+               return true;
+       } else {
+               localtime_r(&(alarm_info->due_time), &ts_ret);
+               strftime(due_time_r, 30, "%c", &ts_ret);
+               SECURE_LOGD("[alarm-server]:Create a new alarm: alarm(%d) due_time(%s)",
+                               alarm_info->alarm_id, due_time_r);
+
+               __alarm_add_to_list(alarm_info);
+       }
+
+       LOGD("[alarm-server]:alarm_context.c_due_time(%ld), due_time(%ld)",
+                       alarm_context.c_due_time, alarm_info->due_time);
+
+       if (alarm_context.c_due_time == -1 || alarm_info->due_time < alarm_context.c_due_time) {
+               _clear_scheduled_alarm_list();
+               _add_to_scheduled_alarm_list(alarm_info);
+               _alarm_set_timer(alarm_context.timer, alarm_info->due_time);
+               alarm_context.c_due_time = alarm_info->due_time;
+               _rtc_set();
+       } else if (alarm_info->due_time == alarm_context.c_due_time) {
+               _add_to_scheduled_alarm_list(alarm_info);
+       }
+
+       return true;
+}
+
+static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
+                          long requested_interval, uid_t uid, int pid, char *bundle_data, int *error_code)
+{
        char app_name[MAX_APP_ID_LEN] = { 0 };
        bundle *b;
        const char *callee_appid = NULL;
@@ -862,57 +951,14 @@ static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id
        memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
        __alarm_generate_alarm_id(__alarm_info, alarm_id);
 
-       time(&current_time);
-
-       if (__alarm_info->alarm_info.mode.repeat == ALARM_REPEAT_MODE_ONCE)
-               __alarm_info->alarm_info.reserved_info = current_time;
-
-       if (alarm_context.c_due_time < current_time) {
-               LOGE("Caution!! alarm_context.c_due_time "
-               "(%ld) is less than current time(%ld)", alarm_context.c_due_time, current_time);
-               alarm_context.c_due_time = -1;
+       if (__alarm_add_and_set(__alarm_info, pid) == false) {
+               *error_code = ERR_ALARM_INVALID_TIME;
+               _release_alarm_info_t(__alarm_info);
+               return false;
        }
 
-       _alarm_set_next_duetime(__alarm_info);
-       __alarm_add_to_list(__alarm_info);
-
        _save_alarm_info_log("CREATE SVC", __alarm_info);
 
-       if (__alarm_info->due_time == 0) {
-               LOGE("[alarm-server]:Create a new alarm: "
-               "__alarm_info->due_time is 0, alarm(%d) \n", *alarm_id);
-               return true;
-       } else if (current_time == __alarm_info->due_time) {
-               LOGE("[alarm-server]:Create alarm: "
-                    "current_time(%ld) is same as __alarm_info->due_time(%ld)", current_time,
-                    __alarm_info->due_time);
-               return true;
-       } else if (difftime(__alarm_info->due_time, current_time) < 0) {
-               LOGE("[alarm-server]: Expired Due Time.\
-                               [Due time=%ld, Current Time=%ld]!!!Do not add to schedule list\n",
-                               __alarm_info->due_time, current_time);
-               return true;
-       } else {
-               localtime_r(&(__alarm_info->due_time), &ts_ret);
-               strftime(due_time_r, 30, "%c", &ts_ret);
-               SECURE_LOGD("[alarm-server]:Create a new alarm: "
-                                   "alarm(%d) __alarm_info->due_time(%s)", *alarm_id,
-                                   due_time_r);
-       }
-
-       LOGD("[alarm-server]:alarm_context.c_due_time(%ld),\
-                       __alarm_info->due_time(%ld)",
-                       alarm_context.c_due_time, __alarm_info->due_time);
-
-       if (alarm_context.c_due_time == -1 || __alarm_info->due_time < alarm_context.c_due_time) {
-               _clear_scheduled_alarm_list();
-               _add_to_scheduled_alarm_list(__alarm_info);
-               _alarm_set_timer(alarm_context.timer, __alarm_info->due_time);
-               _rtc_set();
-       } else if (__alarm_info->due_time == alarm_context.c_due_time) {
-               _add_to_scheduled_alarm_list(__alarm_info);
-       }
-
        return true;
 }
 
@@ -922,11 +968,8 @@ static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id, uid_t
                        const char *dst_service_name, const char *dst_service_name_mod,
                        int *error_code)
 {
-       time_t current_time;
        char unique_name[MAX_APP_ID_LEN] = { 0 };
        bool caller_is_app = false;
-       char due_time_r[100] = { 0 };
-       struct tm ts_ret;
 
        __alarm_info_t *__alarm_info = NULL;
 
@@ -971,60 +1014,14 @@ static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id, uid_t
        memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
        __alarm_generate_alarm_id(__alarm_info, alarm_id);
 
-       time(&current_time);
-
-       if (__alarm_info->alarm_info.mode.repeat == ALARM_REPEAT_MODE_ONCE)
-               __alarm_info->alarm_info.reserved_info = current_time;
-
-       SECURE_LOGD("[alarm-server]:pid=%d, app_unique_name=%s, "
-                       "app_service_name=%s,dst_service_name=%s, c_due_time=%ld", \
-                       pid, __alarm_info->app_unique_name, \
-                       __alarm_info->app_service_name, \
-                       __alarm_info->dst_service_name, \
-                       alarm_context.c_due_time);
-
-       if (alarm_context.c_due_time < current_time) {
-               LOGE("Caution!! alarm_context.c_due_time "
-               "(%ld) is less than current time(%ld)", alarm_context.c_due_time, current_time);
-               alarm_context.c_due_time = -1;
+       if (__alarm_add_and_set(__alarm_info, pid) == false) {
+               *error_code = ERR_ALARM_INVALID_TIME;
+               _release_alarm_info_t(__alarm_info);
+               return false;
        }
 
-       _alarm_set_next_duetime(__alarm_info);
-       __alarm_add_to_list(__alarm_info);
-
        _save_alarm_info_log("CREATE", __alarm_info);
 
-       if (__alarm_info->due_time == 0) {
-               LOGE("[alarm-server]:Create a new alarm: due_time is 0, alarm(%d).", *alarm_id);
-               return true;
-       } else if (current_time == __alarm_info->due_time) {
-               LOGE("[alarm-server]:Create alarm: current_time(%ld)\
-                               is same as due_time(%ld).", current_time, __alarm_info->due_time);
-               return true;
-       } else if (difftime(__alarm_info->due_time, current_time) <  0) {
-               LOGE("[alarm-server]: Expired Due Time.[Due time=%ld,\
-                               Current Time=%ld]!!!Do not add to schedule list.",
-                       __alarm_info->due_time, current_time);
-               return true;
-       } else {
-               localtime_r(&(__alarm_info->due_time), &ts_ret);
-               strftime(due_time_r, 30, "%c", &ts_ret);
-               SECURE_LOGD("[alarm-server]:Create a new alarm: alarm(%d) due_time(%s)",
-                               *alarm_id, due_time_r);
-       }
-
-       LOGD("[alarm-server]:alarm_context.c_due_time(%ld), due_time(%ld)",
-                       alarm_context.c_due_time, __alarm_info->due_time);
-
-       if (alarm_context.c_due_time == -1 || __alarm_info->due_time < alarm_context.c_due_time) {
-               _clear_scheduled_alarm_list();
-               _add_to_scheduled_alarm_list(__alarm_info);
-               _alarm_set_timer(alarm_context.timer, __alarm_info->due_time);
-               _rtc_set();
-       } else if (__alarm_info->due_time == alarm_context.c_due_time) {
-               _add_to_scheduled_alarm_list(__alarm_info);
-       }
-
        return true;
 }
 
@@ -1077,9 +1074,6 @@ end:
 static bool __alarm_create_noti(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
                           long requested_interval, uid_t uid, int pid, char *noti_data, int *error_code)
 {
-       time_t current_time;
-       struct tm ts_ret;
-       char due_time_r[100] = { 0 };
        char app_name[MAX_APP_ID_LEN] = { 0 };
        bool caller_is_app = false;
        char *new_noti_data = NULL;
@@ -1128,59 +1122,14 @@ static bool __alarm_create_noti(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
        memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
        __alarm_generate_alarm_id(__alarm_info, alarm_id);
 
-       time(&current_time);
-
-       if (__alarm_info->alarm_info.mode.repeat == ALARM_REPEAT_MODE_ONCE)
-               __alarm_info->alarm_info.reserved_info = current_time;
-
-       SECURE_LOGD("[alarm-server]:pid=%d, app_unique_name=%s, "
-                       "app_service_name=%s,dst_service_name=%s, c_due_time=%ld", \
-                       pid, __alarm_info->app_unique_name, \
-                       __alarm_info->app_service_name, \
-                       __alarm_info->dst_service_name, \
-                       alarm_context.c_due_time);
-
-       if (alarm_context.c_due_time < current_time) {
-               LOGE("Caution!! alarm_context.c_due_time "
-               "(%ld) is less than current time(%ld)", alarm_context.c_due_time, current_time);
-               alarm_context.c_due_time = -1;
+       if (__alarm_add_and_set(__alarm_info, pid) == false) {
+               *error_code = ERR_ALARM_INVALID_TIME;
+               _release_alarm_info_t(__alarm_info);
+               return false;
        }
 
-       _alarm_set_next_duetime(__alarm_info);
-       __alarm_add_to_list(__alarm_info);
-
        _save_alarm_info_log("CREATE NOTI", __alarm_info);
 
-       if (__alarm_info->due_time == 0) {
-               LOGE("[alarm-server]:Create a new alarm: due_time is 0, alarm(%d).", *alarm_id);
-               return true;
-       } else if (current_time == __alarm_info->due_time) {
-               LOGE("[alarm-server]:Create alarm: current_time(%ld)\
-                               is same as due_time(%ld).", current_time, __alarm_info->due_time);
-               return true;
-       } else if (difftime(__alarm_info->due_time, current_time) <  0) {
-               LOGE("[alarm-server]: Expired Due Time.[Due time=%ld,\
-                               Current Time=%ld]!!!Do not add to schedule list.",
-                       __alarm_info->due_time, current_time);
-               return true;
-       } else {
-               localtime_r(&(__alarm_info->due_time), &ts_ret);
-               strftime(due_time_r, 30, "%c", &ts_ret);
-               SECURE_LOGD("[alarm-server]:Create a new alarm: alarm(%d) due_time(%s)", *alarm_id, due_time_r);
-       }
-
-       LOGD("[alarm-server]:alarm_context.c_due_time(%ld), due_time(%ld)",
-                       alarm_context.c_due_time, __alarm_info->due_time);
-
-       if (alarm_context.c_due_time == -1 || __alarm_info->due_time < alarm_context.c_due_time) {
-               _clear_scheduled_alarm_list();
-               _add_to_scheduled_alarm_list(__alarm_info);
-               _alarm_set_timer(alarm_context.timer, __alarm_info->due_time);
-               _rtc_set();
-       } else if (__alarm_info->due_time == alarm_context.c_due_time) {
-               _add_to_scheduled_alarm_list(__alarm_info);
-       }
-
        return true;
 }
 
@@ -1189,9 +1138,9 @@ static bool __alarm_update(uid_t uid, alarm_id_t alarm_id,
 {
        time_t current_time;
        char due_time_r[100] = { 0 };
-       struct tm ts_ret;
        __alarm_info_t *__alarm_info = NULL;
        bool result = false;
+       struct tm ts_ret;
 
        time(&current_time);
 
@@ -1893,17 +1842,17 @@ static long __get_proper_interval(long interval, int alarm_type)
        return maxInterval;
 }
 
-gboolean __alarm_expired_directly(gpointer user_data)
+static gboolean __alarm_expired_directly(gpointer user_data)
 {
        if (g_scheduled_alarm_list == NULL || g_scheduled_alarm_list->data == NULL)
                return false;
 
-       time_t time_sec = (time_t)(intptr_t)user_data;
+       time_t *time_sec = (time_t *)user_data;
        __scheduled_alarm_t *alarm = (__scheduled_alarm_t *)g_scheduled_alarm_list->data;
        __alarm_info_t *alarm_info = alarm->__alarm_info;
 
        /* Expire alarms with duetime equal to newtime by force */
-       if (alarm_info->due_time == time_sec) {
+       if (alarm_info->due_time == *time_sec) {
                if (_display_lock_state(DEVICED_LCD_OFF, DEVICED_STAY_CUR_STATE, 0) != ALARMMGR_RESULT_SUCCESS)
                        LOGE("_display_lock_state() is failed");
 
@@ -1921,6 +1870,9 @@ gboolean __alarm_expired_directly(gpointer user_data)
                        LOGE("_display_unlock_state() is failed");
        }
 
+       if (time_sec)
+               free(time_sec);
+
        return false;
 }
 
@@ -1928,6 +1880,7 @@ void __reschedule_alarms_with_newtime(time_t cur_time, time_t new_time, double d
 {
        char log_message[ALARMMGR_LOG_MESSAGE_SIZE] = {0,};
 
+       time_t *_new_time;
        vconf_set_int(VCONFKEY_SYSTEM_TIME_CHANGED, (int)diff_time);
        bundle *b = NULL;
        b = bundle_create();
@@ -1950,7 +1903,15 @@ void __reschedule_alarms_with_newtime(time_t cur_time, time_t new_time, double d
 
        _save_module_log("CHANGE TIME", log_message);
 
-       g_idle_add(__alarm_expired_directly, (gpointer)(intptr_t)new_time); /* Expire alarms with duetime equal to newtime directly */
+       _new_time = malloc(sizeof(time_t));
+       if (_new_time) {
+               *_new_time = new_time;
+       } else {
+               LOGE("Out of memory");
+               return;
+       }
+
+       g_idle_add(__alarm_expired_directly, (gpointer)_new_time); /* Expire alarms with duetime equal to newtime directly */
        return;
 }