From: Erez Zadok Date: Thu, 7 Apr 2005 03:50:41 +0000 (+0000) Subject: * include/am_utils.h: external definition for new xstrlcpy X-Git-Tag: am-utils-6_1_rc2~20 X-Git-Url: https://git.fsl.cs.stonybrook.edu/?a=commitdiff_plain;h=8a1df91a3e20600a96ebfc7a1b863b85ebfb4020;p=am-utils-6.0.git * include/am_utils.h: external definition for new xstrlcpy function. * libamu/util.c (xstrlcpy): new function. Similar to strncpy, but uses strlcpy to guarantee that the resulting string is null terminated, and also warn if the resulting string was truncated. * libamu/xutil.c (get_server_pid): move this function from util.c which is for general-purpose utilities. * m4/macros/header_templates.m4: template for HAVE_EXTERN_STRLCPY. * include/am_defs.h: provide extern definition for strlcpy, if needed. * libamu/Makefile.am (EXTRA_DIST): include strlcpy.c in distro. * configure.in: search for strlcpy and its extern. --- diff --git a/ChangeLog b/ChangeLog index 30d910e..2f7d390 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,24 @@ 2005-04-06 Erez Zadok + * include/am_utils.h: external definition for new xstrlcpy + function. + + * libamu/util.c (xstrlcpy): new function. Similar to strncpy, but + uses strlcpy to guarantee that the resulting string is null + terminated, and also warn if the resulting string was truncated. + + * libamu/xutil.c (get_server_pid): move this function from util.c + which is for general-purpose utilities. + + * m4/macros/header_templates.m4: template for HAVE_EXTERN_STRLCPY. + + * include/am_defs.h: provide extern definition for strlcpy, if + needed. + + * libamu/Makefile.am (EXTRA_DIST): include strlcpy.c in distro. + + * configure.in: search for strlcpy and its extern. + * amq/pawd.c (find_mt): It only handles *some* filesystem types, so it breaks on direct xfs mounts for example. The fix (from Christos Zoulas) is simple: We need to exclude toplvl to avoid diff --git a/configure.in b/configure.in index eacab7f..5ed77e5 100644 --- a/configure.in +++ b/configure.in @@ -53,7 +53,7 @@ AH_BOTTOM([ dnl dnl AC_CONFIG_AUX_DIR(m4) AC_PREREQ(2.52) -AC_REVISION($Revision: 1.81 $) +AC_REVISION($Revision: 1.82 $) AC_COPYRIGHT([Copyright (c) 1997-2005 Erez Zadok]) dnl find out system type AC_MSG_NOTICE(*** SYSTEM TYPES ***) @@ -313,10 +313,11 @@ AC_CHECK_FUNCS( \ sigsuspend \ socket \ strchr \ - strcspn \ strcasecmp \ + strcspn \ strdup \ strerror \ + strlcpy \ strspn \ strstr \ svc_getreq \ @@ -341,6 +342,7 @@ AC_REPLACE_FUNCS( \ strcasecmp \ strdup \ strerror \ + strlcpy \ strstr \ ualarm \ ) @@ -711,6 +713,7 @@ AMU_CHECK_EXTERNS( setitimer \ strcasecmp \ strdup \ + strlcpy \ strstr \ ualarm \ usleep \ diff --git a/include/am_defs.h b/include/am_defs.h index 369adda..7bc15d3 100644 --- a/include/am_defs.h +++ b/include/am_defs.h @@ -37,7 +37,7 @@ * SUCH DAMAGE. * * - * $Id: am_defs.h,v 1.54 2005/03/21 19:08:05 ro Exp $ + * $Id: am_defs.h,v 1.55 2005/04/07 03:50:41 ezk Exp $ * */ @@ -1504,6 +1504,14 @@ extern int strcasecmp(const char *s1, const char *s2); extern char *strdup(const char *s); #endif /* not HAVE_EXTERN_STRDUP */ +#ifndef HAVE_EXTERN_STRLCPY +/* + * define this extern even if function does not exist, for it will + * be filled in by libamu/strlcpy.c + */ +extern size_t strlcpy(char *dst, const char *src, size_t siz); +#endif /* not HAVE_EXTERN_STRLCPY */ + #if defined(HAVE_STRSTR) && !defined(HAVE_EXTERN_STRSTR) extern char *strstr(const char *s1, const char *s2); #endif /* defined(HAVE_STRSTR) && !defined(HAVE_EXTERN_STRSTR) */ diff --git a/include/am_utils.h b/include/am_utils.h index 541f189..c835fb6 100644 --- a/include/am_utils.h +++ b/include/am_utils.h @@ -37,7 +37,7 @@ * SUCH DAMAGE. * * - * $Id: am_utils.h,v 1.63 2005/03/09 02:29:55 ezk Exp $ + * $Id: am_utils.h,v 1.64 2005/04/07 03:50:41 ezk Exp $ * */ @@ -317,6 +317,7 @@ extern void rmdirs(char *); extern void rpc_msg_init(struct rpc_msg *, u_long, u_long, u_long); extern void set_amd_program_number(int program); extern void show_opts(int ch, struct opt_tab *); +extern void xstrlcpy(char *dst, const char *src, size_t len); extern void unregister_amq(void); extern voidp xmalloc(int); extern voidp xrealloc(voidp, int); diff --git a/libamu/Makefile.am b/libamu/Makefile.am index 9165af7..07f09b5 100644 --- a/libamu/Makefile.am +++ b/libamu/Makefile.am @@ -31,6 +31,7 @@ EXTRA_DIST = memcmp.c \ strcasecmp.c \ strdup.c \ strerror.c \ + strlcpy.c \ strstr.c \ ualarm.c diff --git a/libamu/strlcpy.c b/libamu/strlcpy.c new file mode 100644 index 0000000..70d8403 --- /dev/null +++ b/libamu/strlcpy.c @@ -0,0 +1,84 @@ +/* + * Copyright (c) 1997-2005 Erez Zadok + * Copyright (c) 1990 Jan-Simon Pendry + * Copyright (c) 1990 Imperial College of Science, Technology & Medicine + * Copyright (c) 1990 The Regents of the University of California. + * All rights reserved. + * + * This code is derived from software contributed to Berkeley by + * Jan-Simon Pendry at Imperial College, London. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgment: + * This product includes software developed by the University of + * California, Berkeley and its contributors. + * 4. Neither the name of the University nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * + * $Id: strlcpy.c,v 1.1 2005/04/07 03:50:42 ezk Exp $ + * + */ + +#ifdef HAVE_CONFIG_H +# include +#endif /* HAVE_CONFIG_H */ +#include +#include + +/* + * Implementation of strlcpy(3) from OpenBSD/NetBSD. + */ + +/* + * Copy src to string dst of size siz. At most siz-1 characters + * will be copied. Always NUL terminates (unless siz == 0). + * Returns strlen(src); if retval >= siz, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t siz) +{ + char *d = dst; + const char *s = src; + size_t n = siz; + + + /* Copy as many bytes as will fit */ + if (n != 0 && --n != 0) { + do { + if ((*d++ = *s++) == 0) + break; + } while (--n != 0); + } + + /* Not enough room in dst, add NUL and traverse rest of src */ + if (n == 0) { + if (siz != 0) + *d = '\0'; /* NUL-terminate dst */ + while (*s++) + ; + } + + return(s - src - 1); /* count does not include NUL */ +} diff --git a/libamu/strutil.c b/libamu/strutil.c index 2d20778..60d44c0 100644 --- a/libamu/strutil.c +++ b/libamu/strutil.c @@ -37,7 +37,7 @@ * SUCH DAMAGE. * * - * $Id: strutil.c,v 1.13 2005/01/17 22:41:28 ezk Exp $ + * $Id: strutil.c,v 1.14 2005/04/07 03:50:42 ezk Exp $ * */ @@ -82,6 +82,22 @@ str3cat(char *p, char *s1, char *s2, char *s3) } +/* + * Use generic strlcpy to copy a string more carefully, null-terminating it + * as needed. However, if the copied string was truncated due to lack of + * space, then warn us. + * + * For now, xstrlcpy returns VOID because it doesn't look like anywhere in + * the Amd code do we actually use the return value of strncpy/strlcpy. + */ +void +xstrlcpy(char *dst, const char *src, size_t len) +{ + if (strlcpy(dst, src, len) >= len) + plog(XLOG_WARNING, "xstrlcpy: string \"%s\" truncated to \"%s\"", src, dst); +} + + /* * Make all the directories in the path. */ @@ -169,9 +185,3 @@ rmdirs(char *dir) XFREE(xdp); } - -long -get_server_pid() -{ - return (long) (foreground ? am_mypid : getppid()); -} diff --git a/libamu/xutil.c b/libamu/xutil.c index eb46272..71de238 100644 --- a/libamu/xutil.c +++ b/libamu/xutil.c @@ -37,7 +37,7 @@ * SUCH DAMAGE. * * - * $Id: xutil.c,v 1.35 2005/02/27 04:23:09 ezk Exp $ + * $Id: xutil.c,v 1.36 2005/04/07 03:50:42 ezk Exp $ * */ @@ -169,6 +169,13 @@ am_set_mypid(void) } +long +get_server_pid() +{ + return (long) (foreground ? am_mypid : getppid()); +} + + voidp xmalloc(int len) { diff --git a/m4/macros/header_templates.m4 b/m4/macros/header_templates.m4 index 6413db5..672ea27 100644 --- a/m4/macros/header_templates.m4 +++ b/m4/macros/header_templates.m4 @@ -913,6 +913,9 @@ AH_TEMPLATE([HAVE_EXTERN_STRCASECMP], AH_TEMPLATE([HAVE_EXTERN_STRDUP], [does extern definition for strdup() exist?]) +AH_TEMPLATE([HAVE_EXTERN_STRLCPY], +[does extern definition for strlcpy() exist?]) + AH_TEMPLATE([HAVE_EXTERN_STRSTR], [does extern definition for strstr() exist?])