comparison contrib/chg/chg.c @ 28789:7f6e0a15189b

chg: replace abortmsg showing errno with abortmsgerrno Since we have abortmsgerrno now, use it to show human friendly error messages across platforms.
author Jun Wu <quark@fb.com>
date Tue, 05 Apr 2016 15:16:01 +0100
parents ea86cdcd9b50
children b0cc9652e8dc
comparison
equal deleted inserted replaced
28788:57a78a64de44 28789:7f6e0a15189b
112 static void preparesockdir(const char *sockdir) 112 static void preparesockdir(const char *sockdir)
113 { 113 {
114 int r; 114 int r;
115 r = mkdir(sockdir, 0700); 115 r = mkdir(sockdir, 0700);
116 if (r < 0 && errno != EEXIST) 116 if (r < 0 && errno != EEXIST)
117 abortmsg("cannot create sockdir %s (errno = %d)", 117 abortmsgerrno("cannot create sockdir %s", sockdir);
118 sockdir, errno);
119 118
120 struct stat st; 119 struct stat st;
121 r = lstat(sockdir, &st); 120 r = lstat(sockdir, &st);
122 if (r < 0) 121 if (r < 0)
123 abortmsg("cannot stat %s (errno = %d)", sockdir, errno); 122 abortmsgerrno("cannot stat %s", sockdir);
124 if (!S_ISDIR(st.st_mode)) 123 if (!S_ISDIR(st.st_mode))
125 abortmsg("cannot create sockdir %s (file exists)", sockdir); 124 abortmsg("cannot create sockdir %s (file exists)", sockdir);
126 if (st.st_uid != geteuid() || st.st_mode & 0077) 125 if (st.st_uid != geteuid() || st.st_mode & 0077)
127 abortmsg("insecure sockdir %s", sockdir); 126 abortmsg("insecure sockdir %s", sockdir);
128 } 127 }
164 static void lockcmdserver(struct cmdserveropts *opts) 163 static void lockcmdserver(struct cmdserveropts *opts)
165 { 164 {
166 if (opts->lockfd == -1) { 165 if (opts->lockfd == -1) {
167 opts->lockfd = open(opts->lockfile, O_RDWR | O_CREAT | O_NOFOLLOW, 0600); 166 opts->lockfd = open(opts->lockfile, O_RDWR | O_CREAT | O_NOFOLLOW, 0600);
168 if (opts->lockfd == -1) 167 if (opts->lockfd == -1)
169 abortmsg("cannot create lock file %s", opts->lockfile); 168 abortmsgerrno("cannot create lock file %s",
169 opts->lockfile);
170 } 170 }
171 int r = flock(opts->lockfd, LOCK_EX); 171 int r = flock(opts->lockfd, LOCK_EX);
172 if (r == -1) 172 if (r == -1)
173 abortmsg("cannot acquire lock"); 173 abortmsgerrno("cannot acquire lock");
174 } 174 }
175 175
176 /* 176 /*
177 * Release the file lock held by calling lockcmdserver. Will do nothing if 177 * Release the file lock held by calling lockcmdserver. Will do nothing if
178 * lockcmdserver is not called. 178 * lockcmdserver is not called.
222 memcpy(argv, baseargv, sizeof(baseargv)); 222 memcpy(argv, baseargv, sizeof(baseargv));
223 memcpy(argv + baseargvsize, opts->args, sizeof(char *) * opts->argsize); 223 memcpy(argv + baseargvsize, opts->args, sizeof(char *) * opts->argsize);
224 argv[argsize - 1] = NULL; 224 argv[argsize - 1] = NULL;
225 225
226 if (putenv("CHGINTERNALMARK=") != 0) 226 if (putenv("CHGINTERNALMARK=") != 0)
227 abortmsg("failed to putenv (errno = %d)", errno); 227 abortmsgerrno("failed to putenv");
228 if (execvp(hgcmd, (char **)argv) < 0) 228 if (execvp(hgcmd, (char **)argv) < 0)
229 abortmsg("failed to exec cmdserver (errno = %d)", errno); 229 abortmsgerrno("failed to exec cmdserver");
230 free(argv); 230 free(argv);
231 } 231 }
232 232
233 /* Retry until we can connect to the server. Give up after some time. */ 233 /* Retry until we can connect to the server. Give up after some time. */
234 static hgclient_t *retryconnectcmdserver(struct cmdserveropts *opts, pid_t pid) 234 static hgclient_t *retryconnectcmdserver(struct cmdserveropts *opts, pid_t pid)
323 323
324 static void forwardsignal(int sig) 324 static void forwardsignal(int sig)
325 { 325 {
326 assert(peerpid > 0); 326 assert(peerpid > 0);
327 if (kill(peerpid, sig) < 0) 327 if (kill(peerpid, sig) < 0)
328 abortmsg("cannot kill %d (errno = %d)", peerpid, errno); 328 abortmsgerrno("cannot kill %d", peerpid);
329 debugmsg("forward signal %d", sig); 329 debugmsg("forward signal %d", sig);
330 } 330 }
331 331
332 static void handlestopsignal(int sig) 332 static void handlestopsignal(int sig)
333 { 333 {
356 if (sigaction(sig, &oldsa, NULL) < 0) 356 if (sigaction(sig, &oldsa, NULL) < 0)
357 goto error; 357 goto error;
358 return; 358 return;
359 359
360 error: 360 error:
361 abortmsg("failed to handle stop signal (errno = %d)", errno); 361 abortmsgerrno("failed to handle stop signal");
362 } 362 }
363 363
364 static void setupsignalhandler(pid_t pid) 364 static void setupsignalhandler(pid_t pid)
365 { 365 {
366 if (pid <= 0) 366 if (pid <= 0)
395 goto error; 395 goto error;
396 396
397 return; 397 return;
398 398
399 error: 399 error:
400 abortmsg("failed to set up signal handlers (errno = %d)", errno); 400 abortmsgerrno("failed to set up signal handlers");
401 } 401 }
402 402
403 /* This implementation is based on hgext/pager.py (pre 369741ef7253) */ 403 /* This implementation is based on hgext/pager.py (pre 369741ef7253) */
404 static void setuppager(hgclient_t *hgc, const char *const args[], 404 static void setuppager(hgclient_t *hgc, const char *const args[],
405 size_t argsize) 405 size_t argsize)
430 close(pipefds[0]); 430 close(pipefds[0]);
431 close(pipefds[1]); 431 close(pipefds[1]);
432 432
433 int r = execlp("/bin/sh", "/bin/sh", "-c", pagercmd, NULL); 433 int r = execlp("/bin/sh", "/bin/sh", "-c", pagercmd, NULL);
434 if (r < 0) { 434 if (r < 0) {
435 abortmsg("cannot start pager '%s' (errno = %d)", 435 abortmsgerrno("cannot start pager '%s'", pagercmd);
436 pagercmd, errno);
437 } 436 }
438 return; 437 return;
439 } 438 }
440 439
441 error: 440 error:
442 close(pipefds[0]); 441 close(pipefds[0]);
443 close(pipefds[1]); 442 close(pipefds[1]);
444 abortmsg("failed to prepare pager (errno = %d)", errno); 443 abortmsgerrno("failed to prepare pager");
445 } 444 }
446 445
447 /* Run instructions sent from the server like unlink and set redirect path 446 /* Run instructions sent from the server like unlink and set redirect path
448 * Return 1 if reconnect is needed, otherwise 0 */ 447 * Return 1 if reconnect is needed, otherwise 0 */
449 static int runinstructions(struct cmdserveropts *opts, const char **insts) 448 static int runinstructions(struct cmdserveropts *opts, const char **insts)
512 511
513 static void execoriginalhg(const char *argv[]) 512 static void execoriginalhg(const char *argv[])
514 { 513 {
515 debugmsg("execute original hg"); 514 debugmsg("execute original hg");
516 if (execvp(gethgcmd(), (char **)argv) < 0) 515 if (execvp(gethgcmd(), (char **)argv) < 0)
517 abortmsg("failed to exec original hg (errno = %d)", errno); 516 abortmsgerrno("failed to exec original hg");
518 } 517 }
519 518
520 int main(int argc, const char *argv[], const char *envp[]) 519 int main(int argc, const char *argv[], const char *envp[])
521 { 520 {
522 if (getenv("CHGDEBUG")) 521 if (getenv("CHGDEBUG"))