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