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
|
/*******************************************************************************
Copyright (c) 2011 Dmitry Matveev <me@dmitrymatveev.co.uk>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*******************************************************************************/
#include <stddef.h> /* NULL */
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/event.h>
#include "config.h"
#include "sys/inotify.h"
#include "utils.h"
#include "worker.h"
#define WORKER_SZ 100
static worker* volatile workers[WORKER_SZ] = {NULL};
static pthread_mutex_t workers_mutex = PTHREAD_MUTEX_INITIALIZER;
/**
* Create a new inotify instance.
*
* This function will create a new inotify instance (actually, a worker
* with its own thread). To destroy the instance, just close its file
* descriptor.
*
* @return -1 on failure, a file descriptor on success.
**/
INO_EXPORT int
inotify_init (void)
{
pthread_mutex_lock (&workers_mutex);
int i;
for (i = 0; i < WORKER_SZ; i++) {
if (workers[i] == NULL) {
worker *wrk = worker_create ();
if (wrk != NULL) {
workers[i] = wrk;
/* We can face into situation when there are two workers with
* the same inotify FDs. It usually occurs when a worker fd has
* been closed but the worker has not been removed from a list
* yet. The fd is free, and when we create a new worker, we can
* receive the same fd. So check for duplicates and remove them
* now. */
int j, lfd = wrk->io[INOTIFY_FD];
for (j = 0; j < WORKER_SZ; j++) {
worker *jw = workers[j];
if (jw != NULL && jw->io[INOTIFY_FD] == lfd && jw != wrk) {
workers[j] = NULL;
perror_msg ("Collision found: fd %d", lfd);
}
}
pthread_mutex_unlock (&workers_mutex);
return wrk->io[INOTIFY_FD];
}
}
}
pthread_mutex_unlock (&workers_mutex);
return -1;
}
/**
* Add or modify a watch.
*
* If the watch with a such filename is already exist, its mask will
* be updated. A new watch will be created otherwise.
*
* @param[in] fd A file descriptor of an inotify instance.
* @param[in] name A path to a file to watch.
* @param[in] mask A combination of inotify flags.
* @return id of a watch, -1 on failure.
**/
INO_EXPORT int
inotify_add_watch (int fd,
const char *name,
uint32_t mask)
{
pthread_mutex_lock (&workers_mutex);
/* look up for an appropriate worker */
int i;
for (i = 0; i < WORKER_SZ; i++) {
worker *wrk = workers[i];
if (wrk != NULL && wrk->io[INOTIFY_FD] == fd && wrk->closed == 0
&& is_opened (wrk->io[INOTIFY_FD])) {
pthread_mutex_lock (&wrk->mutex);
if (wrk->closed) {
worker_free (wrk);
pthread_mutex_unlock (&wrk->mutex);
free (wrk);
pthread_mutex_unlock (&workers_mutex);
return -1;
}
worker_cmd_add (&wrk->cmd, name, mask);
safe_write (wrk->io[INOTIFY_FD], "*", 1);
worker_cmd_wait (&wrk->cmd);
int retval = wrk->cmd.retval;
pthread_mutex_unlock (&wrk->mutex);
if (wrk->closed) {
worker_free (wrk);
free (wrk);
}
pthread_mutex_unlock (&workers_mutex);
return retval;
}
}
pthread_mutex_unlock (&workers_mutex);
return -1;
}
/**
* Remove a watch.
*
* Removes a watch and releases all the associated resources.
* Notifications from the watch should not be received by a client
* anymore.
*
* @param[in] fd Inotify instance file descriptor.
* @param[in] wd Watch id.
* @return 0 on success, -1 on failure.
**/
INO_EXPORT int
inotify_rm_watch (int fd,
int wd)
{
assert (fd != -1);
assert (wd != -1);
pthread_mutex_lock (&workers_mutex);
int i;
for (i = 0; i < WORKER_SZ; i++) {
worker *wrk = workers[i];
if (wrk != NULL && wrk->io[INOTIFY_FD] == fd && wrk->closed == 0
&& is_opened (wrk->io[INOTIFY_FD])) {
pthread_mutex_lock (&wrk->mutex);
if (wrk->closed) {
worker_free (wrk);
pthread_mutex_unlock (&wrk->mutex);
free (wrk);
pthread_mutex_unlock (&workers_mutex);
return -1;
}
worker_cmd_remove (&wrk->cmd, wd);
safe_write (wrk->io[INOTIFY_FD], "*", 1);
worker_cmd_wait (&wrk->cmd);
int retval = wrk->cmd.retval;
pthread_mutex_unlock (&wrk->mutex);
if (wrk->closed) {
worker_free (wrk);
free (wrk);
}
pthread_mutex_unlock (&workers_mutex);
return retval;
}
}
pthread_mutex_unlock (&workers_mutex);
return 0;
}
/**
* Erase a worker from a list of workers.
*
* This function does not lock the global array of workers (I assume that
* marking its items as volatile should be enough). Also this function is
* intended to be called from the worker threads only.
*
* @param[in] wrk A pointer to a worker
**/
void
worker_erase (worker *wrk)
{
assert (wrk != NULL);
int i;
for (i = 0; i < WORKER_SZ; i++) {
if (workers[i] == wrk) {
workers[i] = NULL;
break;
}
}
}
|