29 Commits
Author SHA1 Message Date
Gleb Smirnoff 92820eb1ee monitor_kqueue: plug memory leak from strcpy() on directory delete 2022-02-11 07:41:06 -08:00
Gleb Smirnoff 1d363c209f Use timevals everywhere where it is possible, including API between main
loop and event dispatchers. This simplifies code and eliminates a bug,
when kevent dispatcher is called with 0 timeout.

While here, in the main loop call gettimeofday() right after event
dispatcher returns. Otherwise, we are using outdated "timeofday" in
second part of the loop. I don't know any bugs because of that, but
they are possible.
2021-01-07 11:02:51 -08:00
Gleb Smirnoff cad8c922f0 Consolidate start/finish of monitor on different platforms into
one API. Monitor is started either before main loop, if scanner
isn't running, or in the main loop once we finished waiting for
the scanner.
2021-01-07 11:02:44 -08:00
Gleb Smirnoff ee912576b3 Separate platform indepenent code into monitor.c, and move inotify(7)
specific code to monitor_inotify.c.
Both monitor_inotify.c and monitor_kqueue.c provide their functions
for adding and removing watches. Prefix these functions with monitor_.
2021-01-07 10:58:29 -08:00
Gleb SmirnoffandJustin Maggard 36b9136d2b Use newer IP_MULTICAST_IF API
Use newer API for IP_MULTICAST_IF which allows one to specify
interface by index, not by address. Introduced in Linux 3.5, it IMHO should
be available on all systems that declare struct ip_mreqn.

This fixes operation failure when a system has multiple interfaces
with same address, but only on of them is desired. Example:

> grep interface /usr/local/etc/minidlna.conf
network_interface=igb0
> ifconfig igb0
igb0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
        ether 0c:c4:7a:xx:xx:xx
        inet 10.1.10.3 netmask 0xffffff00 broadcast 10.1.10.255
        media: Ethernet autoselect (1000baseT <full-duplex>)
        status: active
> ifconfig ng0
ng0: flags=88d1<UP,POINTOPOINT,RUNNING,NOARP,SIMPLEX,MULTICAST> metric 0 mtu 1454
        inet 10.1.10.3 --> 10.1.10.2 netmask 0xffffffff

In such configuration, ng0 would be chosen before this fix.
2020-11-24 10:53:50 -08:00
Gleb SmirnoffandJustin Maggard 8fe7293c7a Fix compilation warning for undeclared struct event. 2018-01-16 17:05:53 -08:00
Gleb SmirnoffandJustin Maggard f7604117c0 Fix more compilation mistakes from 338ee4bd7bb44 in TiVo. 2018-01-16 17:05:51 -08:00
Gleb SmirnoffandJustin Maggard 3e8da9bb1a Return back checking file for sparseness. Use lseek(SEEK_HOLE) if supported by the system, otherwise fallback to block check. This allows to workaround bugs with SMB client and not introduce new bugs for filesystems with compression and deduplication.
This has theoretical bug if our collection spans different
filesystems and some filesystems support holes, and some not.
If this ever encountered we should use pathconf(2) and cache
its result for directories.
2018-01-16 17:05:44 -08:00
Gleb SmirnoffandJustin Maggard debb502ff3 Fix compilation mistakes from 338ee4bd7bb44 in TiVo. 2018-01-16 17:05:42 -08:00
Gleb SmirnoffandJustin Maggard da30df3350 Remove C99 code. 2018-01-16 17:05:40 -08:00
Gleb SmirnoffandJustin Maggard 7428b8dede This file is derived from code by Bernard Spil. Fix copyright. 2018-01-16 17:04:32 -08:00
Gleb SmirnoffandJustin Maggard 1cb676f51d Add license and my copyright to monitor_kqueue.c. The file contains portions of code that were made either by William or by Stefan. Since I am unsure about that, put them both. To be clarified later. 2018-01-16 17:03:20 -08:00
Gleb SmirnoffandJustin Maggard a4dc9a1996 Add nginx's and my copyright to the files that borrow ideas from nginx. 2018-01-16 17:03:00 -08:00
Gleb SmirnoffandJustin Maggard 4db229534b Initialize libav before enabling kqueue monitor, missed in b2613161329. 2018-01-16 17:02:13 -08:00
Gleb SmirnoffandJustin Maggard d02866a084 Filled up event list isn't something warning. 2018-01-16 17:02:10 -08:00
Gleb SmirnoffandJustin Maggard d6374fa4e2 Fix SQL queries that intended to match directories. Code originates from FreeBSD ports extra patch. Probably it was working on previous versions of SQLite, or on previous minidlna schema. 2018-01-16 17:02:05 -08:00
Gleb SmirnoffandJustin Maggard 4ba6f1fa27 Fix stupid logic typo, that prevented monitoring from working. 2018-01-16 17:02:03 -08:00
Gleb SmirnoffandJustin Maggard 42f0630198 Remove (st.st_blocks<<9 >= st.st_size) check. It may fail if a file system supports deduplication, compression and other features.
My guess the goal was to skip files with holes in them. Original commit
590f0761f4, doesn't explain that. If that
is still necessary, better to add lseek(SEEK_DATA) check later.
2018-01-16 17:02:01 -08:00
Gleb SmirnoffandJustin Maggard 958da67592 Initialize libav before enabling kqueue monitor. 2018-01-16 17:01:59 -08:00
Gleb SmirnoffandJustin Maggard dfdd81a09f Wrap struct event into struct watch. For now this is just code bloat. We might start monitoring files later. We also might want to create a tree/list of all watches if we ever need to search within them, like the inotify module does. 2018-01-16 17:01:55 -08:00
Gleb SmirnoffandJustin Maggard 5e320f2798 Add monitoring support via kqueue(2). Based on patch from FreeBSD ports, authored by wg@FreeBSD.org and se@FreeBSD.org. However, this version doesn't create any thread, it uses main event dispatcher.
Some effort was made to unify monitoring via kqueue and via inotify
APIs. Now both provide their implementation of add_watch() function.

