Date: Mon, 11 Jul 2005 13:40:26 GMT From: soc-bushman <soc-bushman@FreeBSD.org> To: Perforce Change Reviews <perforce@freebsd.org> Subject: PERFORCE change 79992 for review Message-ID: <200507111340.j6BDeQZF052637@repoman.freebsd.org>
next in thread | raw e-mail | index | archive | help
http://perforce.freebsd.org/chv.cgi?CH=79992 Change 79992 by soc-bushman@soc-bushman_stinger on 2005/07/11 13:39:36 getserv*_r interface reverted to use (int) as the return value Affected files ... .. //depot/projects/soc2005/nsswitch_cached/src/lib/libc/net/getservent.c#9 edit Differences ... ==== //depot/projects/soc2005/nsswitch_cached/src/lib/libc/net/getservent.c#9 (text+ko) ==== @@ -64,9 +64,6 @@ { SETSERVENT = 1, ENDSERVENT = 2, - SERVENT_UNPACK_SUCCES = 3, - SERVENT_UNPACK_ERANGE = 4, - SERVENT_UNPACK_ERROR = 5, SERVENT_STORAGE_INITIAL = 1 << 10, /* 1 KByte */ SERVENT_STORAGE_MAX = 1 << 20, /* 1 MByte */ }; @@ -82,7 +79,7 @@ { NULL, 0 } }; -static enum constants servent_unpack(char *, struct servent *, char **, size_t); +static int servent_unpack(char *, struct servent *, char **, size_t, int *); /* files backend declarations */ struct files_state @@ -135,24 +132,25 @@ }; }; -static struct servent *wrap_getservbyname_r(struct key key, struct servent *serv, - char *buffer, size_t bufsize); -static struct servent *wrap_getservbyport_r(struct key key, struct servent *serv, - char *buffer, size_t bufsize); -static struct servent *wrap_getservent_r(struct key key, struct servent *serv, - char *buffer, size_t bufsize); -static struct servent *getserv(struct servent *(*fn)(struct key, struct servent *, char *, - size_t), struct key key); +static int wrap_getservbyname_r(struct key key, struct servent *serv, char *buffer, + size_t bufsize, struct servent **res); +static int wrap_getservbyport_r(struct key key, struct servent *serv, char *buffer, + size_t bufsize, struct servent **res); +static int wrap_getservent_r(struct key key, struct servent *serv, char *buffer, + size_t bufsize, struct servent **res); +static struct servent *getserv(int (*fn)(struct key, struct servent *, char *, + size_t, struct servent **), struct key key); -static enum constants -servent_unpack(char *p, struct servent * serv, char ** aliases, size_t aliases_size) +static int +servent_unpack(char *p, struct servent * serv, char ** aliases, size_t aliases_size, + int * errnop) { char *cp, **q, *endp; long l; if (*p == '#') - return SERVENT_UNPACK_ERROR; + return -1; memset(serv, 0, sizeof(struct servent)); @@ -163,18 +161,18 @@ p = strpbrk(p, " \t"); if (p == NULL) - return SERVENT_UNPACK_ERROR; + return -1; *p++ = '\0'; while (*p == ' ' || *p == '\t') p++; cp = strpbrk(p, ",/"); if (cp == NULL) - return SERVENT_UNPACK_ERROR; + return -1; *cp++ = '\0'; l = strtol(p, &endp, 10); if (endp == p || *endp != '\0' || l < 0 || l > USHRT_MAX) - return SERVENT_UNPACK_ERROR; + return -1; serv->s_port = htons((in_port_t)l); serv->s_proto = cp; @@ -191,7 +189,8 @@ *q++ = cp; } else { *q = NULL; - return SERVENT_UNPACK_ERANGE; + *errnop = ERANGE; + return -1; } cp = strpbrk(cp, " \t"); if (cp != NULL) @@ -199,7 +198,7 @@ } *q = NULL; - return SERVENT_UNPACK_SUCCES; + return 0; } /* files backend implementation */ @@ -250,6 +249,7 @@ struct servent *serv; char *buffer; size_t bufsize; + int *errnop; char **aliases; int aliases_size; @@ -278,18 +278,19 @@ serv = va_arg(ap, struct servent *); buffer = va_arg(ap, char *); bufsize = va_arg(ap, size_t); + errnop = va_arg(ap,int *); - rv = files_getstate(&st); - if (rv != 0) { - errno = rv; + *errnop = files_getstate(&st); + if (*errnop != 0) return (NS_UNAVAIL); - } if (st->fp == NULL) st->compat_mode_active = 0; - if (st->fp == NULL && (st->fp = fopen(_PATH_SERVICES, "r")) == NULL) + if (st->fp == NULL && (st->fp = fopen(_PATH_SERVICES, "r")) == NULL) { + *errnop = errno; return (NS_UNAVAIL); + } if (serv_mdata->how == nss_lt_all) stayopen = 1; @@ -302,6 +303,7 @@ do { if (!st->compat_mode_active) { if ((line = fgetln(st->fp, &linesize)) == NULL) { + *errnop = errno; rv = NS_RETURN; break; } @@ -311,14 +313,14 @@ st->compat_mode_active = 1; } else { if (bufsize <= linesize + _ALIGNBYTES + sizeof(char *)) { - errno = ERANGE; + *errnop = ERANGE; rv = NS_RETURN; break; } aliases = (char **)_ALIGN(&buffer[linesize+1]); aliases_size = (buffer + bufsize - (char *)aliases)/sizeof(char *); if (aliases_size < 1) { - errno = ERANGE; + *errnop = ERANGE; rv = NS_RETURN; break; } @@ -332,15 +334,15 @@ switch (serv_mdata->how) { case nss_lt_name: rv = nsdispatch(retval, compat_dtab, NSDB_SERVICES_COMPAT, "getservbyname_r", - compat_src, name, proto, serv, buffer, bufsize); + compat_src, name, proto, serv, buffer, bufsize, errnop); break; case nss_lt_id: rv = nsdispatch(retval, compat_dtab, NSDB_SERVICES_COMPAT, "getservbyport_r", - compat_src, port, proto, serv, buffer, bufsize); + compat_src, port, proto, serv, buffer, bufsize, errnop); break; case nss_lt_all: rv = nsdispatch(retval, compat_dtab, NSDB_SERVICES_COMPAT, "getservent_r", - compat_src, serv, buffer, bufsize); + compat_src, serv, buffer, bufsize, errnop); break; } @@ -350,18 +352,17 @@ continue; } - switch (servent_unpack(line, serv, aliases, aliases_size)) - { - case SERVENT_UNPACK_ERROR: - rv = NS_NOTFOUND; - continue; - case SERVENT_UNPACK_ERANGE: - errno = ERANGE; - rv = NS_RETURN; - goto fin; - default: - break; - }; + rv = servent_unpack(line, serv, aliases, aliases_size, errnop); + if (rv !=0 ) { + if (*errnop == 0) { + rv = NS_NOTFOUND; + continue; + } + else { + rv = NS_RETURN; + break; + } + } switch (serv_mdata->how) { case nss_lt_name: @@ -389,14 +390,13 @@ } } while (!(rv & NS_TERMINATE)); - -fin: + if (!stayopen && st->fp!=NULL) { fclose(st->fp); st->fp = NULL; } - if ((rv == NS_SUCCESS) && (retval != NULL)) + if ((rv ==NS_SUCCESS) && (retval != NULL)) *(struct servent **)retval=serv; return (rv); @@ -463,6 +463,7 @@ struct servent *serv; char *buffer; size_t bufsize; + int *errnop; char **aliases; int aliases_size; @@ -488,15 +489,18 @@ serv = va_arg(ap, struct servent *); buffer = va_arg(ap, char *); bufsize = va_arg(ap, size_t); + errnop = va_arg(ap, int *); - rv = nis_getstate(&st); - if (rv != 0) { - errno = rv; + *errnop = nis_getstate(&st); + if (*errnop != 0) return (NS_UNAVAIL); - } - if ((st->yp_domain[0] == '\0') && (getdomainname(st->yp_domain, sizeof st->yp_domain))) + if (st->yp_domain[0] == '\0') { + if (getdomainname(st->yp_domain, sizeof st->yp_domain)) { + *errnop=errno; return (NS_UNAVAIL); + } + } do { switch (how) @@ -563,7 +567,7 @@ /* we need a room for additional \n symbol */ if (bufsize <= resultbuflen +1 + _ALIGNBYTES + sizeof(char *)) { - errno = ERANGE; + *errnop = ERANGE; rv = NS_RETURN; break; } @@ -571,7 +575,7 @@ aliases=(char **)_ALIGN(&buffer[resultbuflen+2]); aliases_size = (buffer + bufsize - (char *)aliases)/sizeof(char *); if (aliases_size < 1) { - errno = ERANGE; + *errnop = ERANGE; rv = NS_RETURN; break; } @@ -581,22 +585,15 @@ buffer[resultbuflen] = '\n'; buffer[resultbuflen+1] = '\0'; - switch (servent_unpack(buffer, serv, aliases, aliases_size)) { - case SERVENT_UNPACK_ERANGE: - errno = ERANGE; - rv = NS_RETURN; - break; - case SERVENT_UNPACK_ERROR: - rv = NS_NOTFOUND; - break; - case SERVENT_UNPACK_SUCCES: + if (servent_unpack(buffer, serv, aliases, aliases_size, errnop) != 0) { + if (*errnop == 0) + rv = NS_NOTFOUND; + else + rv = NS_RETURN; + } else rv = NS_SUCCESS; - break; - default: - break; - } + free(resultbuf); - free(resultbuf); } while (!(rv & NS_TERMINATE) && (how == nss_lt_all)); fin: @@ -668,9 +665,9 @@ } /* get**_r functions implementation */ -struct servent * +int getservbyname_r(const char *name, const char *proto, - struct servent *serv, char *buffer, size_t bufsize) + struct servent *serv, char *buffer, size_t bufsize, struct servent **result) { static const struct servent_mdata mdata = { nss_lt_name, 0 }; static const struct servent_mdata compat_mdata = { nss_lt_name, 1 }; @@ -684,22 +681,22 @@ { NULL, NULL, NULL } }; - struct servent *result; - int rv; + int rv, ret_errno; - result = NULL; - rv = nsdispatch(&result, dtab, NSDB_SERVICES, "getservbyname_r", defaultsrc, - name, proto, serv, buffer, bufsize); + ret_errno = 0; + *result = NULL; + rv = nsdispatch(result, dtab, NSDB_SERVICES, "getservbyname_r", defaultsrc, + name, proto, serv, buffer, bufsize, &ret_errno); if (rv == NS_SUCCESS) - return (result); + return (0); else - return (NULL); + return (ret_errno); } -struct servent * +int getservbyport_r(int port, const char *proto, struct servent *serv, char *buffer, - size_t bufsize) + size_t bufsize, struct servent **result) { static const struct servent_mdata mdata = { nss_lt_id, 0 }; static const struct servent_mdata compat_mdata = { nss_lt_id, 1 }; @@ -712,22 +709,22 @@ { NSSRC_COMPAT, files_servent, (void *)&compat_mdata }, { NULL, NULL, NULL } }; - - struct servent *result; - int rv; + int rv, ret_errno; - result = NULL; - rv = nsdispatch(&result, dtab, NSDB_SERVICES, "getservbyport_r", defaultsrc, - port, proto, serv, buffer, bufsize); + ret_errno = 0; + *result = NULL; + rv = nsdispatch(result, dtab, NSDB_SERVICES, "getservbyport_r", defaultsrc, + port, proto, serv, buffer, bufsize, &ret_errno); if (rv == NS_SUCCESS) - return (result); + return (0); else - return (NULL); + return (ret_errno); } -struct servent * -getservent_r(struct servent *serv, char *buffer, size_t bufsize) +int +getservent_r(struct servent *serv, char *buffer, size_t bufsize, + struct servent **result) { static const struct servent_mdata mdata = { nss_lt_all, 0 }; static const struct servent_mdata compat_mdata = { nss_lt_all, 1 }; @@ -740,18 +737,17 @@ { NSSRC_COMPAT, files_servent, (void *)&compat_mdata }, { NULL, NULL, NULL } }; - - struct servent *result; - int rv; + int rv, ret_errno; - result = NULL; - rv = nsdispatch(&result, dtab, NSDB_SERVICES, "getservent_r", defaultsrc, - serv, buffer, bufsize); + ret_errno = 0; + *result = NULL; + rv = nsdispatch(result, dtab, NSDB_SERVICES, "getservent_r", defaultsrc, + serv, buffer, bufsize, &ret_errno); if (rv == NS_SUCCESS) - return (result); + return (0); else - return (NULL); + return (ret_errno); } void @@ -795,26 +791,30 @@ free(p); } -static struct servent * -wrap_getservbyname_r(struct key key, struct servent *serv, char *buffer, size_t bufsize) +static int +wrap_getservbyname_r(struct key key, struct servent *serv, char *buffer, size_t bufsize, + struct servent **res) { - return (getservbyname_r(key.name, key.proto, serv, buffer, bufsize)); + return (getservbyname_r(key.name, key.proto, serv, buffer, bufsize, res)); } -static struct servent * -wrap_getservbyport_r(struct key key, struct servent *serv, char *buffer, size_t bufsize) +static int +wrap_getservbyport_r(struct key key, struct servent *serv, char *buffer, size_t bufsize, + struct servent **res) { - return (getservbyport_r(key.port, key.proto, serv, buffer, bufsize)); + return (getservbyport_r(key.port, key.proto, serv, buffer, bufsize, res)); } -static struct servent * -wrap_getservent_r(struct key key, struct servent *serv, char *buffer, size_t bufsize) +static int +wrap_getservent_r(struct key key, struct servent *serv, char *buffer, size_t bufsize, + struct servent **res) { - return (getservent_r(serv, buffer, bufsize)); + return (getservent_r(serv, buffer, bufsize, res)); } static struct servent * -getserv(struct servent *(*fn)(struct key, struct servent *, char *, size_t), struct key key) +getserv(int (*fn)(struct key, struct servent *, char *, size_t, struct servent **), + struct key key) { int rv; struct servent *res; @@ -833,11 +833,12 @@ st->bufsize = SERVENT_STORAGE_INITIAL; } do { - res = fn(key, &st->serv, st->buffer, st->bufsize); - if (res == NULL && errno == ERANGE) { + rv = fn(key, &st->serv, st->buffer, st->bufsize, &res); + if (res == NULL && rv == ERANGE) { free(st->buffer); if ((st->bufsize << 1) > SERVENT_STORAGE_MAX) { st->buffer = NULL; + errno = ERANGE; return (NULL); } st->bufsize <<= 1; @@ -845,7 +846,7 @@ if (st->buffer == NULL) return (NULL); } - } while (res == NULL && errno == ERANGE); + } while (res == NULL && rv == ERANGE); if (rv != 0) errno = rv; return (res);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200507111340.j6BDeQZF052637>