Enable dispatcher to delete socket
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
+#include <getopt.h>
#include <inttypes.h>
#include <limits.h>
#include <netinet/in.h>
#include <poll.h>
#include <pthread.h>
#include <signal.h>
+#include <stdbool.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/select.h>
@@ -121,6 +123,8 @@ static chk_input *chk_udp_input;
static zlog_category_t *zc;
void handle_signal(int signal);
+void parse_cmdline(int argc, char **argv);
+void unlink_socket();
int run();
int create_sockets();
@@ -159,7 +163,6 @@ char socket_path[UNIX_PATH_MAX];
#define MAX_NUMBER_PINGS MAX_SOCKETS
PingEntry *ping_list = NULL;
-
int main(int argc, char **argv)
{
signal(SIGTERM, handle_signal);
@@ -184,6 +187,7 @@ int main(int argc, char **argv)
zlog_fini();
return -1;
}
+ parse_cmdline(argc, argv);
/* Allocate for later use */
chk_udp_input = mk_chk_input(UDP_CHK_INPUT_SIZE);
@@ -204,6 +208,59 @@ int main(int argc, char **argv)
return res;
}
+void parse_cmdline(int argc, char **argv) {
+ int c;
+ bool delete_sock=false;
+
+
+ while (1) {
+ int option_index = 0;
+ const static struct option long_options[] = {
+ {"help", no_argument, NULL, 'h' },
+ {"delete-sock", no_argument, NULL, 'd' },
+ {NULL, 0, NULL, 0 }
+ };
+ c = getopt_long(argc, argv, "h", long_options, &option_index);
+ if (c == -1) {
+ break;
+ }
+
+ switch(c) {
+ case 'h':
+ fprintf(stderr, "Usage: %s [flags]\n -h,--help: this message\n"
+ " --delete-sock: delete Unix domain socket on start\n",
+ argv[0]);
+ exit(1);
+ case 'd':
+ delete_sock = true;
+ break;
+ default:
+ fprintf(stderr, "Unknown option '%s'\n", argv[option_index+1]);
+ exit(2);
+ }
+ }
+ if (delete_sock) {
+ unlink_socket();
+ }
+}
+
+void unlink_socket() {
+ char *sockpath = NULL;
+ asprintf(&sockpath, "%s/default.sock", DISPATCHER_DIR);
+ errno = 0;
+ if (unlink(sockpath)) {
+ if (errno == ENOENT) {
+ zlog_debug(zc, "'%s' does not exist, ignoring --delete-sock flag.", sockpath);
+ free(&sockpath);
+ return;
+ }
+ zlog_error(zc, "could not unlink '%s': %s", sockpath, strerror(errno));
+ exit(2);
+ }
+ zlog_info(zc, "successfully deleted '%s'", sockpath);
+ free(&sockpath);
+}
+
void handle_signal(int sig)
{
switch (sig) {
