summaryrefslogtreecommitdiff
path: root/src/common/mf-ug-inotify-handle.c
blob: a45b22ad29952917180d26952260483fc4322fe6 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * myfile
 *
 * Copyright 2012  Samsung Electronics Co., Ltd
 *
 * Licensed under the Flora License, Version 1.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.tizenopensource.org/license
 *
 * 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 <stdio.h>
#include <glib.h>
#include <sys/inotify.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdint.h>
#include <fcntl.h>
#include <errno.h>
#include <pthread.h>

#include "mf-ug-dlog.h"
#include "mf-ug-inotify-handle.h"

#define MF_WATCH_FLAGS \
	IN_CREATE | IN_DELETE | IN_DELETE_SELF | IN_MOVE_SELF | IN_MOVED_FROM | IN_MOVED_TO | IN_CLOSE_WRITE

#define MF_EVENT_SIZE  (sizeof(struct inotify_event))
/** reasonable guess as to size of 1024 events */
#define MF_EVENT_BUF_LEN (1024 * (MF_EVENT_SIZE + 16))

typedef struct _mf_inotify_t {
	int fd;
	int wd;
	gchar *path;
	unsigned int prev_event;
	pthread_t monitor;
	mf_ug_inotify_cb callback;
	void *u_data;
} mf_inotify_t;

static pthread_mutex_t mf_noti_lock;
static mf_inotify_t *g_handle;

static void __mf_ug_inotify_handle_free_handle(void)
{
	pthread_mutex_destroy(&mf_noti_lock);

	if (g_handle) {
		if (g_handle->fd >= 0) {
			close(g_handle->fd);
			g_handle->fd = -1;
		}
		if (g_handle->path) {
			g_free(g_handle->path);
			g_handle->path = NULL;
		}
		g_free(g_handle);
		g_handle = NULL;
	}

	return;
}

static mf_inotify_t *__mf_ug_inotify_handle_init_handle(void)
{
	__mf_ug_inotify_handle_free_handle();
	g_handle = g_new0(mf_inotify_t, 1);

	if (g_handle) {
		g_handle->fd = -1;
		g_handle->wd = -1;
		pthread_mutex_init(&mf_noti_lock, NULL);
	}

	return g_handle;
}

static void __mf_ug_inotify_handle_clean_up_thread(void *data)
{
	pthread_mutex_t *lock = (pthread_mutex_t *) data;
	ug_mf_debug("Thread cancel Clean_up function");
	if (lock) {
		pthread_mutex_unlock(lock);
	}
	return;
}


static gpointer __mf_ug_inotify_handle_watch_thread(gpointer user_data)
{
	mf_inotify_t *handle = (mf_inotify_t *) user_data;
	int oldtype = 0;

	ug_mf_retvm_if(handle == NULL, NULL, "handle is NULL");
	ug_mf_debug("Create __mf_ug_inotify_handle_watch_thread!!! ");

	pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, &oldtype);

	while (1) {
		ssize_t len = 0;
		uint32_t i = 0;
		char event_buff[MF_EVENT_BUF_LEN] = { 0, };

		if (handle->fd < 0) {
			ug_mf_error("fd is not a vaild one");
			pthread_exit(NULL);
		}

		len = read(handle->fd, event_buff, sizeof(event_buff) - 1);
		if (len <= 0 || len > sizeof(event_buff) - 1) {
			ug_mf_error("Fail to read() -fd : %d,  len : %d", handle->fd, len);
			continue;
		}

		while (i < len) {
			struct inotify_event *pevent = (struct inotify_event *)&event_buff[i];
			mf_ug_inotify_event s_event = UG_MF_INOTI_NONE;
			ug_mf_error("mask=%x dir=%s len=%d name=%s",
				    pevent->mask, (pevent->mask & IN_ISDIR) ? "yes" : "no", pevent->len, (pevent->len) ? pevent->name : NULL);

			if (pevent->len && strncmp(pevent->name, ".", 1) == 0) {
				s_event = UG_MF_INOTI_NONE;
			} else if (pevent->mask & IN_ISDIR) {
				if (pevent->mask & IN_DELETE_SELF)
					s_event = UG_MF_INOTI_DELETE_SELF;

				if (pevent->mask & IN_MOVE_SELF)
					s_event = UG_MF_INOTI_MOVE_SELF;

				if (pevent->mask & IN_CREATE)
					s_event = UG_MF_INOTI_CREATE;

				if (pevent->mask & IN_DELETE)
					s_event = UG_MF_INOTI_DELETE;

				if (pevent->mask & IN_MOVED_FROM)
					s_event = UG_MF_INOTI_MOVE_OUT;

				if (pevent->mask & IN_MOVED_TO)
					s_event = UG_MF_INOTI_MOVE_IN;
			} else {
				if (pevent->mask & IN_CREATE) {
					s_event = UG_MF_INOTI_NONE;
					handle->prev_event = IN_CREATE;
				}

				if (pevent->mask & IN_CLOSE_WRITE) {
					if (handle->prev_event == IN_CREATE) {
						s_event = UG_MF_INOTI_CREATE;
					} else {
						s_event = UG_MF_INOTI_MODIFY;
					}
					handle->prev_event = 0;
				}

				if (pevent->mask & IN_DELETE)
					s_event = UG_MF_INOTI_DELETE;

				if (pevent->mask & IN_MOVED_FROM)
					s_event = UG_MF_INOTI_MOVE_OUT;

				if (pevent->mask & IN_MOVED_TO)
					s_event = UG_MF_INOTI_MOVE_IN;
			}

			ug_mf_debug("s_event : %d, prev_event: %x, callback : %p", s_event, handle->prev_event, handle->callback);
			if (s_event != UG_MF_INOTI_NONE) {
				pthread_cleanup_push(__mf_ug_inotify_handle_clean_up_thread, (void *)&mf_noti_lock);
				pthread_mutex_lock(&mf_noti_lock);
				if (handle->callback) {
					handle->callback(s_event, (pevent->len) ? pevent->name : NULL, handle->u_data);
				}
				pthread_mutex_unlock(&mf_noti_lock);
				pthread_cleanup_pop(0);
			}

			i += sizeof(struct inotify_event) + pevent->len;
		}
	}

	ug_mf_debug("end __mf_ug_inotify_handle_watch_thread!!! ");

	return NULL;
}

