svn commit: r226463 - head/sbin/hastd

Pawel Jakub Dawidek pjd at FreeBSD.org
Mon Oct 17 12:22:09 UTC 2011


Author: pjd
Date: Mon Oct 17 12:22:09 2011
New Revision: 226463
URL: http://svn.freebsd.org/changeset/base/226463

Log:
  Allow to specify pidfile in HAST configuration file.
  
  MFC after:	1 week

Modified:
  head/sbin/hastd/hast.conf.5
  head/sbin/hastd/hast.h
  head/sbin/hastd/hastd.c
  head/sbin/hastd/parse.y
  head/sbin/hastd/token.l

Modified: head/sbin/hastd/hast.conf.5
==============================================================================
--- head/sbin/hastd/hast.conf.5	Mon Oct 17 09:59:04 2011	(r226462)
+++ head/sbin/hastd/hast.conf.5	Mon Oct 17 12:22:09 2011	(r226463)
@@ -64,17 +64,20 @@ compression <algorithm>
 timeout <seconds>
 exec <path>
 metaflush "on" | "off"
+pidfile <path>
 
 on <node> {
 	# Node section
         control <addr>
         listen <addr>
+        pidfile <path>
 }
 
 on <node> {
 	# Node section
         control <addr>
         listen <addr>
+        pidfile <path>
 }
 
 resource <name> {
@@ -146,6 +149,14 @@ unix:///var/run/hastctl
 .Pp
 The default value is
 .Pa uds:///var/run/hastctl .
+.It Ic pidfile Aq path
+.Pp
+File in which to store the process ID of the main
+.Xr hastd 8
+process.
+.Pp
+The default value is
+.Pa /var/run/hastd.pid .
 .It Ic listen Aq addr
 .Pp
 Address to listen on in form of:

Modified: head/sbin/hastd/hast.h
==============================================================================
--- head/sbin/hastd/hast.h	Mon Oct 17 09:59:04 2011	(r226462)
+++ head/sbin/hastd/hast.h	Mon Oct 17 12:22:09 2011	(r226463)
@@ -116,6 +116,8 @@ struct hastd_config {
 	struct proto_conn *hc_controlconn;
 	/* Incoming control connection. */
 	struct proto_conn *hc_controlin;
+	/* PID file path. */
+	char	hc_pidfile[PATH_MAX];
 	/* List of addresses to listen on. */
 	TAILQ_HEAD(, hastd_listen) hc_listen;
 	/* List of resources. */

Modified: head/sbin/hastd/hastd.c
==============================================================================
--- head/sbin/hastd/hastd.c	Mon Oct 17 09:59:04 2011	(r226462)
+++ head/sbin/hastd/hastd.c	Mon Oct 17 12:22:09 2011	(r226463)
@@ -476,11 +476,15 @@ hastd_reload(void)
 	struct hastd_config *newcfg;
 	struct hast_resource *nres, *cres, *tres;
 	struct hastd_listen *nlst, *clst;
+	struct pidfh *newpfh;
 	unsigned int nlisten;
 	uint8_t role;
+	pid_t otherpid;
 
 	pjdlog_info("Reloading configuration...");
 
+	newpfh = NULL;
+
 	newcfg = yy_config_parse(cfgpath, false);
 	if (newcfg == NULL)
 		goto failed;
@@ -524,6 +528,31 @@ hastd_reload(void)
 		pjdlog_error("No addresses to listen on.");
 		goto failed;
 	}
+	/*
+	 * Check if pidfile's path has changed.
+	 */
+	if (strcmp(cfg->hc_pidfile, newcfg->hc_pidfile) != 0) {
+		newpfh = pidfile_open(newcfg->hc_pidfile, 0600, &otherpid);
+		if (newpfh == NULL) {
+			if (errno == EEXIST) {
+				pjdlog_errno(LOG_WARNING,
+				    "Another hastd is already running, pidfile: %s, pid: %jd.",
+				    newcfg->hc_pidfile, (intmax_t)otherpid);
+			} else {
+				pjdlog_errno(LOG_WARNING,
+				    "Unable to open or create pidfile %s",
+				    newcfg->hc_pidfile);
+			}
+		} else if (pidfile_write(newpfh) < 0) {
+			/* Write PID to a file. */
+			pjdlog_errno(LOG_WARNING,
+			    "Unable to write PID to file %s",
+			    newcfg->hc_pidfile);
+		} else {
+			pjdlog_debug(1, "PID stored in %s.",
+			    newcfg->hc_pidfile);
+		}
+	}
 
 	/* No failures from now on. */
 
@@ -540,6 +569,13 @@ hastd_reload(void)
 		    sizeof(cfg->hc_controladdr));
 	}
 	/*
+	 * Switch to new pidfile.
+	 */
+	(void)pidfile_remove(pfh);
+	pfh = newpfh;
+	(void)strlcpy(cfg->hc_pidfile, newcfg->hc_pidfile,
+	    sizeof(cfg->hc_pidfile));
+	/*
 	 * Switch to new listen addresses. Close all that were removed.
 	 */
 	while ((clst = TAILQ_FIRST(&cfg->hc_listen)) != NULL) {
@@ -666,6 +702,8 @@ failed:
 		}
 		yy_config_free(newcfg);
 	}
