ia64/xen-unstable

view tools/xenstore/xs_tdb_dump.c @ 7238:971e7c7411b3

Raise an exception if an error appears on the pipes to our children, and make
sure that the child's pipes are closed even under that exception. Move the
handling of POLLHUP to the end of the loop, so that we guarantee to read any
remaining data from the child if POLLHUP and POLLIN appear at the same time.

Signed-off-by: Ewan Mellor <ewan@xensource.com>
author emellor@ewan
date Thu Oct 06 10:13:11 2005 +0100 (2005-10-06)
parents ef9591d03fdd
children 93e27f7ca8a8 61b3b357d827
line source
1 /* Simple program to dump out all records of TDB */
2 #include <stdint.h>
3 #include <stdlib.h>
4 #include <fcntl.h>
5 #include <stdio.h>
6 #include <stdarg.h>
8 #include "xs_lib.h"
9 #include "tdb.h"
10 #include "talloc.h"
11 #include "utils.h"
13 struct record_hdr {
14 u32 num_perms;
15 u32 datalen;
16 u32 childlen;
17 struct xs_permissions perms[0];
18 };
20 static u32 total_size(struct record_hdr *hdr)
21 {
22 return sizeof(*hdr) + hdr->num_perms * sizeof(struct xs_permissions)
23 + hdr->datalen + hdr->childlen;
24 }
26 static char perm_to_char(enum xs_perm_type perm)
27 {
28 return perm == XS_PERM_READ ? 'r' :
29 perm == XS_PERM_WRITE ? 'w' :
30 perm == XS_PERM_NONE ? '-' :
31 perm == (XS_PERM_READ|XS_PERM_WRITE) ? 'b' :
32 '?';
33 }
35 int main(int argc, char *argv[])
36 {
37 TDB_DATA key;
38 TDB_CONTEXT *tdb;
40 if (argc != 2)
41 barf("Usage: xs_tdb_dump <tdbfile>");
43 tdb = tdb_open(talloc_strdup(NULL, argv[1]), 0, 0, O_RDONLY, 0);
44 if (!tdb)
45 barf_perror("Could not open %s", argv[1]);
47 key = tdb_firstkey(tdb);
48 while (key.dptr) {
49 TDB_DATA data;
50 struct record_hdr *hdr;
52 data = tdb_fetch(tdb, key);
53 hdr = (void *)data.dptr;
54 if (data.dsize < sizeof(*hdr))
55 fprintf(stderr, "%.*s: BAD truncated\n",
56 key.dsize, key.dptr);
57 else if (data.dsize != total_size(hdr))
58 fprintf(stderr, "%.*s: BAD length %i for %i/%i/%i (%i)\n",
59 key.dsize, key.dptr, data.dsize,
60 hdr->num_perms, hdr->datalen,
61 hdr->childlen, total_size(hdr));
62 else {
63 unsigned int i;
64 char *p;
66 printf("%.*s: ", key.dsize, key.dptr);
67 for (i = 0; i < hdr->num_perms; i++)
68 printf("%s%c%i",
69 i == 0 ? "" : ",",
70 perm_to_char(hdr->perms[i].perms),
71 hdr->perms[i].id);
72 p = (void *)&hdr->perms[hdr->num_perms];
73 printf(" %.*s\n", hdr->datalen, p);
74 p += hdr->datalen;
75 for (i = 0; i < hdr->childlen; i += strlen(p+i)+1)
76 printf("\t-> %s\n", p+i);
77 }
78 key = tdb_nextkey(tdb, key);
79 }
80 return 0;
81 }