Annotation of libaitrpc/src/srv.c, revision 1.1.1.1.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.1.1.1.2.5! misho 6: * $Id: srv.c,v 1.1.1.1.2.4 2010/06/23 11:33:38 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;
! 188: u_char buf[BLOBSIZ];
! 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: }
! 207: memset(buf, 0, BLOBSIZ);
! 208: if ((ret = recv(c->cli_sock, buf, BLOBSIZ, 0)) == -1) {
! 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:
! 238: ret = rpc_srv_sendBLOB(c, b);
! 239: break;
! 240: case set:
! 241: ret = rpc_srv_recvBLOB(c, b);
! 242: break;
! 243: case unset:
! 244: ret = rpc_srv_freeBLOB(b);
! 245: break;
! 246: default:
! 247: rpc_SetErr(EINVAL, "Error:: unsupported BLOB command (%d)...\n",
! 248: blob->hdr_cmd);
! 249: ret -7;
! 250: }
! 251: } while (ret > -1);
1.1.1.1.2.4 misho 252:
1.1.1.1.2.5! misho 253: shutdown(c->cli_sock, SHUT_RDWR);
! 254: close(c->cli_sock);
! 255: memset(c, 0, sizeof(rpc_cli_t));
1.1.1.1.2.4 misho 256: return (void*) ret;
257: }
258:
1.1 misho 259: // -------------------------------------------------
260:
261: /*
1.1.1.1.2.4 misho 262: * rpc_srv_initBLOBServer() Init & create BLOB Server
263: * @Port = Port for bind server, if Port == 0 default port is selected
1.1.1.1.2.5! misho 264: * @diskDir = Disk place for BLOB file objects
1.1.1.1.2.4 misho 265: * return: -1 == error or 0 bind and created BLOB server instance
266: */
267: int
1.1.1.1.2.5! misho 268: rpc_srv_initBLOBServer(rpc_srv_t * __restrict srv, u_short Port, const char *diskDir)
1.1.1.1.2.4 misho 269: {
270: int n = 1;
271: struct sockaddr_in sin;
272: struct sockaddr_in6 sin6;
273:
274: if (!srv) {
275: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init BLOB server ...\n");
276: return -1;
277: }
278: if (srv->srv_blob.state) {
279: rpc_SetErr(EPERM, "Warning:: Already started BLOB server!\n");
280: return 0;
281: }
282: if (!Port)
283: Port = RPC_DEFPORT + 1;
284:
285: memset(&srv->srv_blob, 0, sizeof srv->srv_blob);
1.1.1.1.2.5! misho 286: if (access(diskDir, R_OK | W_OK) == -1) {
! 287: LOGERR;
! 288: return -1;
! 289: } else
! 290: strlcpy(srv->srv_blob.dir, diskDir, UCHAR_MAX + 1);
! 291:
1.1.1.1.2.4 misho 292: srv->srv_blob.server.cli_tid = pthread_self();
293: srv->srv_blob.server.cli_parent = srv;
294: if (srv->srv_server.cli_sa.sa_family == AF_INET) {
295: memcpy(&sin, &srv->srv_server.cli_sa, sizeof sin);
296: sin.sin_port = htons(Port);
297: memcpy(&srv->srv_blob.server.cli_sa, &sin, sizeof(struct sockaddr));
298: } else {
299: memcpy(&sin6, &srv->srv_server.cli_sa, sizeof sin6);
300: sin6.sin6_port = htons(Port);
301: memcpy(&srv->srv_blob.server.cli_sa, &sin6, sizeof(struct sockaddr));
302: }
303:
304: srv->srv_blob.server.cli_sock = socket(srv->srv_server.cli_sa.sa_family, SOCK_STREAM, 0);
305: if (srv->srv_blob.server.cli_sock == -1) {
306: LOGERR;
307: return -1;
308: }
309: if (setsockopt(srv->srv_blob.server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
310: LOGERR;
311: close(srv->srv_blob.server.cli_sock);
312: return -1;
313: }
314: if (bind(srv->srv_blob.server.cli_sock, &srv->srv_blob.server.cli_sa,
315: sizeof srv->srv_blob.server.cli_sa) == -1) {
316: LOGERR;
317: close(srv->srv_blob.server.cli_sock);
318: return -1;
319: }
320:
321: srv->srv_blob.clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
322: if (!srv->srv_blob.clients) {
323: LOGERR;
324: close(srv->srv_blob.server.cli_sock);
325: return -1;
326: } else
327: memset(srv->srv_blob.clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
328:
329: pthread_mutex_init(&srv->srv_blob.mtx, NULL);
330:
331: pthread_mutex_lock(&srv->srv_mtx);
332: rpc_srv_registerCall(srv, NULL, CALL_BLOBSHUTDOWN, 0);
333: rpc_srv_registerCall(srv, NULL, CALL_BLOBCLIENTS, 0);
334: rpc_srv_registerCall(srv, NULL, CALL_BLOBVARS, 0);
335: pthread_mutex_unlock(&srv->srv_mtx);
336:
1.1.1.1.2.5! misho 337: srv->srv_blob.state = enable; // enable BLOB
1.1.1.1.2.4 misho 338: return 0;
339: }
340:
341: /*
342: * rpc_srv_endBLOBServer() Destroy BLOB server, close all opened sockets and free resources
343: * @srv = RPC Server instance
344: * return: none
345: */
346: void
347: rpc_srv_endBLOBServer(rpc_srv_t * __restrict srv)
348: {
349: rpc_cli_t *c;
350: register int i;
351: rpc_blob_t *f;
352:
353: if (!srv) {
354: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
355: return;
356: } else
1.1.1.1.2.5! misho 357: srv->srv_blob.state = disable;
1.1.1.1.2.4 misho 358:
359: for (i = 0, c = srv->srv_blob.clients; i < srv->srv_numcli && c; i++, c++)
360: if (c->cli_sa.sa_family)
361: shutdown(c->cli_sock, SHUT_RDWR);
362: close(srv->srv_blob.server.cli_sock);
363:
364: if (srv->srv_blob.clients)
365: free(srv->srv_blob.clients);
366:
367: pthread_mutex_lock(&srv->srv_blob.mtx);
368: while ((f = srv->srv_blob.blobs)) {
369: srv->srv_blob.blobs = f->blob_next;
370: free(f);
371: }
372: pthread_mutex_unlock(&srv->srv_blob.mtx);
373:
374: pthread_mutex_destroy(&srv->srv_blob.mtx);
375: }
376:
377: /*
378: * rpc_srv_execBLOBServer() Execute Main BLOB server loop and wait for clients requests
379: * @srv = RPC Server instance
380: * return: -1 error or 0 ok, infinite loop ...
381: */
382: int
383: rpc_srv_execBLOBServer(rpc_srv_t * __restrict srv)
384: {
385: socklen_t salen = sizeof(struct sockaddr);
386: register int i;
387: rpc_cli_t *c;
388: fd_set fds;
389: int ret;
390: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
391:
392: if (!srv || !srv->srv_blob.state) {
393: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start BLOB server ...\n");
394: return -1;
395: }
396:
397: if (listen(srv->srv_blob.server.cli_sock, SOMAXCONN) == -1) {
398: LOGERR;
399: return -1;
400: }
401:
402: while (!rpc_Kill) {
403: for (c = srv->srv_blob.clients, i = 0; i < srv->srv_numcli && c; i++, c++)
404: if (!c->cli_sa.sa_family)
405: break;
406: if (c && c->cli_sa.sa_family && c->cli_parent) {
407: usleep(1000000);
408: continue;
409: }
410:
411: FD_ZERO(&fds);
412: FD_SET(srv->srv_blob.server.cli_sock, &fds);
413: ret = select(srv->srv_blob.server.cli_sock + 1, &fds, NULL, NULL, &tv);
414: if (ret == -1) {
415: LOGERR;
416: ret = 1;
417: break;
418: }
419: if (!ret)
420: continue;
421:
422: c->cli_sock = accept(srv->srv_blob.server.cli_sock, &c->cli_sa, &salen);
423: if (c->cli_sock == -1) {
424: LOGERR;
425: continue;
426: } else
427: c->cli_parent = srv;
428:
429: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchVars, c)) {
430: LOGERR;
431: continue;
432: }
433: }
434:
435: return 0;
436: }
437:
438:
439: /*
1.1 misho 440: * rpc_srv_initServer() Init & create RPC Server
441: * @regProgID = ProgramID for authentication & recognition
442: * @regProcID = ProcessID for authentication & recognition
443: * @concurentClients = Concurent clients at same time to this server
444: * @family = Family socket type, AF_INET or AF_INET6
445: * @csHost = Host name or IP address for bind server, if NULL any address
446: * @Port = Port for bind server, if Port == 0 default port is selected
447: * return: NULL == error or !=NULL bind and created RPC server instance
448: */
449: rpc_srv_t *
450: rpc_srv_initServer(u_int regProgID, u_int regProcID, int concurentClients,
451: u_short family, const char *csHost, u_short Port)
452: {
453: rpc_srv_t *srv = NULL;
454: int n = 1;
455: struct hostent *host = NULL;
456: struct sockaddr_in sin;
457: struct sockaddr_in6 sin6;
458:
459: if (!concurentClients || !regProgID || (family != AF_INET && family != AF_INET6)) {
460: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t init RPC server ...\n");
461: return NULL;
462: }
463: if (!Port)
464: Port = RPC_DEFPORT;
465: if (csHost) {
466: host = gethostbyname2(csHost, family);
467: if (!host) {
468: rpc_SetErr(h_errno, "Error:: %s\n", hstrerror(h_errno));
469: return NULL;
470: }
471: }
472: switch (family) {
473: case AF_INET:
474: memset(&sin, 0, sizeof sin);
475: sin.sin_len = sizeof sin;
476: sin.sin_family = family;
477: sin.sin_port = htons(Port);
478: if (csHost)
479: memcpy(&sin.sin_addr, host->h_addr, host->h_length);
480: break;
481: case AF_INET6:
482: memset(&sin6, 0, sizeof sin6);
483: sin6.sin6_len = sizeof sin6;
484: sin6.sin6_family = family;
485: sin6.sin6_port = htons(Port);
486: if (csHost)
487: memcpy(&sin6.sin6_addr, host->h_addr, host->h_length);
488: break;
489: default:
490: rpc_SetErr(EINVAL, "Error:: Invalid parameters can`t start RPC server ...\n");
491: return NULL;
492: }
493:
494: srv = malloc(sizeof(rpc_srv_t));
495: if (!srv) {
496: LOGERR;
497: return NULL;
498: } else
499: memset(srv, 0, sizeof(rpc_srv_t));
500:
501: srv->srv_numcli = concurentClients;
502: srv->srv_session.sess_version = RPC_VERSION;
503: srv->srv_session.sess_program = regProgID;
504: srv->srv_session.sess_process = regProcID;
505:
1.1.1.1.2.2 misho 506: srv->srv_server.cli_tid = pthread_self();
1.1 misho 507: srv->srv_server.cli_parent = srv;
508: if (family == AF_INET)
509: memcpy(&srv->srv_server.cli_sa, &sin, sizeof srv->srv_server.cli_sa);
510: else
511: memcpy(&srv->srv_server.cli_sa, &sin6, sizeof srv->srv_server.cli_sa);
512: srv->srv_server.cli_sock = socket(family, SOCK_STREAM, 0);
513: if (srv->srv_server.cli_sock == -1) {
514: LOGERR;
515: free(srv);
516: return NULL;
517: }
518: if (setsockopt(srv->srv_server.cli_sock, SOL_SOCKET, SO_REUSEADDR, &n, sizeof n) == -1) {
519: LOGERR;
520: close(srv->srv_server.cli_sock);
521: free(srv);
522: return NULL;
523: }
524: if (bind(srv->srv_server.cli_sock, &srv->srv_server.cli_sa, sizeof srv->srv_server.cli_sa) == -1) {
525: LOGERR;
526: close(srv->srv_server.cli_sock);
527: free(srv);
528: return NULL;
529: }
530:
531: srv->srv_clients = calloc(srv->srv_numcli, sizeof(rpc_cli_t));
532: if (!srv->srv_clients) {
533: LOGERR;
534: close(srv->srv_server.cli_sock);
535: free(srv);
536: return NULL;
537: } else
538: memset(srv->srv_clients, 0, srv->srv_numcli * sizeof(rpc_cli_t));
539:
1.1.1.1.2.1 misho 540: rpc_srv_registerCall(srv, NULL, CALL_SRVSHUTDOWN, 0);
1.1 misho 541: rpc_srv_registerCall(srv, NULL, CALL_SRVCLIENTS, 0);
542: rpc_srv_registerCall(srv, NULL, CALL_SRVCALLS, 0);
543: rpc_srv_registerCall(srv, NULL, CALL_SRVSESSIONS, 0);
1.1.1.1.2.1 misho 544:
1.1.1.1.2.4 misho 545: pthread_mutex_init(&srv->srv_mtx, NULL);
1.1 misho 546: return srv;
547: }
548:
549: /*
550: * rpc_srv_endServer() Destroy RPC server, close all opened sockets and free resources
551: * @srv = RPC Server instance
552: * return: none
553: */
554: void
555: rpc_srv_endServer(rpc_srv_t * __restrict srv)
556: {
557: rpc_cli_t *c;
558: register int i;
559: rpc_func_t *f;
560:
561: if (!srv) {
562: rpc_SetErr(EINVAL, "Error:: Can`t destroy server because parameter is null!\n");
563: return;
564: }
565:
1.1.1.1.2.4 misho 566: rpc_srv_endBLOBServer(srv);
1.1 misho 567:
568: for (i = 0, c = srv->srv_clients; i < srv->srv_numcli && c; i++, c++)
569: if (c->cli_sa.sa_family)
570: shutdown(c->cli_sock, SHUT_RDWR);
571: close(srv->srv_server.cli_sock);
572:
573: if (srv->srv_clients) {
574: free(srv->srv_clients);
575: srv->srv_numcli = 0;
576: }
577:
1.1.1.1.2.4 misho 578: pthread_mutex_lock(&srv->srv_mtx);
579: while ((f = srv->srv_funcs)) {
580: srv->srv_funcs = f->func_next;
581: free(f);
582: }
583: pthread_mutex_unlock(&srv->srv_mtx);
584:
585: pthread_mutex_destroy(&srv->srv_mtx);
586:
1.1 misho 587: free(srv);
588: srv = NULL;
589: }
590:
591: /*
592: * rpc_srv_execServer() Execute Main server loop and wait for clients requests
593: * @srv = RPC Server instance
594: * return: -1 error or 0 ok, infinite loop ...
595: */
596: int
597: rpc_srv_execServer(rpc_srv_t * __restrict srv)
598: {
599: socklen_t salen = sizeof(struct sockaddr);
600: register int i;
601: rpc_cli_t *c;
1.1.1.1.2.2 misho 602: fd_set fds;
603: int ret;
604: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1 misho 605:
606: if (!srv) {
607: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t start RPC server ...\n");
608: return -1;
609: }
610:
611: if (listen(srv->srv_server.cli_sock, SOMAXCONN) == -1) {
612: LOGERR;
613: return -1;
614: }
615:
1.1.1.1.2.1 misho 616: while (!rpc_Kill) {
1.1 misho 617: for (c = srv->srv_clients, i = 0; i < srv->srv_numcli && c; i++, c++)
618: if (!c->cli_sa.sa_family)
619: break;
620: if (c && c->cli_sa.sa_family && c->cli_parent) {
621: usleep(1000000);
622: continue;
623: }
1.1.1.1.2.2 misho 624:
625: FD_ZERO(&fds);
626: FD_SET(srv->srv_server.cli_sock, &fds);
627: ret = select(srv->srv_server.cli_sock + 1, &fds, NULL, NULL, &tv);
628: if (ret == -1) {
629: LOGERR;
630: ret = 1;
631: break;
632: }
633: if (!ret)
634: continue;
635:
1.1 misho 636: c->cli_sock = accept(srv->srv_server.cli_sock, &c->cli_sa, &salen);
637: if (c->cli_sock == -1) {
638: LOGERR;
639: continue;
640: } else
641: c->cli_parent = srv;
642:
643: if (pthread_create(&c->cli_tid, NULL, rpc_srv_dispatchCall, c)) {
644: LOGERR;
645: continue;
646: }
647: }
648:
649: return 0;
650: }
651:
652: // ---------------------------------------------------------
653:
654: /*
655: * rpc_srv_execCall() Execute registered call from RPC server
656: * @data = RPC const data
657: * @call = Register RPC call
658: * @rpc = IN RPC call structure
659: * @args = IN RPC call array of rpc values
660: * return: -1 error, !=-1 ok
661: */
662: int
663: rpc_srv_execCall(void * const data, rpc_func_t * __restrict call,
664: struct tagRPCCall * __restrict rpc, rpc_val_t * __restrict args)
665: {
666: void *dl;
667: rpc_callback_t func;
668: int ret;
669:
670: if (!data || !call || !rpc) {
671: rpc_SetErr(EINVAL, "Error:: Invalid parameter can`t exec call from RPC server ...\n");
672: return -1;
673: }
674:
675: dl = dlopen((char*) (*call->func_file ? call->func_file : NULL), RTLD_NOW);
676: if (!dl) {
677: rpc_SetErr(ENOENT, "Error:: Can`t attach module %s!\n", dlerror());
678: return -1;
679: }
680:
681: func = dlsym(dl, (char*) call->func_name);
682: if (func)
683: ret = func(data, call, rpc->call_argc, args);
684: else {
685: rpc_SetErr(ENOEXEC, "Error:: Can`t find function %s!\n", dlerror());
686: ret = -1;
687: }
688:
689: dlclose(dl);
690: return ret;
691: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>