Nothing in here looks awful, so I think we may as well just do it.
- skip-blame because it's just reformatting with no functionality change
yuja |
hg-reviewers |
Nothing in here looks awful, so I think we may as well just do it.
Automatic diff as part of commit; lint not applicable. |
Automatic diff as part of commit; unit tests not applicable. |
Path | Packages | |||
---|---|---|---|---|
M | contrib/chg/chg.c (80 lines) | |||
M | contrib/chg/hgclient.h (4 lines) | |||
M | contrib/chg/hgclient.c (71 lines) | |||
M | contrib/chg/procutil.c (6 lines) | |||
M | contrib/chg/util.h (2 lines) | |||
M | contrib/chg/util.c (3 lines) | |||
M | contrib/clang-format-blacklist (7 lines) |
struct cmdserveropts { | struct cmdserveropts { | ||||
char sockname[PATH_MAX]; | char sockname[PATH_MAX]; | ||||
char initsockname[PATH_MAX]; | char initsockname[PATH_MAX]; | ||||
char redirectsockname[PATH_MAX]; | char redirectsockname[PATH_MAX]; | ||||
size_t argsize; | size_t argsize; | ||||
const char **args; | const char **args; | ||||
}; | }; | ||||
static void initcmdserveropts(struct cmdserveropts *opts) { | static void initcmdserveropts(struct cmdserveropts *opts) | ||||
{ | |||||
memset(opts, 0, sizeof(struct cmdserveropts)); | memset(opts, 0, sizeof(struct cmdserveropts)); | ||||
} | } | ||||
static void freecmdserveropts(struct cmdserveropts *opts) { | static void freecmdserveropts(struct cmdserveropts *opts) | ||||
{ | |||||
free(opts->args); | free(opts->args); | ||||
opts->args = NULL; | opts->args = NULL; | ||||
opts->argsize = 0; | opts->argsize = 0; | ||||
} | } | ||||
/* | /* | ||||
* Test if an argument is a sensitive flag that should be passed to the server. | * Test if an argument is a sensitive flag that should be passed to the server. | ||||
* Return 0 if not, otherwise the number of arguments starting from the current | * Return 0 if not, otherwise the number of arguments starting from the current | ||||
* one that should be passed to the server. | * one that should be passed to the server. | ||||
*/ | */ | ||||
static size_t testsensitiveflag(const char *arg) | static size_t testsensitiveflag(const char *arg) | ||||
{ | { | ||||
static const struct { | static const struct { | ||||
const char *name; | const char *name; | ||||
size_t narg; | size_t narg; | ||||
} flags[] = { | } flags[] = { | ||||
{"--config", 1}, | {"--config", 1}, {"--cwd", 1}, {"--repo", 1}, | ||||
{"--cwd", 1}, | {"--repository", 1}, {"--traceback", 0}, {"-R", 1}, | ||||
{"--repo", 1}, | |||||
{"--repository", 1}, | |||||
{"--traceback", 0}, | |||||
{"-R", 1}, | |||||
}; | }; | ||||
size_t i; | size_t i; | ||||
for (i = 0; i < sizeof(flags) / sizeof(flags[0]); ++i) { | for (i = 0; i < sizeof(flags) / sizeof(flags[0]); ++i) { | ||||
size_t len = strlen(flags[i].name); | size_t len = strlen(flags[i].name); | ||||
size_t narg = flags[i].narg; | size_t narg = flags[i].narg; | ||||
if (memcmp(arg, flags[i].name, len) == 0) { | if (memcmp(arg, flags[i].name, len) == 0) { | ||||
if (arg[len] == '\0') { | if (arg[len] == '\0') { | ||||
/* --flag (value) */ | /* --flag (value) */ | ||||
return narg + 1; | return narg + 1; | ||||
} else if (arg[len] == '=' && narg > 0) { | } else if (arg[len] == '=' && narg > 0) { | ||||
/* --flag=value */ | /* --flag=value */ | ||||
return 1; | return 1; | ||||
} else if (flags[i].name[1] != '-') { | } else if (flags[i].name[1] != '-') { | ||||
/* short flag */ | /* short flag */ | ||||
return 1; | return 1; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
/* | /* | ||||
* Parse argv[] and put sensitive flags to opts->args | * Parse argv[] and put sensitive flags to opts->args | ||||
*/ | */ | ||||
static void setcmdserverargs(struct cmdserveropts *opts, | static void setcmdserverargs(struct cmdserveropts *opts, int argc, | ||||
int argc, const char *argv[]) | const char *argv[]) | ||||
{ | { | ||||
size_t i, step; | size_t i, step; | ||||
opts->argsize = 0; | opts->argsize = 0; | ||||
for (i = 0, step = 1; i < (size_t)argc; i += step, step = 1) { | for (i = 0, step = 1; i < (size_t)argc; i += step, step = 1) { | ||||
if (!argv[i]) | if (!argv[i]) | ||||
continue; /* pass clang-analyse */ | continue; /* pass clang-analyse */ | ||||
if (strcmp(argv[i], "--") == 0) | if (strcmp(argv[i], "--") == 0) | ||||
break; | break; | ||||
size_t n = testsensitiveflag(argv[i]); | size_t n = testsensitiveflag(argv[i]); | ||||
if (n == 0 || i + n > (size_t)argc) | if (n == 0 || i + n > (size_t)argc) | ||||
continue; | continue; | ||||
opts->args = reallocx(opts->args, | opts->args = | ||||
(n + opts->argsize) * sizeof(char *)); | reallocx(opts->args, (n + opts->argsize) * sizeof(char *)); | ||||
memcpy(opts->args + opts->argsize, argv + i, | memcpy(opts->args + opts->argsize, argv + i, | ||||
sizeof(char *) * n); | sizeof(char *) * n); | ||||
opts->argsize += n; | opts->argsize += n; | ||||
step = n; | step = n; | ||||
} | } | ||||
} | } | ||||
static void preparesockdir(const char *sockdir) | static void preparesockdir(const char *sockdir) | ||||
preparesockdir(sockdir); | preparesockdir(sockdir); | ||||
} | } | ||||
const char *basename = (envsockname) ? envsockname : sockdir; | const char *basename = (envsockname) ? envsockname : sockdir; | ||||
const char *sockfmt = (envsockname) ? "%s" : "%s/server"; | const char *sockfmt = (envsockname) ? "%s" : "%s/server"; | ||||
r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename); | r = snprintf(opts->sockname, sizeof(opts->sockname), sockfmt, basename); | ||||
if (r < 0 || (size_t)r >= sizeof(opts->sockname)) | if (r < 0 || (size_t)r >= sizeof(opts->sockname)) | ||||
abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); | abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); | ||||
r = snprintf(opts->initsockname, sizeof(opts->initsockname), | r = snprintf(opts->initsockname, sizeof(opts->initsockname), "%s.%u", | ||||
"%s.%u", opts->sockname, (unsigned)getpid()); | opts->sockname, (unsigned)getpid()); | ||||
if (r < 0 || (size_t)r >= sizeof(opts->initsockname)) | if (r < 0 || (size_t)r >= sizeof(opts->initsockname)) | ||||
abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); | abortmsg("too long TMPDIR or CHGSOCKNAME (r = %d)", r); | ||||
} | } | ||||
static const char *gethgcmd(void) | static const char *gethgcmd(void) | ||||
{ | { | ||||
static const char *hgcmd = NULL; | static const char *hgcmd = NULL; | ||||
if (!hgcmd) { | if (!hgcmd) { | ||||
return hgcmd; | return hgcmd; | ||||
} | } | ||||
static void execcmdserver(const struct cmdserveropts *opts) | static void execcmdserver(const struct cmdserveropts *opts) | ||||
{ | { | ||||
const char *hgcmd = gethgcmd(); | const char *hgcmd = gethgcmd(); | ||||
const char *baseargv[] = { | const char *baseargv[] = { | ||||
hgcmd, | hgcmd, | ||||
"serve", | "serve", | ||||
"--cmdserver", "chgunix", | "--cmdserver", | ||||
"--address", opts->initsockname, | "chgunix", | ||||
"--daemon-postexec", "chdir:/", | "--address", | ||||
opts->initsockname, | |||||
"--daemon-postexec", | |||||
"chdir:/", | |||||
}; | }; | ||||
size_t baseargvsize = sizeof(baseargv) / sizeof(baseargv[0]); | size_t baseargvsize = sizeof(baseargv) / sizeof(baseargv[0]); | ||||
size_t argsize = baseargvsize + opts->argsize + 1; | size_t argsize = baseargvsize + opts->argsize + 1; | ||||
const char **argv = mallocx(sizeof(char *) * argsize); | const char **argv = mallocx(sizeof(char *) * argsize); | ||||
memcpy(argv, baseargv, sizeof(baseargv)); | memcpy(argv, baseargv, sizeof(baseargv)); | ||||
memcpy(argv + baseargvsize, opts->args, sizeof(char *) * opts->argsize); | memcpy(argv + baseargvsize, opts->args, sizeof(char *) * opts->argsize); | ||||
argv[argsize - 1] = NULL; | argv[argsize - 1] = NULL; | ||||
if (putenv("CHGINTERNALMARK=") != 0) | if (putenv("CHGINTERNALMARK=") != 0) | ||||
abortmsgerrno("failed to putenv"); | abortmsgerrno("failed to putenv"); | ||||
if (execvp(hgcmd, (char **)argv) < 0) | if (execvp(hgcmd, (char **)argv) < 0) | ||||
abortmsgerrno("failed to exec cmdserver"); | abortmsgerrno("failed to exec cmdserver"); | ||||
free(argv); | free(argv); | ||||
} | } | ||||
/* Retry until we can connect to the server. Give up after some time. */ | /* Retry until we can connect to the server. Give up after some time. */ | ||||
static hgclient_t *retryconnectcmdserver(struct cmdserveropts *opts, pid_t pid) | static hgclient_t *retryconnectcmdserver(struct cmdserveropts *opts, pid_t pid) | ||||
{ | { | ||||
static const struct timespec sleepreq = {0, 10 * 1000000}; | static const struct timespec sleepreq = {0, 10 * 1000000}; | ||||
int pst = 0; | int pst = 0; | ||||
debugmsg("try connect to %s repeatedly", opts->initsockname); | debugmsg("try connect to %s repeatedly", opts->initsockname); | ||||
unsigned int timeoutsec = 60; /* default: 60 seconds */ | unsigned int timeoutsec = 60; /* default: 60 seconds */ | ||||
const char *timeoutenv = getenv("CHGTIMEOUT"); | const char *timeoutenv = getenv("CHGTIMEOUT"); | ||||
if (timeoutenv) | if (timeoutenv) | ||||
sscanf(timeoutenv, "%u", &timeoutsec); | sscanf(timeoutenv, "%u", &timeoutsec); | ||||
for (unsigned int i = 0; !timeoutsec || i < timeoutsec * 100; i++) { | for (unsigned int i = 0; !timeoutsec || i < timeoutsec * 100; i++) { | ||||
hgclient_t *hgc = hgc_open(opts->initsockname); | hgclient_t *hgc = hgc_open(opts->initsockname); | ||||
if (hgc) { | if (hgc) { | ||||
debugmsg("rename %s to %s", opts->initsockname, | debugmsg("rename %s to %s", opts->initsockname, | ||||
opts->sockname); | opts->sockname); | ||||
int r = rename(opts->initsockname, opts->sockname); | int r = rename(opts->initsockname, opts->sockname); | ||||
if (r != 0) | if (r != 0) | ||||
abortmsgerrno("cannot rename"); | abortmsgerrno("cannot rename"); | ||||
return hgc; | return hgc; | ||||
} | } | ||||
if (pid > 0) { | if (pid > 0) { | ||||
/* collect zombie if child process fails to start */ | /* collect zombie if child process fails to start */ | ||||
int r = waitpid(pid, &pst, WNOHANG); | int r = waitpid(pid, &pst, WNOHANG); | ||||
if (r != 0) | if (r != 0) | ||||
goto cleanup; | goto cleanup; | ||||
} | } | ||||
nanosleep(&sleepreq, NULL); | nanosleep(&sleepreq, NULL); | ||||
} | } | ||||
abortmsg("timed out waiting for cmdserver %s", opts->initsockname); | abortmsg("timed out waiting for cmdserver %s", opts->initsockname); | ||||
return NULL; | return NULL; | ||||
cleanup: | cleanup: | ||||
if (WIFEXITED(pst)) { | if (WIFEXITED(pst)) { | ||||
if (WEXITSTATUS(pst) == 0) | if (WEXITSTATUS(pst) == 0) | ||||
abortmsg("could not connect to cmdserver " | abortmsg("could not connect to cmdserver " | ||||
"(exited with status 0)"); | "(exited with status 0)"); | ||||
debugmsg("cmdserver exited with status %d", WEXITSTATUS(pst)); | debugmsg("cmdserver exited with status %d", WEXITSTATUS(pst)); | ||||
exit(WEXITSTATUS(pst)); | exit(WEXITSTATUS(pst)); | ||||
} else if (WIFSIGNALED(pst)) { | } else if (WIFSIGNALED(pst)) { | ||||
abortmsg("cmdserver killed by signal %d", WTERMSIG(pst)); | abortmsg("cmdserver killed by signal %d", WTERMSIG(pst)); | ||||
} else { | } else { | ||||
abortmsg("error while waiting for cmdserver"); | abortmsg("error while waiting for cmdserver"); | ||||
} | } | ||||
return NULL; | return NULL; | ||||
} | } | ||||
/* Connect to a cmdserver. Will start a new server on demand. */ | /* Connect to a cmdserver. Will start a new server on demand. */ | ||||
static hgclient_t *connectcmdserver(struct cmdserveropts *opts) | static hgclient_t *connectcmdserver(struct cmdserveropts *opts) | ||||
{ | { | ||||
const char *sockname = opts->redirectsockname[0] ? | const char *sockname = | ||||
opts->redirectsockname : opts->sockname; | opts->redirectsockname[0] ? opts->redirectsockname : opts->sockname; | ||||
debugmsg("try connect to %s", sockname); | debugmsg("try connect to %s", sockname); | ||||
hgclient_t *hgc = hgc_open(sockname); | hgclient_t *hgc = hgc_open(sockname); | ||||
if (hgc) | if (hgc) | ||||
return hgc; | return hgc; | ||||
/* prevent us from being connected to an outdated server: we were | /* prevent us from being connected to an outdated server: we were | ||||
* told by a server to redirect to opts->redirectsockname and that | * told by a server to redirect to opts->redirectsockname and that | ||||
* address does not work. we do not want to connect to the server | * address does not work. we do not want to connect to the server | ||||
opts->redirectsockname[0] = '\0'; | opts->redirectsockname[0] = '\0'; | ||||
const char **pinst; | const char **pinst; | ||||
for (pinst = insts; *pinst; pinst++) { | for (pinst = insts; *pinst; pinst++) { | ||||
debugmsg("instruction: %s", *pinst); | debugmsg("instruction: %s", *pinst); | ||||
if (strncmp(*pinst, "unlink ", 7) == 0) { | if (strncmp(*pinst, "unlink ", 7) == 0) { | ||||
unlink(*pinst + 7); | unlink(*pinst + 7); | ||||
} else if (strncmp(*pinst, "redirect ", 9) == 0) { | } else if (strncmp(*pinst, "redirect ", 9) == 0) { | ||||
int r = snprintf(opts->redirectsockname, | int r = snprintf(opts->redirectsockname, | ||||
sizeof(opts->redirectsockname), | sizeof(opts->redirectsockname), "%s", | ||||
"%s", *pinst + 9); | *pinst + 9); | ||||
if (r < 0 || r >= (int)sizeof(opts->redirectsockname)) | if (r < 0 || r >= (int)sizeof(opts->redirectsockname)) | ||||
abortmsg("redirect path is too long (%d)", r); | abortmsg("redirect path is too long (%d)", r); | ||||
needreconnect = 1; | needreconnect = 1; | ||||
} else if (strncmp(*pinst, "exit ", 5) == 0) { | } else if (strncmp(*pinst, "exit ", 5) == 0) { | ||||
int n = 0; | int n = 0; | ||||
if (sscanf(*pinst + 5, "%d", &n) != 1) | if (sscanf(*pinst + 5, "%d", &n) != 1) | ||||
abortmsg("cannot read the exit code"); | abortmsg("cannot read the exit code"); | ||||
exit(n); | exit(n); | ||||
} else if (strcmp(*pinst, "reconnect") == 0) { | } else if (strcmp(*pinst, "reconnect") == 0) { | ||||
needreconnect = 1; | needreconnect = 1; | ||||
} else { | } else { | ||||
abortmsg("unknown instruction: %s", *pinst); | abortmsg("unknown instruction: %s", *pinst); | ||||
} | } | ||||
} | } | ||||
return needreconnect; | return needreconnect; | ||||
} | } | ||||
/* | /* | ||||
* Test whether the command is unsupported or not. This is not designed to | * Test whether the command is unsupported or not. This is not designed to | ||||
* cover all cases. But it's fast, does not depend on the server and does | * cover all cases. But it's fast, does not depend on the server and does | ||||
* not return false positives. | * not return false positives. | ||||
*/ | */ | ||||
static int isunsupported(int argc, const char *argv[]) | static int isunsupported(int argc, const char *argv[]) | ||||
{ | { | ||||
enum { | enum { SERVE = 1, | ||||
SERVE = 1, | |||||
DAEMON = 2, | DAEMON = 2, | ||||
SERVEDAEMON = SERVE | DAEMON, | SERVEDAEMON = SERVE | DAEMON, | ||||
}; | }; | ||||
unsigned int state = 0; | unsigned int state = 0; | ||||
int i; | int i; | ||||
for (i = 0; i < argc; ++i) { | for (i = 0; i < argc; ++i) { | ||||
if (strcmp(argv[i], "--") == 0) | if (strcmp(argv[i], "--") == 0) | ||||
break; | break; | ||||
if (i == 0 && strcmp("serve", argv[i]) == 0) | if (i == 0 && strcmp("serve", argv[i]) == 0) | ||||
state |= SERVE; | state |= SERVE; | ||||
else if (strcmp("-d", argv[i]) == 0 || | else if (strcmp("-d", argv[i]) == 0 || | ||||
strcmp("--daemon", argv[i]) == 0) | strcmp("--daemon", argv[i]) == 0) | ||||
state |= DAEMON; | state |= DAEMON; | ||||
} | } | ||||
return (state & SERVEDAEMON) == SERVEDAEMON; | return (state & SERVEDAEMON) == SERVEDAEMON; | ||||
} | } | ||||
static void execoriginalhg(const char *argv[]) | static void execoriginalhg(const char *argv[]) | ||||
{ | { | ||||
debugmsg("execute original hg"); | debugmsg("execute original hg"); | ||||
if (execvp(gethgcmd(), (char **)argv) < 0) | if (execvp(gethgcmd(), (char **)argv) < 0) | ||||
abortmsgerrno("failed to exec original hg"); | abortmsgerrno("failed to exec original hg"); | ||||
} | } | ||||
int main(int argc, const char *argv[], const char *envp[]) | int main(int argc, const char *argv[], const char *envp[]) | ||||
{ | { | ||||
if (getenv("CHGDEBUG")) | if (getenv("CHGDEBUG")) | ||||
enabledebugmsg(); | enabledebugmsg(); | ||||
if (!getenv("HGPLAIN") && isatty(fileno(stderr))) | if (!getenv("HGPLAIN") && isatty(fileno(stderr))) | ||||
enablecolor(); | enablecolor(); | ||||
if (getenv("CHGINTERNALMARK")) | if (getenv("CHGINTERNALMARK")) | ||||
abortmsg("chg started by chg detected.\n" | abortmsg("chg started by chg detected.\n" | ||||
"Please make sure ${HG:-hg} is not a symlink or " | "Please make sure ${HG:-hg} is not a symlink or " | ||||
"wrapper to chg. Alternatively, set $CHGHG to the " | "wrapper to chg. Alternatively, set $CHGHG to the " | ||||
"path of real hg."); | "path of real hg."); | ||||
if (isunsupported(argc - 1, argv + 1)) | if (isunsupported(argc - 1, argv + 1)) | ||||
execoriginalhg(argv); | execoriginalhg(argv); | ||||
struct cmdserveropts opts; | struct cmdserveropts opts; | ||||
initcmdserveropts(&opts); | initcmdserveropts(&opts); | ||||
setcmdserveropts(&opts); | setcmdserveropts(&opts); | ||||
setcmdserverargs(&opts, argc, argv); | setcmdserverargs(&opts, argc, argv); | ||||
const char **insts = hgc_validate(hgc, argv + 1, argc - 1); | const char **insts = hgc_validate(hgc, argv + 1, argc - 1); | ||||
int needreconnect = runinstructions(&opts, insts); | int needreconnect = runinstructions(&opts, insts); | ||||
free(insts); | free(insts); | ||||
if (!needreconnect) | if (!needreconnect) | ||||
break; | break; | ||||
hgc_close(hgc); | hgc_close(hgc); | ||||
if (++retry > 10) | if (++retry > 10) | ||||
abortmsg("too many redirections.\n" | abortmsg("too many redirections.\n" | ||||
"Please make sure %s is not a wrapper which " | "Please make sure %s is not a wrapper which " | ||||
"changes sensitive environment variables " | "changes sensitive environment variables " | ||||
"before executing hg. If you have to use a " | "before executing hg. If you have to use a " | ||||
"wrapper, wrap chg instead of hg.", | "wrapper, wrap chg instead of hg.", | ||||
gethgcmd()); | gethgcmd()); | ||||
} | } | ||||
setupsignalhandler(hgc_peerpid(hgc), hgc_peerpgid(hgc)); | setupsignalhandler(hgc_peerpid(hgc), hgc_peerpgid(hgc)); | ||||
atexit(waitpager); | atexit(waitpager); | ||||
int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1); | int exitcode = hgc_runcommand(hgc, argv + 1, argc - 1); | ||||
restoresignalhandler(); | restoresignalhandler(); | ||||
hgc_close(hgc); | hgc_close(hgc); | ||||
freecmdserveropts(&opts); | freecmdserveropts(&opts); | ||||
return exitcode; | return exitcode; | ||||
} | } |
hgclient_t *hgc_open(const char *sockname); | hgclient_t *hgc_open(const char *sockname); | ||||
void hgc_close(hgclient_t *hgc); | void hgc_close(hgclient_t *hgc); | ||||
pid_t hgc_peerpgid(const hgclient_t *hgc); | pid_t hgc_peerpgid(const hgclient_t *hgc); | ||||
pid_t hgc_peerpid(const hgclient_t *hgc); | pid_t hgc_peerpid(const hgclient_t *hgc); | ||||
const char **hgc_validate(hgclient_t *hgc, const char *const args[], | const char **hgc_validate(hgclient_t *hgc, const char *const args[], | ||||
size_t argsize); | size_t argsize); | ||||
int hgc_runcommand(hgclient_t *hgc, const char *const args[], size_t argsize); | int hgc_runcommand(hgclient_t *hgc, const char *const args[], size_t argsize); | ||||
void hgc_attachio(hgclient_t *hgc); | void hgc_attachio(hgclient_t *hgc); | ||||
void hgc_setenv(hgclient_t *hgc, const char *const envp[]); | void hgc_setenv(hgclient_t *hgc, const char *const envp[]); | ||||
#endif /* HGCLIENT_H_ */ | #endif /* HGCLIENT_H_ */ |
/* | /* | ||||
* A command server client that uses Unix domain socket | * A command server client that uses Unix domain socket | ||||
* | * | ||||
* Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org> | * Copyright (c) 2011 Yuya Nishihara <yuya@tcha.org> | ||||
* | * | ||||
* This software may be used and distributed according to the terms of the | * This software may be used and distributed according to the terms of the | ||||
* GNU General Public License version 2 or any later version. | * GNU General Public License version 2 or any later version. | ||||
*/ | */ | ||||
#include <arpa/inet.h> /* for ntohl(), htonl() */ | #include <arpa/inet.h> /* for ntohl(), htonl() */ | ||||
#include <assert.h> | #include <assert.h> | ||||
#include <ctype.h> | #include <ctype.h> | ||||
#include <errno.h> | #include <errno.h> | ||||
#include <fcntl.h> | #include <fcntl.h> | ||||
#include <signal.h> | #include <signal.h> | ||||
#include <stdint.h> | #include <stdint.h> | ||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <sys/socket.h> | #include <sys/socket.h> | ||||
#include <sys/stat.h> | #include <sys/stat.h> | ||||
#include <sys/un.h> | #include <sys/un.h> | ||||
#include <unistd.h> | #include <unistd.h> | ||||
#include "hgclient.h" | #include "hgclient.h" | ||||
#include "procutil.h" | #include "procutil.h" | ||||
#include "util.h" | #include "util.h" | ||||
enum { | enum { CAP_GETENCODING = 0x0001, | ||||
CAP_GETENCODING = 0x0001, | |||||
CAP_RUNCOMMAND = 0x0002, | CAP_RUNCOMMAND = 0x0002, | ||||
/* cHg extension: */ | /* cHg extension: */ | ||||
CAP_ATTACHIO = 0x0100, | CAP_ATTACHIO = 0x0100, | ||||
CAP_CHDIR = 0x0200, | CAP_CHDIR = 0x0200, | ||||
CAP_SETENV = 0x0800, | CAP_SETENV = 0x0800, | ||||
CAP_SETUMASK = 0x1000, | CAP_SETUMASK = 0x1000, | ||||
CAP_VALIDATE = 0x2000, | CAP_VALIDATE = 0x2000, | ||||
CAP_SETPROCNAME = 0x4000, | CAP_SETPROCNAME = 0x4000, | ||||
}; | }; | ||||
typedef struct { | typedef struct { | ||||
const char *name; | const char *name; | ||||
unsigned int flag; | unsigned int flag; | ||||
} cappair_t; | } cappair_t; | ||||
static const cappair_t captable[] = { | static const cappair_t captable[] = { | ||||
{"getencoding", CAP_GETENCODING}, | {"getencoding", CAP_GETENCODING}, | ||||
{"runcommand", CAP_RUNCOMMAND}, | {"runcommand", CAP_RUNCOMMAND}, | ||||
{"attachio", CAP_ATTACHIO}, | {"attachio", CAP_ATTACHIO}, | ||||
{"chdir", CAP_CHDIR}, | {"chdir", CAP_CHDIR}, | ||||
{"setenv", CAP_SETENV}, | {"setenv", CAP_SETENV}, | ||||
{"setumask", CAP_SETUMASK}, | {"setumask", CAP_SETUMASK}, | ||||
{"validate", CAP_VALIDATE}, | {"validate", CAP_VALIDATE}, | ||||
{"setprocname", CAP_SETPROCNAME}, | {"setprocname", CAP_SETPROCNAME}, | ||||
{NULL, 0}, /* terminator */ | {NULL, 0}, /* terminator */ | ||||
}; | }; | ||||
typedef struct { | typedef struct { | ||||
char ch; | char ch; | ||||
char *data; | char *data; | ||||
size_t maxdatasize; | size_t maxdatasize; | ||||
size_t datasize; | size_t datasize; | ||||
} context_t; | } context_t; | ||||
debugmsg("initialize context buffer with size %zu", ctx->maxdatasize); | debugmsg("initialize context buffer with size %zu", ctx->maxdatasize); | ||||
} | } | ||||
static void enlargecontext(context_t *ctx, size_t newsize) | static void enlargecontext(context_t *ctx, size_t newsize) | ||||
{ | { | ||||
if (newsize <= ctx->maxdatasize) | if (newsize <= ctx->maxdatasize) | ||||
return; | return; | ||||
newsize = defaultdatasize | newsize = defaultdatasize * | ||||
* ((newsize + defaultdatasize - 1) / defaultdatasize); | ((newsize + defaultdatasize - 1) / defaultdatasize); | ||||
ctx->data = reallocx(ctx->data, newsize); | ctx->data = reallocx(ctx->data, newsize); | ||||
ctx->maxdatasize = newsize; | ctx->maxdatasize = newsize; | ||||
debugmsg("enlarge context buffer to %zu", ctx->maxdatasize); | debugmsg("enlarge context buffer to %zu", ctx->maxdatasize); | ||||
} | } | ||||
static void freecontext(context_t *ctx) | static void freecontext(context_t *ctx) | ||||
{ | { | ||||
debugmsg("free context buffer"); | debugmsg("free context buffer"); | ||||
if (rsize != sizeof(datasize_n)) | if (rsize != sizeof(datasize_n)) | ||||
abortmsg("failed to read data size"); | abortmsg("failed to read data size"); | ||||
/* datasize denotes the maximum size to write if input request */ | /* datasize denotes the maximum size to write if input request */ | ||||
hgc->ctx.datasize = ntohl(datasize_n); | hgc->ctx.datasize = ntohl(datasize_n); | ||||
enlargecontext(&hgc->ctx, hgc->ctx.datasize); | enlargecontext(&hgc->ctx, hgc->ctx.datasize); | ||||
if (isupper(hgc->ctx.ch) && hgc->ctx.ch != 'S') | if (isupper(hgc->ctx.ch) && hgc->ctx.ch != 'S') | ||||
return; /* assumes input request */ | return; /* assumes input request */ | ||||
size_t cursize = 0; | size_t cursize = 0; | ||||
while (cursize < hgc->ctx.datasize) { | while (cursize < hgc->ctx.datasize) { | ||||
rsize = recv(hgc->sockfd, hgc->ctx.data + cursize, | rsize = recv(hgc->sockfd, hgc->ctx.data + cursize, | ||||
hgc->ctx.datasize - cursize, 0); | hgc->ctx.datasize - cursize, 0); | ||||
if (rsize < 1) | if (rsize < 1) | ||||
abortmsg("failed to read data block"); | abortmsg("failed to read data block"); | ||||
cursize += rsize; | cursize += rsize; | ||||
} | } | ||||
} | } | ||||
static void sendall(int sockfd, const void *data, size_t datasize) | static void sendall(int sockfd, const void *data, size_t datasize) | ||||
{ | { | ||||
sendall(hgc->sockfd, buf, sizeof(buf)); | sendall(hgc->sockfd, buf, sizeof(buf)); | ||||
writeblock(hgc); | writeblock(hgc); | ||||
} | } | ||||
/* Build '\0'-separated list of args. argsize < 0 denotes that args are | /* Build '\0'-separated list of args. argsize < 0 denotes that args are | ||||
* terminated by NULL. */ | * terminated by NULL. */ | ||||
static void packcmdargs(context_t *ctx, const char *const args[], | static void packcmdargs(context_t *ctx, const char *const args[], | ||||
ssize_t argsize) | ssize_t argsize) | ||||
{ | { | ||||
ctx->datasize = 0; | ctx->datasize = 0; | ||||
const char *const *const end = (argsize >= 0) ? args + argsize : NULL; | const char *const *const end = (argsize >= 0) ? args + argsize : NULL; | ||||
for (const char *const *it = args; it != end && *it; ++it) { | for (const char *const *it = args; it != end && *it; ++it) { | ||||
const size_t n = strlen(*it) + 1; /* include '\0' */ | const size_t n = strlen(*it) + 1; /* include '\0' */ | ||||
enlargecontext(ctx, ctx->datasize + n); | enlargecontext(ctx, ctx->datasize + n); | ||||
memcpy(ctx->data + ctx->datasize, *it, n); | memcpy(ctx->data + ctx->datasize, *it, n); | ||||
ctx->datasize += n; | ctx->datasize += n; | ||||
} | } | ||||
if (ctx->datasize > 0) | if (ctx->datasize > 0) | ||||
--ctx->datasize; /* strip last '\0' */ | --ctx->datasize; /* strip last '\0' */ | ||||
} | } | ||||
/* Extract '\0'-separated list of args to new buffer, terminated by NULL */ | /* Extract '\0'-separated list of args to new buffer, terminated by NULL */ | ||||
static const char **unpackcmdargsnul(const context_t *ctx) | static const char **unpackcmdargsnul(const context_t *ctx) | ||||
{ | { | ||||
const char **args = NULL; | const char **args = NULL; | ||||
size_t nargs = 0, maxnargs = 0; | size_t nargs = 0, maxnargs = 0; | ||||
const char *s = ctx->data; | const char *s = ctx->data; | ||||
const char *e = ctx->data + ctx->datasize; | const char *e = ctx->data + ctx->datasize; | ||||
for (;;) { | for (;;) { | ||||
if (nargs + 1 >= maxnargs) { /* including last NULL */ | if (nargs + 1 >= maxnargs) { /* including last NULL */ | ||||
maxnargs += 256; | maxnargs += 256; | ||||
args = reallocx(args, maxnargs * sizeof(args[0])); | args = reallocx(args, maxnargs * sizeof(args[0])); | ||||
} | } | ||||
args[nargs] = s; | args[nargs] = s; | ||||
nargs++; | nargs++; | ||||
s = memchr(s, '\0', e - s); | s = memchr(s, '\0', e - s); | ||||
if (!s) | if (!s) | ||||
break; | break; | ||||
writeblock(hgc); | writeblock(hgc); | ||||
} | } | ||||
/* Execute the requested command and write exit code */ | /* Execute the requested command and write exit code */ | ||||
static void handlesystemrequest(hgclient_t *hgc) | static void handlesystemrequest(hgclient_t *hgc) | ||||
{ | { | ||||
context_t *ctx = &hgc->ctx; | context_t *ctx = &hgc->ctx; | ||||
enlargecontext(ctx, ctx->datasize + 1); | enlargecontext(ctx, ctx->datasize + 1); | ||||
ctx->data[ctx->datasize] = '\0'; /* terminate last string */ | ctx->data[ctx->datasize] = '\0'; /* terminate last string */ | ||||
const char **args = unpackcmdargsnul(ctx); | const char **args = unpackcmdargsnul(ctx); | ||||
if (!args[0] || !args[1] || !args[2]) | if (!args[0] || !args[1] || !args[2]) | ||||
abortmsg("missing type or command or cwd in system request"); | abortmsg("missing type or command or cwd in system request"); | ||||
if (strcmp(args[0], "system") == 0) { | if (strcmp(args[0], "system") == 0) { | ||||
debugmsg("run '%s' at '%s'", args[1], args[2]); | debugmsg("run '%s' at '%s'", args[1], args[2]); | ||||
int32_t r = runshellcmd(args[1], args + 3, args[2]); | int32_t r = runshellcmd(args[1], args + 3, args[2]); | ||||
free(args); | free(args); | ||||
} | } | ||||
/* Read response of command execution until receiving 'r'-esult */ | /* Read response of command execution until receiving 'r'-esult */ | ||||
static void handleresponse(hgclient_t *hgc) | static void handleresponse(hgclient_t *hgc) | ||||
{ | { | ||||
for (;;) { | for (;;) { | ||||
readchannel(hgc); | readchannel(hgc); | ||||
context_t *ctx = &hgc->ctx; | context_t *ctx = &hgc->ctx; | ||||
debugmsg("response read from channel %c, size %zu", | debugmsg("response read from channel %c, size %zu", ctx->ch, | ||||
ctx->ch, ctx->datasize); | ctx->datasize); | ||||
switch (ctx->ch) { | switch (ctx->ch) { | ||||
case 'o': | case 'o': | ||||
fwrite(ctx->data, sizeof(ctx->data[0]), ctx->datasize, | fwrite(ctx->data, sizeof(ctx->data[0]), ctx->datasize, | ||||
stdout); | stdout); | ||||
break; | break; | ||||
case 'e': | case 'e': | ||||
fwrite(ctx->data, sizeof(ctx->data[0]), ctx->datasize, | fwrite(ctx->data, sizeof(ctx->data[0]), ctx->datasize, | ||||
stderr); | stderr); | ||||
handlereadlinerequest(hgc); | handlereadlinerequest(hgc); | ||||
break; | break; | ||||
case 'S': | case 'S': | ||||
handlesystemrequest(hgc); | handlesystemrequest(hgc); | ||||
break; | break; | ||||
default: | default: | ||||
if (isupper(ctx->ch)) | if (isupper(ctx->ch)) | ||||
abortmsg("cannot handle response (ch = %c)", | abortmsg("cannot handle response (ch = %c)", | ||||
ctx->ch); | ctx->ch); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
static unsigned int parsecapabilities(const char *s, const char *e) | static unsigned int parsecapabilities(const char *s, const char *e) | ||||
{ | { | ||||
unsigned int flags = 0; | unsigned int flags = 0; | ||||
while (s < e) { | while (s < e) { | ||||
} | } | ||||
s = u + 1; | s = u + 1; | ||||
} | } | ||||
debugmsg("capflags=0x%04x, pid=%d", hgc->capflags, hgc->pid); | debugmsg("capflags=0x%04x, pid=%d", hgc->capflags, hgc->pid); | ||||
} | } | ||||
static void updateprocname(hgclient_t *hgc) | static void updateprocname(hgclient_t *hgc) | ||||
{ | { | ||||
int r = snprintf(hgc->ctx.data, hgc->ctx.maxdatasize, | int r = snprintf(hgc->ctx.data, hgc->ctx.maxdatasize, "chg[worker/%d]", | ||||
"chg[worker/%d]", (int)getpid()); | (int)getpid()); | ||||
if (r < 0 || (size_t)r >= hgc->ctx.maxdatasize) | if (r < 0 || (size_t)r >= hgc->ctx.maxdatasize) | ||||
abortmsg("insufficient buffer to write procname (r = %d)", r); | abortmsg("insufficient buffer to write procname (r = %d)", r); | ||||
hgc->ctx.datasize = (size_t)r; | hgc->ctx.datasize = (size_t)r; | ||||
writeblockrequest(hgc, "setprocname"); | writeblockrequest(hgc, "setprocname"); | ||||
} | } | ||||
static void attachio(hgclient_t *hgc) | static void attachio(hgclient_t *hgc) | ||||
{ | { | ||||
debugmsg("request attachio"); | debugmsg("request attachio"); | ||||
static const char chcmd[] = "attachio\n"; | static const char chcmd[] = "attachio\n"; | ||||
sendall(hgc->sockfd, chcmd, sizeof(chcmd) - 1); | sendall(hgc->sockfd, chcmd, sizeof(chcmd) - 1); | ||||
readchannel(hgc); | readchannel(hgc); | ||||
context_t *ctx = &hgc->ctx; | context_t *ctx = &hgc->ctx; | ||||
if (ctx->ch != 'I') | if (ctx->ch != 'I') | ||||
abortmsg("unexpected response for attachio (ch = %c)", ctx->ch); | abortmsg("unexpected response for attachio (ch = %c)", ctx->ch); | ||||
static const int fds[3] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}; | static const int fds[3] = {STDIN_FILENO, STDOUT_FILENO, STDERR_FILENO}; | ||||
struct msghdr msgh; | struct msghdr msgh; | ||||
memset(&msgh, 0, sizeof(msgh)); | memset(&msgh, 0, sizeof(msgh)); | ||||
struct iovec iov = {ctx->data, ctx->datasize}; /* dummy payload */ | struct iovec iov = {ctx->data, ctx->datasize}; /* dummy payload */ | ||||
msgh.msg_iov = &iov; | msgh.msg_iov = &iov; | ||||
msgh.msg_iovlen = 1; | msgh.msg_iovlen = 1; | ||||
char fdbuf[CMSG_SPACE(sizeof(fds))]; | char fdbuf[CMSG_SPACE(sizeof(fds))]; | ||||
msgh.msg_control = fdbuf; | msgh.msg_control = fdbuf; | ||||
msgh.msg_controllen = sizeof(fdbuf); | msgh.msg_controllen = sizeof(fdbuf); | ||||
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msgh); | struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msgh); | ||||
cmsg->cmsg_level = SOL_SOCKET; | cmsg->cmsg_level = SOL_SOCKET; | ||||
cmsg->cmsg_type = SCM_RIGHTS; | cmsg->cmsg_type = SCM_RIGHTS; | ||||
* support "validate" command. | * support "validate" command. | ||||
* - a list of strings, the server probably cannot handle our request | * - a list of strings, the server probably cannot handle our request | ||||
* and it sent instructions telling us what to do next. See | * and it sent instructions telling us what to do next. See | ||||
* chgserver.py for possible instruction formats. | * chgserver.py for possible instruction formats. | ||||
* the list should be freed by the caller. | * the list should be freed by the caller. | ||||
* the last string is guaranteed to be NULL. | * the last string is guaranteed to be NULL. | ||||
*/ | */ | ||||
const char **hgc_validate(hgclient_t *hgc, const char *const args[], | const char **hgc_validate(hgclient_t *hgc, const char *const args[], | ||||
size_t argsize) | size_t argsize) | ||||
{ | { | ||||
assert(hgc); | assert(hgc); | ||||
if (!(hgc->capflags & CAP_VALIDATE)) | if (!(hgc->capflags & CAP_VALIDATE)) | ||||
return NULL; | return NULL; | ||||
packcmdargs(&hgc->ctx, args, argsize); | packcmdargs(&hgc->ctx, args, argsize); | ||||
writeblockrequest(hgc, "validate"); | writeblockrequest(hgc, "validate"); | ||||
handleresponse(hgc); | handleresponse(hgc); |
goto error; | goto error; | ||||
memset(&sa, 0, sizeof(sa)); | memset(&sa, 0, sizeof(sa)); | ||||
sa.sa_handler = SIG_DFL; | sa.sa_handler = SIG_DFL; | ||||
sa.sa_flags = SA_RESTART; | sa.sa_flags = SA_RESTART; | ||||
if (sigemptyset(&sa.sa_mask) < 0) | if (sigemptyset(&sa.sa_mask) < 0) | ||||
goto error; | goto error; | ||||
forwardsignal(sig); | forwardsignal(sig); | ||||
if (raise(sig) < 0) /* resend to self */ | if (raise(sig) < 0) /* resend to self */ | ||||
goto error; | goto error; | ||||
if (sigaction(sig, &sa, &oldsa) < 0) | if (sigaction(sig, &sa, &oldsa) < 0) | ||||
goto error; | goto error; | ||||
if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0) | if (sigprocmask(SIG_UNBLOCK, &unblockset, &oldset) < 0) | ||||
goto error; | goto error; | ||||
/* resent signal will be handled before sigprocmask() returns */ | /* resent signal will be handled before sigprocmask() returns */ | ||||
if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0) | if (sigprocmask(SIG_SETMASK, &oldset, NULL) < 0) | ||||
goto error; | goto error; | ||||
close(pipefds[1]); | close(pipefds[1]); | ||||
pagerpid = pid; | pagerpid = pid; | ||||
return pid; | return pid; | ||||
} else { | } else { | ||||
dup2(pipefds[0], fileno(stdin)); | dup2(pipefds[0], fileno(stdin)); | ||||
close(pipefds[0]); | close(pipefds[0]); | ||||
close(pipefds[1]); | close(pipefds[1]); | ||||
int r = execle("/bin/sh", "/bin/sh", "-c", pagercmd, NULL, | int r = | ||||
envp); | execle("/bin/sh", "/bin/sh", "-c", pagercmd, NULL, envp); | ||||
if (r < 0) { | if (r < 0) { | ||||
abortmsgerrno("cannot start pager '%s'", pagercmd); | abortmsgerrno("cannot start pager '%s'", pagercmd); | ||||
} | } | ||||
return 0; | return 0; | ||||
} | } | ||||
error: | error: | ||||
close(pipefds[0]); | close(pipefds[0]); |
void fchdirx(int dirfd); | void fchdirx(int dirfd); | ||||
void fsetcloexec(int fd); | void fsetcloexec(int fd); | ||||
void *mallocx(size_t size); | void *mallocx(size_t size); | ||||
void *reallocx(void *ptr, size_t size); | void *reallocx(void *ptr, size_t size); | ||||
int runshellcmd(const char *cmd, const char *envp[], const char *cwd); | int runshellcmd(const char *cmd, const char *envp[], const char *cwd); | ||||
#endif /* UTIL_H_ */ | #endif /* UTIL_H_ */ |
va_start(args, fmt); | va_start(args, fmt); | ||||
vabortmsgerrno(no, fmt, args); | vabortmsgerrno(no, fmt, args); | ||||
va_end(args); | va_end(args); | ||||
} | } | ||||
static int debugmsgenabled = 0; | static int debugmsgenabled = 0; | ||||
static double debugstart = 0; | static double debugstart = 0; | ||||
static double now() { | static double now() | ||||
{ | |||||
struct timeval t; | struct timeval t; | ||||
gettimeofday(&t, NULL); | gettimeofday(&t, NULL); | ||||
return t.tv_usec / 1e6 + t.tv_sec; | return t.tv_usec / 1e6 + t.tv_sec; | ||||
} | } | ||||
void enablecolor(void) | void enablecolor(void) | ||||
{ | { | ||||
colorenabled = 1; | colorenabled = 1; |
# Files that just need to be migrated to the formatter. | # Files that just need to be migrated to the formatter. | ||||
# Do not add new files here! | # Do not add new files here! | ||||
contrib/chg/chg.c | |||||
contrib/chg/hgclient.c | |||||
contrib/chg/hgclient.h | |||||
contrib/chg/procutil.c | |||||
contrib/chg/procutil.h | |||||
contrib/chg/util.c | |||||
contrib/chg/util.h | |||||
contrib/hgsh/hgsh.c | contrib/hgsh/hgsh.c | ||||
mercurial/cext/base85.c | mercurial/cext/base85.c | ||||
mercurial/cext/bdiff.c | mercurial/cext/bdiff.c | ||||
mercurial/cext/charencode.c | mercurial/cext/charencode.c | ||||
mercurial/cext/charencode.h | mercurial/cext/charencode.h | ||||
mercurial/cext/diffhelpers.c | mercurial/cext/diffhelpers.c | ||||
mercurial/cext/dirs.c | mercurial/cext/dirs.c | ||||
mercurial/cext/manifest.c | mercurial/cext/manifest.c |