]> sourceware.org Git - lvm2.git/blame - daemons/common/daemon-shared.c
Update verbose lvs to print metadata_percent info
[lvm2.git] / daemons / common / daemon-shared.c
CommitLineData
92658f56
PR
1#include <errno.h>
2#include <stdio.h>
3#include <malloc.h>
4#include <string.h>
6e4e3082 5#include <unistd.h>
aac236f4 6#include <assert.h>
4fcbeed6 7#include "daemon-shared.h"
92658f56
PR
8
9/*
10 * Read a single message from a (socket) filedescriptor. Messages are delimited
11 * by blank lines. This call will block until all of a message is received. The
12 * memory will be allocated from heap. Upon error, all memory is freed and the
13 * buffer pointer is set to NULL.
4fcbeed6
PR
14 *
15 * See also write_buffer about blocking (read_buffer has identical behaviour).
92658f56
PR
16 */
17int read_buffer(int fd, char **buffer) {
18 int bytes = 0;
19 int buffersize = 32;
6d404585 20 char *new;
92658f56
PR
21 *buffer = malloc(buffersize + 1);
22
23 while (1) {
24 int result = read(fd, (*buffer) + bytes, buffersize - bytes);
25 if (result > 0)
26 bytes += result;
27 if (result == 0)
28 goto fail; /* we should never encounter EOF here */
29 if (result < 0 && errno != EAGAIN && errno != EWOULDBLOCK)
30 goto fail;
31
3556560b
PR
32 if ((!strncmp((*buffer) + bytes - 4, "\n##\n", 4))) {
33 *(*buffer + bytes - 4) = 0;
34 break; /* success, we have the full message now */
35 }
36
92658f56
PR
37 if (bytes == buffersize) {
38 buffersize += 1024;
6d404585 39 if (!(new = realloc(*buffer, buffersize + 1)))
92658f56 40 goto fail;
6d404585
ZK
41
42 *buffer = new;
92658f56 43 }
3556560b 44 /* TODO call select here if we encountered EAGAIN/EWOULDBLOCK */
92658f56
PR
45 }
46 return 1;
47fail:
48 free(*buffer);
49 *buffer = NULL;
50 return 0;
51}
52
53/*
54 * Write a buffer to a filedescriptor. Keep trying. Blocks (even on
55 * SOCK_NONBLOCK) until all of the write went through.
56 *
57 * TODO use select on EWOULDBLOCK/EAGAIN to avoid useless spinning
58 */
f43c89bf
PR
59int write_buffer(int fd, const char *buffer, int length) {
60 int done = 0;
92658f56 61 int written = 0;
f43c89bf 62write:
92658f56
PR
63 while (1) {
64 int result = write(fd, buffer + written, length - written);
65 if (result > 0)
66 written += result;
67 if (result < 0 && errno != EWOULDBLOCK && errno != EAGAIN)
f43c89bf
PR
68 return 0; /* too bad */
69 if (written == length) {
70 if (done)
71 return 1;
72 else
73 break; /* done */
74 }
92658f56 75 }
f43c89bf
PR
76 const char *terminate = "\n##\n";
77 buffer = terminate;
78 length = 4;
79 written = 0;
80 done = 1;
81 goto write;
92658f56 82}
4fcbeed6 83
14e01287 84char *format_buffer(const char *what, const char *id, va_list ap)
4fcbeed6
PR
85{
86 char *buffer, *old;
87 char *next;
aac236f4 88 int keylen;
4fcbeed6 89
14e01287 90 dm_asprintf(&buffer, "%s = \"%s\"\n", what, id);
4fcbeed6
PR
91 if (!buffer) goto fail;
92
6e4e3082 93 while ((next = va_arg(ap, char *))) {
4fcbeed6 94 old = buffer;
aac236f4
PR
95 assert(strchr(next, '='));
96 keylen = strchr(next, '=') - next;
97 if (strstr(next, "%d")) {
98 int value = va_arg(ap, int);
99 dm_asprintf(&buffer, "%s%.*s= %d\n", buffer, keylen, next, value);
100 dm_free(old);
101 } else if (strstr(next, "%s")) {
102 char *value = va_arg(ap, char *);
103 dm_asprintf(&buffer, "%s%.*s= \"%s\"\n", buffer, keylen, next, value);
104 dm_free(old);
105 } else if (strstr(next, "%b")) {
106 char *block = va_arg(ap, char *);
107 if (!block)
108 continue;
109 dm_asprintf(&buffer, "%s%.*s%s", buffer, keylen, next, block);
4fcbeed6 110 dm_free(old);
4fcbeed6
PR
111 } else {
112 dm_asprintf(&buffer, "%s%s", buffer, next);
113 dm_free(old);
4fcbeed6 114 }
aac236f4 115 if (!buffer) goto fail;
4fcbeed6
PR
116 }
117
4fcbeed6
PR
118 return buffer;
119fail:
120 dm_free(buffer);
121 return NULL;
122}
This page took 0.040711 seconds and 5 git commands to generate.