Initialize Tizen 2.3
[framework/appfw/alarm-manager.git] / alarm-manager.c
1 /*
2  *  alarm-manager
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Venkatesha Sarpangala <sarpangala.v@samsung.com>, Jayoun Lee <airjany@samsung.com>,
7  * Sewook Park <sewook7.park@samsung.com>, Jaeho Lee <jaeho81.lee@samsung.com>
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  */
22
23
24
25 #define _BSD_SOURCE /*for localtime_r*/
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <time.h>
29 #include <signal.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/stat.h>
33 #include <unistd.h>
34
35 #include <dbus/dbus.h>
36 #include <dbus/dbus-glib-lowlevel.h>
37 #include <glib.h>
38 #if !GLIB_CHECK_VERSION (2, 31, 0)
39 #include <glib/gmacros.h>
40 #else
41 #endif
42
43 /*#include "global-gconf.h"*/
44 #include "alarm.h"
45 #include "alarm-internal.h"
46
47 /*#include"limo-event-delivery.h"*/
48
49 #include <aul.h>
50 #include <bundle.h>
51 #include <security-server.h>
52 #include <db-util.h>
53 #include <vconf.h>
54 #include <vconf-keys.h>
55 #include <dlfcn.h>
56 #include <pkgmgr-info.h>
57
58 #define SIG_TIMER 0x32
59 #define WAKEUP_ALARM_APP_ID       "com.samsung.alarm.ALARM"
60         /* alarm ui application's alarm's dbus_service name instead of 21
61            (alarm application's app_id) value */
62 #define WAKEUP_ALARMBOOTING_APP_ID      "com.samsung.alarmboot.ui.ALARM"
63 /*alrmbooting ui application's dbus_service name instead of 121(alarmbooting
64         application's app_id) value */
65
66 #ifdef WEARABLE_PROFILE
67 #include "dd-display.h"
68 #endif
69
70 __alarm_server_context_t alarm_context;
71 bool g_dummy_timer_is_set = FALSE;
72
73 GSList *g_scheduled_alarm_list = NULL;
74
75 GSList *g_expired_alarm_list = NULL;
76
77 #ifndef RTC_WKALM_BOOT_SET
78 #define RTC_WKALM_BOOT_SET _IOW('p', 0x80, struct rtc_wkalrm)
79 #endif
80
81 #ifdef __ALARM_BOOT
82 bool enable_power_on_alarm;
83 bool alarm_boot;
84 time_t ab_due_time = -1;
85 bool poweron_alarm_expired = false;
86 #endif
87
88 /*      2008. 6. 3 sewook7.park
89        When the alarm becoms sleeping mode, alarm timer is not expired.
90        So using RTC, phone is awaken before alarm rings.
91 */
92 #define __WAKEUP_USING_RTC__
93 #ifdef __WAKEUP_USING_RTC__
94 #include<errno.h>
95 #include <linux/rtc.h>
96 #include <sys/ioctl.h>
97 #include <fcntl.h>
98
99 #define ALARM_RTC_WAKEUP        0
100
101 #define ALARM_IOW(c, type, size)            _IOW('a', (c) | ((type) << 4), size)
102 #define ALARM_SET(type)             ALARM_IOW(2, type, struct timespec)
103 #define ALARM_SET_RTC               _IOW('a', 5, struct timespec)
104 #define ALARM_CLEAR(type)           _IO('a', 0 | ((type) << 4))
105
106
107
108 static const char default_rtc[] = "/dev/alarm";
109
110 static int gfd = 0;
111
112 #endif                          /*__WAKEUP_USING_RTC__*/
113
114 static bool __alarm_add_to_list(__alarm_info_t *__alarm_info);
115 static void __alarm_generate_alarm_id(__alarm_info_t *__alarm_info, alarm_id_t *alarm_id);
116 static bool __alarm_update_in_list(int pid, alarm_id_t alarm_id,
117                                    __alarm_info_t *__alarm_info,
118                                    int *error_code);
119 static bool __alarm_remove_from_list(int pid, alarm_id_t alarm_id,
120                                      int *error_code);
121 static bool __alarm_set_start_and_end_time(alarm_info_t *alarm_info,
122                                            __alarm_info_t *__alarm_info);
123 static bool __alarm_update_due_time_of_all_items_in_list(double diff_time);
124 static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
125                            int pid, char *app_service_name, char *app_service_name_mod,
126                            const char *dst_service_name, const char *dst_service_name_mod, int *error_code);
127 static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
128                            int pid, char *bundle_data, int *error_code);
129
130 static bool __alarm_delete(int pid, alarm_id_t alarm_id, int *error_code);
131 static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
132                            alarm_info_t *alarm_info, int *error_code);
133 static bool __alarm_power_on(int app_id, bool on_off, int *error_code);
134 static bool __alarm_power_off(int app_id, int *error_code);
135 static bool __alarm_check_next_duetime(int app_id, int *error_code);
136 static void __alarm_send_noti_to_application(const char *app_service_name,
137                                              alarm_id_t alarm_id);
138 static void __alarm_expired();
139 static gboolean __alarm_handler_idle();
140 static void __alarm_handler(int sigNum, siginfo_t *pSigInfo, void *pUContext);
141 static void __clean_registry();
142 static bool __alarm_manager_reset();
143 static void __on_system_time_external_changed(keynode_t *node, void *data);
144 static void __initialize_timer();
145 static void __initialize_alarm_list();
146 static void __initialize_scheduled_alarm_list();
147 static void __hibernation_leave_callback();
148 static bool __initialize_noti();
149
150 static bool __initialize_dbus();
151 static bool __initialize_db();
152 static void __initialize();
153 static bool __check_false_alarm();
154 static DBusHandlerResult __alarm_server_filter(DBusConnection *connection,
155                                                DBusMessage *message,
156                                                void *user_data);
157
158 static int __rtc_set_b2()
159 {
160 #ifdef __WAKEUP_USING_RTC__
161         const char *rtc = default_rtc;
162         struct rtc_wkalrm rtc_wk;
163         struct tm due_tm;
164         struct timespec alarm_time;
165 #ifdef _SIMUL                   /*if build is simulator, we don't need to set
166                                    RTC because RTC does not work in simulator.*/
167         ALARM_MGR_EXCEPTION_PRINT("because it is simulator's mode, "
168                                   "we don't set RTC.\n");
169         return;
170 #endif
171
172         if (gfd == 0) {
173                 gfd = open(rtc, O_RDWR);
174                 if (gfd == -1) {
175                         ALARM_MGR_EXCEPTION_PRINT("RTC open failed.\n");
176                 }
177         }
178
179         /* Read the RTC time/date */
180         int retval;
181
182         ALARM_MGR_LOG_PRINT("alarm_context.c_due_time is %d\n", \
183                             alarm_context.c_due_time);
184
185         if (alarm_context.c_due_time != -1) {
186
187                 /*retval = ioctl(gfd, ALARM_CLEAR(ALARM_RTC_WAKEUP));
188                 if (retval == -1) {
189                         if (errno == ENOTTY) {
190                                 ALARM_MGR_EXCEPTION_PRINT("\nAlarm IRQs not"
191                                                           "supported.\n");
192                         }
193                         ALARM_MGR_EXCEPTION_PRINT("ALARM_CLEAR ioctl");
194                         //close(fd);
195                         return -1;
196                 }*/
197
198                 time_t due_time = alarm_context.c_due_time;
199                 gmtime_r(&due_time, &due_tm);
200
201                 ALARM_MGR_LOG_PRINT("Setted RTC Alarm date/time is "
202                         "%d-%d-%d, %02d:%02d:%02d.\n", due_tm.tm_mday,
203                         due_tm.tm_mon + 1, due_tm.tm_year + 1900,
204                         due_tm.tm_hour, due_tm.tm_min, due_tm.tm_sec);
205
206                 alarm_time.tv_sec = due_time;
207                 alarm_time.tv_nsec = 0;
208                 retval = ioctl(gfd, ALARM_SET(ALARM_RTC_WAKEUP), &alarm_time);
209                 if (retval == -1) {
210                         if (errno == ENOTTY) {
211                                 ALARM_MGR_EXCEPTION_PRINT("\nAlarm IRQs not"
212                                                           "supported.\n");
213                         }
214                         ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
215                         //close(fd);
216                         return -1;
217                 }
218                 ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is setted");
219
220                 ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is on");
221         } else
222                 ALARM_MGR_LOG_PRINT("[alarm-server]alarm_context.c_due_time is"
223                                     "less than 10 sec. RTC alarm does not need to be set\n");
224
225         //close(fd);
226 #endif                          /* __WAKEUP_USING_RTC__ */
227         return 0;
228 }
229
230
231 static void __rtc_set()
232 {
233 #ifdef __WAKEUP_USING_RTC__
234
235         const char *rtc = "/dev/rtc0";
236         int fd = 0;
237         struct rtc_time rtc_tm;
238         struct rtc_wkalrm rtc_wk;
239         struct tm due_tm;
240
241 #ifdef _SIMUL                   /*if build is simulator, we don't need to set
242                                    RTC because RTC does not work in simulator.*/
243         ALARM_MGR_EXCEPTION_PRINT("because it is simulator's mode, "
244                                   "we don't set RTC.\n");
245         return;
246 #endif
247
248         if(__rtc_set_b2() == 0)
249                 return;
250
251         fd = open(rtc, O_RDONLY);
252         if (fd == -1) {
253                 ALARM_MGR_EXCEPTION_PRINT("RTC open failed.\n");
254                 return;
255         }
256
257         /* Read the RTC time/date */
258         int retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
259         if (retval == -1) {
260                 ALARM_MGR_EXCEPTION_PRINT("RTC_RD_TIME ioctl failed");
261                 close(fd);
262                 return;
263         }
264
265         ALARM_MGR_LOG_PRINT("\n\nCurrent RTC date/time is %d-%d-%d, "
266                 "%02d:%02d:%02d.\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1,
267                 rtc_tm.tm_year + 1900, rtc_tm.tm_hour, rtc_tm.tm_min,
268                 rtc_tm.tm_sec);
269
270         ALARM_MGR_LOG_PRINT("alarm_context.c_due_time is %d\n", \
271                             alarm_context.c_due_time);
272
273         if (alarm_context.c_due_time != -1) {
274                 time_t due_time = alarm_context.c_due_time;
275                 gmtime_r(&due_time, &due_tm);
276
277                 rtc_tm.tm_mday = due_tm.tm_mday;
278                 rtc_tm.tm_mon = due_tm.tm_mon;
279                 rtc_tm.tm_year = due_tm.tm_year;
280                 rtc_tm.tm_hour = due_tm.tm_hour;
281                 rtc_tm.tm_min = due_tm.tm_min;
282                 rtc_tm.tm_sec = due_tm.tm_sec;
283                 memcpy(&rtc_wk.time, &rtc_tm, sizeof(rtc_tm));
284                 rtc_wk.enabled = 1;
285                 rtc_wk.pending = 0;
286
287                 ALARM_MGR_LOG_PRINT("\n\nSetted RTC Alarm date/time is "
288                         "%d-%d-%d, %02d:%02d:%02d.\n", rtc_tm.tm_mday,
289                         rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
290                         rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
291
292                 retval = ioctl(fd, RTC_WKALM_SET, &rtc_wk);
293                 if (retval == -1) {
294                         if (errno == ENOTTY) {
295                                 ALARM_MGR_EXCEPTION_PRINT("\nAlarm IRQs not"
296                                                           "supported.\n");
297                         }
298                         ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
299                         close(fd);
300                         return;
301                 }
302                 ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is setted");
303
304                 /* Enable alarm interrupts */
305                 retval = ioctl(fd, RTC_AIE_ON, 0);
306                 if (retval == -1) {
307                         ALARM_MGR_EXCEPTION_PRINT("RTC_AIE_ON ioctl failed");
308                         close(fd);
309                         return;
310                 }
311                 ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is on");
312         } else
313                 ALARM_MGR_LOG_PRINT("[alarm-server]alarm_context.c_due_time is"
314                                     "less than 10 sec. RTC alarm does not need to be set\n");
315
316         close(fd);
317
318 #endif                          /* __WAKEUP_USING_RTC__ */
319 }
320
321
322 int _set_rtc_time(time_t _time)
323 {
324         int retval0 = 0;
325         const char *rtc0 = default_rtc;
326         struct timespec rtc_time;
327
328         if (gfd == 0) {
329                 gfd = open(rtc0, O_RDWR);
330
331                 if (gfd == -1) {
332                         ALARM_MGR_LOG_PRINT("error to open /dev/alarm.");
333                         perror("\t");
334                 }
335         }
336
337         rtc_time.tv_sec = _time;
338         rtc_time.tv_nsec = 0;
339
340         retval0 = ioctl(gfd, ALARM_SET_RTC, &rtc_time);
341
342         if (retval0 == -1) {
343                 /*if (gfd != -1)
344                         close(fd0);*/
345                 ALARM_MGR_LOG_PRINT("error to ioctl fd0.");
346                 ALARM_MGR_LOG_PRINT("retval0 : %d", retval0);
347                 
348                 perror("\t");
349         }
350         /*if (gfd != -1)
351                 close(fd0);*/
352
353         return 1;
354 }
355
356
357 bool __alarm_clean_list()
358 {
359         GSList *iter = NULL;
360
361         for (iter = alarm_context.alarms; iter != NULL;
362              iter = g_slist_next(iter)) {
363                 free(iter->data);
364         }
365
366         g_slist_free(alarm_context.alarms);
367         return true;
368 }
369
370 static void __alarm_generate_alarm_id(__alarm_info_t *__alarm_info, alarm_id_t *alarm_id)
371 {
372         bool unique_id = false;
373         __alarm_info_t *entry = NULL;
374
375         GSList *iter = NULL;
376
377         /* FIXME: alarm id must be unique. */
378         __alarm_info->alarm_id = (int)(void *)__alarm_info;
379         ALARM_MGR_LOG_PRINT("__alarm_info->alarm_id is %d", \
380                             __alarm_info->alarm_id);
381
382         while (unique_id == false) {
383                 unique_id = true;
384
385                 for (iter = alarm_context.alarms; iter != NULL;
386                      iter = g_slist_next(iter)) {
387                         entry = iter->data;
388                         if (entry->alarm_id == __alarm_info->alarm_id) {
389                                 __alarm_info->alarm_id++;
390                                 unique_id = false;
391                         }
392                 }
393         }
394
395         *alarm_id = __alarm_info->alarm_id;
396
397         return;
398 }
399
400 static bool __alarm_add_to_list(__alarm_info_t *__alarm_info)
401 {
402
403         alarm_info_t *alarm_info = &__alarm_info->alarm_info;
404         __alarm_info_t *entry = NULL;
405
406         GSList *iter = NULL;
407
408         /* list alarms */
409         ALARM_MGR_LOG_PRINT("[alarm-server]: before add\n");
410         for (iter = alarm_context.alarms; iter != NULL;
411              iter = g_slist_next(iter)) {
412                 entry = iter->data;
413                 /*ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n",
414                    entry->alarm_id); */
415         }
416
417         alarm_context.alarms =
418             g_slist_append(alarm_context.alarms, __alarm_info);
419         /*list alarms */
420         ALARM_MGR_LOG_PRINT("[alarm-server]: after add\n");
421         for (iter = alarm_context.alarms; iter != NULL;
422              iter = g_slist_next(iter)) {
423                 entry = iter->data;
424                 ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n",
425                                     entry->alarm_id);
426         }
427
428         if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
429                 _save_alarms(__alarm_info);
430         }
431
432         return true;
433 }
434
435 static bool __alarm_update_in_list(int pid, alarm_id_t alarm_id,
436                                    __alarm_info_t *__alarm_info,
437                                    int *error_code)
438 {
439         bool found = false;
440         alarm_info_t *alarm_info = &__alarm_info->alarm_info;
441         GSList *iter = NULL;
442         __alarm_info_t *entry = NULL;
443
444         for (iter = alarm_context.alarms; iter != NULL;
445              iter = g_slist_next(iter)) {
446                 entry = iter->data;
447                 if (entry->alarm_id == alarm_id) {
448
449                         found = true;
450                         __alarm_info->quark_app_unique_name =
451                             entry->quark_app_unique_name;
452                         __alarm_info->quark_dst_service_name =
453                             entry->quark_dst_service_name;
454                         memcpy(entry, __alarm_info, sizeof(__alarm_info_t));
455
456                         break;
457                 }
458         }
459
460         if (!found) {
461                 if (error_code)
462                         *error_code = ERR_ALARM_INVALID_ID;
463                 return false;
464         }
465
466         if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
467                 _update_alarms(__alarm_info);
468         }
469
470         return true;
471 }
472
473 static bool __alarm_remove_from_list(int pid, alarm_id_t alarm_id,
474                                      int *error_code)
475 {
476         bool found = false;
477
478         alarm_info_t *alarm_info = NULL;
479
480         GSList *iter = NULL;
481         __alarm_info_t *entry = NULL;
482
483         /*list alarms */
484         ALARM_MGR_LOG_PRINT("[alarm-server]: before del : alarm id(%d)\n",
485                             alarm_id);
486
487         for (iter = alarm_context.alarms; iter != NULL;
488              iter = g_slist_next(iter)) {
489                 entry = iter->data;
490                 if (entry->alarm_id == alarm_id) {
491                         alarm_info = &entry->alarm_info;
492
493                         ALARM_MGR_LOG_PRINT("[alarm-server]: "
494                                             "__alarm_remove_from_list : alarm id(%d)\n",
495                                             entry->alarm_id);
496
497                         if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
498                                 _delete_alarms(alarm_id);
499                         }
500
501                         alarm_context.alarms =
502                             g_slist_remove(alarm_context.alarms, iter->data);
503                         found = true;
504                         break;
505                 }
506
507         }
508
509         ALARM_MGR_LOG_PRINT("[alarm-server]: after del\n");
510
511         if (!found) {
512                 if (error_code)
513                         *error_code = ERR_ALARM_INVALID_ID;
514                 return false;
515         }
516
517         return true;
518 }
519
520 static bool __alarm_set_start_and_end_time(alarm_info_t *alarm_info,
521                                            __alarm_info_t *__alarm_info)
522 {
523         alarm_date_t *start = &alarm_info->start;
524         alarm_date_t *end = &alarm_info->end;
525
526         struct tm alarm_tm = { 0, };
527
528         if (start->year != 0) {
529                 alarm_tm.tm_year = start->year - 1900;
530                 alarm_tm.tm_mon = start->month - 1;
531                 alarm_tm.tm_mday = start->day;
532
533                 alarm_tm.tm_hour = start->hour;
534                 alarm_tm.tm_min = start->min;
535                 alarm_tm.tm_sec = start->sec;
536                 alarm_tm.tm_isdst = -1;
537
538                 __alarm_info->start = mktime(&alarm_tm);
539         } else {
540                 __alarm_info->start = 0;
541         }
542
543         if (end->year != 0) {
544                 alarm_tm.tm_year = end->year - 1900;
545                 alarm_tm.tm_mon = end->month - 1;
546                 alarm_tm.tm_mday = end->day;
547
548                 alarm_tm.tm_hour = end->hour;
549                 alarm_tm.tm_min = end->min;
550                 alarm_tm.tm_sec = end->sec;
551
552                 __alarm_info->end = mktime(&alarm_tm);
553         } else {
554                 __alarm_info->end = 0;
555         }
556
557         return true;
558 }
559
560 /*
561 static bool alarm_get_tz_info(int *gmt_idx, int *dst)
562 {
563         GConfValue *value1 = NULL;
564         GConfValue *value2 = NULL;
565         GConfClient* gConfClient = NULL;
566         GError* err = NULL;
567
568         gConfClient = gconf_client_get_default();
569
570         if(gConfClient) {
571                 value1 = gconf_client_get(gConfClient, SETTINGS_CLOCKTIMEZONE,
572                                                                         &err);
573                 if (err) {
574                         ALARM_MGR_LOG_PRINT("__on_system_time_changed:
575                         gconf_client_get() failed:
576                         error:[%s]\n", err->message);
577                         g_error_free(err);
578                         err = NULL;
579                 }
580                 *gmt_idx = gconf_value_get_int(value1);
581                 ALARM_MGR_LOG_PRINT("gconf return gmt_idx =%d\n ", *gmt_idx);
582
583                 value2 = gconf_client_get(gConfClient,
584                         SETTINGS_DAYLIGHTSTATUS, &err);
585                 if (err) {
586                         ALARM_MGR_LOG_PRINT("__on_system_time_changed:
587                 gconf_client_get() failed: error:[%s]\n", err->message);
588                 g_error_free(err);
589                 err = NULL;
590         }
591
592         *dst = gconf_value_get_int(value2);
593         ALARM_MGR_LOG_PRINT("gconf return dst =%d\n ", *dst);
594
595         if(gConfClient != NULL) {
596                 g_object_unref(gConfClient);
597                 gConfClient = NULL;
598                 }
599         }
600         else
601                 ALARM_MGR_LOG_PRINT("check the gconf setting failed!!!!! \n ");
602
603         if(value1) {
604                 gconf_value_free(value1);
605                 value1 = NULL;
606         }
607         if(value2) {
608                 gconf_value_free(value2);
609                 value2 = NULL;
610         }
611
612         return true;
613 }
614 */
615
616 static bool __alarm_update_due_time_of_all_items_in_list(double diff_time)
617 {
618         time_t current_time;
619         time_t min_time = -1;
620         time_t due_time = 0;
621         GSList *iter = NULL;
622         __alarm_info_t *entry = NULL;
623         struct tm *p_time = NULL ;
624         struct tm due_time_result ;
625         struct tm fixed_time = { 0, };
626
627         for (iter = alarm_context.alarms; iter != NULL;
628              iter = g_slist_next(iter)) {
629                 entry = iter->data;
630                 alarm_info_t *alarm_info = &(entry->alarm_info);
631                 if (alarm_info->alarm_type & ALARM_TYPE_RELATIVE) {
632                         /*diff_time Ã³\B8\AE */
633
634                         entry->due_time += diff_time;
635
636                         alarm_date_t *start = &alarm_info->start; /**< start
637                                                         time of the alarm */
638                         alarm_date_t *end = &alarm_info->end;;
639                                                 /**< end time of the alarm */
640
641                         tzset();
642                         p_time = localtime_r(&entry->due_time, &due_time_result);
643                         if (p_time != NULL) {
644                                 start->year = p_time->tm_year + 1900;
645                                 start->month = p_time->tm_mon + 1;
646                                 start->day = p_time->tm_mday;
647                                 start->hour = p_time->tm_hour;
648                                 start->min = p_time->tm_min;
649                                 start->sec = p_time->tm_sec;
650
651                                 end->year = p_time->tm_year + 1900;
652                                 end->month = p_time->tm_mon + 1;
653                                 end->day = p_time->tm_mday;
654
655
656                                 memset(&fixed_time, 0, sizeof(fixed_time));
657                                 fixed_time.tm_year = p_time->tm_year;
658                                 fixed_time.tm_mon = p_time->tm_mon;
659                                 fixed_time.tm_mday = p_time->tm_mday;
660                                 fixed_time.tm_hour = 0;
661                                 fixed_time.tm_min = 0;
662                                 fixed_time.tm_sec = 0;
663                         }
664                         entry->start = mktime(&fixed_time);
665
666                         fixed_time.tm_hour = 23;
667                         fixed_time.tm_min = 59;
668                         fixed_time.tm_sec = 59;
669
670                         entry->end = mktime(&fixed_time);
671
672                         ALARM_MGR_LOG_PRINT("alarm_info->alarm_type is "
673                                             "ALARM_TYPE_RELATIVE\n");
674
675                         _update_alarms(entry);
676                 }
677
678                 _alarm_next_duetime(entry);
679                 ALARM_MGR_LOG_PRINT("entry->due_time is %d\n", entry->due_time);
680         }
681
682         time(&current_time);
683
684         for (iter = alarm_context.alarms; iter != NULL;
685              iter = g_slist_next(iter)) {
686                 entry = iter->data;
687                 due_time = entry->due_time;
688
689                 double interval = 0;
690
691                 ALARM_MGR_LOG_PRINT("alarm[%d] with duetime(%u) at "
692                 "current(%u)\n", entry->alarm_id, due_time, current_time);
693                 if (due_time == 0) {    /* 0 means this alarm has been
694                                            disabled */
695                         continue;
696                 }
697
698                 interval = difftime(due_time, current_time);
699
700                 if (interval <= 0) {
701                         ALARM_MGR_LOG_PRINT("this may be error.. alarm[%d]\n", \
702                                             entry->alarm_id);
703                         continue;
704                 }
705
706                 interval = difftime(due_time, min_time);
707
708                 if ((interval < 0) || min_time == -1) {
709                         min_time = due_time;
710                 }
711
712         }
713
714         alarm_context.c_due_time = min_time;
715
716         return true;
717 }
718
719 static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
720                            int pid,char *bundle_data, int *error_code){
721
722         time_t current_time;
723         time_t due_time;
724         struct tm ts_ret;
725         char due_time_r[100] = { 0 };
726         char proc_file[512] = { 0 };
727         char process_name[512] = { 0 };
728         char app_name[512] = { 0 };
729         char *word = NULL;
730         char *proc_name_ptr = NULL;
731         int fd = 0;
732         int ret = 0;
733         int i = 0;
734         bundle *b;
735         char caller_appid[512];
736         bundle_raw *b_data = NULL;
737         int datalen = 0;
738
739         __alarm_info_t *__alarm_info = NULL;
740
741         __alarm_info = malloc(sizeof(__alarm_info_t));
742         if (__alarm_info == NULL) {
743                 SECURE_LOGE("Caution!! app_pid=%d, malloc "
744                                           "failed. it seems to be OOM\n", pid);
745                 *error_code = -1;       /* -1 means that system
746                                            failed internally. */
747                 return false;
748         }
749         __alarm_info->pid = pid;
750         __alarm_info->alarm_id = -1;
751
752
753         /* we should consider to check whether  pid is running or Not
754          */
755         memset(process_name, '\0', 512);
756         memset(proc_file, '\0', 512);
757         snprintf(proc_file, 512, "/proc/%d/cmdline", pid);
758
759         fd = open(proc_file, O_RDONLY);
760         if (fd < 0) {           /* failure */
761                 SECURE_LOGE("Caution!! app_pid(%d) seems to be "
762                                           "killed, so we failed to get proc file(%s) and do not create "
763                                           "alarm_info\n", pid, proc_file);
764                 *error_code = -1;       /*-1 means that system failed
765                                                         internally.*/
766                 free(__alarm_info);
767                 return false;
768         } else {
769                 ret = read(fd, process_name, 512);
770                 close(fd);
771                 if (ret <=0)
772                 {
773                         ALARM_MGR_EXCEPTION_PRINT("Unable to get application name\n");
774                         *error_code = -1;
775                         free(__alarm_info);
776                         return false;
777                 }
778                 while (process_name[i] != '\0') {
779                         if (process_name[i] == ' ') {
780                                 process_name[i] = '\0';
781                                 break;
782                         }
783                         i++;
784                 }
785
786                 word = strtok_r(process_name, "/", &proc_name_ptr);
787                 while (word != NULL) {
788                         memset(app_name, 0, 512);
789                         snprintf(app_name, 512, "%s", word);
790                         word = strtok_r(NULL, "/", &proc_name_ptr);
791                 }
792                 __alarm_info->quark_app_unique_name =
793                     g_quark_from_string(app_name);
794         }
795
796         b = bundle_decode((bundle_raw *)bundle_data, strlen(bundle_data));
797         ret = aul_app_get_appid_bypid(pid, caller_appid, 512);
798         if(ret == 0) {
799                 bundle_add(b, "__ALARM_MGR_CALLER_APPID", caller_appid);
800         }
801         bundle_encode(b, &b_data, &datalen);
802
803         __alarm_info->quark_bundle=g_quark_from_string(b_data);
804         __alarm_info->quark_app_service_name = g_quark_from_string("null");
805         __alarm_info->quark_dst_service_name = g_quark_from_string("null");
806         __alarm_info->quark_app_service_name_mod = g_quark_from_string("null");
807         __alarm_info->quark_dst_service_name_mod = g_quark_from_string("null");
808
809         bundle_free(b);
810         if (b_data) {
811                 free(b_data);
812                 b_data = NULL;
813         }
814         
815         __alarm_set_start_and_end_time(alarm_info, __alarm_info);
816         memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
817         __alarm_generate_alarm_id(__alarm_info, alarm_id);
818
819 #ifdef __APP_SYNC
820         if (_sync_scheduler_app_sync_on())
821                 _sync_scheduler_repeating_alarms(__alarm_info);
822 #endif
823
824         time(&current_time);
825
826         if (alarm_context.c_due_time < current_time) {
827                 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
828                 "(%d) is less than current time(%d)", alarm_context.c_due_time,
829                                           current_time);
830                 alarm_context.c_due_time = -1;
831         }
832
833         due_time = _alarm_next_duetime(__alarm_info);
834         if (__alarm_add_to_list(__alarm_info) == false) {
835                 free(__alarm_info);
836                 *error_code = -1;
837                 return false;
838         }
839
840         if (due_time == 0) {
841                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create a new alarm: "
842                 "due_time is 0, alarm(%d) \n", *alarm_id);
843                 return true;
844         } else if (current_time == due_time) {
845                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create alarm: "
846                      "current_time(%d) is same as due_time(%d)", current_time,
847                      due_time);
848                 return true;
849         }else if (difftime(due_time, current_time) <  0){
850                 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);
851                 return true;
852         }else {
853                 localtime_r(&due_time, &ts_ret);
854                 strftime(due_time_r, 30, "%c", &ts_ret);
855                 SECURE_LOGD("[alarm-server]:Create a new alarm: "
856                                     "alarm(%d) due_time(%s)", *alarm_id,
857                                     due_time_r);
858         }
859
860         ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
861                             "due_time(%d)", alarm_context.c_due_time, due_time);
862
863         if (alarm_context.c_due_time == -1
864             || due_time < alarm_context.c_due_time) {
865                 _clear_scheduled_alarm_list();
866                 _add_to_scheduled_alarm_list(__alarm_info);
867                 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
868                                  *alarm_id);
869                 alarm_context.c_due_time = due_time;
870
871         } else if (due_time == alarm_context.c_due_time) {
872                 _add_to_scheduled_alarm_list(__alarm_info);
873
874         }
875
876         __rtc_set();
877
878 #ifdef __ALARM_BOOT
879         /*alarm boot */
880         if (enable_power_on_alarm) {
881                 __alarm_power_on(0, enable_power_on_alarm, NULL);
882         }
883 #endif
884
885         return true;
886
887 }
888
889 static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
890                            int pid, char *app_service_name, char *app_service_name_mod,
891                            const char *dst_service_name,const char *dst_service_name_mod,  int *error_code)
892 {
893
894         time_t current_time;
895         time_t due_time;
896         char proc_file[256] = { 0 };
897         char process_name[512] = { 0 };
898         char app_name[256] = { 0 };
899         char *word = NULL;
900         char *proc_name_ptr = NULL;
901
902         __alarm_info_t *__alarm_info = NULL;
903
904         __alarm_info = malloc(sizeof(__alarm_info_t));
905         if (__alarm_info == NULL) {
906                 SECURE_LOGE("Caution!! app_pid=%d, malloc "
907                                           "failed. it seems to be OOM\n", pid);
908                 *error_code = -1;       /* -1 means that system
909                                            failed internally. */
910                 return false;
911         }
912         __alarm_info->pid = pid;
913         __alarm_info->alarm_id = -1;
914
915         /* we should consider to check whether  pid is running or Not
916          */
917         memset(process_name, '\0', 512);
918         memset(proc_file, '\0', 256);
919         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
920
921         int fd;
922         int ret;
923         int i = 0;
924         fd = open(proc_file, O_RDONLY);
925         if (fd < 0) {   /* failure */
926                 __alarm_info->quark_app_unique_name =
927                     g_quark_from_string("unknown");
928                 SECURE_LOGE("Caution!! app_pid(%d) seems to be "
929                                           "killed, so we failed to get proc file(%s) and do not create "
930                                           "alarm_info\n", pid, proc_file);
931                 *error_code = -1;       /*-1 means that system failed
932                                                         internally.*/
933                 free(__alarm_info);
934                 return false;
935         } else {
936                 ret = read(fd, process_name, 512);
937                 if (ret < 0 ) {
938                         *error_code = -1;       /*-1 means that system failed
939                                                         internally.*/
940                         free(__alarm_info);
941                         close(fd);
942                         return false;
943                 }
944                 close(fd);
945                 while (process_name[i] != '\0') {
946                         if (process_name[i] == ' ') {
947                                 process_name[i] = '\0';
948                                 break;
949                         }
950                         i++;
951                 }
952                 /* if (readlink(proc_file, process_name, 256)!=-1) */
953                 /*success */
954
955                 word = strtok_r(process_name, "/", &proc_name_ptr);
956                 while (word != NULL) {
957                         memset(app_name, 0, 256);
958                         snprintf(app_name, 256, "%s", word);
959                         word = strtok_r(NULL, "/", &proc_name_ptr);
960                 }
961                 __alarm_info->quark_app_unique_name =
962                     g_quark_from_string(app_name);
963         }
964
965         __alarm_info->quark_app_service_name =
966             g_quark_from_string(app_service_name);
967         __alarm_info->quark_app_service_name_mod =
968         g_quark_from_string(app_service_name_mod);
969         __alarm_info->quark_dst_service_name =
970             g_quark_from_string(dst_service_name);
971         __alarm_info->quark_dst_service_name_mod =
972             g_quark_from_string(dst_service_name_mod);
973         __alarm_info->quark_bundle = g_quark_from_string("null");
974
975
976         __alarm_set_start_and_end_time(alarm_info, __alarm_info);
977         memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
978         __alarm_generate_alarm_id(__alarm_info, alarm_id);
979
980 #ifdef __APP_SYNC
981         if (_sync_scheduler_app_sync_on())
982                 _sync_scheduler_repeating_alarms(__alarm_info);
983 #endif
984
985         time(&current_time);
986
987         SECURE_LOGD("[alarm-server]:pid=%d, app_unique_name=%s, "
988                 "app_service_name=%s,dst_service_name=%s, c_due_time=%d", \
989                 pid, g_quark_to_string(__alarm_info->quark_app_unique_name), \
990                 g_quark_to_string(__alarm_info->quark_app_service_name), \
991                 g_quark_to_string(__alarm_info->quark_dst_service_name), \
992                             alarm_context.c_due_time);
993
994         if (alarm_context.c_due_time < current_time) {
995                 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
996                 "(%d) is less than current time(%d)", alarm_context.c_due_time,
997                                           current_time);
998                 alarm_context.c_due_time = -1;
999         }
1000
1001         due_time = _alarm_next_duetime(__alarm_info);
1002         if (__alarm_add_to_list(__alarm_info) == false) {
1003                 free(__alarm_info);
1004                 return false;
1005         }
1006
1007         if (due_time == 0) {
1008                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create a new alarm: "
1009                 "due_time is 0, alarm(%d) \n", *alarm_id);
1010                 return true;
1011         } else if (current_time == due_time) {
1012                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create alarm: "
1013                      "current_time(%d) is same as due_time(%d)", current_time,
1014                      due_time);
1015                 return true;
1016         }else if (difftime(due_time, current_time) <  0){
1017                 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);
1018                 return true;
1019         } else {
1020                 char due_time_r[100] = { 0 };
1021                 struct tm ts_ret;
1022                 localtime_r(&due_time, &ts_ret);
1023                 strftime(due_time_r, 30, "%c", &ts_ret);
1024                 SECURE_LOGD("[alarm-server]:Create a new alarm: "
1025                                     "alarm(%d) due_time(%s)", *alarm_id,
1026                                     due_time_r);
1027         }
1028
1029         ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
1030                             "due_time(%d)", alarm_context.c_due_time, due_time);
1031
1032         if (alarm_context.c_due_time == -1
1033             || due_time < alarm_context.c_due_time) {
1034                 _clear_scheduled_alarm_list();
1035                 _add_to_scheduled_alarm_list(__alarm_info);
1036                 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
1037                                  *alarm_id);
1038                 alarm_context.c_due_time = due_time;
1039
1040         } else if (due_time == alarm_context.c_due_time) {
1041                 _add_to_scheduled_alarm_list(__alarm_info);
1042
1043         }
1044
1045         __rtc_set();
1046
1047 #ifdef __ALARM_BOOT
1048         /*alarm boot */
1049         if (enable_power_on_alarm) {
1050                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
1051                  * platform with app-server.because __alarm_power_on(..) fuction don't
1052                  * use first parameter internally, we set this value to 0(zero)
1053                  */
1054                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1055         }
1056 #endif
1057         return true;
1058 }
1059
1060 static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
1061                            alarm_info_t *alarm_info, int *error_code)
1062 {
1063         time_t current_time;
1064         time_t due_time;
1065
1066         __alarm_info_t *__alarm_info = NULL;
1067         bool result = false;
1068
1069         __alarm_info = malloc(sizeof(__alarm_info_t));
1070         if (__alarm_info == NULL) {
1071                 SECURE_LOGE("Caution!! app_pid=%d, "
1072                         "malloc failed. it seems to be OOM\n", pid);
1073                 *error_code = -1;       /*-1 means that system failed
1074                                                 internally.*/
1075                 return false;
1076         }
1077
1078         __alarm_info->pid = pid;
1079         __alarm_info->alarm_id = alarm_id;
1080
1081         /* we should consider to check whether  pid is running or Not
1082          */
1083
1084         __alarm_info->quark_app_service_name =
1085             g_quark_from_string(app_service_name);
1086         __alarm_set_start_and_end_time(alarm_info, __alarm_info);
1087         memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
1088
1089         time(&current_time);
1090
1091         if (alarm_context.c_due_time < current_time) {
1092                 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
1093                 "(%d) is less than current time(%d)", alarm_context.c_due_time,
1094                                           current_time);
1095                 alarm_context.c_due_time = -1;
1096         }
1097
1098         due_time = _alarm_next_duetime(__alarm_info);
1099         if (!__alarm_update_in_list(pid, alarm_id, __alarm_info, error_code)) {
1100                 free(__alarm_info);
1101                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: requested alarm_id "
1102                 "(%d) does not exist. so this value is invalid id.", alarm_id);
1103                 return false;
1104         }
1105         /* ALARM_MGR_LOG_PRINT("[alarm-server]:request_pid=%d, alarm_id=%d,
1106          * app_unique_name=%s, app_service_name=%s, dst_service_name=%s,
1107          * c_due_time=%d", pid, alarm_id, g_quark_to_string
1108          * (__alarm_info->quark_app_unique_name), g_quark_to_string
1109          * (__alarm_info->quark_app_service_name), g_quark_to_string
1110          * (__alarm_info->quark_dst_service_name), alarm_context.c_due_time);
1111          */
1112
1113         result = _remove_from_scheduled_alarm_list(pid, alarm_id);
1114
1115         if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
1116                 /*there is no scheduled alarm */
1117                 _alarm_disable_timer(alarm_context);
1118                 _alarm_schedule();
1119
1120                 ALARM_MGR_LOG_PRINT("[alarm-server]:Update alarm: alarm(%d)\n",
1121                                     alarm_id);
1122
1123                 __rtc_set();
1124
1125 #ifdef __ALARM_BOOT
1126                 /*alarm boot */
1127                 if (enable_power_on_alarm) {
1128                         /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in
1129                          * a platform with app-server.because __alarm_power_on(..) fuction don't
1130                          * use first parameter internally, we set this value to 0(zero)
1131                          */
1132                         __alarm_power_on(0, enable_power_on_alarm, NULL);
1133                 }
1134 #endif
1135
1136                 if (due_time == 0) {
1137                         ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1138                                         "due_time is 0\n");
1139                 }
1140                 free(__alarm_info);
1141                 return true;
1142         }
1143
1144         if (due_time == 0) {
1145                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1146                                 "due_time is 0, alarm(%d)\n", alarm_id);
1147                 free(__alarm_info);
1148                 return true;
1149         } else if (current_time == due_time) {
1150                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1151                 "current_time(%d) is same as due_time(%d)", current_time,
1152                 due_time);
1153                 free(__alarm_info);
1154                 return true;
1155         }else if (difftime(due_time, current_time)< 0){
1156                 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);
1157                 free(__alarm_info);
1158                 return true;
1159         } else {
1160                 char due_time_r[100] = { 0 };
1161                 struct tm ts_ret;
1162                 localtime_r(&due_time, &ts_ret);
1163                 strftime(due_time_r, 30, "%c", &ts_ret);
1164                 SECURE_LOGD("[alarm-server]:Update alarm: alarm(%d) "
1165                                     "due_time(%s)\n", alarm_id, due_time_r);
1166         }
1167
1168         ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
1169                             "due_time(%d)", alarm_context.c_due_time, due_time);
1170
1171         if (alarm_context.c_due_time == -1
1172             || due_time < alarm_context.c_due_time) {
1173                 _clear_scheduled_alarm_list();
1174                 _add_to_scheduled_alarm_list(__alarm_info);
1175                 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
1176                                  alarm_id);
1177                 alarm_context.c_due_time = due_time;
1178                 ALARM_MGR_LOG_PRINT("[alarm-server1]:alarm_context.c_due_time "
1179                      "(%d), due_time(%d)", alarm_context.c_due_time, due_time);
1180         } else if (due_time == alarm_context.c_due_time) {
1181                 _add_to_scheduled_alarm_list(__alarm_info);
1182                 ALARM_MGR_LOG_PRINT("[alarm-server2]:alarm_context.c_due_time "
1183                      "(%d), due_time(%d)", alarm_context.c_due_time, due_time);
1184         }
1185
1186         __rtc_set();
1187
1188 #ifdef __ALARM_BOOT
1189         /*alarm boot */
1190         if (enable_power_on_alarm) {
1191                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID)
1192                  * in a platform with app-server.because __alarm_power_on(..) fuction
1193                  * don't use first parameter internally, we set this value to 0(zero)
1194                  */
1195                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1196         }
1197 #endif
1198         free(__alarm_info);
1199
1200         return true;
1201 }
1202
1203 static bool __alarm_delete(int pid, alarm_id_t alarm_id, int *error_code)
1204 {
1205         bool result = false;
1206
1207         SECURE_LOGD("[alarm-server]:delete alarm: alarm(%d) pid(%d)\n", alarm_id, pid);
1208         result = _remove_from_scheduled_alarm_list(pid, alarm_id);
1209
1210 #ifdef __APP_SYNC
1211         if (_sync_scheduler_app_sync_on())
1212                 _sync_scheduler_remove_repeating_alarm(alarm_id);
1213 #endif
1214
1215         if (!__alarm_remove_from_list(pid, alarm_id, error_code)) {
1216
1217                 SECURE_LOGE("[alarm-server]:delete alarm: "
1218                                           "alarm(%d) pid(%d) has failed with error_code(%d)\n",
1219                                           alarm_id, pid, *error_code);
1220                 return false;
1221         }
1222
1223         if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
1224                 _alarm_disable_timer(alarm_context);
1225                 _alarm_schedule();
1226         }
1227
1228         __rtc_set();
1229
1230 #ifdef __ALARM_BOOT
1231         /*alarm boot */
1232         if (enable_power_on_alarm) {
1233                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
1234                  * platform with app-server.because __alarm_power_on(..) fuction don't
1235                  * use first parameter internally, we set this value to 0(zero)
1236                  */
1237                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1238         }
1239 #endif
1240
1241         return true;
1242 }
1243
1244 static bool __alarm_power_on(int app_id, bool on_off, int *error_code)
1245 {
1246 #if 0 // vincent.jang def __ALARM_BOOT
1247         time_t min_time = 0;
1248         time_t current_time = 0;
1249         struct rtc_time rtc_tm = { 0, };
1250         struct tm min_time_r = { 0, };
1251         int fd = 0;
1252         int retval;
1253
1254         enable_power_on_alarm = on_off;
1255         /*_update_power_on(on_off); */ /*currently its empty*/
1256
1257         fd = open(power_rtc, O_RDONLY);
1258         if (fd < 0) {
1259                 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc0\n");
1260                 return false;
1261         }
1262
1263         if (on_off == true) {
1264                 if (_alarm_find_mintime_power_on(&min_time) == true) {
1265
1266                         ab_due_time = min_time;
1267
1268                         min_time = min_time - 60;
1269
1270                         time(&current_time);
1271
1272                         if (min_time <= current_time)
1273                                 min_time = current_time + 5;
1274
1275                         gmtime_r(&min_time, &min_time_r);
1276
1277                         ALARM_MGR_LOG_PRINT("__alarm_power_on : %d %d %d %d "
1278                                                 "%d\n", \
1279                                            min_time_r.tm_year,\
1280                                            min_time_r.tm_mon,\
1281                                            min_time_r.tm_mday,\
1282                                            min_time_r.tm_hour,\
1283                                            min_time_r.tm_min);
1284                         ALARM_MGR_LOG_PRINT("__alarm_power_on : %d %d %d %d "
1285                                                 "%d\n", \
1286                                            min_time_r.tm_year,\
1287                                            min_time_r.tm_mon,\
1288                                            min_time_r.tm_mday,\
1289                                            min_time_r.tm_hour,\
1290                                            min_time_r.tm_min);
1291
1292                         rtc_tm.tm_mday = min_time_r.tm_mday;
1293                         rtc_tm.tm_mon = min_time_r.tm_mon;
1294                         rtc_tm.tm_year = min_time_r.tm_year;
1295                         rtc_tm.tm_hour = min_time_r.tm_hour;
1296                         rtc_tm.tm_min = min_time_r.tm_min;
1297                         rtc_tm.tm_sec = min_time_r.tm_sec;
1298                         /*set_info.time_zone = 0; */
1299                         /*set_info.u_interval.day_of_week = 0; */
1300
1301                         /*ALARM_MGR_LOG_PRINT("####__alarm_power_on : %d %d
1302                         %d %d %d\n",set_info.year,set_info.month,set_info.day,
1303                         set_info.hour,set_info.minute); */
1304
1305                         ALARM_MGR_LOG_PRINT("due_time : %d \n", ab_due_time);
1306
1307                         ALARM_MGR_LOG_PRINT("\n\nSetted RTC Alarm date/time is "
1308                                             "%d-%d-%d, %02d:%02d:%02d.\n",
1309                                             rtc_tm.tm_mday, rtc_tm.tm_mon + 1,
1310                                 rtc_tm.tm_year + 1900, rtc_tm.tm_hour,
1311                                 rtc_tm.tm_min, rtc_tm.tm_sec);
1312
1313                         retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
1314                         if (retval == -1) {
1315                                 if (errno == ENOTTY) {
1316                                         ALARM_MGR_EXCEPTION_PRINT(
1317                                         "\n...Alarm IRQs not supported.\n");
1318                                 }
1319                                 ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
1320                                 close(fd);
1321                                 return false;
1322                         }
1323                         ALARM_MGR_LOG_PRINT("[alarm-server]RTC "
1324                                             "alarm(POWER ON) is setted");
1325
1326                         /* Enable alarm interrupts */
1327                         retval = ioctl(fd, RTC_AIE_ON, 0);
1328                         if (retval == -1) {
1329                                 ALARM_MGR_EXCEPTION_PRINT(
1330                                     "RTC_AIE_ON ioctl failed");
1331                                 close(fd);
1332                                 return false;
1333                         }
1334                         ALARM_MGR_LOG_PRINT("[alarm-server]RTC(POWER ON) "
1335                                             "alarm is on");
1336
1337                 } else
1338                         retval = ioctl(fd, RTC_AIE_OFF, 0);
1339         } else {
1340                 ALARM_MGR_LOG_PRINT("__alarm_power_on : off\n");
1341                 retval = ioctl(fd, RTC_AIE_OFF, 0);
1342         }
1343
1344         close(fd);
1345 #endif                          /* #ifdef __ALARM_BOOT */
1346
1347         return true;
1348 }
1349
1350 static bool __alarm_power_off(int app_id, int *error_code)
1351 {
1352 #ifdef __ALARM_BOOT
1353
1354 #endif                          /* #ifdef __ALARM_BOOT */
1355         return true;
1356 }
1357
1358 static bool __alarm_check_next_duetime(int app_id, int *error_code)
1359 {
1360 #ifdef __ALARM_BOOT
1361         time_t current_time;
1362         time_t interval;
1363
1364         time(&current_time);
1365
1366         interval = ab_due_time - current_time;
1367
1368         ALARM_MGR_LOG_PRINT("due_time : %d / current_time %d\n", ab_due_time,
1369                             current_time);
1370
1371         if (interval > 0 && interval <= 60)
1372                 return true;
1373         else
1374                 return false;
1375 #else                           /* #ifdef __ALARM_BOOT */
1376         return true;
1377 #endif
1378 }
1379
1380 static void __alarm_send_noti_to_application(const char *app_service_name,
1381                                              alarm_id_t alarm_id)
1382 {
1383
1384
1385         char service_name[MAX_SERVICE_NAME_LEN];
1386         char object_name[MAX_SERVICE_NAME_LEN];
1387
1388         DBusMessage *message;
1389         DBusMessageIter iter;
1390
1391         if (app_service_name == NULL || strlen(app_service_name) == 0) {
1392                 ALARM_MGR_EXCEPTION_PRINT("This alarm destination is "
1393                                           "invalid\n");
1394                 return;
1395         }
1396         memset(service_name, 0, MAX_SERVICE_NAME_LEN);
1397         memcpy(service_name, app_service_name, strlen(app_service_name));
1398
1399         snprintf(object_name, MAX_SERVICE_NAME_LEN,
1400                  "/com/samsung/alarm/client");
1401
1402         SECURE_LOGD("[alarm server][send expired_alarm(alarm_id=%d)to"
1403         "app_service_name(%s), object_name(%s), interface_name(%s)]\n",\
1404         alarm_id, service_name, object_name, "com.samsung.alarm.client");
1405
1406         message = dbus_message_new_method_call(service_name,
1407                                                object_name,
1408                                                "com.samsung.alarm.client",
1409                                                "alarm_expired");
1410         if (message == NULL) {
1411                 ALARM_MGR_EXCEPTION_PRINT("[alarm server] "
1412                         "dbus_message_new_method_call faild. maybe OOM!.\n");
1413                 SECURE_LOGE("[alarm server] so we cannot "
1414                         "send expired alarm to %s\n", service_name);
1415                 return;
1416         }
1417
1418         dbus_message_set_no_reply(message, TRUE);
1419         /*      if(service_name[0]==':') */
1420         /* we don't need auto activation in a case that
1421            destination_app_service_name starts with a charactor like (:) */
1422         dbus_message_set_auto_start(message, FALSE);
1423
1424         dbus_message_iter_init_append(message, &iter);
1425         if (!dbus_message_iter_append_basic
1426                 (&iter, DBUS_TYPE_INT32, &alarm_id)) {
1427                 dbus_message_unref(message);
1428                 ALARM_MGR_EXCEPTION_PRINT("[alarm server] "
1429                 "dbus_message_iter_append_basic faild. maybe OOM!.\n");
1430                 return;
1431         }
1432
1433         dbus_connection_send(dbus_g_connection_get_connection
1434                              (alarm_context.bus), message, NULL);
1435         dbus_connection_flush(dbus_g_connection_get_connection
1436                               (alarm_context.bus));
1437         dbus_message_unref(message);
1438
1439 }
1440
1441 static void __alarm_expired()
1442 {
1443         const char *destination_app_service_name = NULL;
1444         alarm_id_t alarm_id = -1;
1445         int app_pid = 0;
1446         __alarm_info_t *__alarm_info = NULL;
1447         char alarm_id_val[32]={0,};
1448         int b_len = 0;
1449         bundle *b = NULL;
1450         char *appid = NULL;
1451
1452         ALARM_MGR_LOG_PRINT("[alarm-server]: Enter \n");
1453
1454         time_t current_time;
1455         double interval;
1456
1457         time(&current_time);
1458
1459         interval = difftime(alarm_context.c_due_time, current_time);
1460         ALARM_MGR_LOG_PRINT("[alarm-server]: c_due_time(%d), "
1461                 "current_time(%d), interval(%d)\n", alarm_context.c_due_time,
1462                 current_time, interval);
1463
1464         if (alarm_context.c_due_time > current_time + 1) {
1465                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: False Alarm. due time is (%d) seconds future\n",
1466                         alarm_context.c_due_time - current_time);
1467                 goto done;
1468         }
1469         // 10 seconds is maximum permitted delay from timer expire to this function
1470         if (alarm_context.c_due_time + 10 < current_time) {
1471                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: False Alarm. due time is (%d) seconds past\n",
1472                         current_time - alarm_context.c_due_time);
1473                 goto done;
1474         }
1475
1476         GSList *iter = NULL;
1477         __scheduled_alarm_t *alarm = NULL;
1478
1479         for (iter = g_scheduled_alarm_list; iter != NULL;
1480              iter = g_slist_next(iter)) {
1481                 alarm = iter->data;
1482                 alarm_id = alarm->alarm_id;
1483
1484                 __alarm_info = alarm->__alarm_info;
1485
1486                 app_pid = __alarm_info->pid;
1487
1488                 if (strncmp
1489                     (g_quark_to_string(__alarm_info->quark_bundle),
1490                      "null", 4) != 0) {
1491
1492                                 b_len = strlen(g_quark_to_string(__alarm_info->quark_bundle));
1493
1494                                 b = bundle_decode((bundle_raw *)g_quark_to_string(__alarm_info->quark_bundle), b_len);
1495
1496                                 if (NULL == b)
1497                                 {
1498                                         ALARM_MGR_EXCEPTION_PRINT("Error!!!..Unable to decode the bundle!!\n");
1499                                 }
1500                                 else
1501                                 {
1502                                         snprintf(alarm_id_val,31,"%d",alarm_id);
1503
1504                                         if (bundle_add(b,"http://tizen.org/appcontrol/data/alarm_id", alarm_id_val)){
1505                                                 ALARM_MGR_EXCEPTION_PRINT("Unable to add alarm id to the bundle\n");
1506                                         }
1507                                         else
1508                                         {
1509                                                 appid = appsvc_get_appid(b);
1510                                                 if( (__alarm_info->alarm_info.alarm_type & ALARM_TYPE_NOLAUNCH) && !aul_app_is_running(appid))
1511                                                 {
1512                                                         ALARM_MGR_EXCEPTION_PRINT("This alarm is ignored\n");
1513                                                 }
1514                                                 else
1515                                                 {
1516                                                         if ( appsvc_run_service(b, 0, NULL, NULL) < 0)
1517                                                         {
1518                                                                 ALARM_MGR_EXCEPTION_PRINT("Unable to run app svc\n");
1519                                                         }
1520                                                         else
1521                                                         {
1522                                                                 ALARM_MGR_LOG_PRINT("Successfuly ran app svc\n");
1523                                                         }
1524                                                 }
1525                                         }
1526                                         bundle_free(b);
1527                                 }
1528
1529                 }
1530                 else
1531                 {
1532                         if (strncmp
1533                             (g_quark_to_string(__alarm_info->quark_dst_service_name),
1534                              "null",4) == 0) {
1535                                 SECURE_LOGD("[alarm-server]:destination is "
1536                                 "null, so we send expired alarm to %s(%u)\n",\
1537                                         g_quark_to_string(
1538                                         __alarm_info->quark_app_service_name),
1539                                         __alarm_info->quark_app_service_name);
1540                                         destination_app_service_name = g_quark_to_string(
1541                                         __alarm_info->quark_app_service_name_mod);
1542                         } else {
1543                                 SECURE_LOGD("[alarm-server]:destination "
1544                                                     ":%s(%u)\n",
1545                                         g_quark_to_string(
1546                                         __alarm_info->quark_dst_service_name),
1547                                         __alarm_info->quark_dst_service_name);
1548                                         destination_app_service_name = g_quark_to_string(
1549                                                 __alarm_info->quark_dst_service_name_mod);
1550                         }
1551
1552 #if 0 // vincent.jang def __ALARM_BOOT
1553                         /* orginally this code had if(__alarm_info->app_id==21) in a
1554                            platform with app-server. */
1555                         /*if(__alarm_info->quark_dst_service_name  ==
1556                            g_quark_from_string (WAKEUP_ALARM_APP_ID)) */
1557                         if (strcmp
1558                             (g_quark_to_string(__alarm_info->quark_dst_service_name),
1559                              WAKEUP_ALARM_APP_ID) == 0) {
1560                                 int fd = 0;
1561                                 fd = open(power_rtc, O_RDONLY);
1562                                 if (fd < 0) {
1563                                         ALARM_MGR_LOG_PRINT("cannot open /dev/rtc0\n");
1564                                 } else {
1565                                         ioctl(fd, RTC_AIE_OFF, 0);
1566                                         close(fd);
1567                                 }
1568                         }
1569 #endif
1570
1571                         /*
1572                          * we should consider a situation that
1573                          * destination_app_service_name is owner_name like (:xxxx) and
1574                          * application's pid which registered this alarm was killed.In that case,
1575                          * we don't need to send the expire event because the process was killed.
1576                          * this causes needless message to be sent.
1577                          */
1578                         SECURE_LOGD("[alarm-server]: "
1579                                             "destination_app_service_name :%s, app_pid=%d\n",
1580                                             destination_app_service_name, app_pid);
1581                         /* the following is a code that checks the above situation.
1582                            please verify this code. */
1583
1584                         if (dbus_bus_name_has_owner(
1585                              dbus_g_connection_get_connection(alarm_context.bus),
1586                              destination_app_service_name, NULL) == FALSE) {
1587                                 __expired_alarm_t *expire_info;
1588                                 char appid[MAX_SERVICE_NAME_LEN] = { 0, };
1589                                 char alarm_id_str[32] = { 0, };
1590
1591                                 if (__alarm_info->alarm_info.alarm_type & ALARM_TYPE_WITHCB) {
1592                                         __alarm_remove_from_list(__alarm_info->pid, alarm_id, NULL);
1593                                         goto done;
1594                                 }
1595
1596                                 expire_info = malloc(sizeof(__expired_alarm_t));
1597                                 if (G_UNLIKELY(NULL == expire_info)){
1598                                         ALARM_MGR_ASSERT_PRINT("[alarm-server]:Malloc failed!Can't notify alarm expiry info\n");
1599                                         goto done;
1600                                 }
1601                                 memset(expire_info, '\0', MAX_SERVICE_NAME_LEN);
1602                                 strncpy(expire_info->service_name,
1603                                         destination_app_service_name,
1604                                         MAX_SERVICE_NAME_LEN-1);
1605                                 expire_info->alarm_id = alarm_id;
1606                                 g_expired_alarm_list =
1607                                     g_slist_append(g_expired_alarm_list, expire_info);
1608
1609
1610                                 if (strncmp
1611                                         (g_quark_to_string(__alarm_info->quark_dst_service_name),
1612                                              "null",4) == 0) {
1613                                         strncpy(appid,g_quark_to_string(__alarm_info->quark_app_service_name)+6,strlen(g_quark_to_string(__alarm_info->quark_app_service_name))-6);
1614                                 }
1615                                 else
1616                                 {
1617                                         strncpy(appid,g_quark_to_string(__alarm_info->quark_dst_service_name)+6,strlen(g_quark_to_string(__alarm_info->quark_dst_service_name))-6);
1618                                 }
1619
1620                                 snprintf(alarm_id_str, 31, "%d", alarm_id);
1621
1622                                 SECURE_LOGD("before aul_launch appid(%s) "
1623                                         "alarm_id_str(%s)\n", appid, alarm_id_str);
1624
1625                                 bundle *kb;
1626                                 kb = bundle_create();
1627                                 bundle_add(kb, "__ALARM_MGR_ID", alarm_id_str);
1628                                 aul_launch_app(appid, kb);
1629                                 bundle_free(kb);
1630                         } else {
1631                                 ALARM_MGR_LOG_PRINT(
1632                                         "before alarm_send_noti_to_application\n");
1633                                 __alarm_send_noti_to_application(
1634                                              destination_app_service_name, alarm_id);
1635                         }
1636                 }
1637                 ALARM_MGR_LOG_PRINT("after __alarm_send_noti_to_application\n");
1638
1639 /*              if( !(__alarm_info->alarm_info.alarm_type
1640                                         & ALARM_TYPE_VOLATILE) ) {
1641                         __alarm_remove_from_list(__alarm_info->pid,
1642                                                         alarm_id, NULL);
1643                 }
1644                 else */
1645                 if (__alarm_info->alarm_info.mode.repeat
1646                     == ALARM_REPEAT_MODE_ONCE) {
1647 /*                      _alarm_next_duetime(__alarm_info);*/
1648 /*                      _update_alarms(__alarm_info);*/
1649                         __alarm_remove_from_list(__alarm_info->pid, alarm_id,
1650                                                  NULL);
1651                 } else {
1652
1653                         _alarm_next_duetime(__alarm_info);
1654 /*                      _update_alarms(__alarm_info);*/
1655                 }
1656
1657         }
1658
1659  done:
1660         _clear_scheduled_alarm_list();
1661         alarm_context.c_due_time = -1;
1662
1663         ALARM_MGR_LOG_PRINT("[alarm-server]: Leave  \n");
1664 }
1665
1666 static gboolean __alarm_handler_idle()
1667 {
1668 #ifdef WEARABLE_PROFILE
1669         display_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
1670 #endif
1671         if (g_dummy_timer_is_set == true) {
1672                 ALARM_MGR_LOG_PRINT("dummy alarm timer has expired\n");
1673         } else {
1674                 ALARM_MGR_LOG_PRINT("__alarm_handler \n");
1675                 ALARM_MGR_LOG_PRINT("__alarm_handler \n");
1676
1677                 __alarm_expired();
1678
1679         }
1680
1681         _alarm_schedule();
1682
1683         __rtc_set();
1684
1685 #ifdef __ALARM_BOOT
1686         /*alarm boot */
1687         if (enable_power_on_alarm) {
1688                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID)
1689                  *in a platform with app-server.because __alarm_power_on(..) fuction
1690                  *don't use first parameter internally, we set this value to 0(zero)
1691                  */
1692                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1693         }
1694 #endif
1695 #ifdef WEARABLE_PROFILE
1696         display_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
1697 #endif
1698         return false;
1699
1700 }
1701
1702 static void __alarm_handler(int sigNum, siginfo_t *pSigInfo, void *pUContext)
1703 {
1704         /* we moved __alarm_expired() function to __alarm_handler_idle GSource
1705            because of signal safety. */
1706         g_idle_add_full(G_PRIORITY_HIGH, __alarm_handler_idle, NULL, NULL);
1707 }
1708
1709 static void __clean_registry()
1710 {
1711
1712         /*TODO:remove all db entries */
1713 }
1714
1715 static bool __alarm_manager_reset()
1716 {
1717
1718         _alarm_disable_timer(alarm_context);
1719
1720         __alarm_clean_list();
1721
1722         _clear_scheduled_alarm_list();
1723         __clean_registry();
1724
1725         return true;
1726 }
1727
1728 static int __alarm_delete_handler(pkgmgrinfo_appinfo_h handle, void* data)
1729 {
1730         char* appid;
1731         GSList* gs_iter = NULL;
1732         __alarm_info_t* entry = NULL;
1733         alarm_info_t* alarm_info = NULL;
1734         gchar* bundle_data = NULL;
1735         bundle* b = NULL;
1736         const char* caller_appid = NULL;
1737         const char* callee_appid = NULL;
1738         bool isDeleted = false;
1739
1740         pkgmgrinfo_appinfo_get_appid(handle, &appid);   // uninstalled app id
1741         SECURE_LOGD("The uninstalled appid is [%s]\n", appid);
1742
1743         // If the uninstalled app is same to caller or callee app, all alarms registered in the app will be deleted.
1744         for (gs_iter = alarm_context.alarms; gs_iter != NULL; )
1745         {
1746                 bool isFound = false;
1747                 entry = gs_iter->data;
1748
1749                 bundle_data = g_strdup(g_quark_to_string(entry->quark_bundle));
1750                 b = bundle_decode((bundle_raw*)bundle_data, strlen(bundle_data));
1751                 caller_appid = bundle_get_val(b, "__ALARM_MGR_CALLER_APPID");
1752                 callee_appid = appsvc_get_appid(b);
1753
1754                 SECURE_LOGD("Try to remove alarm_id[%d], caller_appid[%s], callee_appid[%s]", entry->alarm_id, caller_appid, callee_appid);
1755
1756                 if (strcmp(appid, caller_appid) == 0 || strcmp(appid, callee_appid) == 0)
1757                 {
1758                         if (_remove_from_scheduled_alarm_list(appid, entry->alarm_id))
1759                         {
1760                                 isDeleted = true;
1761                         }
1762
1763                         alarm_info = &entry->alarm_info;
1764                         if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE))
1765                         {
1766                                 if(!_delete_alarms(entry->alarm_id))
1767                                 {
1768                                         SECURE_LOGD("_delete_alarms() is failed. appid[%s], alarm_id[%d]", appid, entry->alarm_id);
1769                                 }
1770                         }
1771                         SECURE_LOGD("Removing is done! alarm_id[%d]", entry->alarm_id);
1772                         isFound = true;
1773                 }
1774
1775                 gs_iter = g_slist_next(gs_iter);
1776
1777                 if (isFound)
1778                 {
1779                         alarm_context.alarms = g_slist_remove(alarm_context.alarms, entry);
1780                 }
1781         }
1782
1783         if (isDeleted && (g_slist_length(g_scheduled_alarm_list) == 0))
1784         {
1785                 _alarm_disable_timer(alarm_context);
1786                 _alarm_schedule();
1787         }
1788
1789         __rtc_set();
1790
1791         return 0;
1792 }
1793
1794 static void __alarm_delete_for_webapp(char* pkgid)
1795 {
1796         GSList* gs_iter = NULL;
1797         __alarm_info_t* entry = NULL;
1798         alarm_info_t* alarm_info = NULL;
1799         gchar* bundle_data = NULL;
1800         bundle* b = NULL;
1801         const char* caller_appid = NULL;
1802         const char* callee_appid = NULL;
1803         const char* appid = NULL;
1804         bool isDeleted = false;
1805
1806         SECURE_LOGD("The uninstalled pkgid of webapp is [%s]\n", pkgid);
1807
1808         // If the uninstalled web app(wgt) is same to caller or callee app, all alarms registered in the app will be deleted.
1809         for (gs_iter = alarm_context.alarms; gs_iter != NULL; )
1810         {
1811                 entry = gs_iter->data;
1812
1813                 bundle_data = g_strdup(g_quark_to_string(entry->quark_bundle));
1814                 b = bundle_decode((bundle_raw*)bundle_data, strlen(bundle_data));
1815                 caller_appid = bundle_get_val(b, "__ALARM_MGR_CALLER_APPID");
1816                 callee_appid = appsvc_get_appid(b);
1817
1818                 SECURE_LOGD("Try to remove alarm_id[%d], caller_appid[%s], callee_appid[%s]", entry->alarm_id, caller_appid, callee_appid);
1819
1820                 if (strncmp(pkgid, caller_appid, 10) == 0)
1821                 {
1822                         appid = caller_appid;
1823                 }
1824                 else if (strncmp(pkgid, callee_appid, 10) == 0)
1825                 {
1826                         appid = callee_appid;
1827                 }
1828                 else
1829                 {
1830                         gs_iter = g_slist_next(gs_iter);
1831                         continue;
1832                 }
1833
1834                 if (_remove_from_scheduled_alarm_list(appid, entry->alarm_id))
1835                 {
1836                         isDeleted = true;
1837                 }
1838
1839                 alarm_info = &entry->alarm_info;
1840                 if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE))
1841                 {
1842                         if(!_delete_alarms(entry->alarm_id))
1843                         {
1844                                 SECURE_LOGD("_delete_alarms() is failed. appid[%s], alarm_id[%d]", appid, entry->alarm_id);
1845                         }
1846                 }
1847                 SECURE_LOGD("Removing is done! alarm_id[%d]", entry->alarm_id);
1848
1849                 gs_iter = g_slist_next(gs_iter);
1850                 alarm_context.alarms = g_slist_remove(alarm_context.alarms, entry);
1851         }
1852
1853         if (isDeleted && (g_slist_length(g_scheduled_alarm_list) == 0))
1854         {
1855                 _alarm_disable_timer(alarm_context);
1856                 _alarm_schedule();
1857         }
1858
1859         __rtc_set();
1860 }
1861
1862 static void __on_system_time_external_changed(keynode_t *node, void *data)
1863 {
1864         double diff_time;
1865         time_t cur_time = 0;
1866
1867         _alarm_disable_timer(alarm_context);
1868
1869         if (node) {
1870                 diff_time = vconf_keynode_get_dbl(node);
1871         } else {
1872                 vconf_get_dbl(VCONFKEY_SYSTEM_TIMECHANGE_EXTERNAL, &diff_time);
1873         }
1874
1875         tzset();
1876         time(&cur_time);
1877
1878         ALARM_MGR_EXCEPTION_PRINT("diff_time is %f, New time is %s\n", diff_time, ctime(&cur_time));
1879
1880         ALARM_MGR_LOG_PRINT("[alarm-server] System time has been changed externally\n");
1881         ALARM_MGR_LOG_PRINT("1.alarm_context.c_due_time is %d\n",
1882                             alarm_context.c_due_time);
1883
1884         // set rtc time only because the linux time is set externally
1885         _set_rtc_time(cur_time);
1886
1887         vconf_set_int(VCONFKEY_SYSTEM_TIME_CHANGED,(int)diff_time);
1888
1889         __alarm_update_due_time_of_all_items_in_list(diff_time);
1890
1891         ALARM_MGR_LOG_PRINT("2.alarm_context.c_due_time is %d\n",
1892                             alarm_context.c_due_time);
1893         _clear_scheduled_alarm_list();
1894         _alarm_schedule();
1895         __rtc_set();
1896 #ifdef __ALARM_BOOT
1897         /*alarm boot */
1898         if (enable_power_on_alarm) {
1899 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
1900 APP_ID) in a platform with app-server. because _alarm_power_
1901 on(..) fuction don't use first parameter internally, we set
1902 this value to 0(zero)
1903 */
1904                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1905         }
1906 #endif
1907         return;
1908 }
1909
1910 static void __on_time_zone_changed(keynode_t *node, void *data)
1911 {
1912         double diff_time = 0;
1913
1914         _alarm_disable_timer(alarm_context);
1915
1916         tzset();
1917
1918         ALARM_MGR_LOG_PRINT("[alarm-server] time zone has been changed\n");
1919         ALARM_MGR_LOG_PRINT("1.alarm_context.c_due_time is %d\n", alarm_context.c_due_time);
1920
1921         __alarm_update_due_time_of_all_items_in_list(diff_time);
1922
1923         ALARM_MGR_LOG_PRINT("2.alarm_context.c_due_time is %d\n", alarm_context.c_due_time);
1924         _clear_scheduled_alarm_list();
1925         _alarm_schedule();
1926         __rtc_set();
1927 #ifdef __ALARM_BOOT
1928         /*alarm boot */
1929         if (enable_power_on_alarm) {
1930 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
1931 APP_ID) in a platform with app-server. because _alarm_power_
1932 on(..) fuction don't use first parameter internally, we set
1933 this value to 0(zero)
1934 */
1935                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1936         }
1937 #endif
1938         return;
1939 }
1940
1941 static int __on_app_uninstalled(int req_id, const char *pkg_type,
1942                                 const char *pkgid, const char *key, const char *val,
1943                                 const void *pmsg, void *user_data)
1944 {
1945         pkgmgrinfo_pkginfo_h handle;
1946
1947         SECURE_LOGD("pkg_type(%s), pkgid(%s), key(%s), value(%s)", pkg_type, pkgid, key, val);
1948
1949         if (strncmp(key, "start", 5) == 0 && strncmp(val, "uninstall", 9) == 0)
1950         {
1951                 if (strncmp(pkg_type, "wgt", 3) != 0)
1952                 {
1953                         if (pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle) != PMINFO_R_OK)
1954                         {
1955                                 ALARM_MGR_EXCEPTION_PRINT("Getting package info is failed.\n");
1956                                 return PMINFO_R_ERROR;
1957                         }
1958
1959                         if (pkgmgrinfo_appinfo_get_list(handle, PMINFO_UI_APP, __alarm_delete_handler, user_data) != PMINFO_R_OK)
1960                         {
1961                                 ALARM_MGR_EXCEPTION_PRINT("Getting package list is failed.\n");
1962                                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1963                                 return PMINFO_R_ERROR;
1964                         }
1965
1966                         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
1967                 }
1968         }
1969         else if (strncmp(key, "end", 3) == 0 && strncmp(val, "ok", 2) == 0)
1970         {
1971                 if (strncmp(pkg_type, "wgt", 3) == 0)
1972                 {
1973                         __alarm_delete_for_webapp(pkgid);
1974                 }
1975         }
1976
1977         return PMINFO_R_OK;
1978 }
1979
1980 gboolean alarm_manager_alarm_set_rtc_time(void *pObject, int pid,
1981                                 int year, int mon, int day,
1982                                 int hour, int min, int sec, char *e_cookie,
1983                                 int *return_code){
1984         guchar *cookie = NULL;
1985         gsize size;
1986         int retval = 0;
1987         gboolean result = true;
1988
1989         const char *rtc = default_rtc;
1990         struct timespec alarm_time;
1991
1992         struct rtc_time rtc_tm = {0,};
1993         struct rtc_wkalrm rtc_wk;
1994         struct tm *alarm_tm = NULL;
1995
1996         if (return_code){
1997                 *return_code = ALARMMGR_RESULT_SUCCESS;
1998         }
1999
2000         cookie = g_base64_decode(e_cookie, &size);
2001         if (NULL == cookie)
2002         {
2003                 if (return_code)
2004                         *return_code = ERR_ALARM_NO_PERMISSION;
2005                 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
2006                 return true;
2007         }
2008
2009         retval = security_server_check_privilege_by_cookie((const char *)cookie, "alarm-server::alarm", "w");
2010         if (retval < 0) {
2011                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
2012                         ALARM_MGR_EXCEPTION_PRINT(
2013                                 "%s", "Write access has been denied by smack\n");
2014                 }
2015                 ALARM_MGR_EXCEPTION_PRINT("Error has occurred in security_server_check_privilege()\n");
2016                 if (return_code)
2017                         *return_code = ERR_ALARM_NO_PERMISSION;
2018         }
2019         else
2020         {
2021                 /*extract day of the week, day in the year &
2022                 daylight saving time from system*/
2023                 time_t ctime;
2024                 ctime = time(NULL);
2025                 alarm_tm = localtime(&ctime);
2026
2027                 alarm_tm->tm_year = year;
2028                 alarm_tm->tm_mon = mon;
2029                 alarm_tm->tm_mday = day;
2030                 alarm_tm->tm_hour = hour;
2031                 alarm_tm->tm_min = min;
2032                 alarm_tm->tm_sec = sec;
2033
2034                 /*convert to calendar time representation*/
2035                 time_t rtc_time = mktime(alarm_tm);
2036
2037                 if (gfd == 0) {
2038                         gfd = open(rtc, O_RDWR);
2039                         if (gfd == -1) {
2040                                 ALARM_MGR_EXCEPTION_PRINT("RTC open failed.\n");
2041                                 if (return_code)
2042                                         *return_code = ERR_ALARM_SYSTEM_FAIL;
2043                                 return result;
2044                         }
2045                 }
2046
2047                 alarm_time.tv_sec = rtc_time;
2048                 alarm_time.tv_nsec = 0;
2049
2050                 retval = ioctl(gfd, ALARM_SET(ALARM_RTC_WAKEUP), &alarm_time);
2051                 if (retval == -1) {
2052                         if (errno == ENOTTY) {
2053                                 ALARM_MGR_EXCEPTION_PRINT("\nAlarm IRQs not"
2054                                                           "supported.\n");
2055                         }
2056                         ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
2057                         //close(fd);
2058                         if (return_code)
2059                                 *return_code = ERR_ALARM_SYSTEM_FAIL;
2060                 }
2061                 else{
2062                         ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is setted");
2063                         //close(fd);
2064                 }
2065         }
2066
2067         if (cookie){
2068                 g_free(cookie);
2069                 cookie = NULL;
2070         }
2071
2072         return result;
2073
2074 }
2075
2076 gboolean alarm_manager_alarm_set_time(void *pObject, int _time, int *return_code)
2077 {
2078         double diff_time;
2079         time_t before;
2080         gboolean result = true;
2081
2082         if (return_code){
2083                 *return_code = ALARMMGR_RESULT_SUCCESS;
2084         }
2085
2086         _alarm_disable_timer(alarm_context);
2087
2088         time(&before);
2089         diff_time = difftime(_time, before);
2090
2091         tzset();
2092
2093         ALARM_MGR_EXCEPTION_PRINT("New time is %s, diff_time is %f\n", ctime(&_time), diff_time);
2094
2095         ALARM_MGR_LOG_PRINT("[alarm-server] System time has been changed\n");
2096         ALARM_MGR_LOG_PRINT("1.alarm_context.c_due_time is %d\n",
2097                                         alarm_context.c_due_time);
2098
2099         _set_time(_time-1);
2100         vconf_set_int(VCONFKEY_SYSTEM_TIME_CHANGED,(int)diff_time);
2101
2102         __alarm_update_due_time_of_all_items_in_list(diff_time);
2103
2104         ALARM_MGR_LOG_PRINT("2.alarm_context.c_due_time is %d\n",
2105                                 alarm_context.c_due_time);
2106         _clear_scheduled_alarm_list();
2107         _alarm_schedule();
2108         __rtc_set();
2109         _set_time(_time);
2110 #ifdef __ALARM_BOOT
2111         /*alarm boot */
2112         if (enable_power_on_alarm) {
2113         /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
2114         APP_ID) in a platform with app-server. because _alarm_power_
2115         on(..) fuction don't use first parameter internally, we set
2116         this value to 0(zero)
2117         */
2118                         __alarm_power_on(0, enable_power_on_alarm, NULL);
2119                 }
2120 #endif
2121         return result;
2122 }
2123
2124 gboolean alarm_manager_alarm_create_appsvc(void *pObject, int pid,
2125                                     int start_year,
2126                                     int start_month, int start_day,
2127                                     int start_hour, int start_min,
2128                                     int start_sec, int end_year, int end_month,
2129                                     int end_day, int mode_day_of_week,
2130                                     int mode_repeat, int alarm_type,
2131                                     int reserved_info,
2132                                     char *bundle_data, char *e_cookie,
2133                                     int *alarm_id, int *return_code)
2134 {
2135         alarm_info_t alarm_info;
2136         guchar *cookie = NULL;
2137         gsize size;
2138         int retval = 0;
2139         gboolean result = true;
2140
2141         alarm_info.start.year = start_year;
2142         alarm_info.start.month = start_month;
2143         alarm_info.start.day = start_day;
2144         alarm_info.start.hour = start_hour;
2145         alarm_info.start.min = start_min;
2146         alarm_info.start.sec = start_sec;
2147
2148         alarm_info.end.year = end_year;
2149         alarm_info.end.month = end_month;
2150         alarm_info.end.day = end_day;
2151
2152         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
2153         alarm_info.mode.repeat = mode_repeat;
2154
2155         alarm_info.alarm_type = alarm_type;
2156         alarm_info.reserved_info = reserved_info;
2157
2158         *return_code = 0;
2159
2160         cookie = g_base64_decode(e_cookie, &size);
2161         if (NULL == cookie)
2162         {
2163                 *return_code = -1;
2164                 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
2165                 return false;
2166         }
2167
2168         retval = security_server_check_privilege_by_cookie((const char *)cookie, "alarm-server::alarm", "w");
2169         if (retval < 0) {
2170                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
2171                         ALARM_MGR_EXCEPTION_PRINT(
2172                                 "%s", "Write access has been denied by smack\n");
2173                 }
2174                 ALARM_MGR_EXCEPTION_PRINT("Error has occurred in security_server_check_privilege()\n");
2175                 *return_code = ERR_ALARM_NO_PERMISSION;
2176                 result = false;
2177         }
2178         else
2179         {
2180                 result = __alarm_create_appsvc(&alarm_info, alarm_id, pid,
2181                                bundle_data, return_code);
2182                 if (false == result)
2183                 {
2184                         ALARM_MGR_EXCEPTION_PRINT("Unable to create alarm!\n");
2185                 }
2186         }
2187
2188         if (cookie){
2189                 g_free(cookie);
2190                 cookie = NULL;
2191         }
2192
2193         return result;
2194 }
2195
2196 gboolean alarm_manager_alarm_create(void *pObject, int pid,
2197                                     char *app_service_name, char *app_service_name_mod,  int start_year,
2198                                     int start_month, int start_day,
2199                                     int start_hour, int start_min,
2200                                     int start_sec, int end_year, int end_month,
2201                                     int end_day, int mode_day_of_week,
2202                                     int mode_repeat, int alarm_type,
2203                                     int reserved_info,
2204                                     char *reserved_service_name, char *reserved_service_name_mod, char *e_cookie,
2205                                     int *alarm_id, int *return_code)
2206 {
2207         alarm_info_t alarm_info;
2208         guchar *cookie;
2209         gsize size;
2210         int retval = 0;
2211         char buf[128];
2212         FILE *fp = NULL;
2213         char title[128];
2214         int uid = -1;
2215
2216         alarm_info.start.year = start_year;
2217         alarm_info.start.month = start_month;
2218         alarm_info.start.day = start_day;
2219         alarm_info.start.hour = start_hour;
2220         alarm_info.start.min = start_min;
2221         alarm_info.start.sec = start_sec;
2222
2223         alarm_info.end.year = end_year;
2224         alarm_info.end.month = end_month;
2225         alarm_info.end.day = end_day;
2226
2227         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
2228         alarm_info.mode.repeat = mode_repeat;
2229
2230         alarm_info.alarm_type = alarm_type;
2231         alarm_info.reserved_info = reserved_info;
2232
2233         *return_code = 0;
2234
2235         snprintf(buf, sizeof(buf), "/proc/%d/status", pid);
2236         fp = fopen(buf,"r");
2237         if(fp) {
2238                 while (fgets(buf, sizeof(buf), fp) != NULL) {
2239                         if(strncmp(buf, "Uid:", 4) == 0) {
2240                                 sscanf(buf, "%s %d", title, &uid);
2241                                 break;
2242                         }
2243                 }
2244                 fclose(fp);
2245         }
2246
2247         ALARM_MGR_LOG_PRINT("uid :%d", uid);
2248
2249         if(uid != 0) {
2250                 cookie = g_base64_decode(e_cookie, &size);
2251                 retval = security_server_check_privilege_by_cookie((const char *)cookie, "alarm-server::alarm", "w");
2252         }
2253
2254         if (retval < 0) {
2255                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
2256                         ALARM_MGR_EXCEPTION_PRINT(
2257                                 "Write access has been denied by smack\n");
2258                 }
2259                 ALARM_MGR_EXCEPTION_PRINT("Error has occurred\n");
2260                 *return_code = ERR_ALARM_NO_PERMISSION;
2261         }
2262         else
2263         {
2264                 /* return valule and return_code should be checked */
2265                 __alarm_create(&alarm_info, alarm_id, pid, app_service_name,app_service_name_mod,
2266                                reserved_service_name, reserved_service_name_mod, return_code);
2267         }
2268         if(uid != 0)
2269                 g_free(cookie);
2270
2271         return true;
2272 }
2273
2274 gboolean alarm_manager_alarm_delete(void *pObject, int pid, alarm_id_t alarm_id,
2275                                     char *e_cookie, int *return_code)
2276 {
2277         guchar *cookie;
2278         gsize size;
2279         int retval = 0;
2280         char buf[128];
2281         FILE *fp = NULL;
2282         char title[128];
2283         int uid = -1;
2284
2285         snprintf(buf, sizeof(buf), "/proc/%d/status", pid);
2286         fp = fopen(buf,"r");
2287         if(fp) {
2288                 while (fgets(buf, sizeof(buf), fp) != NULL) {
2289                         if(strncmp(buf, "Uid:", 4) == 0) {
2290                                 sscanf(buf, "%s %d", title, &uid);
2291                                 break;
2292                         }
2293                 }
2294                 fclose(fp);
2295         }
2296
2297         ALARM_MGR_LOG_PRINT("uid :%d", uid);
2298
2299         if(uid != 0) {
2300                 cookie = g_base64_decode(e_cookie, &size);
2301                 retval = security_server_check_privilege_by_cookie((const char *)cookie, "alarm-server::alarm", "w");
2302         }
2303
2304         if (retval < 0) {
2305                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
2306                         ALARM_MGR_EXCEPTION_PRINT(
2307                                 "Write access has been denied by smack\n");
2308                 }
2309                 ALARM_MGR_EXCEPTION_PRINT("Error has occurred\n");
2310                 *return_code = ERR_ALARM_NO_PERMISSION;
2311         }
2312         else
2313         {
2314                 __alarm_delete(pid, alarm_id, return_code);
2315         }
2316
2317         if(uid != 0)
2318                 g_free(cookie);
2319
2320         return true;
2321 }
2322
2323 gboolean alarm_manager_alarm_delete_all(void *pObject, int pid, int* return_code)
2324 {
2325         GSList* gs_iter = NULL;
2326         GQuark quark_app_unique_name;   // the fullpath of pid(pid) is converted to quark value.
2327         char proc_file[256] = { 0 };
2328         char process_name[512] = { 0 };
2329         char app_name[256] = { 0 };
2330         char* word = NULL;
2331         alarm_info_t* alarm_info = NULL;
2332         __alarm_info_t* entry = NULL;
2333         char* proc_name_ptr = NULL;
2334         int fd;
2335         int ret;
2336         int i = 0;
2337         bool isDeleted = false;
2338         *return_code = ALARMMGR_RESULT_SUCCESS;
2339
2340         // we should consider to check whether  pid is running or Not
2341         memset(process_name, '\0', 512);
2342         memset(proc_file, '\0', 256);
2343         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
2344
2345         fd = open(proc_file, O_RDONLY);
2346         if (fd < 0)
2347         {
2348                 quark_app_unique_name = g_quark_from_string("unknown");
2349                 memcpy(app_name, "unknown", strlen("unknown") + 1);
2350
2351                 SECURE_LOGE("Caution!! app_pid(%d) seems to be killed. so we failed to get proc file(%s)\n", pid, proc_file);
2352                 *return_code = ERR_ALARM_SYSTEM_FAIL;
2353                 return true;
2354         }
2355         else
2356         {
2357                 ret = read(fd, process_name, 512);
2358                 if (ret < 0)
2359                 {
2360                         *return_code = ERR_ALARM_SYSTEM_FAIL;
2361                         close(fd);
2362                         return true;
2363                 }
2364                 close(fd);
2365                 while (process_name[i] != '\0')
2366                 {
2367                         if (process_name[i] == ' ')
2368                         {
2369                                 process_name[i] = '\0';
2370                                 break;
2371                         }
2372                         i++;
2373                 }
2374
2375                 word = strtok_r(process_name, "/", &proc_name_ptr);
2376                 while (word != NULL)
2377                 {
2378                         memset(app_name, 0, 256);
2379                         snprintf(app_name, 256, "%s", word);
2380                         word = strtok_r(NULL, "/", &proc_name_ptr);
2381                 }
2382                 quark_app_unique_name = g_quark_from_string(app_name);
2383         }
2384
2385         SECURE_LOGD("called for app(pid:%d, name=%s)\n", pid, app_name);
2386
2387         for (gs_iter = alarm_context.alarms; gs_iter != NULL; )
2388         {
2389                 bool isFound = false;
2390                 entry = gs_iter->data;
2391                 SECURE_LOGD("Try to remove app_name[%s], alarm_id[%d]\n", g_quark_to_string(entry->quark_app_unique_name), entry->alarm_id);
2392                 if (strcmp(app_name, g_quark_to_string(entry->quark_app_unique_name)) == 0
2393                         && strcmp(app_name, "unknown") != 0)
2394                 {
2395                         if (_remove_from_scheduled_alarm_list(pid, entry->alarm_id))
2396                         {
2397                                 isDeleted = true;
2398                         }
2399
2400                         alarm_info = &entry->alarm_info;
2401                         if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE))
2402                         {
2403                                 if(!_delete_alarms(entry->alarm_id))
2404                                 {
2405                                         SECURE_LOGE("_delete_alarms() is failed. pid[%d], alarm_id[%d]", pid, entry->alarm_id);
2406                                 }
2407                         }
2408                         SECURE_LOGD("Removing is done. app_name[%s], alarm_id [%d]\n", g_quark_to_string(entry->quark_app_unique_name), entry->alarm_id);
2409                         isFound = true;
2410                 }
2411
2412                 gs_iter = g_slist_next(gs_iter);
2413
2414                 if (isFound)
2415                 {
2416                         alarm_context.alarms = g_slist_remove(alarm_context.alarms, entry);
2417                 }
2418         }
2419
2420         if (isDeleted && (g_slist_length(g_scheduled_alarm_list) == 0))
2421         {
2422                 _alarm_disable_timer(alarm_context);
2423                 _alarm_schedule();
2424         }
2425
2426         __rtc_set();
2427
2428         return true;
2429 }
2430
2431 gboolean alarm_manager_alarm_power_on(void *pObject, int pid, bool on_off,
2432                                       int *return_code)
2433 {
2434
2435         return __alarm_power_on(pid, on_off, return_code);
2436 }
2437
2438 gboolean alarm_manager_alarm_power_off(void *pObject, int pid, int *return_code)
2439 {
2440
2441         return __alarm_power_off(pid, return_code);
2442 }
2443
2444 bool alarm_manager_alarm_check_next_duetime(void *pObject, int pid,
2445                                             int *return_code)
2446 {
2447         return __alarm_check_next_duetime(pid, return_code);
2448 }
2449
2450 gboolean alarm_manager_alarm_update(void *pObject, int pid,
2451                                     char *app_service_name, alarm_id_t alarm_id,
2452                                     int start_year, int start_month,
2453                                     int start_day, int start_hour,
2454                                     int start_min, int start_sec, int end_year,
2455                                     int end_month, int end_day,
2456                                     int mode_day_of_week, int mode_repeat,
2457                                     int alarm_type, int reserved_info,
2458                                     int *return_code)
2459 {
2460         alarm_info_t alarm_info;
2461         alarm_info.start.year = start_year;
2462         alarm_info.start.month = start_month;
2463         alarm_info.start.day = start_day;
2464         alarm_info.start.hour = start_hour;
2465         alarm_info.start.min = start_min;
2466         alarm_info.start.sec = start_sec;
2467
2468         alarm_info.end.year = end_year;
2469         alarm_info.end.month = end_month;
2470         alarm_info.end.day = end_day;
2471
2472         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
2473         alarm_info.mode.repeat = mode_repeat;
2474
2475         alarm_info.alarm_type = alarm_type;
2476         alarm_info.reserved_info = reserved_info;
2477
2478         *return_code = 0;
2479
2480         __alarm_update(pid, app_service_name, alarm_id, &alarm_info,
2481                        return_code);
2482
2483         return true;
2484 }
2485
2486 gboolean alarm_manager_alarm_get_number_of_ids(void *pObject, int pid,
2487                                                int *num_of_ids,
2488                                                int *return_code)
2489 {
2490         GSList *gs_iter = NULL;
2491         GQuark quark_app_unique_name;   /* the fullpath of pid(pid) is
2492                                            converted  to quark value. */
2493         char proc_file[256] = { 0 };
2494         char process_name[512] = { 0 };
2495         char app_name[256] = { 0 };
2496         char *word = NULL;
2497         __alarm_info_t *entry = NULL;
2498         char *proc_name_ptr = NULL;
2499
2500         *num_of_ids = 0;
2501         *return_code = 0;
2502
2503         /* we should consider to check whether  pid is running or Not
2504          */
2505         memset(process_name, '\0', 512);
2506         memset(proc_file, '\0', 256);
2507         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
2508
2509         int fd;
2510         int ret;
2511         int i = 0;
2512         fd = open(proc_file, O_RDONLY);
2513         if (fd < 0) {           /* failure */
2514                 quark_app_unique_name = g_quark_from_string("unknown");
2515                 memcpy(app_name, "unknown", strlen("unknown") + 1);
2516
2517                 SECURE_LOGE("Caution!! app_pid(%d) seems to be "
2518                                           "killed, so we failed to get proc file(%s) \n",
2519                                           pid, proc_file);
2520                 *return_code = -1;      /* -1 means that system
2521                                            failed internally. */
2522                 return true;
2523         } else {
2524                 ret = read(fd, process_name, 512);
2525                 if (ret <0 ) {
2526                         *return_code = -1;      /* -1 means that system
2527                                            failed internally. */
2528                         close(fd);
2529                         return false;
2530                 }
2531                 close(fd);
2532                 while (process_name[i] != '\0') {
2533                         if (process_name[i] == ' ') {
2534                                 process_name[i] = '\0';
2535                                 break;
2536                         }
2537                         i++;
2538                 }
2539                 /*if (readlink(proc_file, process_name, 256)!=-1) */
2540                 /*success */
2541
2542                 word = strtok_r(process_name, "/", &proc_name_ptr);
2543                 while (word != NULL) {
2544                         memset(app_name, 0, 256);
2545                         snprintf(app_name, 256, "%s", word);
2546                         word = strtok_r(NULL, "/", &proc_name_ptr);
2547                 }
2548                 quark_app_unique_name = g_quark_from_string(app_name);
2549         }
2550
2551         SECURE_LOGD("called for  app(pid:%d, name=%s)\n",
2552                             pid, app_name);
2553
2554         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2555              gs_iter = g_slist_next(gs_iter)) {
2556                 entry = gs_iter->data;
2557                 SECURE_LOGD("alarm_manager_alarm_get_number_of_ids(): "
2558                                     "app_name=%s,quark_app_unique_name=%s\n",
2559                 app_name, g_quark_to_string(entry->quark_app_unique_name));
2560                 if (strcmp
2561                     (app_name,
2562                      g_quark_to_string(entry->quark_app_unique_name)) == 0
2563                     && strcmp(app_name, "unknown") != 0) {
2564                         (*num_of_ids)++;
2565                         SECURE_LOGD("inc number of alarms of app "
2566                                             "(pid:%d, name:%s) is %d\n",
2567                                             pid, app_name, *num_of_ids);
2568                 }
2569         }
2570         *return_code = 0;
2571         SECURE_LOGD("number of alarms of app(pid:%d, name:%s) is %d\n",
2572                             pid, app_name, *num_of_ids);
2573         return true;
2574 }
2575
2576 gboolean alarm_manager_alarm_get_list_of_ids(void *pObject, int pid,
2577                                              int max_number_of_ids,
2578                                              GArray **arr, int *num_of_ids,
2579                                              int *return_code)
2580 {
2581         GSList *gs_iter = NULL;
2582         GQuark quark_app_unique_name;   /* the fullpath of pid(pid) is converted
2583                                            to quark value. */
2584         char proc_file[256] = { 0 };
2585         char process_name[512] = { 0 };
2586         char app_name[256] = { 0 };
2587         char *word = NULL;
2588         __alarm_info_t *entry = NULL;
2589         int index = 0;
2590         char *proc_name_ptr = NULL;
2591         int fd;
2592         int ret;
2593         int i = 0;
2594         GArray *garray = NULL;
2595
2596         *return_code = 0;
2597
2598         garray = g_array_new(false, true, sizeof(alarm_id_t));
2599
2600         /* we should check that there is a resource leak.
2601          * Now we don't have free code for g_array_new().
2602          */
2603         if (max_number_of_ids <= 0) {
2604                 *arr = garray;
2605                 SECURE_LOGE("called for  pid(%d), but "
2606                                           "max_number_of_ids(%d) is less than 0.\n",
2607                                           pid, max_number_of_ids);
2608                 return true;
2609         }
2610
2611         /* we should consider to check whether  pid is running or Not
2612          */
2613         memset(process_name, '\0', 512);
2614         memset(proc_file, '\0', 256);
2615         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
2616
2617         fd = open(proc_file, O_RDONLY);
2618         if (fd < 0) {           /* failure */
2619                 quark_app_unique_name = g_quark_from_string("unknown");
2620                 memcpy(app_name, "unknown", strlen("unknown") + 1);
2621
2622                 SECURE_LOGE("Caution!! app_pid(%d) seems to be "
2623                 "killed, so we failed to get proc file(%s)\n", pid, proc_file);
2624                 *return_code = -1;
2625                 /* -1 means that system failed internally. */
2626                 return true;
2627         } else {
2628                 ret = read(fd, process_name, 512);
2629                 if (ret <0 ) {
2630                         *return_code = -1;
2631                         /* -1 means that system failed internally. */
2632                         close(fd);
2633                         return true;
2634                 }
2635                 close(fd);
2636                 while (process_name[i] != '\0') {
2637                         if (process_name[i] == ' ') {
2638                                 process_name[i] = '\0';
2639                                 break;
2640                         }
2641                         i++;
2642                 }
2643                 /* if (readlink(proc_file, process_name, 256)!=-1) */
2644                 /*success */
2645
2646                 word = strtok_r(process_name, "/", &proc_name_ptr);
2647                 while (word != NULL) {
2648                         memset(app_name, 0, 256);
2649                         snprintf(app_name, 256, "%s", word);
2650                         word = strtok_r(NULL, "/", &proc_name_ptr);
2651                 }
2652                 quark_app_unique_name = g_quark_from_string(app_name);
2653         }
2654
2655         SECURE_LOGD("called for  app(pid:%d, name=%s)\n",
2656                             pid, app_name);
2657
2658         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2659              gs_iter = g_slist_next(gs_iter)) {
2660                 entry = gs_iter->data;
2661                 if (strcmp
2662                     (app_name,
2663                      g_quark_to_string(entry->quark_app_unique_name)) == 0
2664                     && strcmp(app_name, "unknown") != 0) {
2665                         g_array_append_val(garray, entry->alarm_id);
2666                 }
2667         }
2668
2669         *num_of_ids = index;
2670
2671         *arr = garray;
2672
2673         return true;
2674 }
2675
2676 gboolean alarm_manager_alarm_get_appsvc_info(void *pObject, int pid, alarm_id_t alarm_id,
2677                                 char *e_cookie, gchar **b_data, int *return_code)
2678 {
2679         bool found = false;
2680
2681         GSList *gs_iter = NULL;
2682         __alarm_info_t *entry = NULL;
2683
2684         guchar *cookie = NULL;
2685         gsize size;
2686         int retval = 0;
2687
2688         SECURE_LOGD("called for  pid(%d) and alarm_id(%d)\n", pid, alarm_id);
2689
2690         cookie = g_base64_decode(e_cookie, &size);
2691         if (NULL == cookie)
2692         {
2693                 if (return_code)
2694                         *return_code = ERR_ALARM_SYSTEM_FAIL;
2695                 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
2696                 return true;
2697         }
2698
2699         retval = security_server_check_privilege_by_cookie((const char *)cookie, "alarm-server::alarm", "r");
2700         if (retval < 0) {
2701                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
2702                         ALARM_MGR_EXCEPTION_PRINT("Read access has been denied by smack\n");
2703                 }
2704                 ALARM_MGR_EXCEPTION_PRINT("Error has occurred\n");
2705
2706                 if (return_code)
2707                         *return_code = ERR_ALARM_NO_PERMISSION;
2708
2709                 if (cookie)
2710                         g_free(cookie);
2711
2712                 return true;
2713         }
2714
2715         if (return_code)
2716                 *return_code = 0;
2717
2718         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2719              gs_iter = g_slist_next(gs_iter)) {
2720                 entry = gs_iter->data;
2721                 if (entry->alarm_id == alarm_id) {
2722                         found = true;
2723                         *b_data = g_strdup(g_quark_to_string(entry->quark_bundle));
2724                         break;
2725                 }
2726         }
2727
2728         if (found) {
2729                 if ( *b_data && strlen(*b_data) == 4 && strncmp(*b_data,"null",4) == 0){
2730                         ALARM_MGR_EXCEPTION_PRINT("Regular alarm,not svc alarm\n");
2731                         if (return_code)
2732                                 *return_code = ERR_ALARM_INVALID_TYPE;
2733                 }
2734         } else {
2735                 if (return_code)
2736                         *return_code = ERR_ALARM_INVALID_ID;
2737         }
2738
2739         if (cookie)
2740                 g_free(cookie);
2741
2742         return true;
2743 }
2744
2745 gboolean alarm_manager_alarm_get_info(void *pObject, int pid,
2746                                       alarm_id_t alarm_id, int *start_year,
2747                                       int *start_month, int *start_day,
2748                                       int *start_hour, int *start_min,
2749                                       int *start_sec, int *end_year,
2750                                       int *end_month, int *end_day,
2751                                       int *mode_day_of_week, int *mode_repeat,
2752                                       int *alarm_type, int *reserved_info,
2753                                       int *return_code)
2754 {
2755         SECURE_LOGD("called for pid(%d) and alarm_id(%d)\n", pid, alarm_id);
2756
2757         GSList *gs_iter = NULL;
2758         __alarm_info_t *entry = NULL;
2759
2760         alarm_info_t *alarm_info = NULL;
2761         *return_code = 0;
2762
2763         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2764              gs_iter = g_slist_next(gs_iter)) {
2765                 entry = gs_iter->data;
2766                 if (entry->alarm_id == alarm_id) {
2767                         alarm_info = &(entry->alarm_info);
2768                         break;
2769                 }
2770         }
2771
2772         if (alarm_info == NULL)
2773         {
2774                 ALARM_MGR_EXCEPTION_PRINT("alarm id(%d) was not found\n", alarm_id);
2775                 *return_code = ERR_ALARM_INVALID_ID;
2776         } else {
2777                 ALARM_MGR_LOG_PRINT("alarm was found\n");
2778                 *start_year = alarm_info->start.year;
2779                 *start_month = alarm_info->start.month;
2780                 *start_day = alarm_info->start.day;
2781                 *start_hour = alarm_info->start.hour;
2782                 *start_min = alarm_info->start.min;
2783                 *start_sec = alarm_info->start.sec;
2784
2785                 *end_year = alarm_info->end.year;
2786                 *end_year = alarm_info->end.month;
2787                 *end_year = alarm_info->end.day;
2788
2789                 *mode_day_of_week = alarm_info->mode.u_interval.day_of_week;
2790                 *mode_repeat = alarm_info->mode.repeat;
2791
2792                 *alarm_type = alarm_info->alarm_type;
2793                 *reserved_info = alarm_info->reserved_info;
2794
2795                 *return_code = 0;
2796         }
2797         return true;
2798 }
2799
2800 gboolean alarm_manager_alarm_get_next_duetime(void *pObject, int pid,
2801                                       alarm_id_t alarm_id, time_t* duetime,
2802                                         int *return_code)
2803 {
2804         SECURE_LOGD("called for pid(%d) and alarm_id(%d)\n", pid, alarm_id);
2805
2806         GSList *gs_iter = NULL;
2807         __alarm_info_t *entry = NULL;
2808         __alarm_info_t *find_item = NULL;
2809
2810         *return_code = 0;
2811
2812         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2813              gs_iter = g_slist_next(gs_iter)) {
2814                 entry = gs_iter->data;
2815                 if (entry->alarm_id == alarm_id) {
2816                         find_item = entry;
2817                         break;
2818                 }
2819         }
2820
2821         if (find_item == NULL)
2822         {
2823                 ALARM_MGR_EXCEPTION_PRINT("alarm id(%d) was not found\n", alarm_id);
2824                 *return_code = ERR_ALARM_INVALID_ID;
2825         } else {
2826                 ALARM_MGR_LOG_PRINT("alarm was found\n");
2827                 *duetime = _alarm_next_duetime(find_item);
2828                 *return_code = 0;
2829         }
2830         return true;
2831 }
2832
2833 #include "alarm-skeleton.h"
2834
2835 typedef struct AlarmManagerObject AlarmManagerObject;
2836 typedef struct AlarmManagerObjectClass AlarmManagerObjectClass;
2837 GType Server_Object_get_type(void);
2838 struct AlarmManagerObject {
2839         GObject parent;
2840 };
2841 struct AlarmManagerObjectClass {
2842         GObjectClass parent;
2843 };
2844
2845 #define DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT 0
2846
2847 #define ALARM_MANAGER_TYPE_OBJECT              (Server_Object_get_type())
2848 #define ALARM_MANAGER_OBJECT(object)           (G_TYPE_CHECK_INSTANCE_CAST \
2849 ((object), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObject))
2850 #define ALARM_MANAGER_OBJECT_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST \
2851 ((klass), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObjectClass))
2852 #define ALARM_MANAGER_IS_OBJECT(object)        (G_TYPE_CHECK_INSTANCE_TYPE \
2853 ((object), ALARM_MANAGER_TYPE_OBJECT))
2854 #define ALARM_MANAGER_IS_OBJECT_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE \
2855 ((klass), ALARM_MANAGER_TYPE_OBJECT))
2856 #define ALARM_MANAGER_OBJECT_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS \
2857 ((obj), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObjectClass))
2858 G_DEFINE_TYPE(AlarmManagerObject, Server_Object, G_TYPE_OBJECT)
2859 static void Server_Object_init(AlarmManagerObject * obj)
2860 {
2861         ;
2862 }
2863
2864 static void Server_Object_class_init(AlarmManagerObjectClass *klass)
2865 {
2866         ;
2867 }
2868
2869 static void __initialize_timer()
2870 {
2871         struct sigaction sig_timer;
2872         sigemptyset(&sig_timer.sa_mask);
2873         sig_timer.sa_flags = SA_SIGINFO;
2874         sig_timer.sa_sigaction = (void *)__alarm_handler;
2875         sigaction(SIG_TIMER, &sig_timer, NULL);
2876
2877         alarm_context.timer = _alarm_create_timer();
2878
2879 }
2880
2881 static void __initialize_alarm_list()
2882 {
2883
2884         alarm_context.alarms = NULL;
2885         alarm_context.c_due_time = -1;
2886
2887         _load_alarms_from_registry();
2888
2889         __rtc_set();    /*Set RTC1 Alarm with alarm due time for alarm-manager initialization*/
2890
2891         /*alarm boot */
2892 #ifdef __ALARM_BOOT
2893         /*alarm boot */
2894         if (enable_power_on_alarm) {
2895                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
2896                  * platform with app-server. because __alarm_power_on(..) fuction don't
2897                  * use first parameter internally, we set this value to 0(zero)
2898                  */
2899                 __alarm_power_on(0, enable_power_on_alarm, NULL);
2900         }
2901 #endif
2902 }
2903
2904 static void __initialize_scheduled_alarm_list()
2905 {
2906         _init_scheduled_alarm_list();
2907 }
2908
2909
2910 static void __hibernation_leave_callback()
2911 {
2912
2913         __initialize_scheduled_alarm_list();
2914
2915         __alarm_clean_list();
2916
2917         __initialize_alarm_list();
2918 }
2919
2920 static bool __initialize_noti()
2921 {
2922         /*system state change noti Ã³\B8\AE */
2923
2924         if (vconf_notify_key_changed
2925             (VCONFKEY_SETAPPL_TIMEZONE_ID, __on_time_zone_changed, NULL) < 0) {
2926                 ALARM_MGR_LOG_PRINT(
2927                         "Failed to add callback for time zone changing event\n");
2928         }
2929
2930         if (vconf_notify_key_changed
2931             (VCONFKEY_SYSTEM_TIMECHANGE_EXTERNAL, __on_system_time_external_changed, NULL) < 0) {
2932                 ALARM_MGR_LOG_PRINT(
2933                         "Failed to add callback for time external changing event\n");
2934         }
2935
2936         // If the caller or callee app is uninstalled, all registered alarms will be canceled.
2937         int event_type = PMINFO_CLIENT_STATUS_UNINSTALL;
2938         pkgmgrinfo_client* pc = pkgmgrinfo_client_new(PMINFO_LISTENING);
2939         pkgmgrinfo_client_set_status_type(pc, event_type);
2940         pkgmgrinfo_client_listen_status(pc, __on_app_uninstalled, NULL);
2941
2942         return true;
2943 }
2944
2945
2946 static DBusHandlerResult __alarm_server_filter(DBusConnection *connection,
2947                                                DBusMessage *message,
2948                                                void *user_data)
2949 {
2950
2951         if (dbus_message_is_signal
2952             (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
2953                 char *service;
2954                 char *old_owner;
2955                 char *new_owner;
2956                 GSList *entry;
2957                 __expired_alarm_t *expire_info;
2958
2959                 dbus_message_get_args(message,
2960                                       NULL,
2961                                       DBUS_TYPE_STRING, &service,
2962                                       DBUS_TYPE_STRING, &old_owner,
2963                                       DBUS_TYPE_STRING, &new_owner,
2964                                       DBUS_TYPE_INVALID);
2965
2966                 for (entry = g_expired_alarm_list; entry; entry = entry->next) {
2967                         if (entry->data) {
2968                                 expire_info = (__expired_alarm_t *) entry->data;
2969
2970                                 SECURE_LOGD("service_name(%s), service(%s)", expire_info->service_name, service);
2971
2972                                 if (strcmp(expire_info->service_name, service)
2973                                     == 0) {
2974                                         SECURE_LOGE(
2975                                         "__alarm_server_filter : "
2976                                              "service name(%s) alarm_id (%d)\n",
2977                                              expire_info->service_name,\
2978                                              expire_info->alarm_id);
2979
2980                                         __alarm_send_noti_to_application(
2981                                              expire_info->service_name,
2982                                              expire_info->alarm_id);
2983                                         g_expired_alarm_list =
2984                                             g_slist_remove(g_expired_alarm_list,
2985                                                            entry->data);
2986                                         free(expire_info);
2987                                 }
2988                         }
2989                 }
2990
2991                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2992         }
2993
2994         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2995 }
2996
2997 static bool __initialize_dbus()
2998 {
2999         GError *error = NULL;
3000         GObject *obj = NULL;
3001         DBusGConnection *connection = NULL;
3002         DBusError derror;
3003
3004         dbus_g_object_type_install_info(ALARM_MANAGER_TYPE_OBJECT,
3005                                         &dbus_glib_alarm_manager_object_info);
3006
3007         connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
3008         if (!connection) {
3009
3010                 ALARM_MGR_EXCEPTION_PRINT("dbus_g_bus_get failed\n");
3011                 return false;
3012         }
3013
3014         dbus_error_init(&derror);
3015
3016         dbus_bus_request_name(dbus_g_connection_get_connection(connection),
3017                                   "com.samsung.alarm.manager",
3018                                   DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &derror);
3019         if (dbus_error_is_set(&derror)) {       /* failure */
3020                 SECURE_LOGE("Failed to dbus_bus_request_name "
3021                 "(com.samsung.alarm.manager): %s\n", derror.message);
3022                 dbus_error_free(&derror);
3023                 return false;
3024         }
3025
3026         if (!(obj = g_object_new(ALARM_MANAGER_TYPE_OBJECT, NULL))) {
3027                 ALARM_MGR_EXCEPTION_PRINT("Could not allocate new object\n");
3028                 return false;
3029         }
3030
3031         dbus_g_connection_register_g_object(connection,
3032                                             "/com/samsung/alarm/manager",
3033                                             G_OBJECT(obj));
3034
3035         /*dbus_bus_add_match (dbus_g_connection_get_connection(connection),
3036            "type='signal',member='NameOwnerChanged'",NULL); */
3037
3038         dbus_bus_add_match(dbus_g_connection_get_connection(connection),
3039                            "type='signal',sender='" DBUS_SERVICE_DBUS
3040                            "',path='" DBUS_PATH_DBUS
3041                            "',interface='" DBUS_INTERFACE_DBUS
3042                            "',member='NameOwnerChanged'", NULL);
3043
3044         if (!dbus_connection_add_filter
3045             (dbus_g_connection_get_connection(connection),
3046              __alarm_server_filter, NULL, NULL)) {
3047                 ALARM_MGR_EXCEPTION_PRINT("add __expire_alarm_filter failed\n");
3048
3049                 return false;
3050         }
3051
3052         alarm_context.bus = connection;
3053         return true;
3054 }
3055
3056 #define ALARMMGR_DB_FILE "/opt/dbspace/.alarmmgr.db"
3057 sqlite3 *alarmmgr_db;
3058 #define QUERY_CREATE_TABLE_ALARMMGR "create table alarmmgr \
3059                                 (alarm_id integer primary key,\
3060                                                 start integer,\
3061                                                 end integer,\
3062                                                 pid integer,\
3063                                                 app_unique_name text,\
3064                                                 app_service_name text,\
3065                                                 app_service_name_mod text,\
3066                                                 bundle text, \
3067                                                 year integer,\
3068                                                 month integer,\
3069                                                 day integer,\
3070                                                 hour integer,\
3071                                                 min integer,\
3072                                                 sec integer,\
3073                                                 day_of_week integer,\
3074                                                 repeat integer,\
3075                                                 alarm_type integer,\
3076                                                 reserved_info integer,\
3077                                                 dst_service_name text, \
3078                                                 dst_service_name_mod text \
3079                                                 )"
3080
3081 static bool __initialize_db()
3082 {
3083         char *error_message = NULL;
3084         int ret;
3085
3086         if (access("/opt/dbspace/.alarmmgr.db", F_OK) == 0) {
3087                 ret =
3088                     db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db,
3089                                  DB_UTIL_REGISTER_HOOK_METHOD);
3090
3091                 if (ret != SQLITE_OK) {
3092                         ALARM_MGR_EXCEPTION_PRINT(
3093                             "====>>>> connect menu_db [%s] failed!\n",
3094                              ALARMMGR_DB_FILE);
3095                         return false;
3096                 }
3097
3098                 return true;
3099         }
3100
3101         ret =
3102             db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db,
3103                          DB_UTIL_REGISTER_HOOK_METHOD);
3104
3105         if (ret != SQLITE_OK) {
3106                 ALARM_MGR_EXCEPTION_PRINT(
3107                     "====>>>> connect menu_db [%s] failed!\n",
3108                      ALARMMGR_DB_FILE);
3109                 return false;
3110         }
3111
3112         if (SQLITE_OK !=
3113             sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL,\
3114                          &error_message)) {
3115                 ALARM_MGR_EXCEPTION_PRINT("Don't execute query = %s, "
3116                 "error message = %s\n", QUERY_CREATE_TABLE_ALARMMGR,
3117                                           error_message);
3118                 return false;
3119         }
3120
3121         return true;
3122 }
3123
3124 static void __initialize()
3125 {
3126
3127         g_type_init();
3128 #ifdef __ALARM_BOOT
3129         FILE *fp;
3130         char temp[2];
3131         int size;
3132
3133         fp = fopen("/proc/alarm_boot", "r");
3134         if (fp == NULL)
3135                 alarm_boot = 0;
3136         else {
3137
3138                 size = fread(&temp, 1, 1, fp);
3139                 if (size != 1)
3140                         alarm_boot = 0;
3141                 else {
3142                         temp[1] = 0;
3143                         alarm_boot = atoi(temp);
3144                 }
3145                 fclose(fp);
3146         }
3147 #endif
3148
3149         __initialize_timer();
3150         if (__initialize_dbus() == false) {     /* because dbus's initialize
3151                                         failed, we cannot continue any more. */
3152                 ALARM_MGR_EXCEPTION_PRINT("because __initialize_dbus failed, "
3153                                           "alarm-server cannot be runned.\n");
3154                 exit(1);
3155         }
3156         __initialize_scheduled_alarm_list();
3157         __initialize_db();
3158         __initialize_alarm_list();
3159         __initialize_noti();
3160 #ifdef __APP_SYNC
3161         _sync_scheduler_init();
3162 #endif
3163
3164 }
3165
3166 #ifdef __ALARM_BOOT
3167 static bool __check_false_alarm()
3168 {
3169         time_t current_time;
3170         time_t interval;
3171
3172         time(&current_time);
3173
3174         interval = ab_due_time - current_time;
3175
3176         ALARM_MGR_LOG_PRINT("due_time : %d / current_time %d\n", \
3177                             alarm_context.c_due_time, current_time);
3178
3179         if (interval > 0 && interval <= 30) {
3180                 return true;
3181         } else if (!poweron_alarm_expired) {
3182                 /* originally, first arguement's value was 121(app_id) which means
3183                  * alarm_booting ui application.and this application's dbus-service
3184                  * name had a com.samsung.alarmboot.ui in a platform with app-server.
3185                  * so we set "com.samsung.alarmboot.ui.ALARM" instead of 121.
3186                  */
3187                 __alarm_send_noti_to_application(
3188                         WAKEUP_ALARMBOOTING_APP_ID, -1);
3189                 return false;
3190         }
3191
3192         return true;
3193 }
3194 #endif
3195
3196 int main()
3197 {
3198         GMainLoop *mainloop = NULL;
3199
3200         ALARM_MGR_LOG_PRINT("Enter main loop\n");
3201
3202         mainloop = g_main_loop_new(NULL, FALSE);
3203
3204         __initialize();
3205
3206 #ifdef __ALARM_BOOT
3207         if (alarm_boot){
3208                 __check_false_alarm();
3209         }
3210 #endif
3211
3212         g_main_loop_run(mainloop);
3213
3214         return 0;
3215 }