Merge branch 'master' into tizen_2.1
[framework/appfw/alarm-manager.git] / alarm-manager.c
index 831842c..951b96b 100755 (executable)
@@ -134,6 +134,7 @@ static void __alarm_handler(int sigNum, siginfo_t *pSigInfo, void *pUContext);
 static void __clean_registry();
 static bool __alarm_manager_reset();
 static void __on_system_time_changed(keynode_t *node, void *data);
+static void __on_system_time_external_changed(keynode_t *node, void *data);
 static void __initialize_timer();
 static void __initialize_alarm_list();
 static void __initialize_scheduled_alarm_lsit();
@@ -180,8 +181,8 @@ static void __rtc_set()
        }
 
        ALARM_MGR_LOG_PRINT("\n\nCurrent RTC date/time is %d-%d-%d, "
-               "%02d:%02d:%02d.\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1, 
-               rtc_tm.tm_year + 1900, rtc_tm.tm_hour, rtc_tm.tm_min, 
+               "%02d:%02d:%02d.\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1,
+               rtc_tm.tm_year + 1900, rtc_tm.tm_hour, rtc_tm.tm_min,
                rtc_tm.tm_sec);
 
        ALARM_MGR_LOG_PRINT("alarm_context.c_due_time is %d\n", \
@@ -202,8 +203,8 @@ static void __rtc_set()
                rtc_wk.pending = 0;
 
                ALARM_MGR_LOG_PRINT("\n\nSetted RTC Alarm date/time is "
-                       "%d-%d-%d, %02d:%02d:%02d.\n", rtc_tm.tm_mday, 
-                       rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900, 
+                       "%d-%d-%d, %02d:%02d:%02d.\n", rtc_tm.tm_mday,
+                       rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
                        rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
 
                retval = ioctl(fd, RTC_WKALM_SET, &rtc_wk);
@@ -241,11 +242,11 @@ int _set_rtc_time(time_t _time)
        int fd1 = 0;
        int retval0 = 0;
        int retval1 = 0;
-       struct rtc_time rtc_tm;
+       struct rtc_time rtc_tm = { 0, };
        const char *rtc0 = power_rtc;
        const char *rtc1 = default_rtc;
-       struct tm *_tm;
-       struct tm time_r;
+       struct tm *_tm = NULL;
+       struct tm time_r = { 0, };
 
        fd0 = open(rtc0, O_RDONLY);
        fd1 = open(rtc1, O_RDONLY);
@@ -263,7 +264,7 @@ int _set_rtc_time(time_t _time)
        memset(&rtc_tm, 0, sizeof(struct rtc_time));
 
 
-       _tm = gmtime_r(&_time, &time_r);
+       (void) gmtime_r(&_time, &time_r);
 
        /* Write the RTC time/date 2008:05:21 19:20:00 */
 
@@ -273,26 +274,30 @@ int _set_rtc_time(time_t _time)
        rtc_tm.tm_year = time_r.tm_year;
        rtc_tm.tm_hour = time_r.tm_hour;
        rtc_tm.tm_min = time_r.tm_min;
-       rtc_tm.tm_sec = 0;
-       
+       rtc_tm.tm_sec = time_r.tm_sec;
+
 
        retval0 = ioctl(fd0, RTC_SET_TIME, &rtc_tm);
 
        if (retval0 == -1) {
-               close(fd0);
+               if (fd0 != -1)
+                       close(fd0);
                ALARM_MGR_LOG_PRINT("error to ioctl fd0.");
                perror("\t");
        }
-       close(fd0);
+       if (fd0 != -1)
+               close(fd0);
 
        retval1 = ioctl(fd1, RTC_SET_TIME, &rtc_tm);
 
        if (retval1 == -1) {
-               close(fd1);
+               if (fd1 != -1)
+                       close(fd1);
                ALARM_MGR_LOG_PRINT("error to ioctl fd1.");
                perror("\t");
        }
-       close(fd1);
+       if (fd1 != -1)
+               close(fd1);
 
        return 1;
 }
@@ -344,7 +349,7 @@ static bool __alarm_add_to_list(__alarm_info_t *__alarm_info,
        for (iter = alarm_context.alarms; iter != NULL;
             iter = g_slist_next(iter)) {
                entry = iter->data;
-               /*ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n", 
+               /*ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n",
                   entry->alarm_id); */
        }
 
@@ -498,12 +503,12 @@ static bool alarm_get_tz_info(int *gmt_idx, int *dst)
        GConfValue *value1 = NULL;
        GConfValue *value2 = NULL;
        GConfClient* gConfClient = NULL;
-       GError* err = NULL;             
-       
+       GError* err = NULL;
+
        gConfClient = gconf_client_get_default();
 
        if(gConfClient) {
-               value1 = gconf_client_get(gConfClient, SETTINGS_CLOCKTIMEZONE, 
+               value1 = gconf_client_get(gConfClient, SETTINGS_CLOCKTIMEZONE,
                                                                        &err);
                if (err) {
                        ALARM_MGR_LOG_PRINT("__on_system_time_changed:
@@ -514,8 +519,8 @@ static bool alarm_get_tz_info(int *gmt_idx, int *dst)
                }
                *gmt_idx = gconf_value_get_int(value1);
                ALARM_MGR_LOG_PRINT("gconf return gmt_idx =%d\n ", *gmt_idx);
-       
-               value2 = gconf_client_get(gConfClient, 
+
+               value2 = gconf_client_get(gConfClient,
                        SETTINGS_DAYLIGHTSTATUS, &err);
                if (err) {
                        ALARM_MGR_LOG_PRINT("__on_system_time_changed:
@@ -523,10 +528,10 @@ static bool alarm_get_tz_info(int *gmt_idx, int *dst)
                g_error_free(err);
                err = NULL;
        }
-       
+
        *dst = gconf_value_get_int(value2);
        ALARM_MGR_LOG_PRINT("gconf return dst =%d\n ", *dst);
-       
+
        if(gConfClient != NULL) {
                g_object_unref(gConfClient);
                gConfClient = NULL;
@@ -534,7 +539,7 @@ static bool alarm_get_tz_info(int *gmt_idx, int *dst)
        }
        else
                ALARM_MGR_LOG_PRINT("check the gconf setting failed!!!!! \n ");
-       
+
        if(value1) {
                gconf_value_free(value1);
                value1 = NULL;
@@ -557,7 +562,7 @@ static bool __alarm_update_due_time_of_all_items_in_list(double diff_time)
        __alarm_info_t *entry = NULL;
        struct tm *p_time = NULL ;
        struct tm due_time_result ;
-       struct tm fixed_time ;
+       struct tm fixed_time = { 0, };
 
        for (iter = alarm_context.alarms; iter != NULL;
             iter = g_slist_next(iter)) {
@@ -568,7 +573,7 @@ static bool __alarm_update_due_time_of_all_items_in_list(double diff_time)
 
                        entry->due_time += diff_time;
 
-                       alarm_date_t *start = &alarm_info->start; /**< start 
+                       alarm_date_t *start = &alarm_info->start; /**< start
                                                        time of the alarm */
                        alarm_date_t *end = &alarm_info->end;;
                                                /**< end time of the alarm */
@@ -587,7 +592,7 @@ static bool __alarm_update_due_time_of_all_items_in_list(double diff_time)
                                end->month = p_time->tm_mon + 1;
                                end->day = p_time->tm_mday;
 
-                               
+
                                memset(&fixed_time, 0, sizeof(fixed_time));
                                fixed_time.tm_year = p_time->tm_year;
                                fixed_time.tm_mon = p_time->tm_mon;
@@ -625,7 +630,7 @@ static bool __alarm_update_due_time_of_all_items_in_list(double diff_time)
 
                ALARM_MGR_LOG_PRINT("alarm[%d] with duetime(%u) at "
                "current(%u)\n", entry->alarm_id, due_time, current_time);
-               if (due_time == 0) {    /* 0 means this alarm has been 
+               if (due_time == 0) {    /* 0 means this alarm has been
                                           disabled */
                        continue;
                }
@@ -673,7 +678,7 @@ static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id
        if (__alarm_info == NULL) {
                ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, malloc "
                                          "failed. it seems to be OOM\n", pid);
-               *error_code = -1;       /* -1 means that system 
+               *error_code = -1;       /* -1 means that system
                                           failed internally. */
                return false;
        }
@@ -688,7 +693,15 @@ static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id
        snprintf(proc_file, 512, "/proc/%d/cmdline", pid);
 
        fd = open(proc_file, O_RDONLY);
-       if (fd > 0) {
+       if (fd < 0) {           /* failure */
+               ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
+                                         "killed, so we failed to get proc file(%s) and do not create "
+                                         "alarm_info\n", pid, proc_file);
+               *error_code = -1;       /*-1 means that system failed
+                                                       internally.*/
+               free(__alarm_info);
+               return false;
+       } else {
                ret = read(fd, process_name, 512);
                close(fd);
                if (ret <=0)
@@ -714,14 +727,6 @@ static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id
                }
                __alarm_info->quark_app_unique_name =
                    g_quark_from_string(app_name);
-       } else {                /* failure */
-               ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
-                                         "killed, so we failed to get proc file(%s) and do not create "
-                                         "alarm_info\n", pid, proc_file);
-               *error_code = -1;       /*-1 means that system failed 
-                                                       internally.*/
-               free(__alarm_info);
-               return false;
        }
 
        __alarm_info->quark_bundle=g_quark_from_string(bundle_data);
@@ -817,7 +822,7 @@ static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
        if (__alarm_info == NULL) {
                ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, malloc "
                                          "failed. it seems to be OOM\n", pid);
-               *error_code = -1;       /* -1 means that system 
+               *error_code = -1;       /* -1 means that system
                                           failed internally. */
                return false;
        }
@@ -834,7 +839,17 @@ static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
        int ret;
        int i = 0;
        fd = open(proc_file, O_RDONLY);
-       if (fd > 0) {
+       if (fd < 0) {   /* failure */
+               __alarm_info->quark_app_unique_name =
+                   g_quark_from_string("unknown");
+               ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
+                                         "killed, so we failed to get proc file(%s) and do not create "
+                                         "alarm_info\n", pid, proc_file);
+               *error_code = -1;       /*-1 means that system failed
+                                                       internally.*/
+               free(__alarm_info);
+               return false;
+       } else {
                ret = read(fd, process_name, 512);
                close(fd);
                while (process_name[i] != '\0') {
@@ -855,17 +870,6 @@ static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
                }
                __alarm_info->quark_app_unique_name =
                    g_quark_from_string(app_name);
-       } else {                /* failure */
-
-               __alarm_info->quark_app_unique_name =
-                   g_quark_from_string("unknown");
-               ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
-                                         "killed, so we failed to get proc file(%s) and do not create "
-                                         "alarm_info\n", pid, proc_file);
-               *error_code = -1;       /*-1 means that system failed 
-                                                       internally.*/
-               free(__alarm_info);
-               return false;
        }
 
        __alarm_info->quark_app_service_name =
@@ -948,7 +952,7 @@ static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
        /*alarm boot */
        if (enable_power_on_alarm) {
                /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
-                * platform with app-server.because __alarm_power_on(..) fuction don't 
+                * platform with app-server.because __alarm_power_on(..) fuction don't
                 * use first parameter internally, we set this value to 0(zero)
                 */
                __alarm_power_on(0, enable_power_on_alarm, NULL);
@@ -970,7 +974,7 @@ static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
        if (__alarm_info == NULL) {
                ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, "
                        "malloc failed. it seems to be OOM\n", pid);
-               *error_code = -1;       /*-1 means that system failed 
+               *error_code = -1;       /*-1 means that system failed
                                                internally.*/
                return false;
        }
@@ -1002,8 +1006,8 @@ static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
                "(%d) does not exist. so this value is invalid id.", alarm_id);
                return false;
        }
-       /* ALARM_MGR_LOG_PRINT("[alarm-server]:request_pid=%d, alarm_id=%d, 
-        * app_unique_name=%s, app_service_name=%s, dst_service_name=%s, 
+       /* ALARM_MGR_LOG_PRINT("[alarm-server]:request_pid=%d, alarm_id=%d,
+        * app_unique_name=%s, app_service_name=%s, dst_service_name=%s,
         * c_due_time=%d", pid, alarm_id, g_quark_to_string
         * (__alarm_info->quark_app_unique_name), g_quark_to_string
         * (__alarm_info->quark_app_service_name), g_quark_to_string
@@ -1025,7 +1029,7 @@ static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
 #ifdef __ALARM_BOOT
                /*alarm boot */
                if (enable_power_on_alarm) {
-                       /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in 
+                       /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in
                         * a platform with app-server.because __alarm_power_on(..) fuction don't
                         * use first parameter internally, we set this value to 0(zero)
                         */
@@ -1054,6 +1058,7 @@ static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
                return true;
        }else if (difftime(due_time, current_time)< 0){
                ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: Expired Due Time.[Due time=%d, Current Time=%d]!!!Do not add to schedule list\n", due_time, current_time);
+               free(__alarm_info);
                return true;
        } else {
                char due_time_r[100] = { 0 };
@@ -1087,13 +1092,14 @@ static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
 #ifdef __ALARM_BOOT
        /*alarm boot */
        if (enable_power_on_alarm) {
-               /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) 
-                * in a platform with app-server.because __alarm_power_on(..) fuction 
+               /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID)
+                * in a platform with app-server.because __alarm_power_on(..) fuction
                 * don't use first parameter internally, we set this value to 0(zero)
                 */
                __alarm_power_on(0, enable_power_on_alarm, NULL);
        }
 #endif
+       free(__alarm_info);
 
        return true;
 }
