Annotation of libaitrpc/src/builtin.c, revision 1.4

1.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 $
1.4     ! misho       6: * $Id: builtin.c,v 1.3.2.12 2011/09/07 01:19:19 misho Exp $
1.1       misho       7: *
1.2       misho       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
                     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: */
1.1       misho      46: #include "global.h"
                     47: 
                     48: 
                     49: /* builtin RPC server functions */
                     50: 
                     51: int
1.4     ! misho      52: rpcServerClients(rpc_func_t *call, int ic, array_t *iv)
1.1       misho      53: {
1.2       misho      54:        rpc_srv_t *srv;
1.4     ! misho      55:        array_t *vals;
1.1       misho      56:        rpc_cli_t *cli;
                     57:        register int i;
1.4     ! misho      58:        int len;
1.3       misho      59:        const char *str = NULL;
1.4     ! misho      60:        char *val, wrk[INET6_ADDRSTRLEN];
1.1       misho      61:        struct sockaddr_in *s;
                     62:        struct sockaddr_in6 *s6;
1.3       misho      63:        struct sockaddr_un *su;
1.1       misho      64: 
1.2       misho      65:        RPC_CALLBACK_CHECK_INPUT(call);
                     66:        if (!call->func_parent)
1.1       misho      67:                return -1;
                     68:        else
1.2       misho      69:                srv = call->func_parent;
                     70: 
1.4     ! misho      71:        rpc_srv_getVars(call, &vals);
        !            72: 
        !            73:        len = srv->srv_numcli * STRSIZ;
        !            74:        if (!(val = malloc(len))) {
        !            75:                LOGERR;
1.1       misho      76:                return -1;
1.4     ! misho      77:        } else
        !            78:                memset(val, 0, len);
1.1       misho      79: 
1.4     ! misho      80:        for (i = 0, cli = srv->srv_clients; i < srv->srv_numcli; i++, cli++) {
        !            81:                if (!cli->cli_sa.sa_family)
1.1       misho      82:                        continue;
                     83: 
1.3       misho      84:                switch (cli->cli_sa.sa_family) {
                     85:                        case AF_INET:
                     86:                                s = (struct sockaddr_in*) &cli->cli_sa;
                     87:                                str = inet_ntop(cli->cli_sa.sa_family, &s->sin_addr, wrk, sizeof wrk);
                     88:                                break;
                     89:                        case AF_INET6:
                     90:                                s6 = (struct sockaddr_in6*) &cli->cli_sa;
                     91:                                str = inet_ntop(cli->cli_sa.sa_family, &s6->sin6_addr, wrk, sizeof wrk);
                     92:                                break;
                     93:                        case AF_LOCAL:
                     94:                                su = (struct sockaddr_un*) &cli->cli_sa;
                     95:                                str = su->sun_path;
                     96:                                break;
1.1       misho      97:                }
1.2       misho      98:                if (str)
1.4     ! misho      99:                        strlcat(val, (char*) str, len);
1.2       misho     100:                else
1.4     ! misho     101:                        strlcat(val, "0.0.0.0", len);
        !           102:                strlcat(val, " ", len);
1.1       misho     103:        }
                    104: 
1.4     ! misho     105:        AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
        !           106:        free(val);
1.1       misho     107:        return 0;
                    108: }
                    109: 
                    110: int
1.4     ! misho     111: rpcServerCalls(rpc_func_t *call, int ic, array_t *iv)
1.1       misho     112: {
1.2       misho     113:        rpc_srv_t *srv;
1.4     ! misho     114:        array_t *vals;
1.1       misho     115:        rpc_func_t *f;
                    116:        register int i;
1.4     ! misho     117:        int len;
        !           118:        char *val, str[MAXPATHLEN];
1.1       misho     119: 
1.2       misho     120:        RPC_CALLBACK_CHECK_INPUT(call);
                    121:        if (!call->func_parent)
1.1       misho     122:                return -1;
1.2       misho     123:        else
                    124:                srv = call->func_parent;
1.1       misho     125: 
                    126:        for (i = 0, f = srv->srv_funcs; f; i++, f = f->func_next);
1.4     ! misho     127:        len = i * STRSIZ;
        !           128: 
        !           129:        rpc_srv_getVars(call, &vals);
        !           130: 
        !           131:        if (!(val = malloc(len))) {
        !           132:                LOGERR;
1.1       misho     133:                return -1;
1.4     ! misho     134:        } else
        !           135:                memset(val, 0, len);
1.1       misho     136: 
1.4     ! misho     137:        for (f = srv->srv_funcs, i = 0; f; f = f->func_next) {
1.1       misho     138:                if (*f->func_name) {
1.3       misho     139:                        memset(str, 0, sizeof str);
1.4     ! misho     140:                        snprintf(str, sizeof str, "/%s/%s(%d); ", 
        !           141:                                        f->func_file, f->func_name, io_arraySize(f->func_vars));
        !           142:                        strlcat(val, str, len);
1.1       misho     143:                }
                    144:        }
                    145: 
1.4     ! misho     146:        AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
        !           147:        free(val);
1.1       misho     148:        return 0;
                    149: }
                    150: 
                    151: int
