sensord: termniate sensor daemon instantly when it receive signal
[platform/core/system/sensord.git] / src / server / main.cpp
1 /*
2  * sensord
3  *
4  * Copyright (c) 2014 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 <signal.h>
21 #include <sensor_log.h>
22 #include <server.h>
23 #include <dbus_util.h>
24 #include <sensor_loader.h>
25 #include <string>
26
27 #define CAL_NODE_PATH "/sys/class/sensors/ssp_sensor/set_cal_data"
28 #define SET_CAL 1
29
30 #define TIMEOUT 10
31
32 static void sig_term_handler(int signo, siginfo_t *info, void *data)
33 {
34         char proc_name[NAME_MAX];
35
36         get_proc_name(info->si_pid, proc_name);
37
38         _E("Received SIGNAL(%d) from %s(%d)\n", signo, proc_name, info->si_pid);
39
40         //server::get_instance().stop();
41         raise(SIGKILL);
42 }
43
44 static void signal_init(void)
45 {
46         struct sigaction sig_act;
47         memset(&sig_act, 0, sizeof(struct sigaction));
48
49         sig_act.sa_handler = SIG_IGN;
50         sigaction(SIGCHLD, &sig_act, NULL);
51         sigaction(SIGPIPE, &sig_act, NULL);
52
53         sig_act.sa_handler = NULL;
54         sig_act.sa_sigaction = sig_term_handler;
55         sig_act.sa_flags = SA_SIGINFO;
56         sigaction(SIGTERM, &sig_act, NULL);
57         sigaction(SIGABRT, &sig_act, NULL);
58         sigaction(SIGINT, &sig_act, NULL);
59 }
60
61 static void set_cal_data(void)
62 {
63         FILE *fp = fopen(CAL_NODE_PATH, "w");
64
65         if (!fp) {
66                 _I("Not support calibration_node");
67                 return;
68         }
69
70         fprintf(fp, "%d", SET_CAL);
71
72         if (fp)
73                 fclose(fp);
74
75         _I("Succeeded to set calibration data");
76
77         return;
78 }
79
80 static gboolean terminate(gpointer data)
81 {
82         std::vector<sensor_base *> sensors = sensor_loader::get_instance().get_sensors(ALL_SENSOR);
83
84         if (sensors.size() == 0) {
85                 _I("Terminating sensord..");
86                 server::get_instance().stop();
87         }
88
89         return FALSE;
90 }
91
92 int main(int argc, char *argv[])
93 {
94         _I("Sensord started");
95
96         signal_init();
97
98         init_dbus();
99
100         set_cal_data();
101
102         /* TODO: loading sequence has to be moved to server */
103         sensor_loader::get_instance().load();
104         g_timeout_add_seconds(TIMEOUT, terminate, NULL);
105
106         server::get_instance().run();
107         server::get_instance().stop();
108
109         fini_dbus();
110
111         _I("Sensord terminated");
112         return 0;
113 }