70cfe46740ccc304d96c3bfa47a800ca56dcce4e
[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 #include <glib/gmacros.h>
39
40 /*#include "global-gconf.h"*/
41 #include"alarm.h"
42 #include"alarm-internal.h"
43
44 /*#include"limo-event-delivery.h"*/
45
46 #include <aul.h>
47 #include <bundle.h>
48 #include <heynoti.h>
49 #include <security-server.h>
50 #include <db-util.h>
51 #include <vconf.h>
52 #include <vconf-keys.h>
53
54 #include <ITapiMisc.h>
55
56 #define SIG_TIMER 0x32
57 #define WAKEUP_ALARM_APP_ID       "org.tizen.alarm.ALARM"
58         /* alarm ui application's alarm's dbus_service name instead of 21
59            (alarm application's app_id) value */
60 #define WAKEUP_ALARMBOOTING_APP_ID      "org.tizen.alarmboot.ui.ALARM"
61 /*alrmbooting ui application's dbus_service name instead of 121(alarmbooting
62         application's app_id) value */
63 /*
64 #include "tapi_misc_ext.h"
65 #include "TelMisc.h"
66 #include "tapi_misc_data.h"
67 #include "tapi_power.h"
68 */
69
70 #include "pmapi.h"
71
72 __alarm_server_context_t alarm_context;
73 bool g_dummy_timer_is_set = FALSE;
74
75 GSList *g_scheduled_alarm_list = NULL;
76
77 GSList *g_expired_alarm_list = NULL;
78
79 #ifdef __ALARM_BOOT
80 bool enable_power_on_alarm;
81 bool alarm_boot;
82 time_t ab_due_time = -1;
83 bool poweron_alarm_expired = false;
84 #endif
85
86 /*      2008. 6. 3 sewook7.park
87        When the alarm becoms sleeping mode, alarm timer is not expired.
88        So using RTC, phone is awaken before alarm rings.
89 */
90 #define __WAKEUP_USING_RTC__
91 #ifdef __WAKEUP_USING_RTC__
92 #include<errno.h>
93 #include <linux/rtc.h>
94 #include <sys/ioctl.h>
95 #include <fcntl.h>
96
97 static const char default_rtc[] = "/dev/rtc1";
98 static const char power_rtc[] = "/dev/rtc0";
99
100 #endif                          /*__WAKEUP_USING_RTC__*/
101
102 static bool __alarm_add_to_list(__alarm_info_t *__alarm_info,
103                                 alarm_id_t *alarm_id);
104 static bool __alarm_update_in_list(int pid, alarm_id_t alarm_id,
105                                    __alarm_info_t *__alarm_info,
106                                    int *error_code);
107 static bool __alarm_remove_from_list(int pid, alarm_id_t alarm_id,
108                                      int *error_code);
109 static bool __alarm_set_start_and_end_time(alarm_info_t *alarm_info,
110                                            __alarm_info_t *__alarm_info);
111 static bool __alarm_update_due_time_of_all_items_in_list(double diff_time);
112 static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
113                            int pid, char *app_service_name, char *app_service_name_mod,
114                            const char *dst_service_name, const char *dst_service_name_mod, int *error_code);
115 static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
116                            int pid, char *bundle_data, int *error_code);
117
118 static bool __alarm_delete(int pid, alarm_id_t alarm_id, int *error_code);
119 static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
120                            alarm_info_t *alarm_info, int *error_code);
121 static bool __alarm_power_on(int app_id, bool on_off, int *error_code);
122 static bool __alarm_power_off(int app_id, int *error_code);
123 static bool __alarm_check_next_duetime(int app_id, int *error_code);
124 static void __alarm_send_noti_to_application(const char *app_service_name,
125                                              alarm_id_t alarm_id);
126 static void __alarm_expired();
127 static gboolean __alarm_handler_idle();
128 static void __alarm_handler(int sigNum, siginfo_t *pSigInfo, void *pUContext);
129 static void __clean_registry();
130 static bool __alarm_manager_reset();
131 static void __on_system_time_changed(keynode_t *node, void *data);
132 static void __initialize_timer();
133 static void __initialize_alarm_list();
134 static void __initialize_scheduled_alarm_lsit();
135 static void __hibernation_leave_callback();
136 static bool __initialize_noti();
137
138 static bool __initialize_dbus();
139 static bool __initialize_db();
140 static void __initialize();
141 static bool __check_false_alarm();
142 static DBusHandlerResult __alarm_server_filter(DBusConnection *connection,
143                                                DBusMessage *message,
144                                                void *user_data);
145
146 static void __rtc_set()
147 {
148 #ifdef __WAKEUP_USING_RTC__
149
150         const char *rtc = default_rtc;
151         int fd = 0;
152         struct rtc_time rtc_tm;
153         struct rtc_wkalrm rtc_wk;
154         struct tm due_tm;
155
156 #ifdef _SIMUL                   /*if build is simulator, we don't need to set
157                                    RTC because RTC does not work in simulator.*/
158         ALARM_MGR_EXCEPTION_PRINT("because it is simulator's mode, "
159                                   "we don't set RTC.\n");
160         return;
161 #endif
162
163         fd = open(rtc, O_RDONLY);
164         if (fd == -1) {
165                 ALARM_MGR_EXCEPTION_PRINT("RTC open failed.\n");
166                 return;
167         }
168
169         /* Read the RTC time/date */
170         int retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
171         if (retval == -1) {
172                 ALARM_MGR_EXCEPTION_PRINT("RTC_RD_TIME ioctl failed");
173                 close(fd);
174                 return;
175         }
176
177         ALARM_MGR_LOG_PRINT("\n\nCurrent RTC date/time is %d-%d-%d, "
178                 "%02d:%02d:%02d.\n", rtc_tm.tm_mday, rtc_tm.tm_mon + 1, 
179                 rtc_tm.tm_year + 1900, rtc_tm.tm_hour, rtc_tm.tm_min, 
180                 rtc_tm.tm_sec);
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                 time_t due_time = alarm_context.c_due_time - 1;
187                 gmtime_r(&due_time, &due_tm);
188
189                 rtc_tm.tm_mday = due_tm.tm_mday;
190                 rtc_tm.tm_mon = due_tm.tm_mon;
191                 rtc_tm.tm_year = due_tm.tm_year;
192                 rtc_tm.tm_hour = due_tm.tm_hour;
193                 rtc_tm.tm_min = due_tm.tm_min;
194                 rtc_tm.tm_sec = due_tm.tm_sec;
195                 memcpy(&rtc_wk.time, &rtc_tm, sizeof(rtc_tm));
196                 rtc_wk.enabled = 1;
197                 rtc_wk.pending = 0;
198
199                 ALARM_MGR_LOG_PRINT("\n\nSetted RTC Alarm date/time is "
200                         "%d-%d-%d, %02d:%02d:%02d.\n", rtc_tm.tm_mday, 
201                         rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900, 
202                         rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
203
204                 retval = ioctl(fd, RTC_WKALM_SET, &rtc_wk);
205                 if (retval == -1) {
206                         if (errno == ENOTTY) {
207                                 ALARM_MGR_EXCEPTION_PRINT("\nAlarm IRQs not"
208                                                           "supported.\n");
209                         }
210                         ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
211                         close(fd);
212                         return;
213                 }
214                 ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is setted");
215
216                 /* Enable alarm interrupts */
217                 retval = ioctl(fd, RTC_AIE_ON, 0);
218                 if (retval == -1) {
219                         ALARM_MGR_EXCEPTION_PRINT("RTC_AIE_ON ioctl failed");
220                         close(fd);
221                         return;
222                 }
223                 ALARM_MGR_LOG_PRINT("[alarm-server]RTC alarm is on");
224         } else
225                 ALARM_MGR_LOG_PRINT("[alarm-server]alarm_context.c_due_time is"
226                                     "less than 10 sec. RTC alarm does not need to be set\n");
227
228         close(fd);
229
230 #endif                          /* __WAKEUP_USING_RTC__ */
231 }
232
233 int _set_rtc_time(time_t _time)
234 {
235         int fd0 = 0;
236         int fd1 = 0;
237         int retval0 = 0;
238         int retval1 = 0;
239         struct rtc_time rtc_tm;
240         const char *rtc0 = power_rtc;
241         const char *rtc1 = default_rtc;
242         struct tm *_tm;
243         struct tm time_r;
244         int tapi_ret;
245         tapi_misc_time_zone_info_type info = { 0, };
246
247         fd0 = open(rtc0, O_RDONLY);
248         fd1 = open(rtc1, O_RDONLY);
249
250         if (fd0 == -1) {
251                 ALARM_MGR_LOG_PRINT("error to open /dev/rtc0.");
252                 perror("\t");
253         }
254
255         if (fd1 == -1) {
256                 ALARM_MGR_LOG_PRINT("error to open /dev/rtc1.");
257                 perror("\t");
258         }
259
260         memset(&rtc_tm, 0, sizeof(struct rtc_time));
261
262
263         _tm = gmtime_r(&_time, &time_r);
264
265         /* Write the RTC time/date 2008:05:21 19:20:00 */
266
267
268         rtc_tm.tm_mday = time_r.tm_mday;
269         rtc_tm.tm_mon = time_r.tm_mon;
270         rtc_tm.tm_year = time_r.tm_year;
271         rtc_tm.tm_hour = time_r.tm_hour;
272         rtc_tm.tm_min = time_r.tm_min;
273         rtc_tm.tm_sec = 0;
274         
275
276         retval0 = ioctl(fd0, RTC_SET_TIME, &rtc_tm);
277
278         if (retval0 == -1) {
279                 close(fd0);
280                 ALARM_MGR_LOG_PRINT("error to ioctl fd0.");
281                 perror("\t");
282         }
283         close(fd0);
284
285         retval1 = ioctl(fd1, RTC_SET_TIME, &rtc_tm);
286
287         if (retval1 == -1) {
288                 close(fd1);
289                 ALARM_MGR_LOG_PRINT("error to ioctl fd1.");
290                 perror("\t");
291         }
292         close(fd1);
293
294         /* set modem */
295         info.time_zone_valid = TAPI_MISC_NITZ_RTC_BKUP_PHONE;
296         info.year = time_r.tm_year;
297         info.month = time_r.tm_mon;
298         info.day = time_r.tm_mday;
299         info.hour = time_r.tm_hour;
300         info.minute = time_r.tm_min;
301         info.second = 0;
302         ALARM_MGR_LOG_PRINT("***********Set timezone info*********\n");
303         ALARM_MGR_LOG_PRINT("***********info.year:%d\t*********\n", info.year);
304         ALARM_MGR_LOG_PRINT("***********info.month:%d\t*********\n",
305                             info.month);
306         ALARM_MGR_LOG_PRINT("***********info.day:%d\t*********\n", info.day);
307         ALARM_MGR_LOG_PRINT("***********info.hour:%d\t*********\n", info.hour);
308         ALARM_MGR_LOG_PRINT("***********info.minute:%d\t*********\n",
309                             info.minute);
310         tapi_ret = tel_set_misc_timezone_info(&info);
311
312         return 1;
313 }
314
315 bool __alarm_clean_list()
316 {
317         GSList *iter = NULL;
318
319         for (iter = alarm_context.alarms; iter != NULL;
320              iter = g_slist_next(iter)) {
321                 free(iter->data);
322         }
323
324         g_slist_free(alarm_context.alarms);
325         return true;
326 }
327
328 static bool __alarm_add_to_list(__alarm_info_t *__alarm_info,
329                                 alarm_id_t *alarm_id)
330 {
331
332         bool unique_id = false;
333         alarm_info_t *alarm_info = &__alarm_info->alarm_info;
334         __alarm_info_t *entry = NULL;
335
336         GSList *iter = NULL;
337
338         /* FIXME: alarm id must be unique. */
339         __alarm_info->alarm_id = (int)(void *)__alarm_info;
340         ALARM_MGR_LOG_PRINT("__alarm_info->alarm_id is %d", \
341                             __alarm_info->alarm_id);
342
343         while (unique_id == false) {
344                 unique_id = true;
345
346                 for (iter = alarm_context.alarms; iter != NULL;
347                      iter = g_slist_next(iter)) {
348                         entry = iter->data;
349                         if (entry->alarm_id == __alarm_info->alarm_id) {
350                                 __alarm_info->alarm_id++;
351                                 unique_id = false;
352                         }
353                 }
354
355         }
356
357         /* list alarms */
358         ALARM_MGR_LOG_PRINT("[alarm-server]: before add\n");
359         for (iter = alarm_context.alarms; iter != NULL;
360              iter = g_slist_next(iter)) {
361                 entry = iter->data;
362                 /*ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n", 
363                    entry->alarm_id); */
364         }
365
366         alarm_context.alarms =
367             g_slist_append(alarm_context.alarms, __alarm_info);
368         /*list alarms */
369         ALARM_MGR_LOG_PRINT("[alarm-server]: after add\n");
370         for (iter = alarm_context.alarms; iter != NULL;
371              iter = g_slist_next(iter)) {
372                 entry = iter->data;
373                 ALARM_MGR_LOG_PRINT("[alarm-server]: alarm_id(%d)\n",
374                                     entry->alarm_id);
375         }
376
377         if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
378                 _save_alarms(__alarm_info);
379         }
380
381         *alarm_id = __alarm_info->alarm_id;
382
383         return true;
384 }
385
386 static bool __alarm_update_in_list(int pid, alarm_id_t alarm_id,
387                                    __alarm_info_t *__alarm_info,
388                                    int *error_code)
389 {
390         bool found = false;
391         alarm_info_t *alarm_info = &__alarm_info->alarm_info;
392         GSList *iter = NULL;
393         __alarm_info_t *entry = NULL;
394
395         for (iter = alarm_context.alarms; iter != NULL;
396              iter = g_slist_next(iter)) {
397                 entry = iter->data;
398                 if (entry->alarm_id == alarm_id) {
399
400                         found = true;
401                         __alarm_info->quark_app_unique_name =
402                             entry->quark_app_unique_name;
403                         __alarm_info->quark_dst_service_name =
404                             entry->quark_dst_service_name;
405                         memcpy(entry, __alarm_info, sizeof(__alarm_info_t));
406
407                         break;
408                 }
409         }
410
411         if (!found) {
412                 if (error_code)
413                         *error_code = ERR_ALARM_INVALID_ID;
414                 return false;
415         }
416
417         if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
418                 _update_alarms(__alarm_info);
419         }
420
421         return true;
422 }
423
424 static bool __alarm_remove_from_list(int pid, alarm_id_t alarm_id,
425                                      int *error_code)
426 {
427         bool found = false;
428
429         alarm_info_t *alarm_info = NULL;
430
431         GSList *iter = NULL;
432         __alarm_info_t *entry = NULL;
433
434         /*list alarms */
435         ALARM_MGR_LOG_PRINT("[alarm-server]: before del : alarm id(%d)\n",
436                             alarm_id);
437
438         for (iter = alarm_context.alarms; iter != NULL;
439              iter = g_slist_next(iter)) {
440                 entry = iter->data;
441                 if (entry->alarm_id == alarm_id) {
442                         alarm_info = &entry->alarm_info;
443
444                         ALARM_MGR_LOG_PRINT("[alarm-server]: "
445                                             "__alarm_remove_from_list : alarm id(%d)\n",
446                                             entry->alarm_id);
447
448                         if (!(alarm_info->alarm_type & ALARM_TYPE_VOLATILE)) {
449                                 _delete_alarms(alarm_id);
450                         }
451
452                         alarm_context.alarms =
453                             g_slist_remove(alarm_context.alarms, iter->data);
454                         found = true;
455                         break;
456                 }
457
458         }
459
460         ALARM_MGR_LOG_PRINT("[alarm-server]: after del\n");
461
462         if (!found) {
463                 if (error_code)
464                         *error_code = ERR_ALARM_INVALID_ID;
465                 return false;
466         }
467
468         return true;
469 }
470
471 static bool __alarm_set_start_and_end_time(alarm_info_t *alarm_info,
472                                            __alarm_info_t *__alarm_info)
473 {
474         alarm_date_t *start = &alarm_info->start;
475         alarm_date_t *end = &alarm_info->end;
476
477         struct tm alarm_tm = { 0, };
478
479         if (start->year != 0) {
480                 alarm_tm.tm_year = start->year - 1900;
481                 alarm_tm.tm_mon = start->month - 1;
482                 alarm_tm.tm_mday = start->day;
483
484                 alarm_tm.tm_hour = 0;
485                 alarm_tm.tm_min = 0;
486                 alarm_tm.tm_sec = 0;
487
488                 __alarm_info->start = mktime(&alarm_tm);
489         } else {
490                 __alarm_info->start = 0;
491         }
492
493         if (end->year != 0) {
494                 alarm_tm.tm_year = end->year - 1900;
495                 alarm_tm.tm_mon = end->month - 1;
496                 alarm_tm.tm_mday = end->day;
497
498                 alarm_tm.tm_hour = 23;
499                 alarm_tm.tm_min = 59;
500                 alarm_tm.tm_sec = 59;
501
502                 __alarm_info->end = mktime(&alarm_tm);
503         } else {
504                 __alarm_info->end = 0;
505         }
506
507         return true;
508 }
509
510 /*
511 static bool alarm_get_tz_info(int *gmt_idx, int *dst)
512 {
513         GConfValue *value1 = NULL;
514         GConfValue *value2 = NULL;
515         GConfClient* gConfClient = NULL;
516         GError* err = NULL;             
517         
518         gConfClient = gconf_client_get_default();
519
520         if(gConfClient) {
521                 value1 = gconf_client_get(gConfClient, SETTINGS_CLOCKTIMEZONE, 
522                                                                         &err);
523                 if (err) {
524                         ALARM_MGR_LOG_PRINT("__on_system_time_changed:
525                         gconf_client_get() failed:
526                         error:[%s]\n", err->message);
527                         g_error_free(err);
528                         err = NULL;
529                 }
530                 *gmt_idx = gconf_value_get_int(value1);
531                 ALARM_MGR_LOG_PRINT("gconf return gmt_idx =%d\n ", *gmt_idx);
532         
533                 value2 = gconf_client_get(gConfClient, 
534                         SETTINGS_DAYLIGHTSTATUS, &err);
535                 if (err) {
536                         ALARM_MGR_LOG_PRINT("__on_system_time_changed:
537                 gconf_client_get() failed: error:[%s]\n", err->message);
538                 g_error_free(err);
539                 err = NULL;
540         }
541         
542         *dst = gconf_value_get_int(value2);
543         ALARM_MGR_LOG_PRINT("gconf return dst =%d\n ", *dst);
544         
545         if(gConfClient != NULL) {
546                 g_object_unref(gConfClient);
547                 gConfClient = NULL;
548                 }
549         }
550         else
551                 ALARM_MGR_LOG_PRINT("check the gconf setting failed!!!!! \n ");
552         
553         if(value1) {
554                 gconf_value_free(value1);
555                 value1 = NULL;
556         }
557         if(value2) {
558                 gconf_value_free(value2);
559                 value2 = NULL;
560         }
561
562         return true;
563 }
564 */
565
566 static bool __alarm_update_due_time_of_all_items_in_list(double diff_time)
567 {
568         time_t current_time;
569         time_t min_time = -1;
570         time_t due_time = 0;
571         GSList *iter = NULL;
572         __alarm_info_t *entry = NULL;
573         struct tm *p_time = NULL ;
574         struct tm due_time_result ;
575         struct tm fixed_time ;
576
577         for (iter = alarm_context.alarms; iter != NULL;
578              iter = g_slist_next(iter)) {
579                 entry = iter->data;
580                 alarm_info_t *alarm_info = &(entry->alarm_info);
581                 if (alarm_info->alarm_type & ALARM_TYPE_RELATIVE) {
582                         /*diff_time ÃƒÂ³Â¸Â® */
583
584                         entry->due_time += diff_time;
585
586                         alarm_date_t *start = &alarm_info->start; /**< start 
587                                                         time of the alarm */
588                         alarm_date_t *end = &alarm_info->end;;
589                                                 /**< end time of the alarm */
590
591                         tzset();
592                         p_time = localtime_r(&entry->due_time, &due_time_result);
593                         if (p_time != NULL) {
594                                 start->year = p_time->tm_year + 1900;
595                                 start->month = p_time->tm_mon + 1;
596                                 start->day = p_time->tm_mday;
597                                 start->hour = p_time->tm_hour;
598                                 start->min = p_time->tm_min;
599                                 start->sec = p_time->tm_sec;
600
601                                 end->year = p_time->tm_year + 1900;
602                                 end->month = p_time->tm_mon + 1;
603                                 end->day = p_time->tm_mday;
604
605                                 
606                                 memset(&fixed_time, 0, sizeof(fixed_time));
607                                 fixed_time.tm_year = p_time->tm_year;
608                                 fixed_time.tm_mon = p_time->tm_mon;
609                                 fixed_time.tm_mday = p_time->tm_mday;
610                                 fixed_time.tm_hour = 0;
611                                 fixed_time.tm_min = 0;
612                                 fixed_time.tm_sec = 0;
613                         }
614                         entry->start = mktime(&fixed_time);
615
616                         fixed_time.tm_hour = 23;
617                         fixed_time.tm_min = 59;
618                         fixed_time.tm_sec = 59;
619
620                         entry->end = mktime(&fixed_time);
621
622                         ALARM_MGR_LOG_PRINT("alarm_info->alarm_type is "
623                                             "ALARM_TYPE_RELATIVE\n");
624
625                         _update_alarms(entry);
626                 }
627
628                 _alarm_next_duetime(entry);
629                 ALARM_MGR_LOG_PRINT("entry->due_time is %d\n", entry->due_time);
630         }
631
632         time(&current_time);
633
634         for (iter = alarm_context.alarms; iter != NULL;
635              iter = g_slist_next(iter)) {
636                 entry = iter->data;
637                 due_time = entry->due_time;
638
639                 double interval = 0;
640
641                 ALARM_MGR_LOG_PRINT("alarm[%d] with duetime(%u) at "
642                 "current(%u)\n", entry->alarm_id, due_time, current_time);
643                 if (due_time == 0) {    /* 0 means this alarm has been 
644                                            disabled */
645                         continue;
646                 }
647
648                 interval = difftime(due_time, current_time);
649
650                 if (interval <= 0) {
651                         ALARM_MGR_LOG_PRINT("this may be error.. alarm[%d]\n", \
652                                             entry->alarm_id);
653                         continue;
654                 }
655
656                 interval = difftime(due_time, min_time);
657
658                 if ((interval < 0) || min_time == -1) {
659                         min_time = due_time;
660                 }
661
662         }
663
664         alarm_context.c_due_time = min_time;
665
666         return true;
667 }
668
669 static bool __alarm_create_appsvc(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
670                            int pid,char *bundle_data, int *error_code){
671
672         time_t current_time;
673         time_t due_time;
674         struct tm ts_ret;
675         char due_time_r[100] = { 0 };
676         char proc_file[512] = { 0 };
677         char process_name[512] = { 0 };
678         char app_name[512] = { 0 };
679         char *word = NULL;
680         char *proc_name_ptr = NULL;
681         int fd = 0;
682         int ret = 0;
683         int i = 0;
684
685         __alarm_info_t *__alarm_info = NULL;
686
687         __alarm_info = malloc(sizeof(__alarm_info_t));
688         if (__alarm_info == NULL) {
689                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, malloc "
690                                           "failed. it seems to be OOM\n", pid);
691                 *error_code = -1;       /* -1 means that system 
692                                            failed internally. */
693                 return false;
694         }
695         __alarm_info->pid = pid;
696         __alarm_info->alarm_id = -1;
697
698
699         /* we should consider to check whether  pid is running or Not
700          */
701         memset(process_name, '\0', 512);
702         memset(proc_file, '\0', 512);
703         snprintf(proc_file, 512, "/proc/%d/cmdline", pid);
704
705         fd = open(proc_file, O_RDONLY);
706         if (fd > 0) {
707                 ret = read(fd, process_name, 512);
708                 close(fd);
709                 if (ret <=0)
710                 {
711                         ALARM_MGR_EXCEPTION_PRINT("Unable to get application name\n");
712                         *error_code = -1;
713                         free(__alarm_info);
714                         return false;
715                 }
716                 while (process_name[i] != '\0') {
717                         if (process_name[i] == ' ') {
718                                 process_name[i] = '\0';
719                                 break;
720                         }
721                         i++;
722                 }
723
724                 word = strtok_r(process_name, "/", &proc_name_ptr);
725                 while (word != NULL) {
726                         memset(app_name, 0, 512);
727                         snprintf(app_name, 512, "%s", word);
728                         word = strtok_r(NULL, "/", &proc_name_ptr);
729                 }
730                 __alarm_info->quark_app_unique_name =
731                     g_quark_from_string(app_name);
732         } else {                /* failure */
733                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
734                                           "killed, so we failed to get proc file(%s) and do not create "
735                                           "alarm_info\n", pid, proc_file);
736                 *error_code = -1;       /*-1 means that system failed 
737                                                         internally.*/
738                 free(__alarm_info);
739                 return false;
740         }
741
742         __alarm_info->quark_bundle=g_quark_from_string(bundle_data);
743         __alarm_info->quark_app_service_name = g_quark_from_string("null");
744         __alarm_info->quark_dst_service_name = g_quark_from_string("null");
745
746         __alarm_set_start_and_end_time(alarm_info, __alarm_info);
747         memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
748
749         time(&current_time);
750
751         ALARM_MGR_LOG_PRINT("[alarm-server]:pid=%d, app_unique_name=%s, "
752                 "app_bundle_encoded_val=%s,c_due_time=%d", \
753                 pid, g_quark_to_string(__alarm_info->quark_app_unique_name), \
754                 g_quark_to_string(__alarm_info->quark_bundle), \
755                             alarm_context.c_due_time);
756
757         if (alarm_context.c_due_time < current_time) {
758                 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
759                 "(%d) is less than current time(%d)", alarm_context.c_due_time,
760                                           current_time);
761                 alarm_context.c_due_time = -1;
762         }
763
764         due_time = _alarm_next_duetime(__alarm_info);
765         if (__alarm_add_to_list(__alarm_info, alarm_id) == false) {
766                 free(__alarm_info);
767                 *error_code = -1;
768                 return false;
769         }
770
771         if (due_time == 0) {
772                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create a new alarm: "
773                 "due_time is 0, alarm(%d) \n", *alarm_id);
774                 return true;
775         } else if (current_time == due_time) {
776                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create alarm: "
777                      "current_time(%d) is same as due_time(%d)", current_time,
778                      due_time);
779                 return true;
780         }else if (difftime(due_time, current_time) <  0){
781                 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);
782                 return true;
783         }else {
784                 localtime_r(&due_time, &ts_ret);
785                 strftime(due_time_r, 30, "%c", &ts_ret);
786                 ALARM_MGR_LOG_PRINT("[alarm-server]:Create a new alarm: "
787                                     "alarm(%d) due_time(%s)", *alarm_id,
788                                     due_time_r);
789         }
790
791         ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
792                             "due_time(%d)", alarm_context.c_due_time, due_time);
793
794         if (alarm_context.c_due_time == -1
795             || due_time < alarm_context.c_due_time) {
796                 _clear_scheduled_alarm_list();
797                 _add_to_scheduled_alarm_list(__alarm_info);
798                 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
799                                  *alarm_id);
800                 alarm_context.c_due_time = due_time;
801
802         } else if (due_time == alarm_context.c_due_time) {
803                 _add_to_scheduled_alarm_list(__alarm_info);
804
805         }
806
807         __rtc_set();
808
809 #ifdef __ALARM_BOOT
810         /*alarm boot */
811         if (enable_power_on_alarm) {
812                 __alarm_power_on(0, enable_power_on_alarm, NULL);
813         }
814 #endif
815
816         return true;
817
818 }
819
820 static bool __alarm_create(alarm_info_t *alarm_info, alarm_id_t *alarm_id,
821                            int pid, char *app_service_name, char *app_service_name_mod,
822                            const char *dst_service_name,const char *dst_service_name_mod,  int *error_code)
823 {
824
825         time_t current_time;
826         time_t due_time;
827         char proc_file[256] = { 0 };
828         char process_name[512] = { 0 };
829         char app_name[256] = { 0 };
830         char *word = NULL;
831         char *proc_name_ptr = NULL;
832
833         __alarm_info_t *__alarm_info = NULL;
834
835         __alarm_info = malloc(sizeof(__alarm_info_t));
836         if (__alarm_info == NULL) {
837                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, malloc "
838                                           "failed. it seems to be OOM\n", pid);
839                 *error_code = -1;       /* -1 means that system 
840                                            failed internally. */
841                 return false;
842         }
843         __alarm_info->pid = pid;
844         __alarm_info->alarm_id = -1;
845
846         /* we should consider to check whether  pid is running or Not
847          */
848         memset(process_name, '\0', 512);
849         memset(proc_file, '\0', 256);
850         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
851
852         int fd;
853         int ret;
854         int i = 0;
855         fd = open(proc_file, O_RDONLY);
856         if (fd > 0) {
857                 ret = read(fd, process_name, 512);
858                 close(fd);
859                 while (process_name[i] != '\0') {
860                         if (process_name[i] == ' ') {
861                                 process_name[i] = '\0';
862                                 break;
863                         }
864                         i++;
865                 }
866                 /* if (readlink(proc_file, process_name, 256)!=-1) */
867                 /*success */
868
869                 word = strtok_r(process_name, "/", &proc_name_ptr);
870                 while (word != NULL) {
871                         memset(app_name, 0, 256);
872                         snprintf(app_name, 256, "%s", word);
873                         word = strtok_r(NULL, "/", &proc_name_ptr);
874                 }
875                 __alarm_info->quark_app_unique_name =
876                     g_quark_from_string(app_name);
877         } else {                /* failure */
878
879                 __alarm_info->quark_app_unique_name =
880                     g_quark_from_string("unknown");
881                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
882                                           "killed, so we failed to get proc file(%s) and do not create "
883                                           "alarm_info\n", pid, proc_file);
884                 *error_code = -1;       /*-1 means that system failed 
885                                                         internally.*/
886                 free(__alarm_info);
887                 return false;
888         }
889
890         __alarm_info->quark_app_service_name =
891             g_quark_from_string(app_service_name);
892         __alarm_info->quark_app_service_name_mod =
893         g_quark_from_string(app_service_name_mod);
894         __alarm_info->quark_dst_service_name =
895             g_quark_from_string(dst_service_name);
896         __alarm_info->quark_dst_service_name_mod =
897             g_quark_from_string(dst_service_name_mod);
898         __alarm_info->quark_bundle = g_quark_from_string("null");
899
900
901         __alarm_set_start_and_end_time(alarm_info, __alarm_info);
902         memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
903
904         time(&current_time);
905
906         ALARM_MGR_LOG_PRINT("[alarm-server]:pid=%d, app_unique_name=%s, "
907                 "app_service_name=%s,dst_service_name=%s, c_due_time=%d", \
908                 pid, g_quark_to_string(__alarm_info->quark_app_unique_name), \
909                 g_quark_to_string(__alarm_info->quark_app_service_name), \
910                 g_quark_to_string(__alarm_info->quark_dst_service_name), \
911                             alarm_context.c_due_time);
912
913         if (alarm_context.c_due_time < current_time) {
914                 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
915                 "(%d) is less than current time(%d)", alarm_context.c_due_time,
916                                           current_time);
917                 alarm_context.c_due_time = -1;
918         }
919
920         due_time = _alarm_next_duetime(__alarm_info);
921         if (__alarm_add_to_list(__alarm_info, alarm_id) == false) {
922                 free(__alarm_info);
923                 return false;
924         }
925
926         if (due_time == 0) {
927                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create a new alarm: "
928                 "due_time is 0, alarm(%d) \n", *alarm_id);
929                 return true;
930         } else if (current_time == due_time) {
931                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Create alarm: "
932                      "current_time(%d) is same as due_time(%d)", current_time,
933                      due_time);
934                 return true;
935         }else if (difftime(due_time, current_time) <  0){
936                 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);
937                 return true;
938         } else {
939                 char due_time_r[100] = { 0 };
940                 struct tm ts_ret;
941                 localtime_r(&due_time, &ts_ret);
942                 strftime(due_time_r, 30, "%c", &ts_ret);
943                 ALARM_MGR_LOG_PRINT("[alarm-server]:Create a new alarm: "
944                                     "alarm(%d) due_time(%s)", *alarm_id,
945                                     due_time_r);
946         }
947
948         ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
949                             "due_time(%d)", alarm_context.c_due_time, due_time);
950
951         if (alarm_context.c_due_time == -1
952             || due_time < alarm_context.c_due_time) {
953                 _clear_scheduled_alarm_list();
954                 _add_to_scheduled_alarm_list(__alarm_info);
955                 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
956                                  *alarm_id);
957                 alarm_context.c_due_time = due_time;
958
959         } else if (due_time == alarm_context.c_due_time) {
960                 _add_to_scheduled_alarm_list(__alarm_info);
961
962         }
963
964         __rtc_set();
965
966 #ifdef __ALARM_BOOT
967         /*alarm boot */
968         if (enable_power_on_alarm) {
969                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
970                  * platform with app-server.because __alarm_power_on(..) fuction don't 
971                  * use first parameter internally, we set this value to 0(zero)
972                  */
973                 __alarm_power_on(0, enable_power_on_alarm, NULL);
974         }
975 #endif
976         return true;
977 }
978
979 static bool __alarm_update(int pid, char *app_service_name, alarm_id_t alarm_id,
980                            alarm_info_t *alarm_info, int *error_code)
981 {
982         time_t current_time;
983         time_t due_time;
984
985         __alarm_info_t *__alarm_info = NULL;
986         bool result = false;
987
988         __alarm_info = malloc(sizeof(__alarm_info_t));
989         if (__alarm_info == NULL) {
990                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid=%d, "
991                         "malloc failed. it seems to be OOM\n", pid);
992                 *error_code = -1;       /*-1 means that system failed 
993                                                 internally.*/
994                 return false;
995         }
996
997         __alarm_info->pid = pid;
998         __alarm_info->alarm_id = alarm_id;
999
1000         /* we should consider to check whether  pid is running or Not
1001          */
1002
1003         __alarm_info->quark_app_service_name =
1004             g_quark_from_string(app_service_name);
1005         __alarm_set_start_and_end_time(alarm_info, __alarm_info);
1006         memcpy(&(__alarm_info->alarm_info), alarm_info, sizeof(alarm_info_t));
1007
1008         time(&current_time);
1009
1010         if (alarm_context.c_due_time < current_time) {
1011                 ALARM_MGR_EXCEPTION_PRINT("Caution!! alarm_context.c_due_time "
1012                 "(%d) is less than current time(%d)", alarm_context.c_due_time,
1013                                           current_time);
1014                 alarm_context.c_due_time = -1;
1015         }
1016
1017         due_time = _alarm_next_duetime(__alarm_info);
1018         if (!__alarm_update_in_list(pid, alarm_id, __alarm_info, error_code)) {
1019                 free(__alarm_info);
1020                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]: requested alarm_id "
1021                 "(%d) does not exist. so this value is invalid id.", alarm_id);
1022                 return false;
1023         }
1024         /* ALARM_MGR_LOG_PRINT("[alarm-server]:request_pid=%d, alarm_id=%d, 
1025          * app_unique_name=%s, app_service_name=%s, dst_service_name=%s, 
1026          * c_due_time=%d", pid, alarm_id, g_quark_to_string
1027          * (__alarm_info->quark_app_unique_name), g_quark_to_string
1028          * (__alarm_info->quark_app_service_name), g_quark_to_string
1029          * (__alarm_info->quark_dst_service_name), alarm_context.c_due_time);
1030          */
1031
1032         result = _remove_from_scheduled_alarm_list(pid, alarm_id);
1033
1034         if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
1035                 /*there is no scheduled alarm */
1036                 _alarm_disable_timer(alarm_context);
1037                 _alarm_schedule();
1038
1039                 ALARM_MGR_LOG_PRINT("[alarm-server]:Update alarm: alarm(%d)\n",
1040                                     alarm_id);
1041
1042                 __rtc_set();
1043
1044 #ifdef __ALARM_BOOT
1045                 /*alarm boot */
1046                 if (enable_power_on_alarm) {
1047                         /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in 
1048                          * a platform with app-server.because __alarm_power_on(..) fuction don't
1049                          * use first parameter internally, we set this value to 0(zero)
1050                          */
1051                         __alarm_power_on(0, enable_power_on_alarm, NULL);
1052                 }
1053 #endif
1054
1055                 if (due_time == 0) {
1056                         ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1057                                         "due_time is 0\n");
1058                 }
1059                 free(__alarm_info);
1060                 return true;
1061         }
1062
1063         if (due_time == 0) {
1064                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1065                                 "due_time is 0, alarm(%d)\n", alarm_id);
1066                 free(__alarm_info);
1067                 return true;
1068         } else if (current_time == due_time) {
1069                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:Update alarm: "
1070                 "current_time(%d) is same as due_time(%d)", current_time,
1071                 due_time);
1072                 free(__alarm_info);
1073                 return true;
1074         }else if (difftime(due_time, current_time)< 0){
1075                 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);
1076                 return true;
1077         } else {
1078                 char due_time_r[100] = { 0 };
1079                 struct tm ts_ret;
1080                 localtime_r(&due_time, &ts_ret);
1081                 strftime(due_time_r, 30, "%c", &ts_ret);
1082                 ALARM_MGR_LOG_PRINT("[alarm-server]:Update alarm: alarm(%d) "
1083                                     "due_time(%s)\n", alarm_id, due_time_r);
1084         }
1085
1086         ALARM_MGR_LOG_PRINT("[alarm-server]:alarm_context.c_due_time(%d), "
1087                             "due_time(%d)", alarm_context.c_due_time, due_time);
1088
1089         if (alarm_context.c_due_time == -1
1090             || due_time < alarm_context.c_due_time) {
1091                 _clear_scheduled_alarm_list();
1092                 _add_to_scheduled_alarm_list(__alarm_info);
1093                 _alarm_set_timer(&alarm_context, alarm_context.timer, due_time,
1094                                  alarm_id);
1095                 alarm_context.c_due_time = due_time;
1096                 ALARM_MGR_LOG_PRINT("[alarm-server1]:alarm_context.c_due_time "
1097                      "(%d), due_time(%d)", alarm_context.c_due_time, due_time);
1098         } else if (due_time == alarm_context.c_due_time) {
1099                 _add_to_scheduled_alarm_list(__alarm_info);
1100                 ALARM_MGR_LOG_PRINT("[alarm-server2]:alarm_context.c_due_time "
1101                      "(%d), due_time(%d)", alarm_context.c_due_time, due_time);
1102         }
1103
1104         __rtc_set();
1105
1106 #ifdef __ALARM_BOOT
1107         /*alarm boot */
1108         if (enable_power_on_alarm) {
1109                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) 
1110                  * in a platform with app-server.because __alarm_power_on(..) fuction 
1111                  * don't use first parameter internally, we set this value to 0(zero)
1112                  */
1113                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1114         }
1115 #endif
1116
1117         return true;
1118 }
1119
1120 static bool __alarm_delete(int pid, alarm_id_t alarm_id, int *error_code)
1121 {
1122         bool result = false;
1123
1124         ALARM_MGR_LOG_PRINT("[alarm-server]:delete alarm: alarm(%d) pid(%d)\n",\
1125                             alarm_id, pid);
1126         result = _remove_from_scheduled_alarm_list(pid, alarm_id);
1127
1128         if (!__alarm_remove_from_list(pid, alarm_id, error_code)) {
1129
1130                 ALARM_MGR_EXCEPTION_PRINT("[alarm-server]:delete alarm: "
1131                                           "alarm(%d) pid(%d) has failed with error_code(%d)\n",
1132                                           alarm_id, pid, *error_code);
1133                 return false;
1134         }
1135
1136         if (result == true && g_slist_length(g_scheduled_alarm_list) == 0) {
1137                 _alarm_disable_timer(alarm_context);
1138                 _alarm_schedule();
1139         }
1140
1141         __rtc_set();
1142
1143 #ifdef __ALARM_BOOT
1144         /*alarm boot */
1145         if (enable_power_on_alarm) {
1146                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a 
1147                  * platform with app-server.because __alarm_power_on(..) fuction don't 
1148                  * use first parameter internally, we set this value to 0(zero)
1149                  */
1150                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1151         }
1152 #endif
1153
1154         return true;
1155 }
1156
1157 static bool __alarm_power_on(int app_id, bool on_off, int *error_code)
1158 {
1159 #ifdef __ALARM_BOOT
1160         time_t min_time = 0;
1161         time_t current_time = 0;
1162         struct tm *temp_info;
1163         struct rtc_time rtc_tm = { 0, };
1164         struct tm min_time_r;
1165         int fd = 0;
1166         int retval;
1167
1168         enable_power_on_alarm = on_off;
1169         /*_update_power_on(on_off); */ /*currently its empty*/
1170
1171         fd = open(power_rtc, O_RDONLY);
1172         if (fd < 0) {
1173                 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc0\n");
1174                 return false;
1175         }
1176
1177         if (on_off == true) {
1178                 if (_alarm_find_mintime_power_on(&min_time) == true) {
1179
1180                         ab_due_time = min_time;
1181
1182                         min_time = min_time - 60;
1183
1184                         time(&current_time);
1185
1186                         if (min_time <= current_time)
1187                                 min_time = current_time + 5;
1188
1189                         temp_info = gmtime_r(&min_time, &min_time_r);
1190
1191                         ALARM_MGR_LOG_PRINT("__alarm_power_on : %d %d %d %d "
1192                                                 "%d\n", \
1193                                            min_time_r.tm_year,\
1194                                            min_time_r.tm_mon,\
1195                                            min_time_r.tm_mday,\
1196                                            min_time_r.tm_hour,\
1197                                            min_time_r.tm_min);
1198                         ALARM_MGR_LOG_PRINT("__alarm_power_on : %d %d %d %d "
1199                                                 "%d\n", \
1200                                            min_time_r.tm_year,\
1201                                            min_time_r.tm_mon,\
1202                                            min_time_r.tm_mday,\
1203                                            min_time_r.tm_hour,\
1204                                            min_time_r.tm_min);
1205
1206                         rtc_tm.tm_mday = min_time_r.tm_mday;
1207                         rtc_tm.tm_mon = min_time_r.tm_mon;
1208                         rtc_tm.tm_year = min_time_r.tm_year;
1209                         rtc_tm.tm_hour = min_time_r.tm_hour;
1210                         rtc_tm.tm_min = min_time_r.tm_min;
1211                         rtc_tm.tm_sec = min_time_r.tm_sec;
1212                         /*set_info.time_zone = 0; */
1213                         /*set_info.u_interval.day_of_week = 0; */
1214
1215                         /*ALARM_MGR_LOG_PRINT("####__alarm_power_on : %d %d 
1216                         %d %d %d\n",set_info.year,set_info.month,set_info.day,
1217                         set_info.hour,set_info.minute); */
1218
1219                         ALARM_MGR_LOG_PRINT("due_time : %d \n", ab_due_time);
1220
1221                         ALARM_MGR_LOG_PRINT("\n\nSetted RTC Alarm date/time is "
1222                                             "%d-%d-%d, %02d:%02d:%02d.\n",
1223                                             rtc_tm.tm_mday, rtc_tm.tm_mon + 1,
1224                                 rtc_tm.tm_year + 1900, rtc_tm.tm_hour, 
1225                                 rtc_tm.tm_min, rtc_tm.tm_sec);
1226
1227                         retval = ioctl(fd, RTC_ALM_SET, &rtc_tm);
1228                         if (retval == -1) {
1229                                 if (errno == ENOTTY) {
1230                                         ALARM_MGR_EXCEPTION_PRINT(
1231                                         "\n...Alarm IRQs not supported.\n");
1232                                 }
1233                                 ALARM_MGR_EXCEPTION_PRINT("RTC_ALM_SET ioctl");
1234                                 close(fd);
1235                                 return false;
1236                         }
1237                         ALARM_MGR_LOG_PRINT("[alarm-server]RTC "
1238                                             "alarm(POWER ON) is setted");
1239
1240                         /* Enable alarm interrupts */
1241                         retval = ioctl(fd, RTC_AIE_ON, 0);
1242                         if (retval == -1) {
1243                                 ALARM_MGR_EXCEPTION_PRINT(
1244                                     "RTC_AIE_ON ioctl failed");
1245                                 close(fd);
1246                                 return false;
1247                         }
1248                         ALARM_MGR_LOG_PRINT("[alarm-server]RTC(POWER ON) "
1249                                             "alarm is on");
1250
1251                 } else
1252                         retval = ioctl(fd, RTC_AIE_OFF, 0);
1253         } else {
1254                 ALARM_MGR_LOG_PRINT("__alarm_power_on : off\n");
1255                 retval = ioctl(fd, RTC_AIE_OFF, 0);
1256         }
1257
1258         close(fd);
1259 #endif                          /* #ifdef __ALARM_BOOT */
1260
1261         return true;
1262 }
1263
1264 static bool __alarm_power_off(int app_id, int *error_code)
1265 {
1266 #ifdef __ALARM_BOOT
1267
1268 #endif                          /* #ifdef __ALARM_BOOT */
1269         return true;
1270 }
1271
1272 static bool __alarm_check_next_duetime(int app_id, int *error_code)
1273 {
1274 #ifdef __ALARM_BOOT
1275         time_t current_time;
1276         time_t interval;
1277
1278         time(&current_time);
1279
1280         interval = ab_due_time - current_time;
1281
1282         ALARM_MGR_LOG_PRINT("due_time : %d / current_time %d\n", ab_due_time,
1283                             current_time);
1284
1285         if (interval > 0 && interval <= 60)
1286                 return true;
1287         else
1288                 return false;
1289 #else                           /* #ifdef __ALARM_BOOT */
1290         return true;
1291 #endif
1292 }
1293
1294 static void __alarm_send_noti_to_application(const char *app_service_name,
1295                                              alarm_id_t alarm_id)
1296 {
1297
1298
1299         char service_name[MAX_SERVICE_NAME_LEN];
1300         char object_name[MAX_SERVICE_NAME_LEN];
1301
1302         DBusMessage *message;
1303         DBusMessageIter iter;
1304
1305         if (app_service_name == NULL || strlen(app_service_name) == 0) {
1306                 ALARM_MGR_EXCEPTION_PRINT("This alarm destination is "
1307                                           "invalid\n");
1308                 return;
1309         }
1310         memset(service_name, 0, MAX_SERVICE_NAME_LEN);
1311         memcpy(service_name, app_service_name, strlen(app_service_name));
1312
1313         snprintf(object_name, MAX_SERVICE_NAME_LEN,
1314                  "/org/tizen/alarm/client");
1315
1316         ALARM_MGR_LOG_PRINT("[alarm server][send expired_alarm(alarm_id=%d)to"
1317         "app_service_name(%s), object_name(%s), interface_name(%s)]\n",\
1318         alarm_id, service_name, object_name, "org.tizen.alarm.client");
1319
1320         message = dbus_message_new_method_call(service_name,
1321                                                object_name,
1322                                                "org.tizen.alarm.client",
1323                                                "alarm_expired");
1324         if (message == NULL) {
1325                 ALARM_MGR_EXCEPTION_PRINT("[alarm server] "
1326                         "dbus_message_new_method_call faild. maybe OOM!.\n");
1327                 ALARM_MGR_EXCEPTION_PRINT("[alarm server] so we cannot "
1328                         "send expired alarm to %s\n", service_name);
1329                 return;
1330         }
1331
1332         dbus_message_set_no_reply(message, TRUE);
1333         /*      if(service_name[0]==':') */
1334         /* we don't need auto activation in a case that 
1335            destination_app_service_name starts with a charactor like (:) */
1336         dbus_message_set_auto_start(message, FALSE);
1337
1338         dbus_message_iter_init_append(message, &iter);
1339         if (!dbus_message_iter_append_basic 
1340                 (&iter, DBUS_TYPE_INT32, &alarm_id)) {
1341                 dbus_message_unref(message);
1342                 ALARM_MGR_EXCEPTION_PRINT("[alarm server] "
1343                 "dbus_message_iter_append_basic faild. maybe OOM!.\n");
1344                 return;
1345         }
1346
1347         dbus_connection_send(dbus_g_connection_get_connection
1348                              (alarm_context.bus), message, NULL);
1349         dbus_connection_flush(dbus_g_connection_get_connection
1350                               (alarm_context.bus));
1351         dbus_message_unref(message);
1352
1353 }
1354
1355 static void __alarm_expired()
1356 {
1357         const char *destination_app_service_name = NULL;
1358         alarm_id_t alarm_id = -1;
1359         int app_pid = 0;
1360         __alarm_info_t *__alarm_info = NULL;
1361         char alarm_id_val[32]={0,};
1362         int b_len = 0;
1363         bundle *b = NULL;
1364
1365         ALARM_MGR_LOG_PRINT("[alarm-server]: Enter \n");
1366
1367         time_t current_time;
1368         double interval;
1369
1370         time(&current_time);
1371
1372         interval = difftime(alarm_context.c_due_time, current_time);
1373         ALARM_MGR_LOG_PRINT("[alarm-server]: c_due_time(%d), "
1374                 "current_time(%d), interval(%d)\n", alarm_context.c_due_time, 
1375                 current_time, interval);
1376
1377         if (alarm_context.c_due_time <= current_time - 1) {
1378                 ALARM_MGR_LOG_PRINT("Wrong alarm is expired\n");
1379                 goto done;
1380         }
1381
1382         GSList *iter = NULL;
1383         __scheduled_alarm_t *alarm = NULL;
1384
1385         for (iter = g_scheduled_alarm_list; iter != NULL;
1386              iter = g_slist_next(iter)) {
1387                 alarm = iter->data;
1388                 alarm_id = alarm->alarm_id;
1389
1390                 __alarm_info = alarm->__alarm_info;
1391
1392                 app_pid = __alarm_info->pid;
1393
1394                 if (strncmp
1395                     (g_quark_to_string(__alarm_info->quark_bundle),
1396                      "null", 4) != 0) {
1397
1398                                 b_len = strlen(g_quark_to_string(__alarm_info->quark_bundle));
1399
1400                                 b = bundle_decode((bundle_raw *)g_quark_to_string(__alarm_info->quark_bundle), b_len);
1401
1402                                 if (NULL == b)
1403                                 {
1404                                         ALARM_MGR_EXCEPTION_PRINT("Error!!!..Unable to decode the bundle!!\n");
1405                                 }
1406                                 else
1407                                 {
1408                                         snprintf(alarm_id_val,31,"%d",alarm_id);
1409
1410                                         if (bundle_add(b,"slp.alarm.data.ID", alarm_id_val)){
1411                                                 ALARM_MGR_EXCEPTION_PRINT("Unable to add alarm id to the bundle\n");
1412                                         }
1413                                         else
1414                                         {
1415                                                 if ( appsvc_run_service(b, 0, NULL, NULL) < 0)
1416                                                 {
1417                                                         ALARM_MGR_EXCEPTION_PRINT("Unable to run app svc\n");
1418                                                 }
1419                                                 else
1420                                                 {
1421                                                         ALARM_MGR_LOG_PRINT("Successfuly ran app svc\n");
1422                                                         bundle_free(b);
1423                                                 }
1424                                         }
1425                                 }
1426
1427                 }
1428                 else
1429                 {
1430                         if (strncmp
1431                             (g_quark_to_string(__alarm_info->quark_dst_service_name),
1432                              "null",4) == 0) {
1433                                 ALARM_MGR_LOG_PRINT("[alarm-server]:destination is "
1434                                 "null, so we send expired alarm to %s(%u)\n",\
1435                                         g_quark_to_string(
1436                                         __alarm_info->quark_app_service_name),
1437                                         __alarm_info->quark_app_service_name); 
1438                                         destination_app_service_name = g_quark_to_string(
1439                                         __alarm_info->quark_app_service_name_mod);
1440                         } else {
1441                                 ALARM_MGR_LOG_PRINT("[alarm-server]:destination "
1442                                                     ":%s(%u)\n",
1443                                         g_quark_to_string(
1444                                         __alarm_info->quark_dst_service_name), 
1445                                         __alarm_info->quark_dst_service_name);
1446                                         destination_app_service_name = g_quark_to_string(
1447                                                 __alarm_info->quark_dst_service_name_mod);
1448                         }
1449
1450 #ifdef __ALARM_BOOT
1451                         /* orginally this code had if(__alarm_info->app_id==21) in a 
1452                            platform with app-server. */
1453                         /*if(__alarm_info->quark_dst_service_name  == 
1454                            g_quark_from_string (WAKEUP_ALARM_APP_ID)) */
1455                         if (strcmp
1456                             (g_quark_to_string(__alarm_info->quark_dst_service_name),
1457                              WAKEUP_ALARM_APP_ID) == 0) {
1458                                 int fd = 0;
1459                                 fd = open(power_rtc, O_RDONLY);
1460                                 if (fd < 0) {
1461                                         ALARM_MGR_LOG_PRINT("cannot open /dev/rtc0\n");
1462                                 } else {
1463                                         ioctl(fd, RTC_AIE_OFF, 0);
1464                                         close(fd);
1465                                 }
1466                         }
1467 #endif
1468
1469                         /* 
1470                          * we should consider a situation that 
1471                          * destination_app_service_name is owner_name like (:xxxx) and
1472                          * application's pid which registered this alarm was killed.In that case,
1473                          * we don't need to send the expire event because the process was killed.
1474                          * this causes needless message to be sent.
1475                          */
1476                         ALARM_MGR_LOG_PRINT("[alarm-server]: "
1477                                             "destination_app_service_name :%s, app_pid=%d\n",
1478                                             destination_app_service_name, app_pid);
1479                         /* the following is a code that checks the above situation. 
1480                            please verify this code. */
1481
1482                         if (dbus_bus_name_has_owner(
1483                              dbus_g_connection_get_connection(alarm_context.bus),
1484                              destination_app_service_name, NULL) == FALSE) {
1485                                 __expired_alarm_t *expire_info;
1486                                 char pkg_name[MAX_SERVICE_NAME_LEN] = { 0, };
1487                                 char alarm_id_str[32] = { 0, };
1488
1489                                 expire_info = malloc(sizeof(__expired_alarm_t));
1490                                 if (G_UNLIKELY(NULL == expire_info)){
1491                                         ALARM_MGR_ASSERT_PRINT("[alarm-server]:Malloc failed!Can't notify alarm expiry info\n");
1492                                         goto done;
1493                                 }
1494                                 strncpy(expire_info->service_name,
1495                                         destination_app_service_name,
1496                                         MAX_SERVICE_NAME_LEN);
1497                                 expire_info->alarm_id = alarm_id;
1498                                 g_expired_alarm_list =
1499                                     g_slist_append(g_expired_alarm_list, expire_info);
1500
1501
1502                                 if (strncmp
1503                                         (g_quark_to_string(__alarm_info->quark_dst_service_name),
1504                                              "null",4) == 0) {
1505                                         strncpy(pkg_name,g_quark_to_string(__alarm_info->quark_app_service_name),strlen(g_quark_to_string(__alarm_info->quark_app_service_name))-6);
1506                                 }
1507                                 else
1508                                 {
1509                                         strncpy(pkg_name,g_quark_to_string(__alarm_info->quark_dst_service_name),strlen(g_quark_to_string(__alarm_info->quark_dst_service_name))-6);
1510                                 }
1511
1512                                 snprintf(alarm_id_str, 31, "%d", alarm_id);
1513
1514                                 ALARM_MGR_LOG_PRINT("before aul_launch pkg_name(%s) "
1515                                         "alarm_id_str(%s)\n", pkg_name, alarm_id_str);
1516
1517                                 bundle *kb;
1518                                 kb = bundle_create();
1519                                 bundle_add(kb, "__ALARM_MGR_ID", alarm_id_str);
1520                                 aul_launch_app(pkg_name, kb);
1521                                 bundle_free(kb);
1522                         } else {
1523                                 ALARM_MGR_LOG_PRINT(
1524                                         "before alarm_send_noti_to_application\n");
1525                                 __alarm_send_noti_to_application(
1526                                              destination_app_service_name, alarm_id);
1527                         }
1528                 }
1529                 ALARM_MGR_LOG_PRINT("after __alarm_send_noti_to_application\n");
1530
1531 /*              if( !(__alarm_info->alarm_info.alarm_type 
1532                                         & ALARM_TYPE_VOLATILE) ) {
1533                         __alarm_remove_from_list(__alarm_info->pid, 
1534                                                         alarm_id, NULL);
1535                 }
1536                 else */
1537                 if (__alarm_info->alarm_info.mode.repeat
1538                     == ALARM_REPEAT_MODE_ONCE) {
1539 /*                      _alarm_next_duetime(__alarm_info);*/
1540 /*                      _update_alarms(__alarm_info);*/
1541                         __alarm_remove_from_list(__alarm_info->pid, alarm_id,
1542                                                  NULL);
1543                 } else {
1544
1545                         _alarm_next_duetime(__alarm_info);
1546 /*                      _update_alarms(__alarm_info);*/
1547                 }
1548
1549         }
1550
1551  done:
1552         _clear_scheduled_alarm_list();
1553         alarm_context.c_due_time = -1;
1554
1555         ALARM_MGR_LOG_PRINT("[alarm-server]: Leave  \n");
1556 }
1557
1558 static gboolean __alarm_handler_idle()
1559 {
1560         if (g_dummy_timer_is_set == true) {
1561                 ALARM_MGR_LOG_PRINT("dummy alarm timer has expired\n");
1562         } else {
1563                 ALARM_MGR_LOG_PRINT("__alarm_handler \n");
1564                 ALARM_MGR_LOG_PRINT("__alarm_handler \n");
1565
1566                 __alarm_expired();
1567
1568         }
1569
1570         _alarm_schedule();
1571
1572         __rtc_set();
1573
1574 #ifdef __ALARM_BOOT
1575         /*alarm boot */
1576         if (enable_power_on_alarm) {
1577                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) 
1578                  *in a platform with app-server.because __alarm_power_on(..) fuction 
1579                  *don't use first parameter internally, we set this value to 0(zero)
1580                  */
1581                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1582         }
1583 #endif
1584         pm_unlock_state(LCD_OFF, PM_SLEEP_MARGIN);
1585         return false;
1586
1587 }
1588
1589 static void __alarm_handler(int sigNum, siginfo_t *pSigInfo, void *pUContext)
1590 {
1591
1592         pm_lock_state(LCD_OFF, STAY_CUR_STATE, 0);
1593
1594         /* we moved __alarm_expired() function to __alarm_handler_idle GSource
1595            because of signal safety. */
1596         g_idle_add_full(G_PRIORITY_HIGH_IDLE, __alarm_handler_idle, NULL, NULL);
1597 }
1598
1599 static void __clean_registry()
1600 {
1601
1602         /*TODO:remove all db entries */
1603 }
1604
1605 static bool __alarm_manager_reset()
1606 {
1607
1608         _alarm_disable_timer(alarm_context);
1609
1610         __alarm_clean_list();
1611
1612         _clear_scheduled_alarm_list();
1613         __clean_registry();
1614
1615         return true;
1616 }
1617
1618 static void __on_system_time_changed(keynode_t *node, void *data)
1619 {
1620         double diff_time;
1621         time_t _time;
1622         time_t before;
1623
1624         _alarm_disable_timer(alarm_context);
1625
1626         if (node) {
1627                 _time = vconf_keynode_get_int(node);
1628         } else {
1629                 vconf_get_int(VCONFKEY_SYSTEM_TIMECHANGE, (int *)&_time);
1630         }
1631
1632         time(&before);
1633         diff_time = difftime(_time, before);
1634
1635         tzset();
1636
1637         ALARM_MGR_ASSERT_PRINT("diff_time is %f\n", diff_time);
1638
1639         ALARM_MGR_LOG_PRINT("[alarm-server] System time has been changed\n");
1640         ALARM_MGR_LOG_PRINT("1.alarm_context.c_due_time is %d\n",
1641                             alarm_context.c_due_time);
1642
1643         _set_time(_time);
1644         __alarm_update_due_time_of_all_items_in_list(diff_time);
1645
1646         ALARM_MGR_LOG_PRINT("2.alarm_context.c_due_time is %d\n",
1647                             alarm_context.c_due_time);
1648         _clear_scheduled_alarm_list();
1649         _alarm_schedule();
1650         __rtc_set();
1651 #ifdef __ALARM_BOOT
1652         /*alarm boot */
1653         if (enable_power_on_alarm) {
1654 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_
1655 APP_ID) in a platform with app-server. because _alarm_power_
1656 on(..) fuction don't use first parameter internally, we set 
1657 this value to 0(zero)
1658 */
1659                 __alarm_power_on(0, enable_power_on_alarm, NULL);
1660         }
1661 #endif
1662         return;
1663 }
1664
1665
1666 gboolean alarm_manager_alarm_create_appsvc(void *pObject, int pid,
1667                                     int start_year,
1668                                     int start_month, int start_day,
1669                                     int start_hour, int start_min,
1670                                     int start_sec, int end_year, int end_month,
1671                                     int end_day, int mode_day_of_week,
1672                                     int mode_repeat, int alarm_type,
1673                                     int reserved_info,
1674                                     char *bundle_data, char *e_cookie,
1675                                     int *alarm_id, int *return_code)
1676 {
1677         alarm_info_t alarm_info;
1678         guchar *cookie = NULL;
1679         gsize size;
1680         int retval = 0;
1681         gid_t call_gid;
1682         gboolean result = true;
1683
1684         alarm_info.start.year = start_year;
1685         alarm_info.start.month = start_month;
1686         alarm_info.start.day = start_day;
1687         alarm_info.start.hour = start_hour;
1688         alarm_info.start.min = start_min;
1689         alarm_info.start.sec = start_sec;
1690
1691         alarm_info.end.year = end_year;
1692         alarm_info.end.month = end_month;
1693         alarm_info.end.day = end_day;
1694
1695         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
1696         alarm_info.mode.repeat = mode_repeat;
1697
1698         alarm_info.alarm_type = alarm_type;
1699         alarm_info.reserved_info = reserved_info;
1700
1701         *return_code = 0;
1702
1703         cookie = g_base64_decode(e_cookie, &size);
1704         if (NULL == cookie)
1705         {
1706                 *return_code = -1;
1707                 ALARM_MGR_EXCEPTION_PRINT("Unable to decode cookie!!!\n");
1708                 return false;
1709         }
1710         
1711         call_gid = security_server_get_gid("alarm");
1712
1713         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
1714
1715         retval = security_server_check_privilege((const char *)cookie, call_gid);
1716         if (retval < 0) {
1717                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1718                         ALARM_MGR_EXCEPTION_PRINT(
1719                                 "%s", "access has been denied\n");
1720                 }
1721                 ALARM_MGR_EXCEPTION_PRINT("Error has occurred in security_server_check_privilege()\n");
1722                 *return_code = -1;
1723                 result = false;
1724         }
1725         else {
1726                 result = __alarm_create_appsvc(&alarm_info, alarm_id, pid,
1727                                bundle_data, return_code);
1728                 if (false == result)
1729                 {
1730                         ALARM_MGR_EXCEPTION_PRINT("Unable to create alarm!\n");
1731                 }
1732         }
1733
1734         if (cookie){
1735                 g_free(cookie);
1736                 cookie = NULL;
1737         }
1738
1739         return result;
1740 }
1741
1742 gboolean alarm_manager_alarm_create(void *pObject, int pid,
1743                                     char *app_service_name, char *app_service_name_mod,  int start_year,
1744                                     int start_month, int start_day,
1745                                     int start_hour, int start_min,
1746                                     int start_sec, int end_year, int end_month,
1747                                     int end_day, int mode_day_of_week,
1748                                     int mode_repeat, int alarm_type,
1749                                     int reserved_info,
1750                                     char *reserved_service_name, char *reserved_service_name_mod, char *e_cookie,
1751                                     int *alarm_id, int *return_code)
1752 {
1753         alarm_info_t alarm_info;
1754         guchar *cookie;
1755         gsize size;
1756         int retval;
1757         gid_t call_gid;
1758
1759         alarm_info.start.year = start_year;
1760         alarm_info.start.month = start_month;
1761         alarm_info.start.day = start_day;
1762         alarm_info.start.hour = start_hour;
1763         alarm_info.start.min = start_min;
1764         alarm_info.start.sec = start_sec;
1765
1766         alarm_info.end.year = end_year;
1767         alarm_info.end.month = end_month;
1768         alarm_info.end.day = end_day;
1769
1770         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
1771         alarm_info.mode.repeat = mode_repeat;
1772
1773         alarm_info.alarm_type = alarm_type;
1774         alarm_info.reserved_info = reserved_info;
1775
1776         *return_code = 0;
1777
1778         cookie = g_base64_decode(e_cookie, &size);
1779         call_gid = security_server_get_gid("alarm");
1780
1781         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
1782
1783         retval = security_server_check_privilege((const char *)cookie, call_gid);
1784         if (retval < 0) {
1785                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1786                         ALARM_MGR_EXCEPTION_PRINT(
1787                                 "%s", "access has been denied\n");
1788                 }
1789                 ALARM_MGR_EXCEPTION_PRINT("%s", "Error has occurred\n");
1790
1791                 *return_code = -1;
1792         }
1793
1794         else {
1795                 /* return valule and return_code should be checked */
1796                 __alarm_create(&alarm_info, alarm_id, pid, app_service_name,app_service_name_mod,
1797                                reserved_service_name, reserved_service_name_mod, return_code);
1798         }
1799
1800         g_free(cookie);
1801
1802         return true;
1803 }
1804
1805 gboolean alarm_manager_alarm_delete(void *pObject, int pid, alarm_id_t alarm_id,
1806                                     char *e_cookie, int *return_code)
1807 {
1808         guchar *cookie;
1809         gsize size;
1810         int retval;
1811         gid_t call_gid;
1812
1813         cookie = g_base64_decode(e_cookie, &size);
1814         call_gid = security_server_get_gid("alarm");
1815
1816         ALARM_MGR_LOG_PRINT("call_gid : %d\n", call_gid);
1817
1818         retval = security_server_check_privilege((const char *)cookie, call_gid);
1819         if (retval < 0) {
1820                 if (retval == SECURITY_SERVER_API_ERROR_ACCESS_DENIED) {
1821                         ALARM_MGR_EXCEPTION_PRINT("%s",
1822                                                   "access has been denied\n");
1823                 }
1824                 ALARM_MGR_EXCEPTION_PRINT("%s", "Error has occurred\n");
1825
1826                 *return_code = -1;
1827         }
1828
1829         else {
1830                 __alarm_delete(pid, alarm_id, return_code);
1831         }
1832
1833         g_free(cookie);
1834
1835         return true;
1836 }
1837
1838 gboolean alarm_manager_alarm_power_on(void *pObject, int pid, bool on_off,
1839                                       int *return_code)
1840 {
1841
1842         return __alarm_power_on(pid, on_off, return_code);
1843 }
1844
1845 gboolean alarm_manager_alarm_power_off(void *pObject, int pid, int *return_code)
1846 {
1847
1848         return __alarm_power_off(pid, return_code);
1849 }
1850
1851 bool alarm_manager_alarm_check_next_duetime(void *pObject, int pid,
1852                                             int *return_code)
1853 {
1854         return __alarm_check_next_duetime(pid, return_code);
1855 }
1856
1857 gboolean alarm_manager_alarm_update(void *pObject, int pid,
1858                                     char *app_service_name, alarm_id_t alarm_id,
1859                                     int start_year, int start_month,
1860                                     int start_day, int start_hour,
1861                                     int start_min, int start_sec, int end_year,
1862                                     int end_month, int end_day,
1863                                     int mode_day_of_week, int mode_repeat,
1864                                     int alarm_type, int reserved_info,
1865                                     int *return_code)
1866 {
1867         alarm_info_t alarm_info;
1868         alarm_info.start.year = start_year;
1869         alarm_info.start.month = start_month;
1870         alarm_info.start.day = start_day;
1871         alarm_info.start.hour = start_hour;
1872         alarm_info.start.min = start_min;
1873         alarm_info.start.sec = start_sec;
1874
1875         alarm_info.end.year = end_year;
1876         alarm_info.end.month = end_month;
1877         alarm_info.end.day = end_day;
1878
1879         alarm_info.mode.u_interval.day_of_week = mode_day_of_week;
1880         alarm_info.mode.repeat = mode_repeat;
1881
1882         alarm_info.alarm_type = alarm_type;
1883         alarm_info.reserved_info = reserved_info;
1884
1885         *return_code = 0;
1886
1887         __alarm_update(pid, app_service_name, alarm_id, &alarm_info,
1888                        return_code);
1889
1890         return true;
1891 }
1892
1893 gboolean alarm_manager_alarm_get_number_of_ids(void *pObject, int pid,
1894                                                int *num_of_ids,
1895                                                int *return_code)
1896 {
1897         GSList *gs_iter = NULL;
1898         GQuark quark_app_unique_name;   /* the fullpath of pid(pid) is 
1899                                            converted  to quark value. */
1900         char proc_file[256] = { 0 };
1901         char process_name[512] = { 0 };
1902         char app_name[256] = { 0 };
1903         char *word = NULL;
1904         __alarm_info_t *entry = NULL;
1905         char *proc_name_ptr = NULL;
1906
1907         *num_of_ids = 0;
1908         *return_code = 0;
1909
1910         /* we should consider to check whether  pid is running or Not
1911          */
1912         memset(process_name, '\0', 512);
1913         memset(proc_file, '\0', 256);
1914         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
1915
1916         int fd;
1917         int ret;
1918         int i = 0;
1919         fd = open(proc_file, O_RDONLY);
1920         if (fd > 0) {
1921                 ret = read(fd, process_name, 512);
1922                 close(fd);
1923                 while (process_name[i] != '\0') {
1924                         if (process_name[i] == ' ') {
1925                                 process_name[i] = '\0';
1926                                 break;
1927                         }
1928                         i++;
1929                 }
1930                 /*if (readlink(proc_file, process_name, 256)!=-1) */
1931                 /*success */
1932
1933                 word = strtok_r(process_name, "/", &proc_name_ptr);
1934                 while (word != NULL) {
1935                         memset(app_name, 0, 256);
1936                         snprintf(app_name, 256, "%s", word);
1937                         word = strtok_r(NULL, "/", &proc_name_ptr);
1938                 }
1939                 quark_app_unique_name = g_quark_from_string(app_name);
1940         } else {                /* failure */
1941
1942                 quark_app_unique_name = g_quark_from_string("unknown");
1943                 memcpy(app_name, "unknown", strlen("unknown") + 1);
1944
1945                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
1946                                           "killed, so we failed to get proc file(%s) \n",
1947                                           pid, proc_file);
1948                 *return_code = -1;      /* -1 means that system 
1949                                            failed internally. */
1950                 return true;
1951         }
1952
1953         ALARM_MGR_LOG_PRINT("called for  app(pid:%d, name=%s)\n",
1954                             pid, app_name);
1955
1956         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
1957              gs_iter = g_slist_next(gs_iter)) {
1958                 entry = gs_iter->data;
1959                 ALARM_MGR_LOG_PRINT("alarm_manager_alarm_get_number_of_ids(): "
1960                                     "app_name=%s,quark_app_unique_name=%s\n",
1961                 app_name, g_quark_to_string(entry->quark_app_unique_name));
1962                 if (strcmp
1963                     (app_name,
1964                      g_quark_to_string(entry->quark_app_unique_name)) == 0
1965                     && strcmp(app_name, "unknown") != 0) {
1966                         (*num_of_ids)++;
1967                         ALARM_MGR_LOG_PRINT("inc number of alarms of app "
1968                                             "(pid:%d, name:%s) is %d\n",
1969                                             pid, app_name, *num_of_ids);
1970                 }
1971         }
1972         *return_code = 0;
1973         ALARM_MGR_LOG_PRINT("number of alarms of app(pid:%d, name:%s) is %d\n",
1974                             pid, app_name, *num_of_ids);
1975         return true;
1976 }
1977
1978 gboolean alarm_manager_alarm_get_list_of_ids(void *pObject, int pid,
1979                                              int max_number_of_ids,
1980                                              GArray **arr, int *num_of_ids,
1981                                              int *return_code)
1982 {
1983         GSList *gs_iter = NULL;
1984         GQuark quark_app_unique_name;   /* the fullpath of pid(pid) is converted
1985                                            to quark value. */
1986         char proc_file[256] = { 0 };
1987         char process_name[512] = { 0 };
1988         char app_name[256] = { 0 };
1989         char *word = NULL;
1990         __alarm_info_t *entry = NULL;
1991         int index = 0;
1992         char *proc_name_ptr = NULL;
1993         int fd;
1994         int ret;
1995         int i = 0;
1996         GArray *garray = NULL;
1997
1998         *return_code = 0;
1999
2000         garray = g_array_new(false, true, sizeof(alarm_id_t));
2001
2002         /* we should check that there is a resource leak.
2003          * Now we don't have free code for g_array_new().
2004          */
2005         if (max_number_of_ids <= 0) {
2006                 *arr = garray;
2007                 ALARM_MGR_EXCEPTION_PRINT("called for  pid(%d), but "
2008                                           "max_number_of_ids(%d) is less than 0.\n",
2009                                           pid, max_number_of_ids);
2010                 return true;
2011         }
2012
2013         /* we should consider to check whether  pid is running or Not
2014          */
2015         memset(process_name, '\0', 512);
2016         memset(proc_file, '\0', 256);
2017         snprintf(proc_file, 256, "/proc/%d/cmdline", pid);
2018
2019         fd = open(proc_file, O_RDONLY);
2020         if (fd > 0) {
2021                 ret = read(fd, process_name, 512);
2022                 close(fd);
2023                 while (process_name[i] != '\0') {
2024                         if (process_name[i] == ' ') {
2025                                 process_name[i] = '\0';
2026                                 break;
2027                         }
2028                         i++;
2029                 }
2030                 /* if (readlink(proc_file, process_name, 256)!=-1) */
2031                 /*success */
2032
2033                 word = strtok_r(process_name, "/", &proc_name_ptr);
2034                 while (word != NULL) {
2035                         memset(app_name, 0, 256);
2036                         snprintf(app_name, 256, "%s", word);
2037                         word = strtok_r(NULL, "/", &proc_name_ptr);
2038                 }
2039                 quark_app_unique_name = g_quark_from_string(app_name);
2040         } else {                /* failure */
2041
2042                 quark_app_unique_name = g_quark_from_string("unknown");
2043                 memcpy(app_name, "unknown", strlen("unknown") + 1);
2044
2045                 ALARM_MGR_EXCEPTION_PRINT("Caution!! app_pid(%d) seems to be "
2046                 "killed, so we failed to get proc file(%s)\n", pid, proc_file);
2047                 *return_code = -1;
2048                 /* -1 means that system failed internally. */
2049                 return true;
2050         }
2051
2052         ALARM_MGR_LOG_PRINT("called for  app(pid:%d, name=%s)\n",
2053                             pid, app_name);
2054
2055         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2056              gs_iter = g_slist_next(gs_iter)) {
2057                 entry = gs_iter->data;
2058                 if (strcmp
2059                     (app_name,
2060                      g_quark_to_string(entry->quark_app_unique_name)) == 0
2061                     && strcmp(app_name, "unknown") != 0) {
2062                         g_array_append_val(garray, entry->alarm_id);
2063                 }
2064         }
2065
2066         *num_of_ids = index;
2067
2068         *arr = garray;
2069
2070         return true;
2071 }
2072
2073 gboolean alarm_manager_alarm_get_info(void *pObject, int pid,
2074                                       alarm_id_t alarm_id, int *start_year,
2075                                       int *start_month, int *start_day,
2076                                       int *start_hour, int *start_min,
2077                                       int *start_sec, int *end_year,
2078                                       int *end_month, int *end_day,
2079                                       int *mode_day_of_week, int *mode_repeat,
2080                                       int *alarm_type, int *reserved_info,
2081                                       int *return_code)
2082 {
2083         ALARM_MGR_LOG_PRINT("called for  pid(%d) and alarm_id(%d)\n", pid,
2084                             alarm_id);
2085
2086         GSList *gs_iter = NULL;
2087         __alarm_info_t *entry = NULL;
2088
2089         alarm_info_t *alarm_info = NULL;
2090         *return_code = 0;
2091
2092         for (gs_iter = alarm_context.alarms; gs_iter != NULL;
2093              gs_iter = g_slist_next(gs_iter)) {
2094                 entry = gs_iter->data;
2095                 if (entry->alarm_id == alarm_id) {
2096                         alarm_info = &(entry->alarm_info);
2097                         break;
2098                 }
2099         }
2100
2101         if (alarm_info == NULL)
2102         {
2103                 ALARM_MGR_EXCEPTION_PRINT("alarm id(%d) was not found\n",
2104                                           alarm_id);
2105                 *return_code = ERR_ALARM_INVALID_PARAM; /*TODO: ERR_ALARM_INVALID_ID?*/
2106         } else {
2107                 ALARM_MGR_LOG_PRINT("alarm was found\n");
2108                 *start_year = alarm_info->start.year;
2109                 *start_month = alarm_info->start.month;
2110                 *start_day = alarm_info->start.day;
2111                 *start_hour = alarm_info->start.hour;
2112                 *start_min = alarm_info->start.min;
2113                 *start_sec = alarm_info->start.sec;
2114
2115                 *end_year = alarm_info->end.year;
2116                 *end_year = alarm_info->end.month;
2117                 *end_year = alarm_info->end.day;
2118
2119                 *mode_day_of_week = alarm_info->mode.u_interval.day_of_week;
2120                 *mode_repeat = alarm_info->mode.repeat;
2121
2122                 *alarm_type = alarm_info->alarm_type;
2123                 *reserved_info = alarm_info->reserved_info;
2124
2125                 *return_code = 0;
2126         }
2127         return true;
2128 }
2129
2130 #include "alarm-skeleton.h"
2131
2132 typedef struct AlarmManagerObject AlarmManagerObject;
2133 typedef struct AlarmManagerObjectClass AlarmManagerObjectClass;
2134 GType Server_Object_get_type(void);
2135 struct AlarmManagerObject {
2136         GObject parent;
2137 };
2138 struct AlarmManagerObjectClass {
2139         GObjectClass parent;
2140 };
2141
2142 #define DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT 0
2143
2144 #define ALARM_MANAGER_TYPE_OBJECT              (Server_Object_get_type())
2145 #define ALARM_MANAGER_OBJECT(object)           (G_TYPE_CHECK_INSTANCE_CAST \
2146 ((object), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObject))
2147 #define ALARM_MANAGER_OBJECT_CLASS(klass)      (G_TYPE_CHECK_CLASS_CAST \
2148 ((klass), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObjectClass))
2149 #define ALARM_MANAGER_IS_OBJECT(object)        (G_TYPE_CHECK_INSTANCE_TYPE \
2150 ((object), ALARM_MANAGER_TYPE_OBJECT))
2151 #define ALARM_MANAGER_IS_OBJECT_CLASS(klass)   (G_TYPE_CHECK_CLASS_TYPE \
2152 ((klass), ALARM_MANAGER_TYPE_OBJECT))
2153 #define ALARM_MANAGER_OBJECT_GET_CLASS(obj)    (G_TYPE_INSTANCE_GET_CLASS \
2154 ((obj), ALARM_MANAGER_TYPE_OBJECT, AlarmManagerObjectClass))
2155 G_DEFINE_TYPE(AlarmManagerObject, Server_Object, G_TYPE_OBJECT)
2156 static void Server_Object_init(AlarmManagerObject * obj)
2157 {
2158         ;
2159 }
2160
2161 static void Server_Object_class_init(AlarmManagerObjectClass *klass)
2162 {
2163         ;
2164 }
2165
2166 static void __initialize_timer()
2167 {
2168         struct sigaction sig_timer;
2169         sigemptyset(&sig_timer.sa_mask);
2170         sig_timer.sa_flags = SA_SIGINFO;
2171         sig_timer.sa_sigaction = (void *)__alarm_handler;
2172         sigaction(SIG_TIMER, &sig_timer, NULL);
2173
2174         alarm_context.timer = _alarm_create_timer();
2175
2176 }
2177
2178 static void __initialize_alarm_list()
2179 {
2180
2181         alarm_context.alarms = NULL;
2182         alarm_context.c_due_time = -1;
2183
2184         _load_alarms_from_registry();
2185
2186         __rtc_set();    /*Set RTC1 Alarm with alarm due time for alarm-manager initialization*/
2187
2188         /*alarm boot */
2189 #ifdef __ALARM_BOOT
2190         /*alarm boot */
2191         if (enable_power_on_alarm) {
2192                 /* orginally first arg's value was 21(app_id, WAKEUP_ALARM_APP_ID) in a
2193                  * platform with app-server. because __alarm_power_on(..) fuction don't 
2194                  * use first parameter internally, we set this value to 0(zero)
2195                  */
2196                 __alarm_power_on(0, enable_power_on_alarm, NULL);
2197         }
2198 #endif
2199 }
2200
2201 static void __initialize_scheduled_alarm_lsit()
2202 {
2203         _init_scheduled_alarm_list();
2204 }
2205
2206
2207 static void __hibernation_leave_callback()
2208 {
2209
2210         __initialize_scheduled_alarm_lsit();
2211
2212         __alarm_clean_list();
2213
2214         __initialize_alarm_list();
2215 }
2216
2217 static bool __initialize_noti()
2218 {
2219
2220         int fd = heynoti_init();
2221         if (fd < 0) {
2222                 ALARM_MGR_EXCEPTION_PRINT("fail to heynoti_init\n");
2223                 return false;
2224         }
2225         heynoti_subscribe(fd, "HIBERNATION_LEAVE", __hibernation_leave_callback,
2226                           NULL);
2227         heynoti_attach_handler(fd);
2228
2229         if (vconf_notify_key_changed
2230             (VCONFKEY_SYSTEM_TIMECHANGE, __on_system_time_changed, NULL) < 0) {
2231                 ALARM_MGR_LOG_PRINT(
2232                         "Failed to add callback for time changing event\n");
2233         }
2234         /*system state change noti ÃƒÂ³Â¸Â® */
2235
2236         return true;
2237 }
2238
2239
2240 static DBusHandlerResult __alarm_server_filter(DBusConnection *connection,
2241                                                DBusMessage *message,
2242                                                void *user_data)
2243 {
2244
2245         if (dbus_message_is_signal
2246             (message, DBUS_INTERFACE_DBUS, "NameOwnerChanged")) {
2247                 char *service;
2248                 char *old_owner;
2249                 char *new_owner;
2250                 GSList *entry;
2251                 __expired_alarm_t *expire_info;
2252
2253                 dbus_message_get_args(message,
2254                                       NULL,
2255                                       DBUS_TYPE_STRING, &service,
2256                                       DBUS_TYPE_STRING, &old_owner,
2257                                       DBUS_TYPE_STRING, &new_owner,
2258                                       DBUS_TYPE_INVALID);
2259
2260                 for (entry = g_expired_alarm_list; entry; entry = entry->next) {
2261                         if (entry->data) {
2262                                 expire_info = (__expired_alarm_t *) entry->data;
2263
2264                                 if (strcmp(expire_info->service_name, service)
2265                                     == 0) {
2266                                         ALARM_MGR_EXCEPTION_PRINT(
2267                                         "__alarm_server_filter : "
2268                                              "service name(%s) alarm_id (%d)\n",
2269                                              expire_info->service_name,\
2270                                              expire_info->alarm_id);
2271
2272                                         __alarm_send_noti_to_application(
2273                                              expire_info->service_name,
2274                                              expire_info->alarm_id);
2275                                         g_expired_alarm_list =
2276                                             g_slist_remove(g_expired_alarm_list,
2277                                                            entry->data);
2278                                         free(expire_info);
2279                                 }
2280                         }
2281                 }
2282
2283                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2284         }
2285
2286         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2287 }
2288
2289 static bool __initialize_dbus()
2290 {
2291         GError *error = NULL;
2292         GObject *obj = NULL;
2293         DBusGConnection *connection = NULL;
2294         DBusError derror;
2295         int request_name_result = 0;
2296
2297         dbus_g_object_type_install_info(ALARM_MANAGER_TYPE_OBJECT,
2298                                         &dbus_glib_alarm_manager_object_info);
2299
2300         connection = dbus_g_bus_get(DBUS_BUS_SYSTEM, &error);
2301         if (!connection) {
2302
2303                 ALARM_MGR_EXCEPTION_PRINT("dbus_g_bus_get failed\n");
2304                 return false;
2305         }
2306
2307         dbus_error_init(&derror);
2308
2309         request_name_result =
2310             dbus_bus_request_name(dbus_g_connection_get_connection(connection),
2311                                   "org.tizen.alarm.manager",
2312                                   DBUS_NAME_FLAG_PROHIBIT_REPLACEMENT, &derror);
2313         if (dbus_error_is_set(&derror)) {       /* failure */
2314                 ALARM_MGR_EXCEPTION_PRINT("Failed to dbus_bus_request_name "
2315                 "(org.tizen.alarm.manager): %s\n", derror.message);
2316                 dbus_error_free(&derror);
2317                 return false;
2318         }
2319
2320         if (!(obj = g_object_new(ALARM_MANAGER_TYPE_OBJECT, NULL))) {
2321                 ALARM_MGR_EXCEPTION_PRINT("Could not allocate new object\n");
2322                 return false;
2323         }
2324
2325         dbus_g_connection_register_g_object(connection,
2326                                             "/org/tizen/alarm/manager",
2327                                             G_OBJECT(obj));
2328
2329         /*dbus_bus_add_match (dbus_g_connection_get_connection(connection),
2330            "type='signal',member='NameOwnerChanged'",NULL); */
2331
2332         dbus_bus_add_match(dbus_g_connection_get_connection(connection),
2333                            "type='signal',sender='" DBUS_SERVICE_DBUS
2334                            "',path='" DBUS_PATH_DBUS
2335                            "',interface='" DBUS_INTERFACE_DBUS
2336                            "',member='NameOwnerChanged'", NULL);
2337
2338         if (!dbus_connection_add_filter
2339             (dbus_g_connection_get_connection(connection),
2340              __alarm_server_filter, NULL, NULL)) {
2341                 ALARM_MGR_EXCEPTION_PRINT("add __expire_alarm_filter failed\n");
2342
2343                 return false;
2344         }
2345
2346         alarm_context.bus = connection;
2347         return true;
2348 }
2349
2350 #define ALARMMGR_DB_FILE "/opt/dbspace/.alarmmgr.db"
2351 sqlite3 *alarmmgr_db;
2352 #define QUERY_CREATE_TABLE_ALARMMGR "create table alarmmgr \
2353                                 (alarm_id integer primary key,\
2354                                                 start integer,\
2355                                                 end integer,\
2356                                                 pid integer,\
2357                                                 app_unique_name text,\
2358                                                 app_service_name text,\
2359                                                 app_service_name_mod text,\
2360                                                 bundle text, \
2361                                                 year integer,\
2362                                                 month integer,\
2363                                                 day integer,\
2364                                                 hour integer,\
2365                                                 min integer,\
2366                                                 sec integer,\
2367                                                 day_of_week integer,\
2368                                                 repeat integer,\
2369                                                 alarm_type integer,\
2370                                                 reserved_info integer,\
2371                                                 dst_service_name text, \
2372                                                 dst_service_name_mod text \
2373                                                 )"
2374
2375 static bool __initialize_db()
2376 {
2377         char *error_message = NULL;
2378         int ret;
2379
2380         if (access("/opt/dbspace/.alarmmgr.db", F_OK) == 0) {
2381                 ret =
2382                     db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db,
2383                                  DB_UTIL_REGISTER_HOOK_METHOD);
2384
2385                 if (ret != SQLITE_OK) {
2386                         ALARM_MGR_EXCEPTION_PRINT(
2387                             "====>>>> connect menu_db [%s] failed!\n",
2388                              ALARMMGR_DB_FILE);
2389                         return false;
2390                 }
2391
2392                 return true;
2393         }
2394
2395         ret =
2396             db_util_open(ALARMMGR_DB_FILE, &alarmmgr_db,
2397                          DB_UTIL_REGISTER_HOOK_METHOD);
2398
2399         if (ret != SQLITE_OK) {
2400                 ALARM_MGR_EXCEPTION_PRINT(
2401                     "====>>>> connect menu_db [%s] failed!\n",
2402                      ALARMMGR_DB_FILE);
2403                 return false;
2404         }
2405
2406         if (SQLITE_OK !=
2407             sqlite3_exec(alarmmgr_db, QUERY_CREATE_TABLE_ALARMMGR, NULL, NULL,\
2408                          &error_message)) {
2409                 ALARM_MGR_EXCEPTION_PRINT("Don't execute query = %s, "
2410                 "error message = %s\n", QUERY_CREATE_TABLE_ALARMMGR, 
2411                                           error_message);
2412                 return false;
2413         }
2414
2415         return true;
2416 }
2417
2418 static void __initialize()
2419 {
2420
2421         g_type_init();
2422 #ifdef __ALARM_BOOT
2423         FILE *fp;
2424         char temp[2];
2425         int size;
2426
2427         fp = fopen("/proc/alarm_boot", "r");
2428         if (fp == NULL)
2429                 alarm_boot = 0;
2430         else {
2431
2432                 size = fread(&temp, 1, 1, fp);
2433                 if (size != 1)
2434                         alarm_boot = 0;
2435                 else {
2436                         temp[1] = 0;
2437                         alarm_boot = atoi(temp);
2438                 }
2439                 fclose(fp);
2440         }
2441 #endif
2442
2443         int fd;
2444         int fd2;
2445         struct rtc_time rtc_tm;
2446         int retval;
2447
2448         fd = open(power_rtc, O_RDONLY);
2449         if (fd < 0) {
2450                 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc0\n");
2451                 return;
2452         }
2453         retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
2454         close(fd);
2455
2456         fd2 = open(default_rtc, O_RDWR);
2457         if (fd2 < 0) {
2458                 ALARM_MGR_EXCEPTION_PRINT("cannot open /dev/rtc1\n");
2459                 return;
2460         }
2461         retval = ioctl(fd2, RTC_SET_TIME, &rtc_tm);
2462         close(fd2);
2463
2464         __initialize_timer();
2465         if (__initialize_dbus() == false) {     /* because dbus's initialize 
2466                                         failed, we cannot continue any more. */
2467                 ALARM_MGR_EXCEPTION_PRINT("because __initialize_dbus failed, "
2468                                           "alarm-server cannot be runned.\n");
2469                 exit(1);
2470         }
2471         __initialize_scheduled_alarm_lsit();
2472         __initialize_db();
2473         __initialize_alarm_list();
2474         __initialize_noti();
2475
2476 }
2477
2478 #ifdef __ALARM_BOOT
2479 static bool __check_false_alarm()
2480 {
2481         time_t current_time;
2482         time_t interval;
2483
2484         time(&current_time);
2485
2486         interval = ab_due_time - current_time;
2487
2488         ALARM_MGR_LOG_PRINT("due_time : %d / current_time %d\n", \
2489                             alarm_context.c_due_time, current_time);
2490
2491         if (interval > 0 && interval <= 30) {
2492                 return true;
2493         } else if (!poweron_alarm_expired) {
2494                 /* originally, first arguement's value was 121(app_id) which means 
2495                  * alarm_booting ui application.and this application's dbus-service 
2496                  * name had a org.tizen.alarmboot.ui in a platform with app-server.
2497                  * so we set "org.tizen.alarmboot.ui.ALARM" instead of 121.
2498                  */
2499                 __alarm_send_noti_to_application(
2500                         WAKEUP_ALARMBOOTING_APP_ID, -1);
2501                 return false;
2502         }
2503
2504         return true;
2505 }
2506 #endif
2507
2508 int main()
2509 {
2510         GMainLoop *mainloop = NULL;
2511
2512         ALARM_MGR_LOG_PRINT("Enter main loop\n");
2513
2514         mainloop = g_main_loop_new(NULL, FALSE);
2515
2516         __initialize();
2517
2518 #ifdef __ALARM_BOOT
2519         if (alarm_boot){
2520                 __check_false_alarm();
2521         }
2522 #endif
2523
2524         g_main_loop_run(mainloop);
2525
2526         return 0;
2527 }