patterncMinor
VLC media player watchdog daemon
Viewed 0 times
playervlcdaemonmediawatchdog
Problem
I'm looking for bugs, ways to make it more portable or standardized, improvements of any kind. Seems to do what it is supposed to on my Ubuntu 12.04 PC.
```
/* VLC-Watchdog v2
*
* A fix for stopped VLC Media Player inhibiting the power management
* daemon and preventing screen saver and/or monitor power off.
*
* Takes start | stop as command line argument.
*
* Checks if VLC Media Player is running using pgrep. If VLC is found
* to be running, queries the playback status through the DBus low level
* API. If the playback status is found to be stopped, calls the Quit()
* method via DBus. If VLC is found not to be running VLC-Watchdog sleeps
* for 10 seconds then repeats all of the above. When VLC-Watchdog receives
* the stop signal ("VLC-Watchdog stop" from the command line) it exits.
*/
#include
#include
#include
#include
#include
#include
#include
#include
static void skeleton_daemon();
int vlc_running();
void kill_vlc_if_stopped(int pid);
void usage(char *pname);
void sig_handler(int signo);
extern int running = 1;
int main(int argc, char *argv[]) {
FILE *fp;
int pid;
//check command line arguments for validity
if ((argc != 2) || (
(strcmp(argv[1], "start") != 0) &&
(strcmp(argv[1], "stop") != 0)))
usage(argv[0]);
//start / run actions
if (strcmp(argv[1], "start") == 0) {
skeleton_daemon(); //daemonize
//write the PID to a file for future reference
fp = fopen("/tmp/vlcwatchdog.pid", "w");
fprintf(fp, "%d\n", getpid());
fclose(fp);
//register signal handler
if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
fprintf(stderr, "error while setting signal handler\n");
exit(1);
}
/* when first started and every 10 seconds thereafter, check if
* VLC is running, check if it is stopped, ask it to Quit() if it
is stopped. Stop and exit when SIGUSR1 is received /
while (running)
```
/* VLC-Watchdog v2
*
* A fix for stopped VLC Media Player inhibiting the power management
* daemon and preventing screen saver and/or monitor power off.
*
* Takes start | stop as command line argument.
*
* Checks if VLC Media Player is running using pgrep. If VLC is found
* to be running, queries the playback status through the DBus low level
* API. If the playback status is found to be stopped, calls the Quit()
* method via DBus. If VLC is found not to be running VLC-Watchdog sleeps
* for 10 seconds then repeats all of the above. When VLC-Watchdog receives
* the stop signal ("VLC-Watchdog stop" from the command line) it exits.
*/
#include
#include
#include
#include
#include
#include
#include
#include
static void skeleton_daemon();
int vlc_running();
void kill_vlc_if_stopped(int pid);
void usage(char *pname);
void sig_handler(int signo);
extern int running = 1;
int main(int argc, char *argv[]) {
FILE *fp;
int pid;
//check command line arguments for validity
if ((argc != 2) || (
(strcmp(argv[1], "start") != 0) &&
(strcmp(argv[1], "stop") != 0)))
usage(argv[0]);
//start / run actions
if (strcmp(argv[1], "start") == 0) {
skeleton_daemon(); //daemonize
//write the PID to a file for future reference
fp = fopen("/tmp/vlcwatchdog.pid", "w");
fprintf(fp, "%d\n", getpid());
fclose(fp);
//register signal handler
if (signal(SIGUSR1, sig_handler) == SIG_ERR) {
fprintf(stderr, "error while setting signal handler\n");
exit(1);
}
/* when first started and every 10 seconds thereafter, check if
* VLC is running, check if it is stopped, ask it to Quit() if it
is stopped. Stop and exit when SIGUSR1 is received /
while (running)
Solution
Just a few simple things:
-
This:
can be written as
removes the need for
-
In general your error logging is a bit adventurous. Sometimes you obtain the file pointer to the error log in which case you usually close it (but it's kinda ugly having to remember this) and sometimes you just log a string in which case you don't close the file pointer. You should be able to just do it with one function:
-
This:
strcpy (name, "org.mpris.MediaPlayer2.vlc-");
sprintf (buf, "%d", pid);
strcat (name, buf);can be written as
sprintf(name, "%s%d", "org.mpris.MediaPlayer2.vlc-", pid);removes the need for
buf and makes it a bit cleaner.-
In general your error logging is a bit adventurous. Sometimes you obtain the file pointer to the error log in which case you usually close it (but it's kinda ugly having to remember this) and sometimes you just log a string in which case you don't close the file pointer. You should be able to just do it with one function:
void log_error(const char* format, ...)
{
va_list args;
FILE *errlog;
errlog = fopen("/tmp/vlcwderr.log", "a");
va_start(args, format);
vfprintf(errlog, format, args);
va_end(args);
fclose(errlog);
return;
}Code Snippets
strcpy (name, "org.mpris.MediaPlayer2.vlc-");
sprintf (buf, "%d", pid);
strcat (name, buf);sprintf(name, "%s%d", "org.mpris.MediaPlayer2.vlc-", pid);void log_error(const char* format, ...)
{
va_list args;
FILE *errlog;
errlog = fopen("/tmp/vlcwderr.log", "a");
va_start(args, format);
vfprintf(errlog, format, args);
va_end(args);
fclose(errlog);
return;
}Context
StackExchange Code Review Q#40119, answer score: 5
Revisions (0)
No revisions yet.