1283cc67406608bb803b56dea9e92971a48b2fc1
[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 =
79                         (struct inotify_event *)next_event;
80                 gchar *ident = NULL;
81                 gsize len = 0;
82
83                 len = sizeof(*event) + event->len;
84                 if (bytes_read < len)
85                         break;
86
87                 if (event->len)
88                         ident = next_event + sizeof(*event);
89
90                 next_event += len;
91                 bytes_read -= len;
92
93                 (inotify->cb)(event, ident);
94         }
95
96         return true;
97 }
98
99 int inotify_register(const char *path, inotify_event_cb cb)
100 {
101         int fd;
102         stc_inotify_s *inotify;
103
104         if (!cb)
105                 return -EINVAL;
106
107         inotify = g_hash_table_lookup(g_inotify_hash, path);
108         if (inotify) {
109                 inotify->cb = cb;
110                 return 0;
111         }
112
113         inotify = g_try_new0(stc_inotify_s, 1);
114         if (!inotify)
115                 return -ENOMEM;
116
117         fd = inotify_init();
118         if (fd < 0) {
119                 FREE(inotify);
120                 return -EIO;
121         }
122
123         inotify->wd = inotify_add_watch(fd, path, IN_MODIFY);
124         if (inotify->wd < 0) {
125                 STC_LOGE("Failed to create watch [%s]", path);
126                 FREE(inotify);
127                 close(fd);
128                 return -EIO;
129         }
130
131         inotify->channel = g_io_channel_unix_new(fd);
132         if (!inotify->channel) {
133                 STC_LOGE("Failed to create channel");
134                 inotify_rm_watch(fd, inotify->wd);
135                 FREE(inotify);
136                 close(fd);
137                 return -EIO;
138         }
139
140         g_io_channel_set_close_on_unref(inotify->channel, TRUE);
141         g_io_channel_set_encoding(inotify->channel, NULL, NULL);
142         g_io_channel_set_buffered(inotify->channel, FALSE);
143
144         inotify->watch = g_io_add_watch(inotify->channel,
145                                                 G_IO_IN | G_IO_HUP | G_IO_NVAL | G_IO_ERR,
146                                                 __inotify_data, inotify);
147
148         inotify->cb = cb;
149
150         g_hash_table_insert(g_inotify_hash, g_strdup(path), inotify);
151         return 0;
152 }
153
154 void inotify_deregister(const char *path)
155 {
156         stc_inotify_s *inotify;
157
158         inotify = g_hash_table_lookup(g_inotify_hash, path);
159         if (!inotify)
160                 return;
161
162         g_hash_table_remove(g_inotify_hash, path);
163 }
164
165 int inotify_initialize(void)
166 {
167         g_inotify_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
168                                                                                         g_free, __inotify_destroy);
169
170         return 0;
171 }
172
173 void inotify_deinitialize(void)
174 {
175         g_hash_table_destroy(g_inotify_hash);
176 }