Annotation of libaitrpc/src/srv.c, revision 1.6.2.5
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.5 ! misho 6: * $Id: srv.c,v 1.6.2.4 2012/03/13 17:10:13 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.1 misho 424: srv->srv_blob.dir = strdup(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.1 misho 441: free(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.1 misho 450: free(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.1 misho 456: free(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.1 misho 463: free(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.1 misho 469: free(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.1 misho 476: free(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.1 misho 485: free(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.1 misho 525: if (srv->srv_blob.dir)
526: free(srv->srv_blob.dir);
527:
1.4 misho 528: /* close all clients connections & server socket */
1.2 misho 529: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
1.6 misho 530: if (c->cli_sa.sa.sa_family)
1.2 misho 531: shutdown(c->cli_sock, SHUT_RDWR);
532: close(srv->srv_blob.server.cli_sock);
533:
1.5 misho 534: pthread_mutex_lock(&srv->srv_blob.mtx);
1.2 misho 535: if (srv->srv_blob.clients) {
536: free(srv->srv_blob.clients);
537: srv->srv_blob.clients = NULL;
538: }
539:
1.4 misho 540: /* detach blobs */
1.2 misho 541: while ((f = srv->srv_blob.blobs)) {
542: srv->srv_blob.blobs = f->blob_next;
543: rpc_srv_blobFree(srv, f);
544: free(f);
545: }
546: pthread_mutex_unlock(&srv->srv_blob.mtx);
547:
548: while (pthread_mutex_trylock(&srv->srv_blob.mtx) == EBUSY);
549: pthread_mutex_destroy(&srv->srv_blob.mtx);
550: }
551:
552: /*
1.6.2.4 misho 553: * rpc_srv_loopBLOB() - Execute Main BLOB server loop and wait for clients requests
554: *
1.2 misho 555: * @srv = RPC Server instance
556: * return: -1 error or 0 ok, infinite loop ...
557: */
558: int
1.5 misho 559: rpc_srv_loopBLOB(rpc_srv_t * __restrict srv)
1.2 misho 560: {
1.6 misho 561: socklen_t salen = sizeof(io_sockaddr_t);
1.2 misho 562: register int i;
563: rpc_cli_t *c;
564: fd_set fds;
565: int ret;
566: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.6.2.1 misho 567: pthread_attr_t attr;
1.2 misho 568:
1.4 misho 569: if (!srv || srv->srv_blob.state == kill) {
1.2 misho 570: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start BLOB server ...\n");
571: return -1;
572: }
573:
1.6.2.1 misho 574: tv.tv_sec = srv->srv_session.sess_timeout;
575:
1.2 misho 576: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
577: LOGERR;
578: return -1;
579: }
580:
1.6.2.1 misho 581: pthread_attr_init(&attr);
582: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
583:
584: /* main BLOB loop */
1.4 misho 585: while (srv->srv_blob.state != kill && srv->srv_kill != kill) {
1.2 misho 586: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
1.6 misho 587: if (!c->cli_sa.sa.sa_family)
1.2 misho 588: break;
589: if (i >= srv->srv_numcli) {
1.5 misho 590: #ifdef HAVE_PTHREAD_YIELD
591: pthread_yield();
592: #else
1.2 misho 593: usleep(1000000);
1.5 misho 594: #endif
1.2 misho 595: continue;
596: }
597:
598: FD_ZERO(&fds);
599: FD_SET(srv->srv_blob.server.cli_sock, &fds);
600: ret = select(srv->srv_blob.server.cli_sock + 1, &fds, NULL, NULL, &tv);
601: if (ret == -1) {
602: LOGERR;
603: ret = 1;
604: break;
605: }
606: if (!ret)
607: continue;
608:
1.6 misho 609: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa.sa, &salen);
1.2 misho 610: if (c->cli_sock == -1) {
611: LOGERR;
612: continue;
613: } else
614: c->cli_parent = srv;
615:
1.6.2.1 misho 616: /* spawn dispatch thread for BLOB client */
617: if (pthread_create(&c->cli_tid, &attr, rpc_srv_dispatchVars, c)) {
1.2 misho 618: LOGERR;
619: continue;
1.6.2.1 misho 620: }
1.2 misho 621: }
622:
1.5 misho 623: srv->srv_blob.state = kill;
1.2 misho 624:
1.6.2.1 misho 625: pthread_attr_destroy(&attr);
1.2 misho 626: return 0;
627: }
628:
629:
630: /*
1.6.2.4 misho 631: * rpc_srv_initServer() - Init & create RPC Server
632: *
1.1 misho 633: * @regProgID = ProgramID for authentication & recognition
634: * @regProcID = ProcessID for authentication & recognition
635: * @concurentClients = Concurent clients at same time to this server
1.5 misho 636: * @netBuf = Network buffer length, if =0 == BUFSIZ (also meaning max RPC packet)
1.4 misho 637: * @family = Family type, AF_INET, AF_INET6 or AF_LOCAL
638: * @csHost = Host name or address for bind server, if NULL any address
1.1 misho 639: * @Port = Port for bind server, if Port == 0 default port is selected
640: * return: NULL == error or !=NULL bind and created RPC server instance
641: */
642: rpc_srv_t *
643: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
1.5 misho 644: int netBuf, u_short family, const char *csHost, u_short Port)
1.1 misho 645: {
646: rpc_srv_t *srv = NULL;
647: int n = 1;
648: struct hostent *host = NULL;
1.6 misho 649: io_sockaddr_t sa;
1.1 misho 650:
1.4 misho 651: if (!concurentClients || !regProgID ||
652: (family != AF_INET && family != AF_INET6 && family != AF_LOCAL)) {
1.1 misho 653: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
654: return NULL;
655: }
656: if (!Port)
657: Port = RPC_DEFPORT;
1.5 misho 658: if (!netBuf)
659: netBuf = BUFSIZ;
1.4 misho 660: if (csHost && family != AF_LOCAL) {
1.1 misho 661: host = gethostbyname2(csHost, family);
662: if (!host) {
663: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
664: return NULL;
665: }
666: }
1.4 misho 667: memset(&sa, 0, sizeof sa);
1.6 misho 668: sa.sa.sa_family = family;
1.1 misho 669: switch (family) {
670: case AF_INET:
1.6 misho 671: sa.sin.sin_len = sizeof(struct sockaddr_in);
672: sa.sin.sin_port = htons(Port);
1.1 misho 673: if (csHost)
1.6 misho 674: memcpy(&sa.sin.sin_addr, host->h_addr, host->h_length);
1.1 misho 675: break;
676: case AF_INET6:
1.6 misho 677: sa.sin6.sin6_len = sizeof(struct sockaddr_in6);
678: sa.sin6.sin6_port = htons(Port);
1.4 misho 679: if (csHost)
1.6 misho 680: memcpy(&sa.sin6.sin6_addr, host->h_addr, host->h_length);
1.4 misho 681: break;
682: case AF_LOCAL:
1.6 misho 683: sa.sun.sun_len = sizeof(struct sockaddr_un);
1.1 misho 684: if (csHost)
1.6 misho 685: strlcpy(sa.sun.sun_path, csHost, sizeof sa.sun.sun_path);
686: unlink(sa.sun.sun_path);
1.1 misho 687: break;
688: default:
689: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
690: return NULL;
691: }
692:
693: srv = malloc(sizeof(rpc_srv_t));
694: if (!srv) {
695: LOGERR;
696: return NULL;
697: } else
698: memset(srv, 0, sizeof(rpc_srv_t));
699:
1.5 misho 700: srv->srv_netbuf = netBuf;
1.1 misho 701: srv->srv_numcli = concurentClients;
702: srv->srv_session.sess_version = RPC_VERSION;
1.6.2.1 misho 703: srv->srv_session.sess_timeout = DEF_RPC_TIMEOUT;
1.1 misho 704: srv->srv_session.sess_program = regProgID;
705: srv->srv_session.sess_process = regProcID;
706:
1.2 misho 707: srv->srv_server.cli_tid = pthread_self();
1.1 misho 708: srv->srv_server.cli_parent = srv;
1.6 misho 709: memcpy(&srv->srv_server.cli_sa, &sa, sizeof sa);
1.4 misho 710:
711: /* create server socket */
1.1 misho 712: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
713: if (srv->srv_server.cli_sock == -1) {
714: LOGERR;
715: free(srv);
716: return NULL;
717: }
718: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
719: LOGERR;
720: close(srv->srv_server.cli_sock);
721: free(srv);
722: return NULL;
723: }
1.5 misho 724: n = srv->srv_netbuf;
725: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_SNDBUF, &n, sizeof n) == -1) {
726: LOGERR;
727: close(srv->srv_server.cli_sock);
728: free(srv);
729: return NULL;
730: }
731: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_RCVBUF, &n, sizeof n) == -1) {
732: LOGERR;
733: close(srv->srv_server.cli_sock);
734: free(srv);
735: return NULL;
736: }
1.6 misho 737: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa.sa,
738: srv->srv_server.cli_sa.sa.sa_len) == -1) {
1.1 misho 739: LOGERR;
740: close(srv->srv_server.cli_sock);
741: free(srv);
742: return NULL;
743: }
744:
1.4 misho 745: /* allocate pool for concurent clients */
1.1 misho 746: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
747: if (!srv->srv_clients) {
748: LOGERR;
749: close(srv->srv_server.cli_sock);
750: free(srv);
751: return NULL;
752: } else
753: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
754:
1.2 misho 755: pthread_mutex_init(&srv->srv_mtx, NULL);
756:
757: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.5 misho 758: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 1);
759: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 4);
760: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 1);
1.1 misho 761: return srv;
762: }
763:
764: /*
1.6.2.4 misho 765: * rpc_srv_endServer() - Destroy RPC server, close all opened sockets and free resources
766: *
1.6 misho 767: * @psrv = RPC Server instance
1.1 misho 768: * return: none
769: */
770: void
1.6 misho 771: rpc_srv_endServer(rpc_srv_t ** __restrict psrv)
1.1 misho 772: {
773: rpc_cli_t *c;
774: register int i;
775: rpc_func_t *f;
776:
1.6 misho 777: if (!psrv || !*psrv) {
1.1 misho 778: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
779: return;
780: }
781:
1.6 misho 782: rpc_srv_endBLOBServer(*psrv);
1.1 misho 783:
1.4 misho 784: /* close all clients connections & server socket */
1.6 misho 785: for (i = 0, c = (*psrv)->srv_clients; i < (*psrv)->srv_numcli && c; i++, c++)
786: if (c->cli_sa.sa.sa_family) {
1.1 misho 787: shutdown(c->cli_sock, SHUT_RDWR);
1.2 misho 788: close(c->cli_sock);
789: }
1.6 misho 790: close((*psrv)->srv_server.cli_sock);
1.1 misho 791:
1.6 misho 792: if ((*psrv)->srv_clients) {
793: free((*psrv)->srv_clients);
794: (*psrv)->srv_clients = NULL;
795: (*psrv)->srv_numcli = 0;
1.1 misho 796: }
797:
1.4 misho 798: /* detach exported calls */
1.6 misho 799: pthread_mutex_lock(&(*psrv)->srv_mtx);
800: while ((f = (*psrv)->srv_funcs)) {
801: (*psrv)->srv_funcs = f->func_next;
802: io_freeVars(&f->func_vars);
1.6.2.4 misho 803: AIT_FREE_VAL(&f->func_name);
804: AIT_FREE_VAL(&f->func_file);
1.2 misho 805: free(f);
806: }
1.6 misho 807: pthread_mutex_unlock(&(*psrv)->srv_mtx);
1.2 misho 808:
1.6 misho 809: while (pthread_mutex_trylock(&(*psrv)->srv_mtx) == EBUSY);
810: pthread_mutex_destroy(&(*psrv)->srv_mtx);
1.2 misho 811:
1.6 misho 812: free(*psrv);
813: *psrv = NULL;
1.1 misho 814: }
815:
816: /*
1.6.2.4 misho 817: * rpc_srv_loopServer() - Execute Main server loop and wait for clients requests
818: *
1.1 misho 819: * @srv = RPC Server instance
820: * return: -1 error or 0 ok, infinite loop ...
821: */
822: int
1.5 misho 823: rpc_srv_loopServer(rpc_srv_t * __restrict srv)
1.1 misho 824: {
1.6 misho 825: socklen_t salen = sizeof(io_sockaddr_t);
1.1 misho 826: register int i;
827: rpc_cli_t *c;
1.2 misho 828: fd_set fds;
829: int ret;
830: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.6.2.2 misho 831: pthread_attr_t attr;
1.1 misho 832:
833: if (!srv) {
834: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
835: return -1;
836: }
837:
1.6.2.3 misho 838: tv.tv_sec = srv->srv_session.sess_timeout;
839:
1.5 misho 840: /* activate BLOB server worker if srv->srv_blob.state == enable */
841: rpc_srv_execBLOBServer(srv);
842:
1.1 misho 843: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
844: LOGERR;
845: return -1;
846: }
847:
1.6.2.2 misho 848: pthread_attr_init(&attr);
849: pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
850:
1.6.2.3 misho 851: /* main rpc loop */
1.4 misho 852: while (srv->srv_kill != kill) {
1.1 misho 853: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
1.6 misho 854: if (!c->cli_sa.sa.sa_family)
1.1 misho 855: break;
1.2 misho 856: if (i >= srv->srv_numcli) {
1.5 misho 857: #ifdef HAVE_PTHREAD_YIELD
858: pthread_yield();
859: #else
1.1 misho 860: usleep(1000000);
1.5 misho 861: #endif
1.1 misho 862: continue;
863: }
1.2 misho 864:
865: FD_ZERO(&fds);
866: FD_SET(srv->srv_server.cli_sock, &fds);
867: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
868: if (ret == -1) {
869: LOGERR;
870: ret = 1;
871: break;
872: }
873: if (!ret)
874: continue;
875:
1.6 misho 876: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa.sa, &salen);
1.1 misho 877: if (c->cli_sock == -1) {
878: LOGERR;
879: continue;
880: } else
881: c->cli_parent = srv;
882:
1.6.2.3 misho 883: /* spawn rpc client dispatcher */
1.6.2.2 misho 884: if (pthread_create(&c->cli_tid, &attr, rpc_srv_dispatchCall, c)) {
1.1 misho 885: LOGERR;
886: continue;
1.6.2.2 misho 887: }
1.1 misho 888: }
889:
1.6.2.2 misho 890: pthread_attr_destroy(&attr);
1.1 misho 891: return 0;
892: }
893:
894: // ---------------------------------------------------------
895:
896: /*
897: * rpc_srv_execCall() Execute registered call from RPC server
1.6.2.4 misho 898: *
1.1 misho 899: * @call = Register RPC call
900: * @rpc = IN RPC call structure
1.5 misho 901: * @args = IN RPC calling arguments from RPC client
1.1 misho 902: * return: -1 error, !=-1 ok
903: */
904: int
1.2 misho 905: rpc_srv_execCall(rpc_func_t * __restrict call, struct tagRPCCall * __restrict rpc,
1.5 misho 906: array_t * __restrict args)
1.1 misho 907: {
908: void *dl;
909: rpc_callback_t func;
910: int ret;
911:
1.2 misho 912: if (!call || !rpc || !call->func_parent) {
1.6.2.4 misho 913: rpc_SetErr(EINVAL, "Invalid parameter can`t exec function");
1.1 misho 914: return -1;
915: }
916:
1.6.2.4 misho 917: dl = dlopen(AIT_VOID(&call->func_file), RTLD_NOW);
1.1 misho 918: if (!dl) {
1.6.2.4 misho 919: rpc_SetErr(ENOENT, "Can`t attach module %s!", dlerror());
1.1 misho 920: return -1;
921: }
922:
1.6.2.5 ! misho 923: func = dlsym(dl, (const char*) AIT_GET_STR(&call->func_name));
1.1 misho 924: if (func)
1.6 misho 925: ret = func(call, ntohs(rpc->call_argc), args);
1.1 misho 926: else {
1.6.2.4 misho 927: rpc_SetErr(ENOEXEC, "Can`t find function %s!", dlerror());
1.1 misho 928: ret = -1;
929: }
930:
931: dlclose(dl);
932: return ret;
933: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>