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