Annotation of libaitrpc/src/blob.c, revision 1.1.2.5
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
! 220: * return: -1 error, 0 ok
! 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;
! 227:
! 228: if (!cli || !var || !data) {
! 229: rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
! 230: return -1;
! 231: }
! 232:
! 233: for (ret = var->val_len, pos = data; ret > 0; ret -= len, pos += len)
! 234: if ((len = send(cli->cli_sock, pos, ret > BLOBSIZ ? BLOBSIZ : ret, 0)) == -1) {
! 235: LOGERR;
! 236: return -1;
! 237: }
! 238:
! 239: return ret;
! 240: }
! 241:
! 242: /*
! 243: * rpc_cli_recvBLOB() Receive BLOB from server
! 244: * @cli = Client instance
! 245: * @var = BLOB variable
! 246: * @data = BLOB data, must be free after use!
! 247: * return: -1 error, 0 ok, >0 unreceived data from server, may be error?
! 248: */
! 249: int
! 250: rpc_cli_recvBLOB(rpc_cli_t * __restrict cli, rpc_val_t * __restrict var, void ** data)
! 251: {
! 252: int ret, len;
! 253: uint8_t *pos;
! 254: fd_set fds;
! 255: struct timeval tv = { DEF_RPC_TIMEOUT, 0 };
! 256:
! 257: if (!cli || !var || !data) {
! 258: rpc_SetErr(EINVAL, "Error:: invalid arguments ...\n");
! 259: return -1;
! 260: }
! 261:
! 262: *data = malloc(var->val_len);
! 263: if (!*data) {
! 264: LOGERR;
! 265: return -1;
! 266: } else
! 267: memset(*data, 0, var->val_len);
! 268:
! 269: for (ret = var->val_len, pos = *data; ret > 0; ret -= len, pos += len) {
! 270: FD_ZERO(&fds);
! 271: FD_SET(cli->cli_sock, &fds);
! 272: len = select(cli->cli_sock + 1, &fds, NULL, NULL, &tv);
! 273: if (len < 1) {
! 274: LOGERR;
! 275: free(*data);
! 276: *data = NULL;
! 277: return -1;
! 278: }
! 279:
! 280: if ((len = recv(cli->cli_sock, pos, BLOBSIZ, 0)) == -1) {
! 281: LOGERR;
! 282: free(*data);
! 283: *data = NULL;
! 284: return -1;
! 285: }
! 286: }
! 287:
! 288: return ret;
! 289: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>