Initialize Tizen 2.3
[framework/system/deviced.git] / src / core / main.c
1 /*
2  * deviced
3  *
4  * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the License);
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <sys/reboot.h>
23
24 #include "display/core.h"
25 #include "log.h"
26 #include "data.h"
27 #include "edbus-handler.h"
28 #include "devices.h"
29 #include "shared/dbus.h"
30 #include "device-notifier.h"
31
32 #define PIDFILE_PATH            "/var/run/.deviced.pid"
33
34 static void init_ad(struct main_data *ad)
35 {
36         memset(ad, 0x0, sizeof(struct main_data));
37 }
38
39 static void writepid(char *pidpath)
40 {
41         FILE *fp;
42
43         fp = fopen(pidpath, "w");
44         if (fp != NULL) {
45                 fprintf(fp, "%d", getpid());
46                 fclose(fp);
47         }
48 }
49
50 static void sig_quit(int signo)
51 {
52         _D("received SIGTERM signal %d", signo);
53 }
54
55 static void sig_usr1(int signo)
56 {
57         _D("received SIGUSR1 signal %d, deviced'll be finished!", signo);
58
59         ecore_main_loop_quit();
60 }
61
62 static void check_systemd_active(void)
63 {
64         DBusError err;
65         DBusMessage *msg;
66         DBusMessageIter iter, sub;
67         const char *state;
68         char *pa[2];
69
70         pa[0] = "org.freedesktop.systemd1.Unit";
71         pa[1] = "ActiveState";
72
73         _I("%s %s", pa[0], pa[1]);
74
75         msg = dbus_method_sync_with_reply("org.freedesktop.systemd1",
76                         "/org/freedesktop/systemd1/unit/default_2etarget",
77                         "org.freedesktop.DBus.Properties",
78                         "Get", "ss", pa);
79         if (!msg)
80                 return;
81
82         dbus_error_init(&err);
83
84         if (!dbus_message_iter_init(msg, &iter) ||
85             dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_VARIANT)
86                 goto out;
87
88         dbus_message_iter_recurse(&iter, &sub);
89
90         if (dbus_message_iter_get_arg_type(&sub) != DBUS_TYPE_STRING)
91                 goto out;
92
93         dbus_message_iter_get_basic(&sub, &state);
94
95         if (strncmp(state, "active", 6) == 0) {
96                 _I("notify relaunch");
97                 device_notify(DEVICE_NOTIFIER_BOOTING_DONE, (void *)TRUE);
98         }
99 out:
100         dbus_message_unref(msg);
101         dbus_error_free(&err);
102 }
103
104 static int system_main(int argc, char **argv)
105 {
106         struct main_data ad;
107
108         init_ad(&ad);
109         edbus_init(&ad);
110         devices_init(&ad);
111         check_systemd_active();
112         signal(SIGTERM, sig_quit);
113         signal(SIGUSR1, sig_usr1);
114
115         ecore_main_loop_begin();
116
117         devices_exit(&ad);
118         edbus_exit(&ad);
119         ecore_shutdown();
120         return 0;
121 }
122
123 int main(int argc, char **argv)
124 {
125         writepid(PIDFILE_PATH);
126         ecore_init();
127         return system_main(argc, argv);
128 }