opnsense-ports/net/minidlna/files/extra-patch-kqueue
Franco Fichtner a71a049e69 */*: sync with upstream
Taken from: HardenedBSD
2018-06-07 10:01:40 +02:00

564 lines
16 KiB
Text

diff --git configure.ac configure.ac
index f343d21..374c3c3 100644
--- configure.ac
+++ configure.ac
@@ -488,7 +488,7 @@ AC_CHECK_LIB(avahi-client, avahi_threaded_poll_new,
################################################################################################################
### Header checks
-AC_CHECK_HEADERS([arpa/inet.h asm/unistd.h endian.h machine/endian.h fcntl.h libintl.h locale.h netdb.h netinet/in.h stddef.h stdlib.h string.h sys/file.h sys/inotify.h sys/ioctl.h sys/param.h sys/socket.h sys/time.h unistd.h])
+AC_CHECK_HEADERS([arpa/inet.h asm/unistd.h endian.h machine/endian.h fcntl.h libintl.h locale.h netdb.h netinet/in.h stddef.h stdlib.h string.h sys/file.h sys/inotify.h sys/ioctl.h sys/param.h sys/socket.h sys/time.h unistd.h sys/event.h])
AC_CHECK_FUNCS(inotify_init, AC_DEFINE(HAVE_INOTIFY,1,[Whether kernel has inotify support]), [
AC_MSG_CHECKING([for __NR_inotify_init syscall])
diff --git minidlna.c minidlna.c
index 322d04d..ff1bd9d 100644
--- minidlna.c
+++ minidlna.c
@@ -46,6 +46,7 @@
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
+#define FD_SETSIZE 8192
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
@@ -357,6 +358,7 @@ rescan:
open_db(&db);
if (*scanner_pid == 0) /* child (scanner) process */
{
+ DPRINTF(E_DEBUG, L_GENERAL, "Starting scanner in forked child\n");
start_scanner();
sqlite3_close(db);
log_close();
@@ -366,6 +368,7 @@ rescan:
}
else if (*scanner_pid < 0)
{
+ DPRINTF(E_DEBUG, L_GENERAL, "Starting scanner in parent\n");
start_scanner();
}
#else
@@ -1057,6 +1060,15 @@ main(int argc, char **argv)
else if (pthread_create(&inotify_thread, NULL, start_inotify, NULL) != 0)
DPRINTF(E_FATAL, L_GENERAL, "ERROR: pthread_create() failed for start_inotify. EXITING\n");
}
+#elif defined(HAVE_SYS_EVENT_H)
+ if( GETFLAG(INOTIFY_MASK) )
+ {
+ if (!sqlite3_threadsafe() || sqlite3_libversion_number() < 3005001)
+ DPRINTF(E_ERROR, L_GENERAL, "SQLite library is not threadsafe! "
+ "Kqueue will be disabled.\n");
+ else if (pthread_create(&inotify_thread, NULL, start_kqueue, NULL) != 0)
+ DPRINTF(E_FATAL, L_GENERAL, "ERROR: pthread_create() failed for start_kqueue. EXITING\n");
+ }
#endif
smonitor = OpenAndConfMonitorSocket();
diff --git monitor.c monitor.c
index ebe8095..e3eaddc 100644
--- monitor.c
+++ monitor.c
@@ -29,16 +29,21 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
-#ifdef HAVE_INOTIFY
+#if defined(HAVE_INOTIFY) || defined(HAVE_SYS_EVENT_H)
#include <sys/resource.h>
#include <poll.h>
+#ifdef HAVE_INOTIFY
#ifdef HAVE_SYS_INOTIFY_H
#include <sys/inotify.h>
-#else
+#else /*HAVE_SYS_INOTIFY_H*/
#include "linux/inotify.h"
#include "linux/inotify-syscalls.h"
-#endif
-#endif
+#endif /*HAVE_SYS_INOTIFY_H*/
+#else /*HAVE_INOTIFY*/
+#include <sys/event.h>
+#include <fcntl.h>
+#endif /*HAVE_INOTIFY*/
+#endif /*defined(HAVE_INOTIFY) || defined(HAVE_SYS_EVENT_H)*/
#include "libav.h"
#include "upnpglobalvars.h"
@@ -53,12 +58,14 @@
static time_t next_pl_fill = 0;
+#if defined(HAVE_INOTIFY) || defined(HAVE_SYS_EVENT_H)
#ifdef HAVE_INOTIFY
#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
+#endif /*HAVE_INOTIFY*/
struct watch
{
@@ -85,6 +92,7 @@ get_path_from_wd(int wd)
return NULL;
}
+#ifdef HAVE_INOTIFY
static unsigned int
next_highest(unsigned int num)
{
@@ -107,6 +115,7 @@ raise_watch_limit(unsigned int limit)
fprintf(max_watches, "%u", next_highest(limit));
fclose(max_watches);
}
+#endif /*HAVE_INOTIFY*/
static int
add_watch(int fd, const char * path)
@@ -114,6 +123,7 @@ add_watch(int fd, const char * path)
struct watch *nw;
int wd;
+#ifdef HAVE_INOTIFY
wd = inotify_add_watch(fd, path, IN_CREATE|IN_CLOSE_WRITE|IN_DELETE|IN_MOVE);
if( wd < 0 && errno == ENOSPC)
{
@@ -125,6 +135,27 @@ add_watch(int fd, const char * path)
DPRINTF(E_ERROR, L_INOTIFY, "inotify_add_watch(%s) [%s]\n", path, strerror(errno));
return -1;
}
+#else /*HAVE_INOTIFY*/
+ wd = open(path, O_RDONLY);
+ if (wd < 0)
+ {
+ DPRINTF(E_ERROR, L_INOTIFY, "inotify_add_watch[kqueue,open](%s) [%s]\n", path, strerror(errno));
+ return -1;
+ }
+
+ struct kevent ke;
+ EV_SET(&ke, wd,
+ EVFILT_VNODE,
+ EV_ADD | EV_ENABLE | EV_CLEAR,
+ NOTE_DELETE | NOTE_WRITE | NOTE_EXTEND /*| NOTE_ATTRB*/,
+ 0, NULL);
+
+ if( kevent(fd, &ke, 1, NULL, 0, NULL) < 0 )
+ {
+ DPRINTF(E_ERROR, L_INOTIFY, "inotify_add_watch[kqueue](%s) [%s]\n", path, strerror(errno));
+ return -1;
+ }
+#endif /*HAVE_INOTIFY*/
nw = malloc(sizeof(struct watch));
if( nw == NULL )
@@ -158,7 +189,12 @@ remove_watch(int fd, const char * path)
for( w = watches; w; w = w->next )
{
if( strcmp(path, w->path) == 0 )
+#ifdef HAVE_INOTIFY
return(inotify_rm_watch(fd, w->wd));
+#else /*HAVE_INOTIFY*/
+ close(w->wd); /* kqueue cleans up events when handle dies*/
+ return(0);
+#endif /*HAVE_INOTIFY*/
}
return 1;
@@ -167,8 +203,7 @@ remove_watch(int fd, const char * path)
static int
inotify_create_watches(int fd)
{
- FILE * max_watches;
- unsigned int num_watches = 0, watch_limit;
+ unsigned int num_watches = 0;
char **result;
int i, rows = 0;
struct media_dir_s * media_path;
@@ -188,6 +223,10 @@ inotify_create_watches(int fd)
}
sqlite3_free_table(result);
+#ifdef HAVE_INOTIFY
+ FILE * max_watches;
+ unsigned int watch_limit;
+
max_watches = fopen("/proc/sys/fs/inotify/max_user_watches", "r");
if( max_watches )
{
@@ -224,6 +263,7 @@ inotify_create_watches(int fd)
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);
}
+#endif /*HAVE_INOTIFY*/
return rows;
}
@@ -238,7 +278,11 @@ inotify_remove_watches(int fd)
while( w )
{
last_w = w;
+#ifdef HAVE_INOTIFY
inotify_rm_watch(fd, w->wd);
+#else /*HAVE_INOTIFY*/
+ close(w->wd); /*kqueue cleans up after fhandle dies*/
+#endif /*HAVE_INOTIFY*/
free(w->path);
rm_watches++;
w = w->next;
@@ -247,7 +291,7 @@ inotify_remove_watches(int fd)
return rm_watches;
}
-#endif
+#endif /*defined(HAVE_INOTIFY) || defined(HAVE_SYS_EVENT_H)*/
int
monitor_remove_file(const char * path)
@@ -304,6 +348,8 @@ monitor_remove_file(const char * path)
continue;
if( children < 2 )
{
+ sql_exec(db, "DELETE from DETAILS where ID ="
+ " (SELECT DETAIL_ID from OBJECTS where OBJECT_ID = '%s')", result[i]);
sql_exec(db, "DELETE from OBJECTS where OBJECT_ID = '%s'", result[i]);
ptr = strrchr(result[i], '$');
@@ -311,6 +357,8 @@ monitor_remove_file(const char * path)
*ptr = '\0';
if( sql_get_int_field(db, "SELECT count(*) from OBJECTS where PARENT_ID = '%s'", result[i]) == 0 )
{
+ sql_exec(db, "DELETE from DETAILS where ID ="
+ " (SELECT DETAIL_ID from OBJECTS where OBJECT_ID = '%s')", result[i]);
sql_exec(db, "DELETE from OBJECTS where OBJECT_ID = '%s'", result[i]);
}
}
@@ -342,6 +390,7 @@ monitor_insert_file(char * name, const char * path)
media_types types = ALL_MEDIA;
struct media_dir_s * media_path = media_dirs;
struct stat st;
+ struct timeval now;
/* Is it cover art for another file? */
if( is_image(path) )
@@ -405,6 +454,16 @@ monitor_insert_file(char * name, const char * path)
/* If it's already in the database and hasn't been modified, skip it. */
if( stat(path, &st) != 0 )
return -1;
+
+ (void)gettimeofday(&now, NULL);
+ while (now.tv_sec < st.st_mtime + 3)
+ {
+ DPRINTF(E_DEBUG, L_INOTIFY, "Sleeping until %s is stable for a few seconds ...\n", path);
+ sleep(1);
+ (void)gettimeofday(&now, NULL);
+ if (stat(path, &st) != 0)
+ return -1;
+ }
ts = sql_get_int_field(db, "SELECT TIMESTAMP from DETAILS where PATH = '%q'", path);
if( !ts && is_playlist(path) && (sql_get_int_field(db, "SELECT ID from PLAYLISTS where PATH = '%q'", path) > 0) )
@@ -528,7 +587,7 @@ monitor_insert_directory(int fd, char *name, const char * path)
if( fd > 0 )
{
- #ifdef HAVE_INOTIFY
+ #if defined(HAVE_INOTIFY) || defined(HAVE_SYS_EVENT_H)
int wd = add_watch(fd, path);
if( wd == -1 )
{
@@ -538,7 +597,7 @@ monitor_insert_directory(int fd, char *name, const char * path)
{
DPRINTF(E_INFO, L_INOTIFY, "Added watch to %s [%d]\n", path, wd);
}
- #endif
+ #endif /*defined(HAVE_INOTIFY) || defined(HAVE_SYS_EVENT_H)*/
}
media_path = media_dirs;
@@ -604,7 +663,7 @@ monitor_remove_directory(int fd, const char * path)
valid_cache = 0;
if( fd > 0 )
{
- #ifdef HAVE_INOTIFY
+ #if defined(HAVE_INOTIFY) || defined(HAVE_SYS_EVENT_H)
remove_watch(fd, path);
#endif
}
@@ -631,6 +690,7 @@ monitor_remove_directory(int fd, const char * path)
return ret;
}
+#if defined(HAVE_INOTIFY) || defined(HAVE_SYS_EVENT_H)
#ifdef HAVE_INOTIFY
void *
start_inotify(void)
@@ -754,4 +814,232 @@ quitting:
return 0;
}
-#endif
+#else /*HAVE_INOTIFY*/
+void *
+start_kqueue()
+{
+ int global_kqueue_handle = -1;
+
+ global_kqueue_handle = kqueue();
+ if ( global_kqueue_handle < 0 )
+ {
+ DPRINTF(E_ERROR, L_INOTIFY, "kqueue() failed: %s\n", strerror(errno));
+ return 0;
+ }
+
+ while( GETFLAG(SCANNING_MASK) )
+ {
+ if( quitting )
+ goto quitting;
+
+ DPRINTF(E_DEBUG, L_INOTIFY, "..waiting for scanning to complete...\n");
+ sleep(1);
+ }
+
+ DPRINTF(E_DEBUG, L_INOTIFY, "GOING WILD!\n");
+
+ inotify_create_watches(global_kqueue_handle);
+ if (setpriority(PRIO_PROCESS, 0, 19) == -1)
+ DPRINTF(E_WARN, L_INOTIFY, "Failed to reduce kqueue thread priority\n");
+ sqlite3_release_memory(1<<31);
+ av_register_all();
+
+ while( !quitting )
+ {
+ struct kevent ke;
+ if ( kevent(global_kqueue_handle, NULL, 0, &ke, 1, NULL) == -1 )
+ {
+ DPRINTF(E_WARN, L_INOTIFY, "kevent polling failure: %s\n", strerror(errno));
+ continue;
+ }
+
+ /*DPRINTF(E_DEBUG, L_INOTIFY, "GOT KEVENT:\n"
+ "ident=0x%X, filter=0x%X, flags=0x%X, fflags=0x%X, data=0x%X, udata=0x%X\n",
+ ke.ident, ke.filter, ke.flags, ke.fflags, ke.data, ke.udata);*/
+
+ char* dir_path = get_path_from_wd(ke.ident);
+ if (dir_path == NULL)
+ {
+ DPRINTF(E_ERROR, L_INOTIFY, "Path with FD=0x%X can't be resolved.\n", ke.ident);
+ continue;
+ }
+
+ if (ke.fflags & NOTE_DELETE)
+ {
+ DPRINTF(E_DEBUG, L_INOTIFY, "Path [%s] deleted.\n", dir_path);
+ monitor_remove_directory(ke.ident, dir_path);
+ }
+ else if ((ke.fflags & (NOTE_WRITE | NOTE_LINK)) == (NOTE_WRITE | NOTE_LINK))
+ {
+ DPRINTF(E_DEBUG, L_INOTIFY, "Path [%s] content updated (directory).\n", dir_path);
+
+ char * sql;
+ char **result;
+ int i, rows;
+ sql = sqlite3_mprintf("SELECT PATH from DETAILS where (PATH > '%q/' and PATH <= '%q/%c')"
+ " and SIZE = ''", dir_path, dir_path, 0xFF);
+ if( (sql_get_table(db, sql, &result, &rows, NULL) != SQLITE_OK) )
+ {
+ DPRINTF(E_WARN, L_INOTIFY, "Read state [%s]: Query failed, not updating\n", dir_path);
+ sqlite3_free(sql);
+ continue;
+ }
+
+ for( i=1; i <= rows; i++ )
+ {
+ DPRINTF(E_DEBUG, L_INOTIFY, "Indexed content: %s\n", result[i]);
+ if (access(result[i], R_OK) == -1)
+ {
+ /* actually, global_kqueue_handle is not used here */
+ monitor_remove_directory(global_kqueue_handle, result[i]);
+ }
+ }
+
+ DIR* d;
+ struct dirent * entry;
+ d = opendir(dir_path);
+ if (!d)
+ {
+ DPRINTF(E_ERROR, L_INOTIFY, "Can't list [%s] (%s)\n", dir_path, strerror(errno));
+ continue;
+ }
+
+ for ( entry = readdir(d); entry != NULL; entry = readdir(d) )
+ {
+ if ( (entry->d_type == DT_DIR) &&
+ (strcmp(entry->d_name, "..") != 0) &&
+ (strcmp(entry->d_name, ".") != 0) )
+ {
+ char tmp_path[PATH_MAX];
+ int result_path_len;
+
+ result_path_len = snprintf(tmp_path, PATH_MAX,
+ "%s/%s", dir_path, entry->d_name);
+ if (result_path_len >= PATH_MAX)
+ {
+ DPRINTF(E_ERROR, L_INOTIFY, "File path too long for %s!", entry->d_name);
+ continue;
+ }
+
+ DPRINTF(E_DEBUG, L_INOTIFY, "Walking %s\n", tmp_path);
+ char found_flag = 0;
+ for( i=1; i <= rows; i++ )
+ {
+ if (strcmp(result[i], tmp_path) == 0)
+ {
+ found_flag = 1;
+ break;
+ }
+ }
+
+ if ( !found_flag )
+ {
+ char * esc_name = NULL;
+ esc_name = modifyString(strdup(entry->d_name), "&", "&amp;amp;", 0);
+ monitor_insert_directory(global_kqueue_handle, esc_name, tmp_path);
+ free(esc_name);
+ }
+ }
+ }
+
+ closedir(d);
+
+ sqlite3_free_table(result);
+ sqlite3_free(sql);
+ }
+ else if (ke.fflags & NOTE_WRITE)
+ {
+ DPRINTF(E_DEBUG, L_INOTIFY, "Path [%s] content updated (file).\n", dir_path);
+
+ char * sql;
+ char **result;
+ int i, rows;
+ sql = sqlite3_mprintf("SELECT PATH from DETAILS where (PATH > '%q/' and PATH <= '%q/%c')"
+ " and SIZE <> ''", dir_path, dir_path, 0xFF);
+ if( (sql_get_table(db, sql, &result, &rows, NULL) != SQLITE_OK) )
+ {
+ DPRINTF(E_WARN, L_INOTIFY, "Read state [%s]: Query failed, not updating\n", dir_path);
+ sqlite3_free(sql);
+ continue;
+ }
+
+ for( i=1; i <= rows; i++ )
+ {
+ DPRINTF(E_DEBUG, L_INOTIFY, "Indexed content: %s\n", result[i]);
+ if (access(result[i], R_OK) == -1) /*oops, our file is gone*/
+ {
+ monitor_remove_file(result[i]);
+ }
+ }
+
+ DIR* d;
+ struct dirent * entry;
+ d = opendir(dir_path);
+ if (!d)
+ {
+ DPRINTF(E_ERROR, L_INOTIFY, "Can't list [%s] (%s)\n", dir_path, strerror(errno));
+ continue;
+ }
+
+ for ( entry = readdir(d); entry != NULL; entry = readdir(d) )
+ {
+ if ( (entry->d_type == DT_REG) ||
+ (entry->d_type == DT_LNK) )
+ {
+ char tmp_path[PATH_MAX];
+ int result_path_len;
+
+ result_path_len = snprintf(tmp_path, PATH_MAX,
+ "%s/%s", dir_path, entry->d_name);
+ if (result_path_len >= PATH_MAX)
+ {
+ DPRINTF(E_ERROR, L_INOTIFY, "File path too long for %s!", entry->d_name);
+ continue;
+ }
+
+ DPRINTF(E_DEBUG, L_INOTIFY, "Walking %s\n", tmp_path);
+
+ char found_flag = 0;
+ for( i=1; i <= rows; i++ )
+ {
+ if (strcmp(result[i], tmp_path) == 0)
+ {
+ found_flag = 1;
+ break;
+ }
+ }
+
+ if ( !found_flag )
+ {
+ char * esc_name = NULL;
+ struct stat st;
+
+ if( stat(tmp_path, &st) != 0 )
+ {
+ DPRINTF(E_ERROR, L_INOTIFY, "'%s' disappeared!", tmp_path);
+ continue;
+ }
+
+ esc_name = modifyString(strdup(entry->d_name), "&", "&amp;amp;", 0);
+ if ( S_ISDIR(st.st_mode) )
+ monitor_insert_directory(global_kqueue_handle, esc_name, tmp_path);
+ else
+ monitor_insert_file(esc_name, tmp_path);
+ free(esc_name);
+ }
+ }
+ }
+
+ closedir(d);
+
+ sqlite3_free_table(result);
+ sqlite3_free(sql);
+ }
+ }
+ inotify_remove_watches(global_kqueue_handle);
+quitting:
+
+ return 0;
+}
+#endif /*HAVE_INOTIFY*/
+#endif /*defined(HAVE_INOTIFY) || defined(HAVE_SYS_EVENT_H)*/
diff --git monitor.h monitor.h
index 92ae41a..d0dd7e4 100644
--- monitor.h
+++ monitor.h
@@ -13,4 +13,7 @@ monitor_remove_directory(int fd, const char * path);
#ifdef HAVE_INOTIFY
void *
start_inotify();
+#elif defined(HAVE_SYS_EVENT_H)
+void *
+start_kqueue();
#endif
diff --git upnpevents.c upnpevents.c
index 06ec43a..9827e6e 100644
--- upnpevents.c
+++ upnpevents.c
@@ -47,6 +47,7 @@
*/
#include "config.h"
+#define FD_SETSIZE 8192
#include <stdio.h>
#include <string.h>
#include <errno.h>
@@ -417,6 +417,10 @@ void upnpevents_selectfds(fd_set *readset, fd_set *writeset, int * max_fd)
{
struct upnp_event_notify * obj;
for(obj = notifylist.lh_first; obj != NULL; obj = obj->entries.le_next) {
+ if (obj->s > FD_SETSIZE)
+ DPRINTF(E_FATAL, L_HTTP,
+ "upnpevents_selectfds: file descriptor %d too big for select, limit is %d\n",
+ obj->s, FD_SETSIZE);
DPRINTF(E_DEBUG, L_HTTP, "upnpevents_selectfds: %p %d %d\n",
obj, obj->state, obj->s);
if(obj->s >= 0) {