Submitter | Jun Wu |
---|---|
Date | April 10, 2016, 11:57 p.m. |
Message ID | <a0233eac2848926116ec.1460332643@x1c> |
Download | mbox | patch |
Permalink | /patch/14504/ |
State | Changes Requested |
Delegated to: | Yuya Nishihara |
Headers | show |
Comments
On Mon, 11 Apr 2016 00:57:23 +0100, Jun Wu wrote: > # HG changeset patch > # User Jun Wu <quark@fb.com> > # Date 1460327827 -3600 > # Sun Apr 10 23:37:07 2016 +0100 > # Node ID a0233eac2848926116ecbdb4bf44fd5f066d7f1d > # Parent 8dcda8964985a6e1826e6ceb8e7f476a66b8a3f9 > chg: use full path as --address to start a new server > > This is a part of the series to support long socket path. We will change > sockname to basename instead of full path soon. > > To make sure the server creates the socket file in a correct location, > we have to pass the full path because we may pass "--cwd" as well and > a "chdir" just before starting the server doesn't work with "--cwd". > > This patch uses getcwd to resolve the dir fd and get the full path used > for the server. > > diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c > --- a/contrib/chg/chg.c > +++ b/contrib/chg/chg.c > @@ -215,6 +215,27 @@ > return hgcmd; > } > > +static const char *getfullpath(int dirfd, const char *name) > +{ > + if (name[0] == '/') > + return name; > + > + static char path[PATH_MAX]; > + int cwdfd = open(".", O_DIRECTORY); > + if (cwdfd == -1) > + abortmsgerrno("failed to open current directory"); > + fchdirx(dirfd); > + if (getcwd(path, sizeof(path)) == NULL) > + abortmsgerrno("failed to getcwd"); > + size_t len = strlen(path); > + assert(len < sizeof(path)); > + int r = snprintf(path + len, sizeof(path) - len, "/%s", name); > + if ((size_t)r >= sizeof(path)) Wrong boundary. You've shifted the destination pointer by len.
Patch
diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c --- a/contrib/chg/chg.c +++ b/contrib/chg/chg.c @@ -215,6 +215,27 @@ return hgcmd; } +static const char *getfullpath(int dirfd, const char *name) +{ + if (name[0] == '/') + return name; + + static char path[PATH_MAX]; + int cwdfd = open(".", O_DIRECTORY); + if (cwdfd == -1) + abortmsgerrno("failed to open current directory"); + fchdirx(dirfd); + if (getcwd(path, sizeof(path)) == NULL) + abortmsgerrno("failed to getcwd"); + size_t len = strlen(path); + assert(len < sizeof(path)); + int r = snprintf(path + len, sizeof(path) - len, "/%s", name); + if ((size_t)r >= sizeof(path)) + abortmsg("full socket path is too long"); + fchdirx(cwdfd); + return path; +} + static void execcmdserver(const struct cmdserveropts *opts) { const char *hgcmd = gethgcmd(); @@ -223,7 +244,7 @@ hgcmd, "serve", "--cmdserver", "chgunix", - "--address", opts->sockname, + "--address", getfullpath(opts->sockdirfd, opts->sockname), "--daemon-postexec", "chdir:/", "--config", "extensions.chgserver=", };