Annotation of embedaddon/libpdel/util/kernelglue.c, revision 1.1

1.1     ! misho       1: 
        !             2: /*
        !             3:  * Copyright (c) 1988, 1993
        !             4:  *     The Regents of the University of California.  All rights reserved.
        !             5:  *
        !             6:  * Redistribution and use in source and binary forms, with or without
        !             7:  * modification, are permitted provided that the following conditions
        !             8:  * are met:
        !             9:  * 1. Redistributions of source code must retain the above copyright
        !            10:  *    notice, this list of conditions and the following disclaimer.
        !            11:  * 2. Redistributions in binary form must reproduce the above copyright
        !            12:  *    notice, this list of conditions and the following disclaimer in the
        !            13:  *    documentation and/or other materials provided with the distribution.
        !            14:  * 3. All advertising materials mentioning features or use of this software
        !            15:  *    must display the following acknowledgement:
        !            16:  *     This product includes software developed by the University of
        !            17:  *     California, Berkeley and its contributors.
        !            18:  * 4. Neither the name of the University nor the names of its contributors
        !            19:  *    may be used to endorse or promote products derived from this software
        !            20:  *    without specific prior written permission.
        !            21:  *
        !            22:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
        !            23:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            24:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            25:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            26:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            27:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            28:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            29:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            30:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            31:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            32:  * SUCH DAMAGE.
        !            33:  */
        !            34: 
        !            35: #include <sys/types.h>
        !            36: #include <sys/errno.h>
        !            37: #include <sys/param.h>
        !            38: #include <sys/libkern.h>
        !            39: #include <sys/systm.h>
        !            40: #include <sys/ctype.h>
        !            41: #include <net/ethernet.h>
        !            42: #include <netinet/in.h>
        !            43: #include <netinet/in_systm.h>
        !            44: 
        !            45: #include "kernelglue.h"
        !            46: 
        !            47: /*
        !            48:  * Kernelglue kernel memory type
        !            49:  */
        !            50: MALLOC_DEFINE(M_LIBPDEL, "LIBPDEL", "libpdel kernel memory");
        !            51: 
        !            52: /* Errno */
        !            53: int    errno;
        !            54: 
        !            55: char *
        !            56: strdup(const char *str)
        !            57: {
        !            58:        size_t len;
        !            59:        char *copy;
        !            60: 
        !            61:        len = strlen(str) + 1;
        !            62:        if ((copy = MALLOC(NULL, len)) == NULL)
        !            63:                return (NULL);
        !            64:        memcpy(copy, str, len);
        !            65:        return (copy);
        !            66: }
        !            67: 
        !            68: int
        !            69: asprintf(char **ret, const char *format, ...)
        !            70: {
        !            71:        va_list args;
        !            72:        int rtn;
        !            73: 
        !            74:        va_start(args, format);
        !            75:        rtn = vasprintf(ret, format, args);
        !            76:        va_end(args);
        !            77:        return (rtn);
        !            78: }
        !            79: 
        !            80: int
        !            81: vasprintf(char **ret, const char *format, va_list ap)
        !            82: {
        !            83:        size_t buflen = 256;
        !            84:        char *buf;
        !            85:        int slen;
        !            86: 
        !            87:        /* Initialize return value in case of failure */
        !            88:        *ret = NULL;
        !            89: 
        !            90: try_again:
        !            91:        /* Allocate buffer */
        !            92:        if ((buf = malloc(buflen, M_LIBPDEL, M_NOWAIT)) == NULL) {
        !            93:                errno = ENOMEM;
        !            94:                return (-1);
        !            95:        }
        !            96: 
        !            97:        /* Format string */
        !            98:        slen = vsnprintf(buf, buflen, format, ap);
        !            99: 
        !           100:        /* If buffer was big enough, we're done */
        !           101:        if (slen < buflen) {
        !           102:                *ret = buf;
        !           103:                return (slen);
        !           104:        }
        !           105: 
        !           106:        /* Increase buffer size and try again */
        !           107:        free(buf, M_LIBPDEL);
        !           108:        buflen = slen + 1;
        !           109:        goto try_again;
        !           110: }
        !           111: 
        !           112: char *
        !           113: strchr(const char *s, int c)
        !           114: {
        !           115:        while (1) {
        !           116:                if (*s == (char)c)
        !           117:                        return ((char *)s);
        !           118:                if (*s == '\0')
        !           119:                        return (NULL);
        !           120:                s++;
        !           121:        }
        !           122: }
        !           123: 
        !           124: int
        !           125: strcasecmp(const char *s1, const char *s2)
        !           126: {
        !           127:        const u_char *s = s1;
        !           128:        const u_char *t = s2;
        !           129: 
        !           130:         while (*s != '\0' && *t != '\0' && tolower(*s) == tolower(*t)) {
        !           131:                s++;
        !           132:                t++;
        !           133:         }
        !           134:         return (*s - *t);
        !           135: }
        !           136: 
        !           137: size_t
        !           138: strlcpy(char *dst, const char *src, size_t siz)
        !           139: {
        !           140:        char *d = dst;
        !           141:        const char *s = src;
        !           142:        size_t n = siz;
        !           143: 
        !           144:        /* Copy as many bytes as will fit */
        !           145:        if (n != 0 && --n != 0) {
        !           146:                do {
        !           147:                        if ((*d++ = *s++) == 0)
        !           148:                                break;
        !           149:                } while (--n != 0);
        !           150:        }
        !           151: 
        !           152:        /* Not enough room in dst, add NUL and traverse rest of src */
        !           153:        if (n == 0) {
        !           154:                if (siz != 0)
        !           155:                        *d = '\0';              /* NUL-terminate dst */
        !           156:                while (*s++)
        !           157:                        ;
        !           158:        }
        !           159: 
        !           160:        return(s - src - 1);    /* count does not include NUL */
        !           161: }
        !           162: 
        !           163: void *
        !           164: memmove(void *dst, const void *src, size_t len)
        !           165: {
        !           166:        ovbcopy(src, dst, len);
        !           167:        return (dst);
        !           168: }
        !           169: 
        !           170: time_t 
        !           171: time(time_t *newtime)
        !           172: {
        !           173:        struct timeval valtime;
        !           174: 
        !           175:        getmicrotime(&valtime);
        !           176:        if (newtime)
        !           177:                *newtime = valtime.tv_sec;
        !           178:        return (valtime.tv_sec);
        !           179: }
        !           180: 
        !           181: char *
        !           182: strerror(int errnum)
        !           183: {
        !           184:        static char buf[32];
        !           185:        const char *s;
        !           186: 
        !           187:        switch (errnum) {
        !           188:        case EPERM:             s = "Operation not permitted"; break;
        !           189:        case ENOENT:            s = "No such file or directory"; break;
        !           190:        case ESRCH:             s = "No such process"; break;
        !           191:        case EINTR:             s = "Interrupted system call"; break;
        !           192:        case EIO:               s = "Input/output error"; break;
        !           193:        case ENXIO:             s = "Device not configured"; break;
        !           194:        case E2BIG:             s = "Argument list too long"; break;
        !           195:        case ENOEXEC:           s = "Exec format error"; break;
        !           196:        case EBADF:             s = "Bad file descriptor"; break;
        !           197:        case ECHILD:            s = "No child processes"; break;
        !           198:        case EDEADLK:           s = "Resource deadlock avoided"; break;
        !           199:        case ENOMEM:            s = "Cannot allocate memory"; break;
        !           200:        case EACCES:            s = "Permission denied"; break;
        !           201:        case EFAULT:            s = "Bad address"; break;
        !           202:        case ENOTBLK:           s = "Block device required"; break;
        !           203:        case EBUSY:             s = "Device busy"; break;
        !           204:        case EEXIST:            s = "File exists"; break;
        !           205:        case EXDEV:             s = "Cross-device link"; break;
        !           206:        case ENODEV:            s = "Operation not supported by device"; break;
        !           207:        case ENOTDIR:           s = "Not a directory"; break;
        !           208:        case EISDIR:            s = "Is a directory"; break;
        !           209:        case EINVAL:            s = "Invalid argument"; break;
        !           210:        case ENFILE:            s = "Too many open files in system"; break;
        !           211:        case EMFILE:            s = "Too many open files"; break;
        !           212:        case ENOTTY:            s = "Inappropriate ioctl for device"; break;
        !           213:        case ETXTBSY:           s = "Text file busy"; break;
        !           214:        case EFBIG:             s = "File too large"; break;
        !           215:        case ENOSPC:            s = "No space left on device"; break;
        !           216:        case ESPIPE:            s = "Illegal seek"; break;
        !           217:        case EROFS:             s = "Read-only file system"; break;
        !           218:        case EMLINK:            s = "Too many links"; break;
        !           219:        case EPIPE:             s = "Broken pipe"; break;
        !           220:        case EDOM:              s = "Numerical argument out of domain"; break;
        !           221:        case ERANGE:            s = "Result too large"; break;
        !           222:        case EAGAIN:            s = "Resource temporarily unavailable"; break;
        !           223:        case EINPROGRESS:       s = "Operation now in progress"; break;
        !           224:        case EALREADY:          s = "Operation already in progress"; break;
        !           225:        case ENOTSOCK:          s = "Socket operation on non-socket"; break;
        !           226:        case EDESTADDRREQ:      s = "Destination address required"; break;
        !           227:        case EMSGSIZE:          s = "Message too long"; break;
        !           228:        case EPROTOTYPE:        s = "Protocol wrong type for socket"; break;
        !           229:        case ENOPROTOOPT:       s = "Protocol not available"; break;
        !           230:        case EPROTONOSUPPORT:   s = "Protocol not supported"; break;
        !           231:        case ESOCKTNOSUPPORT:   s = "Socket type not supported"; break;
        !           232:        case EOPNOTSUPP:        s = "Operation not supported"; break;
        !           233:        case EPFNOSUPPORT:      s = "Protocol family not supported"; break;
        !           234:        case EAFNOSUPPORT:
        !           235:                s = "Address family not supported by protocol family"; break;
        !           236:        case EADDRINUSE:        s = "Address already in use"; break;
        !           237:        case EADDRNOTAVAIL:     s = "Can't assign requested address"; break;
        !           238:        case ENETDOWN:          s = "Network is down"; break;
        !           239:        case ENETUNREACH:       s = "Network is unreachable"; break;
        !           240:        case ENETRESET: 
        !           241:                s = "Network dropped connection on reset"; break;
        !           242:        case ECONNABORTED:      s = "Software caused connection abort"; break;
        !           243:        case ECONNRESET:        s = "Connection reset by peer"; break;
        !           244:        case ENOBUFS:           s = "No buffer space available"; break;
        !           245:        case EISCONN:           s = "Socket is already connected"; break;
        !           246:        case ENOTCONN:          s = "Socket is not connected"; break;
        !           247:        case ESHUTDOWN:         s = "Can't send after socket shutdown"; break;
        !           248:        case ETOOMANYREFS:      s = "Too many references: can't splice"; break;
        !           249:        case ETIMEDOUT:         s = "Operation timed out"; break;
        !           250:        case ECONNREFUSED:      s = "Connection refused"; break;
        !           251:        case ELOOP:             s = "Too many levels of symbolic links"; break;
        !           252:        case ENAMETOOLONG:      s = "File name too long"; break;
        !           253:        case EHOSTDOWN:         s = "Host is down"; break;
        !           254:        case EHOSTUNREACH:      s = "No route to host"; break;
        !           255:        case ENOTEMPTY:         s = "Directory not empty"; break;
        !           256:        case EPROCLIM:          s = "Too many processes"; break;
        !           257:        case EUSERS:            s = "Too many users"; break;
        !           258:        case EDQUOT:            s = "Disc quota exceeded"; break;
        !           259:        case ESTALE:            s = "Stale NFS file handle"; break;
        !           260:        case EREMOTE:           s = "Too many levels of remote in path"; break;
        !           261:        case EBADRPC:           s = "RPC struct is bad"; break;
        !           262:        case ERPCMISMATCH:      s = "RPC version wrong"; break;
        !           263:        case EPROGUNAVAIL:      s = "RPC prog. not avail"; break;
        !           264:        case EPROGMISMATCH:     s = "Program version wrong"; break;
        !           265:        case EPROCUNAVAIL:      s = "Bad procedure for program"; break;
        !           266:        case ENOLCK:            s = "No locks available"; break;
        !           267:        case ENOSYS:            s = "Function not implemented"; break;
        !           268:        case EFTYPE:            s = "Inappropriate file type or format"; break;
        !           269:        case EAUTH:             s = "Authentication error"; break;
        !           270:        case ENEEDAUTH:         s = "Need authenticator"; break;
        !           271:        case EIDRM:             s = "Identifier removed"; break;
        !           272:        case ENOMSG:            s = "No message of desired type"; break;
        !           273:        case EOVERFLOW:         s =
        !           274:                "Value too large to be stored in data type"; break;
        !           275:        case ECANCELED:         s = "Operation canceled"; break;
        !           276:        case EILSEQ:            s = "Illegal byte sequence"; break;
        !           277:        default:
        !           278:                snprintf(buf, sizeof(buf), "Unknown error: %d", errnum);
        !           279:                errno = EINVAL;
        !           280:                return (buf);
        !           281:        }
        !           282:        strlcpy(buf, s, sizeof(buf));
        !           283:        return (buf);
        !           284: }
        !           285: 
        !           286: void *
        !           287: kern_malloc(size_t size)
        !           288: {
        !           289:        void *mem;
        !           290: 
        !           291:        if ((mem = malloc((u_long)size, M_LIBPDEL, M_NOWAIT)) == NULL)
        !           292:                errno = ENOMEM;
        !           293:        return (mem);
        !           294: }
        !           295: 
        !           296: void *
        !           297: kern_realloc(void *mem, size_t size)
        !           298: {
        !           299:        if ((mem = realloc(mem,
        !           300:            (u_long)size, M_LIBPDEL, M_NOWAIT)) == NULL)
        !           301:                errno = ENOMEM;
        !           302:        return (mem);
        !           303: }
        !           304: 
        !           305: void
        !           306: kern_free(void *mem)
        !           307: {
        !           308:        free(mem, M_LIBPDEL);
        !           309: }
        !           310: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>