Use cleanup(); return instead of exit().
Check that the input values for (f)close() are sane to prevent crashing.

Simplify logic while here.
Canonicalize perror() messages.
In live mode, first try to access the hardware, then open the log file.
Only fail in case of an actual error.
Do not redeclare fd.
Simplify printf().