+	if (newpfh != NULL)
+		(void)pidfile_remove(newpfh);
 	pjdlog_warning("Configuration not reloaded.");
 }
 
@@ -1123,7 +1161,7 @@ main(int argc, char *argv[])
 
 	foreground = false;
 	debuglevel = 0;
-	pidfile = HASTD_PIDFILE;
+	pidfile = NULL;
 
 	for (;;) {
 		int ch;
@@ -1157,17 +1195,6 @@ main(int argc, char *argv[])
 
 	g_gate_load();
 
-	pfh = pidfile_open(pidfile, 0600, &otherpid);
-	if (pfh == NULL) {
-		if (errno == EEXIST) {
-			pjdlog_exitx(EX_TEMPFAIL,
-			    "Another hastd is already running, pid: %jd.",
-			    (intmax_t)otherpid);
-		}
-		/* If we cannot create pidfile from other reasons, only warn. */
-		pjdlog_errno(LOG_WARNING, "Unable to open or create pidfile");
-	}
-
 	/*
 	 * When path to the configuration file is relative, obtain full path,
 	 * so we can always find the file, even after daemonizing and changing
@@ -1187,6 +1214,24 @@ main(int argc, char *argv[])
 	cfg = yy_config_parse(cfgpath, true);
 	PJDLOG_ASSERT(cfg != NULL);
 
+	if (pidfile != NULL) {
+		if (strlcpy(cfg->hc_pidfile, pidfile,
+		    sizeof(cfg->hc_pidfile)) >= sizeof(cfg->hc_pidfile)) {
+			pjdlog_exitx(EX_CONFIG, "Pidfile path is too long.");
+		}
+	}
+	pfh = pidfile_open(cfg->hc_pidfile, 0600, &otherpid);
+	if (pfh == NULL) {
+		if (errno == EEXIST) {
+			pjdlog_exitx(EX_TEMPFAIL,
+			    "Another hastd is already running, pidfile: %s, pid: %jd.",
+			    cfg->hc_pidfile, (intmax_t)otherpid);
+		}
+		/* If we cannot create pidfile for other reasons, only warn. */
+		pjdlog_errno(LOG_WARNING, "Unable to open or create pidfile %s",
+		    cfg->hc_pidfile);
+	}
+
 	/*
 	 * Restore default actions for interesting signals in case parent
 	 * process (like init(8)) decided to ignore some of them (like SIGHUP).
@@ -1234,7 +1279,10 @@ main(int argc, char *argv[])
 		/* Write PID to a file. */
 		if (pidfile_write(pfh) < 0) {
 			pjdlog_errno(LOG_WARNING,
-			    "Unable to write PID to a file");
+			    "Unable to write PID to a file %s",
+			    cfg->hc_pidfile);
+		} else {
+			pjdlog_debug(1, "PID stored in %s.", cfg->hc_pidfile);
 		}
 	}
 

Modified: head/sbin/hastd/parse.y
==============================================================================
--- head/sbin/hastd/parse.y	Mon Oct 17 09:59:04 2011	(r226462)
+++ head/sbin/hastd/parse.y	Mon Oct 17 12:22:09 2011	(r226463)
@@ -60,6 +60,7 @@ static struct hast_resource *curres;
 static bool mynode, hadmynode;
 
 static char depth0_control[HAST_ADDRSIZE];
