Patchwork [14,of,14] chg: chdir to sockdir in killcmdserver

login
register
mail settings
Submitter Jun Wu
Date April 10, 2016, 11:57 p.m.
Message ID <a19fc1c9f3e23a98f4c5.1460332651@x1c>
Download mbox | patch
Permalink /patch/14511/
State Accepted
Delegated to: Yuya Nishihara
Headers show

Comments

Jun Wu - April 10, 2016, 11:57 p.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1460331689 -3600
#      Mon Apr 11 00:41:29 2016 +0100
# Node ID a19fc1c9f3e23a98f4c510ba5bece148f6d5e3eb
# Parent  58ea17bb6caad0e680f23473b37f68c03c546146
chg: chdir to sockdir in killcmdserver

Since sockname is a basename now, the unlink in killcmdserver won't work
without a correct directory. If there is realpathat(), things could be
easier but it seems we have to change cwd to make it work.
Yuya Nishihara - April 13, 2016, 3:27 p.m.
On Mon, 11 Apr 2016 00:57:31 +0100, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark@fb.com>
> # Date 1460331689 -3600
> #      Mon Apr 11 00:41:29 2016 +0100
> # Node ID a19fc1c9f3e23a98f4c510ba5bece148f6d5e3eb
> # Parent  58ea17bb6caad0e680f23473b37f68c03c546146
> chg: chdir to sockdir in killcmdserver
> 
> Since sockname is a basename now, the unlink in killcmdserver won't work
> without a correct directory. If there is realpathat(), things could be
> easier but it seems we have to change cwd to make it work.
> 
> diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
> --- a/contrib/chg/chg.c
> +++ b/contrib/chg/chg.c
> @@ -370,12 +370,15 @@
>  
>  static void killcmdserver(const struct cmdserveropts *opts)
>  {
> -	/* resolve config hash */
> +	int cwdfd = opencwdx();
> +	fchdirx(opts->sockdirfd);
> +	/* resolve symbolic link: e.g. server -> server-a4fd1c015493 */
>  	char *resolvedpath = realpath(opts->sockname, NULL);
>  	if (resolvedpath) {
>  		unlink(resolvedpath);
>  		free(resolvedpath);
>  	}
> +	fchdirx(cwdfd);
>  }

Seems fine. The other idea is to make --kill-chg-daemon kills all daemons
under the socket directory.

Patch

diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -370,12 +370,15 @@ 
 
 static void killcmdserver(const struct cmdserveropts *opts)
 {
-	/* resolve config hash */
+	int cwdfd = opencwdx();
+	fchdirx(opts->sockdirfd);
+	/* resolve symbolic link: e.g. server -> server-a4fd1c015493 */
 	char *resolvedpath = realpath(opts->sockname, NULL);
 	if (resolvedpath) {
 		unlink(resolvedpath);
 		free(resolvedpath);
 	}
+	fchdirx(cwdfd);
 }
 
 static pid_t peerpid = 0;