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

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

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