File:
[ELWIX - Embedded LightWeight unIX -] /
libaitrpc /
src /
blob.c
Revision
1.14:
download - view:
text,
annotated -
select for diffs -
revision graph
Tue Jul 16 13:04:20 2013 UTC (11 years, 1 month ago) by
misho
Branches:
MAIN
CVS tags:
rpc6_8,
rpc6_7,
rpc6_6,
rpc6_5,
rpc6_4,
rpc6_3,
rpc6_2,
rpc6_1,
rpc6_0,
rpc5_5,
rpc5_4,
RPC6_7,
RPC6_6,
RPC6_5,
RPC6_4,
RPC6_3,
RPC6_2,
RPC6_1,
RPC6_0,
RPC5_4,
RPC5_3,
HEAD
version 5.3
1: /*************************************************************************
2: * (C) 2010 AITNET ltd - Sofia/Bulgaria - <misho@aitbg.com>
3: * by Michael Pounov <misho@openbsd-bg.org>
4: *
5: * $Author: misho $
6: * $Id: blob.c,v 1.14 2013/07/16 13:04:20 misho Exp $
7: *
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:
15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013
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: */
46: #include "global.h"
47:
48:
49: static void *
50: toutBLOB(sched_task_t *task)
51: {
52: rpc_srv_unregisterBLOB(TASK_DATA(task), ((rpc_blob_t*) TASK_ARG(task))->blob_var);
53:
54: return NULL;
55: }
56:
57:
58: /*
59: * rpc_srv_blobCreate() - Create and map blob to memory region and return object
60: *
61: * @srv = RPC Server instance
62: * @len = BLOB length object
63: * @tout = BLOB live timeout in seconds
64: * return: NULL error or !=NULL allocated BLOB object
65: */
66: rpc_blob_t *
67: rpc_srv_blobCreate(rpc_srv_t * __restrict srv, int len, int tout)
68: {
69: rpc_blob_t *blob = NULL;
70: char szFName[MAXPATHLEN];
71: int f;
72: u_int rnd;
73: struct timespec ts = { tout ? tout : RPC_BLOB_TIMEOUT, 0 };
74:
75: again:
76: rnd = random() % UINT_MAX;
77:
78: memset(szFName, 0, sizeof szFName);
79: snprintf(szFName, sizeof szFName, BLOB_FILE, AIT_GET_STRZ(&srv->srv_blob.dir), rnd);
80: f = open(szFName, O_CREAT | O_EXCL | O_RDWR, 0600);
81: if (f == -1) {
82: if (errno == EEXIST)
83: goto again;
84:
85: LOGERR;
86: return NULL;
87: }
88: if (ftruncate(f, len) == -1) {
89: LOGERR;
90: close(f);
91: unlink(szFName);
92: return NULL;
93: }
94:
95: blob = e_malloc(sizeof(rpc_blob_t));
96: if (!blob) {
97: LOGERR;
98: close(f);
99: unlink(szFName);
100: return NULL;
101: }
102:
103: blob->blob_data = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, f, 0);
104: if (blob->blob_data == MAP_FAILED) {
105: LOGERR;
106: e_free(blob);
107: close(f);
108: unlink(szFName);
109: return NULL;
110: } else
111: close(f);
112:
113: blob->blob_len = len;
114: blob->blob_var = rnd;
115:
116: schedTimer(srv->srv_blob.root, toutBLOB, blob, ts, srv, 0);
117: return blob;
118: }
119:
120: /*
121: * rpc_srv_blobMap() - Map blob to memory region
122: *
123: * @srv = RPC Server instance
124: * @blob = Map to this BLOB element
125: * return: -1 error or 0 ok
126: */
127: int
128: rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob)
129: {
130: int f;
131: char szFName[MAXPATHLEN];
132:
133: if (!blob) {
134: rpc_SetErr(EINVAL, "Invalid argument BLOB");
135: return -1;
136: }
137: if (blob->blob_data) {
138: rpc_SetErr(EPERM, "Already mmapped object found!");
139: return -1;
140: }
141:
142: memset(szFName, 0, sizeof szFName);
143: snprintf(szFName, sizeof szFName, BLOB_FILE, AIT_GET_STRZ(&srv->srv_blob.dir), blob->blob_var);
144: f = open(szFName, O_RDWR);
145: if (f == -1) {
146: LOGERR;
147: return -1;
148: }
149:
150: blob->blob_data = mmap(NULL, blob->blob_len, PROT_READ | PROT_WRITE, MAP_SHARED, f, 0);
151: if (blob->blob_data == MAP_FAILED) {
152: LOGERR;
153: close(f);
154: blob->blob_data = NULL;
155: return -1;
156: } else {
157: close(f);
158:
159: madvise(blob->blob_data, blob->blob_len, MADV_SEQUENTIAL);
160: }
161:
162: return 0;
163: }
164:
165: /*
166: * rpc_srv_blobUnmap() - Unmap blob memory region
167: *
168: * @blob = Mapped BLOB element
169: * return: none
170: */
171: void
172: rpc_srv_blobUnmap(rpc_blob_t * __restrict blob)
173: {
174: if (blob && blob->blob_data) {
175: munmap(blob->blob_data, blob->blob_len);
176: blob->blob_data = NULL;
177: }
178: }
179:
180: /*
181: * rpc_srv_blobFree() - Free blob from disk & memory
182: *
183: * @srv = RPC Server instance
184: * @blob = Mapped BLOB element
185: * return: -1 error or 0 ok
186: */
187: int
188: rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob)
189: {
190: char szFName[MAXPATHLEN];
191:
192: if (!blob) {
193: rpc_SetErr(EINVAL, "Invalid argument BLOB");
194: return -1;
195: } else
196: rpc_srv_blobUnmap(blob);
197:
198: schedCancelby(srv->srv_blob.root, taskTIMER, CRITERIA_ARG, blob, NULL);
199:
200: memset(szFName, 0, sizeof szFName);
201: snprintf(szFName, sizeof szFName, BLOB_FILE, AIT_GET_STRZ(&srv->srv_blob.dir), blob->blob_var);
202: if (unlink(szFName) == -1) {
203: LOGERR;
204: return -1;
205: }
206:
207: return 0;
208: }
209:
210: /* ------------------------------------------------------------ */
211:
212: /*
213: * rpc_srv_sendBLOB() - Send mapped BLOB to client
214: *
215: * @cli = Client instance
216: * @blob = Mapped BLOB element
217: * return: -1 error, 0 ok
218: */
219: int
220: rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob)
221: {
222: int ret, len;
223: uint8_t *pos;
224:
225: if (!cli || !blob || !blob->blob_data) {
226: rpc_SetErr(EINVAL, "Invalid arguments");
227: return -1;
228: }
229:
230: for (ret = blob->blob_len, pos = blob->blob_data; ret > 0; ret -= len, pos += len) {
231: len = send(cli->cli_sock, pos, ret, MSG_NOSIGNAL);
232: if (len == -1) {
233: LOGERR;
234: return -1;
235: }
236: }
237:
238: return ret;
239: }
240:
241: /*
242: * rpc_srv_recvBLOB() - Receive BLOB from client
243: *
244: * @cli = Client instance
245: * @blob = Mapped BLOB element
246: * return: -1 error, 0 ok, >0 unreceived data from client, may be error?
247: */
248: int
249: rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob)
250: {
251: int ret, len;
252: uint8_t *pos;
253: struct pollfd pfd;
254:
255: if (!cli || !blob || !blob->blob_data) {
256: rpc_SetErr(EINVAL, "Invalid arguments");
257: return -1;
258: }
259:
260: pfd.fd = cli->cli_sock;
261: pfd.events = POLLIN | POLLPRI;
262: for (ret = blob->blob_len, pos = blob->blob_data; ret > 0; ret -= len, pos += len) {
263: if ((len = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
264: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
265: if (len)
266: LOGERR;
267: else
268: rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
269: return -1;
270: }
271:
272: len = recv(cli->cli_sock, pos, ret, 0);
273: if (len == -1) {
274: LOGERR;
275: return -1;
276: }
277: }
278:
279: return ret;
280: }
281:
282: /* ------------------------------------------------------------ */
283:
284: /*
285: * rpc_cli_sendBLOB() - Send BLOB to server
286: *
287: * @cli = Client instance
288: * @var = BLOB variable
289: * @data = BLOB data
290: * @tout = BLOB live on server timeout in seconds, if =0 default timeout
291: * return: -1 error, 0 ok, 1 remote error
292: */
293: int
294: rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var,
295: void * __restrict data, int tout)
296: {
297: int ret, len;
298: uint8_t *pos;
299: struct tagBLOBHdr hdr;
300: struct pollfd pfd;
301:
302: if (!cli || !var || !data) {
303: rpc_SetErr(EINVAL, "Invalid arguments");
304: return -1;
305: } else
306: memset(&hdr, 0, sizeof hdr);
307:
308: rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
309: hdr.hdr_cmd = set;
310: hdr.hdr_var = 0;
311: hdr.hdr_ret = tout;
312: hdr.hdr_len = htonl(AIT_LEN(var));
313:
314: /* send SET request */
315: pfd.fd = cli->cli_sock;
316: pfd.events = POLLOUT;
317: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) == -1 ||
318: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
319: LOGERR;
320: return -1;
321: }
322: if (send(cli->cli_sock, &hdr, sizeof hdr, MSG_NOSIGNAL) == -1) {
323: LOGERR;
324: return -1;
325: }
326:
327: /* send BLOB to server */
328: for (ret = AIT_LEN(var), pos = data; ret > 0; ret -= len, pos += len)
329: if ((len = send(cli->cli_sock, pos, ret, MSG_NOSIGNAL)) == -1) {
330: LOGERR;
331: return -1;
332: }
333:
334: /* wait for reply */
335: pfd.events = POLLIN | POLLPRI;
336: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
337: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
338: if (ret)
339: LOGERR;
340: else
341: rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
342: return -1;
343: }
344: if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
345: LOGERR;
346: return 1;
347: }
348:
349: if (hdr.hdr_cmd != error) {
350: AIT_SET_BLOB(var, ntohl(hdr.hdr_var), ntohl(hdr.hdr_len));
351:
352: if (ntohl(hdr.hdr_len) != AIT_LEN(var)) {
353: rpc_cli_delBLOB(cli, var);
354: AIT_NEW_BLOB(var, ntohl(hdr.hdr_len));
355:
356: rpc_SetErr(ECANCELED, "Bad return length packet");
357: return 1;
358: }
359: }
360:
361: return hdr.hdr_cmd == error;
362: }
363:
364: /*
365: * rpc_cli_recvBLOB() - Receive BLOB from server
366: *
367: * @cli = Client instance
368: * @var = BLOB variable
369: * @data = BLOB data, must be e_free after use!
370: * return: -1 error, 0 ok, 1 remote error
371: */
372: int
373: rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var, void ** __restrict data)
374: {
375: int ret, len;
376: uint8_t *pos;
377: struct pollfd pfd;
378: struct tagBLOBHdr hdr;
379:
380: if (!cli || !var || !data) {
381: rpc_SetErr(EINVAL, "Invalid arguments");
382: return -1;
383: }
384:
385: *data = e_malloc(AIT_LEN(var));
386: if (!*data) {
387: LOGERR;
388: return -1;
389: } else {
390: memset(&hdr, 0, sizeof hdr);
391: memset(*data, 0, AIT_LEN(var));
392: }
393:
394: rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
395: hdr.hdr_cmd = get;
396: hdr.hdr_var = htonl((uint32_t) AIT_GET_BLOB(var));
397: hdr.hdr_ret = 0;
398: hdr.hdr_len = 0;
399:
400: /* send GET request */
401: pfd.fd = cli->cli_sock;
402: pfd.events = POLLOUT;
403: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) == -1 ||
404: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
405: LOGERR;
406: e_free(*data);
407: *data = NULL;
408: return -1;
409: }
410: if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
411: LOGERR;
412: e_free(*data);
413: *data = NULL;
414: return -1;
415: }
416:
417: /* receive BLOB from server */
418: pfd.events = POLLIN | POLLPRI;
419: for (ret = AIT_LEN(var), pos = *data; ret > 0; ret -= len, pos += len) {
420: if ((len = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
421: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
422: LOGERR;
423: e_free(*data);
424: *data = NULL;
425: return -1;
426: }
427:
428: if ((len = recv(cli->cli_sock, pos, ret, 0)) == -1) {
429: LOGERR;
430: e_free(*data);
431: *data = NULL;
432: return -1;
433: }
434: }
435:
436: /* wait for reply */
437: if ((len = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
438: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
439: if (len)
440: LOGERR;
441: else
442: rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
443: e_free(*data);
444: *data = NULL;
445: return 1;
446: }
447: if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
448: LOGERR;
449: e_free(*data);
450: *data = NULL;
451: return 1;
452: }
453: if (hdr.hdr_cmd != error) {
454: if (ntohl(hdr.hdr_len) != AIT_LEN(var)) {
455: rpc_SetErr(ECANCELED, "Bad return length packet");
456: e_free(*data);
457: *data = NULL;
458: return 1;
459: }
460: }
461:
462: return hdr.hdr_cmd == error;
463: }
464:
465: /*
466: * rpc_cli_delBLOB() - Delete BLOB from server
467: *
468: * @cli = Client instance
469: * @var = BLOB variable
470: * return: -1 error, 0 ok, 1 remote error
471: */
472: int
473: rpc_cli_delBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var)
474: {
475: struct tagBLOBHdr hdr;
476: struct pollfd pfd;
477: int ret;
478:
479: if (!cli || !var) {
480: rpc_SetErr(EINVAL, "Invalid arguments");
481: return -1;
482: } else
483: memset(&hdr, 0, sizeof hdr);
484:
485: rpc_addPktSession(&hdr.hdr_session, cli->cli_parent);
486: hdr.hdr_cmd = unset;
487: hdr.hdr_var = htonl((uint32_t) AIT_GET_BLOB(var));
488: hdr.hdr_ret = 0;
489: hdr.hdr_len = 0;
490:
491: /* send UNSET request */
492: pfd.fd = cli->cli_sock;
493: pfd.events = POLLOUT;
494: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) == -1 ||
495: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
496: LOGERR;
497: return -1;
498: }
499: if (send(cli->cli_sock, &hdr, sizeof hdr, MSG_NOSIGNAL) == -1) {
500: LOGERR;
501: return -1;
502: }
503:
504: /* wait for reply */
505: pfd.events = POLLIN | POLLPRI;
506: if ((ret = poll(&pfd, 1, DEF_RPC_TIMEOUT * 1000)) < 1 ||
507: pfd.revents & (POLLERR | POLLHUP | POLLNVAL)) {
508: if (ret)
509: LOGERR;
510: else
511: rpc_SetErr(ETIMEDOUT, "Timeout reached! Server not respond");
512: return 1;
513: }
514: if (recv(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
515: LOGERR;
516: return 1;
517: }
518:
519: return hdr.hdr_cmd == error;
520: }
521:
522: /*
523: * rpc_cli_getBLOB() - Receive BLOB from server and Delete after that
524: *
525: * @cli = Client instance
526: * @var = BLOB variable
527: * @data = BLOB data, must be e_free after use!
528: * return: -1 error, 0 ok, 1 remote error
529: */
530: int
531: rpc_cli_getBLOB(rpc_cli_t * __restrict cli, ait_val_t * __restrict var, void ** __restrict data)
532: {
533: int ret;
534:
535: ret = rpc_cli_recvBLOB(cli, var, data);
536: ret |= rpc_cli_delBLOB(cli, var) > 0 ? 2 : 0;
537:
538: return ret;
539: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>