@@ -1124,8 +1130,8 @@ static bool __alarm_delete(int pid, alarm_id_t alarm_id, int *error_code)
 #ifdef __ALARM_BOOT
        /*alarm boot */
        if (enable_power_on_alarm) {
-               /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a 
-                * platform with app-server.because __alarm_power_on(..) fuction don't 
+               /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
+                * platform with app-server.because __alarm_power_on(..) fuction don't
                 * use first parameter internally, we set this value to 0(zero)
                 */
                __alarm_power_on(0, enable_power_on_alarm, NULL);
@@ -1140,9 +1146,8 @@ static bool __alarm_power_on(int app_id, bool on_off, int *error_code)
 #ifdef __ALARM_BOOT
        time_t min_time = 0;
        time_t current_time = 0;
-       struct tm *temp_info;
        struct rtc_time rtc_tm = { 0, };
-       struct tm min_time_r;
+       struct tm min_time_r = { 0, };
        int fd = 0;
        int retval;
 
@@ -1167,7 +1172,7 @@ static bool __alarm_power_on(int app_id, bool on_off, int *error_code)
                        if (min_time <= current_time)
                                min_time = current_time + 5;
 
-                       temp_info = gmtime_r(&min_time, &min_time_r);
+                       gmtime_r(&min_time, &min_time_r);
 
                        ALARM_MGR_LOG_PRINT("__alarm_power_on : %d %d %d %d "
                                                "%d\n", \
@@ -1193,7 +1198,7 @@ static bool __alarm_power_on(int app_id, bool on_off, int *error_code)
                        /*set_info.time_zone = 0; */
                        /*set_info.u_interval.day_of_week = 0; */
 
-                       /*ALARM_MGR_LOG_PRINT("####__alarm_power_on : %d %d 
+                       /*ALARM_MGR_LOG_PRINT("####__alarm_power_on : %d %d
                        %d %d %d\n",set_info.year,set_info.month,set_info.day,
                        set_info.hour,set_info.minute); */
 
@@ -1202,7 +1207,7 @@ static bool __alarm_power_on(int app_id, bool on_off, int *error_code)
                        ALARM_MGR_LOG_PRINT("\n\nSetted RTC Alarm date/time is "
                                            "%d-%d-%d, %02d:%02d:%02d.\n",
                                            rtc_tm.tm_mday, rtc_tm.tm_mon + 1,
-                               rtc_tm.tm_year + 1900, rtc_tm.tm_hour, 
+                               rtc_tm.tm_year + 1900, rtc_tm.tm_hour,
                                rtc_tm.tm_min, rtc_tm.tm_sec);
 
                        retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
@@ -1312,12 +1317,12 @@ static void __alarm_send_noti_to_application(const char *app_service_name,
 
        dbus_message_set_no_reply(message, TRUE);
        /*      if(service_name[0]==':') */
-       /* we don't need auto activation in a case that 
+       /* we don't need auto activation in a case that
           destination_app_service_name starts with a charactor like (:) */
        dbus_message_set_auto_start(message, FALSE);
 
        dbus_message_iter_init_append(message, &iter);
-       if (!dbus_message_iter_append_basic 
+       if (!dbus_message_iter_append_basic
                (&iter, DBUS_TYPE_INT32, &alarm_id)) {
                dbus_message_unref(message);
                ALARM_MGR_EXCEPTION_PRINT("[alarm server] "
@@ -1342,6 +1347,7 @@ static void __alarm_expired()
        char alarm_id_val[32]={0,};
        int b_len = 0;
        bundle *b = NULL;
+       char *appid = NULL;
 
        ALARM_MGR_LOG_PRINT("[alarm-server]: Enter \n");
 
@@ -1356,7 +1362,12 @@ static void __alarm_expired()
                current_time, interval);
 
        if (alarm_context.c_due_time > current_time) {
-               ALARM_MGR_LOG_PRINT("[alarm-server]: False Alarm\n");
+               ALARM_MGR_LOG_PRINT("[alarm-server]: False Alarm (time changed to past)\n");
+               goto done;
+       }
+       // 3 seconds is maximum permitted delay from timer expire to this function
+       if (alarm_context.c_due_time + 3 < current_time) {
+               ALARM_MGR_LOG_PRINT("[alarm-server]: False Alarm (time changed to future)\n");
                goto done;
        }
 
@@ -1393,13 +1404,21 @@ static void __alarm_expired()
                                        }
                                        else
                                        {
-                                               if ( appsvc_run_service(b, 0, NULL, NULL) < 0)
+                                               appid = appsvc_get_appid(b);
+                                               if( (__alarm_info->alarm_info.alarm_type & ALARM_TYPE_NOLAUNCH) && !aul_app_is_running(appid))
                                                {
-                                                       ALARM_MGR_EXCEPTION_PRINT("Unable to run app svc\n");
+                                                       ALARM_MGR_EXCEPTION_PRINT("This alarm is ignored\n");
                                                }
                                                else
                                                {
-                                                       ALARM_MGR_LOG_PRINT("Successfuly ran app svc\n");
+                                                       if ( appsvc_run_service(b, 0, NULL, NULL) < 0)
+                                                       {
+                                                               ALARM_MGR_EXCEPTION_PRINT("Unable to run app svc\n");
+                                                       }
+                                                       else
+                                                       {
+                                                               ALARM_MGR_LOG_PRINT("Successfuly ran app svc\n");
+                                                       }
                                                }
                                        }
                                        bundle_free(b);
@@ -1415,23 +1434,23 @@ static void __alarm_expired()
                                "null, so we send expired alarm to %s(%u)\n",\
                                        g_quark_to_string(
                                        __alarm_info->quark_app_service_name),
-                                       __alarm_info->quark_app_service_name); 
+                                       __alarm_info->quark_app_service_name);
                                        destination_app_service_name = g_quark_to_string(
                                        __alarm_info->quark_app_service_name_mod);
                        } else {
                                ALARM_MGR_LOG_PRINT("[alarm-server]:destination "
                                                    ":%s(%u)\n",
                                        g_quark_to_string(
-                                       __alarm_info->quark_dst_service_name), 
+                                       __alarm_info->quark_dst_service_name),
                                        __alarm_info->quark_dst_service_name);
                                        destination_app_service_name = g_quark_to_string(
                                                __alarm_info->quark_dst_service_name_mod);
                        }
 
 #ifdef __ALARM_BOOT
