Annotation of libaitrpc/src/srv.c, revision 1.6.2.3
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.6.2.3 ! misho 6: * $Id: srv.c,v 1.6.2.2 2012/03/12 15:04:38 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:
1.6.2.1 misho 15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
1.2 misho 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: static void *
50: rpc_srv_dispatchCall(void *arg)
51: {
52: rpc_cli_t *c = arg;
53: rpc_srv_t *s;
1.3 misho 54: rpc_func_t *f = NULL;
1.5 misho 55: array_t *arr;
1.1 misho 56: struct tagRPCCall *rpc;
1.3 misho 57: struct tagRPCRet *rrpc;
58: rpc_sess_t ses = { 0 };
1.1 misho 59: fd_set fds;
1.5 misho 60: u_char *buf;
1.2 misho 61: int ret, argc = 0, Limit = 0;
1.1 misho 62: register int i;
1.6 misho 63: uint16_t tag = 0;
64: uint32_t hash = 0;
1.1 misho 65:
66: if (!arg) {
67: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t procced RPC client ...\n");
68: return NULL;
69: } else
70: s = c->cli_parent;
71:
1.5 misho 72: buf = malloc(s->srv_netbuf);
73: if (!buf) {
74: LOGERR;
75: return NULL;
76: }
77:
1.1 misho 78: do {
79: FD_ZERO(&fds);
80: FD_SET(c->cli_sock, &fds);
81: ret = select(c->cli_sock + 1, &fds, NULL, NULL, NULL);
82: if (ret == -1) {
1.4 misho 83: if (errno == EINTR && s->srv_kill != kill)
84: continue;
85:
86: LOGERR;
1.1 misho 87: ret = -2;
1.4 misho 88: break;
1.1 misho 89: }
1.5 misho 90: memset(buf, 0, s->srv_netbuf);
91: ret = recv(c->cli_sock, buf, s->srv_netbuf, 0);
1.4 misho 92: if (ret == -1) {
1.1 misho 93: LOGERR;
94: ret = -3;
95: break;
96: }
1.4 misho 97: if (!ret) { /* receive EOF */
1.1 misho 98: ret = 0;
99: break;
100: }
101: if (ret < sizeof(struct tagRPCCall)) {
1.5 misho 102: rpc_SetErr(ERPCMISMATCH, "Error:: too short RPC packet ...\n");
1.1 misho 103: ret = -4;
1.6 misho 104: if (s->srv_kill != kill)
105: continue;
106: else
107: break;
1.1 misho 108: } else
109: rpc = (struct tagRPCCall*) buf;
1.4 misho 110: /* check RPC packet session info */
1.6 misho 111: if (rpc_chkPktSession(&rpc->call_session, &s->srv_session)) {
1.5 misho 112: rpc_SetErr(ERPCMISMATCH, "Error:: get invalid RPC session ...\n");
1.1 misho 113: ret = -5;
1.2 misho 114: goto makeReply;
115: } else
116: Limit = sizeof(struct tagRPCCall);
1.5 misho 117:
1.6 misho 118: tag = rpc->call_tag;
119: hash = rpc->call_hash;
120:
1.4 misho 121: /* RPC is OK! Go decapsulate variables ... */
1.6 misho 122: if (ntohs(rpc->call_argc)) {
123: arr = io_buffer2vars(buf + Limit, s->srv_netbuf - Limit,
124: ntohs(rpc->call_argc), 1);
1.5 misho 125: if (!arr) {
1.2 misho 126: ret = -5;
127: goto makeReply;
1.1 misho 128: }
1.5 misho 129: } else
130: arr = NULL;
1.1 misho 131:
1.4 misho 132: /* execute call */
1.1 misho 133: argc = 0;
1.3 misho 134: memcpy(&ses, &rpc->call_session, sizeof ses);
1.6 misho 135: if (!(f = rpc_srv_getCall(s, ntohs(tag), ntohl(hash)))) {
1.5 misho 136: rpc_SetErr(EPROGUNAVAIL, "Error:: call not found into RPC server ...\n");
1.1 misho 137: ret = -6;
138: } else
1.5 misho 139: if ((ret = rpc_srv_execCall(f, rpc, arr)) == -1)
1.2 misho 140: ret = -9;
1.5 misho 141: else {
142: if (arr)
143: io_arrayDestroy(&arr);
144: argc = rpc_srv_getVars(f, &arr);
145: goto makeReply; /* Call finish OK */
146: }
147:
148: if (arr)
149: io_arrayDestroy(&arr);
150:
1.3 misho 151: makeReply:
1.5 misho 152: /* Made reply */
153: memset(buf, 0, s->srv_netbuf);
1.3 misho 154: rrpc = (struct tagRPCRet*) buf;
155: Limit = sizeof(struct tagRPCRet);
1.1 misho 156:
1.3 misho 157: memcpy(&rrpc->ret_session, &ses, sizeof(rpc_sess_t));
1.6 misho 158: rrpc->ret_tag = tag;
159: rrpc->ret_hash = hash;
160: rrpc->ret_errno = htonl(rpc_Errno);
161: rrpc->ret_retcode = htonl(ret);
162: rrpc->ret_argc = htons(argc);
1.1 misho 163:
1.5 misho 164: if (argc && arr) {
165: /* Go Encapsulate variables ... */
1.6 misho 166: if ((i = io_vars2buffer(buf + Limit, s->srv_netbuf - Limit, arr)) == -1) {
167: io_clrVars(f->func_vars);
1.3 misho 168: argc = 0;
169: ret = -7;
1.5 misho 170: rpc_SetErr(EBADRPC, "Error:: in prepare RPC packet values (-7) ...\n");
1.3 misho 171: goto makeReply;
1.5 misho 172: } else {
173: Limit += i;
1.1 misho 174:
1.6 misho 175: io_clrVars(f->func_vars);
1.1 misho 176: }
177: }
178:
1.4 misho 179: ret = send(c->cli_sock, buf, Limit, 0);
180: if (ret == -1) {
1.1 misho 181: LOGERR;
182: ret = -8;
183: break;
184: }
185: if (ret != Limit) {
1.5 misho 186: rpc_SetErr(EPROCUNAVAIL, "Error:: in send RPC request, should be send %d bytes, "
1.1 misho 187: "really is %d\n", Limit, ret);
188: ret = -9;
1.6 misho 189: if (s->srv_kill != kill)
190: continue;
191: else
192: break;
1.1 misho 193: }
1.4 misho 194: } while (ret > -1 || s->srv_kill != kill);
1.1 misho 195:
196: shutdown(c->cli_sock, SHUT_RDWR);
197: close(c->cli_sock);
198: memset(c, 0, sizeof(rpc_cli_t));
1.5 misho 199: free(buf);
1.2 misho 200: return (void*) (long)ret;
201: }
202:
203:
204: static void *
205: rpc_srv_dispatchVars(void *arg)
206: {
207: rpc_cli_t *c = arg;
208: rpc_srv_t *s;
209: rpc_blob_t *b;
1.4 misho 210: int ret = 0;
1.2 misho 211: fd_set fds;
212: u_char buf[sizeof(struct tagBLOBHdr)];
213: struct tagBLOBHdr *blob;
214:
215: if (!arg) {
216: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t procced BLOB client ...\n");
217: return NULL;
218: } else
219: s = c->cli_parent;
220:
221: do {
1.4 misho 222: /* check for disable service at this moment? */
223: if (s->srv_blob.state == disable && s->srv_kill != kill) {
224: usleep(100000);
225: #ifdef HAVE_PTHREAD_YIELD
226: pthread_yield();
227: #endif
228: continue;
1.2 misho 229: }
230:
231: FD_ZERO(&fds);
232: FD_SET(c->cli_sock, &fds);
233: ret = select(c->cli_sock + 1, &fds, NULL, NULL, NULL);
234: if (ret == -1) {
1.4 misho 235: if (errno == EINTR && s->srv_kill != kill && s->srv_blob.state != kill)
236: continue;
237:
238: LOGERR;
1.2 misho 239: ret = -2;
1.4 misho 240: break;
1.2 misho 241: }
242:
243: memset(buf, 0, sizeof buf);
1.4 misho 244: ret = recv(c->cli_sock, buf, sizeof buf, 0);
245: if (ret == -1) {
1.2 misho 246: LOGERR;
247: ret = -3;
248: break;
249: }
1.4 misho 250: /* receive EOF, disable or kill service */
251: if (!ret || s->srv_blob.state == kill || s->srv_kill == kill) {
1.2 misho 252: ret = 0;
253: break;
254: }
255: if (ret < sizeof(struct tagBLOBHdr)) {
1.5 misho 256: rpc_SetErr(ERPCMISMATCH, "Error:: too short BLOB packet ...\n");
1.2 misho 257: ret = -4;
1.6 misho 258: if (s->srv_kill != kill && s->srv_blob.state != kill)
259: continue;
260: else
261: break;
1.2 misho 262: } else
263: blob = (struct tagBLOBHdr*) buf;
1.4 misho 264: /* check BLOB packet session info */
1.2 misho 265: if (memcmp(&blob->hdr_session, &s->srv_session, sizeof blob->hdr_session)) {
266: rpc_SetErr(EINVAL, "Error:: get invalid BLOB session ...\n");
267: ret = -5;
268: goto makeReply;
269: }
1.4 misho 270: /* Go to proceed packet ... */
1.2 misho 271: switch (blob->hdr_cmd) {
272: case get:
273: if (!(b = rpc_srv_getBLOB(s, blob->hdr_var))) {
274: rpc_SetErr(EINVAL, "Error:: var (%x) not found into BLOB server ...\n",
275: blob->hdr_var);
276: ret = -6;
277: break;
278: } else
279: blob->hdr_len = b->blob_len;
280:
281: if (rpc_srv_blobMap(s, b) != -1) {
282: ret = rpc_srv_sendBLOB(c, b);
283: rpc_srv_blobUnmap(b);
284: } else
285: ret = -7;
286: break;
287: case set:
288: if ((b = rpc_srv_registerBLOB(s, blob->hdr_len))) {
1.4 misho 289: /* set new BLOB variable for reply :) */
1.2 misho 290: blob->hdr_var = b->blob_var;
291:
292: ret = rpc_srv_recvBLOB(c, b);
293: rpc_srv_blobUnmap(b);
294: } else
295: ret = -7;
296: break;
297: case unset:
298: ret = rpc_srv_unregisterBLOB(s, blob->hdr_var);
299: if (ret == -1)
300: ret = -7;
301: break;
302: default:
1.5 misho 303: rpc_SetErr(EPROCUNAVAIL, "Error:: unsupported BLOB command (%d)...\n",
1.2 misho 304: blob->hdr_cmd);
305: ret = -7;
306: }
307:
308: makeReply:
1.4 misho 309: /* Replay to client! */
1.2 misho 310: blob->hdr_cmd = ret < 0 ? error : ok;
311: blob->hdr_ret = ret;
1.4 misho 312: ret = send(c->cli_sock, buf, sizeof buf, 0);
313: if (ret == -1) {
1.2 misho 314: LOGERR;
315: ret = -8;
316: break;
317: }
318: if (ret != sizeof buf) {
1.5 misho 319: rpc_SetErr(EPROCUNAVAIL, "Error:: in send BLOB reply, should be send %d bytes, "
1.2 misho 320: "really is %d\n", sizeof buf, ret);
321: ret = -9;
1.6 misho 322: if (s->srv_kill != kill && s->srv_blob.state != kill)
323: continue;
324: else
325: break;
1.2 misho 326: }
1.4 misho 327: } while (ret > -1 || s->srv_kill != kill);
1.2 misho 328:
329: shutdown(c->cli_sock, SHUT_RDWR);
330: close(c->cli_sock);
331: memset(c, 0, sizeof(rpc_cli_t));
1.5 misho 332: return (void*) ((long)ret);
1.1 misho 333: }
334:
335: // -------------------------------------------------
336:
337: /*
1.2 misho 338: * rpc_srv_initBLOBServer() Init & create BLOB Server
1.4 misho 339: * @srv = RPC server instance
1.2 misho 340: * @Port = Port for bind server, if Port == 0 default port is selected
341: * @diskDir = Disk place for BLOB file objects
342: * return: -1 == error or 0 bind and created BLOB server instance
343: */
344: int
345: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
346: {
347: int n = 1;
1.6 misho 348: io_sockaddr_t sa;
1.2 misho 349:
350: if (!srv) {
1.6.2.1 misho 351: rpc_SetErr(EINVAL, "Invalid parameters can`t init BLOB server");
1.2 misho 352: return -1;
353: }
354: if (srv->srv_blob.state) {
1.6.2.1 misho 355: rpc_SetErr(EPERM, "Already started BLOB server!");
1.2 misho 356: return 0;
357: }
358:
359: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
360: if (access(diskDir, R_OK | W_OK) == -1) {
361: LOGERR;
362: return -1;
363: } else
1.6.2.1 misho 364: srv->srv_blob.dir = strdup(diskDir);
1.2 misho 365:
366: srv->srv_blob.server.cli_tid = pthread_self();
367: srv->srv_blob.server.cli_parent = srv;
1.4 misho 368:
369: memcpy(&sa, &srv->srv_server.cli_sa, sizeof sa);
1.6 misho 370: switch (sa.sa.sa_family) {
1.4 misho 371: case AF_INET:
1.6 misho 372: sa.sin.sin_port = htons(Port ? Port : ntohs(sa.sin.sin_port) + 1);
1.4 misho 373: break;
374: case AF_INET6:
1.6 misho 375: sa.sin6.sin6_port = htons(Port ? Port : ntohs(sa.sin6.sin6_port) + 1);
1.4 misho 376: break;
377: case AF_LOCAL:
1.6 misho 378: strlcat(sa.sun.sun_path, ".blob", sizeof sa.sun.sun_path);
1.4 misho 379: break;
380: default:
1.6.2.1 misho 381: free(srv->srv_blob.dir);
1.4 misho 382: return -1;
1.2 misho 383: }
1.6 misho 384: memcpy(&srv->srv_blob.server.cli_sa, &sa, sizeof sa);
1.2 misho 385:
1.4 misho 386: /* create BLOB server socket */
1.6 misho 387: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa.sa_family, SOCK_STREAM, 0);
1.2 misho 388: if (srv->srv_blob.server.cli_sock == -1) {
389: LOGERR;
1.6.2.1 misho 390: free(srv->srv_blob.dir);
1.2 misho 391: return -1;
392: }
393: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
394: LOGERR;
395: close(srv->srv_blob.server.cli_sock);
1.6.2.1 misho 396: free(srv->srv_blob.dir);
1.2 misho 397: return -1;
398: }
1.5 misho 399: n = srv->srv_netbuf;
400: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
401: LOGERR;
402: close(srv->srv_blob.server.cli_sock);
1.6.2.1 misho 403: free(srv->srv_blob.dir);
1.5 misho 404: return -1;
405: }
406: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
407: LOGERR;
408: close(srv->srv_blob.server.cli_sock);
1.6.2.1 misho 409: free(srv->srv_blob.dir);
1.5 misho 410: return -1;
411: }
1.6 misho 412: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa.sa,
413: srv->srv_blob.server.cli_sa.sa.sa_len) == -1) {
1.2 misho 414: LOGERR;
415: close(srv->srv_blob.server.cli_sock);
1.6.2.1 misho 416: free(srv->srv_blob.dir);
1.2 misho 417: return -1;
418: }
419:
1.4 misho 420: /* allocate pool for concurent clients */
1.2 misho 421: srv->srv_blob.clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
422: if (!srv->srv_blob.clients) {
423: LOGERR;
424: close(srv->srv_blob.server.cli_sock);
1.6.2.1 misho 425: free(srv->srv_blob.dir);
1.2 misho 426: return -1;
427: } else
428: memset(srv->srv_blob.clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
429:
430: pthread_mutex_init(&srv->srv_blob.mtx, NULL);
431:
432: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
1.5 misho 433: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 1);
434: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 1);
435: rpc_srv_registerCall(srv, NULL, CALL_BLOBSTATE, 0);
1.2 misho 436:
1.4 misho 437: srv->srv_blob.state = enable; /* enable BLOB */
1.2 misho 438: return 0;
439: }
440:
441: /*
442: * rpc_srv_endBLOBServer() Destroy BLOB server, close all opened sockets and free resources
443: * @srv = RPC Server instance
444: * return: none
445: */
446: void
447: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
448: {
449: rpc_cli_t *c;
450: register int i;
451: rpc_blob_t *f;
452:
453: if (!srv) {
1.6.2.1 misho 454: rpc_SetErr(EINVAL, "Can`t destroy server because parameter is null!");
1.2 misho 455: return;
456: } else
1.4 misho 457: srv->srv_blob.state = kill;
1.2 misho 458:
459: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSHUTDOWN);
460: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBCLIENTS);
461: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBVARS);
462: rpc_srv_unregisterCall(srv, NULL, CALL_BLOBSTATE);
463:
1.6.2.1 misho 464: if (srv->srv_blob.dir)
465: free(srv->srv_blob.dir);
466:
1.4 misho 467: /* close all clients connections & server socket */
1.2 misho 468: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
1.6 misho 469: if (c->cli_sa.sa.sa_family)
1.2 misho 470: shutdown(c->cli_sock, SHUT_RDWR);
471: close(srv->srv_blob.server.cli_sock);
472:
1.5 misho 473: pthread_mutex_lock(&srv->srv_blob.mtx);
1.2 misho 474: if (srv->srv_blob.clients) {
475: free(srv->srv_blob.clients);
476: srv->srv_blob.clients = NULL;
477: }
478:
1.4 misho 479: /* detach blobs */
1.2 misho 480: while ((f = srv->srv_blob.blobs)) {
481: srv->srv_blob.blobs = f->blob_next;
482: rpc_srv_blobFree(srv, f);
483: free(f);
484: }
485: pthread_mutex_unlock(&srv->srv_blob.mtx);
486:
487: while (pthread_mutex_trylock(&srv->srv_blob.mtx) == EBUSY);
488: pthread_mutex_destroy(&srv->srv_blob.mtx);
489: }
490:
491: /*
1.5 misho 492: * rpc_srv_loopBLOB() Execute Main BLOB server loop and wait for clients requests
1.2 misho 493: * @srv = RPC Server instance
494: * return: -1 error or 0 ok, infinite loop ...
495: */
496: int
1.5 misho 497: rpc_srv_loopBLOB(rpc_srv_t * __restrict srv)
1.2 misho 498: {
1.6 misho 499: socklen_t salen = sizeof(io_sockaddr_t);
1.2 misho 500: register int i;
501: rpc_cli_t *c;
502: fd_set fds;
503: int ret;
504: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.6.2.1 misho 505: pthread_attr_t attr;
1.2 misho 506:
1.4 misho 507: if (!srv || srv->srv_blob.state == kill) {
1.2 misho 508: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start BLOB server ...\n");
509: return -1;
510: }
511:
1.6.2.1 misho 512: tv.tv_sec = srv->srv_session.sess_timeout;
513:
1.2 misho 514: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
515: LOGERR;
516: return -1;
517: }
518:
1.6.2.1 misho 519: pthread_attr_init(&attr);
520: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
521:
522: /* main BLOB loop */
1.4 misho 523: while (srv->srv_blob.state != kill && srv->srv_kill != kill) {
1.2 misho 524: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
1.6 misho 525: if (!c->cli_sa.sa.sa_family)
1.2 misho 526: break;
527: if (i >= srv->srv_numcli) {
1.5 misho 528: #ifdef HAVE_PTHREAD_YIELD
529: pthread_yield();
530: #else
1.2 misho 531: usleep(1000000);
1.5 misho 532: #endif
1.2 misho 533: continue;
534: }
535:
536: FD_ZERO(&fds);
537: FD_SET(srv->srv_blob.server.cli_sock, &fds);
538: ret = select(srv->srv_blob.server.cli_sock + 1, &fds, NULL, NULL, &tv);
539: if (ret == -1) {
540: LOGERR;
541: ret = 1;
542: break;
543: }
544: if (!ret)
545: continue;
546:
1.6 misho 547: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa.sa, &salen);
1.2 misho 548: if (c->cli_sock == -1) {
549: LOGERR;
550: continue;
551: } else
552: c->cli_parent = srv;
553:
1.6.2.1 misho 554: /* spawn dispatch thread for BLOB client */
555: if (pthread_create(&c->cli_tid, &attr, rpc_srv_dispatchVars, c)) {
1.2 misho 556: LOGERR;
557: continue;
1.6.2.1 misho 558: }
1.2 misho 559: }
560:
1.5 misho 561: srv->srv_blob.state = kill;
1.2 misho 562:
1.6.2.1 misho 563: pthread_attr_destroy(&attr);
1.2 misho 564: return 0;
565: }
566:
567:
568: /*
1.1 misho 569: * rpc_srv_initServer() Init & create RPC Server
570: * @regProgID = ProgramID for authentication & recognition
571: * @regProcID = ProcessID for authentication & recognition
572: * @concurentClients = Concurent clients at same time to this server
1.5 misho 573: * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 574: * @family = Family type, AF_INET, AF_INET6 or AF_LOCAL
575: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 576: * @Port = Port for bind server, if Port == 0 default port is selected
577: * return: NULL == error or !=NULL bind and created RPC server instance
578: */
579: rpc_srv_t *
580: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
1.5 misho 581: int netBuf, u_short family, const char *csHost, u_short Port)
1.1 misho 582: {
583: rpc_srv_t *srv = NULL;
584: int n = 1;
585: struct hostent *host = NULL;
1.6 misho 586: io_sockaddr_t sa;
1.1 misho 587:
1.4 misho 588: if (!concurentClients || !regProgID ||
589: (family != AF_INET && family != AF_INET6 && family != AF_LOCAL)) {
1.1 misho 590: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
591: return NULL;
592: }
593: if (!Port)
594: Port = RPC_DEFPORT;
1.5 misho 595: if (!netBuf)
596: netBuf = BUFSIZ;
1.4 misho 597: if (csHost && family != AF_LOCAL) {
1.1 misho 598: host = gethostbyname2(csHost, family);
599: if (!host) {
600: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
601: return NULL;
602: }
603: }
1.4 misho 604: memset(&sa, 0, sizeof sa);
1.6 misho 605: sa.sa.sa_family = family;
1.1 misho 606: switch (family) {
607: case AF_INET:
1.6 misho 608: sa.sin.sin_len = sizeof(struct sockaddr_in);
609: sa.sin.sin_port = htons(Port);
1.1 misho 610: if (csHost)
1.6 misho 611: memcpy(&sa.sin.sin_addr, host->h_addr, host->h_length);
1.1 misho 612: break;
613: case AF_INET6:
1.6 misho 614: sa.sin6.sin6_len = sizeof(struct sockaddr_in6);
615: sa.sin6.sin6_port = htons(Port);
1.4 misho 616: if (csHost)
1.6 misho 617: memcpy(&sa.sin6.sin6_addr, host->h_addr, host->h_length);
1.4 misho 618: break;
619: case AF_LOCAL:
1.6 misho 620: sa.sun.sun_len = sizeof(struct sockaddr_un);
1.1 misho 621: if (csHost)
1.6 misho 622: strlcpy(sa.sun.sun_path, csHost, sizeof sa.sun.sun_path);
623: unlink(sa.sun.sun_path);
1.1 misho 624: break;
625: default:
626: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
627: return NULL;
628: }
629:
630: srv = malloc(sizeof(rpc_srv_t));
631: if (!srv) {
632: LOGERR;
633: return NULL;
634: } else
635: memset(srv, 0, sizeof(rpc_srv_t));
636:
1.5 misho 637: srv->srv_netbuf = netBuf;
1.1 misho 638: srv->srv_numcli = concurentClients;
639: srv->srv_session.sess_version = RPC_VERSION;
1.6.2.1 misho 640: srv->srv_session.sess_timeout = DEF_RPC_TIMEOUT;
1.1 misho 641: srv->srv_session.sess_program = regProgID;
642: srv->srv_session.sess_process = regProcID;
643:
1.2 misho 644: srv->srv_server.cli_tid = pthread_self();
1.1 misho 645: srv->srv_server.cli_parent = srv;
1.6 misho 646: memcpy(&srv->srv_server.cli_sa, &sa, sizeof sa);
1.4 misho 647:
1.6.2.1 misho 648: /* init rpc scheduler */
649: srv->srv_sched = schedBegin();
650: if (!srv->srv_sched) {
651: rpc_SetErr(sched_GetErrno(), "%s", sched_GetError());
652: return NULL;
653: }
654:
1.4 misho 655: /* create server socket */
1.1 misho 656: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
657: if (srv->srv_server.cli_sock == -1) {
658: LOGERR;
1.6.2.1 misho 659: schedEnd(&srv->srv_sched);
1.1 misho 660: free(srv);
661: return NULL;
662: }
663: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
664: LOGERR;
665: close(srv->srv_server.cli_sock);
1.6.2.1 misho 666: schedEnd(&srv->srv_sched);
1.1 misho 667: free(srv);
668: return NULL;
669: }
1.5 misho 670: n = srv->srv_netbuf;
671: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
672: LOGERR;
673: close(srv->srv_server.cli_sock);
1.6.2.1 misho 674: schedEnd(&srv->srv_sched);
1.5 misho 675: free(srv);
676: return NULL;
677: }
678: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
679: LOGERR;
680: close(srv->srv_server.cli_sock);
1.6.2.1 misho 681: schedEnd(&srv->srv_sched);
1.5 misho 682: free(srv);
683: return NULL;
684: }
1.6 misho 685: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
686: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 687: LOGERR;
688: close(srv->srv_server.cli_sock);
1.6.2.1 misho 689: schedEnd(&srv->srv_sched);
1.1 misho 690: free(srv);
691: return NULL;
692: }
693:
1.4 misho 694: /* allocate pool for concurent clients */
1.1 misho 695: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
696: if (!srv->srv_clients) {
697: LOGERR;
698: close(srv->srv_server.cli_sock);
1.6.2.1 misho 699: schedEnd(&srv->srv_sched);
1.1 misho 700: free(srv);
701: return NULL;
702: } else
703: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
704:
1.2 misho 705: pthread_mutex_init(&srv->srv_mtx, NULL);
706:
707: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.5 misho 708: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 1);
709: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 4);
710: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 1);
1.1 misho 711: return srv;
712: }
713:
714: /*
715: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
1.6 misho 716: * @psrv = RPC Server instance
1.1 misho 717: * return: none
718: */
719: void
1.6 misho 720: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 721: {
722: rpc_cli_t *c;
723: register int i;
724: rpc_func_t *f;
725:
1.6 misho 726: if (!psrv || !*psrv) {
1.1 misho 727: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
728: return;
729: }
730:
1.6 misho 731: rpc_srv_endBLOBServer(*psrv);
1.1 misho 732:
1.6.2.1 misho 733: /* destroy rpc scheduler */
734: schedEnd(&(*psrv)->srv_sched);
735:
1.4 misho 736: /* close all clients connections & server socket */
1.6 misho 737: for (i = 0, c = (*psrv)->srv_clients; i < (*psrv)->srv_numcli && c; i++, c++)
738: if (c->cli_sa.sa.sa_family) {
1.1 misho 739: shutdown(c->cli_sock, SHUT_RDWR);
1.2 misho 740: close(c->cli_sock);
741: }
1.6 misho 742: close((*psrv)->srv_server.cli_sock);
1.1 misho 743:
1.6 misho 744: if ((*psrv)->srv_clients) {
745: free((*psrv)->srv_clients);
746: (*psrv)->srv_clients = NULL;
747: (*psrv)->srv_numcli = 0;
1.1 misho 748: }
749:
1.4 misho 750: /* detach exported calls */
1.6 misho 751: pthread_mutex_lock(&(*psrv)->srv_mtx);
752: while ((f = (*psrv)->srv_funcs)) {
753: (*psrv)->srv_funcs = f->func_next;
754: io_freeVars(&f->func_vars);
1.2 misho 755: free(f);
756: }
1.6 misho 757: pthread_mutex_unlock(&(*psrv)->srv_mtx);
1.2 misho 758:
1.6 misho 759: while (pthread_mutex_trylock(&(*psrv)->srv_mtx) == EBUSY);
760: pthread_mutex_destroy(&(*psrv)->srv_mtx);
1.2 misho 761:
1.6 misho 762: free(*psrv);
763: *psrv = NULL;
1.1 misho 764: }
765:
766: /*
1.5 misho 767: * rpc_srv_loopServer() Execute Main server loop and wait for clients requests
1.1 misho 768: * @srv = RPC Server instance
769: * return: -1 error or 0 ok, infinite loop ...
770: */
771: int
1.5 misho 772: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 773: {
1.6 misho 774: socklen_t salen = sizeof(io_sockaddr_t);
1.1 misho 775: register int i;
776: rpc_cli_t *c;
1.2 misho 777: fd_set fds;
778: int ret;
779: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.6.2.2 misho 780: pthread_attr_t attr;
1.1 misho 781:
782: if (!srv) {
783: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
784: return -1;
785: }
786:
1.6.2.3 ! misho 787: tv.tv_sec = srv->srv_session.sess_timeout;
! 788:
1.5 misho 789: /* activate BLOB server worker if srv->srv_blob.state == enable */
790: rpc_srv_execBLOBServer(srv);
791:
1.1 misho 792: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
793: LOGERR;
794: return -1;
795: }
796:
1.6.2.2 misho 797: pthread_attr_init(&attr);
798: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
799:
1.6.2.3 ! misho 800: /* main rpc loop */
1.4 misho 801: while (srv->srv_kill != kill) {
1.1 misho 802: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
1.6 misho 803: if (!c->cli_sa.sa.sa_family)
1.1 misho 804: break;
1.2 misho 805: if (i >= srv->srv_numcli) {
1.5 misho 806: #ifdef HAVE_PTHREAD_YIELD
807: pthread_yield();
808: #else
1.1 misho 809: usleep(1000000);
1.5 misho 810: #endif
1.1 misho 811: continue;
812: }
1.2 misho 813:
814: FD_ZERO(&fds);
815: FD_SET(srv->srv_server.cli_sock, &fds);
816: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
817: if (ret == -1) {
818: LOGERR;
819: ret = 1;
820: break;
821: }
822: if (!ret)
823: continue;
824:
1.6 misho 825: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa.sa, &salen);
1.1 misho 826: if (c->cli_sock == -1) {
827: LOGERR;
828: continue;
829: } else
830: c->cli_parent = srv;
831:
1.6.2.3 ! misho 832: /* spawn rpc client dispatcher */
1.6.2.2 misho 833: if (pthread_create(&c->cli_tid, &attr, rpc_srv_dispatchCall, c)) {
1.1 misho 834: LOGERR;
835: continue;
1.6.2.2 misho 836: }
1.1 misho 837: }
838:
1.6.2.2 misho 839: pthread_attr_destroy(&attr);
1.1 misho 840: return 0;
841: }
842:
843: // ---------------------------------------------------------
844:
845: /*
846: * rpc_srv_execCall() Execute registered call from RPC server
847: * @call = Register RPC call
848: * @rpc = IN RPC call structure
1.5 misho 849: * @args = IN RPC calling arguments from RPC client
1.1 misho 850: * return: -1 error, !=-1 ok
851: */
852: int
1.2 misho 853: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
1.5 misho 854: array_t * __restrict args)
1.1 misho 855: {
856: void *dl;
857: rpc_callback_t func;
858: int ret;
859:
1.2 misho 860: if (!call || !rpc || !call->func_parent) {
1.1 misho 861: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
862: return -1;
863: }
864:
865: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
866: if (!dl) {
867: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
868: return -1;
869: }
870:
871: func = dlsym(dl, (char*) call->func_name);
872: if (func)
1.6 misho 873: ret = func(call, ntohs(rpc->call_argc), args);
1.1 misho 874: else {
875: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
876: ret = -1;
877: }
878:
879: dlclose(dl);
880: return ret;
881: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>