Link properly against the math library; this does not happen automatically when e.g. linking statically.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Remove some unused parameters from autoconf_definite_end.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Small cleanup in the signal handling.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Early-abort from log_message().

Previously, we could use as much as 10% of one core just formatting debug
lines that would never get output anyway. Check the verbosity as the first
thing we're doing to save that CPU.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Use recv() instead of read() when getting DVBAPI data.

There's no guarantee that read() won't coalesce multiple neighboring
UDP packets into one, whereas recv() is different.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Round up to the nearest power of two using bit-fiddling tricks.

log2() is not available in uclibc, and abusing floating-point functions
can have scary numeric effects anyway. Replace it with a bit-wise
sequence (which should also be slightly faster; not that it matters).

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Do not use timed pthread joins on uclibc, as they are not supported.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Make all the shutdown variables volatile.

If they are not volatile, there is no guarantee that the compiler does not
simply cache the accesses. Strictly speaking, there's no guarantee int will
be atomic, but on all relevant platforms, it probably is.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Give a more easily understandable message on DVB buffer overruns.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Make now_time non-global.

There's no good reason to have non_time global, except to create a huge amount
of races; and worse, uint64_t is a non-atomic type on 32-bit x86, causing
potentially invalid values to be read as the time. Simply make it local to
each function that needs it, since they were updating it for every read anyway.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Protect autoconf_vars with a mutex.

This is read and written from multiple threads, so it's an obvious
candidate.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Make autoconf_services_to_channels() take in a pointer to the parameters.

This function does not need a full copy of the autoconf parameters,
and making one copies a lock, which confuses Helgrind. Just send down
a const pointer instead.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Hard-code program_invocation_short_name if on uclibc, which does not have the feature.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Introduce a temporary variable "channel" in some cases.

Some places, some long expression such as params->chan_and_pids->channels[curr_channel]
is used over and over again; simply add a temporary variable holding the pointer to the
channel in those cases. This removes a lot of unneccessary clutter that's in the way
when trying to understand the code.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Make the Interrupted variable thread-safe.

Interrupted is set from various places as a generic error variable
any form of locking; sometimes, in contexts that can set it to 0 even though it
was something else already.  This can cause errors to be masked, and trips up
Helgrind a lot.

Make sure to always set it atomically, and only if it is zero (so previous
errors are not overwritten; we always get the first one). Also, never set it
to zero no matter what.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Give time_decsa/time_send full granularity.

Having these only update for every fourth element can give somewhat weird
effects when the circular buffer gets full and the seam point is not at
an index that's a multiple of four. This is maybe not super-critical,
but it's definitely the safest.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Rework ring buffer locking.

Instead of having one mutex per variable, have one that protects everything
except the CWs (which have their own locks). In particular, this protects
time_decsa and time_send, which were not at all protected before. (This could
lead to the decsa thread hanging when time_decsa[] was changed between the
check against "now_time" and the sleep time calculation, and we thus tried to
sleep a negative amount.)

Also, remove the redundant variable ring_buffer_num_packets, which is only
the sum of to_descramble and to_send anyway.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Change how odd/even keys are switched.

Previously, the only limitation on odd/even key switches would be that they
would be switched every other time. This could cause a problem when a new key
comes too early, e.g. there's an ECM containing the new one immediately after
it goes out of use, and due to the delaying done using decsa_time, it can
reach the decsa thread while it is still in use.

Change so that a key is not changed until we are sure that it is no longer
in use; that is, we have a batch where we see packets encrypted with the other
key, and no packets encrypted with this key.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Protect the chan_and_pids structure, and by extension, all the channels.

I considered having per-channel locks, but the contention is so low
(the decsa and send threads only need the ring buffer and CWs, which
are both under other locks) that it wasted more time locking and unlocking
all the smaller mutexes.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>

Ps: global use of chan_and_pids will be corrected later
Start SCAM threads on channels, not PIDs.

This code used to check if a SCAM thread was stared for a given PID,
but the threads operate per-channel. (This is wrong, by the way;
there exist channels where different PIDs need different keys,
but I'm not going to fix that right now.) This would frequently make multiple
decsa and send threads being spawned for the same channel, which would stomp on
each other. Change so that we put the check on the channel instead of the PID,
which fixes the problem.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>