1.4     ! misho     152: rpcServerSessions(rpc_func_t *call, int ic, array_t *iv)
1.1       misho     153: {
1.2       misho     154:        rpc_srv_t *srv;
1.4     ! misho     155:        array_t *vals;
        !           156:        ait_val_t *v;
1.1       misho     157: 
1.2       misho     158:        RPC_CALLBACK_CHECK_INPUT(call);
                    159:        if (!call->func_parent)
1.1       misho     160:                return -1;
1.2       misho     161:        else
                    162:                srv = call->func_parent;
1.1       misho     163: 
1.4     ! misho     164:        rpc_srv_getVars(call, &vals);
1.1       misho     165: 
1.4     ! misho     166:        v = io_array(vals, 0, ait_val_t*);
        !           167:        AIT_SET_U8(v, srv->srv_session.sess_version);
        !           168:        v = io_array(vals, 1, ait_val_t*);
        !           169:        AIT_SET_U32(v, srv->srv_session.sess_program);
        !           170:        v = io_array(vals, 2, ait_val_t*);
        !           171:        AIT_SET_U32(v, srv->srv_session.sess_process);
        !           172:        v = io_array(vals, 3, ait_val_t*);
        !           173:        AIT_SET_I32(v, srv->srv_numcli);
1.1       misho     174: 
                    175:        return 0;
                    176: }
1.2       misho     177: 
                    178: int
