Merge branch 'devel/tizen' into tizen
[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 <sensor_log.h>
21 #include <dbus_util.h>
22 #include <new>
23 #include <csignal>
24
25 #include "server.h"
26
27 #define NEW_FAIL_LIMIT 3
28
29 using namespace sensor;
30
31 static void on_signal(int signum)
32 {
33         _W("Received SIGNAL(%d : %s)", signum, strsignal(signum));
34         server::stop();
35 }
36
37 static void on_new_failed(void)
38 {
39         static unsigned fail_count = 0;
40         _E("Failed to allocate memory");
41
42         fail_count += 1;
43         if (fail_count >= NEW_FAIL_LIMIT) {
44                 raise(SIGTERM);
45                 return;
46         }
47 }
48
49 int main(int argc, char *argv[])
50 {
51         _I("Started");
52         std::signal(SIGINT, on_signal);
53         std::signal(SIGHUP, on_signal);
54         std::signal(SIGTERM, on_signal);
55         std::signal(SIGQUIT, on_signal);
56         std::signal(SIGABRT, on_signal);
57         std::signal(SIGCHLD, SIG_IGN);
58         std::signal(SIGPIPE, SIG_IGN);
59
60         std::set_new_handler(on_new_failed);
61
62         init_dbus();
63
64         server::run();
65
66         fini_dbus();
67
68         _I("Stopped");
69
70         return 0;
71 }