-                       /* orginally this code had if(__alarm_info->app_id==21) in a 
+                       /* orginally this code had if(__alarm_info->app_id==21) in a
                           platform with app-server. */
-                       /*if(__alarm_info->quark_dst_service_name  == 
+                       /*if(__alarm_info->quark_dst_service_name  ==
                           g_quark_from_string (WAKEUP_ALARM_APP_ID)) */
                        if (strcmp
                            (g_quark_to_string(__alarm_info->quark_dst_service_name),
@@ -1447,8 +1466,8 @@ static void __alarm_expired()
                        }
 #endif
 
-                       /* 
-                        * we should consider a situation that 
+                       /*
+                        * we should consider a situation that
                         * destination_app_service_name is owner_name like (:xxxx) and
                         * application's pid which registered this alarm was killed.In that case,
                         * we don't need to send the expire event because the process was killed.
@@ -1457,7 +1476,7 @@ static void __alarm_expired()
                        ALARM_MGR_LOG_PRINT("[alarm-server]: "
                                            "destination_app_service_name :%s, app_pid=%d\n",
                                            destination_app_service_name, app_pid);
-                       /* the following is a code that checks the above situation. 
+                       /* the following is a code that checks the above situation.
                           please verify this code. */
 
                        if (dbus_bus_name_has_owner(
@@ -1467,14 +1486,20 @@ static void __alarm_expired()
                                char appid[MAX_SERVICE_NAME_LEN] = { 0, };
                                char alarm_id_str[32] = { 0, };
 
+                               if (__alarm_info->alarm_info.alarm_type & ALARM_TYPE_WITHCB) {
+                                       __alarm_remove_from_list(__alarm_info->pid, alarm_id, NULL);
+                                       goto done;
+                               }
+
                                expire_info = malloc(sizeof(__expired_alarm_t));
                                if (G_UNLIKELY(NULL == expire_info)){
                                        ALARM_MGR_ASSERT_PRINT("[alarm-server]:Malloc failed!Can't notify alarm expiry info\n");
                                        goto done;
                                }
+                               memset(expire_info, '\0', MAX_SERVICE_NAME_LEN);
                                strncpy(expire_info->service_name,
                                        destination_app_service_name,
-                                       MAX_SERVICE_NAME_LEN);
+                                       MAX_SERVICE_NAME_LEN-1);
                                expire_info->alarm_id = alarm_id;
                                g_expired_alarm_list =
                                    g_slist_append(g_expired_alarm_list, expire_info);
@@ -1509,9 +1534,9 @@ static void __alarm_expired()
                }
                ALARM_MGR_LOG_PRINT("after __alarm_send_noti_to_application\n");
 
-/*             if( !(__alarm_info->alarm_info.alarm_type 
+/*             if( !(__alarm_info->alarm_info.alarm_type
                                        & ALARM_TYPE_VOLATILE) ) {
-                       __alarm_remove_from_list(__alarm_info->pid, 
+                       __alarm_remove_from_list(__alarm_info->pid,
                                                        alarm_id, NULL);
                }
                else */
@@ -1555,8 +1580,8 @@ static gboolean __alarm_handler_idle()
 #ifdef __ALARM_BOOT
        /*alarm boot */
        if (enable_power_on_alarm) {
-               /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) 
-                *in a platform with app-server.because __alarm_power_on(..) fuction 
+               /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID)
+                *in a platform with app-server.because __alarm_power_on(..) fuction
                 *don't use first parameter internally, we set this value to 0(zero)
                 */
                __alarm_power_on(0, enable_power_on_alarm, NULL);
