05d10db2090ae3d5bbf3ddebc03e47ca5c33eb03
[platform/core/connectivity/stc-manager.git] / src / helper / helper-inotify.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd.
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 #include <glib.h>
18
19 #include "helper-inotify.h"
20 #include "stc-manager-util.h"
21
22 typedef struct {
23         GIOChannel *channel;
24         uint watch;
25         int wd;
26
27         inotify_event_cb cb;
28 } stc_inotify_s;
29
30 static GHashTable *g_inotify_hash;
31
32 static void __inotify_destroy(gpointer user_data)
33 {
34         int fd;
35         stc_inotify_s *inotify = user_data;
36
37         if (!inotify->channel)
38                 return;
39
40         if (inotify->watch > 0)
41                 g_source_remove(inotify->watch);
42
43         if (inotify->wd >= 0) {
44                 fd = g_io_channel_unix_get_fd(inotify->channel);
45                 inotify_rm_watch(fd, inotify->wd);
46         }
47
48         g_io_channel_unref(inotify->channel);
49 }
50
51 static gboolean __inotify_data(GIOChannel *channel,
52                                 GIOCondition cond, gpointer user_data)
53 {
54         stc_inotify_s *inotify = user_data;
55         char buf[256];
56         char *next_event = buf;
57         gsize bytes_read;
58         GIOStatus status;
59
60         if (cond & (G_IO_NVAL | G_IO_ERR | G_IO_HUP)) {
61                 inotify->watch = 0;
62                 return false;
63         }
64
65         status = g_io_channel_read_chars(channel, buf,
66                                         sizeof(buf), &bytes_read, NULL);
67         if (status != G_IO_STATUS_NORMAL) {
68                 if (status == G_IO_STATUS_AGAIN)
69                         return true;
70                 else {
71                         STC_LOGE("Failed to read from inotify channel");
72                         inotify->watch = 0;
73                         return false;
74                 }
75         }
76
77         while (bytes_read > 0) {
78                 struct inotify_event *event = next_event;
79                 gchar *ident = NULL;
80                 gsize len = 0;
81
82                 len = sizeof(*event) + event->len;
83                 if (bytes_read < len)
84                         break;
85
86                 if (event->len)
87                         ident = next_event + sizeof(*event);
88
89                 next_event += len;
90                 bytes_read -= len;
91
92                 (inotify->cb)(event, ident);
93         }
94
95         return true;
96 }
97
98 int inotify_register(const char *path, inotify_event_cb cb)
99 {
100         int fd;
101         stc_inotify_s *inotify;
102
103         if (!cb)
104                 return -EINVAL;
105
106         inotify = g_hash_table_lookup(g_inotify_hash, path);
107         if (inotify) {
108                 inotify->cb = cb;
109                 return 0;
110         }
111
112         inotify = g_try_new0(stc_inotify_s, 1);
113         if (!inotify)
114                 return -ENOMEM;
115
116         fd = inotify_init();
117         if (fd < 0) {
118                 FREE(inotify);
119                 return -EIO;
120         }
121
122         inotify->wd = inotify_add_watch(fd, path, IN_MODIFY);
123         if (inotify->wd < 0) {
124                 STC_LOGE("Failed to create watch [%s]", path);
125                 FREE(inotify);
126                 close(fd);
127                 return -EIO;
128         }
129
130         inotify->channel = g_io_channel_unix_new(fd);
131         if (!inotify->channel) {
132                 STC_LOGE("Failed to create channel");
133                 inotify_rm_watch(fd, inotify->wd);
134                 FREE(inotify);
135                 close(fd);
136                 return -EIO;
137         }
138
139         g_io_channel_set_close_on_unref(inotify->channel, TRUE);
140         g_io_channel_set_encoding(inotify->channel, NULL, NULL);
141         g_io_channel_set_buffered(inotify->channel, FALSE);
142
143         inotify->watch = g_io_add_watch(inotify->channel,
144                                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
145                                                 __inotify_data, inotify);
146
147         inotify->cb = cb;
148
149         g_hash_table_insert(g_inotify_hash, g_strdup(path), inotify);
150         return 0;
151 }
152
153 void inotify_deregister(const char *path)
154 {
155         stc_inotify_s *inotify;
156
157         inotify = g_hash_table_lookup(g_inotify_hash, path);
158         if (!inotify)
159                 return;
160
161         g_hash_table_remove(g_inotify_hash, path);
162 }
163
164 int inotify_initialize(void)
165 {
166         g_inotify_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
167                                                                                         g_free, __inotify_destroy);
168
169         return 0;
170 }
171
172 void inotify_deinitialize(void)
173 {
174         g_hash_table_destroy(g_inotify_hash);
175 }