Fix static issues
[platform/core/appfw/aul-1.git] / src / aul_watchdog.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #define _GNU_SOURCE
18 #include <stdio.h>
19 #include <stdbool.h>
20 #include <glib.h>
21
22 #include "aul_api.h"
23 #include "aul_util.h"
24 #include "aul_sock.h"
25 #include "aul_error.h"
26 #include "aul_watchdog.h"
27 #include "aul.h"
28
29 typedef struct watchdog_context_s {
30         bool enabled;
31         unsigned int interval;
32         guint timer;
33 } watchdog_context;
34
35 static watchdog_context __context;
36
37 API int aul_watchdog_enable(void)
38 {
39         int ret;
40
41         if (__context.enabled) {
42                 _W("Watchdog is already enabled");
43                 return AUL_R_OK;
44         }
45
46         ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
47                         WATCHDOG_ENABLE, NULL, 0, AUL_SOCK_NOREPLY);
48         if (ret < 0) {
49                 _E("Failed to send the watchdog request. ret(%d)", ret);
50                 return aul_error_convert(ret);
51         }
52
53         __context.enabled = true;
54         _D("[__WATCHDOG__] enabled, result(%d)", ret);
55         return AUL_R_OK;
56 }
57
58 API int aul_watchdog_disable(void)
59 {
60         int ret;
61
62         if (!__context.enabled) {
63                 _W("Watchdog is not enabled");
64                 return AUL_R_ERROR;
65         }
66
67         ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
68                         WATCHDOG_DISABLE, NULL, 0, AUL_SOCK_NOREPLY);
69         if (ret < 0) {
70                 _E("Failed to send the watchdog request. ret(%d)", ret);
71                 return aul_error_convert(ret);
72         }
73
74         __context.enabled = false;
75         _D("[__WATCHDOG__] disabled, result(%d)", ret);
76         return AUL_R_OK;
77 }
78
79 API int aul_watchdog_kick(void)
80 {
81         int ret;
82
83         if (!__context.enabled) {
84                 _W("Watchdog is not enabled");
85                 return AUL_R_ERROR;
86         }
87
88         ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
89                         WATCHDOG_KICK, NULL, 0, AUL_SOCK_NOREPLY);
90         if (ret < 0) {
91                 _E("Failed to send the watchdog request. ret(%d)", ret);
92                 return aul_error_convert(ret);
93         }
94
95         aul_watchdog_stop();
96         aul_watchdog_start(__context.interval);
97
98         _D("[__WATCHDOG__] kicked, result(%d)", ret);
99         return AUL_R_OK;
100 }
101
102 static int __watchdog_ping(void)
103 {
104         int ret;
105
106         ret = aul_sock_send_raw(AUL_UTIL_PID, getuid(),
107                         WATCHDOG_PING, NULL, 0, AUL_SOCK_NOREPLY);
108         if (ret < 0) {
109                 _E("Failed to send watchdog ping. ret(%d)", ret);
110                 return aul_error_convert(ret);
111         }
112
113         return AUL_R_OK;
114 }
115
116 static gboolean __watchdog_notify_handler(gpointer data)
117 {
118         int ret;
119
120         ret = __watchdog_ping();
121         _W("[__WATCHDOG__] Ping(%d). result(%d)", getpid(), ret);
122
123         return G_SOURCE_CONTINUE;
124 }
125
126 void aul_watchdog_start(unsigned int interval)
127 {
128         if (interval == 0) {
129                 _E("Invalid parameter");
130                 return;
131         }
132
133         if (__context.timer) {
134                 _W("Timer already exists");
135                 return;
136         }
137
138         __context.enabled = true;
139         __context.interval = interval;
140         __context.timer = g_timeout_add(interval,
141                         __watchdog_notify_handler, NULL);
142
143         __watchdog_notify_handler(NULL);
144 }
145
146 void aul_watchdog_stop(void)
147 {
148         if (!__context.timer)
149                 return;
150
151         g_source_remove(__context.timer);
152         __context.timer = 0;
153 }