Execute command on server start and stop

Forums:

Index: options.c =================================================================== --- options.c (revision 2920) +++ options.c (working copy) @@ -735,6 +735,8 @@ add_str ("OnSongChange", NULL, CHECK_NONE); add_bool ("RepeatSongChange", false); + add_str ("OnServerStart", NULL, CHECK_NONE); + add_str ("OnServerStop", NULL, CHECK_NONE); add_str ("OnStop", NULL, CHECK_NONE); add_bool ("QueueNextSongReturn", false); Index: server.c =================================================================== --- server.c (revision 2920) +++ server.c (working copy) @@ -320,6 +320,36 @@ #endif } +/* Handle running external command on requested event. */ +static void extern_cmd (const char *event) +{ + char *command; + + command = xstrdup (options_get_str(event)); + + if (command) { + char *args[2], *err; + + args[0] = xstrdup (command); + args[1] = NULL; + + switch (fork()) { + case 0: + execve (command, args, environ); + exit (EXIT_FAILURE); + case -1: + err = xstrerror (errno); + logit ("Error when running %s command '%s': %s", + event, command, err); + free (err); + break; + } + + free (command); + free (args[0]); + } +} + /* Initialize the server - return fd of the listening socket or -1 on error */ void server_init (int debugging, int foreground) { @@ -401,6 +431,9 @@ redirect_output (stderr); } + logit ("Running OnServerStart"); + extern_cmd("OnServerStart"); + return; } @@ -565,36 +598,6 @@ last_file = curr_file; } -/* Handle running external command on Stop event. */ -static void on_stop () -{ - char *command; - - command = xstrdup (options_get_str("OnStop")); - - if (command) { - char *args[2], *err; - - args[0] = xstrdup (command); - args[1] = NULL; - - switch (fork()) { - case 0: - execve (command, args, environ); - exit (EXIT_FAILURE); - case -1: - err = xstrerror (errno); - logit ("Error when running OnStop command '%s': %s", - command, err); - free (err); - break; - } - - free (command); - free (args[0]); - } -} - /* Return true iff 'event' is a playlist event. */ static inline bool is_plist_event (const int event) { @@ -622,7 +625,7 @@ on_song_change (); break; case STATE_STOP: - on_stop (); + extern_cmd ("OnStop"); break; } } @@ -705,6 +708,8 @@ audio_exit (); tags_cache_free (tags_cache); tags_cache = NULL; + logit ("Running OnServerStop"); + extern_cmd("OnServerStop"); unlink (socket_name()); unlink (create_file_name(PID_FILE)); close (wake_up_pipe[0]);

Humm, it seems the summary didn't get through, here it is:

This patch adds two options for executing a command when starting and stopping the server :
OnServerStart and OnServerStop
I refactored on_start() to extern_cmd() for handling all three options (with OnStart) and possibly more…

Committed today as r2957 and r2958.