From 7d787482040aa327ef2345eca2e9f980ce76cc28 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Wed, 15 Nov 2017 10:47:24 -0800 Subject: Add a routine to format error messages with an errno-based message at the end. That routine will use strerror_s() or strerror_r() if available, in a fashion that's thread-safe. Otherwise, it falls back on pcap_strerror(). Use it in both libpcap and rpcapd. Given that we check for errors in strerror_r(), hopefully this will squelch warnings with newer version of GCC and GNU libc; whilst the macOS (and other BSD-flavored?) strerror_r() always fills in a message, that's not required by the Single UNIX Specification, as far as I can tell, so we apparently really *do* need to check for errors. --- pcap-dlpi.c | 83 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 41 insertions(+), 42 deletions(-) (limited to 'pcap-dlpi.c') diff --git a/pcap-dlpi.c b/pcap-dlpi.c index 619e70e2..60612d8e 100644 --- a/pcap-dlpi.c +++ b/pcap-dlpi.c @@ -238,8 +238,8 @@ pcap_read_dlpi(pcap_t *p, int cnt, pcap_handler callback, u_char *user) case EAGAIN: return (0); } - strlcpy(p->errbuf, pcap_strerror(errno), - sizeof(p->errbuf)); + pcap_fmt_errmsg_for_errno(p->errbuf, + sizeof(p->errbuf), errno, "getmsg"); return (-1); } cc = data.len; @@ -262,8 +262,8 @@ pcap_inject_dlpi(pcap_t *p, const void *buf, size_t size) #if defined(DLIOCRAW) ret = write(p->fd, buf, size); if (ret == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "send"); return (-1); } #elif defined(DL_HP_RAWDLS) @@ -274,8 +274,8 @@ pcap_inject_dlpi(pcap_t *p, const void *buf, size_t size) } ret = dlrawdatareq(pd->send_fd, buf, size); if (ret == -1) { - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "send: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "send"); return (-1); } /* @@ -393,8 +393,8 @@ open_dlpi_device(const char *name, u_int *ppa, char *errbuf) status = PCAP_ERROR_PERM_DENIED; else status = PCAP_ERROR; - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, - "%s: %s", cp, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "%s", cp); return (status); } @@ -442,8 +442,8 @@ open_dlpi_device(const char *name, u_int *ppa, char *errbuf) status = PCAP_ERROR_PERM_DENIED; else status = PCAP_ERROR; - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", dname, - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "%s", dname); return (status); } @@ -482,8 +482,8 @@ open_dlpi_device(const char *name, u_int *ppa, char *errbuf) status = PCAP_ERROR_PERM_DENIED; else status = PCAP_ERROR; - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "%s: %s", - dname2, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, + PCAP_ERRBUF_SIZE, errno, "%s", dname2); } return (status); } @@ -666,8 +666,8 @@ pcap_activate_dlpi(pcap_t *p) */ if (strioctl(p->fd, A_PROMISCON_REQ, 0, NULL) < 0) { status = PCAP_ERROR; - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, - "A_PROMISCON_REQ: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "A_PROMISCON_REQ"); goto bad; } } else @@ -784,8 +784,8 @@ pcap_activate_dlpi(pcap_t *p) */ if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) { status = PCAP_ERROR; - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "DLIOCRAW: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "DLIOCRAW"); goto bad; } #endif @@ -825,8 +825,8 @@ pcap_activate_dlpi(pcap_t *p) */ if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) { status = PCAP_ERROR; - pcap_snprintf(p->errbuf, PCAP_ERRBUF_SIZE, "FLUSHR: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE, + errno, "FLUSHR"); goto bad; } @@ -1086,8 +1086,8 @@ pcap_platform_finddevs(pcap_if_list_t *devlistp, char *errbuf) } if (strioctl(fd, A_GET_UNITS, sizeof(buf), (char *)&buf) < 0) { - pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE, "A_GET_UNITS: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(errbuf, PCAP_ERRBUF_SIZE, + errno, "A_GET_UNITS"); return (-1); } for (i = 0; i < buf.nunits; i++) { @@ -1112,9 +1112,8 @@ send_request(int fd, char *ptr, int len, char *what, char *ebuf) flags = 0; if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "send_request: putmsg \"%s\": %s", - what, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "send_request: putmsg \"%s\"", what); return (-1); } return (0); @@ -1142,8 +1141,8 @@ recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror flags = 0; if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "recv_ack: %s getmsg: %s", - what, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "recv_ack: %s getmsg", what); return (PCAP_ERROR); } @@ -1165,9 +1164,9 @@ recv_ack(int fd, int size, const char *what, char *bufp, char *ebuf, int *uerror case DL_SYSERR: if (uerror != NULL) *uerror = dlp->error_ack.dl_unix_errno; - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "recv_ack: %s: UNIX error - %s", - what, pcap_strerror(dlp->error_ack.dl_unix_errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + dlp->error_ack.dl_unix_errno, + "recv_ack: %s: UNIX error", what); if (dlp->error_ack.dl_unix_errno == EPERM || dlp->error_ack.dl_unix_errno == EACCES) return (PCAP_ERROR_PERM_DENIED); @@ -1618,8 +1617,8 @@ get_dlpi_ppa(register int fd, register const char *device, register u_int unit, */ /* get the head first */ if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "get_dlpi_ppa: hpppa getmsg"); return (PCAP_ERROR); } if (ctl.len == -1) { @@ -1645,8 +1644,8 @@ get_dlpi_ppa(register int fd, register const char *device, register u_int unit, /* allocate buffer */ if ((ppa_data_buf = (char *)malloc(dlp->dl_length)) == NULL) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "get_dlpi_ppa: hpppa malloc: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "get_dlpi_ppa: hpppa malloc"); return (PCAP_ERROR); } ctl.maxlen = dlp->dl_length; @@ -1654,8 +1653,8 @@ get_dlpi_ppa(register int fd, register const char *device, register u_int unit, ctl.buf = (char *)ppa_data_buf; /* get the data */ if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, - "get_dlpi_ppa: hpppa getmsg: %s", pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "get_dlpi_ppa: hpppa getmsg"); free(ppa_data_buf); return (PCAP_ERROR); } @@ -1724,8 +1723,8 @@ get_dlpi_ppa(register int fd, register const char *device, register u_int unit, */ pcap_snprintf(dname, sizeof(dname), "/dev/%s%u", device, unit); if (stat(dname, &statbuf) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "stat: %s: %s", - dname, pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "stat: %s", dname); return (PCAP_ERROR); } majdev = major(statbuf.st_rdev); @@ -1797,8 +1796,8 @@ get_dlpi_ppa(register int fd, register const char *ifname, register u_int unit, } kd = open("/dev/kmem", O_RDONLY); if (kd < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "kmem open: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "kmem open"); return (PCAP_ERROR); } if (dlpi_kread(kd, nl[NL_IFNET].n_value, @@ -1832,14 +1831,14 @@ dlpi_kread(register int fd, register off_t addr, register int cc; if (lseek(fd, addr, SEEK_SET) < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "lseek: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "lseek"); return (-1); } cc = read(fd, buf, len); if (cc < 0) { - pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "read: %s", - pcap_strerror(errno)); + pcap_fmt_errmsg_for_errno(ebuf, PCAP_ERRBUF_SIZE, + errno, "read"); return (-1); } else if (cc != len) { pcap_snprintf(ebuf, PCAP_ERRBUF_SIZE, "short read (%d != %d)", cc, -- cgit v1.2.1