Annotation of libaitcfg/src/pq.c, revision 1.1.2.1

1.1.2.1 ! misho       1: /*************************************************************************
        !             2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
        !             3: *  by Michael Pounov <misho@openbsd-bg.org>
        !             4: *
        !             5: * $Author: misho $
        !             6: * $Id: queue.c,v 1.10.2.1 2012/09/17 14:17:12 misho Exp $
        !             7: *
        !             8: **************************************************************************
        !             9: The ELWIX and AITNET software is distributed under the following
        !            10: terms:
        !            11: 
        !            12: All of the documentation and software included in the ELWIX and AITNET
        !            13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
        !            14: 
        !            15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
        !            16:        by Michael Pounov <misho@elwix.org>.  All rights reserved.
        !            17: 
        !            18: Redistribution and use in source and binary forms, with or without
        !            19: modification, are permitted provided that the following conditions
        !            20: are met:
        !            21: 1. Redistributions of source code must retain the above copyright
        !            22:    notice, this list of conditions and the following disclaimer.
        !            23: 2. Redistributions in binary form must reproduce the above copyright
        !            24:    notice, this list of conditions and the following disclaimer in the
        !            25:    documentation and/or other materials provided with the distribution.
        !            26: 3. All advertising materials mentioning features or use of this software
        !            27:    must display the following acknowledgement:
        !            28: This product includes software developed by Michael Pounov <misho@elwix.org>
        !            29: ELWIX - Embedded LightWeight unIX and its contributors.
        !            30: 4. Neither the name of AITNET nor the names of its contributors
        !            31:    may be used to endorse or promote products derived from this software
        !            32:    without specific prior written permission.
        !            33: 
        !            34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
        !            35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
        !            36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
        !            37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
        !            38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
        !            39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
        !            40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
        !            41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
        !            42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
        !            43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
        !            44: SUCH DAMAGE.
        !            45: */
        !            46: #include "global.h"
        !            47: 
        !            48: 
        !            49: static inline struct tagUser *
        !            50: _selectAttribute(pwd_root_t * __restrict pwd, u_int uid, const char *csName)
        !            51: {
        !            52:        struct tagUser fu;
        !            53: 
        !            54:        if (!pwd)
        !            55:                return NULL;
        !            56:        else
        !            57:                memset(&fu, 0, sizeof fu);
        !            58: 
        !            59:        if (csName) {
        !            60:                io_setlikeVar(&fu.usr_name, string, strlen(csName) + 1, csName);
        !            61:                return RB_FIND(tagPWD, pwd, &fu);
        !            62:        }
        !            63: 
        !            64:        return cfg_findPasswdBy(pwd, PWD_CRIT_UID, uid);
        !            65: }
        !            66: 
        !            67: /* --------------------------------------------------------------- */
        !            68: 
        !            69: /*
        !            70:  * cfg_findPasswdBy() - Find user by criteria position in list
        !            71:  *
        !            72:  * @pwd = Password root
        !            73:  * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID|PWD_CRIT_GID]
        !            74:  * @arg1 = Username | UID | GID
        !            75:  * return: NULL not found item or error and !=NULL found item
        !            76:  */
        !            77: struct tagUser *
        !            78: cfg_findPasswdBy(pwd_root_t * __restrict pwd, int criteria, ...)
        !            79: {
        !            80:        struct tagUser *u;
        !            81:        va_list lst;
        !            82:        ait_val_t v;
        !            83: 
        !            84:        if (!pwd)
        !            85:                return NULL;
        !            86: 
        !            87:        va_start(lst, criteria);
        !            88:        switch (criteria) {
        !            89:                case PWD_CRIT_NAME:
        !            90:                        AIT_SET_STR(&v, va_arg(lst, char*));
        !            91:                        break;
        !            92:                case PWD_CRIT_UID:
        !            93:                case PWD_CRIT_GID:
        !            94:                        AIT_SET_U32(&v, va_arg(lst, u_int));
        !            95:                        break;
        !            96:                default:
        !            97:                        return NULL;
        !            98:        }
        !            99:        va_end(lst);
        !           100: 
        !           101:        SLIST_FOREACH(u, pwd, usr_next)
        !           102:                switch (criteria) {
        !           103:                        case PWD_CRIT_NAME:
        !           104:                                if (!io_cmpVar(&u->usr_name, &v)) {
        !           105:                                        AIT_FREE_VAL(&v);
        !           106:                                        return u;
        !           107:                                }
        !           108:                                break;
        !           109:                        case PWD_CRIT_UID:
        !           110:                                if (AIT_GET_U32(&u->usr_uid) == AIT_GET_U32(&v)) {
        !           111:                                        AIT_FREE_VAL(&v);
        !           112:                                        return u;
        !           113:                                }
        !           114:                                break;
        !           115:                        case PWD_CRIT_GID:
        !           116:                                if (AIT_GET_U32(&u->usr_gid) == AIT_GET_U32(&v)) {
        !           117:                                        AIT_FREE_VAL(&v);
        !           118:                                        return u;
        !           119:                                }
        !           120:                                break;
        !           121:                }
        !           122: 
        !           123:        AIT_FREE_VAL(&v);
        !           124:        return NULL;
        !           125: }
        !           126: 
        !           127: /*
        !           128:  * cfg_unsetPasswd() - Unset item from passwords and free resources
        !           129:  *
        !           130:  * @pwd = Password root
        !           131:  * @criteria = Search criteria [PWD_CRIT_NAME|PWD_CRIT_UID]
        !           132:  * @arg1 = Username | UID
        !           133:  * return: 0 item not found, -1 error or 1 removed item
        !           134:  */
        !           135: int
        !           136: cfg_unsetPasswd(pwd_root_t * __restrict pwd, int criteria, ...)
        !           137: {
        !           138:        struct tagUser *u;
        !           139:        va_list lst;
        !           140: 
        !           141:        if (!pwd)
        !           142:                return -1;
        !           143: 
        !           144:        va_start(lst, criteria);
        !           145:        switch (criteria) {
        !           146:                case PWD_CRIT_NAME:
        !           147:                        u = _selectAttribute(pwd, 0, va_arg(lst, char*));
        !           148:                        break;
        !           149:                case PWD_CRIT_UID:
        !           150:                        u = _selectAttribute(pwd, va_arg(lst, u_int), NULL);
        !           151:                        break;
        !           152:                default:
        !           153:                        va_end(lst);
        !           154:                        return -1;
        !           155:        }
        !           156:        va_end(lst);
        !           157:        if (!u)
        !           158:                return 0;
        !           159: 
        !           160:        PWD_LOCK(pwd);
        !           161:        RB_REMOVE(tagPWD, pwd, u);
        !           162:        SLIST_REMOVE(pwd, u, tagUser, usr_next);
        !           163:        PWD_UNLOCK(pwd);
        !           164: 
        !           165:        AIT_FREE_VAL(&u->usr_name);
        !           166:        AIT_FREE_VAL(&u->usr_pass);
        !           167:        AIT_FREE_VAL(&u->usr_uid);
        !           168:        AIT_FREE_VAL(&u->usr_gid);
        !           169:        AIT_FREE_VAL(&u->usr_class);
        !           170:        AIT_FREE_VAL(&u->usr_change);
        !           171:        AIT_FREE_VAL(&u->usr_expire);
        !           172:        AIT_FREE_VAL(&u->usr_realm);
        !           173:        AIT_FREE_VAL(&u->usr_home);
        !           174:        AIT_FREE_VAL(&u->usr_shell);
        !           175:        io_free(u);
        !           176:        return 1;
        !           177: }
        !           178: 
        !           179: #if 0
        !           180: /*
        !           181:  * cfg_setAttribute() - Set item in config or adding new item if not exists
        !           182:  *
        !           183:  * @cfg = Config root
        !           184:  * @csSec = Config section //[{csSec}], if NULL set in *default* section
        !           185:  * @csAttr = Config attribute //{csAttr} = ...
        !           186:  * @csVal = Config value //... = {csVal} to setup
        !           187:  * return: 0 nothing changed, -1 error, 1 found and updated item or 2 added new item
        !           188:  */
        !           189: int
        !           190: cfg_setAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, const char *csVal)
        !           191: {
        !           192:        struct tagCfg *av, *section;
        !           193: 
        !           194:        if (!cfg || !csAttr)
        !           195:                return -1;
        !           196: 
        !           197:        av = _selectAttribute(cfg, csSec, csAttr);
        !           198:        if (!av) {
        !           199:                /* adding new element */
        !           200:                section = _selectAttribute(cfg, csSec, NULL);
        !           201: 
        !           202:                av = io_malloc(sizeof(struct tagCfg));
        !           203:                if (!av) {
        !           204:                        LOGERR;
        !           205:                        return -1;
        !           206:                } else {
        !           207:                        memset(av, 0, sizeof(struct tagCfg));
        !           208: 
        !           209:                        CFG_RC_LOCK(cfg);
        !           210:                        if (!section)
        !           211:                                SLIST_INSERT_HEAD(cfg, av, cfg_next);
        !           212:                        else
        !           213:                                SLIST_INSERT_AFTER(section, av, cfg_next);
        !           214:                        CFG_RC_UNLOCK(cfg);
        !           215:                }
        !           216: 
        !           217:                if (csSec && *csSec) {
        !           218:                        AIT_SET_STR(&av->cfg_sec, csSec);
        !           219:                        AIT_KEY(&av->cfg_sec) = crcFletcher16(AIT_GET_LIKE(&av->cfg_sec, u_short*), 
        !           220:                                        io_align(AIT_LEN(&av->cfg_sec) - 1, 2) / 2);
        !           221:                }
        !           222:                AIT_SET_STR(&av->cfg_val, csVal ? csVal : "");
        !           223:                AIT_SET_STR(&av->cfg_attr, csAttr);
        !           224:                AIT_KEY(&av->cfg_attr) = crcFletcher16(AIT_GET_LIKE(&av->cfg_attr, u_short*), 
        !           225:                                io_align(AIT_LEN(&av->cfg_attr) - 1, 2) / 2);
        !           226: 
        !           227:                CFG_RC_LOCK(cfg);
        !           228:                RB_INSERT(tagRC, cfg, av);
        !           229:                CFG_RC_UNLOCK(cfg);
        !           230:                return 2;
        !           231:        }
        !           232: 
        !           233:        if (csVal && AIT_ADDR(&av->cfg_val) && 
        !           234:                        strcmp((char*) csVal, (char*) AIT_GET_STR(&av->cfg_val))) {
        !           235:                /* Update element */
        !           236:                AIT_FREE_VAL(&av->cfg_val);
        !           237:                AIT_SET_STR(&av->cfg_val, csVal);
        !           238:                return 1;
        !           239:        }
        !           240: 
        !           241:        /* Nothing happens ... found & values is equal! */
        !           242:        return 0;
        !           243: }
        !           244: 
        !           245: /*
        !           246:  * cfg_getAttribute() - Get item from config and return value from it
        !           247:  *
        !           248:  * @cfg = Config root
        !           249:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
        !           250:  * @csAttr = Config attribute //{csAttr} = ..., if NULL unset as *any* attribute
        !           251:  * return: NULL item not found or null parameters, !=NULL value const string
        !           252:  */
        !           253: inline const char *
        !           254: cfg_getAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr)
        !           255: {
        !           256:        struct tagCfg *av;
        !           257: 
        !           258:        if (!cfg || !csAttr)
        !           259:                return NULL;
        !           260: 
        !           261:        av = _selectAttribute(cfg, csSec, csAttr);
        !           262:        if (!av)
        !           263:                return NULL;
        !           264: 
        !           265:        return AIT_GET_STR(&av->cfg_val);
        !           266: }
        !           267: 
        !           268: /*
        !           269:  * cfg_loadAttribute() - Get guarded attribute, if not found item return *default value*
        !           270:  *
        !           271:  * @cfg = Config root
        !           272:  * @csSec = Config section //[{csSec}], if NULL unset in *default* section
        !           273:  * @csAttr = Config attribute //{csAttr} = ...
        !           274:  * @val = Return buffer for item Value //... = {val}
        !           275:  * @csDefValue = *Default Value* for return in //{val}, if not found item in config
        !           276:  * return: 0 item not found, -1 error or >0 number of copied bytes in //{val}
        !           277:  */
        !           278: int
        !           279: cfg_loadAttribute(cfg_root_t * __restrict cfg, const char *csSec, const char *csAttr, 
        !           280:                ait_val_t * __restrict val, const char *csDefValue)
        !           281: {
        !           282:        struct tagCfg *av;
        !           283:        int ret = 0;
        !           284: 
        !           285:        if (!cfg || !csAttr || !val) {
        !           286:                cfg_SetErr(EINVAL, "Invalid argument(s)");
        !           287:                return -1;
        !           288:        }
        !           289: 
        !           290:        AIT_INIT_VAL(val);
        !           291:        av = _selectAttribute(cfg, csSec, csAttr);
        !           292:        if (!av) {
        !           293:                /* not found item */
        !           294:                if (csDefValue) {
        !           295:                        AIT_SET_STR(val, csDefValue);
        !           296:                        ret = AIT_LEN(val);
        !           297:                } else
        !           298:                        AIT_INIT_VAL(val);
        !           299:                return ret;
        !           300:        }
        !           301: 
        !           302:        if (AIT_ISEMPTY(&av->cfg_val) || !AIT_ADDR(&av->cfg_val) || 
        !           303:                        !*AIT_GET_LIKE(&av->cfg_val, char*)) {
        !           304:                /* empty value */
        !           305:                if (csDefValue) {
        !           306:                        AIT_SET_STR(val, csDefValue);
        !           307:                        ret = AIT_LEN(val);
        !           308:                } else
        !           309:                        AIT_INIT_VAL(val);
        !           310:        } else {
        !           311:                /* copy value */
        !           312:                AIT_SET_STR(val, AIT_GET_STR(&av->cfg_val));
        !           313:                ret = AIT_LEN(val);
        !           314:        }
        !           315: 
        !           316:        return ret;
        !           317: }
        !           318: #endif

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