@@ -1626,8 +1651,54 @@ static void __on_system_time_changed(keynode_t *node, void *data)
        vconf_set_dbl(VCONFKEY_SYSTEM_TIMEDIFF, diff_time);
        vconf_set_int(VCONFKEY_SYSTEM_TIME_CHANGED,(int)diff_time);
 
-       if (heynoti_publish(SYSTEM_TIME_CHANGED))
-               ALARM_MGR_EXCEPTION_PRINT("alarm-server: Unable to publish heynoti for system time change\n");
+       __alarm_update_due_time_of_all_items_in_list(diff_time);
+
+       ALARM_MGR_LOG_PRINT("2.alarm_context.c_due_time is %d\n",
+                           alarm_context.c_due_time);
+       _clear_scheduled_alarm_list();
+       _alarm_schedule();
+       __rtc_set();
+#ifdef __ALARM_BOOT
+       /*alarm boot */
+       if (enable_power_on_alarm) {
+/* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
+APP_ID) in a platform with app-server. because _alarm_power_
+on(..) fuction don't use first parameter internally, we set
+this value to 0(zero)
+*/
+               __alarm_power_on(0, enable_power_on_alarm, NULL);
+       }
+#endif
+       return;
+}
+
+static void __on_system_time_external_changed(keynode_t *node, void *data)
+{
+       double diff_time;
+       time_t cur_time = 0;
+
+       _alarm_disable_timer(alarm_context);
+
+       if (node) {
+               diff_time = vconf_keynode_get_dbl(node);
+       } else {
+               vconf_get_dbl(VCONFKEY_SYSTEM_TIMECHANGE_EXTERNAL, &diff_time);
+       }
+
+       tzset();
+
+       ALARM_MGR_ASSERT_PRINT("diff_time is %f\n", diff_time);
+
+       ALARM_MGR_LOG_PRINT("[alarm-server] System time has been changed externally\n");
+       ALARM_MGR_LOG_PRINT("1.alarm_context.c_due_time is %d\n",
+                           alarm_context.c_due_time);
+
+       // set rtc time only because the linux time is set externally
+       time(&cur_time);
+       _set_rtc_time(cur_time);
+
+       vconf_set_dbl(VCONFKEY_SYSTEM_TIMEDIFF, diff_time);
+       vconf_set_int(VCONFKEY_SYSTEM_TIME_CHANGED,(int)diff_time);
 
        __alarm_update_due_time_of_all_items_in_list(diff_time);
 
@@ -1641,7 +1712,7 @@ static void __on_system_time_changed(keynode_t *node, void *data)
        if (enable_power_on_alarm) {
 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
 APP_ID) in a platform with app-server. because _alarm_power_
-on(..) fuction don't use first parameter internally, we set 
+on(..) fuction don't use first parameter internally, we set
 this value to 0(zero)
 */
                __alarm_power_on(0, enable_power_on_alarm, NULL);
@@ -1650,6 +1721,38 @@ this value to 0(zero)
        return;
 }
 
