Fix a memory leak (free lock_path in lock_exit()).
Close file descriptor.
Missing calls to fclose().