41#define CLOCKID CLOCK_REALTIME
42#define SIGMAINT SIGRTMIN
44#define SEM_OPEN_FILE "/" PACKAGE_NAME "_04806785-b5fb-4615-ba56-b30a2946e80b"
54static void maintenance_handler(
int sig, __attribute__((unused)) siginfo_t *si, __attribute__((unused))
void *uc);
67static void maintenance_handler(
int sig, __attribute__((unused)) siginfo_t *si, __attribute__((unused))
void *uc)
79 Logging::info(
nullptr,
"Running periodic cache maintenance.");
92 struct itimerspec its;
93 long long freq_nanosecs;
96 freq_nanosecs = interval * 1000000000LL;
101 sa.sa_flags = SA_SIGINFO;
103 sigemptyset(&sa.sa_mask);
104 if (sigaction(
SIGMAINT, &sa,
nullptr) == -1)
106 Logging::error(
nullptr,
"start_timer(): sigaction failed: (%1) %2", errno, strerror(errno));
113 if (sigprocmask(SIG_SETMASK, &
mask,
nullptr) == -1)
115 Logging::error(
nullptr,
"start_timer(): sigprocmask(SIG_SETMASK) failed: (%1) %2", errno, strerror(errno));
120 sev.sigev_notify = SIGEV_SIGNAL;
122 sev.sigev_value.sival_ptr = &
timerid;
125 Logging::error(
nullptr,
"start_timer(): timer_create failed: (%1) %2", errno, strerror(errno));
130 its.it_value.tv_sec =
static_cast<time_t
>(freq_nanosecs / 1000000000);
131 its.it_value.tv_nsec =
static_cast<long>(freq_nanosecs % 1000000000);
132 its.it_interval.tv_sec = its.it_value.tv_sec;
133 its.it_interval.tv_nsec = its.it_value.tv_nsec;
135 if (timer_settime(
timerid, 0, &its,
nullptr) == -1)
137 Logging::error(
nullptr,
"start_timer(): timer_settime failed: (%1) %2", errno, strerror(errno));
141 if (sigprocmask(SIG_UNBLOCK, &
mask,
nullptr) == -1)
143 Logging::error(
nullptr,
"start_timer(): sigprocmask(SIG_UNBLOCK) failed: (%1) %2", errno, strerror(errno));
146 Logging::trace(
nullptr,
"The maintenance timer started successfully.");
159 if (timer_delete(
timerid) == -1 && errno)
161 Logging::error(
nullptr,
"stop_timer(): timer_delete failed: (%1) %2", errno, strerror(errno));
176 Logging::debug(
nullptr,
"Activating " PACKAGE
" inter-process link.");
179 shmkey = ftok (
"/dev/null", 5);
183 Logging::error(
nullptr,
"link_up(): ftok error (%1) %2", errno, strerror(errno));
188 shmid = shmget (shmkey,
sizeof (pid_t), S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
197 shmid = shmget (shmkey,
sizeof (pid_t), IPC_CREAT | S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
205 Logging::error(
nullptr,
"link_up(): shmget error (%1) %2", errno, strerror(errno));
219 Logging::info(
nullptr,
"The process with PID %1 is now a client, and PID %2 is the master.", getpid(), *
pid_master);
225 if (
sem == SEM_FAILED)
227 Logging::error(
nullptr,
"link_up(): sem_open error (%1) %2", errno, strerror(errno));
240 pid_t pid_self = getpid();
244 Logging::trace(
nullptr,
"The process with PID %1 is already master.", pid_self);
251 int master_running = (getpgid(*
pid_master) >= 0);
257 Logging::info(
nullptr,
"Master with PID %1 is gone. PID %2 taking over as new master.", *
pid_master, pid_self);
276 Logging::info(
nullptr,
"Shutting " PACKAGE
" inter-process link down.");
278 if (
sem !=
nullptr && sem_close(
sem))
280 Logging::error(
nullptr,
"link_down(): sem_close error (%1) %2", errno, strerror(errno));
287 Logging::error(
nullptr,
"link_down(): shmdt error (%1) %2", errno, strerror(errno));
291 if (shmctl(
shmid, IPC_STAT, &buf))
293 Logging::error(
nullptr,
"link_down(): shmctl error (%1) %2", errno, strerror(errno));
300 if (shmctl (
shmid, IPC_RMID,
nullptr))
302 Logging::error(
nullptr,
"link_down(): shmctl error (%1) %2", errno, strerror(errno));
310 Logging::error(
nullptr,
"link_down(): sem_unlink error (%1) %2", errno, strerror(errno));
static bool master
If true, we are master.
bool stop_cache_maintenance()
Stop cache maintenance timer.
#define CLOCKID
Use real time clock here.
static void maintenance_handler(int sig, __attribute__((unused)) siginfo_t *si, __attribute__((unused)) void *uc)
Run maintenance handler.
static sigset_t mask
Process mask for timer.
static bool start_timer(time_t interval)
Start the maintenance timer at predefined interval.
static sem_t * sem
Semaphore used to synchronise between master and slave processes.
#define SIGMAINT
Map maintenance signal.
#define SEM_OPEN_FILE
Shared semaphore name, should be unique system wide.
static bool link_down()
Set system wide inter process link down.
static timer_t timerid
Timer id.
static bool link_up()
Set system wide inter process link up.
static int shmid
Shared memory segment ID.
static bool stop_timer()
Stop the maintenance timer.
bool start_cache_maintenance(time_t interval)
Start cache maintenance timer.
static pid_t * pid_master
PID of master process.
static void master_check()
Check if a master is already running. We become master if not.
static void debug(const T filename, const std::string &format_string, Args &&...args)
Write debug level log entry.
static void trace(const T filename, const std::string &format_string, Args &&...args)
Write trace level log entry.
static void info(const T filename, const std::string &format_string, Args &&...args)
Write info level log entry.
static void error(const T filename, const std::string &format_string, Args &&...args)
Write error level log entry.
std::string format_time(time_t value)
Format a time in format "w d m s".
Main include for FFmpegfs project.
bool transcoder_cache_maintenance()
Run cache maintenance.
Provide various log facilities to stderr, disk or syslog.