2 * servicedir_listener.c
4 * Created on: Sep 25, 2010
12 #include <sys/types.h>
13 #include <sys/inotify.h>
19 #include "servicedir_listener.h"
21 static pthread_t _listener_thread;
22 static bool _listener_stop_requested;
23 static bool _listener_running = false;
24 static int _dir_handle = -1;
25 static int _inotify_handle = -1;
26 static pthread_mutex_t _listener_mutex;
28 static void init_servicedir_listener() {
29 //_listener_mutex = PTHREAD_MUTEX_INITIALIZER;
30 pthread_mutex_init(&_listener_mutex, NULL);
33 static void deinit_servicedir_listener() {
34 pthread_mutex_destroy(&_listener_mutex);
37 static void *servicedir_listener_thread(void *thread_args_pointer)
42 struct inotify_event *event;
46 * Size of each i-notify event read: size of event structure + filename length that caused the event. (name follows the struct).
47 * We create a buffer that could in theory handle 16 events for files which name would be in maximum 1024 chars. (=CONST_MAX_EXPECTED_INOTIFY_EVENT_SIZE)
49 buffer_max_size = CONST_MAX_INOTIFY_EVENT_COUNT_READ * (sizeof(struct inotify_event) + CONST_MAX_EXPECTED_INOTIFY_EVENT_SIZE);
50 buffer = calloc(1, buffer_max_size);
52 _inotify_handle = inotify_init();
53 if (_inotify_handle >= 0) {
54 _dir_handle = inotify_add_watch(_inotify_handle,
55 CONST_SERVICE_DIRECTORY_LOCATION,
56 IN_CREATE | IN_ATTRIB | IN_MODIFY | IN_DELETE);
57 if (_dir_handle >= 0) {
58 printf("Waiting service directory file events\n");
59 while (_listener_stop_requested == false) {
61 printf("Waiting service directory file event\n");
62 b_count = read(_inotify_handle,
65 printf("Service directory file event received\n");
67 while (ii < b_count) {
68 event = (struct inotify_event *)&buffer[ii];
69 printf("event length = %d\n", event->len);
71 if (event->mask & IN_CREATE) {
72 if ((event->mask & IN_ISDIR) == 0) {
73 printf("file create event: %s\n", event->name);
76 printf("sub-directory create event: %s, no need to process\n", event->name);
79 else if (event->mask & IN_ATTRIB) {
80 if ((event->mask & IN_ISDIR) == 0) {
81 printf("file attribute change event: %s\n", event->name);
84 printf("sub-directory attribute change event: %s, no need to process\n", event->name);
87 else if (event->mask & IN_MODIFY) {
88 if ((event->mask & IN_ISDIR) == 0) {
89 printf("file modify event: %s\n", event->name);
92 printf("sub-directory modify event: %s, no need to process\n", event->name);
95 else if (event->mask & IN_DELETE) {
96 if ((event->mask & IN_ISDIR) == 0) {
97 printf("file delete event: %s\n", event->name);
100 printf("sub-directory delete event: %s, no need to process\n", event->name);
104 ii = ii + sizeof(struct inotify_event) + event->len;
108 printf("Failed to initialize service directory listener: %s, event read failed.\n", CONST_SERVICE_DIRECTORY_LOCATION);
111 printf("Closing service directory listener: %s, close request received.\n", CONST_SERVICE_DIRECTORY_LOCATION);
112 pthread_mutex_lock(&_listener_mutex);
113 if ((_inotify_handle > 0) && (_dir_handle)) {
114 inotify_rm_watch(_inotify_handle, _dir_handle);
117 pthread_mutex_unlock(&_listener_mutex);
120 printf("Failed to initialize service directory listener: %s, event listener start-up error.\n", CONST_SERVICE_DIRECTORY_LOCATION);
122 pthread_mutex_lock(&_listener_mutex);
123 if (_inotify_handle > 0) {
124 close(_inotify_handle);
125 _inotify_handle = -1;
127 pthread_mutex_unlock(&_listener_mutex);
130 printf("Failed to initialize service directory listener: %s\n", CONST_SERVICE_DIRECTORY_LOCATION);
132 if (buffer != NULL) {
138 printf("Failed to initialize service directory listener: %s, buffer allocation error.\n", CONST_SERVICE_DIRECTORY_LOCATION);
140 _listener_running = false;
144 void start_servicedir_listener() {
145 printf("listen_closed_processes()\n");
146 init_servicedir_listener();
147 if (_listener_running == false) {
148 _listener_thread = 0;
149 _listener_stop_requested = false;
150 _listener_running = true;
151 pthread_create(&_listener_thread,
153 servicedir_listener_thread,
158 void stop_servicedir_listener() {
159 _listener_stop_requested = true;
160 pthread_mutex_lock(&_listener_mutex);
161 if ((_inotify_handle > 0) && (_dir_handle)) {
162 inotify_rm_watch(_inotify_handle, _dir_handle);
163 close(_inotify_handle);
165 _inotify_handle = -1;
167 pthread_mutex_unlock(&_listener_mutex);
168 pthread_join(_listener_thread, NULL);
169 deinit_servicedir_listener();