int mf_ug_inotify_handle_init_inotify(void)
{
	mf_inotify_t *handle = NULL;
	handle = __mf_ug_inotify_handle_init_handle();
	ug_mf_retvm_if(handle == NULL, -1, "fail to __mf_ug_inotify_handle_init_handle()");

	handle->fd = inotify_init();

	if (handle->fd < 0) {
		switch (errno) {
		case EMFILE:
			ug_mf_error("The user limit on the total number of inotify instances has been reached.\n");
			break;
		case ENFILE:
			ug_mf_error("The system limit on the total number of file descriptors has been reached.\n");
			break;
		case ENOMEM:
			ug_mf_error("Insufficient kernel memory is available.\n");
			break;
		default:
			ug_mf_error("Fail to inotify_init(), Unknown error.\n");
			break;
		}
		return -1;
	}
	pthread_create(&handle->monitor, NULL, __mf_ug_inotify_handle_watch_thread, handle);
	return 0;
}

int mf_ug_inotify_handle_add_inotify_watch(const char *path, mf_ug_inotify_cb callback, void *user_data)
{
	mf_inotify_t *handle = NULL;
	handle = g_handle;
	ug_mf_retvm_if(handle == NULL, -1, "handle is NULL");

	if (handle->wd >= 0) {
		ug_mf_warnig("The mf_notify module supports single instance, the watch descript [%d] is removed automatically\n", handle->wd);
		mf_ug_inotify_handle_rm_inotify_watch();
	}

	pthread_mutex_lock(&mf_noti_lock);
	handle->wd = inotify_add_watch(handle->fd, path, MF_WATCH_FLAGS);

	if (handle->wd < 0) {
		switch (errno) {
		case EACCES:
			ug_mf_error("Read access to the given file is not permitted.\n");
			break;
		case EBADF:
			ug_mf_error("The given file descriptor is not valid.\n");
			handle->fd = -1;
			break;
		case EFAULT:
			ug_mf_error("pathname points outside of the process's accessible address space.\n");
			break;
		case EINVAL:
			ug_mf_error("The given event mask contains no legal events; or fd is not an inotify file descriptor.\n");
			break;
		case ENOMEM:
			ug_mf_error("Insufficient kernel memory is available.\n");
			break;
		case ENOSPC:
			ug_mf_error("User limit on the total num of inotify watch was reached or the kernel failed to alloc a needed resource.\n");
			break;
		default:
			ug_mf_error("Fail to ug_ug_mf_inotify_add_watch(), Unknown error.\n");
			break;
		}
		pthread_mutex_unlock(&mf_noti_lock);
		return -1;
	}

	ug_mf_debug("start watching [%s] directory", path);
	if (handle->path) {
		g_free(handle->path);
		handle->path = NULL;
	}
	handle->path = g_strdup(path);
	handle->callback = callback;
	handle->u_data = user_data;
	pthread_mutex_unlock(&mf_noti_lock);

	return 0;
}



int mf_ug_inotify_handle_rm_inotify_watch(void)
{
	int ret = -1;
	mf_inotify_t *handle = NULL;

	handle = g_handle;
	ug_mf_retvm_if(handle == NULL, -1, "handle is NULL");

	if (handle->fd < 0 || handle->wd < 0) {
		ug_mf_warnig("inotify is not initialized or has no watching dir - fd [%d] wd [%d]", handle->fd, handle->wd);
		return 0;
	}

	pthread_mutex_lock(&mf_noti_lock);

	ret = inotify_rm_watch(handle->fd, handle->wd);
	if (ret < 0) {
		switch (errno) {
		case EBADF:
			ug_mf_error("fd is not a valid file descriptor\n");
			handle->fd = -1;
			break;
		case EINVAL:
			ug_mf_error("The watch descriptor wd is not valid; or fd is not an inotify file descriptor.\n");
			handle->wd = -1;
			break;
		default:
			ug_mf_error("Fail to mf_ug_inotify_handle_add_inotify_watch(), Unknown error.\n");
			break;
		}
		pthread_mutex_unlock(&mf_noti_lock);
		return -1;
	}
	ug_mf_debug("stop watching [%s] directory", handle->path);
	if (handle->path) {
		g_free(handle->path);
		handle->path = NULL;
	}
	handle->callback = NULL;
	handle->u_data = NULL;
	handle->wd = -1;
	pthread_mutex_unlock(&mf_noti_lock);

	return 0;
}

void mf_ug_inotify_handle_finalize_inotify(void)
{
	mf_inotify_t *handle = NULL;
	handle = g_handle;

	ug_mf_retm_if(handle == NULL, "handle is NULL");

	if (handle->fd >= 0 && handle->wd >= 0) {
		mf_ug_inotify_handle_rm_inotify_watch();
	}

	pthread_cancel(handle->monitor);
	pthread_join(handle->monitor, NULL);

	__mf_ug_inotify_handle_free_handle();

	return;
}