1.4     ! misho     179: rpcServerShutdown(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     180: {
                    181:        rpc_srv_t *srv;
                    182: 
                    183:        RPC_CALLBACK_CHECK_INPUT(call);
                    184:        if (!call->func_parent)
                    185:                return -1;
                    186:        else
                    187:                srv = call->func_parent;
                    188: 
                    189:        pthread_mutex_lock(&srv->srv_mtx);
1.3       misho     190:        srv->srv_kill = kill;
1.2       misho     191:        pthread_mutex_unlock(&srv->srv_mtx);
                    192: 
                    193:        return 0;
                    194: }
                    195: 
                    196: // ----------------------------------------------------
                    197: 
                    198: int
1.4     ! misho     199: rpcBLOBServerShutdown(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     200: {
                    201:        rpc_srv_t *srv;
                    202: 
                    203:        RPC_CALLBACK_CHECK_INPUT(call);
                    204:        if (!call->func_parent)
                    205:                return -1;
                    206:        else
                    207:                srv = call->func_parent;
                    208: 
                    209:        pthread_mutex_lock(&srv->srv_blob.mtx);
1.3       misho     210:        srv->srv_blob.state = kill;
1.2       misho     211:        pthread_mutex_unlock(&srv->srv_blob.mtx);
                    212: 
                    213:        return 0;
                    214: }
                    215: 
                    216: int
1.4     ! misho     217: rpcBLOBServerVars(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     218: {
                    219:        rpc_srv_t *srv;
1.4     ! misho     220:        array_t *vals;
1.2       misho     221:        rpc_blob_t *b;
                    222:        register int i;
1.4     ! misho     223:        char *val, str[64];
        !           224:        int len;
1.2       misho     225: 
                    226:        RPC_CALLBACK_CHECK_INPUT(call);
                    227:        if (!call->func_parent)
                    228:                return -1;
                    229:        else
                    230:                srv = call->func_parent;
                    231: 
1.4     ! misho     232:        rpc_srv_getVars(call, &vals);
        !           233: 
        !           234:        if (srv->srv_blob.state == kill) {
        !           235:                AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is killed");
        !           236:                return 1;
        !           237:        }
        !           238:        if (srv->srv_blob.state == disable) {
        !           239:                AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is disabled");
        !           240:                return 1;
        !           241:        }
        !           242: 
1.2       misho     243:        pthread_mutex_lock(&srv->srv_blob.mtx);
                    244:        for (i = 0, b = srv->srv_blob.blobs; b; i++, b = b->blob_next);
1.4     ! misho     245:        len = i * sizeof str;
        !           246: 
        !           247:        if (!len) {
        !           248:                AIT_SET_STR(io_array(vals, 0, ait_val_t*), "");
1.2       misho     249:                pthread_mutex_unlock(&srv->srv_blob.mtx);
                    250:                return 0;
                    251:        }
                    252: 
1.4     ! misho     253:        if (!(val = malloc(len))) {
        !           254:                LOGERR;
        !           255:                pthread_mutex_unlock(&srv->srv_blob.mtx);
        !           256:                return -1;
        !           257:        } else
        !           258:                memset(val, 0, len);
        !           259: 
        !           260:        for (b = srv->srv_blob.blobs, i = 0; b; i++, b = b->blob_next) {
        !           261:                memset(str, 0, sizeof str);
        !           262:                snprintf(str, sizeof str, "0x%0X(%lu)=%p ", b->blob_var, (u_long) b->blob_len, b->blob_data);
        !           263:                strlcat(val, str, len);
        !           264:        }
        !           265: 
        !           266:        AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
        !           267:        free(val);
1.2       misho     268:        pthread_mutex_unlock(&srv->srv_blob.mtx);
                    269:        return 0;
                    270: }
                    271: 
                    272: int
1.4     ! misho     273: rpcBLOBServerState(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     274: {
                    275:        rpc_srv_t *srv;
                    276: 
                    277:        RPC_CALLBACK_CHK_ARGS(call, ic);
                    278:        if (!call->func_parent)
                    279:                return -1;
                    280:        else
                    281:                srv = call->func_parent;
                    282: 
1.4     ! misho     283:        if (AIT_TYPE(io_array(iv, 0, ait_val_t*)) != i32)
1.2       misho     284:                return -1;
                    285: 
1.4     ! misho     286:        pthread_mutex_lock(&srv->srv_mtx);
        !           287:        srv->srv_blob.state = AIT_GET_I32(io_array(iv, 0, ait_val_t*));
        !           288:        pthread_mutex_unlock(&srv->srv_mtx);
1.2       misho     289:        return 0;
                    290: }
                    291: 
                    292: int
1.4     ! misho     293: rpcBLOBServerClients(rpc_func_t *call, int ic, array_t *iv)
1.2       misho     294: {
                    295:        rpc_srv_t *srv;
1.4     ! misho     296:        array_t *vals;
1.2       misho     297:        rpc_cli_t *cli;
                    298:        register int i;
1.4     ! misho     299:        int len;
1.3       misho     300:        const char *str = NULL;
1.4     ! misho     301:        char *val, wrk[INET6_ADDRSTRLEN];
1.2       misho     302:        struct sockaddr_in *s;
                    303:        struct sockaddr_in6 *s6;
1.3       misho     304:        struct sockaddr_un *su;
1.2       misho     305: 
                    306:        RPC_CALLBACK_CHECK_INPUT(call);
                    307:        if (!call->func_parent)
                    308:                return -1;
                    309:        else
                    310:                srv = call->func_parent;
                    311: 
1.4     ! misho     312:        rpc_srv_getVars(call, &vals);
        !           313: 
        !           314:        if (srv->srv_blob.state == kill) {
        !           315:                AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is killed");
        !           316:                return 1;
        !           317:        }
        !           318:        if (srv->srv_blob.state == disable) {
        !           319:                AIT_SET_STR(io_array(vals, 0, ait_val_t*), "BLOB Server is disabled");
        !           320:                return 1;
        !           321:        }
        !           322: 
        !           323:        len = srv->srv_numcli * STRSIZ;
        !           324:        if (!(val = malloc(len))) {
        !           325:                LOGERR;
1.2       misho     326:                return -1;
1.4     ! misho     327:        } else
        !           328:                memset(val, 0, len);
1.2       misho     329: 
1.4     ! misho     330:        for (i = 0, cli = srv->srv_blob.clients; i < srv->srv_numcli; i++, cli++) {
        !           331:                if (!cli->cli_sa.sa_family)
1.2       misho     332:                        continue;
                    333: 
1.3       misho     334:                switch (cli->cli_sa.sa_family) {
                    335:                        case AF_INET:
                    336:                                s = (struct sockaddr_in*) &cli->cli_sa;
                    337:                                str = inet_ntop(cli->cli_sa.sa_family, &s->sin_addr, wrk, sizeof wrk);
                    338:                                break;
                    339:                        case AF_INET6:
                    340:                                s6 = (struct sockaddr_in6*) &cli->cli_sa;
                    341:                                str = inet_ntop(cli->cli_sa.sa_family, &s6->sin6_addr, wrk, sizeof wrk);
                    342:                                break;
                    343:                        case AF_LOCAL:
                    344:                                su = (struct sockaddr_un*) &cli->cli_sa;
                    345:                                str = su->sun_path;
                    346:                                break;
1.2       misho     347:                }
                    348:                if (str)
1.4     ! misho     349:                        strlcat(val, (char*) str, len);
1.2       misho     350:                else
1.4     ! misho     351:                        strlcat(val, "0.0.0.0", len);
        !           352:                strlcat(val, " ", len);
1.2       misho     353:        }
                    354: 
1.4     ! misho     355:        AIT_SET_STR(io_array(vals, 0, ait_val_t*), val);
        !           356:        free(val);
1.2       misho     357:        return 0;
                    358: }

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