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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
|
/* MiniDLNA media server
* Copyright (C) 2008-2010 Justin Maggard
*
* This file is part of MiniDLNA.
*
* MiniDLNA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* MiniDLNA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include <errno.h>
#include <pthread.h>
#include <unistd.h>
#include <dirent.h>
#include <libgen.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#ifdef HAVE_INOTIFY
#include <sys/resource.h>
#include <poll.h>
#ifdef HAVE_SYS_INOTIFY_H
#include <sys/inotify.h>
#else
#include "linux/inotify.h"
#include "linux/inotify-syscalls.h"
#endif
#endif
#include "libav.h"
#include "upnpglobalvars.h"
#include "monitor.h"
#include "utils.h"
#include "sql.h"
#include "scanner.h"
#include "metadata.h"
#include "albumart.h"
#include "playlist.h"
#include "log.h"
extern time_t next_pl_fill;
#define EVENT_SIZE ( sizeof (struct inotify_event) )
#define BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) )
#define DESIRED_WATCH_LIMIT 65536
#define PATH_BUF_SIZE PATH_MAX
struct watch
{
int wd; /* watch descriptor */
char *path; /* watched path */
struct watch *next;
};
static struct watch *watches;
static struct watch *lastwatch = NULL;
static pthread_t thread_id;
static char *
get_path_from_wd(int wd)
{
struct watch *w = watches;
while( w != NULL )
{
if( w->wd == wd )
return w->path;
w = w->next;
}
return NULL;
}
static unsigned int
next_highest(unsigned int num)
{
num |= num >> 1;
num |= num >> 2;
num |= num >> 4;
num |= num >> 8;
num |= num >> 16;
return ++num;
}
static void
raise_watch_limit(unsigned int limit)
{
FILE *max_watches = fopen("/proc/sys/fs/inotify/max_user_watches", "r+");
if (!max_watches)
return;
if (!limit)
{
if (fscanf(max_watches, "%u", &limit) < 1)
limit = 8192;
rewind(max_watches);
}
fprintf(max_watches, "%u", next_highest(limit));
fclose(max_watches);
}
int
monitor_add_watch(int fd, const char * path)
{
struct watch *nw;
int wd;
wd = inotify_add_watch(fd, path, IN_CREATE|IN_CLOSE_WRITE|IN_DELETE|IN_MOVE);
if( wd < 0 && errno == ENOSPC)
{
raise_watch_limit(0);
wd = inotify_add_watch(fd, path, IN_CREATE|IN_CLOSE_WRITE|IN_DELETE|IN_MOVE);
}
if( wd < 0 )
{
DPRINTF(E_ERROR, L_INOTIFY, "inotify_add_watch(%s) [%s]\n", path, strerror(errno));
return (errno);
}
nw = malloc(sizeof(struct watch));
if( nw == NULL )
{
DPRINTF(E_ERROR, L_INOTIFY, "malloc() error\n");
return (ENOMEM);
}
nw->wd = wd;
nw->next = NULL;
nw->path = strdup(path);
if( watches == NULL )
{
watches = nw;
}
if( lastwatch != NULL )
{
lastwatch->next = nw;
}
lastwatch = nw;
DPRINTF(E_INFO, L_INOTIFY, "Added watch to %s [%d]\n", path, wd);
return (0);
}
int
monitor_remove_watch(int fd, const char * path)
{
struct watch *w;
for( w = watches; w; w = w->next )
{
if( strcmp(path, w->path) == 0 )
return(inotify_rm_watch(fd, w->wd));
}
return 1;
}
static int
inotify_create_watches(int fd)
{
FILE * max_watches;
unsigned int num_watches = 0, watch_limit;
char **result;
int i, rows = 0;
struct media_dir_s * media_path;
for( media_path = media_dirs; media_path != NULL; media_path = media_path->next )
{
DPRINTF(E_DEBUG, L_INOTIFY, "Add watch to %s\n", media_path->path);
monitor_add_watch(fd, media_path->path);
num_watches++;
}
sql_get_table(db, "SELECT PATH from DETAILS where MIME is NULL and PATH is not NULL", &result, &rows, NULL);
for( i=1; i <= rows; i++ )
{
DPRINTF(E_DEBUG, L_INOTIFY, "Add watch to %s\n", result[i]);
monitor_add_watch(fd, result[i]);
num_watches++;
}
sqlite3_free_table(result);
max_watches = fopen("/proc/sys/fs/inotify/max_user_watches", "r");
if( max_watches )
{
if( fscanf(max_watches, "%10u", &watch_limit) < 1 )
watch_limit = 8192;
fclose(max_watches);
if( (watch_limit < DESIRED_WATCH_LIMIT) || (watch_limit < (num_watches*4/3)) )
{
if (access("/proc/sys/fs/inotify/max_user_watches", W_OK) == 0)
{
if( DESIRED_WATCH_LIMIT >= (num_watches*3/4) )
{
raise_watch_limit(8191U);
}
else if( next_highest(num_watches) >= (num_watches*3/4) )
{
raise_watch_limit(num_watches);
}
else
{
raise_watch_limit(next_highest(num_watches));
}
}
else
{
DPRINTF(E_WARN, L_INOTIFY, "WARNING: Inotify max_user_watches [%u] is low or close to the number of used watches [%u] "
"and I do not have permission to increase this limit. Please do so manually by "
"writing a higher value into /proc/sys/fs/inotify/max_user_watches.\n", watch_limit, num_watches);
}
}
}
else
{
DPRINTF(E_WARN, L_INOTIFY, "WARNING: Could not read inotify max_user_watches! "
"Hopefully it is enough to cover %u current directories plus any new ones added.\n", num_watches);
}
return rows;
}
static int
inotify_remove_watches(int fd)
{
struct watch *w = watches;
struct watch *last_w;
int rm_watches = 0;
while( w )
{
last_w = w;
inotify_rm_watch(fd, w->wd);
free(w->path);
rm_watches++;
w = w->next;
free(last_w);
}
return rm_watches;
}
static void *
inotify_thread(void *arg)
{
struct pollfd pollfds[1];
char buffer[BUF_LEN];
char path_buf[PATH_MAX];
int length, i = 0;
char * esc_name = NULL;
struct stat st;
sigset_t set;
#ifdef THUMBNAIL_CREATION
char renpath_buf[PATH_MAX];
int cookie = 0;
#endif
sigfillset(&set);
sigdelset(&set, SIGCHLD);
pthread_sigmask(SIG_BLOCK, &set, NULL);
pollfds[0].fd = inotify_init();
pollfds[0].events = POLLIN;
if ( pollfds[0].fd < 0 )
DPRINTF(E_ERROR, L_INOTIFY, "inotify_init() failed!\n");
inotify_create_watches(pollfds[0].fd);
if (setpriority(PRIO_PROCESS, 0, 19) == -1)
DPRINTF(E_WARN, L_INOTIFY, "Failed to reduce inotify thread priority\n");
sqlite3_release_memory(1<<31);
while( !quitting )
{
int timeout = -1;
if (next_pl_fill)
{
time_t diff = next_pl_fill - time(NULL);
if (diff < 0)
timeout = 0;
else
timeout = diff * 1000;
}
length = poll(pollfds, 1, timeout);
if( !length )
{
if( next_pl_fill && (time(NULL) >= next_pl_fill) )
{
fill_playlists();
next_pl_fill = 0;
}
continue;
}
else if( length < 0 )
{
if( (errno == EINTR) || (errno == EAGAIN) )
continue;
else
DPRINTF(E_ERROR, L_INOTIFY, "read failed!\n");
}
else
{
length = read(pollfds[0].fd, buffer, BUF_LEN);
buffer[BUF_LEN-1] = '\0';
}
i = 0;
while( !quitting && i < length )
{
struct inotify_event * event = (struct inotify_event *) &buffer[i];
if( event->len )
{
if( *(event->name) == '.' )
{
i += EVENT_SIZE + event->len;
continue;
}
esc_name = modifyString(strdup(event->name), "&", "&amp;", 0);
snprintf(path_buf, sizeof(path_buf), "%s/%s", get_path_from_wd(event->wd), event->name);
if ( event->mask & IN_ISDIR && (event->mask & (IN_CREATE|IN_MOVED_TO)) )
{
DPRINTF(E_DEBUG, L_INOTIFY, "The directory %s was %s.\n",
path_buf, (event->mask & IN_MOVED_TO ? "moved here" : "created"));
#ifdef THUMBNAIL_CREATION
/* We do not want to regenerate the thumbnails if e rename a directory.
We should keep at least four cookies/olddir since IN_MOVED_FROM/IN_MOVED_TO may
not arrive in sequence, but one should cover most cases */
if (event->cookie == cookie && event->mask & IN_MOVED_TO)
{
DPRINTF(E_DEBUG, L_INOTIFY, "Directory rename: %s -> %s \n", renpath_buf, path_buf);
rename_artcache_dir(renpath_buf, path_buf);
}
#endif
monitor_insert_directory(pollfds[0].fd, esc_name, path_buf);
}
else if ( (event->mask & (IN_CLOSE_WRITE|IN_MOVED_TO|IN_CREATE)) &&
(lstat(path_buf, &st) == 0) )
{
if( (event->mask & (IN_MOVED_TO|IN_CREATE)) && (S_ISLNK(st.st_mode) || st.st_nlink > 1) )
{
DPRINTF(E_DEBUG, L_INOTIFY, "The %s link %s was %s.\n",
(S_ISLNK(st.st_mode) ? "symbolic" : "hard"),
path_buf, (event->mask & IN_MOVED_TO ? "moved here" : "created"));
if( stat(path_buf, &st) == 0 && S_ISDIR(st.st_mode) )
monitor_insert_directory(pollfds[0].fd, esc_name, path_buf);
else
monitor_insert_file(esc_name, path_buf);
}
else if( event->mask & (IN_CLOSE_WRITE|IN_MOVED_TO) && st.st_size > 0 )
{
if( (event->mask & IN_MOVED_TO) ||
(sql_get_int_field(db, "SELECT TIMESTAMP from DETAILS where PATH = '%q'", path_buf) != st.st_mtime) )
{
DPRINTF(E_DEBUG, L_INOTIFY, "The file %s was %s.\n",
path_buf, (event->mask & IN_MOVED_TO ? "moved here" : "changed"));
monitor_insert_file(esc_name, path_buf);
}
}
}
else if ( event->mask & (IN_DELETE|IN_MOVED_FROM) )
{
DPRINTF(E_DEBUG, L_INOTIFY, "The %s %s was %s.\n",
(event->mask & IN_ISDIR ? "directory" : "file"),
path_buf, (event->mask & IN_MOVED_FROM ? "moved away" : "deleted"));
if ( event->mask & IN_ISDIR )
#ifdef THUMBNAIL_CREATION
{
if ( event->mask & IN_MOVED_FROM )
{
strncpy(renpath_buf, path_buf, sizeof(renpath_buf));
cookie = event->cookie;
}
#endif
monitor_remove_directory(pollfds[0].fd, path_buf);
#ifdef THUMBNAIL_CREATION
}
#endif
else
{
monitor_remove_file(path_buf);
/*
* When a symlink to a directory is deleted
* we cannot tell it from a regular file deletion
* to prevent its children from becoming orphans
* we delete the whole tree when it exists
*/
monitor_remove_tree(path_buf);
}
}
free(esc_name);
}
i += EVENT_SIZE + event->len;
}
}
inotify_remove_watches(pollfds[0].fd);
close(pollfds[0].fd);
return 0;
}
void
monitor_start(void)
{
if (!sqlite3_threadsafe() || sqlite3_libversion_number() < 3005001) {
DPRINTF(E_ERROR, L_GENERAL, "SQLite library is not threadsafe!"
"Inotify will be disabled.\n");
return;
}
if (pthread_create(&thread_id, NULL, inotify_thread, NULL) != 0)
DPRINTF(E_FATAL, L_GENERAL, "pthread_create() failed [%s]\n",
strerror(errno));
}
void
monitor_stop(void)
{
if (thread_id != 0) {
pthread_kill(thread_id, SIGCHLD);
pthread_join(thread_id, NULL);
}
}
|