sensord: terminate sensor daemon safely
[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 <sensor_loader.h>
24 #include <string>
25
26 #define CAL_NODE_PATH "/sys/class/sensors/ssp_sensor/set_cal_data"
27 #define SET_CAL 1
28
29 static void sig_term_handler(int signo, siginfo_t *info, void *data)
30 {
31         char proc_name[NAME_MAX];
32
33         get_proc_name(info->si_pid, proc_name);
34
35         _E("Received SIGTERM(%d) from %s(%d)\n", signo, proc_name, info->si_pid);
36
37         server::get_instance().stop();
38 }
39
40 static void signal_init(void)
41 {
42         struct sigaction sig_act;
43         memset(&sig_act, 0, sizeof(struct sigaction));
44
45         sig_act.sa_handler = SIG_IGN;
46         sigaction(SIGCHLD, &sig_act, NULL);
47         sigaction(SIGPIPE, &sig_act, NULL);
48
49         sig_act.sa_handler = NULL;
50         sig_act.sa_sigaction = sig_term_handler;
51         sig_act.sa_flags = SA_SIGINFO;
52         sigaction(SIGTERM, &sig_act, NULL);
53         sigaction(SIGABRT, &sig_act, NULL);
54         sigaction(SIGINT, &sig_act, NULL);
55 }
56
57 static void set_cal_data(void)
58 {
59         FILE *fp = fopen(CAL_NODE_PATH, "w");
60
61         if (!fp) {
62                 _I("Not support calibration_node");
63                 return;
64         }
65
66         fprintf(fp, "%d", SET_CAL);
67
68         if (fp)
69                 fclose(fp);
70
71         _I("Succeeded to set calibration data");
72
73         return;
74 }
75
76 int main(int argc, char *argv[])
77 {
78         _I("Sensord started");
79
80         signal_init();
81
82         set_cal_data();
83
84         /* TODO: loader has to be moved to server */
85         sensor_loader::get_instance().load();
86
87         server::get_instance().run();
88         server::get_instance().stop();
89
90         _I("Sensord terminated");
91
92         return 0;
93 }