Annotation of libaitrpc/src/srv.c, revision 1.1.1.1.2.7
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.1.1.1.2.7! misho 6: * $Id: srv.c,v 1.1.1.1.2.6 2010/06/23 16:23:31 misho Exp $
1.1 misho 7: *
8: *************************************************************************/
9: #include "global.h"
10:
11:
12: static void *
13: rpc_srv_dispatchCall(void *arg)
14: {
1.1.1.1.2.4 misho 15: rpc_cli_t cli, *c = arg;
1.1 misho 16: rpc_srv_t *s;
17: rpc_val_t *vals, *v = NULL;
18: rpc_func_t *f;
19: struct tagRPCCall *rpc;
20: struct tagRPCRet rrpc;
21: fd_set fds;
22: u_char buf[BUFSIZ], *data;
23: int ret, argc, Limit = 0;
24: register int i;
25:
26: if (!arg) {
27: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t procced RPC client ...\n");
28: return NULL;
29: } else
30: s = c->cli_parent;
31:
32: do {
33: FD_ZERO(&fds);
34: FD_SET(c->cli_sock, &fds);
35: ret = select(c->cli_sock + 1, &fds, NULL, NULL, NULL);
36: if (ret == -1) {
37: ret = -2;
38: }
39: memset(&rrpc, 0, sizeof rrpc);
40: memset(buf, 0, BUFSIZ);
41: if ((ret = recv(c->cli_sock, buf, BUFSIZ, 0)) == -1) {
42: LOGERR;
43: ret = -3;
44: break;
45: }
46: if (!ret) { // receive EOF
47: ret = 0;
48: break;
49: }
50: if (ret < sizeof(struct tagRPCCall)) {
51: rpc_SetErr(EMSGSIZE, "Error:: too short RPC packet ...\n");
52: ret = -4;
53: break;
54: } else
55: rpc = (struct tagRPCCall*) buf;
56: // check RPC packet session info
57: if (memcmp(&rpc->call_session, &s->srv_session, sizeof rpc->call_session)) {
58: rpc_SetErr(EINVAL, "Error:: get invalid RPC session ...\n");
59: ret = -5;
60: break;
61: }
62: // RPC is OK! Go decapsulate variables ...
63: if (rpc->call_argc) {
64: v = (rpc_val_t*) (buf + sizeof(struct tagRPCCall));
65: // RPC received variables types OK!
66: data = (u_char*) v + rpc->call_argc * sizeof(rpc_val_t);
67: for (i = 0; i < rpc->call_argc; i++) {
68: switch (v[i].val_type) {
69: case buffer:
70: v[i].val.buffer = data;
71: data += v[i].val_len;
72: break;
73: case string:
74: v[i].val.string = (int8_t*) data;
75: data += v[i].val_len + 1;
76: break;
1.1.1.1.2.3 misho 77: case blob:
1.1.1.1.2.5 misho 78: rpc_srv_getBLOBVar(&v[i], data);
1.1.1.1.2.4 misho 79: data += sizeof(rpc_cli_t);
1.1 misho 80: break;
81: default:
82: break;
83: }
84: }
85: }
86:
87: argc = 0;
88: vals = NULL;
89: if (!(f = rpc_srv_getCall(s, rpc->call_tag, rpc->call_hash))) {
90: rpc_SetErr(EINVAL, "Error:: call not found into RPC server ...\n");
91: ret = -6;
92: } else
93: if ((ret = rpc_srv_execCall(s, f, rpc, v)) == -1)
94: ret = -6;
95: else
96: argc = rpc_srv_getValsCall(f, &vals);
97:
98: memcpy(&rrpc.ret_session, &rpc->call_session, sizeof rrpc.ret_session);
99: rrpc.ret_tag = rpc->call_tag;
100: rrpc.ret_hash = rpc->call_hash;
101: rrpc.ret_errno = rpc_Errno;
102: rrpc.ret_retcode = ret;
103: rrpc.ret_argc = argc;
104:
105: memset(buf, 0, BUFSIZ);
106: memcpy(buf, &rrpc, (Limit = sizeof rrpc));
107: if (argc && vals) {
108: v = (rpc_val_t*) (buf + sizeof rrpc);
109: memcpy(v, vals, argc * sizeof(rpc_val_t));
110: Limit += argc * sizeof(rpc_val_t);
111: data = (u_char*) v + argc * sizeof(rpc_val_t);
112: for (ret = i = 0; i < argc; i++) {
113: switch (vals[i].val_type) {
114: case buffer:
115: if (ret || Limit + vals[i].val_len > BUFSIZ) {
116: rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet (-7) ...\n");
117: rrpc.ret_retcode = ret = -7;
118: rrpc.ret_argc = 0;
119: break;
120: }
121:
122: memcpy(data, vals[i].val.buffer, vals[i].val_len);
123: data += vals[i].val_len;
124: Limit += vals[i].val_len;
125: break;
126: case string:
127: if (ret || Limit + vals[i].val_len + 1 > BUFSIZ) {
128: rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet (-7) ...\n");
129: rrpc.ret_retcode = ret = -7;
130: rrpc.ret_argc = 0;
131: break;
132: }
133:
134: memcpy(data, vals[i].val.string, vals[i].val_len + 1);
135: data += vals[i].val_len + 1;
136: Limit += vals[i].val_len + 1;
137: break;
1.1.1.1.2.3 misho 138: case blob:
1.1.1.1.2.4 misho 139: if (ret || Limit + sizeof(rpc_cli_t) > BUFSIZ) {
1.1 misho 140: rpc_SetErr(EMSGSIZE, "Error:: in prepare RPC packet (-7) ...\n");
141: rrpc.ret_retcode = ret = -7;
142: rrpc.ret_argc = 0;
143: break;
144: }
145:
1.1.1.1.2.4 misho 146: memcpy(data, &cli, sizeof(rpc_cli_t));
147: data += sizeof(rpc_cli_t);
148: Limit += sizeof(rpc_cli_t);
149:
1.1.1.1.2.5 misho 150: rpc_srv_setBLOBVar(&vals[i], &cli);
1.1 misho 151: break;
152: default:
153: break;
154: }
155:
156: RPC_FREE_VAL(&vals[i]);
157: }
158: }
159:
160: if ((ret = send(c->cli_sock, buf, Limit, 0)) == -1) {
161: LOGERR;
162: ret = -8;
163: break;
164: }
165: if (ret != Limit) {
166: rpc_SetErr(EBADMSG, "Error:: in send RPC request, should be send %d bytes, "
167: "really is %d\n", Limit, ret);
168: ret = -9;
169: break;
170: }
171: } while (ret > -1);
172:
173: shutdown(c->cli_sock, SHUT_RDWR);
174: close(c->cli_sock);
175: memset(c, 0, sizeof(rpc_cli_t));
176: return (void*) ret;
177: }
178:
1.1.1.1.2.4 misho 179:
180: static void *
181: rpc_srv_dispatchVars(void *arg)
182: {
183: rpc_cli_t *c = arg;
1.1.1.1.2.5 misho 184: rpc_srv_t *s;
185: rpc_blob_t *b;
186: int cx, ret;
187: fd_set fds;
1.1.1.1.2.6 misho 188: u_char buf[sizeof(struct tagBLOBHdr)];
1.1.1.1.2.5 misho 189: struct tagBLOBHdr *blob;
190:
191: if (!arg) {
192: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t procced BLOB client ...\n");
193: return NULL;
194: } else
195: s = c->cli_parent;
196:
197: cx = -1;
198: do {
199: cx++;
200:
201: FD_ZERO(&fds);
202: FD_SET(c->cli_sock, &fds);
203: ret = select(c->cli_sock + 1, &fds, NULL, NULL, NULL);
204: if (ret == -1) {
205: ret = -2;
206: }
1.1.1.1.2.6 misho 207: memset(buf, 0, sizeof buf);
208: if ((ret = recv(c->cli_sock, buf, sizeof buf, 0)) == -1) {
1.1.1.1.2.5 misho 209: LOGERR;
210: ret = -3;
211: break;
212: }
213: if (!ret) { // receive EOF
214: ret = 0;
215: break;
216: }
217: if (ret < sizeof(struct tagBLOBHdr)) {
218: rpc_SetErr(EMSGSIZE, "Error:: too short BLOB packet ...\n");
219: ret = -4;
220: break;
221: } else
222: blob = (struct tagBLOBHdr*) buf;
223: // check BLOB packet session info
224: if (memcmp(&blob->hdr_session, &s->srv_session, sizeof blob->hdr_session) ||
225: blob->hdr_seq != cx) {
226: rpc_SetErr(EINVAL, "Error:: get invalid BLOB session in seq=%d...\n", blob->hdr_seq);
227: ret = -5;
228: break;
229: }
230: // Go to decapsulate packet ...
231: if (!(b = rpc_srv_getBLOB(s, blob->hdr_var))) {
232: rpc_SetErr(EINVAL, "Error:: var (%x) not found into BLOB server ...\n", blob->hdr_var);
233: ret = -6;
234: break;
235: }
236: switch (blob->hdr_cmd) {
237: case get:
1.1.1.1.2.6 misho 238: if (rpc_srv_blobMap(s, b) != -1) {
239: ret = rpc_srv_sendBLOB(c, b);
240: rpc_srv_blobUnmap(b);
241: } else
242: ret = -7;
1.1.1.1.2.5 misho 243: break;
244: case set:
245: ret = rpc_srv_recvBLOB(c, b);
1.1.1.1.2.6 misho 246: if (ret == -1)
247: ret = -7;
1.1.1.1.2.5 misho 248: break;
249: case unset:
1.1.1.1.2.6 misho 250: ret = rpc_srv_deleteBLOB(c, b);
251: if (ret == -1)
252: ret = -7;
1.1.1.1.2.5 misho 253: break;
254: default:
255: rpc_SetErr(EINVAL, "Error:: unsupported BLOB command (%d)...\n",
256: blob->hdr_cmd);
1.1.1.1.2.7! misho 257: ret = -7;
1.1.1.1.2.5 misho 258: }
1.1.1.1.2.6 misho 259:
260: // Replay to client!
1.1.1.1.2.7! misho 261: blob->hdr_cmd = ret < 0 ? error : ok;
! 262: blob->hdr_seq = ret;
! 263: if ((ret = send(c->cli_sock, buf, sizeof buf, 0)) == -1) {
! 264: LOGERR;
! 265: ret = -8;
! 266: break;
! 267: }
! 268: if (ret != sizeof buf) {
! 269: rpc_SetErr(EBADMSG, "Error:: in send BLOB reply, should be send %d bytes, "
! 270: "really is %d\n", sizeof buf, ret);
! 271: ret = -9;
! 272: break;
! 273: }
1.1.1.1.2.5 misho 274: } while (ret > -1);
1.1.1.1.2.4 misho 275:
1.1.1.1.2.5 misho 276: shutdown(c->cli_sock, SHUT_RDWR);
277: close(c->cli_sock);
278: memset(c, 0, sizeof(rpc_cli_t));
1.1.1.1.2.4 misho 279: return (void*) ret;
280: }
281:
1.1 misho 282: // -------------------------------------------------
283:
284: /*
1.1.1.1.2.4 misho 285: * rpc_srv_initBLOBServer() Init & create BLOB Server
286: * @Port = Port for bind server, if Port == 0 default port is selected
1.1.1.1.2.5 misho 287: * @diskDir = Disk place for BLOB file objects
1.1.1.1.2.4 misho 288: * return: -1 == error or 0 bind and created BLOB server instance
289: */
290: int
1.1.1.1.2.5 misho 291: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
1.1.1.1.2.4 misho 292: {
293: int n = 1;
294: struct sockaddr_in sin;
295: struct sockaddr_in6 sin6;
296:
297: if (!srv) {
298: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init BLOB server ...\n");
299: return -1;
300: }
301: if (srv->srv_blob.state) {
302: rpc_SetErr(EPERM, "Warning:: Already started BLOB server!\n");
303: return 0;
304: }
305: if (!Port)
306: Port = RPC_DEFPORT + 1;
307:
308: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
1.1.1.1.2.5 misho 309: if (access(diskDir, R_OK | W_OK) == -1) {
310: LOGERR;
311: return -1;
312: } else
313: strlcpy(srv->srv_blob.dir, diskDir, UCHAR_MAX + 1);
314:
1.1.1.1.2.4 misho 315: srv->srv_blob.server.cli_tid = pthread_self();
316: srv->srv_blob.server.cli_parent = srv;
317: if (srv->srv_server.cli_sa.sa_family == AF_INET) {
318: memcpy(&sin, &srv->srv_server.cli_sa, sizeof sin);
319: sin.sin_port = htons(Port);
320: memcpy(&srv->srv_blob.server.cli_sa, &sin, sizeof(struct sockaddr));
321: } else {
322: memcpy(&sin6, &srv->srv_server.cli_sa, sizeof sin6);
323: sin6.sin6_port = htons(Port);
324: memcpy(&srv->srv_blob.server.cli_sa, &sin6, sizeof(struct sockaddr));
325: }
326:
327: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa_family, SOCK_STREAM, 0);
328: if (srv->srv_blob.server.cli_sock == -1) {
329: LOGERR;
330: return -1;
331: }
332: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
333: LOGERR;
334: close(srv->srv_blob.server.cli_sock);
335: return -1;
336: }
337: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa,
338: sizeof srv->srv_blob.server.cli_sa) == -1) {
339: LOGERR;
340: close(srv->srv_blob.server.cli_sock);
341: return -1;
342: }
343:
344: srv->srv_blob.clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
345: if (!srv->srv_blob.clients) {
346: LOGERR;
347: close(srv->srv_blob.server.cli_sock);
348: return -1;
349: } else
350: memset(srv->srv_blob.clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
351:
352: pthread_mutex_init(&srv->srv_blob.mtx, NULL);
353:
354: pthread_mutex_lock(&srv->srv_mtx);
355: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
356: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 0);
357: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 0);
358: pthread_mutex_unlock(&srv->srv_mtx);
359:
1.1.1.1.2.5 misho 360: srv->srv_blob.state = enable; // enable BLOB
1.1.1.1.2.4 misho 361: return 0;
362: }
363:
364: /*
365: * rpc_srv_endBLOBServer() Destroy BLOB server, close all opened sockets and free resources
366: * @srv = RPC Server instance
367: * return: none
368: */
369: void
370: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
371: {
372: rpc_cli_t *c;
373: register int i;
374: rpc_blob_t *f;
375:
376: if (!srv) {
377: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
378: return;
379: } else
1.1.1.1.2.5 misho 380: srv->srv_blob.state = disable;
1.1.1.1.2.4 misho 381:
382: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
383: if (c->cli_sa.sa_family)
384: shutdown(c->cli_sock, SHUT_RDWR);
385: close(srv->srv_blob.server.cli_sock);
386:
387: if (srv->srv_blob.clients)
388: free(srv->srv_blob.clients);
389:
390: pthread_mutex_lock(&srv->srv_blob.mtx);
391: while ((f = srv->srv_blob.blobs)) {
392: srv->srv_blob.blobs = f->blob_next;
393: free(f);
394: }
395: pthread_mutex_unlock(&srv->srv_blob.mtx);
396:
397: pthread_mutex_destroy(&srv->srv_blob.mtx);
398: }
399:
400: /*
401: * rpc_srv_execBLOBServer() Execute Main BLOB server loop and wait for clients requests
402: * @srv = RPC Server instance
403: * return: -1 error or 0 ok, infinite loop ...
404: */
405: int
406: rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv)
407: {
408: socklen_t salen = sizeof(struct sockaddr);
409: register int i;
410: rpc_cli_t *c;
411: fd_set fds;
412: int ret;
413: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
414:
415: if (!srv || !srv->srv_blob.state) {
416: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start BLOB server ...\n");
417: return -1;
418: }
419:
420: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
421: LOGERR;
422: return -1;
423: }
424:
425: while (!rpc_Kill) {
426: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
427: if (!c->cli_sa.sa_family)
428: break;
429: if (c && c->cli_sa.sa_family && c->cli_parent) {
430: usleep(1000000);
431: continue;
432: }
433:
434: FD_ZERO(&fds);
435: FD_SET(srv->srv_blob.server.cli_sock, &fds);
436: ret = select(srv->srv_blob.server.cli_sock + 1, &fds, NULL, NULL, &tv);
437: if (ret == -1) {
438: LOGERR;
439: ret = 1;
440: break;
441: }
442: if (!ret)
443: continue;
444:
445: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa, &salen);
446: if (c->cli_sock == -1) {
447: LOGERR;
448: continue;
449: } else
450: c->cli_parent = srv;
451:
452: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchVars, c)) {
453: LOGERR;
454: continue;
455: }
456: }
457:
458: return 0;
459: }
460:
461:
462: /*
1.1 misho 463: * rpc_srv_initServer() Init & create RPC Server
464: * @regProgID = ProgramID for authentication & recognition
465: * @regProcID = ProcessID for authentication & recognition
466: * @concurentClients = Concurent clients at same time to this server
467: * @family = Family socket type, AF_INET or AF_INET6
468: * @csHost = Host name or IP address for bind server, if NULL any address
469: * @Port = Port for bind server, if Port == 0 default port is selected
470: * return: NULL == error or !=NULL bind and created RPC server instance
471: */
472: rpc_srv_t *
473: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
474: u_short family, const char *csHost, u_short Port)
475: {
476: rpc_srv_t *srv = NULL;
477: int n = 1;
478: struct hostent *host = NULL;
479: struct sockaddr_in sin;
480: struct sockaddr_in6 sin6;
481:
482: if (!concurentClients || !regProgID || (family != AF_INET && family != AF_INET6)) {
483: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
484: return NULL;
485: }
486: if (!Port)
487: Port = RPC_DEFPORT;
488: if (csHost) {
489: host = gethostbyname2(csHost, family);
490: if (!host) {
491: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
492: return NULL;
493: }
494: }
495: switch (family) {
496: case AF_INET:
497: memset(&sin, 0, sizeof sin);
498: sin.sin_len = sizeof sin;
499: sin.sin_family = family;
500: sin.sin_port = htons(Port);
501: if (csHost)
502: memcpy(&sin.sin_addr, host->h_addr, host->h_length);
503: break;
504: case AF_INET6:
505: memset(&sin6, 0, sizeof sin6);
506: sin6.sin6_len = sizeof sin6;
507: sin6.sin6_family = family;
508: sin6.sin6_port = htons(Port);
509: if (csHost)
510: memcpy(&sin6.sin6_addr, host->h_addr, host->h_length);
511: break;
512: default:
513: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
514: return NULL;
515: }
516:
517: srv = malloc(sizeof(rpc_srv_t));
518: if (!srv) {
519: LOGERR;
520: return NULL;
521: } else
522: memset(srv, 0, sizeof(rpc_srv_t));
523:
524: srv->srv_numcli = concurentClients;
525: srv->srv_session.sess_version = RPC_VERSION;
526: srv->srv_session.sess_program = regProgID;
527: srv->srv_session.sess_process = regProcID;
528:
1.1.1.1.2.2 misho 529: srv->srv_server.cli_tid = pthread_self();
1.1 misho 530: srv->srv_server.cli_parent = srv;
531: if (family == AF_INET)
532: memcpy(&srv->srv_server.cli_sa, &sin, sizeof srv->srv_server.cli_sa);
533: else
534: memcpy(&srv->srv_server.cli_sa, &sin6, sizeof srv->srv_server.cli_sa);
535: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
536: if (srv->srv_server.cli_sock == -1) {
537: LOGERR;
538: free(srv);
539: return NULL;
540: }
541: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
542: LOGERR;
543: close(srv->srv_server.cli_sock);
544: free(srv);
545: return NULL;
546: }
547: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa, sizeof srv->srv_server.cli_sa) == -1) {
548: LOGERR;
549: close(srv->srv_server.cli_sock);
550: free(srv);
551: return NULL;
552: }
553:
554: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
555: if (!srv->srv_clients) {
556: LOGERR;
557: close(srv->srv_server.cli_sock);
558: free(srv);
559: return NULL;
560: } else
561: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
562:
1.1.1.1.2.1 misho 563: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.1 misho 564: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 0);
565: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 0);
566: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 0);
1.1.1.1.2.1 misho 567:
1.1.1.1.2.4 misho 568: pthread_mutex_init(&srv->srv_mtx, NULL);
1.1 misho 569: return srv;
570: }
571:
572: /*
573: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
574: * @srv = RPC Server instance
575: * return: none
576: */
577: void
578: rpc_srv_endServer(rpc_srv_t * __restrict srv)
579: {
580: rpc_cli_t *c;
581: register int i;
582: rpc_func_t *f;
583:
584: if (!srv) {
585: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
586: return;
587: }
588:
1.1.1.1.2.4 misho 589: rpc_srv_endBLOBServer(srv);
1.1 misho 590:
591: for (i = 0, c = srv->srv_clients; i < srv->srv_numcli && c; i++, c++)
592: if (c->cli_sa.sa_family)
593: shutdown(c->cli_sock, SHUT_RDWR);
594: close(srv->srv_server.cli_sock);
595:
596: if (srv->srv_clients) {
597: free(srv->srv_clients);
598: srv->srv_numcli = 0;
599: }
600:
1.1.1.1.2.4 misho 601: pthread_mutex_lock(&srv->srv_mtx);
602: while ((f = srv->srv_funcs)) {
603: srv->srv_funcs = f->func_next;
604: free(f);
605: }
606: pthread_mutex_unlock(&srv->srv_mtx);
607:
608: pthread_mutex_destroy(&srv->srv_mtx);
609:
1.1 misho 610: free(srv);
611: srv = NULL;
612: }
613:
614: /*
615: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
616: * @srv = RPC Server instance
617: * return: -1 error or 0 ok, infinite loop ...
618: */
619: int
620: rpc_srv_execServer(rpc_srv_t * __restrict srv)
621: {
622: socklen_t salen = sizeof(struct sockaddr);
623: register int i;
624: rpc_cli_t *c;
1.1.1.1.2.2 misho 625: fd_set fds;
626: int ret;
627: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 628:
629: if (!srv) {
630: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
631: return -1;
632: }
633:
634: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
635: LOGERR;
636: return -1;
637: }
638:
1.1.1.1.2.1 misho 639: while (!rpc_Kill) {
1.1 misho 640: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
641: if (!c->cli_sa.sa_family)
642: break;
643: if (c && c->cli_sa.sa_family && c->cli_parent) {
644: usleep(1000000);
645: continue;
646: }
1.1.1.1.2.2 misho 647:
648: FD_ZERO(&fds);
649: FD_SET(srv->srv_server.cli_sock, &fds);
650: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
651: if (ret == -1) {
652: LOGERR;
653: ret = 1;
654: break;
655: }
656: if (!ret)
657: continue;
658:
1.1 misho 659: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
660: if (c->cli_sock == -1) {
661: LOGERR;
662: continue;
663: } else
664: c->cli_parent = srv;
665:
666: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
667: LOGERR;
668: continue;
669: }
670: }
671:
672: return 0;
673: }
674:
675: // ---------------------------------------------------------
676:
677: /*
678: * rpc_srv_execCall() Execute registered call from RPC server
679: * @data = RPC const data
680: * @call = Register RPC call
681: * @rpc = IN RPC call structure
682: * @args = IN RPC call array of rpc values
683: * return: -1 error, !=-1 ok
684: */
685: int
686: rpc_srv_execCall(void * const data, rpc_func_t * __restrict call,
687: struct tagRPCCall * __restrict rpc, rpc_val_t * __restrict args)
688: {
689: void *dl;
690: rpc_callback_t func;
691: int ret;
692:
693: if (!data || !call || !rpc) {
694: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
695: return -1;
696: }
697:
698: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
699: if (!dl) {
700: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
701: return -1;
702: }
703:
704: func = dlsym(dl, (char*) call->func_name);
705: if (func)
706: ret = func(data, call, rpc->call_argc, args);
707: else {
708: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
709: ret = -1;
710: }
711:
712: dlclose(dl);
713: return ret;
714: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>