summaryrefslogtreecommitdiff
path: root/vconf-kdb.c
blob: f30cc4e66cf8171014744961a4e4ae764f987383 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * libslp-setting
 *
 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact: Hakjoo Ko <hakjoo.ko@samsung.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#include <sys/types.h>
#include <sys/inotify.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <linux/version.h>
#include <stdlib.h>
#include <pthread.h>
#include <glib.h>
#include "vconf-internals.h"

#include <glib.h>

#define INOTY_EVENT_MASK   (IN_CLOSE_WRITE | IN_DELETE_SELF)

/* inotify */
struct noti_node {
	int wd;
	char *keyname;
	vconf_callback_fn cb;
	void *cb_data;
	struct noti_node *next;
};
typedef struct noti_node notilist;
static GList *g_notilist;

static int _vconf_inoti_comp_with_wd(gconstpointer a, gconstpointer b)
{
	int r;

	notilist *key1 = (notilist *) a;
	notilist *key2 = (notilist *) b;

	r = key1->wd - key2->wd;
	return r;
}

static int _vconf_inoti_comp_with_wd_cb(gconstpointer a, gconstpointer b)
{
	int r;

	notilist *key1 = (notilist *) a;
	notilist *key2 = (notilist *) b;

	r = key1->wd - key2->wd;
	if (r != 0)
		return r;

	r = (int)(key1->cb - key2->cb);
	return r;
}

static int _kdb_inoti_fd;

static pthread_mutex_t _kdb_inoti_fd_mutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_mutex_t _kdb_g_ns_mutex = PTHREAD_MUTEX_INITIALIZER;

static GSource *_kdb_handler;

static gboolean _vconf_kdb_gio_cb(GIOChannel *src, GIOCondition cond, gpointer data)
{
	int fd, r;
	struct inotify_event ie;

	INFO("vconf noti function");

	fd = g_io_channel_unix_get_fd(src);
	r = read(fd, &ie, sizeof(ie));

	while (r > 0) {
		INFO("read event from GIOChannel. pid : %d", getpid());

		if (g_notilist) {

			struct noti_node *t = NULL;
			GList *noti_list = NULL;
			keynode_t* keynode = NULL;

			retvm_if(!(ie.mask & INOTY_EVENT_MASK), TRUE,
				"Invalid argument: ie.mask(%d), ie.len(%d)",
				 ie.mask, ie.len);

			/* pthread_mutex_lock(&_kdb_g_ns_mutex); */

			noti_list = g_list_first(g_notilist);

			while (noti_list) {
				t = noti_list->data;

				keynode_t* keynode = _vconf_keynode_new();
				retvm_if(keynode == NULL, TRUE, "key malloc fail");

				if( (t) && (t->wd == ie.wd) ) {
					if ((ie.mask & IN_DELETE_SELF)) {
						INFO("Notify that key(%s) is deleted", t->keyname);
						_vconf_keynode_set_keyname(keynode, (const char *)t->keyname);
						_vconf_keynode_set_null(keynode);
						t->cb(keynode, t->cb_data);
						_vconf_kdb_del_notify(t->keyname, t->cb);
					} else {
						_vconf_keynode_set_keyname(keynode, t->keyname);
						_vconf_get_key(keynode);
						t->cb(keynode, t->cb_data);
					}
				}

				_vconf_keynode_free(keynode);

				noti_list = g_list_next(noti_list);
			}
		}

		if (ie.len > 0)
			lseek(fd, ie.len, SEEK_CUR);

		r = read(fd, &ie, sizeof(ie));
	}
	return TRUE;
}

static int _vconf_kdb_noti_init(void)
{
	GIOChannel *gio;

	pthread_mutex_lock(&_kdb_inoti_fd_mutex);

	retm_if(0 < _kdb_inoti_fd, "Error : invalid _kdb_inoti_fd");

	_kdb_inoti_fd = inotify_init();
	if (_kdb_inoti_fd == -1) {
		char err_buf[100] = { 0, };
		strerror_r(errno, err_buf, sizeof(err_buf));
		ERR("inotify init: %s", err_buf);
		pthread_mutex_unlock(&_kdb_inoti_fd_mutex);
		return VCONF_ERROR;
	}

	fcntl(_kdb_inoti_fd, F_SETFD, FD_CLOEXEC);
	fcntl(_kdb_inoti_fd, F_SETFL, O_NONBLOCK);

	pthread_mutex_unlock(&_kdb_inoti_fd_mutex);

	gio = g_io_channel_unix_new(_kdb_inoti_fd);
	retvm_if(gio == NULL, -1, "Error: create a new GIOChannel");

	g_io_channel_set_flags(gio, G_IO_FLAG_NONBLOCK, NULL);

	_kdb_handler = g_io_create_watch(gio, G_IO_IN);
	g_source_set_callback(_kdb_handler, (GSourceFunc) _vconf_kdb_gio_cb, NULL, NULL);
	g_source_attach(_kdb_handler, NULL);
	g_io_channel_unref(gio);
	g_source_unref(_kdb_handler);

	return VCONF_OK;
}