I guess there are some logical bugs in vnode_process(). With this commit
it would be better provide code as is, and resolve bugs separately.
2018-01-16 17:01:43 -08:00
Gleb SmirnoffandJustin Maggard 184607cb56 Remove obsoleted useless flag. 2018-01-16 16:55:26 -08:00
Gleb SmirnoffandJustin Maggard 7ba9e52fc8 Add kqueue event module. Code inspired by nginx. There are a lot of room for optimization here, this is just first working version. 2018-01-16 16:55:23 -08:00
Gleb SmirnoffandJustin Maggard aefe4dd0bf With current forking model of minidlna, we no longer need the event module in the child, so shut it down. While here, make any error from fork() a fatal error. 2018-01-16 16:53:14 -08:00
Gleb SmirnoffandJustin Maggard 33fb664fba Failure of event_module init is fatal. 2018-01-16 16:53:12 -08:00
Gleb SmirnoffandJustin Maggard 8fcb1258b4 Shut up compilation warning. 2018-01-16 16:53:10 -08:00
Gleb SmirnoffandJustin Maggard f9a78d598e Support for pluggable event modules, stage 1. Take out select() loop and associated code into a separate module select.c. Detailed list of changes down below.
The idea is taken from the nginx web server, but much simplified and
almost no copypaste left. This will allow minidlna to use different
event dispatcher APIs, which would be defined at compile time.

My personal goal is to convert minidlna to kqueue(2) on FreeBSD. This
would later allow for kqueue based directory change notification, which
won't conflict with select(2) like the current patch does.
Other platforms will also benefit from the pluggability of the event
system, Linux can switch to epoll(2) or at least to poll(2).

Detailed list of changes:

* event.h [New]
  Our internal API to unify different event dispatch systems.
* select.c [New]
  Much simplified version of nginx's ngx_select_module.c.
* minidlna.c
  - Split out listen socket event processing into separate function
    ProcessListen(), which matches event_process_t type.
  - Create and initialize struct event for the monitor socket, SSDP
    socket, HTTP socket and beacon socket.
  - Simplify and make more precise timeout calculation using
    helper timeval functions from utils.c. Treat gettimeofday() error
    as a fatal event.
  - Rip out all stuff related to select(2). Just call event_module.process().
* upnpevents.c
  - Embed struct event into upnp_event_notify.
  - Merge upnp_event_create_notify() with upnp_event_notify_connect().
    Start connecting immediately after socket creation. Garbage collect
    now useless ECreated state.
  - Make upnp_event_process_notify() of event_process_t type, and use it
    as process callback for upnp_event_notify event.
  - Looks like we always create upnp_event_notify with existing subscriber,
    and never clear it later. Remove checks for obj->sub and assert that it
    is never NULL. Simplifies things.
  - When switching obj state, add/del it to event dispatcher accrodingly.
  - Garbage collect upnpevents_selectfds().
  - Garbage collect select(2) related stuff from upnpevents_processfds().
    Rename function to upnpevents_gc(), since the remaining functionality
    is garbage collecting, not file descriptor processing.
    Actually, this can be simplified even more. We can safely close sockets
    and free objects immediately, eliminating need for upnpevents_gc(). But
    this change would be beyond scope of this commit.
* upnphttp.c, upnphttp.h
  Embed struct event into struct upnphttp. Adjust Process_upnphttp() to match
  event_process_t type. Add/del to event dispatcher once creating/closing a
  socket.
* minissdp.c, minissdp.h
  Make ProcessSSDPRequest() of event_process_t type.
* getifaddr.c, getifaddr.h
  Make ProcessMonitorEvent() of event_process_t type.
2018-01-16 16:53:08 -08:00
Gleb SmirnoffandJustin Maggard 04e243c85c Add handy functions for timeval manipulations. Obtained from FreeBSD. 2018-01-16 16:49:52 -08:00
Gleb SmirnoffandJustin Maggard 8fb89dc6fa Remove reference to non-existent INSTALL file. 2018-01-16 16:49:43 -08:00