+static char depth0_pidfile[PATH_MAX];
 static char depth0_listen_tcp4[HAST_ADDRSIZE];
 static char depth0_listen_tcp6[HAST_ADDRSIZE];
 static TAILQ_HEAD(, hastd_listen) depth0_listen;
@@ -193,6 +194,7 @@ yy_config_parse(const char *config, bool
 	depth0_checksum = HAST_CHECKSUM_NONE;
 	depth0_compression = HAST_COMPRESSION_HOLE;
 	strlcpy(depth0_control, HAST_CONTROL, sizeof(depth0_control));
+	strlcpy(depth0_pidfile, HASTD_PIDFILE, sizeof(depth0_pidfile));
 	TAILQ_INIT(&depth0_listen);
 	strlcpy(depth0_listen_tcp4, HASTD_LISTEN_TCP4,
 	    sizeof(depth0_listen_tcp4));
@@ -238,6 +240,10 @@ yy_config_parse(const char *config, bool
 		strlcpy(lconfig->hc_controladdr, depth0_control,
 		    sizeof(lconfig->hc_controladdr));
 	}
+	if (lconfig->hc_pidfile[0] == '\0') {
+		strlcpy(lconfig->hc_pidfile, depth0_pidfile,
+		    sizeof(lconfig->hc_pidfile));
+	}
 	if (!TAILQ_EMPTY(&depth0_listen))
 		TAILQ_CONCAT(&lconfig->hc_listen, &depth0_listen, hl_next);
 	if (TAILQ_EMPTY(&lconfig->hc_listen)) {
@@ -365,7 +371,7 @@ yy_config_free(struct hastd_config *conf
 }
 %}
 
-%token CONTROL LISTEN PORT REPLICATION CHECKSUM COMPRESSION METAFLUSH
+%token CONTROL PIDFILE LISTEN PORT REPLICATION CHECKSUM COMPRESSION METAFLUSH
 %token TIMEOUT EXEC EXTENTSIZE RESOURCE NAME LOCAL REMOTE SOURCE ON OFF
 %token FULLSYNC MEMSYNC ASYNC NONE CRC32 SHA256 HOLE LZF
 %token NUM STR OB CB
@@ -395,6 +401,8 @@ statements:
 statement:
 	control_statement
 	|
+	pidfile_statement
+	|
 	listen_statement
 	|
 	replication_statement
@@ -444,6 +452,36 @@ control_statement:	CONTROL STR
 	}
 	;
 
+pidfile_statement:	PIDFILE STR
+	{
+		switch (depth) {
+		case 0:
+			if (strlcpy(depth0_pidfile, $2,
+			    sizeof(depth0_pidfile)) >=
+			    sizeof(depth0_pidfile)) {
+				pjdlog_error("pidfile argument is too long.");
+				free($2);
+				return (1);
+			}
+			break;
+		case 1:
+			if (!mynode)
+				break;
+			if (strlcpy(lconfig->hc_pidfile, $2,
+			    sizeof(lconfig->hc_pidfile)) >=
+			    sizeof(lconfig->hc_pidfile)) {
+				pjdlog_error("pidfile argument is too long.");
+				free($2);
+				return (1);
+			}
+			break;
+		default:
+			PJDLOG_ABORT("pidfile at wrong depth level");
+		}
+		free($2);
+	}
+	;
+
 listen_statement:	LISTEN STR
 	{
 		struct hastd_listen *lst;
@@ -658,6 +696,8 @@ node_entries:
 node_entry:
 	control_statement
 	|
+	pidfile_statement
+	|
 	listen_statement
 	;
 

Modified: head/sbin/hastd/token.l
==============================================================================
--- head/sbin/hastd/token.l	Mon Oct 17 09:59:04 2011	(r226462)
+++ head/sbin/hastd/token.l	Mon Oct 17 12:22:09 2011	(r226463)
@@ -46,6 +46,7 @@ int lineno;
 
 %%
 control			{ DP; return CONTROL; }
+pidfile			{ DP; return PIDFILE; }
 listen			{ DP; return LISTEN; }
 port			{ DP; return PORT; }
 replication		{ DP; return REPLICATION; }


More information about the svn-src-head mailing list