Annotation of libaitrpc/src/blob.c, revision 1.1.2.6
1.1.2.1 misho 1: #include "global.h"
2:
3:
1.1.2.2 misho 4: /*
1.1.2.4 misho 5: * rpc_srv_blobCreate() Create map blob to memory region and return object
6: * @srv = RPC Server instance
7: * @len = BLOB length object
8: * return: NULL error or !=NULL allocated BLOB object
9: */
10: inline rpc_blob_t *
11: rpc_srv_blobCreate(rpc_srv_t * __restrict srv, int len)
12: {
13: rpc_blob_t *blob = NULL;
14: char szFName[MAXPATHLEN];
15: int f;
16:
17: memset(szFName, 0, MAXPATHLEN);
18: snprintf(szFName, MAXPATHLEN, "%s/BLOB-XXXXXXXX", srv->srv_blob.dir);
19: f = mkstemp(szFName);
20: if (f == -1) {
21: LOGERR;
22: return NULL;
23: }
24: if (lseek(f, len - 1, SEEK_SET) == -1) {
25: LOGERR;
26: close(f);
27: unlink(szFName);
28: return NULL;
29: } else
30: write(f, "", 1);
31:
32: blob = malloc(sizeof(rpc_blob_t));
33: if (!blob) {
34: LOGERR;
35: close(f);
36: unlink(szFName);
37: return NULL;
38: }
39:
40: blob->blob_data = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, f, 0);
41: if (blob->blob_data == MAP_FAILED) {
42: LOGERR;
43: free(blob);
44: close(f);
45: unlink(szFName);
46: return NULL;
47: } else
48: close(f);
49:
50: blob->blob_len = len;
51: blob->blob_var = strtol(strrchr(szFName, '-') + 1, NULL, 16);
52: return blob;
53: }
54:
55: /*
1.1.2.2 misho 56: * rpc_srv_blobMap() Map blob to memory region
57: * @srv = RPC Server instance
58: * @blob = Map to this BLOB element
59: * return: -1 error or 0 ok
60: */
61: inline int
62: rpc_srv_blobMap(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob)
63: {
64: int f;
65: struct stat sb;
66: char szFName[MAXPATHLEN];
67:
68: if (!blob) {
69: rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
70: return -1;
71: }
72:
73: memset(szFName, 0, MAXPATHLEN);
74: snprintf(szFName, MAXPATHLEN, BLOB_FILE, srv->srv_blob.dir, blob->blob_var);
75: f = open(szFName, O_RDONLY);
76: if (f == -1) {
77: LOGERR;
78: return -1;
79: }
80: if (fstat(f, &sb) == -1) {
81: LOGERR;
82: close(f);
83: return -1;
84: }
85:
86: blob->blob_data = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, f, 0);
87: if (blob->blob_data == MAP_FAILED) {
88: LOGERR;
89: close(f);
90: blob->blob_data = NULL;
91: return -1;
92: } else {
93: blob->blob_len = sb.st_size;
94: close(f);
1.1.2.4 misho 95:
96: madvise(blob->blob_data, sb.st_size, MADV_SEQUENTIAL);
1.1.2.2 misho 97: }
98:
99: return 0;
100: }
101:
102: /*
103: * rpc_srv_blobUnmap() Unmap blob memory region
104: * @blob = Mapped BLOB element
105: * return: none
106: */
107: inline void
108: rpc_srv_blobUnmap(rpc_blob_t * __restrict blob)
109: {
110: if (!blob || !blob->blob_data)
111: rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
112: else {
113: munmap(blob->blob_data, blob->blob_len);
114: blob->blob_data = NULL;
115: blob->blob_len = 0;
116: }
117: }
118:
119: /*
120: * rpc_srv_blobFree() Free blob from disk & memory
121: * @srv = RPC Server instance
122: * @blob = Mapped BLOB element
123: * return: -1 error or 0 ok
124: */
125: inline int
126: rpc_srv_blobFree(rpc_srv_t * __restrict srv, rpc_blob_t * __restrict blob)
127: {
128: char szFName[MAXPATHLEN];
129:
130: if (!blob) {
131: rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
132: return -1;
133: }
134:
135: if (blob->blob_data)
136: rpc_srv_blobUnmap(blob);
137:
138: memset(szFName, 0, MAXPATHLEN);
139: snprintf(szFName, MAXPATHLEN, BLOB_FILE, srv->srv_blob.dir, blob->blob_var);
140: if (remove(szFName) == -1) {
141: LOGERR;
142: return -1;
143: }
144:
145: return 0;
146: }
147:
148: // ------------------------------------------------------------
149:
1.1.2.3 misho 150: /*
151: * rpc_srv_sendBLOB() Send mapped BLOB to client
152: * @cli = Client instance
153: * @blob = Mapped BLOB element
154: * return: -1 error, 0 ok
155: */
1.1.2.2 misho 156: int
1.1.2.3 misho 157: rpc_srv_sendBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob)
1.1.2.2 misho 158: {
1.1.2.3 misho 159: int ret, len;
160: uint8_t *pos;
1.1.2.2 misho 161:
1.1.2.3 misho 162: if (!cli || !blob || !blob->blob_data) {
1.1.2.2 misho 163: rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
164: return -1;
165: }
166:
1.1.2.3 misho 167: for (ret = blob->blob_len, pos = blob->blob_data; ret > 0; ret -= len, pos += len)
168: if ((len = send(cli->cli_sock, pos, ret > BLOBSIZ ? BLOBSIZ : ret, 0)) == -1) {
169: LOGERR;
170: return -1;
171: }
1.1.2.2 misho 172:
1.1.2.3 misho 173: return ret;
174: }
175:
176: /*
177: * rpc_srv_recvBLOB() Receive BLOB from client
178: * @cli = Client instance
1.1.2.4 misho 179: * @blob = Mapped BLOB element
180: * return: -1 error, 0 ok, >0 unreceived data from client, may be error?
1.1.2.3 misho 181: */
182: int
1.1.2.4 misho 183: rpc_srv_recvBLOB(rpc_cli_t * __restrict cli, rpc_blob_t * __restrict blob)
1.1.2.3 misho 184: {
185: int ret, len;
186: uint8_t *pos;
1.1.2.4 misho 187: fd_set fds;
188: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1.2.3 misho 189:
1.1.2.4 misho 190: if (!cli || !blob || !blob->blob_data) {
1.1.2.3 misho 191: rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
192: return -1;
1.1.2.4 misho 193: }
194:
195: for (ret = blob->blob_len, pos = blob->blob_data; ret > 0; ret -= len, pos += len) {
196: FD_ZERO(&fds);
197: FD_SET(cli->cli_sock, &fds);
198: len = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv);
199: if (len < 1) {
200: LOGERR;
201: return -1;
202: }
1.1.2.3 misho 203:
1.1.2.4 misho 204: if ((len = recv(cli->cli_sock, pos, BLOBSIZ, 0)) == -1) {
205: LOGERR;
206: return -1;
207: }
208: }
1.1.2.3 misho 209:
210: return ret;
1.1.2.2 misho 211: }
1.1.2.5 misho 212:
213: // ------------------------------------------------------------
214:
215: /*
216: * rpc_cli_sendBLOB() Send BLOB to server
217: * @cli = Client instance
218: * @var = BLOB variable
219: * @data = BLOB data
1.1.2.6 ! misho 220: * return: -1 error, 0 ok, 1 remote error
1.1.2.5 misho 221: */
222: int
223: rpc_cli_sendBLOB(rpc_cli_t * __restrict cli, rpc_val_t * __restrict var, void * __restrict data)
224: {
225: int ret, len;
226: uint8_t *pos;
1.1.2.6 ! misho 227: struct tagBLOBHdr hdr;
! 228: fd_set fds;
! 229: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
1.1.2.5 misho 230:
231: if (!cli || !var || !data) {
232: rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
233: return -1;
234: }
235:
1.1.2.6 ! misho 236: memcpy(&hdr.hdr_session, cli->cli_parent, sizeof(rpc_sess_t));
! 237: hdr.hdr_cmd = set;
! 238: hdr.hdr_var = (uint32_t) RPC_GET_BLOB(var);
! 239: hdr.hdr_seq = 0;
! 240: hdr.hdr_len = var->val_len;
! 241: if (send(cli->cli_sock, &hdr, sizeof hdr, 0) == -1) {
! 242: LOGERR;
! 243: return -1;
! 244: }
! 245:
1.1.2.5 misho 246: for (ret = var->val_len, pos = data; ret > 0; ret -= len, pos += len)
247: if ((len = send(cli->cli_sock, pos, ret > BLOBSIZ ? BLOBSIZ : ret, 0)) == -1) {
248: LOGERR;
249: return -1;
250: }
251:
1.1.2.6 ! misho 252: FD_ZERO(&fds);
! 253: FD_SET(cli->cli_sock, &fds);
! 254: switch (select(cli->cli_sock + 1, &fds, NULL, NULL, &tv)) {
! 255: case -1:
! 256: LOGERR;
! 257: return -1;
! 258: case 0:
! 259: rpc_SetErr(ETIMEDOUT, "Error:: Timeout reached! Server not responde ...\n");
! 260: return -1;
! 261: }
! 262: if (read(cli->cli_sock, &hdr, sizeof hdr) == -1) {
! 263: LOGERR;
! 264: return -1;
! 265: }
! 266:
! 267: return hdr.hdr_cmd == error;
1.1.2.5 misho 268: }
269:
270: /*
271: * rpc_cli_recvBLOB() Receive BLOB from server
272: * @cli = Client instance
273: * @var = BLOB variable
274: * @data = BLOB data, must be free after use!
275: * return: -1 error, 0 ok, >0 unreceived data from server, may be error?
276: */
277: int
278: rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, rpc_val_t * __restrict var, void ** data)
279: {
280: int ret, len;
281: uint8_t *pos;
282: fd_set fds;
283: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
284:
285: if (!cli || !var || !data) {
286: rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
287: return -1;
288: }
289:
290: *data = malloc(var->val_len);
291: if (!*data) {
292: LOGERR;
293: return -1;
294: } else
295: memset(*data, 0, var->val_len);
296:
297: for (ret = var->val_len, pos = *data; ret > 0; ret -= len, pos += len) {
298: FD_ZERO(&fds);
299: FD_SET(cli->cli_sock, &fds);
300: len = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv);
301: if (len < 1) {
302: LOGERR;
303: free(*data);
304: *data = NULL;
305: return -1;
306: }
307:
308: if ((len = recv(cli->cli_sock, pos, BLOBSIZ, 0)) == -1) {
309: LOGERR;
310: free(*data);
311: *data = NULL;
312: return -1;
313: }
314: }
315:
316: return ret;
317: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>