int
_vconf_kdb_add_notify(const char *keyname, vconf_callback_fn cb, void *data)
{
	char path[KEY_PATH];
	int wd, r;
	struct noti_node t, *n;
	char err_buf[ERR_LEN] = { 0, };
	int ret = 0;
	GList *list = NULL;
	int func_ret = VCONF_OK;

	retvm_if((keyname == NULL || cb == NULL), VCONF_ERROR,
		"_vconf_kdb_add_notify : Invalid argument - keyname(%s) cb(%p)",
		 keyname, cb);

	if (_kdb_inoti_fd <= 0)
		if (_vconf_kdb_noti_init())
			return VCONF_ERROR;

	ret = _vconf_get_key_path((char*)keyname, path);
	retvm_if(ret != VCONF_OK, VCONF_ERROR, "Invalid argument: key is not valid");

	if (0 != access(path, F_OK)) {
		if (errno == ENOENT) {
			ERR("_vconf_kdb_add_notify : Key(%s) does not exist", keyname);
			return VCONF_ERROR;
		}
	}

	wd = inotify_add_watch(_kdb_inoti_fd, path, INOTY_EVENT_MASK);
	if (wd == -1) {
		strerror_r(errno, err_buf, sizeof(err_buf));
		ERR("_vconf_kdb_add_notify : add noti(%s)", err_buf);
		return VCONF_ERROR;
	}

	t.wd = wd;
	t.cb = cb;

	pthread_mutex_lock(&_kdb_g_ns_mutex);

	list = g_list_find_custom(g_notilist, &t, (GCompareFunc)_vconf_inoti_comp_with_wd_cb);
	if (list) {
		ERR("_vconf_kdb_add_notify : key(%s) has same callback(%p)", keyname, cb);
		errno = EALREADY;
		func_ret = VCONF_ERROR;
		goto out_func;
	}

	n = calloc(1, sizeof(notilist));
	if (n == NULL) {
		strerror_r(errno, err_buf, sizeof(err_buf));
		ERR("_vconf_kdb_add_notify : add noti(%s)", err_buf);
		func_ret = VCONF_ERROR;
		goto out_func;
	}

	n->wd = wd;
	n->keyname = strndup(keyname, BUF_LEN);
	n->cb_data = data;
	n->cb = cb;

	g_notilist = g_list_append(g_notilist, n);
	if(!g_notilist) {
		ERR("g_list_append fail");
	}

	INFO("cb(%p) is added for %s. tot cb cnt : %d\n", cb, n->keyname, g_list_length(g_notilist));

out_func:
	pthread_mutex_unlock(&_kdb_g_ns_mutex);

	return func_ret;
}

int
_vconf_kdb_del_notify(const char *keyname, vconf_callback_fn cb)
{
	int wd = 0;
	int r = 0;
	struct noti_node *n = NULL;
	struct noti_node t;
	char path[KEY_PATH] = { 0, };
	char err_buf[ERR_LEN] = { 0, };
	int del = 0;
	int remain = 0;
	int ret = 0;
	int func_ret = VCONF_OK;
	GList *noti_list;

	retvm_if(keyname == NULL, VCONF_ERROR, "Invalid argument: keyname(%s)", keyname);
	retvm_if(_kdb_inoti_fd == 0, VCONF_ERROR, "Invalid operation: not exist anything for inotify");

	ret = _vconf_get_key_path((char*)keyname, path);
	retvm_if(ret != VCONF_OK, VCONF_ERROR, "Invalid argument: key is not valid");

	/* get wd */
	wd = inotify_add_watch(_kdb_inoti_fd, path, INOTY_EVENT_MASK);
	if (wd == -1) {
		strerror_r(errno, err_buf, sizeof(err_buf));
		ERR("Error: inotify_add_watch() [%s]: %s", path, err_buf);
		return VCONF_ERROR;
	}

	pthread_mutex_lock(&_kdb_g_ns_mutex);

	t.wd = wd;
	t.cb = cb;

	noti_list = g_list_find_custom(g_notilist, &t, (GCompareFunc)_vconf_inoti_comp_with_wd_cb);
	if(noti_list) {
		del++;

		n = noti_list->data;
		g_notilist = g_list_remove(g_notilist, n);
		g_free(n->keyname);
		g_free(n);

		INFO("key(%s) cb is removed. remained noti list total length(%d)",
			keyname, g_list_length(g_notilist));
	}

	noti_list = NULL;
	noti_list = g_list_find_custom(g_notilist, &t, (GCompareFunc)_vconf_inoti_comp_with_wd);
	if(noti_list == NULL) {
		INFO("all noti for keyname(%s)/wd(%d) is removed", keyname, wd);

		r = inotify_rm_watch(_kdb_inoti_fd, wd);
		if(r == -1) {
			strerror_r(errno, err_buf, sizeof(err_buf));
			ERR("Error: inotify_rm_watch [%s]: %s", keyname, err_buf);
			func_ret = VCONF_ERROR;
		}
	}

	if(g_list_length(g_notilist) == 0) {
		close(_kdb_inoti_fd);
		_kdb_inoti_fd = 0;

		g_source_destroy(_kdb_handler);
		_kdb_handler = NULL;

		g_list_free(g_notilist);
		g_notilist = NULL;

		INFO("all noti list is freed");
	}

	pthread_mutex_unlock(&_kdb_g_ns_mutex);

	if(del == 0) {
		ERR("Error: nothing deleted");
		errno = ENOENT;
		func_ret = VCONF_ERROR;
	}

	return func_ret;
}