comparison contrib/chg/hgclient.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 222f482930c8
children f5764e177bbe
comparison
equal deleted inserted replaced
28788:57a78a64de44 28789:7f6e0a15189b
139 const char *p = data; 139 const char *p = data;
140 const char *const endp = p + datasize; 140 const char *const endp = p + datasize;
141 while (p < endp) { 141 while (p < endp) {
142 ssize_t r = send(sockfd, p, endp - p, 0); 142 ssize_t r = send(sockfd, p, endp - p, 0);
143 if (r < 0) 143 if (r < 0)
144 abortmsg("cannot communicate (errno = %d)", errno); 144 abortmsgerrno("cannot communicate");
145 p += r; 145 p += r;
146 } 146 }
147 } 147 }
148 148
149 /* Write lengh-data block to cmdserver */ 149 /* Write lengh-data block to cmdserver */
372 cmsg->cmsg_len = CMSG_LEN(sizeof(fds)); 372 cmsg->cmsg_len = CMSG_LEN(sizeof(fds));
373 memcpy(CMSG_DATA(cmsg), fds, sizeof(fds)); 373 memcpy(CMSG_DATA(cmsg), fds, sizeof(fds));
374 msgh.msg_controllen = cmsg->cmsg_len; 374 msgh.msg_controllen = cmsg->cmsg_len;
375 ssize_t r = sendmsg(hgc->sockfd, &msgh, 0); 375 ssize_t r = sendmsg(hgc->sockfd, &msgh, 0);
376 if (r < 0) 376 if (r < 0)
377 abortmsg("sendmsg failed (errno = %d)", errno); 377 abortmsgerrno("sendmsg failed");
378 378
379 handleresponse(hgc); 379 handleresponse(hgc);
380 int32_t n; 380 int32_t n;
381 if (ctx->datasize != sizeof(n)) 381 if (ctx->datasize != sizeof(n))
382 abortmsg("unexpected size of attachio result"); 382 abortmsg("unexpected size of attachio result");
387 } 387 }
388 388
389 static void chdirtocwd(hgclient_t *hgc) 389 static void chdirtocwd(hgclient_t *hgc)
390 { 390 {
391 if (!getcwd(hgc->ctx.data, hgc->ctx.maxdatasize)) 391 if (!getcwd(hgc->ctx.data, hgc->ctx.maxdatasize))
392 abortmsg("failed to getcwd (errno = %d)", errno); 392 abortmsgerrno("failed to getcwd");
393 hgc->ctx.datasize = strlen(hgc->ctx.data); 393 hgc->ctx.datasize = strlen(hgc->ctx.data);
394 writeblockrequest(hgc, "chdir"); 394 writeblockrequest(hgc, "chdir");
395 } 395 }
396 396
397 static void forwardumask(hgclient_t *hgc) 397 static void forwardumask(hgclient_t *hgc)
412 */ 412 */
413 hgclient_t *hgc_open(const char *sockname) 413 hgclient_t *hgc_open(const char *sockname)
414 { 414 {
415 int fd = socket(AF_UNIX, SOCK_STREAM, 0); 415 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
416 if (fd < 0) 416 if (fd < 0)
417 abortmsg("cannot create socket (errno = %d)", errno); 417 abortmsgerrno("cannot create socket");
418 418
419 /* don't keep fd on fork(), so that it can be closed when the parent 419 /* don't keep fd on fork(), so that it can be closed when the parent
420 * process get terminated. */ 420 * process get terminated. */
421 int flags = fcntl(fd, F_GETFD); 421 int flags = fcntl(fd, F_GETFD);
422 if (flags < 0) 422 if (flags < 0)
423 abortmsg("cannot get flags of socket (errno = %d)", errno); 423 abortmsgerrno("cannot get flags of socket");
424 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0) 424 if (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) < 0)
425 abortmsg("cannot set flags of socket (errno = %d)", errno); 425 abortmsgerrno("cannot set flags of socket");
426 426
427 struct sockaddr_un addr; 427 struct sockaddr_un addr;
428 addr.sun_family = AF_UNIX; 428 addr.sun_family = AF_UNIX;
429 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path)); 429 strncpy(addr.sun_path, sockname, sizeof(addr.sun_path));
430 addr.sun_path[sizeof(addr.sun_path) - 1] = '\0'; 430 addr.sun_path[sizeof(addr.sun_path) - 1] = '\0';
432 int r = connect(fd, (struct sockaddr *)&addr, sizeof(addr)); 432 int r = connect(fd, (struct sockaddr *)&addr, sizeof(addr));
433 if (r < 0) { 433 if (r < 0) {
434 close(fd); 434 close(fd);
435 if (errno == ENOENT || errno == ECONNREFUSED) 435 if (errno == ENOENT || errno == ECONNREFUSED)
436 return NULL; 436 return NULL;
437 abortmsg("cannot connect to %s (errno = %d)", 437 abortmsgerrno("cannot connect to %s", addr.sun_path);
438 addr.sun_path, errno);
439 } 438 }
440 debugmsg("connected to %s", addr.sun_path); 439 debugmsg("connected to %s", addr.sun_path);
441 440
442 hgclient_t *hgc = mallocx(sizeof(hgclient_t)); 441 hgclient_t *hgc = mallocx(sizeof(hgclient_t));
443 memset(hgc, 0, sizeof(*hgc)); 442 memset(hgc, 0, sizeof(*hgc));