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>
Some simple cleanups in the decsa and send threads.

In particular, this fixes a few stray accesses not done under the
ring buffer lock.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Fix UTF-8 output in state.xml.

First of all, do not clear non-ASCII characters from the output.

Second; BOMs are not recommended for UTF-8 (the only reason they're
in use at all, is that Microsoft for some reason decided to
start using them as some sort of UTF-8 marker without talking to the Unicode
Consortium), so do not include them.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
Add the printf attribute to log_message(), and fix all the newly detected bugs.

Signed-off-by: Brice DUBOST <mumudvb@braice.net>
rewrite_eit: fix for software descrambling

Conflicts:
	src/rewrite_eit.c

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