+static void __on_time_zone_changed(keynode_t *node, void *data)
+{
+       double diff_time = 0;
+
+       _alarm_disable_timer(alarm_context);
+
+       tzset();
+
+       ALARM_MGR_LOG_PRINT("[alarm-server] time zone has been changed\n");
+       ALARM_MGR_LOG_PRINT("1.alarm_context.c_due_time is %d\n", alarm_context.c_due_time);
+
+       __alarm_update_due_time_of_all_items_in_list(diff_time);
+
+       ALARM_MGR_LOG_PRINT("2.alarm_context.c_due_time is %d\n", alarm_context.c_due_time);
+       _clear_scheduled_alarm_list();
+       _alarm_schedule();
+       __rtc_set();
+#ifdef __ALARM_BOOT
+       /*alarm boot */
+       if (enable_power_on_alarm) {
+/* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
+APP_ID) in a platform with app-server. because _alarm_power_
+on(..) fuction don't use first parameter internally, we set
+this value to 0(zero)
+*/
+               __alarm_power_on(0, enable_power_on_alarm, NULL);
+       }
+#endif
+       return;
+}
+
+
 gboolean alarm_manager_alarm_set_rtc_time(void *pObject, int pid,
                                int year, int mon, int day,
                                int hour, int min, int sec, char *e_cookie,
@@ -1823,7 +1926,7 @@ gboolean alarm_manager_alarm_create_appsvc(void *pObject, int pid,
                ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
                return false;
        }
-       
+
        call_gid = security_server_get_gid("alarm");
 
        ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
@@ -2011,7 +2114,7 @@ gboolean alarm_manager_alarm_get_number_of_ids(void *pObject, int pid,
                                               int *return_code)
 {
        GSList *gs_iter = NULL;
-       GQuark quark_app_unique_name;   /* the fullpath of pid(pid) is 
+       GQuark quark_app_unique_name;   /* the fullpath of pid(pid) is
                                           converted  to quark value. */
        char proc_file[256] = { 0 };
        char process_name[512] = { 0 };
@@ -2033,7 +2136,17 @@ gboolean alarm_manager_alarm_get_number_of_ids(void *pObject, int pid,
        int ret;
        int i = 0;
        fd = open(proc_file, O_RDONLY);
-       if (fd > 0) {
+       if (fd < 0) {           /* failure */
+               quark_app_unique_name = g_quark_from_string("unknown");
+               memcpy(app_name, "unknown", strlen("unknown") + 1);
+
+               ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
+                                         "killed, so we failed to get proc file(%s) \n",
+                                         pid, proc_file);
+               *return_code = -1;      /* -1 means that system
+                                          failed internally. */
+               return true;
+       } else {
                ret = read(fd, process_name, 512);
                close(fd);
                while (process_name[i] != '\0') {
@@ -2053,17 +2166,6 @@ gboolean alarm_manager_alarm_get_number_of_ids(void *pObject, int pid,
                        word = strtok_r(NULL, "/", &proc_name_ptr);
                }
                quark_app_unique_name = g_quark_from_string(app_name);
-       } else {                /* failure */
-
-               quark_app_unique_name = g_quark_from_string("unknown");
-               memcpy(app_name, "unknown", strlen("unknown") + 1);
-
-               ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
-                                         "killed, so we failed to get proc file(%s) \n",
-                                         pid, proc_file);
-               *return_code = -1;      /* -1 means that system 
-                                          failed internally. */
-               return true;
        }
 
        ALARM_MGR_LOG_PRINT("called for  app(pid:%d, name=%s)\n",
@@ -2133,7 +2235,16 @@ gboolean alarm_manager_alarm_get_list_of_ids(void *pObject, int pid,
        snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
 
        fd = open(proc_file, O_RDONLY);
-       if (fd > 0) {
+       if (fd < 0) {           /* failure */
+               quark_app_unique_name = g_quark_from_string("unknown");
+               memcpy(app_name, "unknown", strlen("unknown") + 1);
+
+               ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
+               "killed, so we failed to get proc file(%s)\n", pid, proc_file);
+               *return_code = -1;
+               /* -1 means that system failed internally. */
+               return true;
+       } else {
                ret = read(fd, process_name, 512);
                close(fd);
                while (process_name[i] != '\0') {
@@ -2153,16 +2264,6 @@ gboolean alarm_manager_alarm_get_list_of_ids(void *pObject, int pid,
                        word = strtok_r(NULL, "/", &proc_name_ptr);
                }
                quark_app_unique_name = g_quark_from_string(app_name);
-       } else {                /* failure */
-
-               quark_app_unique_name = g_quark_from_string("unknown");
-               memcpy(app_name, "unknown", strlen("unknown") + 1);
-
-               ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
-               "killed, so we failed to get proc file(%s)\n", pid, proc_file);
-               *return_code = -1;
-               /* -1 means that system failed internally. */
-               return true;
        }
 
        ALARM_MGR_LOG_PRINT("called for  app(pid:%d, name=%s)\n",
@@ -2381,7 +2482,7 @@ static void __initialize_alarm_list()
        /*alarm boot */
        if (enable_power_on_alarm) {
                /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
-                * platform with app-server. because __alarm_power_on(..) fuction don't 
+                * platform with app-server. because __alarm_power_on(..) fuction don't
                 * use first parameter internally, we set this value to 0(zero)
                 */
                __alarm_power_on(0, enable_power_on_alarm, NULL);
@@ -2407,7 +2508,7 @@ static void __hibernation_leave_callback()
 
 static bool __initialize_noti()
 {
-
+       int ret = -1;
        int fd = heynoti_init();
        if (fd < 0) {
                ALARM_MGR_EXCEPTION_PRINT("fail to heynoti_init\n");
@@ -2415,7 +2516,10 @@ static bool __initialize_noti()
        }
        heynoti_subscribe(fd, "HIBERNATION_LEAVE", __hibernation_leave_callback,
                          NULL);
-       heynoti_attach_handler(fd);
+       ret = heynoti_attach_handler(fd);
+       if(ret<0) {
+               ALARM_MGR_EXCEPTION_PRINT("heynoti_attach_handler fail");
+       }
 
        if (vconf_notify_key_changed
            (VCONFKEY_SYSTEM_TIMECHANGE, __on_system_time_changed, NULL) < 0) {
@@ -2424,6 +2528,18 @@ static bool __initialize_noti()
        }
        /*system state change noti Ã³¸® */
 
+       if (vconf_notify_key_changed
+           (VCONFKEY_SETAPPL_TIMEZONE_ID, __on_time_zone_changed, NULL) < 0) {
+               ALARM_MGR_LOG_PRINT(
+                       "Failed to add callback for time zone changing event\n");
+       }
+
+       if (vconf_notify_key_changed
+           (VCONFKEY_SYSTEM_TIMECHANGE_EXTERNAL, __on_system_time_external_changed, NULL) < 0) {
+               ALARM_MGR_LOG_PRINT(
+                       "Failed to add callback for time external changing event\n");
+       }
+
        return true;
 }
 
@@ -2598,7 +2714,7 @@ static bool __initialize_db()
            sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL,\
                         &error_message)) {
                ALARM_MGR_EXCEPTION_PRINT("Don't execute query = %s, "
-               "error message = %s\n", QUERY_CREATE_TABLE_ALARMMGR, 
+               "error message = %s\n", QUERY_CREATE_TABLE_ALARMMGR,
                                          error_message);
                return false;
        }
@@ -2653,7 +2769,7 @@ static void __initialize()
        close(fd2);
 
        __initialize_timer();
-       if (__initialize_dbus() == false) {     /* because dbus's initialize 
+       if (__initialize_dbus() == false) {     /* because dbus's initialize
                                        failed, we cannot continue any more. */
                ALARM_MGR_EXCEPTION_PRINT("because __initialize_dbus failed, "
                                          "alarm-server cannot be runned.\n");